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