raid5-ppl: PPL support for disks with write-back cache enabled
[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 struct md_rdev *md_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 EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
725
726 static struct md_personality *find_pers(int level, char *clevel)
727 {
728         struct md_personality *pers;
729         list_for_each_entry(pers, &pers_list, list) {
730                 if (level != LEVEL_NONE && pers->level == level)
731                         return pers;
732                 if (strcmp(pers->name, clevel)==0)
733                         return pers;
734         }
735         return NULL;
736 }
737
738 /* return the offset of the super block in 512byte sectors */
739 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
740 {
741         sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
742         return MD_NEW_SIZE_SECTORS(num_sectors);
743 }
744
745 static int alloc_disk_sb(struct md_rdev *rdev)
746 {
747         rdev->sb_page = alloc_page(GFP_KERNEL);
748         if (!rdev->sb_page)
749                 return -ENOMEM;
750         return 0;
751 }
752
753 void md_rdev_clear(struct md_rdev *rdev)
754 {
755         if (rdev->sb_page) {
756                 put_page(rdev->sb_page);
757                 rdev->sb_loaded = 0;
758                 rdev->sb_page = NULL;
759                 rdev->sb_start = 0;
760                 rdev->sectors = 0;
761         }
762         if (rdev->bb_page) {
763                 put_page(rdev->bb_page);
764                 rdev->bb_page = NULL;
765         }
766         badblocks_exit(&rdev->badblocks);
767 }
768 EXPORT_SYMBOL_GPL(md_rdev_clear);
769
770 static void super_written(struct bio *bio)
771 {
772         struct md_rdev *rdev = bio->bi_private;
773         struct mddev *mddev = rdev->mddev;
774
775         if (bio->bi_status) {
776                 pr_err("md: super_written gets error=%d\n", bio->bi_status);
777                 md_error(mddev, rdev);
778                 if (!test_bit(Faulty, &rdev->flags)
779                     && (bio->bi_opf & MD_FAILFAST)) {
780                         set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
781                         set_bit(LastDev, &rdev->flags);
782                 }
783         } else
784                 clear_bit(LastDev, &rdev->flags);
785
786         if (atomic_dec_and_test(&mddev->pending_writes))
787                 wake_up(&mddev->sb_wait);
788         rdev_dec_pending(rdev, mddev);
789         bio_put(bio);
790 }
791
792 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
793                    sector_t sector, int size, struct page *page)
794 {
795         /* write first size bytes of page to sector of rdev
796          * Increment mddev->pending_writes before returning
797          * and decrement it on completion, waking up sb_wait
798          * if zero is reached.
799          * If an error occurred, call md_error
800          */
801         struct bio *bio;
802         int ff = 0;
803
804         if (test_bit(Faulty, &rdev->flags))
805                 return;
806
807         bio = md_bio_alloc_sync(mddev);
808
809         atomic_inc(&rdev->nr_pending);
810
811         bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
812         bio->bi_iter.bi_sector = sector;
813         bio_add_page(bio, page, size, 0);
814         bio->bi_private = rdev;
815         bio->bi_end_io = super_written;
816
817         if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
818             test_bit(FailFast, &rdev->flags) &&
819             !test_bit(LastDev, &rdev->flags))
820                 ff = MD_FAILFAST;
821         bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
822
823         atomic_inc(&mddev->pending_writes);
824         submit_bio(bio);
825 }
826
827 int md_super_wait(struct mddev *mddev)
828 {
829         /* wait for all superblock writes that were scheduled to complete */
830         wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
831         if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
832                 return -EAGAIN;
833         return 0;
834 }
835
836 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
837                  struct page *page, int op, int op_flags, bool metadata_op)
838 {
839         struct bio *bio = md_bio_alloc_sync(rdev->mddev);
840         int ret;
841
842         if (metadata_op && rdev->meta_bdev)
843                 bio_set_dev(bio, rdev->meta_bdev);
844         else
845                 bio_set_dev(bio, rdev->bdev);
846         bio_set_op_attrs(bio, op, op_flags);
847         if (metadata_op)
848                 bio->bi_iter.bi_sector = sector + rdev->sb_start;
849         else if (rdev->mddev->reshape_position != MaxSector &&
850                  (rdev->mddev->reshape_backwards ==
851                   (sector >= rdev->mddev->reshape_position)))
852                 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
853         else
854                 bio->bi_iter.bi_sector = sector + rdev->data_offset;
855         bio_add_page(bio, page, size, 0);
856
857         submit_bio_wait(bio);
858
859         ret = !bio->bi_status;
860         bio_put(bio);
861         return ret;
862 }
863 EXPORT_SYMBOL_GPL(sync_page_io);
864
865 static int read_disk_sb(struct md_rdev *rdev, int size)
866 {
867         char b[BDEVNAME_SIZE];
868
869         if (rdev->sb_loaded)
870                 return 0;
871
872         if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
873                 goto fail;
874         rdev->sb_loaded = 1;
875         return 0;
876
877 fail:
878         pr_err("md: disabled device %s, could not read superblock.\n",
879                bdevname(rdev->bdev,b));
880         return -EINVAL;
881 }
882
883 static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
884 {
885         return  sb1->set_uuid0 == sb2->set_uuid0 &&
886                 sb1->set_uuid1 == sb2->set_uuid1 &&
887                 sb1->set_uuid2 == sb2->set_uuid2 &&
888                 sb1->set_uuid3 == sb2->set_uuid3;
889 }
890
891 static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
892 {
893         int ret;
894         mdp_super_t *tmp1, *tmp2;
895
896         tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
897         tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
898
899         if (!tmp1 || !tmp2) {
900                 ret = 0;
901                 goto abort;
902         }
903
904         *tmp1 = *sb1;
905         *tmp2 = *sb2;
906
907         /*
908          * nr_disks is not constant
909          */
910         tmp1->nr_disks = 0;
911         tmp2->nr_disks = 0;
912
913         ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
914 abort:
915         kfree(tmp1);
916         kfree(tmp2);
917         return ret;
918 }
919
920 static u32 md_csum_fold(u32 csum)
921 {
922         csum = (csum & 0xffff) + (csum >> 16);
923         return (csum & 0xffff) + (csum >> 16);
924 }
925
926 static unsigned int calc_sb_csum(mdp_super_t *sb)
927 {
928         u64 newcsum = 0;
929         u32 *sb32 = (u32*)sb;
930         int i;
931         unsigned int disk_csum, csum;
932
933         disk_csum = sb->sb_csum;
934         sb->sb_csum = 0;
935
936         for (i = 0; i < MD_SB_BYTES/4 ; i++)
937                 newcsum += sb32[i];
938         csum = (newcsum & 0xffffffff) + (newcsum>>32);
939
940 #ifdef CONFIG_ALPHA
941         /* This used to use csum_partial, which was wrong for several
942          * reasons including that different results are returned on
943          * different architectures.  It isn't critical that we get exactly
944          * the same return value as before (we always csum_fold before
945          * testing, and that removes any differences).  However as we
946          * know that csum_partial always returned a 16bit value on
947          * alphas, do a fold to maximise conformity to previous behaviour.
948          */
949         sb->sb_csum = md_csum_fold(disk_csum);
950 #else
951         sb->sb_csum = disk_csum;
952 #endif
953         return csum;
954 }
955
956 /*
957  * Handle superblock details.
958  * We want to be able to handle multiple superblock formats
959  * so we have a common interface to them all, and an array of
960  * different handlers.
961  * We rely on user-space to write the initial superblock, and support
962  * reading and updating of superblocks.
963  * Interface methods are:
964  *   int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
965  *      loads and validates a superblock on dev.
966  *      if refdev != NULL, compare superblocks on both devices
967  *    Return:
968  *      0 - dev has a superblock that is compatible with refdev
969  *      1 - dev has a superblock that is compatible and newer than refdev
970  *          so dev should be used as the refdev in future
971  *     -EINVAL superblock incompatible or invalid
972  *     -othererror e.g. -EIO
973  *
974  *   int validate_super(struct mddev *mddev, struct md_rdev *dev)
975  *      Verify that dev is acceptable into mddev.
976  *       The first time, mddev->raid_disks will be 0, and data from
977  *       dev should be merged in.  Subsequent calls check that dev
978  *       is new enough.  Return 0 or -EINVAL
979  *
980  *   void sync_super(struct mddev *mddev, struct md_rdev *dev)
981  *     Update the superblock for rdev with data in mddev
982  *     This does not write to disc.
983  *
984  */
985
986 struct super_type  {
987         char                *name;
988         struct module       *owner;
989         int                 (*load_super)(struct md_rdev *rdev,
990                                           struct md_rdev *refdev,
991                                           int minor_version);
992         int                 (*validate_super)(struct mddev *mddev,
993                                               struct md_rdev *rdev);
994         void                (*sync_super)(struct mddev *mddev,
995                                           struct md_rdev *rdev);
996         unsigned long long  (*rdev_size_change)(struct md_rdev *rdev,
997                                                 sector_t num_sectors);
998         int                 (*allow_new_offset)(struct md_rdev *rdev,
999                                                 unsigned long long new_offset);
1000 };
1001
1002 /*
1003  * Check that the given mddev has no bitmap.
1004  *
1005  * This function is called from the run method of all personalities that do not
1006  * support bitmaps. It prints an error message and returns non-zero if mddev
1007  * has a bitmap. Otherwise, it returns 0.
1008  *
1009  */
1010 int md_check_no_bitmap(struct mddev *mddev)
1011 {
1012         if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1013                 return 0;
1014         pr_warn("%s: bitmaps are not supported for %s\n",
1015                 mdname(mddev), mddev->pers->name);
1016         return 1;
1017 }
1018 EXPORT_SYMBOL(md_check_no_bitmap);
1019
1020 /*
1021  * load_super for 0.90.0
1022  */
1023 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1024 {
1025         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1026         mdp_super_t *sb;
1027         int ret;
1028
1029         /*
1030          * Calculate the position of the superblock (512byte sectors),
1031          * it's at the end of the disk.
1032          *
1033          * It also happens to be a multiple of 4Kb.
1034          */
1035         rdev->sb_start = calc_dev_sboffset(rdev);
1036
1037         ret = read_disk_sb(rdev, MD_SB_BYTES);
1038         if (ret)
1039                 return ret;
1040
1041         ret = -EINVAL;
1042
1043         bdevname(rdev->bdev, b);
1044         sb = page_address(rdev->sb_page);
1045
1046         if (sb->md_magic != MD_SB_MAGIC) {
1047                 pr_warn("md: invalid raid superblock magic on %s\n", b);
1048                 goto abort;
1049         }
1050
1051         if (sb->major_version != 0 ||
1052             sb->minor_version < 90 ||
1053             sb->minor_version > 91) {
1054                 pr_warn("Bad version number %d.%d on %s\n",
1055                         sb->major_version, sb->minor_version, b);
1056                 goto abort;
1057         }
1058
1059         if (sb->raid_disks <= 0)
1060                 goto abort;
1061
1062         if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1063                 pr_warn("md: invalid superblock checksum on %s\n", b);
1064                 goto abort;
1065         }
1066
1067         rdev->preferred_minor = sb->md_minor;
1068         rdev->data_offset = 0;
1069         rdev->new_data_offset = 0;
1070         rdev->sb_size = MD_SB_BYTES;
1071         rdev->badblocks.shift = -1;
1072
1073         if (sb->level == LEVEL_MULTIPATH)
1074                 rdev->desc_nr = -1;
1075         else
1076                 rdev->desc_nr = sb->this_disk.number;
1077
1078         if (!refdev) {
1079                 ret = 1;
1080         } else {
1081                 __u64 ev1, ev2;
1082                 mdp_super_t *refsb = page_address(refdev->sb_page);
1083                 if (!md_uuid_equal(refsb, sb)) {
1084                         pr_warn("md: %s has different UUID to %s\n",
1085                                 b, bdevname(refdev->bdev,b2));
1086                         goto abort;
1087                 }
1088                 if (!md_sb_equal(refsb, sb)) {
1089                         pr_warn("md: %s has same UUID but different superblock to %s\n",
1090                                 b, bdevname(refdev->bdev, b2));
1091                         goto abort;
1092                 }
1093                 ev1 = md_event(sb);
1094                 ev2 = md_event(refsb);
1095                 if (ev1 > ev2)
1096                         ret = 1;
1097                 else
1098                         ret = 0;
1099         }
1100         rdev->sectors = rdev->sb_start;
1101         /* Limit to 4TB as metadata cannot record more than that.
1102          * (not needed for Linear and RAID0 as metadata doesn't
1103          * record this size)
1104          */
1105         if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1106             sb->level >= 1)
1107                 rdev->sectors = (sector_t)(2ULL << 32) - 2;
1108
1109         if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1110                 /* "this cannot possibly happen" ... */
1111                 ret = -EINVAL;
1112
1113  abort:
1114         return ret;
1115 }
1116
1117 /*
1118  * validate_super for 0.90.0
1119  */
1120 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1121 {
1122         mdp_disk_t *desc;
1123         mdp_super_t *sb = page_address(rdev->sb_page);
1124         __u64 ev1 = md_event(sb);
1125
1126         rdev->raid_disk = -1;
1127         clear_bit(Faulty, &rdev->flags);
1128         clear_bit(In_sync, &rdev->flags);
1129         clear_bit(Bitmap_sync, &rdev->flags);
1130         clear_bit(WriteMostly, &rdev->flags);
1131
1132         if (mddev->raid_disks == 0) {
1133                 mddev->major_version = 0;
1134                 mddev->minor_version = sb->minor_version;
1135                 mddev->patch_version = sb->patch_version;
1136                 mddev->external = 0;
1137                 mddev->chunk_sectors = sb->chunk_size >> 9;
1138                 mddev->ctime = sb->ctime;
1139                 mddev->utime = sb->utime;
1140                 mddev->level = sb->level;
1141                 mddev->clevel[0] = 0;
1142                 mddev->layout = sb->layout;
1143                 mddev->raid_disks = sb->raid_disks;
1144                 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1145                 mddev->events = ev1;
1146                 mddev->bitmap_info.offset = 0;
1147                 mddev->bitmap_info.space = 0;
1148                 /* bitmap can use 60 K after the 4K superblocks */
1149                 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1150                 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1151                 mddev->reshape_backwards = 0;
1152
1153                 if (mddev->minor_version >= 91) {
1154                         mddev->reshape_position = sb->reshape_position;
1155                         mddev->delta_disks = sb->delta_disks;
1156                         mddev->new_level = sb->new_level;
1157                         mddev->new_layout = sb->new_layout;
1158                         mddev->new_chunk_sectors = sb->new_chunk >> 9;
1159                         if (mddev->delta_disks < 0)
1160                                 mddev->reshape_backwards = 1;
1161                 } else {
1162                         mddev->reshape_position = MaxSector;
1163                         mddev->delta_disks = 0;
1164                         mddev->new_level = mddev->level;
1165                         mddev->new_layout = mddev->layout;
1166                         mddev->new_chunk_sectors = mddev->chunk_sectors;
1167                 }
1168
1169                 if (sb->state & (1<<MD_SB_CLEAN))
1170                         mddev->recovery_cp = MaxSector;
1171                 else {
1172                         if (sb->events_hi == sb->cp_events_hi &&
1173                                 sb->events_lo == sb->cp_events_lo) {
1174                                 mddev->recovery_cp = sb->recovery_cp;
1175                         } else
1176                                 mddev->recovery_cp = 0;
1177                 }
1178
1179                 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1180                 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1181                 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1182                 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1183
1184                 mddev->max_disks = MD_SB_DISKS;
1185
1186                 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1187                     mddev->bitmap_info.file == NULL) {
1188                         mddev->bitmap_info.offset =
1189                                 mddev->bitmap_info.default_offset;
1190                         mddev->bitmap_info.space =
1191                                 mddev->bitmap_info.default_space;
1192                 }
1193
1194         } else if (mddev->pers == NULL) {
1195                 /* Insist on good event counter while assembling, except
1196                  * for spares (which don't need an event count) */
1197                 ++ev1;
1198                 if (sb->disks[rdev->desc_nr].state & (
1199                             (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1200                         if (ev1 < mddev->events)
1201                                 return -EINVAL;
1202         } else if (mddev->bitmap) {
1203                 /* if adding to array with a bitmap, then we can accept an
1204                  * older device ... but not too old.
1205                  */
1206                 if (ev1 < mddev->bitmap->events_cleared)
1207                         return 0;
1208                 if (ev1 < mddev->events)
1209                         set_bit(Bitmap_sync, &rdev->flags);
1210         } else {
1211                 if (ev1 < mddev->events)
1212                         /* just a hot-add of a new device, leave raid_disk at -1 */
1213                         return 0;
1214         }
1215
1216         if (mddev->level != LEVEL_MULTIPATH) {
1217                 desc = sb->disks + rdev->desc_nr;
1218
1219                 if (desc->state & (1<<MD_DISK_FAULTY))
1220                         set_bit(Faulty, &rdev->flags);
1221                 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1222                             desc->raid_disk < mddev->raid_disks */) {
1223                         set_bit(In_sync, &rdev->flags);
1224                         rdev->raid_disk = desc->raid_disk;
1225                         rdev->saved_raid_disk = desc->raid_disk;
1226                 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1227                         /* active but not in sync implies recovery up to
1228                          * reshape position.  We don't know exactly where
1229                          * that is, so set to zero for now */
1230                         if (mddev->minor_version >= 91) {
1231                                 rdev->recovery_offset = 0;
1232                                 rdev->raid_disk = desc->raid_disk;
1233                         }
1234                 }
1235                 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1236                         set_bit(WriteMostly, &rdev->flags);
1237                 if (desc->state & (1<<MD_DISK_FAILFAST))
1238                         set_bit(FailFast, &rdev->flags);
1239         } else /* MULTIPATH are always insync */
1240                 set_bit(In_sync, &rdev->flags);
1241         return 0;
1242 }
1243
1244 /*
1245  * sync_super for 0.90.0
1246  */
1247 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1248 {
1249         mdp_super_t *sb;
1250         struct md_rdev *rdev2;
1251         int next_spare = mddev->raid_disks;
1252
1253         /* make rdev->sb match mddev data..
1254          *
1255          * 1/ zero out disks
1256          * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1257          * 3/ any empty disks < next_spare become removed
1258          *
1259          * disks[0] gets initialised to REMOVED because
1260          * we cannot be sure from other fields if it has
1261          * been initialised or not.
1262          */
1263         int i;
1264         int active=0, working=0,failed=0,spare=0,nr_disks=0;
1265
1266         rdev->sb_size = MD_SB_BYTES;
1267
1268         sb = page_address(rdev->sb_page);
1269
1270         memset(sb, 0, sizeof(*sb));
1271
1272         sb->md_magic = MD_SB_MAGIC;
1273         sb->major_version = mddev->major_version;
1274         sb->patch_version = mddev->patch_version;
1275         sb->gvalid_words  = 0; /* ignored */
1276         memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1277         memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1278         memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1279         memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1280
1281         sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1282         sb->level = mddev->level;
1283         sb->size = mddev->dev_sectors / 2;
1284         sb->raid_disks = mddev->raid_disks;
1285         sb->md_minor = mddev->md_minor;
1286         sb->not_persistent = 0;
1287         sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1288         sb->state = 0;
1289         sb->events_hi = (mddev->events>>32);
1290         sb->events_lo = (u32)mddev->events;
1291
1292         if (mddev->reshape_position == MaxSector)
1293                 sb->minor_version = 90;
1294         else {
1295                 sb->minor_version = 91;
1296                 sb->reshape_position = mddev->reshape_position;
1297                 sb->new_level = mddev->new_level;
1298                 sb->delta_disks = mddev->delta_disks;
1299                 sb->new_layout = mddev->new_layout;
1300                 sb->new_chunk = mddev->new_chunk_sectors << 9;
1301         }
1302         mddev->minor_version = sb->minor_version;
1303         if (mddev->in_sync)
1304         {
1305                 sb->recovery_cp = mddev->recovery_cp;
1306                 sb->cp_events_hi = (mddev->events>>32);
1307                 sb->cp_events_lo = (u32)mddev->events;
1308                 if (mddev->recovery_cp == MaxSector)
1309                         sb->state = (1<< MD_SB_CLEAN);
1310         } else
1311                 sb->recovery_cp = 0;
1312
1313         sb->layout = mddev->layout;
1314         sb->chunk_size = mddev->chunk_sectors << 9;
1315
1316         if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1317                 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1318
1319         sb->disks[0].state = (1<<MD_DISK_REMOVED);
1320         rdev_for_each(rdev2, mddev) {
1321                 mdp_disk_t *d;
1322                 int desc_nr;
1323                 int is_active = test_bit(In_sync, &rdev2->flags);
1324
1325                 if (rdev2->raid_disk >= 0 &&
1326                     sb->minor_version >= 91)
1327                         /* we have nowhere to store the recovery_offset,
1328                          * but if it is not below the reshape_position,
1329                          * we can piggy-back on that.
1330                          */
1331                         is_active = 1;
1332                 if (rdev2->raid_disk < 0 ||
1333                     test_bit(Faulty, &rdev2->flags))
1334                         is_active = 0;
1335                 if (is_active)
1336                         desc_nr = rdev2->raid_disk;
1337                 else
1338                         desc_nr = next_spare++;
1339                 rdev2->desc_nr = desc_nr;
1340                 d = &sb->disks[rdev2->desc_nr];
1341                 nr_disks++;
1342                 d->number = rdev2->desc_nr;
1343                 d->major = MAJOR(rdev2->bdev->bd_dev);
1344                 d->minor = MINOR(rdev2->bdev->bd_dev);
1345                 if (is_active)
1346                         d->raid_disk = rdev2->raid_disk;
1347                 else
1348                         d->raid_disk = rdev2->desc_nr; /* compatibility */
1349                 if (test_bit(Faulty, &rdev2->flags))
1350                         d->state = (1<<MD_DISK_FAULTY);
1351                 else if (is_active) {
1352                         d->state = (1<<MD_DISK_ACTIVE);
1353                         if (test_bit(In_sync, &rdev2->flags))
1354                                 d->state |= (1<<MD_DISK_SYNC);
1355                         active++;
1356                         working++;
1357                 } else {
1358                         d->state = 0;
1359                         spare++;
1360                         working++;
1361                 }
1362                 if (test_bit(WriteMostly, &rdev2->flags))
1363                         d->state |= (1<<MD_DISK_WRITEMOSTLY);
1364                 if (test_bit(FailFast, &rdev2->flags))
1365                         d->state |= (1<<MD_DISK_FAILFAST);
1366         }
1367         /* now set the "removed" and "faulty" bits on any missing devices */
1368         for (i=0 ; i < mddev->raid_disks ; i++) {
1369                 mdp_disk_t *d = &sb->disks[i];
1370                 if (d->state == 0 && d->number == 0) {
1371                         d->number = i;
1372                         d->raid_disk = i;
1373                         d->state = (1<<MD_DISK_REMOVED);
1374                         d->state |= (1<<MD_DISK_FAULTY);
1375                         failed++;
1376                 }
1377         }
1378         sb->nr_disks = nr_disks;
1379         sb->active_disks = active;
1380         sb->working_disks = working;
1381         sb->failed_disks = failed;
1382         sb->spare_disks = spare;
1383
1384         sb->this_disk = sb->disks[rdev->desc_nr];
1385         sb->sb_csum = calc_sb_csum(sb);
1386 }
1387
1388 /*
1389  * rdev_size_change for 0.90.0
1390  */
1391 static unsigned long long
1392 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1393 {
1394         if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1395                 return 0; /* component must fit device */
1396         if (rdev->mddev->bitmap_info.offset)
1397                 return 0; /* can't move bitmap */
1398         rdev->sb_start = calc_dev_sboffset(rdev);
1399         if (!num_sectors || num_sectors > rdev->sb_start)
1400                 num_sectors = rdev->sb_start;
1401         /* Limit to 4TB as metadata cannot record more than that.
1402          * 4TB == 2^32 KB, or 2*2^32 sectors.
1403          */
1404         if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1405             rdev->mddev->level >= 1)
1406                 num_sectors = (sector_t)(2ULL << 32) - 2;
1407         do {
1408                 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1409                        rdev->sb_page);
1410         } while (md_super_wait(rdev->mddev) < 0);
1411         return num_sectors;
1412 }
1413
1414 static int
1415 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1416 {
1417         /* non-zero offset changes not possible with v0.90 */
1418         return new_offset == 0;
1419 }
1420
1421 /*
1422  * version 1 superblock
1423  */
1424
1425 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1426 {
1427         __le32 disk_csum;
1428         u32 csum;
1429         unsigned long long newcsum;
1430         int size = 256 + le32_to_cpu(sb->max_dev)*2;
1431         __le32 *isuper = (__le32*)sb;
1432
1433         disk_csum = sb->sb_csum;
1434         sb->sb_csum = 0;
1435         newcsum = 0;
1436         for (; size >= 4; size -= 4)
1437                 newcsum += le32_to_cpu(*isuper++);
1438
1439         if (size == 2)
1440                 newcsum += le16_to_cpu(*(__le16*) isuper);
1441
1442         csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1443         sb->sb_csum = disk_csum;
1444         return cpu_to_le32(csum);
1445 }
1446
1447 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1448 {
1449         struct mdp_superblock_1 *sb;
1450         int ret;
1451         sector_t sb_start;
1452         sector_t sectors;
1453         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1454         int bmask;
1455
1456         /*
1457          * Calculate the position of the superblock in 512byte sectors.
1458          * It is always aligned to a 4K boundary and
1459          * depeding on minor_version, it can be:
1460          * 0: At least 8K, but less than 12K, from end of device
1461          * 1: At start of device
1462          * 2: 4K from start of device.
1463          */
1464         switch(minor_version) {
1465         case 0:
1466                 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1467                 sb_start -= 8*2;
1468                 sb_start &= ~(sector_t)(4*2-1);
1469                 break;
1470         case 1:
1471                 sb_start = 0;
1472                 break;
1473         case 2:
1474                 sb_start = 8;
1475                 break;
1476         default:
1477                 return -EINVAL;
1478         }
1479         rdev->sb_start = sb_start;
1480
1481         /* superblock is rarely larger than 1K, but it can be larger,
1482          * and it is safe to read 4k, so we do that
1483          */
1484         ret = read_disk_sb(rdev, 4096);
1485         if (ret) return ret;
1486
1487         sb = page_address(rdev->sb_page);
1488
1489         if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1490             sb->major_version != cpu_to_le32(1) ||
1491             le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1492             le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1493             (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1494                 return -EINVAL;
1495
1496         if (calc_sb_1_csum(sb) != sb->sb_csum) {
1497                 pr_warn("md: invalid superblock checksum on %s\n",
1498                         bdevname(rdev->bdev,b));
1499                 return -EINVAL;
1500         }
1501         if (le64_to_cpu(sb->data_size) < 10) {
1502                 pr_warn("md: data_size too small on %s\n",
1503                         bdevname(rdev->bdev,b));
1504                 return -EINVAL;
1505         }
1506         if (sb->pad0 ||
1507             sb->pad3[0] ||
1508             memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1509                 /* Some padding is non-zero, might be a new feature */
1510                 return -EINVAL;
1511
1512         rdev->preferred_minor = 0xffff;
1513         rdev->data_offset = le64_to_cpu(sb->data_offset);
1514         rdev->new_data_offset = rdev->data_offset;
1515         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1516             (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1517                 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1518         atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1519
1520         rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1521         bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1522         if (rdev->sb_size & bmask)
1523                 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1524
1525         if (minor_version
1526             && rdev->data_offset < sb_start + (rdev->sb_size/512))
1527                 return -EINVAL;
1528         if (minor_version
1529             && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1530                 return -EINVAL;
1531
1532         if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1533                 rdev->desc_nr = -1;
1534         else
1535                 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1536
1537         if (!rdev->bb_page) {
1538                 rdev->bb_page = alloc_page(GFP_KERNEL);
1539                 if (!rdev->bb_page)
1540                         return -ENOMEM;
1541         }
1542         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1543             rdev->badblocks.count == 0) {
1544                 /* need to load the bad block list.
1545                  * Currently we limit it to one page.
1546                  */
1547                 s32 offset;
1548                 sector_t bb_sector;
1549                 u64 *bbp;
1550                 int i;
1551                 int sectors = le16_to_cpu(sb->bblog_size);
1552                 if (sectors > (PAGE_SIZE / 512))
1553                         return -EINVAL;
1554                 offset = le32_to_cpu(sb->bblog_offset);
1555                 if (offset == 0)
1556                         return -EINVAL;
1557                 bb_sector = (long long)offset;
1558                 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1559                                   rdev->bb_page, REQ_OP_READ, 0, true))
1560                         return -EIO;
1561                 bbp = (u64 *)page_address(rdev->bb_page);
1562                 rdev->badblocks.shift = sb->bblog_shift;
1563                 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1564                         u64 bb = le64_to_cpu(*bbp);
1565                         int count = bb & (0x3ff);
1566                         u64 sector = bb >> 10;
1567                         sector <<= sb->bblog_shift;
1568                         count <<= sb->bblog_shift;
1569                         if (bb + 1 == 0)
1570                                 break;
1571                         if (badblocks_set(&rdev->badblocks, sector, count, 1))
1572                                 return -EINVAL;
1573                 }
1574         } else if (sb->bblog_offset != 0)
1575                 rdev->badblocks.shift = 0;
1576
1577         if ((le32_to_cpu(sb->feature_map) &
1578             (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
1579                 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1580                 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1581                 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1582         }
1583
1584         if (!refdev) {
1585                 ret = 1;
1586         } else {
1587                 __u64 ev1, ev2;
1588                 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1589
1590                 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1591                     sb->level != refsb->level ||
1592                     sb->layout != refsb->layout ||
1593                     sb->chunksize != refsb->chunksize) {
1594                         pr_warn("md: %s has strangely different superblock to %s\n",
1595                                 bdevname(rdev->bdev,b),
1596                                 bdevname(refdev->bdev,b2));
1597                         return -EINVAL;
1598                 }
1599                 ev1 = le64_to_cpu(sb->events);
1600                 ev2 = le64_to_cpu(refsb->events);
1601
1602                 if (ev1 > ev2)
1603                         ret = 1;
1604                 else
1605                         ret = 0;
1606         }
1607         if (minor_version) {
1608                 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1609                 sectors -= rdev->data_offset;
1610         } else
1611                 sectors = rdev->sb_start;
1612         if (sectors < le64_to_cpu(sb->data_size))
1613                 return -EINVAL;
1614         rdev->sectors = le64_to_cpu(sb->data_size);
1615         return ret;
1616 }
1617
1618 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1619 {
1620         struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1621         __u64 ev1 = le64_to_cpu(sb->events);
1622
1623         rdev->raid_disk = -1;
1624         clear_bit(Faulty, &rdev->flags);
1625         clear_bit(In_sync, &rdev->flags);
1626         clear_bit(Bitmap_sync, &rdev->flags);
1627         clear_bit(WriteMostly, &rdev->flags);
1628
1629         if (mddev->raid_disks == 0) {
1630                 mddev->major_version = 1;
1631                 mddev->patch_version = 0;
1632                 mddev->external = 0;
1633                 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1634                 mddev->ctime = le64_to_cpu(sb->ctime);
1635                 mddev->utime = le64_to_cpu(sb->utime);
1636                 mddev->level = le32_to_cpu(sb->level);
1637                 mddev->clevel[0] = 0;
1638                 mddev->layout = le32_to_cpu(sb->layout);
1639                 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1640                 mddev->dev_sectors = le64_to_cpu(sb->size);
1641                 mddev->events = ev1;
1642                 mddev->bitmap_info.offset = 0;
1643                 mddev->bitmap_info.space = 0;
1644                 /* Default location for bitmap is 1K after superblock
1645                  * using 3K - total of 4K
1646                  */
1647                 mddev->bitmap_info.default_offset = 1024 >> 9;
1648                 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1649                 mddev->reshape_backwards = 0;
1650
1651                 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1652                 memcpy(mddev->uuid, sb->set_uuid, 16);
1653
1654                 mddev->max_disks =  (4096-256)/2;
1655
1656                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1657                     mddev->bitmap_info.file == NULL) {
1658                         mddev->bitmap_info.offset =
1659                                 (__s32)le32_to_cpu(sb->bitmap_offset);
1660                         /* Metadata doesn't record how much space is available.
1661                          * For 1.0, we assume we can use up to the superblock
1662                          * if before, else to 4K beyond superblock.
1663                          * For others, assume no change is possible.
1664                          */
1665                         if (mddev->minor_version > 0)
1666                                 mddev->bitmap_info.space = 0;
1667                         else if (mddev->bitmap_info.offset > 0)
1668                                 mddev->bitmap_info.space =
1669                                         8 - mddev->bitmap_info.offset;
1670                         else
1671                                 mddev->bitmap_info.space =
1672                                         -mddev->bitmap_info.offset;
1673                 }
1674
1675                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1676                         mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1677                         mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1678                         mddev->new_level = le32_to_cpu(sb->new_level);
1679                         mddev->new_layout = le32_to_cpu(sb->new_layout);
1680                         mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1681                         if (mddev->delta_disks < 0 ||
1682                             (mddev->delta_disks == 0 &&
1683                              (le32_to_cpu(sb->feature_map)
1684                               & MD_FEATURE_RESHAPE_BACKWARDS)))
1685                                 mddev->reshape_backwards = 1;
1686                 } else {
1687                         mddev->reshape_position = MaxSector;
1688                         mddev->delta_disks = 0;
1689                         mddev->new_level = mddev->level;
1690                         mddev->new_layout = mddev->layout;
1691                         mddev->new_chunk_sectors = mddev->chunk_sectors;
1692                 }
1693
1694                 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1695                         set_bit(MD_HAS_JOURNAL, &mddev->flags);
1696
1697                 if (le32_to_cpu(sb->feature_map) &
1698                     (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
1699                         if (le32_to_cpu(sb->feature_map) &
1700                             (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1701                                 return -EINVAL;
1702                         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1703                             (le32_to_cpu(sb->feature_map) &
1704                                             MD_FEATURE_MULTIPLE_PPLS))
1705                                 return -EINVAL;
1706                         set_bit(MD_HAS_PPL, &mddev->flags);
1707                 }
1708         } else if (mddev->pers == NULL) {
1709                 /* Insist of good event counter while assembling, except for
1710                  * spares (which don't need an event count) */
1711                 ++ev1;
1712                 if (rdev->desc_nr >= 0 &&
1713                     rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1714                     (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1715                      le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1716                         if (ev1 < mddev->events)
1717                                 return -EINVAL;
1718         } else if (mddev->bitmap) {
1719                 /* If adding to array with a bitmap, then we can accept an
1720                  * older device, but not too old.
1721                  */
1722                 if (ev1 < mddev->bitmap->events_cleared)
1723                         return 0;
1724                 if (ev1 < mddev->events)
1725                         set_bit(Bitmap_sync, &rdev->flags);
1726         } else {
1727                 if (ev1 < mddev->events)
1728                         /* just a hot-add of a new device, leave raid_disk at -1 */
1729                         return 0;
1730         }
1731         if (mddev->level != LEVEL_MULTIPATH) {
1732                 int role;
1733                 if (rdev->desc_nr < 0 ||
1734                     rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1735                         role = MD_DISK_ROLE_SPARE;
1736                         rdev->desc_nr = -1;
1737                 } else
1738                         role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1739                 switch(role) {
1740                 case MD_DISK_ROLE_SPARE: /* spare */
1741                         break;
1742                 case MD_DISK_ROLE_FAULTY: /* faulty */
1743                         set_bit(Faulty, &rdev->flags);
1744                         break;
1745                 case MD_DISK_ROLE_JOURNAL: /* journal device */
1746                         if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1747                                 /* journal device without journal feature */
1748                                 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1749                                 return -EINVAL;
1750                         }
1751                         set_bit(Journal, &rdev->flags);
1752                         rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1753                         rdev->raid_disk = 0;
1754                         break;
1755                 default:
1756                         rdev->saved_raid_disk = role;
1757                         if ((le32_to_cpu(sb->feature_map) &
1758                              MD_FEATURE_RECOVERY_OFFSET)) {
1759                                 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1760                                 if (!(le32_to_cpu(sb->feature_map) &
1761                                       MD_FEATURE_RECOVERY_BITMAP))
1762                                         rdev->saved_raid_disk = -1;
1763                         } else
1764                                 set_bit(In_sync, &rdev->flags);
1765                         rdev->raid_disk = role;
1766                         break;
1767                 }
1768                 if (sb->devflags & WriteMostly1)
1769                         set_bit(WriteMostly, &rdev->flags);
1770                 if (sb->devflags & FailFast1)
1771                         set_bit(FailFast, &rdev->flags);
1772                 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1773                         set_bit(Replacement, &rdev->flags);
1774         } else /* MULTIPATH are always insync */
1775                 set_bit(In_sync, &rdev->flags);
1776
1777         return 0;
1778 }
1779
1780 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1781 {
1782         struct mdp_superblock_1 *sb;
1783         struct md_rdev *rdev2;
1784         int max_dev, i;
1785         /* make rdev->sb match mddev and rdev data. */
1786
1787         sb = page_address(rdev->sb_page);
1788
1789         sb->feature_map = 0;
1790         sb->pad0 = 0;
1791         sb->recovery_offset = cpu_to_le64(0);
1792         memset(sb->pad3, 0, sizeof(sb->pad3));
1793
1794         sb->utime = cpu_to_le64((__u64)mddev->utime);
1795         sb->events = cpu_to_le64(mddev->events);
1796         if (mddev->in_sync)
1797                 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1798         else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1799                 sb->resync_offset = cpu_to_le64(MaxSector);
1800         else
1801                 sb->resync_offset = cpu_to_le64(0);
1802
1803         sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1804
1805         sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1806         sb->size = cpu_to_le64(mddev->dev_sectors);
1807         sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1808         sb->level = cpu_to_le32(mddev->level);
1809         sb->layout = cpu_to_le32(mddev->layout);
1810         if (test_bit(FailFast, &rdev->flags))
1811                 sb->devflags |= FailFast1;
1812         else
1813                 sb->devflags &= ~FailFast1;
1814
1815         if (test_bit(WriteMostly, &rdev->flags))
1816                 sb->devflags |= WriteMostly1;
1817         else
1818                 sb->devflags &= ~WriteMostly1;
1819         sb->data_offset = cpu_to_le64(rdev->data_offset);
1820         sb->data_size = cpu_to_le64(rdev->sectors);
1821
1822         if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1823                 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1824                 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1825         }
1826
1827         if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
1828             !test_bit(In_sync, &rdev->flags)) {
1829                 sb->feature_map |=
1830                         cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1831                 sb->recovery_offset =
1832                         cpu_to_le64(rdev->recovery_offset);
1833                 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1834                         sb->feature_map |=
1835                                 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1836         }
1837         /* Note: recovery_offset and journal_tail share space  */
1838         if (test_bit(Journal, &rdev->flags))
1839                 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
1840         if (test_bit(Replacement, &rdev->flags))
1841                 sb->feature_map |=
1842                         cpu_to_le32(MD_FEATURE_REPLACEMENT);
1843
1844         if (mddev->reshape_position != MaxSector) {
1845                 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1846                 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1847                 sb->new_layout = cpu_to_le32(mddev->new_layout);
1848                 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1849                 sb->new_level = cpu_to_le32(mddev->new_level);
1850                 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1851                 if (mddev->delta_disks == 0 &&
1852                     mddev->reshape_backwards)
1853                         sb->feature_map
1854                                 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1855                 if (rdev->new_data_offset != rdev->data_offset) {
1856                         sb->feature_map
1857                                 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1858                         sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1859                                                              - rdev->data_offset));
1860                 }
1861         }
1862
1863         if (mddev_is_clustered(mddev))
1864                 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1865
1866         if (rdev->badblocks.count == 0)
1867                 /* Nothing to do for bad blocks*/ ;
1868         else if (sb->bblog_offset == 0)
1869                 /* Cannot record bad blocks on this device */
1870                 md_error(mddev, rdev);
1871         else {
1872                 struct badblocks *bb = &rdev->badblocks;
1873                 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1874                 u64 *p = bb->page;
1875                 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1876                 if (bb->changed) {
1877                         unsigned seq;
1878
1879 retry:
1880                         seq = read_seqbegin(&bb->lock);
1881
1882                         memset(bbp, 0xff, PAGE_SIZE);
1883
1884                         for (i = 0 ; i < bb->count ; i++) {
1885                                 u64 internal_bb = p[i];
1886                                 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1887                                                 | BB_LEN(internal_bb));
1888                                 bbp[i] = cpu_to_le64(store_bb);
1889                         }
1890                         bb->changed = 0;
1891                         if (read_seqretry(&bb->lock, seq))
1892                                 goto retry;
1893
1894                         bb->sector = (rdev->sb_start +
1895                                       (int)le32_to_cpu(sb->bblog_offset));
1896                         bb->size = le16_to_cpu(sb->bblog_size);
1897                 }
1898         }
1899
1900         max_dev = 0;
1901         rdev_for_each(rdev2, mddev)
1902                 if (rdev2->desc_nr+1 > max_dev)
1903                         max_dev = rdev2->desc_nr+1;
1904
1905         if (max_dev > le32_to_cpu(sb->max_dev)) {
1906                 int bmask;
1907                 sb->max_dev = cpu_to_le32(max_dev);
1908                 rdev->sb_size = max_dev * 2 + 256;
1909                 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1910                 if (rdev->sb_size & bmask)
1911                         rdev->sb_size = (rdev->sb_size | bmask) + 1;
1912         } else
1913                 max_dev = le32_to_cpu(sb->max_dev);
1914
1915         for (i=0; i<max_dev;i++)
1916                 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
1917
1918         if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1919                 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
1920
1921         if (test_bit(MD_HAS_PPL, &mddev->flags)) {
1922                 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1923                         sb->feature_map |=
1924                             cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1925                 else
1926                         sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
1927                 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1928                 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1929         }
1930
1931         rdev_for_each(rdev2, mddev) {
1932                 i = rdev2->desc_nr;
1933                 if (test_bit(Faulty, &rdev2->flags))
1934                         sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1935                 else if (test_bit(In_sync, &rdev2->flags))
1936                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1937                 else if (test_bit(Journal, &rdev2->flags))
1938                         sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
1939                 else if (rdev2->raid_disk >= 0)
1940                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1941                 else
1942                         sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
1943         }
1944
1945         sb->sb_csum = calc_sb_1_csum(sb);
1946 }
1947
1948 static unsigned long long
1949 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1950 {
1951         struct mdp_superblock_1 *sb;
1952         sector_t max_sectors;
1953         if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1954                 return 0; /* component must fit device */
1955         if (rdev->data_offset != rdev->new_data_offset)
1956                 return 0; /* too confusing */
1957         if (rdev->sb_start < rdev->data_offset) {
1958                 /* minor versions 1 and 2; superblock before data */
1959                 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1960                 max_sectors -= rdev->data_offset;
1961                 if (!num_sectors || num_sectors > max_sectors)
1962                         num_sectors = max_sectors;
1963         } else if (rdev->mddev->bitmap_info.offset) {
1964                 /* minor version 0 with bitmap we can't move */
1965                 return 0;
1966         } else {
1967                 /* minor version 0; superblock after data */
1968                 sector_t sb_start;
1969                 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1970                 sb_start &= ~(sector_t)(4*2 - 1);
1971                 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1972                 if (!num_sectors || num_sectors > max_sectors)
1973                         num_sectors = max_sectors;
1974                 rdev->sb_start = sb_start;
1975         }
1976         sb = page_address(rdev->sb_page);
1977         sb->data_size = cpu_to_le64(num_sectors);
1978         sb->super_offset = cpu_to_le64(rdev->sb_start);
1979         sb->sb_csum = calc_sb_1_csum(sb);
1980         do {
1981                 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1982                                rdev->sb_page);
1983         } while (md_super_wait(rdev->mddev) < 0);
1984         return num_sectors;
1985
1986 }
1987
1988 static int
1989 super_1_allow_new_offset(struct md_rdev *rdev,
1990                          unsigned long long new_offset)
1991 {
1992         /* All necessary checks on new >= old have been done */
1993         struct bitmap *bitmap;
1994         if (new_offset >= rdev->data_offset)
1995                 return 1;
1996
1997         /* with 1.0 metadata, there is no metadata to tread on
1998          * so we can always move back */
1999         if (rdev->mddev->minor_version == 0)
2000                 return 1;
2001
2002         /* otherwise we must be sure not to step on
2003          * any metadata, so stay:
2004          * 36K beyond start of superblock
2005          * beyond end of badblocks
2006          * beyond write-intent bitmap
2007          */
2008         if (rdev->sb_start + (32+4)*2 > new_offset)
2009                 return 0;
2010         bitmap = rdev->mddev->bitmap;
2011         if (bitmap && !rdev->mddev->bitmap_info.file &&
2012             rdev->sb_start + rdev->mddev->bitmap_info.offset +
2013             bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
2014                 return 0;
2015         if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2016                 return 0;
2017
2018         return 1;
2019 }
2020
2021 static struct super_type super_types[] = {
2022         [0] = {
2023                 .name   = "0.90.0",
2024                 .owner  = THIS_MODULE,
2025                 .load_super         = super_90_load,
2026                 .validate_super     = super_90_validate,
2027                 .sync_super         = super_90_sync,
2028                 .rdev_size_change   = super_90_rdev_size_change,
2029                 .allow_new_offset   = super_90_allow_new_offset,
2030         },
2031         [1] = {
2032                 .name   = "md-1",
2033                 .owner  = THIS_MODULE,
2034                 .load_super         = super_1_load,
2035                 .validate_super     = super_1_validate,
2036                 .sync_super         = super_1_sync,
2037                 .rdev_size_change   = super_1_rdev_size_change,
2038                 .allow_new_offset   = super_1_allow_new_offset,
2039         },
2040 };
2041
2042 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
2043 {
2044         if (mddev->sync_super) {
2045                 mddev->sync_super(mddev, rdev);
2046                 return;
2047         }
2048
2049         BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2050
2051         super_types[mddev->major_version].sync_super(mddev, rdev);
2052 }
2053
2054 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
2055 {
2056         struct md_rdev *rdev, *rdev2;
2057
2058         rcu_read_lock();
2059         rdev_for_each_rcu(rdev, mddev1) {
2060                 if (test_bit(Faulty, &rdev->flags) ||
2061                     test_bit(Journal, &rdev->flags) ||
2062                     rdev->raid_disk == -1)
2063                         continue;
2064                 rdev_for_each_rcu(rdev2, mddev2) {
2065                         if (test_bit(Faulty, &rdev2->flags) ||
2066                             test_bit(Journal, &rdev2->flags) ||
2067                             rdev2->raid_disk == -1)
2068                                 continue;
2069                         if (rdev->bdev->bd_contains ==
2070                             rdev2->bdev->bd_contains) {
2071                                 rcu_read_unlock();
2072                                 return 1;
2073                         }
2074                 }
2075         }
2076         rcu_read_unlock();
2077         return 0;
2078 }
2079
2080 static LIST_HEAD(pending_raid_disks);
2081
2082 /*
2083  * Try to register data integrity profile for an mddev
2084  *
2085  * This is called when an array is started and after a disk has been kicked
2086  * from the array. It only succeeds if all working and active component devices
2087  * are integrity capable with matching profiles.
2088  */
2089 int md_integrity_register(struct mddev *mddev)
2090 {
2091         struct md_rdev *rdev, *reference = NULL;
2092
2093         if (list_empty(&mddev->disks))
2094                 return 0; /* nothing to do */
2095         if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2096                 return 0; /* shouldn't register, or already is */
2097         rdev_for_each(rdev, mddev) {
2098                 /* skip spares and non-functional disks */
2099                 if (test_bit(Faulty, &rdev->flags))
2100                         continue;
2101                 if (rdev->raid_disk < 0)
2102                         continue;
2103                 if (!reference) {
2104                         /* Use the first rdev as the reference */
2105                         reference = rdev;
2106                         continue;
2107                 }
2108                 /* does this rdev's profile match the reference profile? */
2109                 if (blk_integrity_compare(reference->bdev->bd_disk,
2110                                 rdev->bdev->bd_disk) < 0)
2111                         return -EINVAL;
2112         }
2113         if (!reference || !bdev_get_integrity(reference->bdev))
2114                 return 0;
2115         /*
2116          * All component devices are integrity capable and have matching
2117          * profiles, register the common profile for the md device.
2118          */
2119         blk_integrity_register(mddev->gendisk,
2120                                bdev_get_integrity(reference->bdev));
2121
2122         pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2123         if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2124                 pr_err("md: failed to create integrity pool for %s\n",
2125                        mdname(mddev));
2126                 return -EINVAL;
2127         }
2128         return 0;
2129 }
2130 EXPORT_SYMBOL(md_integrity_register);
2131
2132 /*
2133  * Attempt to add an rdev, but only if it is consistent with the current
2134  * integrity profile
2135  */
2136 int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2137 {
2138         struct blk_integrity *bi_rdev;
2139         struct blk_integrity *bi_mddev;
2140         char name[BDEVNAME_SIZE];
2141
2142         if (!mddev->gendisk)
2143                 return 0;
2144
2145         bi_rdev = bdev_get_integrity(rdev->bdev);
2146         bi_mddev = blk_get_integrity(mddev->gendisk);
2147
2148         if (!bi_mddev) /* nothing to do */
2149                 return 0;
2150
2151         if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2152                 pr_err("%s: incompatible integrity profile for %s\n",
2153                        mdname(mddev), bdevname(rdev->bdev, name));
2154                 return -ENXIO;
2155         }
2156
2157         return 0;
2158 }
2159 EXPORT_SYMBOL(md_integrity_add_rdev);
2160
2161 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2162 {
2163         char b[BDEVNAME_SIZE];
2164         struct kobject *ko;
2165         int err;
2166
2167         /* prevent duplicates */
2168         if (find_rdev(mddev, rdev->bdev->bd_dev))
2169                 return -EEXIST;
2170
2171         if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2172             mddev->pers)
2173                 return -EROFS;
2174
2175         /* make sure rdev->sectors exceeds mddev->dev_sectors */
2176         if (!test_bit(Journal, &rdev->flags) &&
2177             rdev->sectors &&
2178             (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2179                 if (mddev->pers) {
2180                         /* Cannot change size, so fail
2181                          * If mddev->level <= 0, then we don't care
2182                          * about aligning sizes (e.g. linear)
2183                          */
2184                         if (mddev->level > 0)
2185                                 return -ENOSPC;
2186                 } else
2187                         mddev->dev_sectors = rdev->sectors;
2188         }
2189
2190         /* Verify rdev->desc_nr is unique.
2191          * If it is -1, assign a free number, else
2192          * check number is not in use
2193          */
2194         rcu_read_lock();
2195         if (rdev->desc_nr < 0) {
2196                 int choice = 0;
2197                 if (mddev->pers)
2198                         choice = mddev->raid_disks;
2199                 while (md_find_rdev_nr_rcu(mddev, choice))
2200                         choice++;
2201                 rdev->desc_nr = choice;
2202         } else {
2203                 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2204                         rcu_read_unlock();
2205                         return -EBUSY;
2206                 }
2207         }
2208         rcu_read_unlock();
2209         if (!test_bit(Journal, &rdev->flags) &&
2210             mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2211                 pr_warn("md: %s: array is limited to %d devices\n",
2212                         mdname(mddev), mddev->max_disks);
2213                 return -EBUSY;
2214         }
2215         bdevname(rdev->bdev,b);
2216         strreplace(b, '/', '!');
2217
2218         rdev->mddev = mddev;
2219         pr_debug("md: bind<%s>\n", b);
2220
2221         if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2222                 goto fail;
2223
2224         ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2225         if (sysfs_create_link(&rdev->kobj, ko, "block"))
2226                 /* failure here is OK */;
2227         rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2228
2229         list_add_rcu(&rdev->same_set, &mddev->disks);
2230         bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2231
2232         /* May as well allow recovery to be retried once */
2233         mddev->recovery_disabled++;
2234
2235         return 0;
2236
2237  fail:
2238         pr_warn("md: failed to register dev-%s for %s\n",
2239                 b, mdname(mddev));
2240         return err;
2241 }
2242
2243 static void md_delayed_delete(struct work_struct *ws)
2244 {
2245         struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2246         kobject_del(&rdev->kobj);
2247         kobject_put(&rdev->kobj);
2248 }
2249
2250 static void unbind_rdev_from_array(struct md_rdev *rdev)
2251 {
2252         char b[BDEVNAME_SIZE];
2253
2254         bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2255         list_del_rcu(&rdev->same_set);
2256         pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
2257         rdev->mddev = NULL;
2258         sysfs_remove_link(&rdev->kobj, "block");
2259         sysfs_put(rdev->sysfs_state);
2260         rdev->sysfs_state = NULL;
2261         rdev->badblocks.count = 0;
2262         /* We need to delay this, otherwise we can deadlock when
2263          * writing to 'remove' to "dev/state".  We also need
2264          * to delay it due to rcu usage.
2265          */
2266         synchronize_rcu();
2267         INIT_WORK(&rdev->del_work, md_delayed_delete);
2268         kobject_get(&rdev->kobj);
2269         queue_work(md_misc_wq, &rdev->del_work);
2270 }
2271
2272 /*
2273  * prevent the device from being mounted, repartitioned or
2274  * otherwise reused by a RAID array (or any other kernel
2275  * subsystem), by bd_claiming the device.
2276  */
2277 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2278 {
2279         int err = 0;
2280         struct block_device *bdev;
2281         char b[BDEVNAME_SIZE];
2282
2283         bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2284                                  shared ? (struct md_rdev *)lock_rdev : rdev);
2285         if (IS_ERR(bdev)) {
2286                 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
2287                 return PTR_ERR(bdev);
2288         }
2289         rdev->bdev = bdev;
2290         return err;
2291 }
2292
2293 static void unlock_rdev(struct md_rdev *rdev)
2294 {
2295         struct block_device *bdev = rdev->bdev;
2296         rdev->bdev = NULL;
2297         blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2298 }
2299
2300 void md_autodetect_dev(dev_t dev);
2301
2302 static void export_rdev(struct md_rdev *rdev)
2303 {
2304         char b[BDEVNAME_SIZE];
2305
2306         pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
2307         md_rdev_clear(rdev);
2308 #ifndef MODULE
2309         if (test_bit(AutoDetected, &rdev->flags))
2310                 md_autodetect_dev(rdev->bdev->bd_dev);
2311 #endif
2312         unlock_rdev(rdev);
2313         kobject_put(&rdev->kobj);
2314 }
2315
2316 void md_kick_rdev_from_array(struct md_rdev *rdev)
2317 {
2318         unbind_rdev_from_array(rdev);
2319         export_rdev(rdev);
2320 }
2321 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2322
2323 static void export_array(struct mddev *mddev)
2324 {
2325         struct md_rdev *rdev;
2326
2327         while (!list_empty(&mddev->disks)) {
2328                 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2329                                         same_set);
2330                 md_kick_rdev_from_array(rdev);
2331         }
2332         mddev->raid_disks = 0;
2333         mddev->major_version = 0;
2334 }
2335
2336 static bool set_in_sync(struct mddev *mddev)
2337 {
2338         lockdep_assert_held(&mddev->lock);
2339         if (!mddev->in_sync) {
2340                 mddev->sync_checkers++;
2341                 spin_unlock(&mddev->lock);
2342                 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2343                 spin_lock(&mddev->lock);
2344                 if (!mddev->in_sync &&
2345                     percpu_ref_is_zero(&mddev->writes_pending)) {
2346                         mddev->in_sync = 1;
2347                         /*
2348                          * Ensure ->in_sync is visible before we clear
2349                          * ->sync_checkers.
2350                          */
2351                         smp_mb();
2352                         set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2353                         sysfs_notify_dirent_safe(mddev->sysfs_state);
2354                 }
2355                 if (--mddev->sync_checkers == 0)
2356                         percpu_ref_switch_to_percpu(&mddev->writes_pending);
2357         }
2358         if (mddev->safemode == 1)
2359                 mddev->safemode = 0;
2360         return mddev->in_sync;
2361 }
2362
2363 static void sync_sbs(struct mddev *mddev, int nospares)
2364 {
2365         /* Update each superblock (in-memory image), but
2366          * if we are allowed to, skip spares which already
2367          * have the right event counter, or have one earlier
2368          * (which would mean they aren't being marked as dirty
2369          * with the rest of the array)
2370          */
2371         struct md_rdev *rdev;
2372         rdev_for_each(rdev, mddev) {
2373                 if (rdev->sb_events == mddev->events ||
2374                     (nospares &&
2375                      rdev->raid_disk < 0 &&
2376                      rdev->sb_events+1 == mddev->events)) {
2377                         /* Don't update this superblock */
2378                         rdev->sb_loaded = 2;
2379                 } else {
2380                         sync_super(mddev, rdev);
2381                         rdev->sb_loaded = 1;
2382                 }
2383         }
2384 }
2385
2386 static bool does_sb_need_changing(struct mddev *mddev)
2387 {
2388         struct md_rdev *rdev;
2389         struct mdp_superblock_1 *sb;
2390         int role;
2391
2392         /* Find a good rdev */
2393         rdev_for_each(rdev, mddev)
2394                 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2395                         break;
2396
2397         /* No good device found. */
2398         if (!rdev)
2399                 return false;
2400
2401         sb = page_address(rdev->sb_page);
2402         /* Check if a device has become faulty or a spare become active */
2403         rdev_for_each(rdev, mddev) {
2404                 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2405                 /* Device activated? */
2406                 if (role == 0xffff && rdev->raid_disk >=0 &&
2407                     !test_bit(Faulty, &rdev->flags))
2408                         return true;
2409                 /* Device turned faulty? */
2410                 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2411                         return true;
2412         }
2413
2414         /* Check if any mddev parameters have changed */
2415         if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2416             (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2417             (mddev->layout != le32_to_cpu(sb->layout)) ||
2418             (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2419             (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2420                 return true;
2421
2422         return false;
2423 }
2424
2425 void md_update_sb(struct mddev *mddev, int force_change)
2426 {
2427         struct md_rdev *rdev;
2428         int sync_req;
2429         int nospares = 0;
2430         int any_badblocks_changed = 0;
2431         int ret = -1;
2432
2433         if (mddev->ro) {
2434                 if (force_change)
2435                         set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2436                 return;
2437         }
2438
2439 repeat:
2440         if (mddev_is_clustered(mddev)) {
2441                 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2442                         force_change = 1;
2443                 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2444                         nospares = 1;
2445                 ret = md_cluster_ops->metadata_update_start(mddev);
2446                 /* Has someone else has updated the sb */
2447                 if (!does_sb_need_changing(mddev)) {
2448                         if (ret == 0)
2449                                 md_cluster_ops->metadata_update_cancel(mddev);
2450                         bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2451                                                          BIT(MD_SB_CHANGE_DEVS) |
2452                                                          BIT(MD_SB_CHANGE_CLEAN));
2453                         return;
2454                 }
2455         }
2456
2457         /*
2458          * First make sure individual recovery_offsets are correct
2459          * curr_resync_completed can only be used during recovery.
2460          * During reshape/resync it might use array-addresses rather
2461          * that device addresses.
2462          */
2463         rdev_for_each(rdev, mddev) {
2464                 if (rdev->raid_disk >= 0 &&
2465                     mddev->delta_disks >= 0 &&
2466                     test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2467                     test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2468                     !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2469                     !test_bit(Journal, &rdev->flags) &&
2470                     !test_bit(In_sync, &rdev->flags) &&
2471                     mddev->curr_resync_completed > rdev->recovery_offset)
2472                                 rdev->recovery_offset = mddev->curr_resync_completed;
2473
2474         }
2475         if (!mddev->persistent) {
2476                 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2477                 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2478                 if (!mddev->external) {
2479                         clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2480                         rdev_for_each(rdev, mddev) {
2481                                 if (rdev->badblocks.changed) {
2482                                         rdev->badblocks.changed = 0;
2483                                         ack_all_badblocks(&rdev->badblocks);
2484                                         md_error(mddev, rdev);
2485                                 }
2486                                 clear_bit(Blocked, &rdev->flags);
2487                                 clear_bit(BlockedBadBlocks, &rdev->flags);
2488                                 wake_up(&rdev->blocked_wait);
2489                         }
2490                 }
2491                 wake_up(&mddev->sb_wait);
2492                 return;
2493         }
2494
2495         spin_lock(&mddev->lock);
2496
2497         mddev->utime = ktime_get_real_seconds();
2498
2499         if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2500                 force_change = 1;
2501         if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2502                 /* just a clean<-> dirty transition, possibly leave spares alone,
2503                  * though if events isn't the right even/odd, we will have to do
2504                  * spares after all
2505                  */
2506                 nospares = 1;
2507         if (force_change)
2508                 nospares = 0;
2509         if (mddev->degraded)
2510                 /* If the array is degraded, then skipping spares is both
2511                  * dangerous and fairly pointless.
2512                  * Dangerous because a device that was removed from the array
2513                  * might have a event_count that still looks up-to-date,
2514                  * so it can be re-added without a resync.
2515                  * Pointless because if there are any spares to skip,
2516                  * then a recovery will happen and soon that array won't
2517                  * be degraded any more and the spare can go back to sleep then.
2518                  */
2519                 nospares = 0;
2520
2521         sync_req = mddev->in_sync;
2522
2523         /* If this is just a dirty<->clean transition, and the array is clean
2524          * and 'events' is odd, we can roll back to the previous clean state */
2525         if (nospares
2526             && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2527             && mddev->can_decrease_events
2528             && mddev->events != 1) {
2529                 mddev->events--;
2530                 mddev->can_decrease_events = 0;
2531         } else {
2532                 /* otherwise we have to go forward and ... */
2533                 mddev->events ++;
2534                 mddev->can_decrease_events = nospares;
2535         }
2536
2537         /*
2538          * This 64-bit counter should never wrap.
2539          * Either we are in around ~1 trillion A.C., assuming
2540          * 1 reboot per second, or we have a bug...
2541          */
2542         WARN_ON(mddev->events == 0);
2543
2544         rdev_for_each(rdev, mddev) {
2545                 if (rdev->badblocks.changed)
2546                         any_badblocks_changed++;
2547                 if (test_bit(Faulty, &rdev->flags))
2548                         set_bit(FaultRecorded, &rdev->flags);
2549         }
2550
2551         sync_sbs(mddev, nospares);
2552         spin_unlock(&mddev->lock);
2553
2554         pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2555                  mdname(mddev), mddev->in_sync);
2556
2557         if (mddev->queue)
2558                 blk_add_trace_msg(mddev->queue, "md md_update_sb");
2559 rewrite:
2560         bitmap_update_sb(mddev->bitmap);
2561         rdev_for_each(rdev, mddev) {
2562                 char b[BDEVNAME_SIZE];
2563
2564                 if (rdev->sb_loaded != 1)
2565                         continue; /* no noise on spare devices */
2566
2567                 if (!test_bit(Faulty, &rdev->flags)) {
2568                         md_super_write(mddev,rdev,
2569                                        rdev->sb_start, rdev->sb_size,
2570                                        rdev->sb_page);
2571                         pr_debug("md: (write) %s's sb offset: %llu\n",
2572                                  bdevname(rdev->bdev, b),
2573                                  (unsigned long long)rdev->sb_start);
2574                         rdev->sb_events = mddev->events;
2575                         if (rdev->badblocks.size) {
2576                                 md_super_write(mddev, rdev,
2577                                                rdev->badblocks.sector,
2578                                                rdev->badblocks.size << 9,
2579                                                rdev->bb_page);
2580                                 rdev->badblocks.size = 0;
2581                         }
2582
2583                 } else
2584                         pr_debug("md: %s (skipping faulty)\n",
2585                                  bdevname(rdev->bdev, b));
2586
2587                 if (mddev->level == LEVEL_MULTIPATH)
2588                         /* only need to write one superblock... */
2589                         break;
2590         }
2591         if (md_super_wait(mddev) < 0)
2592                 goto rewrite;
2593         /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2594
2595         if (mddev_is_clustered(mddev) && ret == 0)
2596                 md_cluster_ops->metadata_update_finish(mddev);
2597
2598         if (mddev->in_sync != sync_req ||
2599             !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2600                                BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2601                 /* have to write it out again */
2602                 goto repeat;
2603         wake_up(&mddev->sb_wait);
2604         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2605                 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2606
2607         rdev_for_each(rdev, mddev) {
2608                 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2609                         clear_bit(Blocked, &rdev->flags);
2610
2611                 if (any_badblocks_changed)
2612                         ack_all_badblocks(&rdev->badblocks);
2613                 clear_bit(BlockedBadBlocks, &rdev->flags);
2614                 wake_up(&rdev->blocked_wait);
2615         }
2616 }
2617 EXPORT_SYMBOL(md_update_sb);
2618
2619 static int add_bound_rdev(struct md_rdev *rdev)
2620 {
2621         struct mddev *mddev = rdev->mddev;
2622         int err = 0;
2623         bool add_journal = test_bit(Journal, &rdev->flags);
2624
2625         if (!mddev->pers->hot_remove_disk || add_journal) {
2626                 /* If there is hot_add_disk but no hot_remove_disk
2627                  * then added disks for geometry changes,
2628                  * and should be added immediately.
2629                  */
2630                 super_types[mddev->major_version].
2631                         validate_super(mddev, rdev);
2632                 if (add_journal)
2633                         mddev_suspend(mddev);
2634                 err = mddev->pers->hot_add_disk(mddev, rdev);
2635                 if (add_journal)
2636                         mddev_resume(mddev);
2637                 if (err) {
2638                         md_kick_rdev_from_array(rdev);
2639                         return err;
2640                 }
2641         }
2642         sysfs_notify_dirent_safe(rdev->sysfs_state);
2643
2644         set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2645         if (mddev->degraded)
2646                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2647         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2648         md_new_event(mddev);
2649         md_wakeup_thread(mddev->thread);
2650         return 0;
2651 }
2652
2653 /* words written to sysfs files may, or may not, be \n terminated.
2654  * We want to accept with case. For this we use cmd_match.
2655  */
2656 static int cmd_match(const char *cmd, const char *str)
2657 {
2658         /* See if cmd, written into a sysfs file, matches
2659          * str.  They must either be the same, or cmd can
2660          * have a trailing newline
2661          */
2662         while (*cmd && *str && *cmd == *str) {
2663                 cmd++;
2664                 str++;
2665         }
2666         if (*cmd == '\n')
2667                 cmd++;
2668         if (*str || *cmd)
2669                 return 0;
2670         return 1;
2671 }
2672
2673 struct rdev_sysfs_entry {
2674         struct attribute attr;
2675         ssize_t (*show)(struct md_rdev *, char *);
2676         ssize_t (*store)(struct md_rdev *, const char *, size_t);
2677 };
2678
2679 static ssize_t
2680 state_show(struct md_rdev *rdev, char *page)
2681 {
2682         char *sep = ",";
2683         size_t len = 0;
2684         unsigned long flags = READ_ONCE(rdev->flags);
2685
2686         if (test_bit(Faulty, &flags) ||
2687             (!test_bit(ExternalBbl, &flags) &&
2688             rdev->badblocks.unacked_exist))
2689                 len += sprintf(page+len, "faulty%s", sep);
2690         if (test_bit(In_sync, &flags))
2691                 len += sprintf(page+len, "in_sync%s", sep);
2692         if (test_bit(Journal, &flags))
2693                 len += sprintf(page+len, "journal%s", sep);
2694         if (test_bit(WriteMostly, &flags))
2695                 len += sprintf(page+len, "write_mostly%s", sep);
2696         if (test_bit(Blocked, &flags) ||
2697             (rdev->badblocks.unacked_exist
2698              && !test_bit(Faulty, &flags)))
2699                 len += sprintf(page+len, "blocked%s", sep);
2700         if (!test_bit(Faulty, &flags) &&
2701             !test_bit(Journal, &flags) &&
2702             !test_bit(In_sync, &flags))
2703                 len += sprintf(page+len, "spare%s", sep);
2704         if (test_bit(WriteErrorSeen, &flags))
2705                 len += sprintf(page+len, "write_error%s", sep);
2706         if (test_bit(WantReplacement, &flags))
2707                 len += sprintf(page+len, "want_replacement%s", sep);
2708         if (test_bit(Replacement, &flags))
2709                 len += sprintf(page+len, "replacement%s", sep);
2710         if (test_bit(ExternalBbl, &flags))
2711                 len += sprintf(page+len, "external_bbl%s", sep);
2712         if (test_bit(FailFast, &flags))
2713                 len += sprintf(page+len, "failfast%s", sep);
2714
2715         if (len)
2716                 len -= strlen(sep);
2717
2718         return len+sprintf(page+len, "\n");
2719 }
2720
2721 static ssize_t
2722 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2723 {
2724         /* can write
2725          *  faulty  - simulates an error
2726          *  remove  - disconnects the device
2727          *  writemostly - sets write_mostly
2728          *  -writemostly - clears write_mostly
2729          *  blocked - sets the Blocked flags
2730          *  -blocked - clears the Blocked and possibly simulates an error
2731          *  insync - sets Insync providing device isn't active
2732          *  -insync - clear Insync for a device with a slot assigned,
2733          *            so that it gets rebuilt based on bitmap
2734          *  write_error - sets WriteErrorSeen
2735          *  -write_error - clears WriteErrorSeen
2736          *  {,-}failfast - set/clear FailFast
2737          */
2738         int err = -EINVAL;
2739         if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2740                 md_error(rdev->mddev, rdev);
2741                 if (test_bit(Faulty, &rdev->flags))
2742                         err = 0;
2743                 else
2744                         err = -EBUSY;
2745         } else if (cmd_match(buf, "remove")) {
2746                 if (rdev->mddev->pers) {
2747                         clear_bit(Blocked, &rdev->flags);
2748                         remove_and_add_spares(rdev->mddev, rdev);
2749                 }
2750                 if (rdev->raid_disk >= 0)
2751                         err = -EBUSY;
2752                 else {
2753                         struct mddev *mddev = rdev->mddev;
2754                         err = 0;
2755                         if (mddev_is_clustered(mddev))
2756                                 err = md_cluster_ops->remove_disk(mddev, rdev);
2757
2758                         if (err == 0) {
2759                                 md_kick_rdev_from_array(rdev);
2760                                 if (mddev->pers) {
2761                                         set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2762                                         md_wakeup_thread(mddev->thread);
2763                                 }
2764                                 md_new_event(mddev);
2765                         }
2766                 }
2767         } else if (cmd_match(buf, "writemostly")) {
2768                 set_bit(WriteMostly, &rdev->flags);
2769                 err = 0;
2770         } else if (cmd_match(buf, "-writemostly")) {
2771                 clear_bit(WriteMostly, &rdev->flags);
2772                 err = 0;
2773         } else if (cmd_match(buf, "blocked")) {
2774                 set_bit(Blocked, &rdev->flags);
2775                 err = 0;
2776         } else if (cmd_match(buf, "-blocked")) {
2777                 if (!test_bit(Faulty, &rdev->flags) &&
2778                     !test_bit(ExternalBbl, &rdev->flags) &&
2779                     rdev->badblocks.unacked_exist) {
2780                         /* metadata handler doesn't understand badblocks,
2781                          * so we need to fail the device
2782                          */
2783                         md_error(rdev->mddev, rdev);
2784                 }
2785                 clear_bit(Blocked, &rdev->flags);
2786                 clear_bit(BlockedBadBlocks, &rdev->flags);
2787                 wake_up(&rdev->blocked_wait);
2788                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2789                 md_wakeup_thread(rdev->mddev->thread);
2790
2791                 err = 0;
2792         } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2793                 set_bit(In_sync, &rdev->flags);
2794                 err = 0;
2795         } else if (cmd_match(buf, "failfast")) {
2796                 set_bit(FailFast, &rdev->flags);
2797                 err = 0;
2798         } else if (cmd_match(buf, "-failfast")) {
2799                 clear_bit(FailFast, &rdev->flags);
2800                 err = 0;
2801         } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2802                    !test_bit(Journal, &rdev->flags)) {
2803                 if (rdev->mddev->pers == NULL) {
2804                         clear_bit(In_sync, &rdev->flags);
2805                         rdev->saved_raid_disk = rdev->raid_disk;
2806                         rdev->raid_disk = -1;
2807                         err = 0;
2808                 }
2809         } else if (cmd_match(buf, "write_error")) {
2810                 set_bit(WriteErrorSeen, &rdev->flags);
2811                 err = 0;
2812         } else if (cmd_match(buf, "-write_error")) {
2813                 clear_bit(WriteErrorSeen, &rdev->flags);
2814                 err = 0;
2815         } else if (cmd_match(buf, "want_replacement")) {
2816                 /* Any non-spare device that is not a replacement can
2817                  * become want_replacement at any time, but we then need to
2818                  * check if recovery is needed.
2819                  */
2820                 if (rdev->raid_disk >= 0 &&
2821                     !test_bit(Journal, &rdev->flags) &&
2822                     !test_bit(Replacement, &rdev->flags))
2823                         set_bit(WantReplacement, &rdev->flags);
2824                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2825                 md_wakeup_thread(rdev->mddev->thread);
2826                 err = 0;
2827         } else if (cmd_match(buf, "-want_replacement")) {
2828                 /* Clearing 'want_replacement' is always allowed.
2829                  * Once replacements starts it is too late though.
2830                  */
2831                 err = 0;
2832                 clear_bit(WantReplacement, &rdev->flags);
2833         } else if (cmd_match(buf, "replacement")) {
2834                 /* Can only set a device as a replacement when array has not
2835                  * yet been started.  Once running, replacement is automatic
2836                  * from spares, or by assigning 'slot'.
2837                  */
2838                 if (rdev->mddev->pers)
2839                         err = -EBUSY;
2840                 else {
2841                         set_bit(Replacement, &rdev->flags);
2842                         err = 0;
2843                 }
2844         } else if (cmd_match(buf, "-replacement")) {
2845                 /* Similarly, can only clear Replacement before start */
2846                 if (rdev->mddev->pers)
2847                         err = -EBUSY;
2848                 else {
2849                         clear_bit(Replacement, &rdev->flags);
2850                         err = 0;
2851                 }
2852         } else if (cmd_match(buf, "re-add")) {
2853                 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
2854                         /* clear_bit is performed _after_ all the devices
2855                          * have their local Faulty bit cleared. If any writes
2856                          * happen in the meantime in the local node, they
2857                          * will land in the local bitmap, which will be synced
2858                          * by this node eventually
2859                          */
2860                         if (!mddev_is_clustered(rdev->mddev) ||
2861                             (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2862                                 clear_bit(Faulty, &rdev->flags);
2863                                 err = add_bound_rdev(rdev);
2864                         }
2865                 } else
2866                         err = -EBUSY;
2867         } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2868                 set_bit(ExternalBbl, &rdev->flags);
2869                 rdev->badblocks.shift = 0;
2870                 err = 0;
2871         } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2872                 clear_bit(ExternalBbl, &rdev->flags);
2873                 err = 0;
2874         }
2875         if (!err)
2876                 sysfs_notify_dirent_safe(rdev->sysfs_state);
2877         return err ? err : len;
2878 }
2879 static struct rdev_sysfs_entry rdev_state =
2880 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
2881
2882 static ssize_t
2883 errors_show(struct md_rdev *rdev, char *page)
2884 {
2885         return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2886 }
2887
2888 static ssize_t
2889 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2890 {
2891         unsigned int n;
2892         int rv;
2893
2894         rv = kstrtouint(buf, 10, &n);
2895         if (rv < 0)
2896                 return rv;
2897         atomic_set(&rdev->corrected_errors, n);
2898         return len;
2899 }
2900 static struct rdev_sysfs_entry rdev_errors =
2901 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2902
2903 static ssize_t
2904 slot_show(struct md_rdev *rdev, char *page)
2905 {
2906         if (test_bit(Journal, &rdev->flags))
2907                 return sprintf(page, "journal\n");
2908         else if (rdev->raid_disk < 0)
2909                 return sprintf(page, "none\n");
2910         else
2911                 return sprintf(page, "%d\n", rdev->raid_disk);
2912 }
2913
2914 static ssize_t
2915 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2916 {
2917         int slot;
2918         int err;
2919
2920         if (test_bit(Journal, &rdev->flags))
2921                 return -EBUSY;
2922         if (strncmp(buf, "none", 4)==0)
2923                 slot = -1;
2924         else {
2925                 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2926                 if (err < 0)
2927                         return err;
2928         }
2929         if (rdev->mddev->pers && slot == -1) {
2930                 /* Setting 'slot' on an active array requires also
2931                  * updating the 'rd%d' link, and communicating
2932                  * with the personality with ->hot_*_disk.
2933                  * For now we only support removing
2934                  * failed/spare devices.  This normally happens automatically,
2935                  * but not when the metadata is externally managed.
2936                  */
2937                 if (rdev->raid_disk == -1)
2938                         return -EEXIST;
2939                 /* personality does all needed checks */
2940                 if (rdev->mddev->pers->hot_remove_disk == NULL)
2941                         return -EINVAL;
2942                 clear_bit(Blocked, &rdev->flags);
2943                 remove_and_add_spares(rdev->mddev, rdev);
2944                 if (rdev->raid_disk >= 0)
2945                         return -EBUSY;
2946                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2947                 md_wakeup_thread(rdev->mddev->thread);
2948         } else if (rdev->mddev->pers) {
2949                 /* Activating a spare .. or possibly reactivating
2950                  * if we ever get bitmaps working here.
2951                  */
2952                 int err;
2953
2954                 if (rdev->raid_disk != -1)
2955                         return -EBUSY;
2956
2957                 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2958                         return -EBUSY;
2959
2960                 if (rdev->mddev->pers->hot_add_disk == NULL)
2961                         return -EINVAL;
2962
2963                 if (slot >= rdev->mddev->raid_disks &&
2964                     slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2965                         return -ENOSPC;
2966
2967                 rdev->raid_disk = slot;
2968                 if (test_bit(In_sync, &rdev->flags))
2969                         rdev->saved_raid_disk = slot;
2970                 else
2971                         rdev->saved_raid_disk = -1;
2972                 clear_bit(In_sync, &rdev->flags);
2973                 clear_bit(Bitmap_sync, &rdev->flags);
2974                 err = rdev->mddev->pers->
2975                         hot_add_disk(rdev->mddev, rdev);
2976                 if (err) {
2977                         rdev->raid_disk = -1;
2978                         return err;
2979                 } else
2980                         sysfs_notify_dirent_safe(rdev->sysfs_state);
2981                 if (sysfs_link_rdev(rdev->mddev, rdev))
2982                         /* failure here is OK */;
2983                 /* don't wakeup anyone, leave that to userspace. */
2984         } else {
2985                 if (slot >= rdev->mddev->raid_disks &&
2986                     slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2987                         return -ENOSPC;
2988                 rdev->raid_disk = slot;
2989                 /* assume it is working */
2990                 clear_bit(Faulty, &rdev->flags);
2991                 clear_bit(WriteMostly, &rdev->flags);
2992                 set_bit(In_sync, &rdev->flags);
2993                 sysfs_notify_dirent_safe(rdev->sysfs_state);
2994         }
2995         return len;
2996 }
2997
2998 static struct rdev_sysfs_entry rdev_slot =
2999 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
3000
3001 static ssize_t
3002 offset_show(struct md_rdev *rdev, char *page)
3003 {
3004         return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
3005 }
3006
3007 static ssize_t
3008 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
3009 {
3010         unsigned long long offset;
3011         if (kstrtoull(buf, 10, &offset) < 0)
3012                 return -EINVAL;
3013         if (rdev->mddev->pers && rdev->raid_disk >= 0)
3014                 return -EBUSY;
3015         if (rdev->sectors && rdev->mddev->external)
3016                 /* Must set offset before size, so overlap checks
3017                  * can be sane */
3018                 return -EBUSY;
3019         rdev->data_offset = offset;
3020         rdev->new_data_offset = offset;
3021         return len;
3022 }
3023
3024 static struct rdev_sysfs_entry rdev_offset =
3025 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
3026
3027 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3028 {
3029         return sprintf(page, "%llu\n",
3030                        (unsigned long long)rdev->new_data_offset);
3031 }
3032
3033 static ssize_t new_offset_store(struct md_rdev *rdev,
3034                                 const char *buf, size_t len)
3035 {
3036         unsigned long long new_offset;
3037         struct mddev *mddev = rdev->mddev;
3038
3039         if (kstrtoull(buf, 10, &new_offset) < 0)
3040                 return -EINVAL;
3041
3042         if (mddev->sync_thread ||
3043             test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
3044                 return -EBUSY;
3045         if (new_offset == rdev->data_offset)
3046                 /* reset is always permitted */
3047                 ;
3048         else if (new_offset > rdev->data_offset) {
3049                 /* must not push array size beyond rdev_sectors */
3050                 if (new_offset - rdev->data_offset
3051                     + mddev->dev_sectors > rdev->sectors)
3052                                 return -E2BIG;
3053         }
3054         /* Metadata worries about other space details. */
3055
3056         /* decreasing the offset is inconsistent with a backwards
3057          * reshape.
3058          */
3059         if (new_offset < rdev->data_offset &&
3060             mddev->reshape_backwards)
3061                 return -EINVAL;
3062         /* Increasing offset is inconsistent with forwards
3063          * reshape.  reshape_direction should be set to
3064          * 'backwards' first.
3065          */
3066         if (new_offset > rdev->data_offset &&
3067             !mddev->reshape_backwards)
3068                 return -EINVAL;
3069
3070         if (mddev->pers && mddev->persistent &&
3071             !super_types[mddev->major_version]
3072             .allow_new_offset(rdev, new_offset))
3073                 return -E2BIG;
3074         rdev->new_data_offset = new_offset;
3075         if (new_offset > rdev->data_offset)
3076                 mddev->reshape_backwards = 1;
3077         else if (new_offset < rdev->data_offset)
3078                 mddev->reshape_backwards = 0;
3079
3080         return len;
3081 }
3082 static struct rdev_sysfs_entry rdev_new_offset =
3083 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3084
3085 static ssize_t
3086 rdev_size_show(struct md_rdev *rdev, char *page)
3087 {
3088         return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
3089 }
3090
3091 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3092 {
3093         /* check if two start/length pairs overlap */
3094         if (s1+l1 <= s2)
3095                 return 0;
3096         if (s2+l2 <= s1)
3097                 return 0;
3098         return 1;
3099 }
3100
3101 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3102 {
3103         unsigned long long blocks;
3104         sector_t new;
3105
3106         if (kstrtoull(buf, 10, &blocks) < 0)
3107                 return -EINVAL;
3108
3109         if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3110                 return -EINVAL; /* sector conversion overflow */
3111
3112         new = blocks * 2;
3113         if (new != blocks * 2)
3114                 return -EINVAL; /* unsigned long long to sector_t overflow */
3115
3116         *sectors = new;
3117         return 0;
3118 }
3119
3120 static ssize_t
3121 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3122 {
3123         struct mddev *my_mddev = rdev->mddev;
3124         sector_t oldsectors = rdev->sectors;
3125         sector_t sectors;
3126
3127         if (test_bit(Journal, &rdev->flags))
3128                 return -EBUSY;
3129         if (strict_blocks_to_sectors(buf, &sectors) < 0)
3130                 return -EINVAL;
3131         if (rdev->data_offset != rdev->new_data_offset)
3132                 return -EINVAL; /* too confusing */
3133         if (my_mddev->pers && rdev->raid_disk >= 0) {
3134                 if (my_mddev->persistent) {
3135                         sectors = super_types[my_mddev->major_version].
3136                                 rdev_size_change(rdev, sectors);
3137                         if (!sectors)
3138                                 return -EBUSY;
3139                 } else if (!sectors)
3140                         sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
3141                                 rdev->data_offset;
3142                 if (!my_mddev->pers->resize)
3143                         /* Cannot change size for RAID0 or Linear etc */
3144                         return -EINVAL;
3145         }
3146         if (sectors < my_mddev->dev_sectors)
3147                 return -EINVAL; /* component must fit device */
3148
3149         rdev->sectors = sectors;
3150         if (sectors > oldsectors && my_mddev->external) {
3151                 /* Need to check that all other rdevs with the same
3152                  * ->bdev do not overlap.  'rcu' is sufficient to walk
3153                  * the rdev lists safely.
3154                  * This check does not provide a hard guarantee, it
3155                  * just helps avoid dangerous mistakes.
3156                  */
3157                 struct mddev *mddev;
3158                 int overlap = 0;
3159                 struct list_head *tmp;
3160
3161                 rcu_read_lock();
3162                 for_each_mddev(mddev, tmp) {
3163                         struct md_rdev *rdev2;
3164
3165                         rdev_for_each(rdev2, mddev)
3166                                 if (rdev->bdev == rdev2->bdev &&
3167                                     rdev != rdev2 &&
3168                                     overlaps(rdev->data_offset, rdev->sectors,
3169                                              rdev2->data_offset,
3170                                              rdev2->sectors)) {
3171                                         overlap = 1;
3172                                         break;
3173                                 }
3174                         if (overlap) {
3175                                 mddev_put(mddev);
3176                                 break;
3177                         }
3178                 }
3179                 rcu_read_unlock();
3180                 if (overlap) {
3181                         /* Someone else could have slipped in a size
3182                          * change here, but doing so is just silly.