xref: /linux/drivers/md/md.c (revision 55f1b540d893da740a81200450014c45a8103f54)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3    md.c : Multiple Devices driver for Linux
4      Copyright (C) 1998, 1999, 2000 Ingo Molnar
5 
6      completely rewritten, based on the MD driver code from Marc Zyngier
7 
8    Changes:
9 
10    - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11    - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12    - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13    - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14    - kmod support by: Cyrus Durgin
15    - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16    - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17 
18    - lots of fixes and improvements to the RAID1/RAID5 and generic
19      RAID code (such as request based resynchronization):
20 
21      Neil Brown <neilb@cse.unsw.edu.au>.
22 
23    - persistent bitmap code
24      Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25 
26 
27    Errors, Warnings, etc.
28    Please use:
29      pr_crit() for error conditions that risk data loss
30      pr_err() for error conditions that are unexpected, like an IO error
31          or internal inconsistency
32      pr_warn() for error conditions that could have been predicated, like
33          adding a device to an array when it has incompatible metadata
34      pr_info() for every interesting, very rare events, like an array starting
35          or stopping, or resync starting or stopping
36      pr_debug() for everything else.
37 
38 */
39 
40 #include <linux/sched/mm.h>
41 #include <linux/sched/signal.h>
42 #include <linux/kthread.h>
43 #include <linux/blkdev.h>
44 #include <linux/blk-integrity.h>
45 #include <linux/badblocks.h>
46 #include <linux/sysctl.h>
47 #include <linux/seq_file.h>
48 #include <linux/fs.h>
49 #include <linux/poll.h>
50 #include <linux/ctype.h>
51 #include <linux/string.h>
52 #include <linux/hdreg.h>
53 #include <linux/proc_fs.h>
54 #include <linux/random.h>
55 #include <linux/major.h>
56 #include <linux/module.h>
57 #include <linux/reboot.h>
58 #include <linux/file.h>
59 #include <linux/compat.h>
60 #include <linux/delay.h>
61 #include <linux/raid/md_p.h>
62 #include <linux/raid/md_u.h>
63 #include <linux/raid/detect.h>
64 #include <linux/slab.h>
65 #include <linux/percpu-refcount.h>
66 #include <linux/part_stat.h>
67 
68 #include "md.h"
69 #include "md-bitmap.h"
70 #include "md-cluster.h"
71 
72 static const char *action_name[NR_SYNC_ACTIONS] = {
73 	[ACTION_RESYNC]		= "resync",
74 	[ACTION_RECOVER]	= "recover",
75 	[ACTION_CHECK]		= "check",
76 	[ACTION_REPAIR]		= "repair",
77 	[ACTION_RESHAPE]	= "reshape",
78 	[ACTION_FROZEN]		= "frozen",
79 	[ACTION_IDLE]		= "idle",
80 };
81 
82 /* pers_list is a list of registered personalities protected by pers_lock. */
83 static LIST_HEAD(pers_list);
84 static DEFINE_SPINLOCK(pers_lock);
85 
86 static const struct kobj_type md_ktype;
87 
88 const struct md_cluster_operations *md_cluster_ops;
89 EXPORT_SYMBOL(md_cluster_ops);
90 static struct module *md_cluster_mod;
91 
92 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
93 static struct workqueue_struct *md_wq;
94 
95 /*
96  * This workqueue is used for sync_work to register new sync_thread, and for
97  * del_work to remove rdev, and for event_work that is only set by dm-raid.
98  *
99  * Noted that sync_work will grab reconfig_mutex, hence never flush this
100  * workqueue whith reconfig_mutex grabbed.
101  */
102 static struct workqueue_struct *md_misc_wq;
103 struct workqueue_struct *md_bitmap_wq;
104 
105 static int remove_and_add_spares(struct mddev *mddev,
106 				 struct md_rdev *this);
107 static void mddev_detach(struct mddev *mddev);
108 static void export_rdev(struct md_rdev *rdev, struct mddev *mddev);
109 static void md_wakeup_thread_directly(struct md_thread __rcu *thread);
110 
111 /*
112  * Default number of read corrections we'll attempt on an rdev
113  * before ejecting it from the array. We divide the read error
114  * count by 2 for every hour elapsed between read errors.
115  */
116 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
117 /* Default safemode delay: 200 msec */
118 #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
119 /*
120  * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
121  * is 1000 KB/sec, so the extra system load does not show up that much.
122  * Increase it if you want to have more _guaranteed_ speed. Note that
123  * the RAID driver will use the maximum available bandwidth if the IO
124  * subsystem is idle. There is also an 'absolute maximum' reconstruction
125  * speed limit - in case reconstruction slows down your system despite
126  * idle IO detection.
127  *
128  * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
129  * or /sys/block/mdX/md/sync_speed_{min,max}
130  */
131 
132 static int sysctl_speed_limit_min = 1000;
133 static int sysctl_speed_limit_max = 200000;
134 static inline int speed_min(struct mddev *mddev)
135 {
136 	return mddev->sync_speed_min ?
137 		mddev->sync_speed_min : sysctl_speed_limit_min;
138 }
139 
140 static inline int speed_max(struct mddev *mddev)
141 {
142 	return mddev->sync_speed_max ?
143 		mddev->sync_speed_max : sysctl_speed_limit_max;
144 }
145 
146 static void rdev_uninit_serial(struct md_rdev *rdev)
147 {
148 	if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
149 		return;
150 
151 	kvfree(rdev->serial);
152 	rdev->serial = NULL;
153 }
154 
155 static void rdevs_uninit_serial(struct mddev *mddev)
156 {
157 	struct md_rdev *rdev;
158 
159 	rdev_for_each(rdev, mddev)
160 		rdev_uninit_serial(rdev);
161 }
162 
163 static int rdev_init_serial(struct md_rdev *rdev)
164 {
165 	/* serial_nums equals with BARRIER_BUCKETS_NR */
166 	int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
167 	struct serial_in_rdev *serial = NULL;
168 
169 	if (test_bit(CollisionCheck, &rdev->flags))
170 		return 0;
171 
172 	serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
173 			  GFP_KERNEL);
174 	if (!serial)
175 		return -ENOMEM;
176 
177 	for (i = 0; i < serial_nums; i++) {
178 		struct serial_in_rdev *serial_tmp = &serial[i];
179 
180 		spin_lock_init(&serial_tmp->serial_lock);
181 		serial_tmp->serial_rb = RB_ROOT_CACHED;
182 		init_waitqueue_head(&serial_tmp->serial_io_wait);
183 	}
184 
185 	rdev->serial = serial;
186 	set_bit(CollisionCheck, &rdev->flags);
187 
188 	return 0;
189 }
190 
191 static int rdevs_init_serial(struct mddev *mddev)
192 {
193 	struct md_rdev *rdev;
194 	int ret = 0;
195 
196 	rdev_for_each(rdev, mddev) {
197 		ret = rdev_init_serial(rdev);
198 		if (ret)
199 			break;
200 	}
201 
202 	/* Free all resources if pool is not existed */
203 	if (ret && !mddev->serial_info_pool)
204 		rdevs_uninit_serial(mddev);
205 
206 	return ret;
207 }
208 
209 /*
210  * rdev needs to enable serial stuffs if it meets the conditions:
211  * 1. it is multi-queue device flaged with writemostly.
212  * 2. the write-behind mode is enabled.
213  */
214 static int rdev_need_serial(struct md_rdev *rdev)
215 {
216 	return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
217 		rdev->bdev->bd_disk->queue->nr_hw_queues != 1 &&
218 		test_bit(WriteMostly, &rdev->flags));
219 }
220 
221 /*
222  * Init resource for rdev(s), then create serial_info_pool if:
223  * 1. rdev is the first device which return true from rdev_enable_serial.
224  * 2. rdev is NULL, means we want to enable serialization for all rdevs.
225  */
226 void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev)
227 {
228 	int ret = 0;
229 
230 	if (rdev && !rdev_need_serial(rdev) &&
231 	    !test_bit(CollisionCheck, &rdev->flags))
232 		return;
233 
234 	if (!rdev)
235 		ret = rdevs_init_serial(mddev);
236 	else
237 		ret = rdev_init_serial(rdev);
238 	if (ret)
239 		return;
240 
241 	if (mddev->serial_info_pool == NULL) {
242 		/*
243 		 * already in memalloc noio context by
244 		 * mddev_suspend()
245 		 */
246 		mddev->serial_info_pool =
247 			mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
248 						sizeof(struct serial_info));
249 		if (!mddev->serial_info_pool) {
250 			rdevs_uninit_serial(mddev);
251 			pr_err("can't alloc memory pool for serialization\n");
252 		}
253 	}
254 }
255 
256 /*
257  * Free resource from rdev(s), and destroy serial_info_pool under conditions:
258  * 1. rdev is the last device flaged with CollisionCheck.
259  * 2. when bitmap is destroyed while policy is not enabled.
260  * 3. for disable policy, the pool is destroyed only when no rdev needs it.
261  */
262 void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev)
263 {
264 	if (rdev && !test_bit(CollisionCheck, &rdev->flags))
265 		return;
266 
267 	if (mddev->serial_info_pool) {
268 		struct md_rdev *temp;
269 		int num = 0; /* used to track if other rdevs need the pool */
270 
271 		rdev_for_each(temp, mddev) {
272 			if (!rdev) {
273 				if (!mddev->serialize_policy ||
274 				    !rdev_need_serial(temp))
275 					rdev_uninit_serial(temp);
276 				else
277 					num++;
278 			} else if (temp != rdev &&
279 				   test_bit(CollisionCheck, &temp->flags))
280 				num++;
281 		}
282 
283 		if (rdev)
284 			rdev_uninit_serial(rdev);
285 
286 		if (num)
287 			pr_info("The mempool could be used by other devices\n");
288 		else {
289 			mempool_destroy(mddev->serial_info_pool);
290 			mddev->serial_info_pool = NULL;
291 		}
292 	}
293 }
294 
295 static struct ctl_table_header *raid_table_header;
296 
297 static struct ctl_table raid_table[] = {
298 	{
299 		.procname	= "speed_limit_min",
300 		.data		= &sysctl_speed_limit_min,
301 		.maxlen		= sizeof(int),
302 		.mode		= S_IRUGO|S_IWUSR,
303 		.proc_handler	= proc_dointvec,
304 	},
305 	{
306 		.procname	= "speed_limit_max",
307 		.data		= &sysctl_speed_limit_max,
308 		.maxlen		= sizeof(int),
309 		.mode		= S_IRUGO|S_IWUSR,
310 		.proc_handler	= proc_dointvec,
311 	},
312 };
313 
314 static int start_readonly;
315 
316 /*
317  * The original mechanism for creating an md device is to create
318  * a device node in /dev and to open it.  This causes races with device-close.
319  * The preferred method is to write to the "new_array" module parameter.
320  * This can avoid races.
321  * Setting create_on_open to false disables the original mechanism
322  * so all the races disappear.
323  */
324 static bool create_on_open = true;
325 
326 /*
327  * We have a system wide 'event count' that is incremented
328  * on any 'interesting' event, and readers of /proc/mdstat
329  * can use 'poll' or 'select' to find out when the event
330  * count increases.
331  *
332  * Events are:
333  *  start array, stop array, error, add device, remove device,
334  *  start build, activate spare
335  */
336 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
337 static atomic_t md_event_count;
338 void md_new_event(void)
339 {
340 	atomic_inc(&md_event_count);
341 	wake_up(&md_event_waiters);
342 }
343 EXPORT_SYMBOL_GPL(md_new_event);
344 
345 /*
346  * Enables to iterate over all existing md arrays
347  * all_mddevs_lock protects this list.
348  */
349 static LIST_HEAD(all_mddevs);
350 static DEFINE_SPINLOCK(all_mddevs_lock);
351 
352 static bool is_md_suspended(struct mddev *mddev)
353 {
354 	return percpu_ref_is_dying(&mddev->active_io);
355 }
356 /* Rather than calling directly into the personality make_request function,
357  * IO requests come here first so that we can check if the device is
358  * being suspended pending a reconfiguration.
359  * We hold a refcount over the call to ->make_request.  By the time that
360  * call has finished, the bio has been linked into some internal structure
361  * and so is visible to ->quiesce(), so we don't need the refcount any more.
362  */
363 static bool is_suspended(struct mddev *mddev, struct bio *bio)
364 {
365 	if (is_md_suspended(mddev))
366 		return true;
367 	if (bio_data_dir(bio) != WRITE)
368 		return false;
369 	if (READ_ONCE(mddev->suspend_lo) >= READ_ONCE(mddev->suspend_hi))
370 		return false;
371 	if (bio->bi_iter.bi_sector >= READ_ONCE(mddev->suspend_hi))
372 		return false;
373 	if (bio_end_sector(bio) < READ_ONCE(mddev->suspend_lo))
374 		return false;
375 	return true;
376 }
377 
378 bool md_handle_request(struct mddev *mddev, struct bio *bio)
379 {
380 check_suspended:
381 	if (is_suspended(mddev, bio)) {
382 		DEFINE_WAIT(__wait);
383 		/* Bail out if REQ_NOWAIT is set for the bio */
384 		if (bio->bi_opf & REQ_NOWAIT) {
385 			bio_wouldblock_error(bio);
386 			return true;
387 		}
388 		for (;;) {
389 			prepare_to_wait(&mddev->sb_wait, &__wait,
390 					TASK_UNINTERRUPTIBLE);
391 			if (!is_suspended(mddev, bio))
392 				break;
393 			schedule();
394 		}
395 		finish_wait(&mddev->sb_wait, &__wait);
396 	}
397 	if (!percpu_ref_tryget_live(&mddev->active_io))
398 		goto check_suspended;
399 
400 	if (!mddev->pers->make_request(mddev, bio)) {
401 		percpu_ref_put(&mddev->active_io);
402 		if (!mddev->gendisk && mddev->pers->prepare_suspend)
403 			return false;
404 		goto check_suspended;
405 	}
406 
407 	percpu_ref_put(&mddev->active_io);
408 	return true;
409 }
410 EXPORT_SYMBOL(md_handle_request);
411 
412 static void md_submit_bio(struct bio *bio)
413 {
414 	const int rw = bio_data_dir(bio);
415 	struct mddev *mddev = bio->bi_bdev->bd_disk->private_data;
416 
417 	if (mddev == NULL || mddev->pers == NULL) {
418 		bio_io_error(bio);
419 		return;
420 	}
421 
422 	if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
423 		bio_io_error(bio);
424 		return;
425 	}
426 
427 	bio = bio_split_to_limits(bio);
428 	if (!bio)
429 		return;
430 
431 	if (mddev->ro == MD_RDONLY && unlikely(rw == WRITE)) {
432 		if (bio_sectors(bio) != 0)
433 			bio->bi_status = BLK_STS_IOERR;
434 		bio_endio(bio);
435 		return;
436 	}
437 
438 	/* bio could be mergeable after passing to underlayer */
439 	bio->bi_opf &= ~REQ_NOMERGE;
440 
441 	md_handle_request(mddev, bio);
442 }
443 
444 /*
445  * Make sure no new requests are submitted to the device, and any requests that
446  * have been submitted are completely handled.
447  */
448 int mddev_suspend(struct mddev *mddev, bool interruptible)
449 {
450 	int err = 0;
451 
452 	/*
453 	 * hold reconfig_mutex to wait for normal io will deadlock, because
454 	 * other context can't update super_block, and normal io can rely on
455 	 * updating super_block.
456 	 */
457 	lockdep_assert_not_held(&mddev->reconfig_mutex);
458 
459 	if (interruptible)
460 		err = mutex_lock_interruptible(&mddev->suspend_mutex);
461 	else
462 		mutex_lock(&mddev->suspend_mutex);
463 	if (err)
464 		return err;
465 
466 	if (mddev->suspended) {
467 		WRITE_ONCE(mddev->suspended, mddev->suspended + 1);
468 		mutex_unlock(&mddev->suspend_mutex);
469 		return 0;
470 	}
471 
472 	percpu_ref_kill(&mddev->active_io);
473 	if (interruptible)
474 		err = wait_event_interruptible(mddev->sb_wait,
475 				percpu_ref_is_zero(&mddev->active_io));
476 	else
477 		wait_event(mddev->sb_wait,
478 				percpu_ref_is_zero(&mddev->active_io));
479 	if (err) {
480 		percpu_ref_resurrect(&mddev->active_io);
481 		mutex_unlock(&mddev->suspend_mutex);
482 		return err;
483 	}
484 
485 	/*
486 	 * For raid456, io might be waiting for reshape to make progress,
487 	 * allow new reshape to start while waiting for io to be done to
488 	 * prevent deadlock.
489 	 */
490 	WRITE_ONCE(mddev->suspended, mddev->suspended + 1);
491 
492 	/* restrict memory reclaim I/O during raid array is suspend */
493 	mddev->noio_flag = memalloc_noio_save();
494 
495 	mutex_unlock(&mddev->suspend_mutex);
496 	return 0;
497 }
498 EXPORT_SYMBOL_GPL(mddev_suspend);
499 
500 static void __mddev_resume(struct mddev *mddev, bool recovery_needed)
501 {
502 	lockdep_assert_not_held(&mddev->reconfig_mutex);
503 
504 	mutex_lock(&mddev->suspend_mutex);
505 	WRITE_ONCE(mddev->suspended, mddev->suspended - 1);
506 	if (mddev->suspended) {
507 		mutex_unlock(&mddev->suspend_mutex);
508 		return;
509 	}
510 
511 	/* entred the memalloc scope from mddev_suspend() */
512 	memalloc_noio_restore(mddev->noio_flag);
513 
514 	percpu_ref_resurrect(&mddev->active_io);
515 	wake_up(&mddev->sb_wait);
516 
517 	if (recovery_needed)
518 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
519 	md_wakeup_thread(mddev->thread);
520 	md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
521 
522 	mutex_unlock(&mddev->suspend_mutex);
523 }
524 
525 void mddev_resume(struct mddev *mddev)
526 {
527 	return __mddev_resume(mddev, true);
528 }
529 EXPORT_SYMBOL_GPL(mddev_resume);
530 
531 /* sync bdev before setting device to readonly or stopping raid*/
532 static int mddev_set_closing_and_sync_blockdev(struct mddev *mddev, int opener_num)
533 {
534 	mutex_lock(&mddev->open_mutex);
535 	if (mddev->pers && atomic_read(&mddev->openers) > opener_num) {
536 		mutex_unlock(&mddev->open_mutex);
537 		return -EBUSY;
538 	}
539 	if (test_and_set_bit(MD_CLOSING, &mddev->flags)) {
540 		mutex_unlock(&mddev->open_mutex);
541 		return -EBUSY;
542 	}
543 	mutex_unlock(&mddev->open_mutex);
544 
545 	sync_blockdev(mddev->gendisk->part0);
546 	return 0;
547 }
548 
549 bool md_flush_request(struct mddev *mddev, struct bio *bio)
550 {
551 	struct md_rdev *rdev;
552 	struct bio *new;
553 
554 	/*
555 	 * md_flush_reqeust() should be called under md_handle_request() and
556 	 * 'active_io' is already grabbed. Hence it's safe to get rdev directly
557 	 * without rcu protection.
558 	 */
559 	WARN_ON(percpu_ref_is_zero(&mddev->active_io));
560 
561 	rdev_for_each(rdev, mddev) {
562 		if (rdev->raid_disk < 0 || test_bit(Faulty, &rdev->flags))
563 			continue;
564 
565 		new = bio_alloc_bioset(rdev->bdev, 0,
566 				       REQ_OP_WRITE | REQ_PREFLUSH, GFP_NOIO,
567 				       &mddev->bio_set);
568 		bio_chain(new, bio);
569 		submit_bio(new);
570 	}
571 
572 	if (bio_sectors(bio) == 0) {
573 		bio_endio(bio);
574 		return true;
575 	}
576 
577 	bio->bi_opf &= ~REQ_PREFLUSH;
578 	return false;
579 }
580 EXPORT_SYMBOL(md_flush_request);
581 
582 static inline struct mddev *mddev_get(struct mddev *mddev)
583 {
584 	lockdep_assert_held(&all_mddevs_lock);
585 
586 	if (test_bit(MD_DELETED, &mddev->flags))
587 		return NULL;
588 	atomic_inc(&mddev->active);
589 	return mddev;
590 }
591 
592 static void mddev_delayed_delete(struct work_struct *ws);
593 
594 static void __mddev_put(struct mddev *mddev)
595 {
596 	if (mddev->raid_disks || !list_empty(&mddev->disks) ||
597 	    mddev->ctime || mddev->hold_active)
598 		return;
599 
600 	/* Array is not configured at all, and not held active, so destroy it */
601 	set_bit(MD_DELETED, &mddev->flags);
602 
603 	/*
604 	 * Call queue_work inside the spinlock so that flush_workqueue() after
605 	 * mddev_find will succeed in waiting for the work to be done.
606 	 */
607 	queue_work(md_misc_wq, &mddev->del_work);
608 }
609 
610 void mddev_put(struct mddev *mddev)
611 {
612 	if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
613 		return;
614 
615 	__mddev_put(mddev);
616 	spin_unlock(&all_mddevs_lock);
617 }
618 
619 static void md_safemode_timeout(struct timer_list *t);
620 static void md_start_sync(struct work_struct *ws);
621 
622 static void active_io_release(struct percpu_ref *ref)
623 {
624 	struct mddev *mddev = container_of(ref, struct mddev, active_io);
625 
626 	wake_up(&mddev->sb_wait);
627 }
628 
629 static void no_op(struct percpu_ref *r) {}
630 
631 int mddev_init(struct mddev *mddev)
632 {
633 
634 	if (percpu_ref_init(&mddev->active_io, active_io_release,
635 			    PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
636 		return -ENOMEM;
637 
638 	if (percpu_ref_init(&mddev->writes_pending, no_op,
639 			    PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
640 		percpu_ref_exit(&mddev->active_io);
641 		return -ENOMEM;
642 	}
643 
644 	/* We want to start with the refcount at zero */
645 	percpu_ref_put(&mddev->writes_pending);
646 
647 	mutex_init(&mddev->open_mutex);
648 	mutex_init(&mddev->reconfig_mutex);
649 	mutex_init(&mddev->suspend_mutex);
650 	mutex_init(&mddev->bitmap_info.mutex);
651 	INIT_LIST_HEAD(&mddev->disks);
652 	INIT_LIST_HEAD(&mddev->all_mddevs);
653 	INIT_LIST_HEAD(&mddev->deleting);
654 	timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
655 	atomic_set(&mddev->active, 1);
656 	atomic_set(&mddev->openers, 0);
657 	atomic_set(&mddev->sync_seq, 0);
658 	spin_lock_init(&mddev->lock);
659 	init_waitqueue_head(&mddev->sb_wait);
660 	init_waitqueue_head(&mddev->recovery_wait);
661 	mddev->reshape_position = MaxSector;
662 	mddev->reshape_backwards = 0;
663 	mddev->last_sync_action = ACTION_IDLE;
664 	mddev->resync_min = 0;
665 	mddev->resync_max = MaxSector;
666 	mddev->level = LEVEL_NONE;
667 	mddev_set_bitmap_ops(mddev);
668 
669 	INIT_WORK(&mddev->sync_work, md_start_sync);
670 	INIT_WORK(&mddev->del_work, mddev_delayed_delete);
671 
672 	return 0;
673 }
674 EXPORT_SYMBOL_GPL(mddev_init);
675 
676 void mddev_destroy(struct mddev *mddev)
677 {
678 	percpu_ref_exit(&mddev->active_io);
679 	percpu_ref_exit(&mddev->writes_pending);
680 }
681 EXPORT_SYMBOL_GPL(mddev_destroy);
682 
683 static struct mddev *mddev_find_locked(dev_t unit)
684 {
685 	struct mddev *mddev;
686 
687 	list_for_each_entry(mddev, &all_mddevs, all_mddevs)
688 		if (mddev->unit == unit)
689 			return mddev;
690 
691 	return NULL;
692 }
693 
694 /* find an unused unit number */
695 static dev_t mddev_alloc_unit(void)
696 {
697 	static int next_minor = 512;
698 	int start = next_minor;
699 	bool is_free = 0;
700 	dev_t dev = 0;
701 
702 	while (!is_free) {
703 		dev = MKDEV(MD_MAJOR, next_minor);
704 		next_minor++;
705 		if (next_minor > MINORMASK)
706 			next_minor = 0;
707 		if (next_minor == start)
708 			return 0;		/* Oh dear, all in use. */
709 		is_free = !mddev_find_locked(dev);
710 	}
711 
712 	return dev;
713 }
714 
715 static struct mddev *mddev_alloc(dev_t unit)
716 {
717 	struct mddev *new;
718 	int error;
719 
720 	if (unit && MAJOR(unit) != MD_MAJOR)
721 		unit &= ~((1 << MdpMinorShift) - 1);
722 
723 	new = kzalloc(sizeof(*new), GFP_KERNEL);
724 	if (!new)
725 		return ERR_PTR(-ENOMEM);
726 
727 	error = mddev_init(new);
728 	if (error)
729 		goto out_free_new;
730 
731 	spin_lock(&all_mddevs_lock);
732 	if (unit) {
733 		error = -EEXIST;
734 		if (mddev_find_locked(unit))
735 			goto out_destroy_new;
736 		new->unit = unit;
737 		if (MAJOR(unit) == MD_MAJOR)
738 			new->md_minor = MINOR(unit);
739 		else
740 			new->md_minor = MINOR(unit) >> MdpMinorShift;
741 		new->hold_active = UNTIL_IOCTL;
742 	} else {
743 		error = -ENODEV;
744 		new->unit = mddev_alloc_unit();
745 		if (!new->unit)
746 			goto out_destroy_new;
747 		new->md_minor = MINOR(new->unit);
748 		new->hold_active = UNTIL_STOP;
749 	}
750 
751 	list_add(&new->all_mddevs, &all_mddevs);
752 	spin_unlock(&all_mddevs_lock);
753 	return new;
754 
755 out_destroy_new:
756 	spin_unlock(&all_mddevs_lock);
757 	mddev_destroy(new);
758 out_free_new:
759 	kfree(new);
760 	return ERR_PTR(error);
761 }
762 
763 static void mddev_free(struct mddev *mddev)
764 {
765 	spin_lock(&all_mddevs_lock);
766 	list_del(&mddev->all_mddevs);
767 	spin_unlock(&all_mddevs_lock);
768 
769 	mddev_destroy(mddev);
770 	kfree(mddev);
771 }
772 
773 static const struct attribute_group md_redundancy_group;
774 
775 void mddev_unlock(struct mddev *mddev)
776 {
777 	struct md_rdev *rdev;
778 	struct md_rdev *tmp;
779 	LIST_HEAD(delete);
780 
781 	if (!list_empty(&mddev->deleting))
782 		list_splice_init(&mddev->deleting, &delete);
783 
784 	if (mddev->to_remove) {
785 		/* These cannot be removed under reconfig_mutex as
786 		 * an access to the files will try to take reconfig_mutex
787 		 * while holding the file unremovable, which leads to
788 		 * a deadlock.
789 		 * So hold set sysfs_active while the remove in happeing,
790 		 * and anything else which might set ->to_remove or my
791 		 * otherwise change the sysfs namespace will fail with
792 		 * -EBUSY if sysfs_active is still set.
793 		 * We set sysfs_active under reconfig_mutex and elsewhere
794 		 * test it under the same mutex to ensure its correct value
795 		 * is seen.
796 		 */
797 		const struct attribute_group *to_remove = mddev->to_remove;
798 		mddev->to_remove = NULL;
799 		mddev->sysfs_active = 1;
800 		mutex_unlock(&mddev->reconfig_mutex);
801 
802 		if (mddev->kobj.sd) {
803 			if (to_remove != &md_redundancy_group)
804 				sysfs_remove_group(&mddev->kobj, to_remove);
805 			if (mddev->pers == NULL ||
806 			    mddev->pers->sync_request == NULL) {
807 				sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
808 				if (mddev->sysfs_action)
809 					sysfs_put(mddev->sysfs_action);
810 				if (mddev->sysfs_completed)
811 					sysfs_put(mddev->sysfs_completed);
812 				if (mddev->sysfs_degraded)
813 					sysfs_put(mddev->sysfs_degraded);
814 				mddev->sysfs_action = NULL;
815 				mddev->sysfs_completed = NULL;
816 				mddev->sysfs_degraded = NULL;
817 			}
818 		}
819 		mddev->sysfs_active = 0;
820 	} else
821 		mutex_unlock(&mddev->reconfig_mutex);
822 
823 	md_wakeup_thread(mddev->thread);
824 	wake_up(&mddev->sb_wait);
825 
826 	list_for_each_entry_safe(rdev, tmp, &delete, same_set) {
827 		list_del_init(&rdev->same_set);
828 		kobject_del(&rdev->kobj);
829 		export_rdev(rdev, mddev);
830 	}
831 }
832 EXPORT_SYMBOL_GPL(mddev_unlock);
833 
834 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
835 {
836 	struct md_rdev *rdev;
837 
838 	rdev_for_each_rcu(rdev, mddev)
839 		if (rdev->desc_nr == nr)
840 			return rdev;
841 
842 	return NULL;
843 }
844 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
845 
846 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
847 {
848 	struct md_rdev *rdev;
849 
850 	rdev_for_each(rdev, mddev)
851 		if (rdev->bdev->bd_dev == dev)
852 			return rdev;
853 
854 	return NULL;
855 }
856 
857 struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
858 {
859 	struct md_rdev *rdev;
860 
861 	rdev_for_each_rcu(rdev, mddev)
862 		if (rdev->bdev->bd_dev == dev)
863 			return rdev;
864 
865 	return NULL;
866 }
867 EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
868 
869 static struct md_personality *find_pers(int level, char *clevel)
870 {
871 	struct md_personality *pers;
872 	list_for_each_entry(pers, &pers_list, list) {
873 		if (level != LEVEL_NONE && pers->level == level)
874 			return pers;
875 		if (strcmp(pers->name, clevel)==0)
876 			return pers;
877 	}
878 	return NULL;
879 }
880 
881 /* return the offset of the super block in 512byte sectors */
882 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
883 {
884 	return MD_NEW_SIZE_SECTORS(bdev_nr_sectors(rdev->bdev));
885 }
886 
887 static int alloc_disk_sb(struct md_rdev *rdev)
888 {
889 	rdev->sb_page = alloc_page(GFP_KERNEL);
890 	if (!rdev->sb_page)
891 		return -ENOMEM;
892 	return 0;
893 }
894 
895 void md_rdev_clear(struct md_rdev *rdev)
896 {
897 	if (rdev->sb_page) {
898 		put_page(rdev->sb_page);
899 		rdev->sb_loaded = 0;
900 		rdev->sb_page = NULL;
901 		rdev->sb_start = 0;
902 		rdev->sectors = 0;
903 	}
904 	if (rdev->bb_page) {
905 		put_page(rdev->bb_page);
906 		rdev->bb_page = NULL;
907 	}
908 	badblocks_exit(&rdev->badblocks);
909 }
910 EXPORT_SYMBOL_GPL(md_rdev_clear);
911 
912 static void super_written(struct bio *bio)
913 {
914 	struct md_rdev *rdev = bio->bi_private;
915 	struct mddev *mddev = rdev->mddev;
916 
917 	if (bio->bi_status) {
918 		pr_err("md: %s gets error=%d\n", __func__,
919 		       blk_status_to_errno(bio->bi_status));
920 		md_error(mddev, rdev);
921 		if (!test_bit(Faulty, &rdev->flags)
922 		    && (bio->bi_opf & MD_FAILFAST)) {
923 			set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
924 			set_bit(LastDev, &rdev->flags);
925 		}
926 	} else
927 		clear_bit(LastDev, &rdev->flags);
928 
929 	bio_put(bio);
930 
931 	rdev_dec_pending(rdev, mddev);
932 
933 	if (atomic_dec_and_test(&mddev->pending_writes))
934 		wake_up(&mddev->sb_wait);
935 }
936 
937 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
938 		   sector_t sector, int size, struct page *page)
939 {
940 	/* write first size bytes of page to sector of rdev
941 	 * Increment mddev->pending_writes before returning
942 	 * and decrement it on completion, waking up sb_wait
943 	 * if zero is reached.
944 	 * If an error occurred, call md_error
945 	 */
946 	struct bio *bio;
947 
948 	if (!page)
949 		return;
950 
951 	if (test_bit(Faulty, &rdev->flags))
952 		return;
953 
954 	bio = bio_alloc_bioset(rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev,
955 			      1,
956 			      REQ_OP_WRITE | REQ_SYNC | REQ_IDLE | REQ_META
957 				  | REQ_PREFLUSH | REQ_FUA,
958 			      GFP_NOIO, &mddev->sync_set);
959 
960 	atomic_inc(&rdev->nr_pending);
961 
962 	bio->bi_iter.bi_sector = sector;
963 	__bio_add_page(bio, page, size, 0);
964 	bio->bi_private = rdev;
965 	bio->bi_end_io = super_written;
966 
967 	if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
968 	    test_bit(FailFast, &rdev->flags) &&
969 	    !test_bit(LastDev, &rdev->flags))
970 		bio->bi_opf |= MD_FAILFAST;
971 
972 	atomic_inc(&mddev->pending_writes);
973 	submit_bio(bio);
974 }
975 
976 int md_super_wait(struct mddev *mddev)
977 {
978 	/* wait for all superblock writes that were scheduled to complete */
979 	wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
980 	if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
981 		return -EAGAIN;
982 	return 0;
983 }
984 
985 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
986 		 struct page *page, blk_opf_t opf, bool metadata_op)
987 {
988 	struct bio bio;
989 	struct bio_vec bvec;
990 
991 	if (metadata_op && rdev->meta_bdev)
992 		bio_init(&bio, rdev->meta_bdev, &bvec, 1, opf);
993 	else
994 		bio_init(&bio, rdev->bdev, &bvec, 1, opf);
995 
996 	if (metadata_op)
997 		bio.bi_iter.bi_sector = sector + rdev->sb_start;
998 	else if (rdev->mddev->reshape_position != MaxSector &&
999 		 (rdev->mddev->reshape_backwards ==
1000 		  (sector >= rdev->mddev->reshape_position)))
1001 		bio.bi_iter.bi_sector = sector + rdev->new_data_offset;
1002 	else
1003 		bio.bi_iter.bi_sector = sector + rdev->data_offset;
1004 	__bio_add_page(&bio, page, size, 0);
1005 
1006 	submit_bio_wait(&bio);
1007 
1008 	return !bio.bi_status;
1009 }
1010 EXPORT_SYMBOL_GPL(sync_page_io);
1011 
1012 static int read_disk_sb(struct md_rdev *rdev, int size)
1013 {
1014 	if (rdev->sb_loaded)
1015 		return 0;
1016 
1017 	if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, true))
1018 		goto fail;
1019 	rdev->sb_loaded = 1;
1020 	return 0;
1021 
1022 fail:
1023 	pr_err("md: disabled device %pg, could not read superblock.\n",
1024 	       rdev->bdev);
1025 	return -EINVAL;
1026 }
1027 
1028 static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
1029 {
1030 	return	sb1->set_uuid0 == sb2->set_uuid0 &&
1031 		sb1->set_uuid1 == sb2->set_uuid1 &&
1032 		sb1->set_uuid2 == sb2->set_uuid2 &&
1033 		sb1->set_uuid3 == sb2->set_uuid3;
1034 }
1035 
1036 static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
1037 {
1038 	int ret;
1039 	mdp_super_t *tmp1, *tmp2;
1040 
1041 	tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1042 	tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1043 
1044 	if (!tmp1 || !tmp2) {
1045 		ret = 0;
1046 		goto abort;
1047 	}
1048 
1049 	*tmp1 = *sb1;
1050 	*tmp2 = *sb2;
1051 
1052 	/*
1053 	 * nr_disks is not constant
1054 	 */
1055 	tmp1->nr_disks = 0;
1056 	tmp2->nr_disks = 0;
1057 
1058 	ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
1059 abort:
1060 	kfree(tmp1);
1061 	kfree(tmp2);
1062 	return ret;
1063 }
1064 
1065 static u32 md_csum_fold(u32 csum)
1066 {
1067 	csum = (csum & 0xffff) + (csum >> 16);
1068 	return (csum & 0xffff) + (csum >> 16);
1069 }
1070 
1071 static unsigned int calc_sb_csum(mdp_super_t *sb)
1072 {
1073 	u64 newcsum = 0;
1074 	u32 *sb32 = (u32*)sb;
1075 	int i;
1076 	unsigned int disk_csum, csum;
1077 
1078 	disk_csum = sb->sb_csum;
1079 	sb->sb_csum = 0;
1080 
1081 	for (i = 0; i < MD_SB_BYTES/4 ; i++)
1082 		newcsum += sb32[i];
1083 	csum = (newcsum & 0xffffffff) + (newcsum>>32);
1084 
1085 #ifdef CONFIG_ALPHA
1086 	/* This used to use csum_partial, which was wrong for several
1087 	 * reasons including that different results are returned on
1088 	 * different architectures.  It isn't critical that we get exactly
1089 	 * the same return value as before (we always csum_fold before
1090 	 * testing, and that removes any differences).  However as we
1091 	 * know that csum_partial always returned a 16bit value on
1092 	 * alphas, do a fold to maximise conformity to previous behaviour.
1093 	 */
1094 	sb->sb_csum = md_csum_fold(disk_csum);
1095 #else
1096 	sb->sb_csum = disk_csum;
1097 #endif
1098 	return csum;
1099 }
1100 
1101 /*
1102  * Handle superblock details.
1103  * We want to be able to handle multiple superblock formats
1104  * so we have a common interface to them all, and an array of
1105  * different handlers.
1106  * We rely on user-space to write the initial superblock, and support
1107  * reading and updating of superblocks.
1108  * Interface methods are:
1109  *   int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
1110  *      loads and validates a superblock on dev.
1111  *      if refdev != NULL, compare superblocks on both devices
1112  *    Return:
1113  *      0 - dev has a superblock that is compatible with refdev
1114  *      1 - dev has a superblock that is compatible and newer than refdev
1115  *          so dev should be used as the refdev in future
1116  *     -EINVAL superblock incompatible or invalid
1117  *     -othererror e.g. -EIO
1118  *
1119  *   int validate_super(struct mddev *mddev, struct md_rdev *dev)
1120  *      Verify that dev is acceptable into mddev.
1121  *       The first time, mddev->raid_disks will be 0, and data from
1122  *       dev should be merged in.  Subsequent calls check that dev
1123  *       is new enough.  Return 0 or -EINVAL
1124  *
1125  *   void sync_super(struct mddev *mddev, struct md_rdev *dev)
1126  *     Update the superblock for rdev with data in mddev
1127  *     This does not write to disc.
1128  *
1129  */
1130 
1131 struct super_type  {
1132 	char		    *name;
1133 	struct module	    *owner;
1134 	int		    (*load_super)(struct md_rdev *rdev,
1135 					  struct md_rdev *refdev,
1136 					  int minor_version);
1137 	int		    (*validate_super)(struct mddev *mddev,
1138 					      struct md_rdev *freshest,
1139 					      struct md_rdev *rdev);
1140 	void		    (*sync_super)(struct mddev *mddev,
1141 					  struct md_rdev *rdev);
1142 	unsigned long long  (*rdev_size_change)(struct md_rdev *rdev,
1143 						sector_t num_sectors);
1144 	int		    (*allow_new_offset)(struct md_rdev *rdev,
1145 						unsigned long long new_offset);
1146 };
1147 
1148 /*
1149  * Check that the given mddev has no bitmap.
1150  *
1151  * This function is called from the run method of all personalities that do not
1152  * support bitmaps. It prints an error message and returns non-zero if mddev
1153  * has a bitmap. Otherwise, it returns 0.
1154  *
1155  */
1156 int md_check_no_bitmap(struct mddev *mddev)
1157 {
1158 	if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1159 		return 0;
1160 	pr_warn("%s: bitmaps are not supported for %s\n",
1161 		mdname(mddev), mddev->pers->name);
1162 	return 1;
1163 }
1164 EXPORT_SYMBOL(md_check_no_bitmap);
1165 
1166 /*
1167  * load_super for 0.90.0
1168  */
1169 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1170 {
1171 	mdp_super_t *sb;
1172 	int ret;
1173 	bool spare_disk = true;
1174 
1175 	/*
1176 	 * Calculate the position of the superblock (512byte sectors),
1177 	 * it's at the end of the disk.
1178 	 *
1179 	 * It also happens to be a multiple of 4Kb.
1180 	 */
1181 	rdev->sb_start = calc_dev_sboffset(rdev);
1182 
1183 	ret = read_disk_sb(rdev, MD_SB_BYTES);
1184 	if (ret)
1185 		return ret;
1186 
1187 	ret = -EINVAL;
1188 
1189 	sb = page_address(rdev->sb_page);
1190 
1191 	if (sb->md_magic != MD_SB_MAGIC) {
1192 		pr_warn("md: invalid raid superblock magic on %pg\n",
1193 			rdev->bdev);
1194 		goto abort;
1195 	}
1196 
1197 	if (sb->major_version != 0 ||
1198 	    sb->minor_version < 90 ||
1199 	    sb->minor_version > 91) {
1200 		pr_warn("Bad version number %d.%d on %pg\n",
1201 			sb->major_version, sb->minor_version, rdev->bdev);
1202 		goto abort;
1203 	}
1204 
1205 	if (sb->raid_disks <= 0)
1206 		goto abort;
1207 
1208 	if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1209 		pr_warn("md: invalid superblock checksum on %pg\n", rdev->bdev);
1210 		goto abort;
1211 	}
1212 
1213 	rdev->preferred_minor = sb->md_minor;
1214 	rdev->data_offset = 0;
1215 	rdev->new_data_offset = 0;
1216 	rdev->sb_size = MD_SB_BYTES;
1217 	rdev->badblocks.shift = -1;
1218 
1219 	rdev->desc_nr = sb->this_disk.number;
1220 
1221 	/* not spare disk */
1222 	if (rdev->desc_nr >= 0 && rdev->desc_nr < MD_SB_DISKS &&
1223 	    sb->disks[rdev->desc_nr].state & ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1224 		spare_disk = false;
1225 
1226 	if (!refdev) {
1227 		if (!spare_disk)
1228 			ret = 1;
1229 		else
1230 			ret = 0;
1231 	} else {
1232 		__u64 ev1, ev2;
1233 		mdp_super_t *refsb = page_address(refdev->sb_page);
1234 		if (!md_uuid_equal(refsb, sb)) {
1235 			pr_warn("md: %pg has different UUID to %pg\n",
1236 				rdev->bdev, refdev->bdev);
1237 			goto abort;
1238 		}
1239 		if (!md_sb_equal(refsb, sb)) {
1240 			pr_warn("md: %pg has same UUID but different superblock to %pg\n",
1241 				rdev->bdev, refdev->bdev);
1242 			goto abort;
1243 		}
1244 		ev1 = md_event(sb);
1245 		ev2 = md_event(refsb);
1246 
1247 		if (!spare_disk && ev1 > ev2)
1248 			ret = 1;
1249 		else
1250 			ret = 0;
1251 	}
1252 	rdev->sectors = rdev->sb_start;
1253 	/* Limit to 4TB as metadata cannot record more than that.
1254 	 * (not needed for Linear and RAID0 as metadata doesn't
1255 	 * record this size)
1256 	 */
1257 	if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1258 		rdev->sectors = (sector_t)(2ULL << 32) - 2;
1259 
1260 	if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1261 		/* "this cannot possibly happen" ... */
1262 		ret = -EINVAL;
1263 
1264  abort:
1265 	return ret;
1266 }
1267 
1268 static u64 md_bitmap_events_cleared(struct mddev *mddev)
1269 {
1270 	struct md_bitmap_stats stats;
1271 	int err;
1272 
1273 	err = mddev->bitmap_ops->get_stats(mddev->bitmap, &stats);
1274 	if (err)
1275 		return 0;
1276 
1277 	return stats.events_cleared;
1278 }
1279 
1280 /*
1281  * validate_super for 0.90.0
1282  * note: we are not using "freshest" for 0.9 superblock
1283  */
1284 static int super_90_validate(struct mddev *mddev, struct md_rdev *freshest, struct md_rdev *rdev)
1285 {
1286 	mdp_disk_t *desc;
1287 	mdp_super_t *sb = page_address(rdev->sb_page);
1288 	__u64 ev1 = md_event(sb);
1289 
1290 	rdev->raid_disk = -1;
1291 	clear_bit(Faulty, &rdev->flags);
1292 	clear_bit(In_sync, &rdev->flags);
1293 	clear_bit(Bitmap_sync, &rdev->flags);
1294 	clear_bit(WriteMostly, &rdev->flags);
1295 
1296 	if (mddev->raid_disks == 0) {
1297 		mddev->major_version = 0;
1298 		mddev->minor_version = sb->minor_version;
1299 		mddev->patch_version = sb->patch_version;
1300 		mddev->external = 0;
1301 		mddev->chunk_sectors = sb->chunk_size >> 9;
1302 		mddev->ctime = sb->ctime;
1303 		mddev->utime = sb->utime;
1304 		mddev->level = sb->level;
1305 		mddev->clevel[0] = 0;
1306 		mddev->layout = sb->layout;
1307 		mddev->raid_disks = sb->raid_disks;
1308 		mddev->dev_sectors = ((sector_t)sb->size) * 2;
1309 		mddev->events = ev1;
1310 		mddev->bitmap_info.offset = 0;
1311 		mddev->bitmap_info.space = 0;
1312 		/* bitmap can use 60 K after the 4K superblocks */
1313 		mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1314 		mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1315 		mddev->reshape_backwards = 0;
1316 
1317 		if (mddev->minor_version >= 91) {
1318 			mddev->reshape_position = sb->reshape_position;
1319 			mddev->delta_disks = sb->delta_disks;
1320 			mddev->new_level = sb->new_level;
1321 			mddev->new_layout = sb->new_layout;
1322 			mddev->new_chunk_sectors = sb->new_chunk >> 9;
1323 			if (mddev->delta_disks < 0)
1324 				mddev->reshape_backwards = 1;
1325 		} else {
1326 			mddev->reshape_position = MaxSector;
1327 			mddev->delta_disks = 0;
1328 			mddev->new_level = mddev->level;
1329 			mddev->new_layout = mddev->layout;
1330 			mddev->new_chunk_sectors = mddev->chunk_sectors;
1331 		}
1332 		if (mddev->level == 0)
1333 			mddev->layout = -1;
1334 
1335 		if (sb->state & (1<<MD_SB_CLEAN))
1336 			mddev->recovery_cp = MaxSector;
1337 		else {
1338 			if (sb->events_hi == sb->cp_events_hi &&
1339 				sb->events_lo == sb->cp_events_lo) {
1340 				mddev->recovery_cp = sb->recovery_cp;
1341 			} else
1342 				mddev->recovery_cp = 0;
1343 		}
1344 
1345 		memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1346 		memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1347 		memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1348 		memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1349 
1350 		mddev->max_disks = MD_SB_DISKS;
1351 
1352 		if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1353 		    mddev->bitmap_info.file == NULL) {
1354 			mddev->bitmap_info.offset =
1355 				mddev->bitmap_info.default_offset;
1356 			mddev->bitmap_info.space =
1357 				mddev->bitmap_info.default_space;
1358 		}
1359 
1360 	} else if (mddev->pers == NULL) {
1361 		/* Insist on good event counter while assembling, except
1362 		 * for spares (which don't need an event count) */
1363 		++ev1;
1364 		if (sb->disks[rdev->desc_nr].state & (
1365 			    (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1366 			if (ev1 < mddev->events)
1367 				return -EINVAL;
1368 	} else if (mddev->bitmap) {
1369 		/* if adding to array with a bitmap, then we can accept an
1370 		 * older device ... but not too old.
1371 		 */
1372 		if (ev1 < md_bitmap_events_cleared(mddev))
1373 			return 0;
1374 		if (ev1 < mddev->events)
1375 			set_bit(Bitmap_sync, &rdev->flags);
1376 	} else {
1377 		if (ev1 < mddev->events)
1378 			/* just a hot-add of a new device, leave raid_disk at -1 */
1379 			return 0;
1380 	}
1381 
1382 	desc = sb->disks + rdev->desc_nr;
1383 
1384 	if (desc->state & (1<<MD_DISK_FAULTY))
1385 		set_bit(Faulty, &rdev->flags);
1386 	else if (desc->state & (1<<MD_DISK_SYNC)) {
1387 		set_bit(In_sync, &rdev->flags);
1388 		rdev->raid_disk = desc->raid_disk;
1389 		rdev->saved_raid_disk = desc->raid_disk;
1390 	} else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1391 		/* active but not in sync implies recovery up to
1392 		 * reshape position.  We don't know exactly where
1393 		 * that is, so set to zero for now
1394 		 */
1395 		if (mddev->minor_version >= 91) {
1396 			rdev->recovery_offset = 0;
1397 			rdev->raid_disk = desc->raid_disk;
1398 		}
1399 	}
1400 	if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1401 		set_bit(WriteMostly, &rdev->flags);
1402 	if (desc->state & (1<<MD_DISK_FAILFAST))
1403 		set_bit(FailFast, &rdev->flags);
1404 	return 0;
1405 }
1406 
1407 /*
1408  * sync_super for 0.90.0
1409  */
1410 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1411 {
1412 	mdp_super_t *sb;
1413 	struct md_rdev *rdev2;
1414 	int next_spare = mddev->raid_disks;
1415 
1416 	/* make rdev->sb match mddev data..
1417 	 *
1418 	 * 1/ zero out disks
1419 	 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1420 	 * 3/ any empty disks < next_spare become removed
1421 	 *
1422 	 * disks[0] gets initialised to REMOVED because
1423 	 * we cannot be sure from other fields if it has
1424 	 * been initialised or not.
1425 	 */
1426 	int i;
1427 	int active=0, working=0,failed=0,spare=0,nr_disks=0;
1428 
1429 	rdev->sb_size = MD_SB_BYTES;
1430 
1431 	sb = page_address(rdev->sb_page);
1432 
1433 	memset(sb, 0, sizeof(*sb));
1434 
1435 	sb->md_magic = MD_SB_MAGIC;
1436 	sb->major_version = mddev->major_version;
1437 	sb->patch_version = mddev->patch_version;
1438 	sb->gvalid_words  = 0; /* ignored */
1439 	memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1440 	memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1441 	memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1442 	memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1443 
1444 	sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1445 	sb->level = mddev->level;
1446 	sb->size = mddev->dev_sectors / 2;
1447 	sb->raid_disks = mddev->raid_disks;
1448 	sb->md_minor = mddev->md_minor;
1449 	sb->not_persistent = 0;
1450 	sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1451 	sb->state = 0;
1452 	sb->events_hi = (mddev->events>>32);
1453 	sb->events_lo = (u32)mddev->events;
1454 
1455 	if (mddev->reshape_position == MaxSector)
1456 		sb->minor_version = 90;
1457 	else {
1458 		sb->minor_version = 91;
1459 		sb->reshape_position = mddev->reshape_position;
1460 		sb->new_level = mddev->new_level;
1461 		sb->delta_disks = mddev->delta_disks;
1462 		sb->new_layout = mddev->new_layout;
1463 		sb->new_chunk = mddev->new_chunk_sectors << 9;
1464 	}
1465 	mddev->minor_version = sb->minor_version;
1466 	if (mddev->in_sync)
1467 	{
1468 		sb->recovery_cp = mddev->recovery_cp;
1469 		sb->cp_events_hi = (mddev->events>>32);
1470 		sb->cp_events_lo = (u32)mddev->events;
1471 		if (mddev->recovery_cp == MaxSector)
1472 			sb->state = (1<< MD_SB_CLEAN);
1473 	} else
1474 		sb->recovery_cp = 0;
1475 
1476 	sb->layout = mddev->layout;
1477 	sb->chunk_size = mddev->chunk_sectors << 9;
1478 
1479 	if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1480 		sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1481 
1482 	sb->disks[0].state = (1<<MD_DISK_REMOVED);
1483 	rdev_for_each(rdev2, mddev) {
1484 		mdp_disk_t *d;
1485 		int desc_nr;
1486 		int is_active = test_bit(In_sync, &rdev2->flags);
1487 
1488 		if (rdev2->raid_disk >= 0 &&
1489 		    sb->minor_version >= 91)
1490 			/* we have nowhere to store the recovery_offset,
1491 			 * but if it is not below the reshape_position,
1492 			 * we can piggy-back on that.
1493 			 */
1494 			is_active = 1;
1495 		if (rdev2->raid_disk < 0 ||
1496 		    test_bit(Faulty, &rdev2->flags))
1497 			is_active = 0;
1498 		if (is_active)
1499 			desc_nr = rdev2->raid_disk;
1500 		else
1501 			desc_nr = next_spare++;
1502 		rdev2->desc_nr = desc_nr;
1503 		d = &sb->disks[rdev2->desc_nr];
1504 		nr_disks++;
1505 		d->number = rdev2->desc_nr;
1506 		d->major = MAJOR(rdev2->bdev->bd_dev);
1507 		d->minor = MINOR(rdev2->bdev->bd_dev);
1508 		if (is_active)
1509 			d->raid_disk = rdev2->raid_disk;
1510 		else
1511 			d->raid_disk = rdev2->desc_nr; /* compatibility */
1512 		if (test_bit(Faulty, &rdev2->flags))
1513 			d->state = (1<<MD_DISK_FAULTY);
1514 		else if (is_active) {
1515 			d->state = (1<<MD_DISK_ACTIVE);
1516 			if (test_bit(In_sync, &rdev2->flags))
1517 				d->state |= (1<<MD_DISK_SYNC);
1518 			active++;
1519 			working++;
1520 		} else {
1521 			d->state = 0;
1522 			spare++;
1523 			working++;
1524 		}
1525 		if (test_bit(WriteMostly, &rdev2->flags))
1526 			d->state |= (1<<MD_DISK_WRITEMOSTLY);
1527 		if (test_bit(FailFast, &rdev2->flags))
1528 			d->state |= (1<<MD_DISK_FAILFAST);
1529 	}
1530 	/* now set the "removed" and "faulty" bits on any missing devices */
1531 	for (i=0 ; i < mddev->raid_disks ; i++) {
1532 		mdp_disk_t *d = &sb->disks[i];
1533 		if (d->state == 0 && d->number == 0) {
1534 			d->number = i;
1535 			d->raid_disk = i;
1536 			d->state = (1<<MD_DISK_REMOVED);
1537 			d->state |= (1<<MD_DISK_FAULTY);
1538 			failed++;
1539 		}
1540 	}
1541 	sb->nr_disks = nr_disks;
1542 	sb->active_disks = active;
1543 	sb->working_disks = working;
1544 	sb->failed_disks = failed;
1545 	sb->spare_disks = spare;
1546 
1547 	sb->this_disk = sb->disks[rdev->desc_nr];
1548 	sb->sb_csum = calc_sb_csum(sb);
1549 }
1550 
1551 /*
1552  * rdev_size_change for 0.90.0
1553  */
1554 static unsigned long long
1555 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1556 {
1557 	if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1558 		return 0; /* component must fit device */
1559 	if (rdev->mddev->bitmap_info.offset)
1560 		return 0; /* can't move bitmap */
1561 	rdev->sb_start = calc_dev_sboffset(rdev);
1562 	if (!num_sectors || num_sectors > rdev->sb_start)
1563 		num_sectors = rdev->sb_start;
1564 	/* Limit to 4TB as metadata cannot record more than that.
1565 	 * 4TB == 2^32 KB, or 2*2^32 sectors.
1566 	 */
1567 	if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1568 		num_sectors = (sector_t)(2ULL << 32) - 2;
1569 	do {
1570 		md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1571 		       rdev->sb_page);
1572 	} while (md_super_wait(rdev->mddev) < 0);
1573 	return num_sectors;
1574 }
1575 
1576 static int
1577 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1578 {
1579 	/* non-zero offset changes not possible with v0.90 */
1580 	return new_offset == 0;
1581 }
1582 
1583 /*
1584  * version 1 superblock
1585  */
1586 
1587 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1588 {
1589 	__le32 disk_csum;
1590 	u32 csum;
1591 	unsigned long long newcsum;
1592 	int size = 256 + le32_to_cpu(sb->max_dev)*2;
1593 	__le32 *isuper = (__le32*)sb;
1594 
1595 	disk_csum = sb->sb_csum;
1596 	sb->sb_csum = 0;
1597 	newcsum = 0;
1598 	for (; size >= 4; size -= 4)
1599 		newcsum += le32_to_cpu(*isuper++);
1600 
1601 	if (size == 2)
1602 		newcsum += le16_to_cpu(*(__le16*) isuper);
1603 
1604 	csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1605 	sb->sb_csum = disk_csum;
1606 	return cpu_to_le32(csum);
1607 }
1608 
1609 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1610 {
1611 	struct mdp_superblock_1 *sb;
1612 	int ret;
1613 	sector_t sb_start;
1614 	sector_t sectors;
1615 	int bmask;
1616 	bool spare_disk = true;
1617 
1618 	/*
1619 	 * Calculate the position of the superblock in 512byte sectors.
1620 	 * It is always aligned to a 4K boundary and
1621 	 * depeding on minor_version, it can be:
1622 	 * 0: At least 8K, but less than 12K, from end of device
1623 	 * 1: At start of device
1624 	 * 2: 4K from start of device.
1625 	 */
1626 	switch(minor_version) {
1627 	case 0:
1628 		sb_start = bdev_nr_sectors(rdev->bdev) - 8 * 2;
1629 		sb_start &= ~(sector_t)(4*2-1);
1630 		break;
1631 	case 1:
1632 		sb_start = 0;
1633 		break;
1634 	case 2:
1635 		sb_start = 8;
1636 		break;
1637 	default:
1638 		return -EINVAL;
1639 	}
1640 	rdev->sb_start = sb_start;
1641 
1642 	/* superblock is rarely larger than 1K, but it can be larger,
1643 	 * and it is safe to read 4k, so we do that
1644 	 */
1645 	ret = read_disk_sb(rdev, 4096);
1646 	if (ret) return ret;
1647 
1648 	sb = page_address(rdev->sb_page);
1649 
1650 	if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1651 	    sb->major_version != cpu_to_le32(1) ||
1652 	    le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1653 	    le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1654 	    (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1655 		return -EINVAL;
1656 
1657 	if (calc_sb_1_csum(sb) != sb->sb_csum) {
1658 		pr_warn("md: invalid superblock checksum on %pg\n",
1659 			rdev->bdev);
1660 		return -EINVAL;
1661 	}
1662 	if (le64_to_cpu(sb->data_size) < 10) {
1663 		pr_warn("md: data_size too small on %pg\n",
1664 			rdev->bdev);
1665 		return -EINVAL;
1666 	}
1667 	if (sb->pad0 ||
1668 	    sb->pad3[0] ||
1669 	    memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1670 		/* Some padding is non-zero, might be a new feature */
1671 		return -EINVAL;
1672 
1673 	rdev->preferred_minor = 0xffff;
1674 	rdev->data_offset = le64_to_cpu(sb->data_offset);
1675 	rdev->new_data_offset = rdev->data_offset;
1676 	if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1677 	    (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1678 		rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1679 	atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1680 
1681 	rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1682 	bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1683 	if (rdev->sb_size & bmask)
1684 		rdev->sb_size = (rdev->sb_size | bmask) + 1;
1685 
1686 	if (minor_version
1687 	    && rdev->data_offset < sb_start + (rdev->sb_size/512))
1688 		return -EINVAL;
1689 	if (minor_version
1690 	    && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1691 		return -EINVAL;
1692 
1693 	rdev->desc_nr = le32_to_cpu(sb->dev_number);
1694 
1695 	if (!rdev->bb_page) {
1696 		rdev->bb_page = alloc_page(GFP_KERNEL);
1697 		if (!rdev->bb_page)
1698 			return -ENOMEM;
1699 	}
1700 	if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1701 	    rdev->badblocks.count == 0) {
1702 		/* need to load the bad block list.
1703 		 * Currently we limit it to one page.
1704 		 */
1705 		s32 offset;
1706 		sector_t bb_sector;
1707 		__le64 *bbp;
1708 		int i;
1709 		int sectors = le16_to_cpu(sb->bblog_size);
1710 		if (sectors > (PAGE_SIZE / 512))
1711 			return -EINVAL;
1712 		offset = le32_to_cpu(sb->bblog_offset);
1713 		if (offset == 0)
1714 			return -EINVAL;
1715 		bb_sector = (long long)offset;
1716 		if (!sync_page_io(rdev, bb_sector, sectors << 9,
1717 				  rdev->bb_page, REQ_OP_READ, true))
1718 			return -EIO;
1719 		bbp = (__le64 *)page_address(rdev->bb_page);
1720 		rdev->badblocks.shift = sb->bblog_shift;
1721 		for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1722 			u64 bb = le64_to_cpu(*bbp);
1723 			int count = bb & (0x3ff);
1724 			u64 sector = bb >> 10;
1725 			sector <<= sb->bblog_shift;
1726 			count <<= sb->bblog_shift;
1727 			if (bb + 1 == 0)
1728 				break;
1729 			if (badblocks_set(&rdev->badblocks, sector, count, 1))
1730 				return -EINVAL;
1731 		}
1732 	} else if (sb->bblog_offset != 0)
1733 		rdev->badblocks.shift = 0;
1734 
1735 	if ((le32_to_cpu(sb->feature_map) &
1736 	    (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
1737 		rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1738 		rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1739 		rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1740 	}
1741 
1742 	if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1743 	    sb->level != 0)
1744 		return -EINVAL;
1745 
1746 	/* not spare disk */
1747 	if (rdev->desc_nr >= 0 && rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1748 	    (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1749 	     le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1750 		spare_disk = false;
1751 
1752 	if (!refdev) {
1753 		if (!spare_disk)
1754 			ret = 1;
1755 		else
1756 			ret = 0;
1757 	} else {
1758 		__u64 ev1, ev2;
1759 		struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1760 
1761 		if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1762 		    sb->level != refsb->level ||
1763 		    sb->layout != refsb->layout ||
1764 		    sb->chunksize != refsb->chunksize) {
1765 			pr_warn("md: %pg has strangely different superblock to %pg\n",
1766 				rdev->bdev,
1767 				refdev->bdev);
1768 			return -EINVAL;
1769 		}
1770 		ev1 = le64_to_cpu(sb->events);
1771 		ev2 = le64_to_cpu(refsb->events);
1772 
1773 		if (!spare_disk && ev1 > ev2)
1774 			ret = 1;
1775 		else
1776 			ret = 0;
1777 	}
1778 	if (minor_version)
1779 		sectors = bdev_nr_sectors(rdev->bdev) - rdev->data_offset;
1780 	else
1781 		sectors = rdev->sb_start;
1782 	if (sectors < le64_to_cpu(sb->data_size))
1783 		return -EINVAL;
1784 	rdev->sectors = le64_to_cpu(sb->data_size);
1785 	return ret;
1786 }
1787 
1788 static int super_1_validate(struct mddev *mddev, struct md_rdev *freshest, struct md_rdev *rdev)
1789 {
1790 	struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1791 	__u64 ev1 = le64_to_cpu(sb->events);
1792 	int role;
1793 
1794 	rdev->raid_disk = -1;
1795 	clear_bit(Faulty, &rdev->flags);
1796 	clear_bit(In_sync, &rdev->flags);
1797 	clear_bit(Bitmap_sync, &rdev->flags);
1798 	clear_bit(WriteMostly, &rdev->flags);
1799 
1800 	if (mddev->raid_disks == 0) {
1801 		mddev->major_version = 1;
1802 		mddev->patch_version = 0;
1803 		mddev->external = 0;
1804 		mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1805 		mddev->ctime = le64_to_cpu(sb->ctime);
1806 		mddev->utime = le64_to_cpu(sb->utime);
1807 		mddev->level = le32_to_cpu(sb->level);
1808 		mddev->clevel[0] = 0;
1809 		mddev->layout = le32_to_cpu(sb->layout);
1810 		mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1811 		mddev->dev_sectors = le64_to_cpu(sb->size);
1812 		mddev->events = ev1;
1813 		mddev->bitmap_info.offset = 0;
1814 		mddev->bitmap_info.space = 0;
1815 		/* Default location for bitmap is 1K after superblock
1816 		 * using 3K - total of 4K
1817 		 */
1818 		mddev->bitmap_info.default_offset = 1024 >> 9;
1819 		mddev->bitmap_info.default_space = (4096-1024) >> 9;
1820 		mddev->reshape_backwards = 0;
1821 
1822 		mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1823 		memcpy(mddev->uuid, sb->set_uuid, 16);
1824 
1825 		mddev->max_disks =  (4096-256)/2;
1826 
1827 		if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1828 		    mddev->bitmap_info.file == NULL) {
1829 			mddev->bitmap_info.offset =
1830 				(__s32)le32_to_cpu(sb->bitmap_offset);
1831 			/* Metadata doesn't record how much space is available.
1832 			 * For 1.0, we assume we can use up to the superblock
1833 			 * if before, else to 4K beyond superblock.
1834 			 * For others, assume no change is possible.
1835 			 */
1836 			if (mddev->minor_version > 0)
1837 				mddev->bitmap_info.space = 0;
1838 			else if (mddev->bitmap_info.offset > 0)
1839 				mddev->bitmap_info.space =
1840 					8 - mddev->bitmap_info.offset;
1841 			else
1842 				mddev->bitmap_info.space =
1843 					-mddev->bitmap_info.offset;
1844 		}
1845 
1846 		if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1847 			mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1848 			mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1849 			mddev->new_level = le32_to_cpu(sb->new_level);
1850 			mddev->new_layout = le32_to_cpu(sb->new_layout);
1851 			mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1852 			if (mddev->delta_disks < 0 ||
1853 			    (mddev->delta_disks == 0 &&
1854 			     (le32_to_cpu(sb->feature_map)
1855 			      & MD_FEATURE_RESHAPE_BACKWARDS)))
1856 				mddev->reshape_backwards = 1;
1857 		} else {
1858 			mddev->reshape_position = MaxSector;
1859 			mddev->delta_disks = 0;
1860 			mddev->new_level = mddev->level;
1861 			mddev->new_layout = mddev->layout;
1862 			mddev->new_chunk_sectors = mddev->chunk_sectors;
1863 		}
1864 
1865 		if (mddev->level == 0 &&
1866 		    !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1867 			mddev->layout = -1;
1868 
1869 		if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1870 			set_bit(MD_HAS_JOURNAL, &mddev->flags);
1871 
1872 		if (le32_to_cpu(sb->feature_map) &
1873 		    (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
1874 			if (le32_to_cpu(sb->feature_map) &
1875 			    (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1876 				return -EINVAL;
1877 			if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1878 			    (le32_to_cpu(sb->feature_map) &
1879 					    MD_FEATURE_MULTIPLE_PPLS))
1880 				return -EINVAL;
1881 			set_bit(MD_HAS_PPL, &mddev->flags);
1882 		}
1883 	} else if (mddev->pers == NULL) {
1884 		/* Insist of good event counter while assembling, except for
1885 		 * spares (which don't need an event count).
1886 		 * Similar to mdadm, we allow event counter difference of 1
1887 		 * from the freshest device.
1888 		 */
1889 		if (rdev->desc_nr >= 0 &&
1890 		    rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1891 		    (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1892 		     le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1893 			if (ev1 + 1 < mddev->events)
1894 				return -EINVAL;
1895 	} else if (mddev->bitmap) {
1896 		/* If adding to array with a bitmap, then we can accept an
1897 		 * older device, but not too old.
1898 		 */
1899 		if (ev1 < md_bitmap_events_cleared(mddev))
1900 			return 0;
1901 		if (ev1 < mddev->events)
1902 			set_bit(Bitmap_sync, &rdev->flags);
1903 	} else {
1904 		if (ev1 < mddev->events)
1905 			/* just a hot-add of a new device, leave raid_disk at -1 */
1906 			return 0;
1907 	}
1908 
1909 	if (rdev->desc_nr < 0 ||
1910 	    rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1911 		role = MD_DISK_ROLE_SPARE;
1912 		rdev->desc_nr = -1;
1913 	} else if (mddev->pers == NULL && freshest && ev1 < mddev->events) {
1914 		/*
1915 		 * If we are assembling, and our event counter is smaller than the
1916 		 * highest event counter, we cannot trust our superblock about the role.
1917 		 * It could happen that our rdev was marked as Faulty, and all other
1918 		 * superblocks were updated with +1 event counter.
1919 		 * Then, before the next superblock update, which typically happens when
1920 		 * remove_and_add_spares() removes the device from the array, there was
1921 		 * a crash or reboot.
1922 		 * If we allow current rdev without consulting the freshest superblock,
1923 		 * we could cause data corruption.
1924 		 * Note that in this case our event counter is smaller by 1 than the
1925 		 * highest, otherwise, this rdev would not be allowed into array;
1926 		 * both kernel and mdadm allow event counter difference of 1.
1927 		 */
1928 		struct mdp_superblock_1 *freshest_sb = page_address(freshest->sb_page);
1929 		u32 freshest_max_dev = le32_to_cpu(freshest_sb->max_dev);
1930 
1931 		if (rdev->desc_nr >= freshest_max_dev) {
1932 			/* this is unexpected, better not proceed */
1933 			pr_warn("md: %s: rdev[%pg]: desc_nr(%d) >= freshest(%pg)->sb->max_dev(%u)\n",
1934 				mdname(mddev), rdev->bdev, rdev->desc_nr,
1935 				freshest->bdev, freshest_max_dev);
1936 			return -EUCLEAN;
1937 		}
1938 
1939 		role = le16_to_cpu(freshest_sb->dev_roles[rdev->desc_nr]);
1940 		pr_debug("md: %s: rdev[%pg]: role=%d(0x%x) according to freshest %pg\n",
1941 			 mdname(mddev), rdev->bdev, role, role, freshest->bdev);
1942 	} else {
1943 		role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1944 	}
1945 	switch (role) {
1946 	case MD_DISK_ROLE_SPARE: /* spare */
1947 		break;
1948 	case MD_DISK_ROLE_FAULTY: /* faulty */
1949 		set_bit(Faulty, &rdev->flags);
1950 		break;
1951 	case MD_DISK_ROLE_JOURNAL: /* journal device */
1952 		if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1953 			/* journal device without journal feature */
1954 			pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1955 			return -EINVAL;
1956 		}
1957 		set_bit(Journal, &rdev->flags);
1958 		rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1959 		rdev->raid_disk = 0;
1960 		break;
1961 	default:
1962 		rdev->saved_raid_disk = role;
1963 		if ((le32_to_cpu(sb->feature_map) &
1964 		     MD_FEATURE_RECOVERY_OFFSET)) {
1965 			rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1966 			if (!(le32_to_cpu(sb->feature_map) &
1967 			      MD_FEATURE_RECOVERY_BITMAP))
1968 				rdev->saved_raid_disk = -1;
1969 		} else {
1970 			/*
1971 			 * If the array is FROZEN, then the device can't
1972 			 * be in_sync with rest of array.
1973 			 */
1974 			if (!test_bit(MD_RECOVERY_FROZEN,
1975 				      &mddev->recovery))
1976 				set_bit(In_sync, &rdev->flags);
1977 		}
1978 		rdev->raid_disk = role;
1979 		break;
1980 	}
1981 	if (sb->devflags & WriteMostly1)
1982 		set_bit(WriteMostly, &rdev->flags);
1983 	if (sb->devflags & FailFast1)
1984 		set_bit(FailFast, &rdev->flags);
1985 	if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1986 		set_bit(Replacement, &rdev->flags);
1987 
1988 	return 0;
1989 }
1990 
1991 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1992 {
1993 	struct mdp_superblock_1 *sb;
1994 	struct md_rdev *rdev2;
1995 	int max_dev, i;
1996 	/* make rdev->sb match mddev and rdev data. */
1997 
1998 	sb = page_address(rdev->sb_page);
1999 
2000 	sb->feature_map = 0;
2001 	sb->pad0 = 0;
2002 	sb->recovery_offset = cpu_to_le64(0);
2003 	memset(sb->pad3, 0, sizeof(sb->pad3));
2004 
2005 	sb->utime = cpu_to_le64((__u64)mddev->utime);
2006 	sb->events = cpu_to_le64(mddev->events);
2007 	if (mddev->in_sync)
2008 		sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
2009 	else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
2010 		sb->resync_offset = cpu_to_le64(MaxSector);
2011 	else
2012 		sb->resync_offset = cpu_to_le64(0);
2013 
2014 	sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
2015 
2016 	sb->raid_disks = cpu_to_le32(mddev->raid_disks);
2017 	sb->size = cpu_to_le64(mddev->dev_sectors);
2018 	sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
2019 	sb->level = cpu_to_le32(mddev->level);
2020 	sb->layout = cpu_to_le32(mddev->layout);
2021 	if (test_bit(FailFast, &rdev->flags))
2022 		sb->devflags |= FailFast1;
2023 	else
2024 		sb->devflags &= ~FailFast1;
2025 
2026 	if (test_bit(WriteMostly, &rdev->flags))
2027 		sb->devflags |= WriteMostly1;
2028 	else
2029 		sb->devflags &= ~WriteMostly1;
2030 	sb->data_offset = cpu_to_le64(rdev->data_offset);
2031 	sb->data_size = cpu_to_le64(rdev->sectors);
2032 
2033 	if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2034 		sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
2035 		sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
2036 	}
2037 
2038 	if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
2039 	    !test_bit(In_sync, &rdev->flags)) {
2040 		sb->feature_map |=
2041 			cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2042 		sb->recovery_offset =
2043 			cpu_to_le64(rdev->recovery_offset);
2044 		if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2045 			sb->feature_map |=
2046 				cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
2047 	}
2048 	/* Note: recovery_offset and journal_tail share space  */
2049 	if (test_bit(Journal, &rdev->flags))
2050 		sb->journal_tail = cpu_to_le64(rdev->journal_tail);
2051 	if (test_bit(Replacement, &rdev->flags))
2052 		sb->feature_map |=
2053 			cpu_to_le32(MD_FEATURE_REPLACEMENT);
2054 
2055 	if (mddev->reshape_position != MaxSector) {
2056 		sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2057 		sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2058 		sb->new_layout = cpu_to_le32(mddev->new_layout);
2059 		sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2060 		sb->new_level = cpu_to_le32(mddev->new_level);
2061 		sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
2062 		if (mddev->delta_disks == 0 &&
2063 		    mddev->reshape_backwards)
2064 			sb->feature_map
2065 				|= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
2066 		if (rdev->new_data_offset != rdev->data_offset) {
2067 			sb->feature_map
2068 				|= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2069 			sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2070 							     - rdev->data_offset));
2071 		}
2072 	}
2073 
2074 	if (mddev_is_clustered(mddev))
2075 		sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2076 
2077 	if (rdev->badblocks.count == 0)
2078 		/* Nothing to do for bad blocks*/ ;
2079 	else if (sb->bblog_offset == 0)
2080 		/* Cannot record bad blocks on this device */
2081 		md_error(mddev, rdev);
2082 	else {
2083 		struct badblocks *bb = &rdev->badblocks;
2084 		__le64 *bbp = (__le64 *)page_address(rdev->bb_page);
2085 		u64 *p = bb->page;
2086 		sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2087 		if (bb->changed) {
2088 			unsigned seq;
2089 
2090 retry:
2091 			seq = read_seqbegin(&bb->lock);
2092 
2093 			memset(bbp, 0xff, PAGE_SIZE);
2094 
2095 			for (i = 0 ; i < bb->count ; i++) {
2096 				u64 internal_bb = p[i];
2097 				u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2098 						| BB_LEN(internal_bb));
2099 				bbp[i] = cpu_to_le64(store_bb);
2100 			}
2101 			bb->changed = 0;
2102 			if (read_seqretry(&bb->lock, seq))
2103 				goto retry;
2104 
2105 			bb->sector = (rdev->sb_start +
2106 				      (int)le32_to_cpu(sb->bblog_offset));
2107 			bb->size = le16_to_cpu(sb->bblog_size);
2108 		}
2109 	}
2110 
2111 	max_dev = 0;
2112 	rdev_for_each(rdev2, mddev)
2113 		if (rdev2->desc_nr+1 > max_dev)
2114 			max_dev = rdev2->desc_nr+1;
2115 
2116 	if (max_dev > le32_to_cpu(sb->max_dev)) {
2117 		int bmask;
2118 		sb->max_dev = cpu_to_le32(max_dev);
2119 		rdev->sb_size = max_dev * 2 + 256;
2120 		bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2121 		if (rdev->sb_size & bmask)
2122 			rdev->sb_size = (rdev->sb_size | bmask) + 1;
2123 	} else
2124 		max_dev = le32_to_cpu(sb->max_dev);
2125 
2126 	for (i=0; i<max_dev;i++)
2127 		sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
2128 
2129 	if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2130 		sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
2131 
2132 	if (test_bit(MD_HAS_PPL, &mddev->flags)) {
2133 		if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2134 			sb->feature_map |=
2135 			    cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2136 		else
2137 			sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
2138 		sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2139 		sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2140 	}
2141 
2142 	rdev_for_each(rdev2, mddev) {
2143 		i = rdev2->desc_nr;
2144 		if (test_bit(Faulty, &rdev2->flags))
2145 			sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
2146 		else if (test_bit(In_sync, &rdev2->flags))
2147 			sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
2148 		else if (test_bit(Journal, &rdev2->flags))
2149 			sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
2150 		else if (rdev2->raid_disk >= 0)
2151 			sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
2152 		else
2153 			sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
2154 	}
2155 
2156 	sb->sb_csum = calc_sb_1_csum(sb);
2157 }
2158 
2159 static sector_t super_1_choose_bm_space(sector_t dev_size)
2160 {
2161 	sector_t bm_space;
2162 
2163 	/* if the device is bigger than 8Gig, save 64k for bitmap
2164 	 * usage, if bigger than 200Gig, save 128k
2165 	 */
2166 	if (dev_size < 64*2)
2167 		bm_space = 0;
2168 	else if (dev_size - 64*2 >= 200*1024*1024*2)
2169 		bm_space = 128*2;
2170 	else if (dev_size - 4*2 > 8*1024*1024*2)
2171 		bm_space = 64*2;
2172 	else
2173 		bm_space = 4*2;
2174 	return bm_space;
2175 }
2176 
2177 static unsigned long long
2178 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
2179 {
2180 	struct mdp_superblock_1 *sb;
2181 	sector_t max_sectors;
2182 	if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
2183 		return 0; /* component must fit device */
2184 	if (rdev->data_offset != rdev->new_data_offset)
2185 		return 0; /* too confusing */
2186 	if (rdev->sb_start < rdev->data_offset) {
2187 		/* minor versions 1 and 2; superblock before data */
2188 		max_sectors = bdev_nr_sectors(rdev->bdev) - rdev->data_offset;
2189 		if (!num_sectors || num_sectors > max_sectors)
2190 			num_sectors = max_sectors;
2191 	} else if (rdev->mddev->bitmap_info.offset) {
2192 		/* minor version 0 with bitmap we can't move */
2193 		return 0;
2194 	} else {
2195 		/* minor version 0; superblock after data */
2196 		sector_t sb_start, bm_space;
2197 		sector_t dev_size = bdev_nr_sectors(rdev->bdev);
2198 
2199 		/* 8K is for superblock */
2200 		sb_start = dev_size - 8*2;
2201 		sb_start &= ~(sector_t)(4*2 - 1);
2202 
2203 		bm_space = super_1_choose_bm_space(dev_size);
2204 
2205 		/* Space that can be used to store date needs to decrease
2206 		 * superblock bitmap space and bad block space(4K)
2207 		 */
2208 		max_sectors = sb_start - bm_space - 4*2;
2209 
2210 		if (!num_sectors || num_sectors > max_sectors)
2211 			num_sectors = max_sectors;
2212 		rdev->sb_start = sb_start;
2213 	}
2214 	sb = page_address(rdev->sb_page);
2215 	sb->data_size = cpu_to_le64(num_sectors);
2216 	sb->super_offset = cpu_to_le64(rdev->sb_start);
2217 	sb->sb_csum = calc_sb_1_csum(sb);
2218 	do {
2219 		md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2220 			       rdev->sb_page);
2221 	} while (md_super_wait(rdev->mddev) < 0);
2222 	return num_sectors;
2223 
2224 }
2225 
2226 static int
2227 super_1_allow_new_offset(struct md_rdev *rdev,
2228 			 unsigned long long new_offset)
2229 {
2230 	/* All necessary checks on new >= old have been done */
2231 	if (new_offset >= rdev->data_offset)
2232 		return 1;
2233 
2234 	/* with 1.0 metadata, there is no metadata to tread on
2235 	 * so we can always move back */
2236 	if (rdev->mddev->minor_version == 0)
2237 		return 1;
2238 
2239 	/* otherwise we must be sure not to step on
2240 	 * any metadata, so stay:
2241 	 * 36K beyond start of superblock
2242 	 * beyond end of badblocks
2243 	 * beyond write-intent bitmap
2244 	 */
2245 	if (rdev->sb_start + (32+4)*2 > new_offset)
2246 		return 0;
2247 
2248 	if (!rdev->mddev->bitmap_info.file) {
2249 		struct mddev *mddev = rdev->mddev;
2250 		struct md_bitmap_stats stats;
2251 		int err;
2252 
2253 		err = mddev->bitmap_ops->get_stats(mddev->bitmap, &stats);
2254 		if (!err && rdev->sb_start + mddev->bitmap_info.offset +
2255 		    stats.file_pages * (PAGE_SIZE >> 9) > new_offset)
2256 			return 0;
2257 	}
2258 
2259 	if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2260 		return 0;
2261 
2262 	return 1;
2263 }
2264 
2265 static struct super_type super_types[] = {
2266 	[0] = {
2267 		.name	= "0.90.0",
2268 		.owner	= THIS_MODULE,
2269 		.load_super	    = super_90_load,
2270 		.validate_super	    = super_90_validate,
2271 		.sync_super	    = super_90_sync,
2272 		.rdev_size_change   = super_90_rdev_size_change,
2273 		.allow_new_offset   = super_90_allow_new_offset,
2274 	},
2275 	[1] = {
2276 		.name	= "md-1",
2277 		.owner	= THIS_MODULE,
2278 		.load_super	    = super_1_load,
2279 		.validate_super	    = super_1_validate,
2280 		.sync_super	    = super_1_sync,
2281 		.rdev_size_change   = super_1_rdev_size_change,
2282 		.allow_new_offset   = super_1_allow_new_offset,
2283 	},
2284 };
2285 
2286 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
2287 {
2288 	if (mddev->sync_super) {
2289 		mddev->sync_super(mddev, rdev);
2290 		return;
2291 	}
2292 
2293 	BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2294 
2295 	super_types[mddev->major_version].sync_super(mddev, rdev);
2296 }
2297 
2298 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
2299 {
2300 	struct md_rdev *rdev, *rdev2;
2301 
2302 	rcu_read_lock();
2303 	rdev_for_each_rcu(rdev, mddev1) {
2304 		if (test_bit(Faulty, &rdev->flags) ||
2305 		    test_bit(Journal, &rdev->flags) ||
2306 		    rdev->raid_disk == -1)
2307 			continue;
2308 		rdev_for_each_rcu(rdev2, mddev2) {
2309 			if (test_bit(Faulty, &rdev2->flags) ||
2310 			    test_bit(Journal, &rdev2->flags) ||
2311 			    rdev2->raid_disk == -1)
2312 				continue;
2313 			if (rdev->bdev->bd_disk == rdev2->bdev->bd_disk) {
2314 				rcu_read_unlock();
2315 				return 1;
2316 			}
2317 		}
2318 	}
2319 	rcu_read_unlock();
2320 	return 0;
2321 }
2322 
2323 static LIST_HEAD(pending_raid_disks);
2324 
2325 /*
2326  * Try to register data integrity profile for an mddev
2327  *
2328  * This is called when an array is started and after a disk has been kicked
2329  * from the array. It only succeeds if all working and active component devices
2330  * are integrity capable with matching profiles.
2331  */
2332 int md_integrity_register(struct mddev *mddev)
2333 {
2334 	if (list_empty(&mddev->disks))
2335 		return 0; /* nothing to do */
2336 	if (mddev_is_dm(mddev) || !blk_get_integrity(mddev->gendisk))
2337 		return 0; /* shouldn't register */
2338 
2339 	pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2340 	if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE) ||
2341 	    (mddev->level != 1 && mddev->level != 10 &&
2342 	     bioset_integrity_create(&mddev->io_clone_set, BIO_POOL_SIZE))) {
2343 		/*
2344 		 * No need to handle the failure of bioset_integrity_create,
2345 		 * because the function is called by md_run() -> pers->run(),
2346 		 * md_run calls bioset_exit -> bioset_integrity_free in case
2347 		 * of failure case.
2348 		 */
2349 		pr_err("md: failed to create integrity pool for %s\n",
2350 		       mdname(mddev));
2351 		return -EINVAL;
2352 	}
2353 	return 0;
2354 }
2355 EXPORT_SYMBOL(md_integrity_register);
2356 
2357 static bool rdev_read_only(struct md_rdev *rdev)
2358 {
2359 	return bdev_read_only(rdev->bdev) ||
2360 		(rdev->meta_bdev && bdev_read_only(rdev->meta_bdev));
2361 }
2362 
2363 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2364 {
2365 	char b[BDEVNAME_SIZE];
2366 	int err;
2367 
2368 	/* prevent duplicates */
2369 	if (find_rdev(mddev, rdev->bdev->bd_dev))
2370 		return -EEXIST;
2371 
2372 	if (rdev_read_only(rdev) && mddev->pers)
2373 		return -EROFS;
2374 
2375 	/* make sure rdev->sectors exceeds mddev->dev_sectors */
2376 	if (!test_bit(Journal, &rdev->flags) &&
2377 	    rdev->sectors &&
2378 	    (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2379 		if (mddev->pers) {
2380 			/* Cannot change size, so fail
2381 			 * If mddev->level <= 0, then we don't care
2382 			 * about aligning sizes (e.g. linear)
2383 			 */
2384 			if (mddev->level > 0)
2385 				return -ENOSPC;
2386 		} else
2387 			mddev->dev_sectors = rdev->sectors;
2388 	}
2389 
2390 	/* Verify rdev->desc_nr is unique.
2391 	 * If it is -1, assign a free number, else
2392 	 * check number is not in use
2393 	 */
2394 	rcu_read_lock();
2395 	if (rdev->desc_nr < 0) {
2396 		int choice = 0;
2397 		if (mddev->pers)
2398 			choice = mddev->raid_disks;
2399 		while (md_find_rdev_nr_rcu(mddev, choice))
2400 			choice++;
2401 		rdev->desc_nr = choice;
2402 	} else {
2403 		if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2404 			rcu_read_unlock();
2405 			return -EBUSY;
2406 		}
2407 	}
2408 	rcu_read_unlock();
2409 	if (!test_bit(Journal, &rdev->flags) &&
2410 	    mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2411 		pr_warn("md: %s: array is limited to %d devices\n",
2412 			mdname(mddev), mddev->max_disks);
2413 		return -EBUSY;
2414 	}
2415 	snprintf(b, sizeof(b), "%pg", rdev->bdev);
2416 	strreplace(b, '/', '!');
2417 
2418 	rdev->mddev = mddev;
2419 	pr_debug("md: bind<%s>\n", b);
2420 
2421 	if (mddev->raid_disks)
2422 		mddev_create_serial_pool(mddev, rdev);
2423 
2424 	if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2425 		goto fail;
2426 
2427 	/* failure here is OK */
2428 	err = sysfs_create_link(&rdev->kobj, bdev_kobj(rdev->bdev), "block");
2429 	rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2430 	rdev->sysfs_unack_badblocks =
2431 		sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks");
2432 	rdev->sysfs_badblocks =
2433 		sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks");
2434 
2435 	list_add_rcu(&rdev->same_set, &mddev->disks);
2436 	bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2437 
2438 	/* May as well allow recovery to be retried once */
2439 	mddev->recovery_disabled++;
2440 
2441 	return 0;
2442 
2443  fail:
2444 	pr_warn("md: failed to register dev-%s for %s\n",
2445 		b, mdname(mddev));
2446 	mddev_destroy_serial_pool(mddev, rdev);
2447 	return err;
2448 }
2449 
2450 void md_autodetect_dev(dev_t dev);
2451 
2452 /* just for claiming the bdev */
2453 static struct md_rdev claim_rdev;
2454 
2455 static void export_rdev(struct md_rdev *rdev, struct mddev *mddev)
2456 {
2457 	pr_debug("md: export_rdev(%pg)\n", rdev->bdev);
2458 	md_rdev_clear(rdev);
2459 #ifndef MODULE
2460 	if (test_bit(AutoDetected, &rdev->flags))
2461 		md_autodetect_dev(rdev->bdev->bd_dev);
2462 #endif
2463 	fput(rdev->bdev_file);
2464 	rdev->bdev = NULL;
2465 	kobject_put(&rdev->kobj);
2466 }
2467 
2468 static void md_kick_rdev_from_array(struct md_rdev *rdev)
2469 {
2470 	struct mddev *mddev = rdev->mddev;
2471 
2472 	bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2473 	list_del_rcu(&rdev->same_set);
2474 	pr_debug("md: unbind<%pg>\n", rdev->bdev);
2475 	mddev_destroy_serial_pool(rdev->mddev, rdev);
2476 	WRITE_ONCE(rdev->mddev, NULL);
2477 	sysfs_remove_link(&rdev->kobj, "block");
2478 	sysfs_put(rdev->sysfs_state);
2479 	sysfs_put(rdev->sysfs_unack_badblocks);
2480 	sysfs_put(rdev->sysfs_badblocks);
2481 	rdev->sysfs_state = NULL;
2482 	rdev->sysfs_unack_badblocks = NULL;
2483 	rdev->sysfs_badblocks = NULL;
2484 	rdev->badblocks.count = 0;
2485 
2486 	synchronize_rcu();
2487 
2488 	/*
2489 	 * kobject_del() will wait for all in progress writers to be done, where
2490 	 * reconfig_mutex is held, hence it can't be called under
2491 	 * reconfig_mutex and it's delayed to mddev_unlock().
2492 	 */
2493 	list_add(&rdev->same_set, &mddev->deleting);
2494 }
2495 
2496 static void export_array(struct mddev *mddev)
2497 {
2498 	struct md_rdev *rdev;
2499 
2500 	while (!list_empty(&mddev->disks)) {
2501 		rdev = list_first_entry(&mddev->disks, struct md_rdev,
2502 					same_set);
2503 		md_kick_rdev_from_array(rdev);
2504 	}
2505 	mddev->raid_disks = 0;
2506 	mddev->major_version = 0;
2507 }
2508 
2509 static bool set_in_sync(struct mddev *mddev)
2510 {
2511 	lockdep_assert_held(&mddev->lock);
2512 	if (!mddev->in_sync) {
2513 		mddev->sync_checkers++;
2514 		spin_unlock(&mddev->lock);
2515 		percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2516 		spin_lock(&mddev->lock);
2517 		if (!mddev->in_sync &&
2518 		    percpu_ref_is_zero(&mddev->writes_pending)) {
2519 			mddev->in_sync = 1;
2520 			/*
2521 			 * Ensure ->in_sync is visible before we clear
2522 			 * ->sync_checkers.
2523 			 */
2524 			smp_mb();
2525 			set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2526 			sysfs_notify_dirent_safe(mddev->sysfs_state);
2527 		}
2528 		if (--mddev->sync_checkers == 0)
2529 			percpu_ref_switch_to_percpu(&mddev->writes_pending);
2530 	}
2531 	if (mddev->safemode == 1)
2532 		mddev->safemode = 0;
2533 	return mddev->in_sync;
2534 }
2535 
2536 static void sync_sbs(struct mddev *mddev, int nospares)
2537 {
2538 	/* Update each superblock (in-memory image), but
2539 	 * if we are allowed to, skip spares which already
2540 	 * have the right event counter, or have one earlier
2541 	 * (which would mean they aren't being marked as dirty
2542 	 * with the rest of the array)
2543 	 */
2544 	struct md_rdev *rdev;
2545 	rdev_for_each(rdev, mddev) {
2546 		if (rdev->sb_events == mddev->events ||
2547 		    (nospares &&
2548 		     rdev->raid_disk < 0 &&
2549 		     rdev->sb_events+1 == mddev->events)) {
2550 			/* Don't update this superblock */
2551 			rdev->sb_loaded = 2;
2552 		} else {
2553 			sync_super(mddev, rdev);
2554 			rdev->sb_loaded = 1;
2555 		}
2556 	}
2557 }
2558 
2559 static bool does_sb_need_changing(struct mddev *mddev)
2560 {
2561 	struct md_rdev *rdev = NULL, *iter;
2562 	struct mdp_superblock_1 *sb;
2563 	int role;
2564 
2565 	/* Find a good rdev */
2566 	rdev_for_each(iter, mddev)
2567 		if ((iter->raid_disk >= 0) && !test_bit(Faulty, &iter->flags)) {
2568 			rdev = iter;
2569 			break;
2570 		}
2571 
2572 	/* No good device found. */
2573 	if (!rdev)
2574 		return false;
2575 
2576 	sb = page_address(rdev->sb_page);
2577 	/* Check if a device has become faulty or a spare become active */
2578 	rdev_for_each(rdev, mddev) {
2579 		role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2580 		/* Device activated? */
2581 		if (role == MD_DISK_ROLE_SPARE && rdev->raid_disk >= 0 &&
2582 		    !test_bit(Faulty, &rdev->flags))
2583 			return true;
2584 		/* Device turned faulty? */
2585 		if (test_bit(Faulty, &rdev->flags) && (role < MD_DISK_ROLE_MAX))
2586 			return true;
2587 	}
2588 
2589 	/* Check if any mddev parameters have changed */
2590 	if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2591 	    (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2592 	    (mddev->layout != le32_to_cpu(sb->layout)) ||
2593 	    (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2594 	    (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2595 		return true;
2596 
2597 	return false;
2598 }
2599 
2600 void md_update_sb(struct mddev *mddev, int force_change)
2601 {
2602 	struct md_rdev *rdev;
2603 	int sync_req;
2604 	int nospares = 0;
2605 	int any_badblocks_changed = 0;
2606 	int ret = -1;
2607 
2608 	if (!md_is_rdwr(mddev)) {
2609 		if (force_change)
2610 			set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2611 		return;
2612 	}
2613 
2614 repeat:
2615 	if (mddev_is_clustered(mddev)) {
2616 		if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2617 			force_change = 1;
2618 		if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2619 			nospares = 1;
2620 		ret = md_cluster_ops->metadata_update_start(mddev);
2621 		/* Has someone else has updated the sb */
2622 		if (!does_sb_need_changing(mddev)) {
2623 			if (ret == 0)
2624 				md_cluster_ops->metadata_update_cancel(mddev);
2625 			bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2626 							 BIT(MD_SB_CHANGE_DEVS) |
2627 							 BIT(MD_SB_CHANGE_CLEAN));
2628 			return;
2629 		}
2630 	}
2631 
2632 	/*
2633 	 * First make sure individual recovery_offsets are correct
2634 	 * curr_resync_completed can only be used during recovery.
2635 	 * During reshape/resync it might use array-addresses rather
2636 	 * that device addresses.
2637 	 */
2638 	rdev_for_each(rdev, mddev) {
2639 		if (rdev->raid_disk >= 0 &&
2640 		    mddev->delta_disks >= 0 &&
2641 		    test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2642 		    test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2643 		    !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2644 		    !test_bit(Journal, &rdev->flags) &&
2645 		    !test_bit(In_sync, &rdev->flags) &&
2646 		    mddev->curr_resync_completed > rdev->recovery_offset)
2647 				rdev->recovery_offset = mddev->curr_resync_completed;
2648 
2649 	}
2650 	if (!mddev->persistent) {
2651 		clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2652 		clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2653 		if (!mddev->external) {
2654 			clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2655 			rdev_for_each(rdev, mddev) {
2656 				if (rdev->badblocks.changed) {
2657 					rdev->badblocks.changed = 0;
2658 					ack_all_badblocks(&rdev->badblocks);
2659 					md_error(mddev, rdev);
2660 				}
2661 				clear_bit(Blocked, &rdev->flags);
2662 				clear_bit(BlockedBadBlocks, &rdev->flags);
2663 				wake_up(&rdev->blocked_wait);
2664 			}
2665 		}
2666 		wake_up(&mddev->sb_wait);
2667 		return;
2668 	}
2669 
2670 	spin_lock(&mddev->lock);
2671 
2672 	mddev->utime = ktime_get_real_seconds();
2673 
2674 	if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2675 		force_change = 1;
2676 	if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2677 		/* just a clean<-> dirty transition, possibly leave spares alone,
2678 		 * though if events isn't the right even/odd, we will have to do
2679 		 * spares after all
2680 		 */
2681 		nospares = 1;
2682 	if (force_change)
2683 		nospares = 0;
2684 	if (mddev->degraded)
2685 		/* If the array is degraded, then skipping spares is both
2686 		 * dangerous and fairly pointless.
2687 		 * Dangerous because a device that was removed from the array
2688 		 * might have a event_count that still looks up-to-date,
2689 		 * so it can be re-added without a resync.
2690 		 * Pointless because if there are any spares to skip,
2691 		 * then a recovery will happen and soon that array won't
2692 		 * be degraded any more and the spare can go back to sleep then.
2693 		 */
2694 		nospares = 0;
2695 
2696 	sync_req = mddev->in_sync;
2697 
2698 	/* If this is just a dirty<->clean transition, and the array is clean
2699 	 * and 'events' is odd, we can roll back to the previous clean state */
2700 	if (nospares
2701 	    && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2702 	    && mddev->can_decrease_events
2703 	    && mddev->events != 1) {
2704 		mddev->events--;
2705 		mddev->can_decrease_events = 0;
2706 	} else {
2707 		/* otherwise we have to go forward and ... */
2708 		mddev->events ++;
2709 		mddev->can_decrease_events = nospares;
2710 	}
2711 
2712 	/*
2713 	 * This 64-bit counter should never wrap.
2714 	 * Either we are in around ~1 trillion A.C., assuming
2715 	 * 1 reboot per second, or we have a bug...
2716 	 */
2717 	WARN_ON(mddev->events == 0);
2718 
2719 	rdev_for_each(rdev, mddev) {
2720 		if (rdev->badblocks.changed)
2721 			any_badblocks_changed++;
2722 		if (test_bit(Faulty, &rdev->flags))
2723 			set_bit(FaultRecorded, &rdev->flags);
2724 	}
2725 
2726 	sync_sbs(mddev, nospares);
2727 	spin_unlock(&mddev->lock);
2728 
2729 	pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2730 		 mdname(mddev), mddev->in_sync);
2731 
2732 	mddev_add_trace_msg(mddev, "md md_update_sb");
2733 rewrite:
2734 	mddev->bitmap_ops->update_sb(mddev->bitmap);
2735 	rdev_for_each(rdev, mddev) {
2736 		if (rdev->sb_loaded != 1)
2737 			continue; /* no noise on spare devices */
2738 
2739 		if (!test_bit(Faulty, &rdev->flags)) {
2740 			md_super_write(mddev,rdev,
2741 				       rdev->sb_start, rdev->sb_size,
2742 				       rdev->sb_page);
2743 			pr_debug("md: (write) %pg's sb offset: %llu\n",
2744 				 rdev->bdev,
2745 				 (unsigned long long)rdev->sb_start);
2746 			rdev->sb_events = mddev->events;
2747 			if (rdev->badblocks.size) {
2748 				md_super_write(mddev, rdev,
2749 					       rdev->badblocks.sector,
2750 					       rdev->badblocks.size << 9,
2751 					       rdev->bb_page);
2752 				rdev->badblocks.size = 0;
2753 			}
2754 
2755 		} else
2756 			pr_debug("md: %pg (skipping faulty)\n",
2757 				 rdev->bdev);
2758 	}
2759 	if (md_super_wait(mddev) < 0)
2760 		goto rewrite;
2761 	/* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2762 
2763 	if (mddev_is_clustered(mddev) && ret == 0)
2764 		md_cluster_ops->metadata_update_finish(mddev);
2765 
2766 	if (mddev->in_sync != sync_req ||
2767 	    !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2768 			       BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2769 		/* have to write it out again */
2770 		goto repeat;
2771 	wake_up(&mddev->sb_wait);
2772 	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2773 		sysfs_notify_dirent_safe(mddev->sysfs_completed);
2774 
2775 	rdev_for_each(rdev, mddev) {
2776 		if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2777 			clear_bit(Blocked, &rdev->flags);
2778 
2779 		if (any_badblocks_changed)
2780 			ack_all_badblocks(&rdev->badblocks);
2781 		clear_bit(BlockedBadBlocks, &rdev->flags);
2782 		wake_up(&rdev->blocked_wait);
2783 	}
2784 }
2785 EXPORT_SYMBOL(md_update_sb);
2786 
2787 static int add_bound_rdev(struct md_rdev *rdev)
2788 {
2789 	struct mddev *mddev = rdev->mddev;
2790 	int err = 0;
2791 	bool add_journal = test_bit(Journal, &rdev->flags);
2792 
2793 	if (!mddev->pers->hot_remove_disk || add_journal) {
2794 		/* If there is hot_add_disk but no hot_remove_disk
2795 		 * then added disks for geometry changes,
2796 		 * and should be added immediately.
2797 		 */
2798 		super_types[mddev->major_version].
2799 			validate_super(mddev, NULL/*freshest*/, rdev);
2800 		err = mddev->pers->hot_add_disk(mddev, rdev);
2801 		if (err) {
2802 			md_kick_rdev_from_array(rdev);
2803 			return err;
2804 		}
2805 	}
2806 	sysfs_notify_dirent_safe(rdev->sysfs_state);
2807 
2808 	set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2809 	if (mddev->degraded)
2810 		set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2811 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2812 	md_new_event();
2813 	return 0;
2814 }
2815 
2816 /* words written to sysfs files may, or may not, be \n terminated.
2817  * We want to accept with case. For this we use cmd_match.
2818  */
2819 static int cmd_match(const char *cmd, const char *str)
2820 {
2821 	/* See if cmd, written into a sysfs file, matches
2822 	 * str.  They must either be the same, or cmd can
2823 	 * have a trailing newline
2824 	 */
2825 	while (*cmd && *str && *cmd == *str) {
2826 		cmd++;
2827 		str++;
2828 	}
2829 	if (*cmd == '\n')
2830 		cmd++;
2831 	if (*str || *cmd)
2832 		return 0;
2833 	return 1;
2834 }
2835 
2836 struct rdev_sysfs_entry {
2837 	struct attribute attr;
2838 	ssize_t (*show)(struct md_rdev *, char *);
2839 	ssize_t (*store)(struct md_rdev *, const char *, size_t);
2840 };
2841 
2842 static ssize_t
2843 state_show(struct md_rdev *rdev, char *page)
2844 {
2845 	char *sep = ",";
2846 	size_t len = 0;
2847 	unsigned long flags = READ_ONCE(rdev->flags);
2848 
2849 	if (test_bit(Faulty, &flags) ||
2850 	    (!test_bit(ExternalBbl, &flags) &&
2851 	    rdev->badblocks.unacked_exist))
2852 		len += sprintf(page+len, "faulty%s", sep);
2853 	if (test_bit(In_sync, &flags))
2854 		len += sprintf(page+len, "in_sync%s", sep);
2855 	if (test_bit(Journal, &flags))
2856 		len += sprintf(page+len, "journal%s", sep);
2857 	if (test_bit(WriteMostly, &flags))
2858 		len += sprintf(page+len, "write_mostly%s", sep);
2859 	if (test_bit(Blocked, &flags) ||
2860 	    (rdev->badblocks.unacked_exist
2861 	     && !test_bit(Faulty, &flags)))
2862 		len += sprintf(page+len, "blocked%s", sep);
2863 	if (!test_bit(Faulty, &flags) &&
2864 	    !test_bit(Journal, &flags) &&
2865 	    !test_bit(In_sync, &flags))
2866 		len += sprintf(page+len, "spare%s", sep);
2867 	if (test_bit(WriteErrorSeen, &flags))
2868 		len += sprintf(page+len, "write_error%s", sep);
2869 	if (test_bit(WantReplacement, &flags))
2870 		len += sprintf(page+len, "want_replacement%s", sep);
2871 	if (test_bit(Replacement, &flags))
2872 		len += sprintf(page+len, "replacement%s", sep);
2873 	if (test_bit(ExternalBbl, &flags))
2874 		len += sprintf(page+len, "external_bbl%s", sep);
2875 	if (test_bit(FailFast, &flags))
2876 		len += sprintf(page+len, "failfast%s", sep);
2877 
2878 	if (len)
2879 		len -= strlen(sep);
2880 
2881 	return len+sprintf(page+len, "\n");
2882 }
2883 
2884 static ssize_t
2885 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2886 {
2887 	/* can write
2888 	 *  faulty  - simulates an error
2889 	 *  remove  - disconnects the device
2890 	 *  writemostly - sets write_mostly
2891 	 *  -writemostly - clears write_mostly
2892 	 *  blocked - sets the Blocked flags
2893 	 *  -blocked - clears the Blocked and possibly simulates an error
2894 	 *  insync - sets Insync providing device isn't active
2895 	 *  -insync - clear Insync for a device with a slot assigned,
2896 	 *            so that it gets rebuilt based on bitmap
2897 	 *  write_error - sets WriteErrorSeen
2898 	 *  -write_error - clears WriteErrorSeen
2899 	 *  {,-}failfast - set/clear FailFast
2900 	 */
2901 
2902 	struct mddev *mddev = rdev->mddev;
2903 	int err = -EINVAL;
2904 	bool need_update_sb = false;
2905 
2906 	if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2907 		md_error(rdev->mddev, rdev);
2908 
2909 		if (test_bit(MD_BROKEN, &rdev->mddev->flags))
2910 			err = -EBUSY;
2911 		else
2912 			err = 0;
2913 	} else if (cmd_match(buf, "remove")) {
2914 		if (rdev->mddev->pers) {
2915 			clear_bit(Blocked, &rdev->flags);
2916 			remove_and_add_spares(rdev->mddev, rdev);
2917 		}
2918 		if (rdev->raid_disk >= 0)
2919 			err = -EBUSY;
2920 		else {
2921 			err = 0;
2922 			if (mddev_is_clustered(mddev))
2923 				err = md_cluster_ops->remove_disk(mddev, rdev);
2924 
2925 			if (err == 0) {
2926 				md_kick_rdev_from_array(rdev);
2927 				if (mddev->pers)
2928 					set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2929 				md_new_event();
2930 			}
2931 		}
2932 	} else if (cmd_match(buf, "writemostly")) {
2933 		set_bit(WriteMostly, &rdev->flags);
2934 		mddev_create_serial_pool(rdev->mddev, rdev);
2935 		need_update_sb = true;
2936 		err = 0;
2937 	} else if (cmd_match(buf, "-writemostly")) {
2938 		mddev_destroy_serial_pool(rdev->mddev, rdev);
2939 		clear_bit(WriteMostly, &rdev->flags);
2940 		need_update_sb = true;
2941 		err = 0;
2942 	} else if (cmd_match(buf, "blocked")) {
2943 		set_bit(Blocked, &rdev->flags);
2944 		err = 0;
2945 	} else if (cmd_match(buf, "-blocked")) {
2946 		if (!test_bit(Faulty, &rdev->flags) &&
2947 		    !test_bit(ExternalBbl, &rdev->flags) &&
2948 		    rdev->badblocks.unacked_exist) {
2949 			/* metadata handler doesn't understand badblocks,
2950 			 * so we need to fail the device
2951 			 */
2952 			md_error(rdev->mddev, rdev);
2953 		}
2954 		clear_bit(Blocked, &rdev->flags);
2955 		clear_bit(BlockedBadBlocks, &rdev->flags);
2956 		wake_up(&rdev->blocked_wait);
2957 		set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2958 
2959 		err = 0;
2960 	} else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2961 		set_bit(In_sync, &rdev->flags);
2962 		err = 0;
2963 	} else if (cmd_match(buf, "failfast")) {
2964 		set_bit(FailFast, &rdev->flags);
2965 		need_update_sb = true;
2966 		err = 0;
2967 	} else if (cmd_match(buf, "-failfast")) {
2968 		clear_bit(FailFast, &rdev->flags);
2969 		need_update_sb = true;
2970 		err = 0;
2971 	} else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2972 		   !test_bit(Journal, &rdev->flags)) {
2973 		if (rdev->mddev->pers == NULL) {
2974 			clear_bit(In_sync, &rdev->flags);
2975 			rdev->saved_raid_disk = rdev->raid_disk;
2976 			rdev->raid_disk = -1;
2977 			err = 0;
2978 		}
2979 	} else if (cmd_match(buf, "write_error")) {
2980 		set_bit(WriteErrorSeen, &rdev->flags);
2981 		err = 0;
2982 	} else if (cmd_match(buf, "-write_error")) {
2983 		clear_bit(WriteErrorSeen, &rdev->flags);
2984 		err = 0;
2985 	} else if (cmd_match(buf, "want_replacement")) {
2986 		/* Any non-spare device that is not a replacement can
2987 		 * become want_replacement at any time, but we then need to
2988 		 * check if recovery is needed.
2989 		 */
2990 		if (rdev->raid_disk >= 0 &&
2991 		    !test_bit(Journal, &rdev->flags) &&
2992 		    !test_bit(Replacement, &rdev->flags))
2993 			set_bit(WantReplacement, &rdev->flags);
2994 		set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2995 		err = 0;
2996 	} else if (cmd_match(buf, "-want_replacement")) {
2997 		/* Clearing 'want_replacement' is always allowed.
2998 		 * Once replacements starts it is too late though.
2999 		 */
3000 		err = 0;
3001 		clear_bit(WantReplacement, &rdev->flags);
3002 	} else if (cmd_match(buf, "replacement")) {
3003 		/* Can only set a device as a replacement when array has not
3004 		 * yet been started.  Once running, replacement is automatic
3005 		 * from spares, or by assigning 'slot'.
3006 		 */
3007 		if (rdev->mddev->pers)
3008 			err = -EBUSY;
3009 		else {
3010 			set_bit(Replacement, &rdev->flags);
3011 			err = 0;
3012 		}
3013 	} else if (cmd_match(buf, "-replacement")) {
3014 		/* Similarly, can only clear Replacement before start */
3015 		if (rdev->mddev->pers)
3016 			err = -EBUSY;
3017 		else {
3018 			clear_bit(Replacement, &rdev->flags);
3019 			err = 0;
3020 		}
3021 	} else if (cmd_match(buf, "re-add")) {
3022 		if (!rdev->mddev->pers)
3023 			err = -EINVAL;
3024 		else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3025 				rdev->saved_raid_disk >= 0) {
3026 			/* clear_bit is performed _after_ all the devices
3027 			 * have their local Faulty bit cleared. If any writes
3028 			 * happen in the meantime in the local node, they
3029 			 * will land in the local bitmap, which will be synced
3030 			 * by this node eventually
3031 			 */
3032 			if (!mddev_is_clustered(rdev->mddev) ||
3033 			    (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3034 				clear_bit(Faulty, &rdev->flags);
3035 				err = add_bound_rdev(rdev);
3036 			}
3037 		} else
3038 			err = -EBUSY;
3039 	} else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3040 		set_bit(ExternalBbl, &rdev->flags);
3041 		rdev->badblocks.shift = 0;
3042 		err = 0;
3043 	} else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3044 		clear_bit(ExternalBbl, &rdev->flags);
3045 		err = 0;
3046 	}
3047 	if (need_update_sb)
3048 		md_update_sb(mddev, 1);
3049 	if (!err)
3050 		sysfs_notify_dirent_safe(rdev->sysfs_state);
3051 	return err ? err : len;
3052 }
3053 static struct rdev_sysfs_entry rdev_state =
3054 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
3055 
3056 static ssize_t
3057 errors_show(struct md_rdev *rdev, char *page)
3058 {
3059 	return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3060 }
3061 
3062 static ssize_t
3063 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
3064 {
3065 	unsigned int n;
3066 	int rv;
3067 
3068 	rv = kstrtouint(buf, 10, &n);
3069 	if (rv < 0)
3070 		return rv;
3071 	atomic_set(&rdev->corrected_errors, n);
3072 	return len;
3073 }
3074 static struct rdev_sysfs_entry rdev_errors =
3075 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
3076 
3077 static ssize_t
3078 slot_show(struct md_rdev *rdev, char *page)
3079 {
3080 	if (test_bit(Journal, &rdev->flags))
3081 		return sprintf(page, "journal\n");
3082 	else if (rdev->raid_disk < 0)
3083 		return sprintf(page, "none\n");
3084 	else
3085 		return sprintf(page, "%d\n", rdev->raid_disk);
3086 }
3087 
3088 static ssize_t
3089 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
3090 {
3091 	int slot;
3092 	int err;
3093 
3094 	if (test_bit(Journal, &rdev->flags))
3095 		return -EBUSY;
3096 	if (strncmp(buf, "none", 4)==0)
3097 		slot = -1;
3098 	else {
3099 		err = kstrtouint(buf, 10, (unsigned int *)&slot);
3100 		if (err < 0)
3101 			return err;
3102 		if (slot < 0)
3103 			/* overflow */
3104 			return -ENOSPC;
3105 	}
3106 	if (rdev->mddev->pers && slot == -1) {
3107 		/* Setting 'slot' on an active array requires also
3108 		 * updating the 'rd%d' link, and communicating
3109 		 * with the personality with ->hot_*_disk.
3110 		 * For now we only support removing
3111 		 * failed/spare devices.  This normally happens automatically,
3112 		 * but not when the metadata is externally managed.
3113 		 */
3114 		if (rdev->raid_disk == -1)
3115 			return -EEXIST;
3116 		/* personality does all needed checks */
3117 		if (rdev->mddev->pers->hot_remove_disk == NULL)
3118 			return -EINVAL;
3119 		clear_bit(Blocked, &rdev->flags);
3120 		remove_and_add_spares(rdev->mddev, rdev);
3121 		if (rdev->raid_disk >= 0)
3122 			return -EBUSY;
3123 		set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3124 	} else if (rdev->mddev->pers) {
3125 		/* Activating a spare .. or possibly reactivating
3126 		 * if we ever get bitmaps working here.
3127 		 */
3128 		int err;
3129 
3130 		if (rdev->raid_disk != -1)
3131 			return -EBUSY;
3132 
3133 		if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3134 			return -EBUSY;
3135 
3136 		if (rdev->mddev->pers->hot_add_disk == NULL)
3137 			return -EINVAL;
3138 
3139 		if (slot >= rdev->mddev->raid_disks &&
3140 		    slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3141 			return -ENOSPC;
3142 
3143 		rdev->raid_disk = slot;
3144 		if (test_bit(In_sync, &rdev->flags))
3145 			rdev->saved_raid_disk = slot;
3146 		else
3147 			rdev->saved_raid_disk = -1;
3148 		clear_bit(In_sync, &rdev->flags);
3149 		clear_bit(Bitmap_sync, &rdev->flags);
3150 		err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
3151 		if (err) {
3152 			rdev->raid_disk = -1;
3153 			return err;
3154 		} else
3155 			sysfs_notify_dirent_safe(rdev->sysfs_state);
3156 		/* failure here is OK */;
3157 		sysfs_link_rdev(rdev->mddev, rdev);
3158 		/* don't wakeup anyone, leave that to userspace. */
3159 	} else {
3160 		if (slot >= rdev->mddev->raid_disks &&
3161 		    slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3162 			return -ENOSPC;
3163 		rdev->raid_disk = slot;
3164 		/* assume it is working */
3165 		clear_bit(Faulty, &rdev->flags);
3166 		clear_bit(WriteMostly, &rdev->flags);
3167 		set_bit(In_sync, &rdev->flags);
3168 		sysfs_notify_dirent_safe(rdev->sysfs_state);
3169 	}
3170 	return len;
3171 }
3172 
3173 static struct rdev_sysfs_entry rdev_slot =
3174 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
3175 
3176 static ssize_t
3177 offset_show(struct md_rdev *rdev, char *page)
3178 {
3179 	return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
3180 }
3181 
3182 static ssize_t
3183 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
3184 {
3185 	unsigned long long offset;
3186 	if (kstrtoull(buf, 10, &offset) < 0)
3187 		return -EINVAL;
3188 	if (rdev->mddev->pers && rdev->raid_disk >= 0)
3189 		return -EBUSY;
3190 	if (rdev->sectors && rdev->mddev->external)
3191 		/* Must set offset before size, so overlap checks
3192 		 * can be sane */
3193 		return -EBUSY;
3194 	rdev->data_offset = offset;
3195 	rdev->new_data_offset = offset;
3196 	return len;
3197 }
3198 
3199 static struct rdev_sysfs_entry rdev_offset =
3200 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
3201 
3202 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3203 {
3204 	return sprintf(page, "%llu\n",
3205 		       (unsigned long long)rdev->new_data_offset);
3206 }
3207 
3208 static ssize_t new_offset_store(struct md_rdev *rdev,
3209 				const char *buf, size_t len)
3210 {
3211 	unsigned long long new_offset;
3212 	struct mddev *mddev = rdev->mddev;
3213 
3214 	if (kstrtoull(buf, 10, &new_offset) < 0)
3215 		return -EINVAL;
3216 
3217 	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3218 		return -EBUSY;
3219 	if (new_offset == rdev->data_offset)
3220 		/* reset is always permitted */
3221 		;
3222 	else if (new_offset > rdev->data_offset) {
3223 		/* must not push array size beyond rdev_sectors */
3224 		if (new_offset - rdev->data_offset
3225 		    + mddev->dev_sectors > rdev->sectors)
3226 				return -E2BIG;
3227 	}
3228 	/* Metadata worries about other space details. */
3229 
3230 	/* decreasing the offset is inconsistent with a backwards
3231 	 * reshape.
3232 	 */
3233 	if (new_offset < rdev->data_offset &&
3234 	    mddev->reshape_backwards)
3235 		return -EINVAL;
3236 	/* Increasing offset is inconsistent with forwards
3237 	 * reshape.  reshape_direction should be set to
3238 	 * 'backwards' first.
3239 	 */
3240 	if (new_offset > rdev->data_offset &&
3241 	    !mddev->reshape_backwards)
3242 		return -EINVAL;
3243 
3244 	if (mddev->pers && mddev->persistent &&
3245 	    !super_types[mddev->major_version]
3246 	    .allow_new_offset(rdev, new_offset))
3247 		return -E2BIG;
3248 	rdev->new_data_offset = new_offset;
3249 	if (new_offset > rdev->data_offset)
3250 		mddev->reshape_backwards = 1;
3251 	else if (new_offset < rdev->data_offset)
3252 		mddev->reshape_backwards = 0;
3253 
3254 	return len;
3255 }
3256 static struct rdev_sysfs_entry rdev_new_offset =
3257 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3258 
3259 static ssize_t
3260 rdev_size_show(struct md_rdev *rdev, char *page)
3261 {
3262 	return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
3263 }
3264 
3265 static int md_rdevs_overlap(struct md_rdev *a, struct md_rdev *b)
3266 {
3267 	/* check if two start/length pairs overlap */
3268 	if (a->data_offset + a->sectors <= b->data_offset)
3269 		return false;
3270 	if (b->data_offset + b->sectors <= a->data_offset)
3271 		return false;
3272 	return true;
3273 }
3274 
3275 static bool md_rdev_overlaps(struct md_rdev *rdev)
3276 {
3277 	struct mddev *mddev;
3278 	struct md_rdev *rdev2;
3279 
3280 	spin_lock(&all_mddevs_lock);
3281 	list_for_each_entry(mddev, &all_mddevs, all_mddevs) {
3282 		if (test_bit(MD_DELETED, &mddev->flags))
3283 			continue;
3284 		rdev_for_each(rdev2, mddev) {
3285 			if (rdev != rdev2 && rdev->bdev == rdev2->bdev &&
3286 			    md_rdevs_overlap(rdev, rdev2)) {
3287 				spin_unlock(&all_mddevs_lock);
3288 				return true;
3289 			}
3290 		}
3291 	}
3292 	spin_unlock(&all_mddevs_lock);
3293 	return false;
3294 }
3295 
3296 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3297 {
3298 	unsigned long long blocks;
3299 	sector_t new;
3300 
3301 	if (kstrtoull(buf, 10, &blocks) < 0)
3302 		return -EINVAL;
3303 
3304 	if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3305 		return -EINVAL; /* sector conversion overflow */
3306 
3307 	new = blocks * 2;
3308 	if (new != blocks * 2)
3309 		return -EINVAL; /* unsigned long long to sector_t overflow */
3310 
3311 	*sectors = new;
3312 	return 0;
3313 }
3314 
3315 static ssize_t
3316 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3317 {
3318 	struct mddev *my_mddev = rdev->mddev;
3319 	sector_t oldsectors = rdev->sectors;
3320 	sector_t sectors;
3321 
3322 	if (test_bit(Journal, &rdev->flags))
3323 		return -EBUSY;
3324 	if (strict_blocks_to_sectors(buf, &sectors) < 0)
3325 		return -EINVAL;
3326 	if (rdev->data_offset != rdev->new_data_offset)
3327 		return -EINVAL; /* too confusing */
3328 	if (my_mddev->pers && rdev->raid_disk >= 0) {
3329 		if (my_mddev->persistent) {
3330 			sectors = super_types[my_mddev->major_version].
3331 				rdev_size_change(rdev, sectors);
3332 			if (!sectors)
3333 				return -EBUSY;
3334 		} else if (!sectors)
3335 			sectors = bdev_nr_sectors(rdev->bdev) -
3336 				rdev->data_offset;
3337 		if (!my_mddev->pers->resize)
3338 			/* Cannot change size for RAID0 or Linear etc */
3339 			return -EINVAL;
3340 	}
3341 	if (sectors < my_mddev->dev_sectors)
3342 		return -EINVAL; /* component must fit device */
3343 
3344 	rdev->sectors = sectors;
3345 
3346 	/*
3347 	 * Check that all other rdevs with the same bdev do not overlap.  This
3348 	 * check does not provide a hard guarantee, it just helps avoid
3349 	 * dangerous mistakes.
3350 	 */
3351 	if (sectors > oldsectors && my_mddev->external &&
3352 	    md_rdev_overlaps(rdev)) {
3353 		/*
3354 		 * Someone else could have slipped in a size change here, but
3355 		 * doing so is just silly.  We put oldsectors back because we
3356 		 * know it is safe, and trust userspace not to race with itself.
3357 		 */
3358 		rdev->sectors = oldsectors;
3359 		return -EBUSY;
3360 	}
3361 	return len;
3362 }
3363 
3364 static struct rdev_sysfs_entry rdev_size =
3365 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3366 
3367 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3368 {
3369 	unsigned long long recovery_start = rdev->recovery_offset;
3370 
3371 	if (test_bit(In_sync, &rdev->flags) ||
3372 	    recovery_start == MaxSector)
3373 		return sprintf(page, "none\n");
3374 
3375 	return sprintf(page, "%llu\n", recovery_start);
3376 }
3377 
3378 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3379 {
3380 	unsigned long long recovery_start;
3381 
3382 	if (cmd_match(buf, "none"))
3383 		recovery_start = MaxSector;
3384 	else if (kstrtoull(buf, 10, &recovery_start))
3385 		return -EINVAL;
3386 
3387 	if (rdev->mddev->pers &&
3388 	    rdev->raid_disk >= 0)
3389 		return -EBUSY;
3390 
3391 	rdev->recovery_offset = recovery_start;
3392 	if (recovery_start == MaxSector)
3393 		set_bit(In_sync, &rdev->flags);
3394 	else
3395 		clear_bit(In_sync, &rdev->flags);
3396 	return len;
3397 }
3398 
3399 static struct rdev_sysfs_entry rdev_recovery_start =
3400 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3401 
3402 /* sysfs access to bad-blocks list.
3403  * We present two files.
3404  * 'bad-blocks' lists sector numbers and lengths of ranges that
3405  *    are recorded as bad.  The list is truncated to fit within
3406  *    the one-page limit of sysfs.
3407  *    Writing "sector length" to this file adds an acknowledged
3408  *    bad block list.
3409  * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3410  *    been acknowledged.  Writing to this file adds bad blocks
3411  *    without acknowledging them.  This is largely for testing.
3412  */
3413 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3414 {
3415 	return badblocks_show(&rdev->badblocks, page, 0);
3416 }
3417 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3418 {
3419 	int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3420 	/* Maybe that ack was all we needed */
3421 	if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3422 		wake_up(&rdev->blocked_wait);
3423 	return rv;
3424 }
3425 static struct rdev_sysfs_entry rdev_bad_blocks =
3426 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3427 
3428 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3429 {
3430 	return badblocks_show(&rdev->badblocks, page, 1);
3431 }
3432 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3433 {
3434 	return badblocks_store(&rdev->badblocks, page, len, 1);
3435 }
3436 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3437 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3438 
3439 static ssize_t
3440 ppl_sector_show(struct md_rdev *rdev, char *page)
3441 {
3442 	return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3443 }
3444 
3445 static ssize_t
3446 ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3447 {
3448 	unsigned long long sector;
3449 
3450 	if (kstrtoull(buf, 10, &sector) < 0)
3451 		return -EINVAL;
3452 	if (sector != (sector_t)sector)
3453 		return -EINVAL;
3454 
3455 	if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3456 	    rdev->raid_disk >= 0)
3457 		return -EBUSY;
3458 
3459 	if (rdev->mddev->persistent) {
3460 		if (rdev->mddev->major_version == 0)
3461 			return -EINVAL;
3462 		if ((sector > rdev->sb_start &&
3463 		     sector - rdev->sb_start > S16_MAX) ||
3464 		    (sector < rdev->sb_start &&
3465 		     rdev->sb_start - sector > -S16_MIN))
3466 			return -EINVAL;
3467 		rdev->ppl.offset = sector - rdev->sb_start;
3468 	} else if (!rdev->mddev->external) {
3469 		return -EBUSY;
3470 	}
3471 	rdev->ppl.sector = sector;
3472 	return len;
3473 }
3474 
3475 static struct rdev_sysfs_entry rdev_ppl_sector =
3476 __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3477 
3478 static ssize_t
3479 ppl_size_show(struct md_rdev *rdev, char *page)
3480 {
3481 	return sprintf(page, "%u\n", rdev->ppl.size);
3482 }
3483 
3484 static ssize_t
3485 ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3486 {
3487 	unsigned int size;
3488 
3489 	if (kstrtouint(buf, 10, &size) < 0)
3490 		return -EINVAL;
3491 
3492 	if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3493 	    rdev->raid_disk >= 0)
3494 		return -EBUSY;
3495 
3496 	if (rdev->mddev->persistent) {
3497 		if (rdev->mddev->major_version == 0)
3498 			return -EINVAL;
3499 		if (size > U16_MAX)
3500 			return -EINVAL;
3501 	} else if (!rdev->mddev->external) {
3502 		return -EBUSY;
3503 	}
3504 	rdev->ppl.size = size;
3505 	return len;
3506 }
3507 
3508 static struct rdev_sysfs_entry rdev_ppl_size =
3509 __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3510 
3511 static struct attribute *rdev_default_attrs[] = {
3512 	&rdev_state.attr,
3513 	&rdev_errors.attr,
3514 	&rdev_slot.attr,
3515 	&rdev_offset.attr,
3516 	&rdev_new_offset.attr,
3517 	&rdev_size.attr,
3518 	&rdev_recovery_start.attr,
3519 	&rdev_bad_blocks.attr,
3520 	&rdev_unack_bad_blocks.attr,
3521 	&rdev_ppl_sector.attr,
3522 	&rdev_ppl_size.attr,
3523 	NULL,
3524 };
3525 ATTRIBUTE_GROUPS(rdev_default);
3526 static ssize_t
3527 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3528 {
3529 	struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3530 	struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3531 
3532 	if (!entry->show)
3533 		return -EIO;
3534 	if (!rdev->mddev)
3535 		return -ENODEV;
3536 	return entry->show(rdev, page);
3537 }
3538 
3539 static ssize_t
3540 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3541 	      const char *page, size_t length)
3542 {
3543 	struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3544 	struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3545 	struct kernfs_node *kn = NULL;
3546 	bool suspend = false;
3547 	ssize_t rv;
3548 	struct mddev *mddev = READ_ONCE(rdev->mddev);
3549 
3550 	if (!entry->store)
3551 		return -EIO;
3552 	if (!capable(CAP_SYS_ADMIN))
3553 		return -EACCES;
3554 	if (!mddev)
3555 		return -ENODEV;
3556 
3557 	if (entry->store == state_store) {
3558 		if (cmd_match(page, "remove"))
3559 			kn = sysfs_break_active_protection(kobj, attr);
3560 		if (cmd_match(page, "remove") || cmd_match(page, "re-add") ||
3561 		    cmd_match(page, "writemostly") ||
3562 		    cmd_match(page, "-writemostly"))
3563 			suspend = true;
3564 	}
3565 
3566 	rv = suspend ? mddev_suspend_and_lock(mddev) : mddev_lock(mddev);
3567 	if (!rv) {
3568 		if (rdev->mddev == NULL)
3569 			rv = -ENODEV;
3570 		else
3571 			rv = entry->store(rdev, page, length);
3572 		suspend ? mddev_unlock_and_resume(mddev) : mddev_unlock(mddev);
3573 	}
3574 
3575 	if (kn)
3576 		sysfs_unbreak_active_protection(kn);
3577 
3578 	return rv;
3579 }
3580 
3581 static void rdev_free(struct kobject *ko)
3582 {
3583 	struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3584 	kfree(rdev);
3585 }
3586 static const struct sysfs_ops rdev_sysfs_ops = {
3587 	.show		= rdev_attr_show,
3588 	.store		= rdev_attr_store,
3589 };
3590 static const struct kobj_type rdev_ktype = {
3591 	.release	= rdev_free,
3592 	.sysfs_ops	= &rdev_sysfs_ops,
3593 	.default_groups	= rdev_default_groups,
3594 };
3595 
3596 int md_rdev_init(struct md_rdev *rdev)
3597 {
3598 	rdev->desc_nr = -1;
3599 	rdev->saved_raid_disk = -1;
3600 	rdev->raid_disk = -1;
3601 	rdev->flags = 0;
3602 	rdev->data_offset = 0;
3603 	rdev->new_data_offset = 0;
3604 	rdev->sb_events = 0;
3605 	rdev->last_read_error = 0;
3606 	rdev->sb_loaded = 0;
3607 	rdev->bb_page = NULL;
3608 	atomic_set(&rdev->nr_pending, 0);
3609 	atomic_set(&rdev->read_errors, 0);
3610 	atomic_set(&rdev->corrected_errors, 0);
3611 
3612 	INIT_LIST_HEAD(&rdev->same_set);
3613 	init_waitqueue_head(&rdev->blocked_wait);
3614 
3615 	/* Add space to store bad block list.
3616 	 * This reserves the space even on arrays where it cannot
3617 	 * be used - I wonder if that matters
3618 	 */
3619 	return badblocks_init(&rdev->badblocks, 0);
3620 }
3621 EXPORT_SYMBOL_GPL(md_rdev_init);
3622 
3623 /*
3624  * Import a device. If 'super_format' >= 0, then sanity check the superblock
3625  *
3626  * mark the device faulty if:
3627  *
3628  *   - the device is nonexistent (zero size)
3629  *   - the device has no valid superblock
3630  *
3631  * a faulty rdev _never_ has rdev->sb set.
3632  */
3633 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3634 {
3635 	struct md_rdev *rdev;
3636 	sector_t size;
3637 	int err;
3638 
3639 	rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3640 	if (!rdev)
3641 		return ERR_PTR(-ENOMEM);
3642 
3643 	err = md_rdev_init(rdev);
3644 	if (err)
3645 		goto out_free_rdev;
3646 	err = alloc_disk_sb(rdev);
3647 	if (err)
3648 		goto out_clear_rdev;
3649 
3650 	rdev->bdev_file = bdev_file_open_by_dev(newdev,
3651 			BLK_OPEN_READ | BLK_OPEN_WRITE,
3652 			super_format == -2 ? &claim_rdev : rdev, NULL);
3653 	if (IS_ERR(rdev->bdev_file)) {
3654 		pr_warn("md: could not open device unknown-block(%u,%u).\n",
3655 			MAJOR(newdev), MINOR(newdev));
3656 		err = PTR_ERR(rdev->bdev_file);
3657 		goto out_clear_rdev;
3658 	}
3659 	rdev->bdev = file_bdev(rdev->bdev_file);
3660 
3661 	kobject_init(&rdev->kobj, &rdev_ktype);
3662 
3663 	size = bdev_nr_bytes(rdev->bdev) >> BLOCK_SIZE_BITS;
3664 	if (!size) {
3665 		pr_warn("md: %pg has zero or unknown size, marking faulty!\n",
3666 			rdev->bdev);
3667 		err = -EINVAL;
3668 		goto out_blkdev_put;
3669 	}
3670 
3671 	if (super_format >= 0) {
3672 		err = super_types[super_format].
3673 			load_super(rdev, NULL, super_minor);
3674 		if (err == -EINVAL) {
3675 			pr_warn("md: %pg does not have a valid v%d.%d superblock, not importing!\n",
3676 				rdev->bdev,
3677 				super_format, super_minor);
3678 			goto out_blkdev_put;
3679 		}
3680 		if (err < 0) {
3681 			pr_warn("md: could not read %pg's sb, not importing!\n",
3682 				rdev->bdev);
3683 			goto out_blkdev_put;
3684 		}
3685 	}
3686 
3687 	return rdev;
3688 
3689 out_blkdev_put:
3690 	fput(rdev->bdev_file);
3691 out_clear_rdev:
3692 	md_rdev_clear(rdev);
3693 out_free_rdev:
3694 	kfree(rdev);
3695 	return ERR_PTR(err);
3696 }
3697 
3698 /*
3699  * Check a full RAID array for plausibility
3700  */
3701 
3702 static int analyze_sbs(struct mddev *mddev)
3703 {
3704 	int i;
3705 	struct md_rdev *rdev, *freshest, *tmp;
3706 
3707 	freshest = NULL;
3708 	rdev_for_each_safe(rdev, tmp, mddev)
3709 		switch (super_types[mddev->major_version].
3710 			load_super(rdev, freshest, mddev->minor_version)) {
3711 		case 1:
3712 			freshest = rdev;
3713 			break;
3714 		case 0:
3715 			break;
3716 		default:
3717 			pr_warn("md: fatal superblock inconsistency in %pg -- removing from array\n",
3718 				rdev->bdev);
3719 			md_kick_rdev_from_array(rdev);
3720 		}
3721 
3722 	/* Cannot find a valid fresh disk */
3723 	if (!freshest) {
3724 		pr_warn("md: cannot find a valid disk\n");
3725 		return -EINVAL;
3726 	}
3727 
3728 	super_types[mddev->major_version].
3729 		validate_super(mddev, NULL/*freshest*/, freshest);
3730 
3731 	i = 0;
3732 	rdev_for_each_safe(rdev, tmp, mddev) {
3733 		if (mddev->max_disks &&
3734 		    (rdev->desc_nr >= mddev->max_disks ||
3735 		     i > mddev->max_disks)) {
3736 			pr_warn("md: %s: %pg: only %d devices permitted\n",
3737 				mdname(mddev), rdev->bdev,
3738 				mddev->max_disks);
3739 			md_kick_rdev_from_array(rdev);
3740 			continue;
3741 		}
3742 		if (rdev != freshest) {
3743 			if (super_types[mddev->major_version].
3744 			    validate_super(mddev, freshest, rdev)) {
3745 				pr_warn("md: kicking non-fresh %pg from array!\n",
3746 					rdev->bdev);
3747 				md_kick_rdev_from_array(rdev);
3748 				continue;
3749 			}
3750 		}
3751 		if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3752 		    !test_bit(Journal, &rdev->flags)) {
3753 			rdev->raid_disk = -1;
3754 			clear_bit(In_sync, &rdev->flags);
3755 		}
3756 	}
3757 
3758 	return 0;
3759 }
3760 
3761 /* Read a fixed-point number.
3762  * Numbers in sysfs attributes should be in "standard" units where
3763  * possible, so time should be in seconds.
3764  * However we internally use a a much smaller unit such as
3765  * milliseconds or jiffies.
3766  * This function takes a decimal number with a possible fractional
3767  * component, and produces an integer which is the result of
3768  * multiplying that number by 10^'scale'.
3769  * all without any floating-point arithmetic.
3770  */
3771 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3772 {
3773 	unsigned long result = 0;
3774 	long decimals = -1;
3775 	while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3776 		if (*cp == '.')
3777 			decimals = 0;
3778 		else if (decimals < scale) {
3779 			unsigned int value;
3780 			value = *cp - '0';
3781 			result = result * 10 + value;
3782 			if (decimals >= 0)
3783 				decimals++;
3784 		}
3785 		cp++;
3786 	}
3787 	if (*cp == '\n')
3788 		cp++;
3789 	if (*cp)
3790 		return -EINVAL;
3791 	if (decimals < 0)
3792 		decimals = 0;
3793 	*res = result * int_pow(10, scale - decimals);
3794 	return 0;
3795 }
3796 
3797 static ssize_t
3798 safe_delay_show(struct mddev *mddev, char *page)
3799 {
3800 	unsigned int msec = ((unsigned long)mddev->safemode_delay*1000)/HZ;
3801 
3802 	return sprintf(page, "%u.%03u\n", msec/1000, msec%1000);
3803 }
3804 static ssize_t
3805 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3806 {
3807 	unsigned long msec;
3808 
3809 	if (mddev_is_clustered(mddev)) {
3810 		pr_warn("md: Safemode is disabled for clustered mode\n");
3811 		return -EINVAL;
3812 	}
3813 
3814 	if (strict_strtoul_scaled(cbuf, &msec, 3) < 0 || msec > UINT_MAX / HZ)
3815 		return -EINVAL;
3816 	if (msec == 0)
3817 		mddev->safemode_delay = 0;
3818 	else {
3819 		unsigned long old_delay = mddev->safemode_delay;
3820 		unsigned long new_delay = (msec*HZ)/1000;
3821 
3822 		if (new_delay == 0)
3823 			new_delay = 1;
3824 		mddev->safemode_delay = new_delay;
3825 		if (new_delay < old_delay || old_delay == 0)
3826 			mod_timer(&mddev->safemode_timer, jiffies+1);
3827 	}
3828 	return len;
3829 }
3830 static struct md_sysfs_entry md_safe_delay =
3831 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3832 
3833 static ssize_t
3834 level_show(struct mddev *mddev, char *page)
3835 {
3836 	struct md_personality *p;
3837 	int ret;
3838 	spin_lock(&mddev->lock);
3839 	p = mddev->pers;
3840 	if (p)
3841 		ret = sprintf(page, "%s\n", p->name);
3842 	else if (mddev->clevel[0])
3843 		ret = sprintf(page, "%s\n", mddev->clevel);
3844 	else if (mddev->level != LEVEL_NONE)
3845 		ret = sprintf(page, "%d\n", mddev->level);
3846 	else
3847 		ret = 0;
3848 	spin_unlock(&mddev->lock);
3849 	return ret;
3850 }
3851 
3852 static ssize_t
3853 level_store(struct mddev *mddev, const char *buf, size_t len)
3854 {
3855 	char clevel[16];
3856 	ssize_t rv;
3857 	size_t slen = len;
3858 	struct md_personality *pers, *oldpers;
3859 	long level;
3860 	void *priv, *oldpriv;
3861 	struct md_rdev *rdev;
3862 
3863 	if (slen == 0 || slen >= sizeof(clevel))
3864 		return -EINVAL;
3865 
3866 	rv = mddev_suspend_and_lock(mddev);
3867 	if (rv)
3868 		return rv;
3869 
3870 	if (mddev->pers == NULL) {
3871 		memcpy(mddev->clevel, buf, slen);
3872 		if (mddev->clevel[slen-1] == '\n')
3873 			slen--;
3874 		mddev->clevel[slen] = 0;
3875 		mddev->level = LEVEL_NONE;
3876 		rv = len;
3877 		goto out_unlock;
3878 	}
3879 	rv = -EROFS;
3880 	if (!md_is_rdwr(mddev))
3881 		goto out_unlock;
3882 
3883 	/* request to change the personality.  Need to ensure:
3884 	 *  - array is not engaged in resync/recovery/reshape
3885 	 *  - old personality can be suspended
3886 	 *  - new personality will access other array.
3887 	 */
3888 
3889 	rv = -EBUSY;
3890 	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3891 	    mddev->reshape_position != MaxSector ||
3892 	    mddev->sysfs_active)
3893 		goto out_unlock;
3894 
3895 	rv = -EINVAL;
3896 	if (!mddev->pers->quiesce) {
3897 		pr_warn("md: %s: %s does not support online personality change\n",
3898 			mdname(mddev), mddev->pers->name);
3899 		goto out_unlock;
3900 	}
3901 
3902 	/* Now find the new personality */
3903 	memcpy(clevel, buf, slen);
3904 	if (clevel[slen-1] == '\n')
3905 		slen--;
3906 	clevel[slen] = 0;
3907 	if (kstrtol(clevel, 10, &level))
3908 		level = LEVEL_NONE;
3909 
3910 	if (request_module("md-%s", clevel) != 0)
3911 		request_module("md-level-%s", clevel);
3912 	spin_lock(&pers_lock);
3913 	pers = find_pers(level, clevel);
3914 	if (!pers || !try_module_get(pers->owner)) {
3915 		spin_unlock(&pers_lock);
3916 		pr_warn("md: personality %s not loaded\n", clevel);
3917 		rv = -EINVAL;
3918 		goto out_unlock;
3919 	}
3920 	spin_unlock(&pers_lock);
3921 
3922 	if (pers == mddev->pers) {
3923 		/* Nothing to do! */
3924 		module_put(pers->owner);
3925 		rv = len;
3926 		goto out_unlock;
3927 	}
3928 	if (!pers->takeover) {
3929 		module_put(pers->owner);
3930 		pr_warn("md: %s: %s does not support personality takeover\n",
3931 			mdname(mddev), clevel);
3932 		rv = -EINVAL;
3933 		goto out_unlock;
3934 	}
3935 
3936 	rdev_for_each(rdev, mddev)
3937 		rdev->new_raid_disk = rdev->raid_disk;
3938 
3939 	/* ->takeover must set new_* and/or delta_disks
3940 	 * if it succeeds, and may set them when it fails.
3941 	 */
3942 	priv = pers->takeover(mddev);
3943 	if (IS_ERR(priv)) {
3944 		mddev->new_level = mddev->level;
3945 		mddev->new_layout = mddev->layout;
3946 		mddev->new_chunk_sectors = mddev->chunk_sectors;
3947 		mddev->raid_disks -= mddev->delta_disks;
3948 		mddev->delta_disks = 0;
3949 		mddev->reshape_backwards = 0;
3950 		module_put(pers->owner);
3951 		pr_warn("md: %s: %s would not accept array\n",
3952 			mdname(mddev), clevel);
3953 		rv = PTR_ERR(priv);
3954 		goto out_unlock;
3955 	}
3956 
3957 	/* Looks like we have a winner */
3958 	mddev_detach(mddev);
3959 
3960 	spin_lock(&mddev->lock);
3961 	oldpers = mddev->pers;
3962 	oldpriv = mddev->private;
3963 	mddev->pers = pers;
3964 	mddev->private = priv;
3965 	strscpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3966 	mddev->level = mddev->new_level;
3967 	mddev->layout = mddev->new_layout;
3968 	mddev->chunk_sectors = mddev->new_chunk_sectors;
3969 	mddev->delta_disks = 0;
3970 	mddev->reshape_backwards = 0;
3971 	mddev->degraded = 0;
3972 	spin_unlock(&mddev->lock);
3973 
3974 	if (oldpers->sync_request == NULL &&
3975 	    mddev->external) {
3976 		/* We are converting from a no-redundancy array
3977 		 * to a redundancy array and metadata is managed
3978 		 * externally so we need to be sure that writes
3979 		 * won't block due to a need to transition
3980 		 *      clean->dirty
3981 		 * until external management is started.
3982 		 */
3983 		mddev->in_sync = 0;
3984 		mddev->safemode_delay = 0;
3985 		mddev->safemode = 0;
3986 	}
3987 
3988 	oldpers->free(mddev, oldpriv);
3989 
3990 	if (oldpers->sync_request == NULL &&
3991 	    pers->sync_request != NULL) {
3992 		/* need to add the md_redundancy_group */
3993 		if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3994 			pr_warn("md: cannot register extra attributes for %s\n",
3995 				mdname(mddev));
3996 		mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3997 		mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
3998 		mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
3999 	}
4000 	if (oldpers->sync_request != NULL &&
4001 	    pers->sync_request == NULL) {
4002 		/* need to remove the md_redundancy_group */
4003 		if (mddev->to_remove == NULL)
4004 			mddev->to_remove = &md_redundancy_group;
4005 	}
4006 
4007 	module_put(oldpers->owner);
4008 
4009 	rdev_for_each(rdev, mddev) {
4010 		if (rdev->raid_disk < 0)
4011 			continue;
4012 		if (rdev->new_raid_disk >= mddev->raid_disks)
4013 			rdev->new_raid_disk = -1;
4014 		if (rdev->new_raid_disk == rdev->raid_disk)
4015 			continue;
4016 		sysfs_unlink_rdev(mddev, rdev);
4017 	}
4018 	rdev_for_each(rdev, mddev) {
4019 		if (rdev->raid_disk < 0)
4020 			continue;
4021 		if (rdev->new_raid_disk == rdev->raid_disk)
4022 			continue;
4023 		rdev->raid_disk = rdev->new_raid_disk;
4024 		if (rdev->raid_disk < 0)
4025 			clear_bit(In_sync, &rdev->flags);
4026 		else {
4027 			if (sysfs_link_rdev(mddev, rdev))
4028 				pr_warn("md: cannot register rd%d for %s after level change\n",
4029 					rdev->raid_disk, mdname(mddev));
4030 		}
4031 	}
4032 
4033 	if (pers->sync_request == NULL) {
4034 		/* this is now an array without redundancy, so
4035 		 * it must always be in_sync
4036 		 */
4037 		mddev->in_sync = 1;
4038 		del_timer_sync(&mddev->safemode_timer);
4039 	}
4040 	pers->run(mddev);
4041 	set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4042 	if (!mddev->thread)
4043 		md_update_sb(mddev, 1);
4044 	sysfs_notify_dirent_safe(mddev->sysfs_level);
4045 	md_new_event();
4046 	rv = len;
4047 out_unlock:
4048 	mddev_unlock_and_resume(mddev);
4049 	return rv;
4050 }
4051 
4052 static struct md_sysfs_entry md_level =
4053 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
4054 
4055 static ssize_t
4056 new_level_show(struct mddev *mddev, char *page)
4057 {
4058 	return sprintf(page, "%d\n", mddev->new_level);
4059 }
4060 
4061 static ssize_t
4062 new_level_store(struct mddev *mddev, const char *buf, size_t len)
4063 {
4064 	unsigned int n;
4065 	int err;
4066 
4067 	err = kstrtouint(buf, 10, &n);
4068 	if (err < 0)
4069 		return err;
4070 	err = mddev_lock(mddev);
4071 	if (err)
4072 		return err;
4073 
4074 	mddev->new_level = n;
4075 	md_update_sb(mddev, 1);
4076 
4077 	mddev_unlock(mddev);
4078 	return len;
4079 }
4080 static struct md_sysfs_entry md_new_level =
4081 __ATTR(new_level, 0664, new_level_show, new_level_store);
4082 
4083 static ssize_t
4084 layout_show(struct mddev *mddev, char *page)
4085 {
4086 	/* just a number, not meaningful for all levels */
4087 	if (mddev->reshape_position != MaxSector &&
4088 	    mddev->layout != mddev->new_layout)
4089 		return sprintf(page, "%d (%d)\n",
4090 			       mddev->new_layout, mddev->layout);
4091 	return sprintf(page, "%d\n", mddev->layout);
4092 }
4093 
4094 static ssize_t
4095 layout_store(struct mddev *mddev, const char *buf, size_t len)
4096 {
4097 	unsigned int n;
4098 	int err;
4099 
4100 	err = kstrtouint(buf, 10, &n);
4101 	if (err < 0)
4102 		return err;
4103 	err = mddev_lock(mddev);
4104 	if (err)
4105 		return err;
4106 
4107 	if (mddev->pers) {
4108 		if (mddev->pers->check_reshape == NULL)
4109 			err = -EBUSY;
4110 		else if (!md_is_rdwr(mddev))
4111 			err = -EROFS;
4112 		else {
4113 			mddev->new_layout = n;
4114 			err = mddev->pers->check_reshape(mddev);
4115 			if (err)
4116 				mddev->new_layout = mddev->layout;
4117 		}
4118 	} else {
4119 		mddev->new_layout = n;
4120 		if (mddev->reshape_position == MaxSector)
4121 			mddev->layout = n;
4122 	}
4123 	mddev_unlock(mddev);
4124 	return err ?: len;
4125 }
4126 static struct md_sysfs_entry md_layout =
4127 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
4128 
4129 static ssize_t
4130 raid_disks_show(struct mddev *mddev, char *page)
4131 {
4132 	if (mddev->raid_disks == 0)
4133 		return 0;
4134 	if (mddev->reshape_position != MaxSector &&
4135 	    mddev->delta_disks != 0)
4136 		return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4137 			       mddev->raid_disks - mddev->delta_disks);
4138 	return sprintf(page, "%d\n", mddev->raid_disks);
4139 }
4140 
4141 static int update_raid_disks(struct mddev *mddev, int raid_disks);
4142 
4143 static ssize_t
4144 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
4145 {
4146 	unsigned int n;
4147 	int err;
4148 
4149 	err = kstrtouint(buf, 10, &n);
4150 	if (err < 0)
4151 		return err;
4152 
4153 	err = mddev_lock(mddev);
4154 	if (err)
4155 		return err;
4156 	if (mddev->pers)
4157 		err = update_raid_disks(mddev, n);
4158 	else if (mddev->reshape_position != MaxSector) {
4159 		struct md_rdev *rdev;
4160 		int olddisks = mddev->raid_disks - mddev->delta_disks;
4161 
4162 		err = -EINVAL;
4163 		rdev_for_each(rdev, mddev) {
4164 			if (olddisks < n &&
4165 			    rdev->data_offset < rdev->new_data_offset)
4166 				goto out_unlock;
4167 			if (olddisks > n &&
4168 			    rdev->data_offset > rdev->new_data_offset)
4169 				goto out_unlock;
4170 		}
4171 		err = 0;
4172 		mddev->delta_disks = n - olddisks;
4173 		mddev->raid_disks = n;
4174 		mddev->reshape_backwards = (mddev->delta_disks < 0);
4175 	} else
4176 		mddev->raid_disks = n;
4177 out_unlock:
4178 	mddev_unlock(mddev);
4179 	return err ? err : len;
4180 }
4181 static struct md_sysfs_entry md_raid_disks =
4182 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
4183 
4184 static ssize_t
4185 uuid_show(struct mddev *mddev, char *page)
4186 {
4187 	return sprintf(page, "%pU\n", mddev->uuid);
4188 }
4189 static struct md_sysfs_entry md_uuid =
4190 __ATTR(uuid, S_IRUGO, uuid_show, NULL);
4191 
4192 static ssize_t
4193 chunk_size_show(struct mddev *mddev, char *page)
4194 {
4195 	if (mddev->reshape_position != MaxSector &&
4196 	    mddev->chunk_sectors != mddev->new_chunk_sectors)
4197 		return sprintf(page, "%d (%d)\n",
4198 			       mddev->new_chunk_sectors << 9,
4199 			       mddev->chunk_sectors << 9);
4200 	return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
4201 }
4202 
4203 static ssize_t
4204 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
4205 {
4206 	unsigned long n;
4207 	int err;
4208 
4209 	err = kstrtoul(buf, 10, &n);
4210 	if (err < 0)
4211 		return err;
4212 
4213 	err = mddev_lock(mddev);
4214 	if (err)
4215 		return err;
4216 	if (mddev->pers) {
4217 		if (mddev->pers->check_reshape == NULL)
4218 			err = -EBUSY;
4219 		else if (!md_is_rdwr(mddev))
4220 			err = -EROFS;
4221 		else {
4222 			mddev->new_chunk_sectors = n >> 9;
4223 			err = mddev->pers->check_reshape(mddev);
4224 			if (err)
4225 				mddev->new_chunk_sectors = mddev->chunk_sectors;
4226 		}
4227 	} else {
4228 		mddev->new_chunk_sectors = n >> 9;
4229 		if (mddev->reshape_position == MaxSector)
4230 			mddev->chunk_sectors = n >> 9;
4231 	}
4232 	mddev_unlock(mddev);
4233 	return err ?: len;
4234 }
4235 static struct md_sysfs_entry md_chunk_size =
4236 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
4237 
4238 static ssize_t
4239 resync_start_show(struct mddev *mddev, char *page)
4240 {
4241 	if (mddev->recovery_cp == MaxSector)
4242 		return sprintf(page, "none\n");
4243 	return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4244 }
4245 
4246 static ssize_t
4247 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
4248 {
4249 	unsigned long long n;
4250 	int err;
4251 
4252 	if (cmd_match(buf, "none"))
4253 		n = MaxSector;
4254 	else {
4255 		err = kstrtoull(buf, 10, &n);
4256 		if (err < 0)
4257 			return err;
4258 		if (n != (sector_t)n)
4259 			return -EINVAL;
4260 	}
4261 
4262 	err = mddev_lock(mddev);
4263 	if (err)
4264 		return err;
4265 	if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4266 		err = -EBUSY;
4267 
4268 	if (!err) {
4269 		mddev->recovery_cp = n;
4270 		if (mddev->pers)
4271 			set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
4272 	}
4273 	mddev_unlock(mddev);
4274 	return err ?: len;
4275 }
4276 static struct md_sysfs_entry md_resync_start =
4277 __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4278 		resync_start_show, resync_start_store);
4279 
4280 /*
4281  * The array state can be:
4282  *
4283  * clear
4284  *     No devices, no size, no level
4285  *     Equivalent to STOP_ARRAY ioctl
4286  * inactive
4287  *     May have some settings, but array is not active
4288  *        all IO results in error
4289  *     When written, doesn't tear down array, but just stops it
4290  * suspended (not supported yet)
4291  *     All IO requests will block. The array can be reconfigured.
4292  *     Writing this, if accepted, will block until array is quiescent
4293  * readonly
4294  *     no resync can happen.  no superblocks get written.
4295  *     write requests fail
4296  * read-auto
4297  *     like readonly, but behaves like 'clean' on a write request.
4298  *
4299  * clean - no pending writes, but otherwise active.
4300  *     When written to inactive array, starts without resync
4301  *     If a write request arrives then
4302  *       if metadata is known, mark 'dirty' and switch to 'active'.
4303  *       if not known, block and switch to write-pending
4304  *     If written to an active array that has pending writes, then fails.
4305  * active
4306  *     fully active: IO and resync can be happening.
4307  *     When written to inactive array, starts with resync
4308  *
4309  * write-pending
4310  *     clean, but writes are blocked waiting for 'active' to be written.
4311  *
4312  * active-idle
4313  *     like active, but no writes have been seen for a while (100msec).
4314  *
4315  * broken
4316 *     Array is failed. It's useful because mounted-arrays aren't stopped
4317 *     when array is failed, so this state will at least alert the user that
4318 *     something is wrong.
4319  */
4320 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4321 		   write_pending, active_idle, broken, bad_word};
4322 static char *array_states[] = {
4323 	"clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4324 	"write-pending", "active-idle", "broken", NULL };
4325 
4326 static int match_word(const char *word, char **list)
4327 {
4328 	int n;
4329 	for (n=0; list[n]; n++)
4330 		if (cmd_match(word, list[n]))
4331 			break;
4332 	return n;
4333 }
4334 
4335 static ssize_t
4336 array_state_show(struct mddev *mddev, char *page)
4337 {
4338 	enum array_state st = inactive;
4339 
4340 	if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
4341 		switch(mddev->ro) {
4342 		case MD_RDONLY:
4343 			st = readonly;
4344 			break;
4345 		case MD_AUTO_READ:
4346 			st = read_auto;
4347 			break;
4348 		case MD_RDWR:
4349 			spin_lock(&mddev->lock);
4350 			if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
4351 				st = write_pending;
4352 			else if (mddev->in_sync)
4353 				st = clean;
4354 			else if (mddev->safemode)
4355 				st = active_idle;
4356 			else
4357 				st = active;
4358 			spin_unlock(&mddev->lock);
4359 		}
4360 
4361 		if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4362 			st = broken;
4363 	} else {
4364 		if (list_empty(&mddev->disks) &&
4365 		    mddev->raid_disks == 0 &&
4366 		    mddev->dev_sectors == 0)
4367 			st = clear;
4368 		else
4369 			st = inactive;
4370 	}
4371 	return sprintf(page, "%s\n", array_states[st]);
4372 }
4373 
4374 static int do_md_stop(struct mddev *mddev, int ro);
4375 static int md_set_readonly(struct mddev *mddev);
4376 static int restart_array(struct mddev *mddev);
4377 
4378 static ssize_t
4379 array_state_store(struct mddev *mddev, const char *buf, size_t len)
4380 {
4381 	int err = 0;
4382 	enum array_state st = match_word(buf, array_states);
4383 
4384 	/* No lock dependent actions */
4385 	switch (st) {
4386 	case suspended:		/* not supported yet */
4387 	case write_pending:	/* cannot be set */
4388 	case active_idle:	/* cannot be set */
4389 	case broken:		/* cannot be set */
4390 	case bad_word:
4391 		return -EINVAL;
4392 	case clear:
4393 	case readonly:
4394 	case inactive:
4395 	case read_auto:
4396 		if (!mddev->pers || !md_is_rdwr(mddev))
4397 			break;
4398 		/* write sysfs will not open mddev and opener should be 0 */
4399 		err = mddev_set_closing_and_sync_blockdev(mddev, 0);
4400 		if (err)
4401 			return err;
4402 		break;
4403 	default:
4404 		break;
4405 	}
4406 
4407 	if (mddev->pers && (st == active || st == clean) &&
4408 	    mddev->ro != MD_RDONLY) {
4409 		/* don't take reconfig_mutex when toggling between
4410 		 * clean and active
4411 		 */
4412 		spin_lock(&mddev->lock);
4413 		if (st == active) {
4414 			restart_array(mddev);
4415 			clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4416 			md_wakeup_thread(mddev->thread);
4417 			wake_up(&mddev->sb_wait);
4418 		} else /* st == clean */ {
4419 			restart_array(mddev);
4420 			if (!set_in_sync(mddev))
4421 				err = -EBUSY;
4422 		}
4423 		if (!err)
4424 			sysfs_notify_dirent_safe(mddev->sysfs_state);
4425 		spin_unlock(&mddev->lock);
4426 		return err ?: len;
4427 	}
4428 	err = mddev_lock(mddev);
4429 	if (err)
4430 		return err;
4431 
4432 	switch (st) {
4433 	case inactive:
4434 		/* stop an active array, return 0 otherwise */
4435 		if (mddev->pers)
4436 			err = do_md_stop(mddev, 2);
4437 		break;
4438 	case clear:
4439 		err = do_md_stop(mddev, 0);
4440 		break;
4441 	case readonly:
4442 		if (mddev->pers)
4443 			err = md_set_readonly(mddev);
4444 		else {
4445 			mddev->ro = MD_RDONLY;
4446 			set_disk_ro(mddev->gendisk, 1);
4447 			err = do_md_run(mddev);
4448 		}
4449 		break;
4450 	case read_auto:
4451 		if (mddev->pers) {
4452 			if (md_is_rdwr(mddev))
4453 				err = md_set_readonly(mddev);
4454 			else if (mddev->ro == MD_RDONLY)
4455 				err = restart_array(mddev);
4456 			if (err == 0) {
4457 				mddev->ro = MD_AUTO_READ;
4458 				set_disk_ro(mddev->gendisk, 0);
4459 			}
4460 		} else {
4461 			mddev->ro = MD_AUTO_READ;
4462 			err = do_md_run(mddev);
4463 		}
4464 		break;
4465 	case clean:
4466 		if (mddev->pers) {
4467 			err = restart_array(mddev);
4468 			if (err)
4469 				break;
4470 			spin_lock(&mddev->lock);
4471 			if (!set_in_sync(mddev))
4472 				err = -EBUSY;
4473 			spin_unlock(&mddev->lock);
4474 		} else
4475 			err = -EINVAL;
4476 		break;
4477 	case active:
4478 		if (mddev->pers) {
4479 			err = restart_array(mddev);
4480 			if (err)
4481 				break;
4482 			clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
4483 			wake_up(&mddev->sb_wait);
4484 			err = 0;
4485 		} else {
4486 			mddev->ro = MD_RDWR;
4487 			set_disk_ro(mddev->gendisk, 0);
4488 			err = do_md_run(mddev);
4489 		}
4490 		break;
4491 	default:
4492 		err = -EINVAL;
4493 		break;
4494 	}
4495 
4496 	if (!err) {
4497 		if (mddev->hold_active == UNTIL_IOCTL)
4498 			mddev->hold_active = 0;
4499 		sysfs_notify_dirent_safe(mddev->sysfs_state);
4500 	}
4501 	mddev_unlock(mddev);
4502 
4503 	if (st == readonly || st == read_auto || st == inactive ||
4504 	    (err && st == clear))
4505 		clear_bit(MD_CLOSING, &mddev->flags);
4506 
4507 	return err ?: len;
4508 }
4509 static struct md_sysfs_entry md_array_state =
4510 __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
4511 
4512 static ssize_t
4513 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
4514 	return sprintf(page, "%d\n",
4515 		       atomic_read(&mddev->max_corr_read_errors));
4516 }
4517 
4518 static ssize_t
4519 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
4520 {
4521 	unsigned int n;
4522 	int rv;
4523 
4524 	rv = kstrtouint(buf, 10, &n);
4525 	if (rv < 0)
4526 		return rv;
4527 	if (n > INT_MAX)
4528 		return -EINVAL;
4529 	atomic_set(&mddev->max_corr_read_errors, n);
4530 	return len;
4531 }
4532 
4533 static struct md_sysfs_entry max_corr_read_errors =
4534 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4535 	max_corrected_read_errors_store);
4536 
4537 static ssize_t
4538 null_show(struct mddev *mddev, char *page)
4539 {
4540 	return -EINVAL;
4541 }
4542 
4543 static ssize_t
4544 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4545 {
4546 	/* buf must be %d:%d\n? giving major and minor numbers */
4547 	/* The new device is added to the array.
4548 	 * If the array has a persistent superblock, we read the
4549 	 * superblock to initialise info and check validity.
4550 	 * Otherwise, only checking done is that in bind_rdev_to_array,
4551 	 * which mainly checks size.
4552 	 */
4553 	char *e;
4554 	int major = simple_strtoul(buf, &e, 10);
4555 	int minor;
4556 	dev_t dev;
4557 	struct md_rdev *rdev;
4558 	int err;
4559 
4560 	if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4561 		return -EINVAL;
4562 	minor = simple_strtoul(e+1, &e, 10);
4563 	if (*e && *e != '\n')
4564 		return -EINVAL;
4565 	dev = MKDEV(major, minor);
4566 	if (major != MAJOR(dev) ||
4567 	    minor != MINOR(dev))
4568 		return -EOVERFLOW;
4569 
4570 	err = mddev_suspend_and_lock(mddev);
4571 	if (err)
4572 		return err;
4573 	if (mddev->persistent) {
4574 		rdev = md_import_device(dev, mddev->major_version,
4575 					mddev->minor_version);
4576 		if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4577 			struct md_rdev *rdev0
4578 				= list_entry(mddev->disks.next,
4579 					     struct md_rdev, same_set);
4580 			err = super_types[mddev->major_version]
4581 				.load_super(rdev, rdev0, mddev->minor_version);
4582 			if (err < 0)
4583 				goto out;
4584 		}
4585 	} else if (mddev->external)
4586 		rdev = md_import_device(dev, -2, -1);
4587 	else
4588 		rdev = md_import_device(dev, -1, -1);
4589 
4590 	if (IS_ERR(rdev)) {
4591 		mddev_unlock_and_resume(mddev);
4592 		return PTR_ERR(rdev);
4593 	}
4594 	err = bind_rdev_to_array(rdev, mddev);
4595  out:
4596 	if (err)
4597 		export_rdev(rdev, mddev);
4598 	mddev_unlock_and_resume(mddev);
4599 	if (!err)
4600 		md_new_event();
4601 	return err ? err : len;
4602 }
4603 
4604 static struct md_sysfs_entry md_new_device =
4605 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4606 
4607 static ssize_t
4608 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4609 {
4610 	char *end;
4611 	unsigned long chunk, end_chunk;
4612 	int err;
4613 
4614 	err = mddev_lock(mddev);
4615 	if (err)
4616 		return err;
4617 	if (!mddev->bitmap)
4618 		goto out;
4619 	/* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4620 	while (*buf) {
4621 		chunk = end_chunk = simple_strtoul(buf, &end, 0);
4622 		if (buf == end)
4623 			break;
4624 
4625 		if (*end == '-') { /* range */
4626 			buf = end + 1;
4627 			end_chunk = simple_strtoul(buf, &end, 0);
4628 			if (buf == end)
4629 				break;
4630 		}
4631 
4632 		if (*end && !isspace(*end))
4633 			break;
4634 
4635 		mddev->bitmap_ops->dirty_bits(mddev, chunk, end_chunk);
4636 		buf = skip_spaces(end);
4637 	}
4638 	mddev->bitmap_ops->unplug(mddev, true); /* flush the bits to disk */
4639 out:
4640 	mddev_unlock(mddev);
4641 	return len;
4642 }
4643 
4644 static struct md_sysfs_entry md_bitmap =
4645 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4646 
4647 static ssize_t
4648 size_show(struct mddev *mddev, char *page)
4649 {
4650 	return sprintf(page, "%llu\n",
4651 		(unsigned long long)mddev->dev_sectors / 2);
4652 }
4653 
4654 static int update_size(struct mddev *mddev, sector_t num_sectors);
4655 
4656 static ssize_t
4657 size_store(struct mddev *mddev, const char *buf, size_t len)
4658 {
4659 	/* If array is inactive, we can reduce the component size, but
4660 	 * not increase it (except from 0).
4661 	 * If array is active, we can try an on-line resize
4662 	 */
4663 	sector_t sectors;
4664 	int err = strict_blocks_to_sectors(buf, &sectors);
4665 
4666 	if (err < 0)
4667 		return err;
4668 	err = mddev_lock(mddev);
4669 	if (err)
4670 		return err;
4671 	if (mddev->pers) {
4672 		err = update_size(mddev, sectors);
4673 		if (err == 0)
4674 			md_update_sb(mddev, 1);
4675 	} else {
4676 		if (mddev->dev_sectors == 0 ||
4677 		    mddev->dev_sectors > sectors)
4678 			mddev->dev_sectors = sectors;
4679 		else
4680 			err = -ENOSPC;
4681 	}
4682 	mddev_unlock(mddev);
4683 	return err ? err : len;
4684 }
4685 
4686 static struct md_sysfs_entry md_size =
4687 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4688 
4689 /* Metadata version.
4690  * This is one of
4691  *   'none' for arrays with no metadata (good luck...)
4692  *   'external' for arrays with externally managed metadata,
4693  * or N.M for internally known formats
4694  */
4695 static ssize_t
4696 metadata_show(struct mddev *mddev, char *page)
4697 {
4698 	if (mddev->persistent)
4699 		return sprintf(page, "%d.%d\n",
4700 			       mddev->major_version, mddev->minor_version);
4701 	else if (mddev->external)
4702 		return sprintf(page, "external:%s\n", mddev->metadata_type);
4703 	else
4704 		return sprintf(page, "none\n");
4705 }
4706 
4707 static ssize_t
4708 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4709 {
4710 	int major, minor;
4711 	char *e;
4712 	int err;
4713 	/* Changing the details of 'external' metadata is
4714 	 * always permitted.  Otherwise there must be
4715 	 * no devices attached to the array.
4716 	 */
4717 
4718 	err = mddev_lock(mddev);
4719 	if (err)
4720 		return err;
4721 	err = -EBUSY;
4722 	if (mddev->external && strncmp(buf, "external:", 9) == 0)
4723 		;
4724 	else if (!list_empty(&mddev->disks))
4725 		goto out_unlock;
4726 
4727 	err = 0;
4728 	if (cmd_match(buf, "none")) {
4729 		mddev->persistent = 0;
4730 		mddev->external = 0;
4731 		mddev->major_version = 0;
4732 		mddev->minor_version = 90;
4733 		goto out_unlock;
4734 	}
4735 	if (strncmp(buf, "external:", 9) == 0) {
4736 		size_t namelen = len-9;
4737 		if (namelen >= sizeof(mddev->metadata_type))
4738 			namelen = sizeof(mddev->metadata_type)-1;
4739 		memcpy(mddev->metadata_type, buf+9, namelen);
4740 		mddev->metadata_type[namelen] = 0;
4741 		if (namelen && mddev->metadata_type[namelen-1] == '\n')
4742 			mddev->metadata_type[--namelen] = 0;
4743 		mddev->persistent = 0;
4744 		mddev->external = 1;
4745 		mddev->major_version = 0;
4746 		mddev->minor_version = 90;
4747 		goto out_unlock;
4748 	}
4749 	major = simple_strtoul(buf, &e, 10);
4750 	err = -EINVAL;
4751 	if (e==buf || *e != '.')
4752 		goto out_unlock;
4753 	buf = e+1;
4754 	minor = simple_strtoul(buf, &e, 10);
4755 	if (e==buf || (*e && *e != '\n') )
4756 		goto out_unlock;
4757 	err = -ENOENT;
4758 	if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4759 		goto out_unlock;
4760 	mddev->major_version = major;
4761 	mddev->minor_version = minor;
4762 	mddev->persistent = 1;
4763 	mddev->external = 0;
4764 	err = 0;
4765 out_unlock:
4766 	mddev_unlock(mddev);
4767 	return err ?: len;
4768 }
4769 
4770 static struct md_sysfs_entry md_metadata =
4771 __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4772 
4773 enum sync_action md_sync_action(struct mddev *mddev)
4774 {
4775 	unsigned long recovery = mddev->recovery;
4776 
4777 	/*
4778 	 * frozen has the highest priority, means running sync_thread will be
4779 	 * stopped immediately, and no new sync_thread can start.
4780 	 */
4781 	if (test_bit(MD_RECOVERY_FROZEN, &recovery))
4782 		return ACTION_FROZEN;
4783 
4784 	/*
4785 	 * read-only array can't register sync_thread, and it can only
4786 	 * add/remove spares.
4787 	 */
4788 	if (!md_is_rdwr(mddev))
4789 		return ACTION_IDLE;
4790 
4791 	/*
4792 	 * idle means no sync_thread is running, and no new sync_thread is
4793 	 * requested.
4794 	 */
4795 	if (!test_bit(MD_RECOVERY_RUNNING, &recovery) &&
4796 	    !test_bit(MD_RECOVERY_NEEDED, &recovery))
4797 		return ACTION_IDLE;
4798 
4799 	if (test_bit(MD_RECOVERY_RESHAPE, &recovery) ||
4800 	    mddev->reshape_position != MaxSector)
4801 		return ACTION_RESHAPE;
4802 
4803 	if (test_bit(MD_RECOVERY_RECOVER, &recovery))
4804 		return ACTION_RECOVER;
4805 
4806 	if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4807 		/*
4808 		 * MD_RECOVERY_CHECK must be paired with
4809 		 * MD_RECOVERY_REQUESTED.
4810 		 */
4811 		if (test_bit(MD_RECOVERY_CHECK, &recovery))
4812 			return ACTION_CHECK;
4813 		if (test_bit(MD_RECOVERY_REQUESTED, &recovery))
4814 			return ACTION_REPAIR;
4815 		return ACTION_RESYNC;
4816 	}
4817 
4818 	/*
4819 	 * MD_RECOVERY_NEEDED or MD_RECOVERY_RUNNING is set, however, no
4820 	 * sync_action is specified.
4821 	 */
4822 	return ACTION_IDLE;
4823 }
4824 
4825 enum sync_action md_sync_action_by_name(const char *page)
4826 {
4827 	enum sync_action action;
4828 
4829 	for (action = 0; action < NR_SYNC_ACTIONS; ++action) {
4830 		if (cmd_match(page, action_name[action]))
4831 			return action;
4832 	}
4833 
4834 	return NR_SYNC_ACTIONS;
4835 }
4836 
4837 const char *md_sync_action_name(enum sync_action action)
4838 {
4839 	return action_name[action];
4840 }
4841 
4842 static ssize_t
4843 action_show(struct mddev *mddev, char *page)
4844 {
4845 	enum sync_action action = md_sync_action(mddev);
4846 
4847 	return sprintf(page, "%s\n", md_sync_action_name(action));
4848 }
4849 
4850 /**
4851  * stop_sync_thread() - wait for sync_thread to stop if it's running.
4852  * @mddev:	the array.
4853  * @locked:	if set, reconfig_mutex will still be held after this function
4854  *		return; if not set, reconfig_mutex will be released after this
4855  *		function return.
4856  */
4857 static void stop_sync_thread(struct mddev *mddev, bool locked)
4858 {
4859 	int sync_seq = atomic_read(&mddev->sync_seq);
4860 
4861 	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
4862 		if (!locked)
4863 			mddev_unlock(mddev);
4864 		return;
4865 	}
4866 
4867 	mddev_unlock(mddev);
4868 
4869 	set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4870 	/*
4871 	 * Thread might be blocked waiting for metadata update which will now
4872 	 * never happen
4873 	 */
4874 	md_wakeup_thread_directly(mddev->sync_thread);
4875 	if (work_pending(&mddev->sync_work))
4876 		flush_work(&mddev->sync_work);
4877 
4878 	wait_event(resync_wait,
4879 		   !test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4880 		   (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery) &&
4881 		    sync_seq != atomic_read(&mddev->sync_seq)));
4882 
4883 	if (locked)
4884 		mddev_lock_nointr(mddev);
4885 }
4886 
4887 void md_idle_sync_thread(struct mddev *mddev)
4888 {
4889 	lockdep_assert_held(&mddev->reconfig_mutex);
4890 
4891 	clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4892 	stop_sync_thread(mddev, true);
4893 }
4894 EXPORT_SYMBOL_GPL(md_idle_sync_thread);
4895 
4896 void md_frozen_sync_thread(struct mddev *mddev)
4897 {
4898 	lockdep_assert_held(&mddev->reconfig_mutex);
4899 
4900 	set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4901 	stop_sync_thread(mddev, true);
4902 }
4903 EXPORT_SYMBOL_GPL(md_frozen_sync_thread);
4904 
4905 void md_unfrozen_sync_thread(struct mddev *mddev)
4906 {
4907 	lockdep_assert_held(&mddev->reconfig_mutex);
4908 
4909 	clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4910 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4911 	md_wakeup_thread(mddev->thread);
4912 	sysfs_notify_dirent_safe(mddev->sysfs_action);
4913 }
4914 EXPORT_SYMBOL_GPL(md_unfrozen_sync_thread);
4915 
4916 static int mddev_start_reshape(struct mddev *mddev)
4917 {
4918 	int ret;
4919 
4920 	if (mddev->pers->start_reshape == NULL)
4921 		return -EINVAL;
4922 
4923 	if (mddev->reshape_position == MaxSector ||
4924 	    mddev->pers->check_reshape == NULL ||
4925 	    mddev->pers->check_reshape(mddev)) {
4926 		clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4927 		ret = mddev->pers->start_reshape(mddev);
4928 		if (ret)
4929 			return ret;
4930 	} else {
4931 		/*
4932 		 * If reshape is still in progress, and md_check_recovery() can
4933 		 * continue to reshape, don't restart reshape because data can
4934 		 * be corrupted for raid456.
4935 		 */
4936 		clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4937 	}
4938 
4939 	sysfs_notify_dirent_safe(mddev->sysfs_degraded);
4940 	return 0;
4941 }
4942 
4943 static ssize_t
4944 action_store(struct mddev *mddev, const char *page, size_t len)
4945 {
4946 	int ret;
4947 	enum sync_action action;
4948 
4949 	if (!mddev->pers || !mddev->pers->sync_request)
4950 		return -EINVAL;
4951 
4952 retry:
4953 	if (work_busy(&mddev->sync_work))
4954 		flush_work(&mddev->sync_work);
4955 
4956 	ret = mddev_lock(mddev);
4957 	if (ret)
4958 		return ret;
4959 
4960 	if (work_busy(&mddev->sync_work)) {
4961 		mddev_unlock(mddev);
4962 		goto retry;
4963 	}
4964 
4965 	action = md_sync_action_by_name(page);
4966 
4967 	/* TODO: mdadm rely on "idle" to start sync_thread. */
4968 	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
4969 		switch (action) {
4970 		case ACTION_FROZEN:
4971 			md_frozen_sync_thread(mddev);
4972 			ret = len;
4973 			goto out;
4974 		case ACTION_IDLE:
4975 			md_idle_sync_thread(mddev);
4976 			break;
4977 		case ACTION_RESHAPE:
4978 		case ACTION_RECOVER:
4979 		case ACTION_CHECK:
4980 		case ACTION_REPAIR:
4981 		case ACTION_RESYNC:
4982 			ret = -EBUSY;
4983 			goto out;
4984 		default:
4985 			ret = -EINVAL;
4986 			goto out;
4987 		}
4988 	} else {
4989 		switch (action) {
4990 		case ACTION_FROZEN:
4991 			set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4992 			ret = len;
4993 			goto out;
4994 		case ACTION_RESHAPE:
4995 			clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4996 			ret = mddev_start_reshape(mddev);
4997 			if (ret)
4998 				goto out;
4999 			break;
5000 		case ACTION_RECOVER:
5001 			clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5002 			set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5003 			break;
5004 		case ACTION_CHECK:
5005 			set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5006 			fallthrough;
5007 		case ACTION_REPAIR:
5008 			set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
5009 			set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5010 			fallthrough;
5011 		case ACTION_RESYNC:
5012 		case ACTION_IDLE:
5013 			clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5014 			break;
5015 		default:
5016 			ret = -EINVAL;
5017 			goto out;
5018 		}
5019 	}
5020 
5021 	if (mddev->ro == MD_AUTO_READ) {
5022 		/* A write to sync_action is enough to justify
5023 		 * canceling read-auto mode
5024 		 */
5025 		mddev->ro = MD_RDWR;
5026 		md_wakeup_thread(mddev->sync_thread);
5027 	}
5028 
5029 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5030 	md_wakeup_thread(mddev->thread);
5031 	sysfs_notify_dirent_safe(mddev->sysfs_action);
5032 	ret = len;
5033 
5034 out:
5035 	mddev_unlock(mddev);
5036 	return ret;
5037 }
5038 
5039 static struct md_sysfs_entry md_scan_mode =
5040 __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
5041 
5042 static ssize_t
5043 last_sync_action_show(struct mddev *mddev, char *page)
5044 {
5045 	return sprintf(page, "%s\n",
5046 		       md_sync_action_name(mddev->last_sync_action));
5047 }
5048 
5049 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
5050 
5051 static ssize_t
5052 mismatch_cnt_show(struct mddev *mddev, char *page)
5053 {
5054 	return sprintf(page, "%llu\n",
5055 		       (unsigned long long)
5056 		       atomic64_read(&mddev->resync_mismatches));
5057 }
5058 
5059 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
5060 
5061 static ssize_t
5062 sync_min_show(struct mddev *mddev, char *page)
5063 {
5064 	return sprintf(page, "%d (%s)\n", speed_min(mddev),
5065 		       mddev->sync_speed_min ? "local": "system");
5066 }
5067 
5068 static ssize_t
5069 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
5070 {
5071 	unsigned int min;
5072 	int rv;
5073 
5074 	if (strncmp(buf, "system", 6)==0) {
5075 		min = 0;
5076 	} else {
5077 		rv = kstrtouint(buf, 10, &min);
5078 		if (rv < 0)
5079 			return rv;
5080 		if (min == 0)
5081 			return -EINVAL;
5082 	}
5083 	mddev->sync_speed_min = min;
5084 	return len;
5085 }
5086 
5087 static struct md_sysfs_entry md_sync_min =
5088 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
5089 
5090 static ssize_t
5091 sync_max_show(struct mddev *mddev, char *page)
5092 {
5093 	return sprintf(page, "%d (%s)\n", speed_max(mddev),
5094 		       mddev->sync_speed_max ? "local": "system");
5095 }
5096 
5097 static ssize_t
5098 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
5099 {
5100 	unsigned int max;
5101 	int rv;
5102 
5103 	if (strncmp(buf, "system", 6)==0) {
5104 		max = 0;
5105 	} else {
5106 		rv = kstrtouint(buf, 10, &max);
5107 		if (rv < 0)
5108 			return rv;
5109 		if (max == 0)
5110 			return -EINVAL;
5111 	}
5112 	mddev->sync_speed_max = max;
5113 	return len;
5114 }
5115 
5116 static struct md_sysfs_entry md_sync_max =
5117 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
5118 
5119 static ssize_t
5120 degraded_show(struct mddev *mddev, char *page)
5121 {
5122 	return sprintf(page, "%d\n", mddev->degraded);
5123 }
5124 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
5125 
5126 static ssize_t
5127 sync_force_parallel_show(struct mddev *mddev, char *page)
5128 {
5129 	return sprintf(page, "%d\n", mddev->parallel_resync);
5130 }
5131 
5132 static ssize_t
5133 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
5134 {
5135 	long n;
5136 
5137 	if (kstrtol(buf, 10, &n))
5138 		return -EINVAL;
5139 
5140 	if (n != 0 && n != 1)
5141 		return -EINVAL;
5142 
5143 	mddev->parallel_resync = n;
5144 
5145 	if (mddev->sync_thread)
5146 		wake_up(&resync_wait);
5147 
5148 	return len;
5149 }
5150 
5151 /* force parallel resync, even with shared block devices */
5152 static struct md_sysfs_entry md_sync_force_parallel =
5153 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
5154        sync_force_parallel_show, sync_force_parallel_store);
5155 
5156 static ssize_t
5157 sync_speed_show(struct mddev *mddev, char *page)
5158 {
5159 	unsigned long resync, dt, db;
5160 	if (mddev->curr_resync == MD_RESYNC_NONE)
5161 		return sprintf(page, "none\n");
5162 	resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
5163 	dt = (jiffies - mddev->resync_mark) / HZ;
5164 	if (!dt) dt++;
5165 	db = resync - mddev->resync_mark_cnt;
5166 	return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
5167 }
5168 
5169 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
5170 
5171 static ssize_t
5172 sync_completed_show(struct mddev *mddev, char *page)
5173 {
5174 	unsigned long long max_sectors, resync;
5175 
5176 	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5177 		return sprintf(page, "none\n");
5178 
5179 	if (mddev->curr_resync == MD_RESYNC_YIELDED ||
5180 	    mddev->curr_resync == MD_RESYNC_DELAYED)
5181 		return sprintf(page, "delayed\n");
5182 
5183 	if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
5184 	    test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5185 		max_sectors = mddev->resync_max_sectors;
5186 	else
5187 		max_sectors = mddev->dev_sectors;
5188 
5189 	resync = mddev->curr_resync_completed;
5190 	return sprintf(page, "%llu / %llu\n", resync, max_sectors);
5191 }
5192 
5193 static struct md_sysfs_entry md_sync_completed =
5194 	__ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
5195 
5196 static ssize_t
5197 min_sync_show(struct mddev *mddev, char *page)
5198 {
5199 	return sprintf(page, "%llu\n",
5200 		       (unsigned long long)mddev->resync_min);
5201 }
5202 static ssize_t
5203 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
5204 {
5205 	unsigned long long min;
5206 	int err;
5207 
5208 	if (kstrtoull(buf, 10, &min))
5209 		return -EINVAL;
5210 
5211 	spin_lock(&mddev->lock);
5212 	err = -EINVAL;
5213 	if (min > mddev->resync_max)
5214 		goto out_unlock;
5215 
5216 	err = -EBUSY;
5217 	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5218 		goto out_unlock;
5219 
5220 	/* Round down to multiple of 4K for safety */
5221 	mddev->resync_min = round_down(min, 8);
5222 	err = 0;
5223 
5224 out_unlock:
5225 	spin_unlock(&mddev->lock);
5226 	return err ?: len;
5227 }
5228 
5229 static struct md_sysfs_entry md_min_sync =
5230 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5231 
5232 static ssize_t
5233 max_sync_show(struct mddev *mddev, char *page)
5234 {
5235 	if (mddev->resync_max == MaxSector)
5236 		return sprintf(page, "max\n");
5237 	else
5238 		return sprintf(page, "%llu\n",
5239 			       (unsigned long long)mddev->resync_max);
5240 }
5241 static ssize_t
5242 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
5243 {
5244 	int err;
5245 	spin_lock(&mddev->lock);
5246 	if (strncmp(buf, "max", 3) == 0)
5247 		mddev->resync_max = MaxSector;
5248 	else {
5249 		unsigned long long max;
5250 		int chunk;
5251 
5252 		err = -EINVAL;
5253 		if (kstrtoull(buf, 10, &max))
5254 			goto out_unlock;
5255 		if (max < mddev->resync_min)
5256 			goto out_unlock;
5257 
5258 		err = -EBUSY;
5259 		if (max < mddev->resync_max && md_is_rdwr(mddev) &&
5260 		    test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5261 			goto out_unlock;
5262 
5263 		/* Must be a multiple of chunk_size */
5264 		chunk = mddev->chunk_sectors;
5265 		if (chunk) {
5266 			sector_t temp = max;
5267 
5268 			err = -EINVAL;
5269 			if (sector_div(temp, chunk))
5270 				goto out_unlock;
5271 		}
5272 		mddev->resync_max = max;
5273 	}
5274 	wake_up(&mddev->recovery_wait);
5275 	err = 0;
5276 out_unlock:
5277 	spin_unlock(&mddev->lock);
5278 	return err ?: len;
5279 }
5280 
5281 static struct md_sysfs_entry md_max_sync =
5282 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5283 
5284 static ssize_t
5285 suspend_lo_show(struct mddev *mddev, char *page)
5286 {
5287 	return sprintf(page, "%llu\n",
5288 		       (unsigned long long)READ_ONCE(mddev->suspend_lo));
5289 }
5290 
5291 static ssize_t
5292 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
5293 {
5294 	unsigned long long new;
5295 	int err;
5296 
5297 	err = kstrtoull(buf, 10, &new);
5298 	if (err < 0)
5299 		return err;
5300 	if (new != (sector_t)new)
5301 		return -EINVAL;
5302 
5303 	err = mddev_suspend(mddev, true);
5304 	if (err)
5305 		return err;
5306 
5307 	WRITE_ONCE(mddev->suspend_lo, new);
5308 	mddev_resume(mddev);
5309 
5310 	return len;
5311 }
5312 static struct md_sysfs_entry md_suspend_lo =
5313 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5314 
5315 static ssize_t
5316 suspend_hi_show(struct mddev *mddev, char *page)
5317 {
5318 	return sprintf(page, "%llu\n",
5319 		       (unsigned long long)READ_ONCE(mddev->suspend_hi));
5320 }
5321 
5322 static ssize_t
5323 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
5324 {
5325 	unsigned long long new;
5326 	int err;
5327 
5328 	err = kstrtoull(buf, 10, &new);
5329 	if (err < 0)
5330 		return err;
5331 	if (new != (sector_t)new)
5332 		return -EINVAL;
5333 
5334 	err = mddev_suspend(mddev, true);
5335 	if (err)
5336 		return err;
5337 
5338 	WRITE_ONCE(mddev->suspend_hi, new);
5339 	mddev_resume(mddev);
5340 
5341 	return len;
5342 }
5343 static struct md_sysfs_entry md_suspend_hi =
5344 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5345 
5346 static ssize_t
5347 reshape_position_show(struct mddev *mddev, char *page)
5348 {
5349 	if (mddev->reshape_position != MaxSector)
5350 		return sprintf(page, "%llu\n",
5351 			       (unsigned long long)mddev->reshape_position);
5352 	strcpy(page, "none\n");
5353 	return 5;
5354 }
5355 
5356 static ssize_t
5357 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
5358 {
5359 	struct md_rdev *rdev;
5360 	unsigned long long new;
5361 	int err;
5362 
5363 	err = kstrtoull(buf, 10, &new);
5364 	if (err < 0)
5365 		return err;
5366 	if (new != (sector_t)new)
5367 		return -EINVAL;
5368 	err = mddev_lock(mddev);
5369 	if (err)
5370 		return err;
5371 	err = -EBUSY;
5372 	if (mddev->pers)
5373 		goto unlock;
5374 	mddev->reshape_position = new;
5375 	mddev->delta_disks = 0;
5376 	mddev->reshape_backwards = 0;
5377 	mddev->new_level = mddev->level;
5378 	mddev->new_layout = mddev->layout;
5379 	mddev->new_chunk_sectors = mddev->chunk_sectors;
5380 	rdev_for_each(rdev, mddev)
5381 		rdev->new_data_offset = rdev->data_offset;
5382 	err = 0;
5383 unlock:
5384 	mddev_unlock(mddev);
5385 	return err ?: len;
5386 }
5387 
5388 static struct md_sysfs_entry md_reshape_position =
5389 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5390        reshape_position_store);
5391 
5392 static ssize_t
5393 reshape_direction_show(struct mddev *mddev, char *page)
5394 {
5395 	return sprintf(page, "%s\n",
5396 		       mddev->reshape_backwards ? "backwards" : "forwards");
5397 }
5398 
5399 static ssize_t
5400 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5401 {
5402 	int backwards = 0;
5403 	int err;
5404 
5405 	if (cmd_match(buf, "forwards"))
5406 		backwards = 0;
5407 	else if (cmd_match(buf, "backwards"))
5408 		backwards = 1;
5409 	else
5410 		return -EINVAL;
5411 	if (mddev->reshape_backwards == backwards)
5412 		return len;
5413 
5414 	err = mddev_lock(mddev);
5415 	if (err)
5416 		return err;
5417 	/* check if we are allowed to change */
5418 	if (mddev->delta_disks)
5419 		err = -EBUSY;
5420 	else if (mddev->persistent &&
5421 	    mddev->major_version == 0)
5422 		err =  -EINVAL;
5423 	else
5424 		mddev->reshape_backwards = backwards;
5425 	mddev_unlock(mddev);
5426 	return err ?: len;
5427 }
5428 
5429 static struct md_sysfs_entry md_reshape_direction =
5430 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5431        reshape_direction_store);
5432 
5433 static ssize_t
5434 array_size_show(struct mddev *mddev, char *page)
5435 {
5436 	if (mddev->external_size)
5437 		return sprintf(page, "%llu\n",
5438 			       (unsigned long long)mddev->array_sectors/2);
5439 	else
5440 		return sprintf(page, "default\n");
5441 }
5442 
5443 static ssize_t
5444 array_size_store(struct mddev *mddev, const char *buf, size_t len)
5445 {
5446 	sector_t sectors;
5447 	int err;
5448 
5449 	err = mddev_lock(mddev);
5450 	if (err)
5451 		return err;
5452 
5453 	/* cluster raid doesn't support change array_sectors */
5454 	if (mddev_is_clustered(mddev)) {
5455 		mddev_unlock(mddev);
5456 		return -EINVAL;
5457 	}
5458 
5459 	if (strncmp(buf, "default", 7) == 0) {
5460 		if (mddev->pers)
5461 			sectors = mddev->pers->size(mddev, 0, 0);
5462 		else
5463 			sectors = mddev->array_sectors;
5464 
5465 		mddev->external_size = 0;
5466 	} else {
5467 		if (strict_blocks_to_sectors(buf, &sectors) < 0)
5468 			err = -EINVAL;
5469 		else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5470 			err = -E2BIG;
5471 		else
5472 			mddev->external_size = 1;
5473 	}
5474 
5475 	if (!err) {
5476 		mddev->array_sectors = sectors;
5477 		if (mddev->pers)
5478 			set_capacity_and_notify(mddev->gendisk,
5479 						mddev->array_sectors);
5480 	}
5481 	mddev_unlock(mddev);
5482 	return err ?: len;
5483 }
5484 
5485 static struct md_sysfs_entry md_array_size =
5486 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5487        array_size_store);
5488 
5489 static ssize_t
5490 consistency_policy_show(struct mddev *mddev, char *page)
5491 {
5492 	int ret;
5493 
5494 	if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5495 		ret = sprintf(page, "journal\n");
5496 	} else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5497 		ret = sprintf(page, "ppl\n");
5498 	} else if (mddev->bitmap) {
5499 		ret = sprintf(page, "bitmap\n");
5500 	} else if (mddev->pers) {
5501 		if (mddev->pers->sync_request)
5502 			ret = sprintf(page, "resync\n");
5503 		else
5504 			ret = sprintf(page, "none\n");
5505 	} else {
5506 		ret = sprintf(page, "unknown\n");
5507 	}
5508 
5509 	return ret;
5510 }
5511 
5512 static ssize_t
5513 consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5514 {
5515 	int err = 0;
5516 
5517 	if (mddev->pers) {
5518 		if (mddev->pers->change_consistency_policy)
5519 			err = mddev->pers->change_consistency_policy(mddev, buf);
5520 		else
5521 			err = -EBUSY;
5522 	} else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5523 		set_bit(MD_HAS_PPL, &mddev->flags);
5524 	} else {
5525 		err = -EINVAL;
5526 	}
5527 
5528 	return err ? err : len;
5529 }
5530 
5531 static struct md_sysfs_entry md_consistency_policy =
5532 __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5533        consistency_policy_store);
5534 
5535 static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5536 {
5537 	return sprintf(page, "%d\n", mddev->fail_last_dev);
5538 }
5539 
5540 /*
5541  * Setting fail_last_dev to true to allow last device to be forcibly removed
5542  * from RAID1/RAID10.
5543  */
5544 static ssize_t
5545 fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5546 {
5547 	int ret;
5548 	bool value;
5549 
5550 	ret = kstrtobool(buf, &value);
5551 	if (ret)
5552 		return ret;
5553 
5554 	if (value != mddev->fail_last_dev)
5555 		mddev->fail_last_dev = value;
5556 
5557 	return len;
5558 }
5559 static struct md_sysfs_entry md_fail_last_dev =
5560 __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5561        fail_last_dev_store);
5562 
5563 static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5564 {
5565 	if (mddev->pers == NULL || (mddev->pers->level != 1))
5566 		return sprintf(page, "n/a\n");
5567 	else
5568 		return sprintf(page, "%d\n", mddev->serialize_policy);
5569 }
5570 
5571 /*
5572  * Setting serialize_policy to true to enforce write IO is not reordered
5573  * for raid1.
5574  */
5575 static ssize_t
5576 serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5577 {
5578 	int err;
5579 	bool value;
5580 
5581 	err = kstrtobool(buf, &value);
5582 	if (err)
5583 		return err;
5584 
5585 	if (value == mddev->serialize_policy)
5586 		return len;
5587 
5588 	err = mddev_suspend_and_lock(mddev);
5589 	if (err)
5590 		return err;
5591 	if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5592 		pr_err("md: serialize_policy is only effective for raid1\n");
5593 		err = -EINVAL;
5594 		goto unlock;
5595 	}
5596 
5597 	if (value)
5598 		mddev_create_serial_pool(mddev, NULL);
5599 	else
5600 		mddev_destroy_serial_pool(mddev, NULL);
5601 	mddev->serialize_policy = value;
5602 unlock:
5603 	mddev_unlock_and_resume(mddev);
5604 	return err ?: len;
5605 }
5606 
5607 static struct md_sysfs_entry md_serialize_policy =
5608 __ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5609        serialize_policy_store);
5610 
5611 
5612 static struct attribute *md_default_attrs[] = {
5613 	&md_level.attr,
5614 	&md_new_level.attr,
5615 	&md_layout.attr,
5616 	&md_raid_disks.attr,
5617 	&md_uuid.attr,
5618 	&md_chunk_size.attr,
5619 	&md_size.attr,
5620 	&md_resync_start.attr,
5621 	&md_metadata.attr,
5622 	&md_new_device.attr,
5623 	&md_safe_delay.attr,
5624 	&md_array_state.attr,
5625 	&md_reshape_position.attr,
5626 	&md_reshape_direction.attr,
5627 	&md_array_size.attr,
5628 	&max_corr_read_errors.attr,
5629 	&md_consistency_policy.attr,
5630 	&md_fail_last_dev.attr,
5631 	&md_serialize_policy.attr,
5632 	NULL,
5633 };
5634 
5635 static const struct attribute_group md_default_group = {
5636 	.attrs = md_default_attrs,
5637 };
5638 
5639 static struct attribute *md_redundancy_attrs[] = {
5640 	&md_scan_mode.attr,
5641 	&md_last_scan_mode.attr,
5642 	&md_mismatches.attr,
5643 	&md_sync_min.attr,
5644 	&md_sync_max.attr,
5645 	&md_sync_speed.attr,
5646 	&md_sync_force_parallel.attr,
5647 	&md_sync_completed.attr,
5648 	&md_min_sync.attr,
5649 	&md_max_sync.attr,
5650 	&md_suspend_lo.attr,
5651 	&md_suspend_hi.attr,
5652 	&md_bitmap.attr,
5653 	&md_degraded.attr,
5654 	NULL,
5655 };
5656 static const struct attribute_group md_redundancy_group = {
5657 	.name = NULL,
5658 	.attrs = md_redundancy_attrs,
5659 };
5660 
5661 static const struct attribute_group *md_attr_groups[] = {
5662 	&md_default_group,
5663 	&md_bitmap_group,
5664 	NULL,
5665 };
5666 
5667 static ssize_t
5668 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5669 {
5670 	struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5671 	struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5672 	ssize_t rv;
5673 
5674 	if (!entry->show)
5675 		return -EIO;
5676 	spin_lock(&all_mddevs_lock);
5677 	if (!mddev_get(mddev)) {
5678 		spin_unlock(&all_mddevs_lock);
5679 		return -EBUSY;
5680 	}
5681 	spin_unlock(&all_mddevs_lock);
5682 
5683 	rv = entry->show(mddev, page);
5684 	mddev_put(mddev);
5685 	return rv;
5686 }
5687 
5688 static ssize_t
5689 md_attr_store(struct kobject *kobj, struct attribute *attr,
5690 	      const char *page, size_t length)
5691 {
5692 	struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
5693 	struct mddev *mddev = container_of(kobj, struct mddev, kobj);
5694 	ssize_t rv;
5695 
5696 	if (!entry->store)
5697 		return -EIO;
5698 	if (!capable(CAP_SYS_ADMIN))
5699 		return -EACCES;
5700 	spin_lock(&all_mddevs_lock);
5701 	if (!mddev_get(mddev)) {
5702 		spin_unlock(&all_mddevs_lock);
5703 		return -EBUSY;
5704 	}
5705 	spin_unlock(&all_mddevs_lock);
5706 	rv = entry->store(mddev, page, length);
5707 	mddev_put(mddev);
5708 	return rv;
5709 }
5710 
5711 static void md_kobj_release(struct kobject *ko)
5712 {
5713 	struct mddev *mddev = container_of(ko, struct mddev, kobj);
5714 
5715 	if (mddev->sysfs_state)
5716 		sysfs_put(mddev->sysfs_state);
5717 	if (mddev->sysfs_level)
5718 		sysfs_put(mddev->sysfs_level);
5719 
5720 	del_gendisk(mddev->gendisk);
5721 	put_disk(mddev->gendisk);
5722 }
5723 
5724 static const struct sysfs_ops md_sysfs_ops = {
5725 	.show	= md_attr_show,
5726 	.store	= md_attr_store,
5727 };
5728 static const struct kobj_type md_ktype = {
5729 	.release	= md_kobj_release,
5730 	.sysfs_ops	= &md_sysfs_ops,
5731 	.default_groups	= md_attr_groups,
5732 };
5733 
5734 int mdp_major = 0;
5735 
5736 /* stack the limit for all rdevs into lim */
5737 int mddev_stack_rdev_limits(struct mddev *mddev, struct queue_limits *lim,
5738 		unsigned int flags)
5739 {
5740 	struct md_rdev *rdev;
5741 
5742 	rdev_for_each(rdev, mddev) {
5743 		queue_limits_stack_bdev(lim, rdev->bdev, rdev->data_offset,
5744 					mddev->gendisk->disk_name);
5745 		if ((flags & MDDEV_STACK_INTEGRITY) &&
5746 		    !queue_limits_stack_integrity_bdev(lim, rdev->bdev))
5747 			return -EINVAL;
5748 	}
5749 
5750 	return 0;
5751 }
5752 EXPORT_SYMBOL_GPL(mddev_stack_rdev_limits);
5753 
5754 /* apply the extra stacking limits from a new rdev into mddev */
5755 int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev)
5756 {
5757 	struct queue_limits lim;
5758 
5759 	if (mddev_is_dm(mddev))
5760 		return 0;
5761 
5762 	lim = queue_limits_start_update(mddev->gendisk->queue);
5763 	queue_limits_stack_bdev(&lim, rdev->bdev, rdev->data_offset,
5764 				mddev->gendisk->disk_name);
5765 
5766 	if (!queue_limits_stack_integrity_bdev(&lim, rdev->bdev)) {
5767 		pr_err("%s: incompatible integrity profile for %pg\n",
5768 		       mdname(mddev), rdev->bdev);
5769 		queue_limits_cancel_update(mddev->gendisk->queue);
5770 		return -ENXIO;
5771 	}
5772 
5773 	return queue_limits_commit_update(mddev->gendisk->queue, &lim);
5774 }
5775 EXPORT_SYMBOL_GPL(mddev_stack_new_rdev);
5776 
5777 /* update the optimal I/O size after a reshape */
5778 void mddev_update_io_opt(struct mddev *mddev, unsigned int nr_stripes)
5779 {
5780 	struct queue_limits lim;
5781 
5782 	if (mddev_is_dm(mddev))
5783 		return;
5784 
5785 	/* don't bother updating io_opt if we can't suspend the array */
5786 	if (mddev_suspend(mddev, false) < 0)
5787 		return;
5788 	lim = queue_limits_start_update(mddev->gendisk->queue);
5789 	lim.io_opt = lim.io_min * nr_stripes;
5790 	queue_limits_commit_update(mddev->gendisk->queue, &lim);
5791 	mddev_resume(mddev);
5792 }
5793 EXPORT_SYMBOL_GPL(mddev_update_io_opt);
5794 
5795 static void mddev_delayed_delete(struct work_struct *ws)
5796 {
5797 	struct mddev *mddev = container_of(ws, struct mddev, del_work);
5798 
5799 	kobject_put(&mddev->kobj);
5800 }
5801 
5802 void md_init_stacking_limits(struct queue_limits *lim)
5803 {
5804 	blk_set_stacking_limits(lim);
5805 	lim->features = BLK_FEAT_WRITE_CACHE | BLK_FEAT_FUA |
5806 			BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT;
5807 }
5808 EXPORT_SYMBOL_GPL(md_init_stacking_limits);
5809 
5810 struct mddev *md_alloc(dev_t dev, char *name)
5811 {
5812 	/*
5813 	 * If dev is zero, name is the name of a device to allocate with
5814 	 * an arbitrary minor number.  It will be "md_???"
5815 	 * If dev is non-zero it must be a device number with a MAJOR of
5816 	 * MD_MAJOR or mdp_major.  In this case, if "name" is NULL, then
5817 	 * the device is being created by opening a node in /dev.
5818 	 * If "name" is not NULL, the device is being created by
5819 	 * writing to /sys/module/md_mod/parameters/new_array.
5820 	 */
5821 	static DEFINE_MUTEX(disks_mutex);
5822 	struct mddev *mddev;
5823 	struct gendisk *disk;
5824 	int partitioned;
5825 	int shift;
5826 	int unit;
5827 	int error;
5828 
5829 	/*
5830 	 * Wait for any previous instance of this device to be completely
5831 	 * removed (mddev_delayed_delete).
5832 	 */
5833 	flush_workqueue(md_misc_wq);
5834 
5835 	mutex_lock(&disks_mutex);
5836 	mddev = mddev_alloc(dev);
5837 	if (IS_ERR(mddev)) {
5838 		error = PTR_ERR(mddev);
5839 		goto out_unlock;
5840 	}
5841 
5842 	partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5843 	shift = partitioned ? MdpMinorShift : 0;
5844 	unit = MINOR(mddev->unit) >> shift;
5845 
5846 	if (name && !dev) {
5847 		/* Need to ensure that 'name' is not a duplicate.
5848 		 */
5849 		struct mddev *mddev2;
5850 		spin_lock(&all_mddevs_lock);
5851 
5852 		list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5853 			if (mddev2->gendisk &&
5854 			    strcmp(mddev2->gendisk->disk_name, name) == 0) {
5855 				spin_unlock(&all_mddevs_lock);
5856 				error = -EEXIST;
5857 				goto out_free_mddev;
5858 			}
5859 		spin_unlock(&all_mddevs_lock);
5860 	}
5861 	if (name && dev)
5862 		/*
5863 		 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5864 		 */
5865 		mddev->hold_active = UNTIL_STOP;
5866 
5867 	disk = blk_alloc_disk(NULL, NUMA_NO_NODE);
5868 	if (IS_ERR(disk)) {
5869 		error = PTR_ERR(disk);
5870 		goto out_free_mddev;
5871 	}
5872 
5873 	disk->major = MAJOR(mddev->unit);
5874 	disk->first_minor = unit << shift;
5875 	disk->minors = 1 << shift;
5876 	if (name)
5877 		strcpy(disk->disk_name, name);
5878 	else if (partitioned)
5879 		sprintf(disk->disk_name, "md_d%d", unit);
5880 	else
5881 		sprintf(disk->disk_name, "md%d", unit);
5882 	disk->fops = &md_fops;
5883 	disk->private_data = mddev;
5884 
5885 	disk->events |= DISK_EVENT_MEDIA_CHANGE;
5886 	mddev->gendisk = disk;
5887 	error = add_disk(disk);
5888 	if (error)
5889 		goto out_put_disk;
5890 
5891 	kobject_init(&mddev->kobj, &md_ktype);
5892 	error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
5893 	if (error) {
5894 		/*
5895 		 * The disk is already live at this point.  Clear the hold flag
5896 		 * and let mddev_put take care of the deletion, as it isn't any
5897 		 * different from a normal close on last release now.
5898 		 */
5899 		mddev->hold_active = 0;
5900 		mutex_unlock(&disks_mutex);
5901 		mddev_put(mddev);
5902 		return ERR_PTR(error);
5903 	}
5904 
5905 	kobject_uevent(&mddev->kobj, KOBJ_ADD);
5906 	mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
5907 	mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level");
5908 	mutex_unlock(&disks_mutex);
5909 	return mddev;
5910 
5911 out_put_disk:
5912 	put_disk(disk);
5913 out_free_mddev:
5914 	mddev_free(mddev);
5915 out_unlock:
5916 	mutex_unlock(&disks_mutex);
5917 	return ERR_PTR(error);
5918 }
5919 
5920 static int md_alloc_and_put(dev_t dev, char *name)
5921 {
5922 	struct mddev *mddev = md_alloc(dev, name);
5923 
5924 	if (IS_ERR(mddev))
5925 		return PTR_ERR(mddev);
5926 	mddev_put(mddev);
5927 	return 0;
5928 }
5929 
5930 static void md_probe(dev_t dev)
5931 {
5932 	if (MAJOR(dev) == MD_MAJOR && MINOR(dev) >= 512)
5933 		return;
5934 	if (create_on_open)
5935 		md_alloc_and_put(dev, NULL);
5936 }
5937 
5938 static int add_named_array(const char *val, const struct kernel_param *kp)
5939 {
5940 	/*
5941 	 * val must be "md_*" or "mdNNN".
5942 	 * For "md_*" we allocate an array with a large free minor number, and
5943 	 * set the name to val.  val must not already be an active name.
5944 	 * For "mdNNN" we allocate an array with the minor number NNN
5945 	 * which must not already be in use.
5946 	 */
5947 	int len = strlen(val);
5948 	char buf[DISK_NAME_LEN];
5949 	unsigned long devnum;
5950 
5951 	while (len && val[len-1] == '\n')
5952 		len--;
5953 	if (len >= DISK_NAME_LEN)
5954 		return -E2BIG;
5955 	strscpy(buf, val, len+1);
5956 	if (strncmp(buf, "md_", 3) == 0)
5957 		return md_alloc_and_put(0, buf);
5958 	if (strncmp(buf, "md", 2) == 0 &&
5959 	    isdigit(buf[2]) &&
5960 	    kstrtoul(buf+2, 10, &devnum) == 0 &&
5961 	    devnum <= MINORMASK)
5962 		return md_alloc_and_put(MKDEV(MD_MAJOR, devnum), NULL);
5963 
5964 	return -EINVAL;
5965 }
5966 
5967 static void md_safemode_timeout(struct timer_list *t)
5968 {
5969 	struct mddev *mddev = from_timer(mddev, t, safemode_timer);
5970 
5971 	mddev->safemode = 1;
5972 	if (mddev->external)
5973 		sysfs_notify_dirent_safe(mddev->sysfs_state);
5974 
5975 	md_wakeup_thread(mddev->thread);
5976 }
5977 
5978 static int start_dirty_degraded;
5979 
5980 int md_run(struct mddev *mddev)
5981 {
5982 	int err;
5983 	struct md_rdev *rdev;
5984 	struct md_personality *pers;
5985 	bool nowait = true;
5986 
5987 	if (list_empty(&mddev->disks))
5988 		/* cannot run an array with no devices.. */
5989 		return -EINVAL;
5990 
5991 	if (mddev->pers)
5992 		return -EBUSY;
5993 	/* Cannot run until previous stop completes properly */
5994 	if (mddev->sysfs_active)
5995 		return -EBUSY;
5996 
5997 	/*
5998 	 * Analyze all RAID superblock(s)
5999 	 */
6000 	if (!mddev->raid_disks) {
6001 		if (!mddev->persistent)
6002 			return -EINVAL;
6003 		err = analyze_sbs(mddev);
6004 		if (err)
6005 			return -EINVAL;
6006 	}
6007 
6008 	if (mddev->level != LEVEL_NONE)
6009 		request_module("md-level-%d", mddev->level);
6010 	else if (mddev->clevel[0])
6011 		request_module("md-%s", mddev->clevel);
6012 
6013 	/*
6014 	 * Drop all container device buffers, from now on
6015 	 * the only valid external interface is through the md
6016 	 * device.
6017 	 */
6018 	mddev->has_superblocks = false;
6019 	rdev_for_each(rdev, mddev) {
6020 		if (test_bit(Faulty, &rdev->flags))
6021 			continue;
6022 		sync_blockdev(rdev->bdev);
6023 		invalidate_bdev(rdev->bdev);
6024 		if (mddev->ro != MD_RDONLY && rdev_read_only(rdev)) {
6025 			mddev->ro = MD_RDONLY;
6026 			if (!mddev_is_dm(mddev))
6027 				set_disk_ro(mddev->gendisk, 1);
6028 		}
6029 
6030 		if (rdev->sb_page)
6031 			mddev->has_superblocks = true;
6032 
6033 		/* perform some consistency tests on the device.
6034 		 * We don't want the data to overlap the metadata,
6035 		 * Internal Bitmap issues have been handled elsewhere.
6036 		 */
6037 		if (rdev->meta_bdev) {
6038 			/* Nothing to check */;
6039 		} else if (rdev->data_offset < rdev->sb_start) {
6040 			if (mddev->dev_sectors &&
6041 			    rdev->data_offset + mddev->dev_sectors
6042 			    > rdev->sb_start) {
6043 				pr_warn("md: %s: data overlaps metadata\n",
6044 					mdname(mddev));
6045 				return -EINVAL;
6046 			}
6047 		} else {
6048 			if (rdev->sb_start + rdev->sb_size/512
6049 			    > rdev->data_offset) {
6050 				pr_warn("md: %s: metadata overlaps data\n",
6051 					mdname(mddev));
6052 				return -EINVAL;
6053 			}
6054 		}
6055 		sysfs_notify_dirent_safe(rdev->sysfs_state);
6056 		nowait = nowait && bdev_nowait(rdev->bdev);
6057 	}
6058 
6059 	if (!bioset_initialized(&mddev->bio_set)) {
6060 		err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
6061 		if (err)
6062 			return err;
6063 	}
6064 	if (!bioset_initialized(&mddev->sync_set)) {
6065 		err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
6066 		if (err)
6067 			goto exit_bio_set;
6068 	}
6069 
6070 	if (!bioset_initialized(&mddev->io_clone_set)) {
6071 		err = bioset_init(&mddev->io_clone_set, BIO_POOL_SIZE,
6072 				  offsetof(struct md_io_clone, bio_clone), 0);
6073 		if (err)
6074 			goto exit_sync_set;
6075 	}
6076 
6077 	spin_lock(&pers_lock);
6078 	pers = find_pers(mddev->level, mddev->clevel);
6079 	if (!pers || !try_module_get(pers->owner)) {
6080 		spin_unlock(&pers_lock);
6081 		if (mddev->level != LEVEL_NONE)
6082 			pr_warn("md: personality for level %d is not loaded!\n",
6083 				mddev->level);
6084 		else
6085 			pr_warn("md: personality for level %s is not loaded!\n",
6086 				mddev->clevel);
6087 		err = -EINVAL;
6088 		goto abort;
6089 	}
6090 	spin_unlock(&pers_lock);
6091 	if (mddev->level != pers->level) {
6092 		mddev->level = pers->level;
6093 		mddev->new_level = pers->level;
6094 	}
6095 	strscpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
6096 
6097 	if (mddev->reshape_position != MaxSector &&
6098 	    pers->start_reshape == NULL) {
6099 		/* This personality cannot handle reshaping... */
6100 		module_put(pers->owner);
6101 		err = -EINVAL;
6102 		goto abort;
6103 	}
6104 
6105 	if (pers->sync_request) {
6106 		/* Warn if this is a potentially silly
6107 		 * configuration.
6108 		 */
6109 		struct md_rdev *rdev2;
6110 		int warned = 0;
6111 
6112 		rdev_for_each(rdev, mddev)
6113 			rdev_for_each(rdev2, mddev) {
6114 				if (rdev < rdev2 &&
6115 				    rdev->bdev->bd_disk ==
6116 				    rdev2->bdev->bd_disk) {
6117 					pr_warn("%s: WARNING: %pg appears to be on the same physical disk as %pg.\n",
6118 						mdname(mddev),
6119 						rdev->bdev,
6120 						rdev2->bdev);
6121 					warned = 1;
6122 				}
6123 			}
6124 
6125 		if (warned)
6126 			pr_warn("True protection against single-disk failure might be compromised.\n");
6127 	}
6128 
6129 	/* dm-raid expect sync_thread to be frozen until resume */
6130 	if (mddev->gendisk)
6131 		mddev->recovery = 0;
6132 
6133 	/* may be over-ridden by personality */
6134 	mddev->resync_max_sectors = mddev->dev_sectors;
6135 
6136 	mddev->ok_start_degraded = start_dirty_degraded;
6137 
6138 	if (start_readonly && md_is_rdwr(mddev))
6139 		mddev->ro = MD_AUTO_READ; /* read-only, but switch on first write */
6140 
6141 	err = pers->run(mddev);
6142 	if (err)
6143 		pr_warn("md: pers->run() failed ...\n");
6144 	else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
6145 		WARN_ONCE(!mddev->external_size,
6146 			  "%s: default size too small, but 'external_size' not in effect?\n",
6147 			  __func__);
6148 		pr_warn("md: invalid array_size %llu > default size %llu\n",
6149 			(unsigned long long)mddev->array_sectors / 2,
6150 			(unsigned long long)pers->size(mddev, 0, 0) / 2);
6151 		err = -EINVAL;
6152 	}
6153 	if (err == 0 && pers->sync_request &&
6154 	    (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
6155 		err = mddev->bitmap_ops->create(mddev, -1);
6156 		if (err)
6157 			pr_warn("%s: failed to create bitmap (%d)\n",
6158 				mdname(mddev), err);
6159 	}
6160 	if (err)
6161 		goto bitmap_abort;
6162 
6163 	if (mddev->bitmap_info.max_write_behind > 0) {
6164 		bool create_pool = false;
6165 
6166 		rdev_for_each(rdev, mddev) {
6167 			if (test_bit(WriteMostly, &rdev->flags) &&
6168 			    rdev_init_serial(rdev))
6169 				create_pool = true;
6170 		}
6171 		if (create_pool && mddev->serial_info_pool == NULL) {
6172 			mddev->serial_info_pool =
6173 				mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
6174 						    sizeof(struct serial_info));
6175 			if (!mddev->serial_info_pool) {
6176 				err = -ENOMEM;
6177 				goto bitmap_abort;
6178 			}
6179 		}
6180 	}
6181 
6182 	if (pers->sync_request) {
6183 		if (mddev->kobj.sd &&
6184 		    sysfs_create_group(&mddev->kobj, &md_redundancy_group))
6185 			pr_warn("md: cannot register extra attributes for %s\n",
6186 				mdname(mddev));
6187 		mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
6188 		mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
6189 		mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
6190 	} else if (mddev->ro == MD_AUTO_READ)
6191 		mddev->ro = MD_RDWR;
6192 
6193 	atomic_set(&mddev->max_corr_read_errors,
6194 		   MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
6195 	mddev->safemode = 0;
6196 	if (mddev_is_clustered(mddev))
6197 		mddev->safemode_delay = 0;
6198 	else
6199 		mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
6200 	mddev->in_sync = 1;
6201 	smp_wmb();
6202 	spin_lock(&mddev->lock);
6203 	mddev->pers = pers;
6204 	spin_unlock(&mddev->lock);
6205 	rdev_for_each(rdev, mddev)
6206 		if (rdev->raid_disk >= 0)
6207 			sysfs_link_rdev(mddev, rdev); /* failure here is OK */
6208 
6209 	if (mddev->degraded && md_is_rdwr(mddev))
6210 		/* This ensures that recovering status is reported immediately
6211 		 * via sysfs - until a lack of spares is confirmed.
6212 		 */
6213 		set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6214 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6215 
6216 	if (mddev->sb_flags)
6217 		md_update_sb(mddev, 0);
6218 
6219 	md_new_event();
6220 	return 0;
6221 
6222 bitmap_abort:
6223 	mddev_detach(mddev);
6224 	if (mddev->private)
6225 		pers->free(mddev, mddev->private);
6226 	mddev->private = NULL;
6227 	module_put(pers->owner);
6228 	mddev->bitmap_ops->destroy(mddev);
6229 abort:
6230 	bioset_exit(&mddev->io_clone_set);
6231 exit_sync_set:
6232 	bioset_exit(&mddev->sync_set);
6233 exit_bio_set:
6234 	bioset_exit(&mddev->bio_set);
6235 	return err;
6236 }
6237 EXPORT_SYMBOL_GPL(md_run);
6238 
6239 int do_md_run(struct mddev *mddev)
6240 {
6241 	int err;
6242 
6243 	set_bit(MD_NOT_READY, &mddev->flags);
6244 	err = md_run(mddev);
6245 	if (err)
6246 		goto out;
6247 
6248 	err = mddev->bitmap_ops->load(mddev);
6249 	if (err) {
6250 		mddev->bitmap_ops->destroy(mddev);
6251 		goto out;
6252 	}
6253 
6254 	if (mddev_is_clustered(mddev))
6255 		md_allow_write(mddev);
6256 
6257 	/* run start up tasks that require md_thread */
6258 	md_start(mddev);
6259 
6260 	md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6261 
6262 	set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
6263 	clear_bit(MD_NOT_READY, &mddev->flags);
6264 	mddev->changed = 1;
6265 	kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
6266 	sysfs_notify_dirent_safe(mddev->sysfs_state);
6267 	sysfs_notify_dirent_safe(mddev->sysfs_action);
6268 	sysfs_notify_dirent_safe(mddev->sysfs_degraded);
6269 out:
6270 	clear_bit(MD_NOT_READY, &mddev->flags);
6271 	return err;
6272 }
6273 
6274 int md_start(struct mddev *mddev)
6275 {
6276 	int ret = 0;
6277 
6278 	if (mddev->pers->start) {
6279 		set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6280 		ret = mddev->pers->start(mddev);
6281 		clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6282 		md_wakeup_thread(mddev->sync_thread);
6283 	}
6284 	return ret;
6285 }
6286 EXPORT_SYMBOL_GPL(md_start);
6287 
6288 static int restart_array(struct mddev *mddev)
6289 {
6290 	struct gendisk *disk = mddev->gendisk;
6291 	struct md_rdev *rdev;
6292 	bool has_journal = false;
6293 	bool has_readonly = false;
6294 
6295 	/* Complain if it has no devices */
6296 	if (list_empty(&mddev->disks))
6297 		return -ENXIO;
6298 	if (!mddev->pers)
6299 		return -EINVAL;
6300 	if (md_is_rdwr(mddev))
6301 		return -EBUSY;
6302 
6303 	rcu_read_lock();
6304 	rdev_for_each_rcu(rdev, mddev) {
6305 		if (test_bit(Journal, &rdev->flags) &&
6306 		    !test_bit(Faulty, &rdev->flags))
6307 			has_journal = true;
6308 		if (rdev_read_only(rdev))
6309 			has_readonly = true;
6310 	}
6311 	rcu_read_unlock();
6312 	if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6313 		/* Don't restart rw with journal missing/faulty */
6314 			return -EINVAL;
6315 	if (has_readonly)
6316 		return -EROFS;
6317 
6318 	mddev->safemode = 0;
6319 	mddev->ro = MD_RDWR;
6320 	set_disk_ro(disk, 0);
6321 	pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
6322 	/* Kick recovery or resync if necessary */
6323 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6324 	md_wakeup_thread(mddev->sync_thread);
6325 	sysfs_notify_dirent_safe(mddev->sysfs_state);
6326 	return 0;
6327 }
6328 
6329 static void md_clean(struct mddev *mddev)
6330 {
6331 	mddev->array_sectors = 0;
6332 	mddev->external_size = 0;
6333 	mddev->dev_sectors = 0;
6334 	mddev->raid_disks = 0;
6335 	mddev->recovery_cp = 0;
6336 	mddev->resync_min = 0;
6337 	mddev->resync_max = MaxSector;
6338 	mddev->reshape_position = MaxSector;
6339 	/* we still need mddev->external in export_rdev, do not clear it yet */
6340 	mddev->persistent = 0;
6341 	mddev->level = LEVEL_NONE;
6342 	mddev->clevel[0] = 0;
6343 	/*
6344 	 * Don't clear MD_CLOSING, or mddev can be opened again.
6345 	 * 'hold_active != 0' means mddev is still in the creation
6346 	 * process and will be used later.
6347 	 */
6348 	if (mddev->hold_active)
6349 		mddev->flags = 0;
6350 	else
6351 		mddev->flags &= BIT_ULL_MASK(MD_CLOSING);
6352 	mddev->sb_flags = 0;
6353 	mddev->ro = MD_RDWR;
6354 	mddev->metadata_type[0] = 0;
6355 	mddev->chunk_sectors = 0;
6356 	mddev->ctime = mddev->utime = 0;
6357 	mddev->layout = 0;
6358 	mddev->max_disks = 0;
6359 	mddev->events = 0;
6360 	mddev->can_decrease_events = 0;
6361 	mddev->delta_disks = 0;
6362 	mddev->reshape_backwards = 0;
6363 	mddev->new_level = LEVEL_NONE;
6364 	mddev->new_layout = 0;
6365 	mddev->new_chunk_sectors = 0;
6366 	mddev->curr_resync = MD_RESYNC_NONE;
6367 	atomic64_set(&mddev->resync_mismatches, 0);
6368 	mddev->suspend_lo = mddev->suspend_hi = 0;
6369 	mddev->sync_speed_min = mddev->sync_speed_max = 0;
6370 	mddev->recovery = 0;
6371 	mddev->in_sync = 0;
6372 	mddev->changed = 0;
6373 	mddev->degraded = 0;
6374 	mddev->safemode = 0;
6375 	mddev->private = NULL;
6376 	mddev->cluster_info = NULL;
6377 	mddev->bitmap_info.offset = 0;
6378 	mddev->bitmap_info.default_offset = 0;
6379 	mddev->bitmap_info.default_space = 0;
6380 	mddev->bitmap_info.chunksize = 0;
6381 	mddev->bitmap_info.daemon_sleep = 0;
6382 	mddev->bitmap_info.max_write_behind = 0;
6383 	mddev->bitmap_info.nodes = 0;
6384 }
6385 
6386 static void __md_stop_writes(struct mddev *mddev)
6387 {
6388 	del_timer_sync(&mddev->safemode_timer);
6389 
6390 	if (mddev->pers && mddev->pers->quiesce) {
6391 		mddev->pers->quiesce(mddev, 1);
6392 		mddev->pers->quiesce(mddev, 0);
6393 	}
6394 
6395 	mddev->bitmap_ops->flush(mddev);
6396 
6397 	if (md_is_rdwr(mddev) &&
6398 	    ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
6399 	     mddev->sb_flags)) {
6400 		/* mark array as shutdown cleanly */
6401 		if (!mddev_is_clustered(mddev))
6402 			mddev->in_sync = 1;
6403 		md_update_sb(mddev, 1);
6404 	}
6405 	/* disable policy to guarantee rdevs free resources for serialization */
6406 	mddev->serialize_policy = 0;
6407 	mddev_destroy_serial_pool(mddev, NULL);
6408 }
6409 
6410 void md_stop_writes(struct mddev *mddev)
6411 {
6412 	mddev_lock_nointr(mddev);
6413 	set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6414 	stop_sync_thread(mddev, true);
6415 	__md_stop_writes(mddev);
6416 	mddev_unlock(mddev);
6417 }
6418 EXPORT_SYMBOL_GPL(md_stop_writes);
6419 
6420 static void mddev_detach(struct mddev *mddev)
6421 {
6422 	mddev->bitmap_ops->wait_behind_writes(mddev);
6423 	if (mddev->pers && mddev->pers->quiesce && !is_md_suspended(mddev)) {
6424 		mddev->pers->quiesce(mddev, 1);
6425 		mddev->pers->quiesce(mddev, 0);
6426 	}
6427 	md_unregister_thread(mddev, &mddev->thread);
6428 
6429 	/* the unplug fn references 'conf' */
6430 	if (!mddev_is_dm(mddev))
6431 		blk_sync_queue(mddev->gendisk->queue);
6432 }
6433 
6434 static void __md_stop(struct mddev *mddev)
6435 {
6436 	struct md_personality *pers = mddev->pers;
6437 
6438 	mddev->bitmap_ops->destroy(mddev);
6439 	mddev_detach(mddev);
6440 	spin_lock(&mddev->lock);
6441 	mddev->pers = NULL;
6442 	spin_unlock(&mddev->lock);
6443 	if (mddev->private)
6444 		pers->free(mddev, mddev->private);
6445 	mddev->private = NULL;
6446 	if (pers->sync_request && mddev->to_remove == NULL)
6447 		mddev->to_remove = &md_redundancy_group;
6448 	module_put(pers->owner);
6449 	clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6450 
6451 	bioset_exit(&mddev->bio_set);
6452 	bioset_exit(&mddev->sync_set);
6453 	bioset_exit(&mddev->io_clone_set);
6454 }
6455 
6456 void md_stop(struct mddev *mddev)
6457 {
6458 	lockdep_assert_held(&mddev->reconfig_mutex);
6459 
6460 	/* stop the array and free an attached data structures.
6461 	 * This is called from dm-raid
6462 	 */
6463 	__md_stop_writes(mddev);
6464 	__md_stop(mddev);
6465 }
6466 
6467 EXPORT_SYMBOL_GPL(md_stop);
6468 
6469 /* ensure 'mddev->pers' exist before calling md_set_readonly() */
6470 static int md_set_readonly(struct mddev *mddev)
6471 {
6472 	int err = 0;
6473 	int did_freeze = 0;
6474 
6475 	if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
6476 		return -EBUSY;
6477 
6478 	if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6479 		did_freeze = 1;
6480 		set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6481 	}
6482 
6483 	stop_sync_thread(mddev, false);
6484 	wait_event(mddev->sb_wait,
6485 		   !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
6486 	mddev_lock_nointr(mddev);
6487 
6488 	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6489 		pr_warn("md: %s still in use.\n",mdname(mddev));
6490 		err = -EBUSY;
6491 		goto out;
6492 	}
6493 
6494 	__md_stop_writes(mddev);
6495 
6496 	if (mddev->ro == MD_RDONLY) {
6497 		err  = -ENXIO;
6498 		goto out;
6499 	}
6500 
6501 	mddev->ro = MD_RDONLY;
6502 	set_disk_ro(mddev->gendisk, 1);
6503 
6504 out:
6505 	if (!err || did_freeze) {
6506 		clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6507 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6508 		sysfs_notify_dirent_safe(mddev->sysfs_state);
6509 	}
6510 
6511 	return err;
6512 }
6513 
6514 /* mode:
6515  *   0 - completely stop and dis-assemble array
6516  *   2 - stop but do not disassemble array
6517  */
6518 static int do_md_stop(struct mddev *mddev, int mode)
6519 {
6520 	struct gendisk *disk = mddev->gendisk;
6521 	struct md_rdev *rdev;
6522 	int did_freeze = 0;
6523 
6524 	if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6525 		did_freeze = 1;
6526 		set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6527 	}
6528 
6529 	stop_sync_thread(mddev, true);
6530 
6531 	if (mddev->sysfs_active ||
6532 	    test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
6533 		pr_warn("md: %s still in use.\n",mdname(mddev));
6534 		if (did_freeze) {
6535 			clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6536 			set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6537 		}
6538 		return -EBUSY;
6539 	}
6540 	if (mddev->pers) {
6541 		if (!md_is_rdwr(mddev))
6542 			set_disk_ro(disk, 0);
6543 
6544 		__md_stop_writes(mddev);
6545 		__md_stop(mddev);
6546 
6547 		/* tell userspace to handle 'inactive' */
6548 		sysfs_notify_dirent_safe(mddev->sysfs_state);
6549 
6550 		rdev_for_each(rdev, mddev)
6551 			if (rdev->raid_disk >= 0)
6552 				sysfs_unlink_rdev(mddev, rdev);
6553 
6554 		set_capacity_and_notify(disk, 0);
6555 		mddev->changed = 1;
6556 
6557 		if (!md_is_rdwr(mddev))
6558 			mddev->ro = MD_RDWR;
6559 	}
6560 	/*
6561 	 * Free resources if final stop
6562 	 */
6563 	if (mode == 0) {
6564 		pr_info("md: %s stopped.\n", mdname(mddev));
6565 
6566 		if (mddev->bitmap_info.file) {
6567 			struct file *f = mddev->bitmap_info.file;
6568 			spin_lock(&mddev->lock);
6569 			mddev->bitmap_info.file = NULL;
6570 			spin_unlock(&mddev->lock);
6571 			fput(f);
6572 		}
6573 		mddev->bitmap_info.offset = 0;
6574 
6575 		export_array(mddev);
6576 
6577 		md_clean(mddev);
6578 		if (mddev->hold_active == UNTIL_STOP)
6579 			mddev->hold_active = 0;
6580 	}
6581 	md_new_event();
6582 	sysfs_notify_dirent_safe(mddev->sysfs_state);
6583 	return 0;
6584 }
6585 
6586 #ifndef MODULE
6587 static void autorun_array(struct mddev *mddev)
6588 {
6589 	struct md_rdev *rdev;
6590 	int err;
6591 
6592 	if (list_empty(&mddev->disks))
6593 		return;
6594 
6595 	pr_info("md: running: ");
6596 
6597 	rdev_for_each(rdev, mddev) {
6598 		pr_cont("<%pg>", rdev->bdev);
6599 	}
6600 	pr_cont("\n");
6601 
6602 	err = do_md_run(mddev);
6603 	if (err) {
6604 		pr_warn("md: do_md_run() returned %d\n", err);
6605 		do_md_stop(mddev, 0);
6606 	}
6607 }
6608 
6609 /*
6610  * lets try to run arrays based on all disks that have arrived
6611  * until now. (those are in pending_raid_disks)
6612  *
6613  * the method: pick the first pending disk, collect all disks with
6614  * the same UUID, remove all from the pending list and put them into
6615  * the 'same_array' list. Then order this list based on superblock
6616  * update time (freshest comes first), kick out 'old' disks and
6617  * compare superblocks. If everything's fine then run it.
6618  *
6619  * If "unit" is allocated, then bump its reference count
6620  */
6621 static void autorun_devices(int part)
6622 {
6623 	struct md_rdev *rdev0, *rdev, *tmp;
6624 	struct mddev *mddev;
6625 
6626 	pr_info("md: autorun ...\n");
6627 	while (!list_empty(&pending_raid_disks)) {
6628 		int unit;
6629 		dev_t dev;
6630 		LIST_HEAD(candidates);
6631 		rdev0 = list_entry(pending_raid_disks.next,
6632 					 struct md_rdev, same_set);
6633 
6634 		pr_debug("md: considering %pg ...\n", rdev0->bdev);
6635 		INIT_LIST_HEAD(&candidates);
6636 		rdev_for_each_list(rdev, tmp, &pending_raid_disks)
6637 			if (super_90_load(rdev, rdev0, 0) >= 0) {
6638 				pr_debug("md:  adding %pg ...\n",
6639 					 rdev->bdev);
6640 				list_move(&rdev->same_set, &candidates);
6641 			}
6642 		/*
6643 		 * now we have a set of devices, with all of them having
6644 		 * mostly sane superblocks. It's time to allocate the
6645 		 * mddev.
6646 		 */
6647 		if (part) {
6648 			dev = MKDEV(mdp_major,
6649 				    rdev0->preferred_minor << MdpMinorShift);
6650 			unit = MINOR(dev) >> MdpMinorShift;
6651 		} else {
6652 			dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6653 			unit = MINOR(dev);
6654 		}
6655 		if (rdev0->preferred_minor != unit) {
6656 			pr_warn("md: unit number in %pg is bad: %d\n",
6657 				rdev0->bdev, rdev0->preferred_minor);
6658 			break;
6659 		}
6660 
6661 		mddev = md_alloc(dev, NULL);
6662 		if (IS_ERR(mddev))
6663 			break;
6664 
6665 		if (mddev_suspend_and_lock(mddev))
6666 			pr_warn("md: %s locked, cannot run\n", mdname(mddev));
6667 		else if (mddev->raid_disks || mddev->major_version
6668 			 || !list_empty(&mddev->disks)) {
6669 			pr_warn("md: %s already running, cannot run %pg\n",
6670 				mdname(mddev), rdev0->bdev);
6671 			mddev_unlock_and_resume(mddev);
6672 		} else {
6673 			pr_debug("md: created %s\n", mdname(mddev));
6674 			mddev->persistent = 1;
6675 			rdev_for_each_list(rdev, tmp, &candidates) {
6676 				list_del_init(&rdev->same_set);
6677 				if (bind_rdev_to_array(rdev, mddev))
6678 					export_rdev(rdev, mddev);
6679 			}
6680 			autorun_array(mddev);
6681 			mddev_unlock_and_resume(mddev);
6682 		}
6683 		/* on success, candidates will be empty, on error
6684 		 * it won't...
6685 		 */
6686 		rdev_for_each_list(rdev, tmp, &candidates) {
6687 			list_del_init(&rdev->same_set);
6688 			export_rdev(rdev, mddev);
6689 		}
6690 		mddev_put(mddev);
6691 	}
6692 	pr_info("md: ... autorun DONE.\n");
6693 }
6694 #endif /* !MODULE */
6695 
6696 static int get_version(void __user *arg)
6697 {
6698 	mdu_version_t ver;
6699 
6700 	ver.major = MD_MAJOR_VERSION;
6701 	ver.minor = MD_MINOR_VERSION;
6702 	ver.patchlevel = MD_PATCHLEVEL_VERSION;
6703 
6704 	if (copy_to_user(arg, &ver, sizeof(ver)))
6705 		return -EFAULT;
6706 
6707 	return 0;
6708 }
6709 
6710 static int get_array_info(struct mddev *mddev, void __user *arg)
6711 {
6712 	mdu_array_info_t info;
6713 	int nr,working,insync,failed,spare;
6714 	struct md_rdev *rdev;
6715 
6716 	nr = working = insync = failed = spare = 0;
6717 	rcu_read_lock();
6718 	rdev_for_each_rcu(rdev, mddev) {
6719 		nr++;
6720 		if (test_bit(Faulty, &rdev->flags))
6721 			failed++;
6722 		else {
6723 			working++;
6724 			if (test_bit(In_sync, &rdev->flags))
6725 				insync++;
6726 			else if (test_bit(Journal, &rdev->flags))
6727 				/* TODO: add journal count to md_u.h */
6728 				;
6729 			else
6730 				spare++;
6731 		}
6732 	}
6733 	rcu_read_unlock();
6734 
6735 	info.major_version = mddev->major_version;
6736 	info.minor_version = mddev->minor_version;
6737 	info.patch_version = MD_PATCHLEVEL_VERSION;
6738 	info.ctime         = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
6739 	info.level         = mddev->level;
6740 	info.size          = mddev->dev_sectors / 2;
6741 	if (info.size != mddev->dev_sectors / 2) /* overflow */
6742 		info.size = -1;
6743 	info.nr_disks      = nr;
6744 	info.raid_disks    = mddev->raid_disks;
6745 	info.md_minor      = mddev->md_minor;
6746 	info.not_persistent= !mddev->persistent;
6747 
6748 	info.utime         = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
6749 	info.state         = 0;
6750 	if (mddev->in_sync)
6751 		info.state = (1<<MD_SB_CLEAN);
6752 	if (mddev->bitmap && mddev->bitmap_info.offset)
6753 		info.state |= (1<<MD_SB_BITMAP_PRESENT);
6754 	if (mddev_is_clustered(mddev))
6755 		info.state |= (1<<MD_SB_CLUSTERED);
6756 	info.active_disks  = insync;
6757 	info.working_disks = working;
6758 	info.failed_disks  = failed;
6759 	info.spare_disks   = spare;
6760 
6761 	info.layout        = mddev->layout;
6762 	info.chunk_size    = mddev->chunk_sectors << 9;
6763 
6764 	if (copy_to_user(arg, &info, sizeof(info)))
6765 		return -EFAULT;
6766 
6767 	return 0;
6768 }
6769 
6770 static int get_bitmap_file(struct mddev *mddev, void __user * arg)
6771 {
6772 	mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
6773 	char *ptr;
6774 	int err;
6775 
6776 	file = kzalloc(sizeof(*file), GFP_NOIO);
6777 	if (!file)
6778 		return -ENOMEM;
6779 
6780 	err = 0;
6781 	spin_lock(&mddev->lock);
6782 	/* bitmap enabled */
6783 	if (mddev->bitmap_info.file) {
6784 		ptr = file_path(mddev->bitmap_info.file, file->pathname,
6785 				sizeof(file->pathname));
6786 		if (IS_ERR(ptr))
6787 			err = PTR_ERR(ptr);
6788 		else
6789 			memmove(file->pathname, ptr,
6790 				sizeof(file->pathname)-(ptr-file->pathname));
6791 	}
6792 	spin_unlock(&mddev->lock);
6793 
6794 	if (err == 0 &&
6795 	    copy_to_user(arg, file, sizeof(*file)))
6796 		err = -EFAULT;
6797 
6798 	kfree(file);
6799 	return err;
6800 }
6801 
6802 static int get_disk_info(struct mddev *mddev, void __user * arg)
6803 {
6804 	mdu_disk_info_t info;
6805 	struct md_rdev *rdev;
6806 
6807 	if (copy_from_user(&info, arg, sizeof(info)))
6808 		return -EFAULT;
6809 
6810 	rcu_read_lock();
6811 	rdev = md_find_rdev_nr_rcu(mddev, info.number);
6812 	if (rdev) {
6813 		info.major = MAJOR(rdev->bdev->bd_dev);
6814 		info.minor = MINOR(rdev->bdev->bd_dev);
6815 		info.raid_disk = rdev->raid_disk;
6816 		info.state = 0;
6817 		if (test_bit(Faulty, &rdev->flags))
6818 			info.state |= (1<<MD_DISK_FAULTY);
6819 		else if (test_bit(In_sync, &rdev->flags)) {
6820 			info.state |= (1<<MD_DISK_ACTIVE);
6821 			info.state |= (1<<MD_DISK_SYNC);
6822 		}
6823 		if (test_bit(Journal, &rdev->flags))
6824 			info.state |= (1<<MD_DISK_JOURNAL);
6825 		if (test_bit(WriteMostly, &rdev->flags))
6826 			info.state |= (1<<MD_DISK_WRITEMOSTLY);
6827 		if (test_bit(FailFast, &rdev->flags))
6828 			info.state |= (1<<MD_DISK_FAILFAST);
6829 	} else {
6830 		info.major = info.minor = 0;
6831 		info.raid_disk = -1;
6832 		info.state = (1<<MD_DISK_REMOVED);
6833 	}
6834 	rcu_read_unlock();
6835 
6836 	if (copy_to_user(arg, &info, sizeof(info)))
6837 		return -EFAULT;
6838 
6839 	return 0;
6840 }
6841 
6842 int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info)
6843 {
6844 	struct md_rdev *rdev;
6845 	dev_t dev = MKDEV(info->major,info->minor);
6846 
6847 	if (mddev_is_clustered(mddev) &&
6848 		!(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
6849 		pr_warn("%s: Cannot add to clustered mddev.\n",
6850 			mdname(mddev));
6851 		return -EINVAL;
6852 	}
6853 
6854 	if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6855 		return -EOVERFLOW;
6856 
6857 	if (!mddev->raid_disks) {
6858 		int err;
6859 		/* expecting a device which has a superblock */
6860 		rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6861 		if (IS_ERR(rdev)) {
6862 			pr_warn("md: md_import_device returned %ld\n",
6863 				PTR_ERR(rdev));
6864 			return PTR_ERR(rdev);
6865 		}
6866 		if (!list_empty(&mddev->disks)) {
6867 			struct md_rdev *rdev0
6868 				= list_entry(mddev->disks.next,
6869 					     struct md_rdev, same_set);
6870 			err = super_types[mddev->major_version]
6871 				.load_super(rdev, rdev0, mddev->minor_version);
6872 			if (err < 0) {
6873 				pr_warn("md: %pg has different UUID to %pg\n",
6874 					rdev->bdev,
6875 					rdev0->bdev);
6876 				export_rdev(rdev, mddev);
6877 				return -EINVAL;
6878 			}
6879 		}
6880 		err = bind_rdev_to_array(rdev, mddev);
6881 		if (err)
6882 			export_rdev(rdev, mddev);
6883 		return err;
6884 	}
6885 
6886 	/*
6887 	 * md_add_new_disk can be used once the array is assembled
6888 	 * to add "hot spares".  They must already have a superblock
6889 	 * written
6890 	 */
6891 	if (mddev->pers) {
6892 		int err;
6893 		if (!mddev->pers->hot_add_disk) {
6894 			pr_warn("%s: personality does not support diskops!\n",
6895 				mdname(mddev));
6896 			return -EINVAL;
6897 		}
6898 		if (mddev->persistent)
6899 			rdev = md_import_device(dev, mddev->major_version,
6900 						mddev->minor_version);
6901 		else
6902 			rdev = md_import_device(dev, -1, -1);
6903 		if (IS_ERR(rdev)) {
6904 			pr_warn("md: md_import_device returned %ld\n",
6905 				PTR_ERR(rdev));
6906 			return PTR_ERR(rdev);
6907 		}
6908 		/* set saved_raid_disk if appropriate */
6909 		if (!mddev->persistent) {
6910 			if (info->state & (1<<MD_DISK_SYNC)  &&
6911 			    info->raid_disk < mddev->raid_disks) {
6912 				rdev->raid_disk = info->raid_disk;
6913 				clear_bit(Bitmap_sync, &rdev->flags);
6914 			} else
6915 				rdev->raid_disk = -1;
6916 			rdev->saved_raid_disk = rdev->raid_disk;
6917 		} else
6918 			super_types[mddev->major_version].
6919 				validate_super(mddev, NULL/*freshest*/, rdev);
6920 		if ((info->state & (1<<MD_DISK_SYNC)) &&
6921 		     rdev->raid_disk != info->raid_disk) {
6922 			/* This was a hot-add request, but events doesn't
6923 			 * match, so reject it.
6924 			 */
6925 			export_rdev(rdev, mddev);
6926 			return -EINVAL;
6927 		}
6928 
6929 		clear_bit(In_sync, &rdev->flags); /* just to be sure */
6930 		if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6931 			set_bit(WriteMostly, &rdev->flags);
6932 		else
6933 			clear_bit(WriteMostly, &rdev->flags);
6934 		if (info->state & (1<<MD_DISK_FAILFAST))
6935 			set_bit(FailFast, &rdev->flags);
6936 		else
6937 			clear_bit(FailFast, &rdev->flags);
6938 
6939 		if (info->state & (1<<MD_DISK_JOURNAL)) {
6940 			struct md_rdev *rdev2;
6941 			bool has_journal = false;
6942 
6943 			/* make sure no existing journal disk */
6944 			rdev_for_each(rdev2, mddev) {
6945 				if (test_bit(Journal, &rdev2->flags)) {
6946 					has_journal = true;
6947 					break;
6948 				}
6949 			}
6950 			if (has_journal || mddev->bitmap) {
6951 				export_rdev(rdev, mddev);
6952 				return -EBUSY;
6953 			}
6954 			set_bit(Journal, &rdev->flags);
6955 		}
6956 		/*
6957 		 * check whether the device shows up in other nodes
6958 		 */
6959 		if (mddev_is_clustered(mddev)) {
6960 			if (info->state & (1 << MD_DISK_CANDIDATE))
6961 				set_bit(Candidate, &rdev->flags);
6962 			else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
6963 				/* --add initiated by this node */
6964 				err = md_cluster_ops->add_new_disk(mddev, rdev);
6965 				if (err) {
6966 					export_rdev(rdev, mddev);
6967 					return err;
6968 				}
6969 			}
6970 		}
6971 
6972 		rdev->raid_disk = -1;
6973 		err = bind_rdev_to_array(rdev, mddev);
6974 
6975 		if (err)
6976 			export_rdev(rdev, mddev);
6977 
6978 		if (mddev_is_clustered(mddev)) {
6979 			if (info->state & (1 << MD_DISK_CANDIDATE)) {
6980 				if (!err) {
6981 					err = md_cluster_ops->new_disk_ack(mddev,
6982 						err == 0);
6983 					if (err)
6984 						md_kick_rdev_from_array(rdev);
6985 				}
6986 			} else {
6987 				if (err)
6988 					md_cluster_ops->add_new_disk_cancel(mddev);
6989 				else
6990 					err = add_bound_rdev(rdev);
6991 			}
6992 
6993 		} else if (!err)
6994 			err = add_bound_rdev(rdev);
6995 
6996 		return err;
6997 	}
6998 
6999 	/* otherwise, md_add_new_disk is only allowed
7000 	 * for major_version==0 superblocks
7001 	 */
7002 	if (mddev->major_version != 0) {
7003 		pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
7004 		return -EINVAL;
7005 	}
7006 
7007 	if (!(info->state & (1<<MD_DISK_FAULTY))) {
7008 		int err;
7009 		rdev = md_import_device(dev, -1, 0);
7010 		if (IS_ERR(rdev)) {
7011 			pr_warn("md: error, md_import_device() returned %ld\n",
7012 				PTR_ERR(rdev));
7013 			return PTR_ERR(rdev);
7014 		}
7015 		rdev->desc_nr = info->number;
7016 		if (info->raid_disk < mddev->raid_disks)
7017 			rdev->raid_disk = info->raid_disk;
7018 		else
7019 			rdev->raid_disk = -1;
7020 
7021 		if (rdev->raid_disk < mddev->raid_disks)
7022 			if (info->state & (1<<MD_DISK_SYNC))
7023 				set_bit(In_sync, &rdev->flags);
7024 
7025 		if (info->state & (1<<MD_DISK_WRITEMOSTLY))
7026 			set_bit(WriteMostly, &rdev->flags);
7027 		if (info->state & (1<<MD_DISK_FAILFAST))
7028 			set_bit(FailFast, &rdev->flags);
7029 
7030 		if (!mddev->persistent) {
7031 			pr_debug("md: nonpersistent superblock ...\n");
7032 			rdev->sb_start = bdev_nr_sectors(rdev->bdev);
7033 		} else
7034 			rdev->sb_start = calc_dev_sboffset(rdev);
7035 		rdev->sectors = rdev->sb_start;
7036 
7037 		err = bind_rdev_to_array(rdev, mddev);
7038 		if (err) {
7039 			export_rdev(rdev, mddev);
7040 			return err;
7041 		}
7042 	}
7043 
7044 	return 0;
7045 }
7046 
7047 static int hot_remove_disk(struct mddev *mddev, dev_t dev)
7048 {
7049 	struct md_rdev *rdev;
7050 
7051 	if (!mddev->pers)
7052 		return -ENODEV;
7053 
7054 	rdev = find_rdev(mddev, dev);
7055 	if (!rdev)
7056 		return -ENXIO;
7057 
7058 	if (rdev->raid_disk < 0)
7059 		goto kick_rdev;
7060 
7061 	clear_bit(Blocked, &rdev->flags);
7062 	remove_and_add_spares(mddev, rdev);
7063 
7064 	if (rdev->raid_disk >= 0)
7065 		goto busy;
7066 
7067 kick_rdev:
7068 	if (mddev_is_clustered(mddev)) {
7069 		if (md_cluster_ops->remove_disk(mddev, rdev))
7070 			goto busy;
7071 	}
7072 
7073 	md_kick_rdev_from_array(rdev);
7074 	set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
7075 	if (!mddev->thread)
7076 		md_update_sb(mddev, 1);
7077 	md_new_event();
7078 
7079 	return 0;
7080 busy:
7081 	pr_debug("md: cannot remove active disk %pg from %s ...\n",
7082 		 rdev->bdev, mdname(mddev));
7083 	return -EBUSY;
7084 }
7085 
7086 static int hot_add_disk(struct mddev *mddev, dev_t dev)
7087 {
7088 	int err;
7089 	struct md_rdev *rdev;
7090 
7091 	if (!mddev->pers)
7092 		return -ENODEV;
7093 
7094 	if (mddev->major_version != 0) {
7095 		pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
7096 			mdname(mddev));
7097 		return -EINVAL;
7098 	}
7099 	if (!mddev->pers->hot_add_disk) {
7100 		pr_warn("%s: personality does not support diskops!\n",
7101 			mdname(mddev));
7102 		return -EINVAL;
7103 	}
7104 
7105 	rdev = md_import_device(dev, -1, 0);
7106 	if (IS_ERR(rdev)) {
7107 		pr_warn("md: error, md_import_device() returned %ld\n",
7108 			PTR_ERR(rdev));
7109 		return -EINVAL;
7110 	}
7111 
7112 	if (mddev->persistent)
7113 		rdev->sb_start = calc_dev_sboffset(rdev);
7114 	else
7115 		rdev->sb_start = bdev_nr_sectors(rdev->bdev);
7116 
7117 	rdev->sectors = rdev->sb_start;
7118 
7119 	if (test_bit(Faulty, &rdev->flags)) {
7120 		pr_warn("md: can not hot-add faulty %pg disk to %s!\n",
7121 			rdev->bdev, mdname(mddev));
7122 		err = -EINVAL;
7123 		goto abort_export;
7124 	}
7125 
7126 	clear_bit(In_sync, &rdev->flags);
7127 	rdev->desc_nr = -1;
7128 	rdev->saved_raid_disk = -1;
7129 	err = bind_rdev_to_array(rdev, mddev);
7130 	if (err)
7131 		goto abort_export;
7132 
7133 	/*
7134 	 * The rest should better be atomic, we can have disk failures
7135 	 * noticed in interrupt contexts ...
7136 	 */
7137 
7138 	rdev->raid_disk = -1;
7139 
7140 	set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
7141 	if (!mddev->thread)
7142 		md_update_sb(mddev, 1);
7143 	/*
7144 	 * Kick recovery, maybe this spare has to be added to the
7145 	 * array immediately.
7146 	 */
7147 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7148 	md_new_event();
7149 	return 0;
7150 
7151 abort_export:
7152 	export_rdev(rdev, mddev);
7153 	return err;
7154 }
7155 
7156 static int set_bitmap_file(struct mddev *mddev, int fd)
7157 {
7158 	int err = 0;
7159 
7160 	if (mddev->pers) {
7161 		if (!mddev->pers->quiesce || !mddev->thread)
7162 			return -EBUSY;
7163 		if (mddev->recovery || mddev->sync_thread)
7164 			return -EBUSY;
7165 		/* we should be able to change the bitmap.. */
7166 	}
7167 
7168 	if (fd >= 0) {
7169 		struct inode *inode;
7170 		struct file *f;
7171 
7172 		if (mddev->bitmap || mddev->bitmap_info.file)
7173 			return -EEXIST; /* cannot add when bitmap is present */
7174 
7175 		if (!IS_ENABLED(CONFIG_MD_BITMAP_FILE)) {
7176 			pr_warn("%s: bitmap files not supported by this kernel\n",
7177 				mdname(mddev));
7178 			return -EINVAL;
7179 		}
7180 		pr_warn("%s: using deprecated bitmap file support\n",
7181 			mdname(mddev));
7182 
7183 		f = fget(fd);
7184 
7185 		if (f == NULL) {
7186 			pr_warn("%s: error: failed to get bitmap file\n",
7187 				mdname(mddev));
7188 			return -EBADF;
7189 		}
7190 
7191 		inode = f->f_mapping->host;
7192 		if (!S_ISREG(inode->i_mode)) {
7193 			pr_warn("%s: error: bitmap file must be a regular file\n",
7194 				mdname(mddev));
7195 			err = -EBADF;
7196 		} else if (!(f->f_mode & FMODE_WRITE)) {
7197 			pr_warn("%s: error: bitmap file must open for write\n",
7198 				mdname(mddev));
7199 			err = -EBADF;
7200 		} else if (atomic_read(&inode->i_writecount) != 1) {
7201 			pr_warn("%s: error: bitmap file is already in use\n",
7202 				mdname(mddev));
7203 			err = -EBUSY;
7204 		}
7205 		if (err) {
7206 			fput(f);
7207 			return err;
7208 		}
7209 		mddev->bitmap_info.file = f;
7210 		mddev->bitmap_info.offset = 0; /* file overrides offset */
7211 	} else if (mddev->bitmap == NULL)
7212 		return -ENOENT; /* cannot remove what isn't there */
7213 	err = 0;
7214 	if (mddev->pers) {
7215 		if (fd >= 0) {
7216 			err = mddev->bitmap_ops->create(mddev, -1);
7217 			if (!err)
7218 				err = mddev->bitmap_ops->load(mddev);
7219 
7220 			if (err) {
7221 				mddev->bitmap_ops->destroy(mddev);
7222 				fd = -1;
7223 			}
7224 		} else if (fd < 0) {
7225 			mddev->bitmap_ops->destroy(mddev);
7226 		}
7227 	}
7228 
7229 	if (fd < 0) {
7230 		struct file *f = mddev->bitmap_info.file;
7231 		if (f) {
7232 			spin_lock(&mddev->lock);
7233 			mddev->bitmap_info.file = NULL;
7234 			spin_unlock(&mddev->lock);
7235 			fput(f);
7236 		}
7237 	}
7238 
7239 	return err;
7240 }
7241 
7242 /*
7243  * md_set_array_info is used two different ways
7244  * The original usage is when creating a new array.
7245  * In this usage, raid_disks is > 0 and it together with
7246  *  level, size, not_persistent,layout,chunksize determine the
7247  *  shape of the array.
7248  *  This will always create an array with a type-0.90.0 superblock.
7249  * The newer usage is when assembling an array.
7250  *  In this case raid_disks will be 0, and the major_version field is
7251  *  use to determine which style super-blocks are to be found on the devices.
7252  *  The minor and patch _version numbers are also kept incase the
7253  *  super_block handler wishes to interpret them.
7254  */
7255 int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info)
7256 {
7257 	if (info->raid_disks == 0) {
7258 		/* just setting version number for superblock loading */
7259 		if (info->major_version < 0 ||
7260 		    info->major_version >= ARRAY_SIZE(super_types) ||
7261 		    super_types[info->major_version].name == NULL) {
7262 			/* maybe try to auto-load a module? */
7263 			pr_warn("md: superblock version %d not known\n",
7264 				info->major_version);
7265 			return -EINVAL;
7266 		}
7267 		mddev->major_version = info->major_version;
7268 		mddev->minor_version = info->minor_version;
7269 		mddev->patch_version = info->patch_version;
7270 		mddev->persistent = !info->not_persistent;
7271 		/* ensure mddev_put doesn't delete this now that there
7272 		 * is some minimal configuration.
7273 		 */
7274 		mddev->ctime         = ktime_get_real_seconds();
7275 		return 0;
7276 	}
7277 	mddev->major_version = MD_MAJOR_VERSION;
7278 	mddev->minor_version = MD_MINOR_VERSION;
7279 	mddev->patch_version = MD_PATCHLEVEL_VERSION;
7280 	mddev->ctime         = ktime_get_real_seconds();
7281 
7282 	mddev->level         = info->level;
7283 	mddev->clevel[0]     = 0;
7284 	mddev->dev_sectors   = 2 * (sector_t)info->size;
7285 	mddev->raid_disks    = info->raid_disks;
7286 	/* don't set md_minor, it is determined by which /dev/md* was
7287 	 * openned
7288 	 */
7289 	if (info->state & (1<<MD_SB_CLEAN))
7290 		mddev->recovery_cp = MaxSector;
7291 	else
7292 		mddev->recovery_cp = 0;
7293 	mddev->persistent    = ! info->not_persistent;
7294 	mddev->external	     = 0;
7295 
7296 	mddev->layout        = info->layout;
7297 	if (mddev->level == 0)
7298 		/* Cannot trust RAID0 layout info here */
7299 		mddev->layout = -1;
7300 	mddev->chunk_sectors = info->chunk_size >> 9;
7301 
7302 	if (mddev->persistent) {
7303 		mddev->max_disks = MD_SB_DISKS;
7304 		mddev->flags = 0;
7305 		mddev->sb_flags = 0;
7306 	}
7307 	set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
7308 
7309 	mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
7310 	mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
7311 	mddev->bitmap_info.offset = 0;
7312 
7313 	mddev->reshape_position = MaxSector;
7314 
7315 	/*
7316 	 * Generate a 128 bit UUID
7317 	 */
7318 	get_random_bytes(mddev->uuid, 16);
7319 
7320 	mddev->new_level = mddev->level;
7321 	mddev->new_chunk_sectors = mddev->chunk_sectors;
7322 	mddev->new_layout = mddev->layout;
7323 	mddev->delta_disks = 0;
7324 	mddev->reshape_backwards = 0;
7325 
7326 	return 0;
7327 }
7328 
7329 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
7330 {
7331 	lockdep_assert_held(&mddev->reconfig_mutex);
7332 
7333 	if (mddev->external_size)
7334 		return;
7335 
7336 	mddev->array_sectors = array_sectors;
7337 }
7338 EXPORT_SYMBOL(md_set_array_sectors);
7339 
7340 static int update_size(struct mddev *mddev, sector_t num_sectors)
7341 {
7342 	struct md_rdev *rdev;
7343 	int rv;
7344 	int fit = (num_sectors == 0);
7345 	sector_t old_dev_sectors = mddev->dev_sectors;
7346 
7347 	if (mddev->pers->resize == NULL)
7348 		return -EINVAL;
7349 	/* The "num_sectors" is the number of sectors of each device that
7350 	 * is used.  This can only make sense for arrays with redundancy.
7351 	 * linear and raid0 always use whatever space is available. We can only
7352 	 * consider changing this number if no resync or reconstruction is
7353 	 * happening, and if the new size is acceptable. It must fit before the
7354 	 * sb_start or, if that is <data_offset, it must fit before the size
7355 	 * of each device.  If num_sectors is zero, we find the largest size
7356 	 * that fits.
7357 	 */
7358 	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
7359 		return -EBUSY;
7360 	if (!md_is_rdwr(mddev))
7361 		return -EROFS;
7362 
7363 	rdev_for_each(rdev, mddev) {
7364 		sector_t avail = rdev->sectors;
7365 
7366 		if (fit && (num_sectors == 0 || num_sectors > avail))
7367 			num_sectors = avail;
7368 		if (avail < num_sectors)
7369 			return -ENOSPC;
7370 	}
7371 	rv = mddev->pers->resize(mddev, num_sectors);
7372 	if (!rv) {
7373 		if (mddev_is_clustered(mddev))
7374 			md_cluster_ops->update_size(mddev, old_dev_sectors);
7375 		else if (!mddev_is_dm(mddev))
7376 			set_capacity_and_notify(mddev->gendisk,
7377 						mddev->array_sectors);
7378 	}
7379 	return rv;
7380 }
7381 
7382 static int update_raid_disks(struct mddev *mddev, int raid_disks)
7383 {
7384 	int rv;
7385 	struct md_rdev *rdev;
7386 	/* change the number of raid disks */
7387 	if (mddev->pers->check_reshape == NULL)
7388 		return -EINVAL;
7389 	if (!md_is_rdwr(mddev))
7390 		return -EROFS;
7391 	if (raid_disks <= 0 ||
7392 	    (mddev->max_disks && raid_disks >= mddev->max_disks))
7393 		return -EINVAL;
7394 	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7395 	    test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) ||
7396 	    mddev->reshape_position != MaxSector)
7397 		return -EBUSY;
7398 
7399 	rdev_for_each(rdev, mddev) {
7400 		if (mddev->raid_disks < raid_disks &&
7401 		    rdev->data_offset < rdev->new_data_offset)
7402 			return -EINVAL;
7403 		if (mddev->raid_disks > raid_disks &&
7404 		    rdev->data_offset > rdev->new_data_offset)
7405 			return -EINVAL;
7406 	}
7407 
7408 	mddev->delta_disks = raid_disks - mddev->raid_disks;
7409 	if (mddev->delta_disks < 0)
7410 		mddev->reshape_backwards = 1;
7411 	else if (mddev->delta_disks > 0)
7412 		mddev->reshape_backwards = 0;
7413 
7414 	rv = mddev->pers->check_reshape(mddev);
7415 	if (rv < 0) {
7416 		mddev->delta_disks = 0;
7417 		mddev->reshape_backwards = 0;
7418 	}
7419 	return rv;
7420 }
7421 
7422 /*
7423  * update_array_info is used to change the configuration of an
7424  * on-line array.
7425  * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7426  * fields in the info are checked against the array.
7427  * Any differences that cannot be handled will cause an error.
7428  * Normally, only one change can be managed at a time.
7429  */
7430 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
7431 {
7432 	int rv = 0;
7433 	int cnt = 0;
7434 	int state = 0;
7435 
7436 	/* calculate expected state,ignoring low bits */
7437 	if (mddev->bitmap && mddev->bitmap_info.offset)
7438 		state |= (1 << MD_SB_BITMAP_PRESENT);
7439 
7440 	if (mddev->major_version != info->major_version ||
7441 	    mddev->minor_version != info->minor_version ||
7442 /*	    mddev->patch_version != info->patch_version || */
7443 	    mddev->ctime         != info->ctime         ||
7444 	    mddev->level         != info->level         ||
7445 /*	    mddev->layout        != info->layout        || */
7446 	    mddev->persistent	 != !info->not_persistent ||
7447 	    mddev->chunk_sectors != info->chunk_size >> 9 ||
7448 	    /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7449 	    ((state^info->state) & 0xfffffe00)
7450 		)
7451 		return -EINVAL;
7452 	/* Check there is only one change */
7453 	if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7454 		cnt++;
7455 	if (mddev->raid_disks != info->raid_disks)
7456 		cnt++;
7457 	if (mddev->layout != info->layout)
7458 		cnt++;
7459 	if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7460 		cnt++;
7461 	if (cnt == 0)
7462 		return 0;
7463 	if (cnt > 1)
7464 		return -EINVAL;
7465 
7466 	if (mddev->layout != info->layout) {
7467 		/* Change layout
7468 		 * we don't need to do anything at the md level, the
7469 		 * personality will take care of it all.
7470 		 */
7471 		if (mddev->pers->check_reshape == NULL)
7472 			return -EINVAL;
7473 		else {
7474 			mddev->new_layout = info->layout;
7475 			rv = mddev->pers->check_reshape(mddev);
7476 			if (rv)
7477 				mddev->new_layout = mddev->layout;
7478 			return rv;
7479 		}
7480 	}
7481 	if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7482 		rv = update_size(mddev, (sector_t)info->size * 2);
7483 
7484 	if (mddev->raid_disks    != info->raid_disks)
7485 		rv = update_raid_disks(mddev, info->raid_disks);
7486 
7487 	if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
7488 		if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7489 			rv = -EINVAL;
7490 			goto err;
7491 		}
7492 		if (mddev->recovery || mddev->sync_thread) {
7493 			rv = -EBUSY;
7494 			goto err;
7495 		}
7496 		if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
7497 			/* add the bitmap */
7498 			if (mddev->bitmap) {
7499 				rv = -EEXIST;
7500 				goto err;
7501 			}
7502 			if (mddev->bitmap_info.default_offset == 0) {
7503 				rv = -EINVAL;
7504 				goto err;
7505 			}
7506 			mddev->bitmap_info.offset =
7507 				mddev->bitmap_info.default_offset;
7508 			mddev->bitmap_info.space =
7509 				mddev->bitmap_info.default_space;
7510 			rv = mddev->bitmap_ops->create(mddev, -1);
7511 			if (!rv)
7512 				rv = mddev->bitmap_ops->load(mddev);
7513 
7514 			if (rv)
7515 				mddev->bitmap_ops->destroy(mddev);
7516 		} else {
7517 			struct md_bitmap_stats stats;
7518 
7519 			rv = mddev->bitmap_ops->get_stats(mddev->bitmap, &stats);
7520 			if (rv)
7521 				goto err;
7522 
7523 			if (stats.file) {
7524 				rv = -EINVAL;
7525 				goto err;
7526 			}
7527 
7528 			if (mddev->bitmap_info.nodes) {
7529 				/* hold PW on all the bitmap lock */
7530 				if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
7531 					pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
7532 					rv = -EPERM;
7533 					md_cluster_ops->unlock_all_bitmaps(mddev);
7534 					goto err;
7535 				}
7536 
7537 				mddev->bitmap_info.nodes = 0;
7538 				md_cluster_ops->leave(mddev);
7539 				module_put(md_cluster_mod);
7540 				mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
7541 			}
7542 			mddev->bitmap_ops->destroy(mddev);
7543 			mddev->bitmap_info.offset = 0;
7544 		}
7545 	}
7546 	md_update_sb(mddev, 1);
7547 	return rv;
7548 err:
7549 	return rv;
7550 }
7551 
7552 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
7553 {
7554 	struct md_rdev *rdev;
7555 	int err = 0;
7556 
7557 	if (mddev->pers == NULL)
7558 		return -ENODEV;
7559 
7560 	rcu_read_lock();
7561 	rdev = md_find_rdev_rcu(mddev, dev);
7562 	if (!rdev)
7563 		err =  -ENODEV;
7564 	else {
7565 		md_error(mddev, rdev);
7566 		if (test_bit(MD_BROKEN, &mddev->flags))
7567 			err = -EBUSY;
7568 	}
7569 	rcu_read_unlock();
7570 	return err;
7571 }
7572 
7573 /*
7574  * We have a problem here : there is no easy way to give a CHS
7575  * virtual geometry. We currently pretend that we have a 2 heads
7576  * 4 sectors (with a BIG number of cylinders...). This drives
7577  * dosfs just mad... ;-)
7578  */
7579 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7580 {
7581 	struct mddev *mddev = bdev->bd_disk->private_data;
7582 
7583 	geo->heads = 2;
7584 	geo->sectors = 4;
7585 	geo->cylinders = mddev->array_sectors / 8;
7586 	return 0;
7587 }
7588 
7589 static inline int md_ioctl_valid(unsigned int cmd)
7590 {
7591 	switch (cmd) {
7592 	case GET_ARRAY_INFO:
7593 	case GET_DISK_INFO:
7594 	case RAID_VERSION:
7595 		return 0;
7596 	case ADD_NEW_DISK:
7597 	case GET_BITMAP_FILE:
7598 	case HOT_ADD_DISK:
7599 	case HOT_REMOVE_DISK:
7600 	case RESTART_ARRAY_RW:
7601 	case RUN_ARRAY:
7602 	case SET_ARRAY_INFO:
7603 	case SET_BITMAP_FILE:
7604 	case SET_DISK_FAULTY:
7605 	case STOP_ARRAY:
7606 	case STOP_ARRAY_RO:
7607 	case CLUSTERED_DISK_NACK:
7608 		if (!capable(CAP_SYS_ADMIN))
7609 			return -EACCES;
7610 		return 0;
7611 	default:
7612 		return -ENOTTY;
7613 	}
7614 }
7615 
7616 static bool md_ioctl_need_suspend(unsigned int cmd)
7617 {
7618 	switch (cmd) {
7619 	case ADD_NEW_DISK:
7620 	case HOT_ADD_DISK:
7621 	case HOT_REMOVE_DISK:
7622 	case SET_BITMAP_FILE:
7623 	case SET_ARRAY_INFO:
7624 		return true;
7625 	default:
7626 		return false;
7627 	}
7628 }
7629 
7630 static int __md_set_array_info(struct mddev *mddev, void __user *argp)
7631 {
7632 	mdu_array_info_t info;
7633 	int err;
7634 
7635 	if (!argp)
7636 		memset(&info, 0, sizeof(info));
7637 	else if (copy_from_user(&info, argp, sizeof(info)))
7638 		return -EFAULT;
7639 
7640 	if (mddev->pers) {
7641 		err = update_array_info(mddev, &info);
7642 		if (err)
7643 			pr_warn("md: couldn't update array info. %d\n", err);
7644 		return err;
7645 	}
7646 
7647 	if (!list_empty(&mddev->disks)) {
7648 		pr_warn("md: array %s already has disks!\n", mdname(mddev));
7649 		return -EBUSY;
7650 	}
7651 
7652 	if (mddev->raid_disks) {
7653 		pr_warn("md: array %s already initialised!\n", mdname(mddev));
7654 		return -EBUSY;
7655 	}
7656 
7657 	err = md_set_array_info(mddev, &info);
7658 	if (err)
7659 		pr_warn("md: couldn't set array info. %d\n", err);
7660 
7661 	return err;
7662 }
7663 
7664 static int md_ioctl(struct block_device *bdev, blk_mode_t mode,
7665 			unsigned int cmd, unsigned long arg)
7666 {
7667 	int err = 0;
7668 	void __user *argp = (void __user *)arg;
7669 	struct mddev *mddev = NULL;
7670 
7671 	err = md_ioctl_valid(cmd);
7672 	if (err)
7673 		return err;
7674 
7675 	/*
7676 	 * Commands dealing with the RAID driver but not any
7677 	 * particular array:
7678 	 */
7679 	if (cmd == RAID_VERSION)
7680 		return get_version(argp);
7681 
7682 	/*
7683 	 * Commands creating/starting a new array:
7684 	 */
7685 
7686 	mddev = bdev->bd_disk->private_data;
7687 
7688 	/* Some actions do not requires the mutex */
7689 	switch (cmd) {
7690 	case GET_ARRAY_INFO:
7691 		if (!mddev->raid_disks && !mddev->external)
7692 			return -ENODEV;
7693 		return get_array_info(mddev, argp);
7694 
7695 	case GET_DISK_INFO:
7696 		if (!mddev->raid_disks && !mddev->external)
7697 			return -ENODEV;
7698 		return get_disk_info(mddev, argp);
7699 
7700 	case SET_DISK_FAULTY:
7701 		return set_disk_faulty(mddev, new_decode_dev(arg));
7702 
7703 	case GET_BITMAP_FILE:
7704 		return get_bitmap_file(mddev, argp);
7705 	}
7706 
7707 	if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7708 		/* Need to flush page cache, and ensure no-one else opens
7709 		 * and writes
7710 		 */
7711 		err = mddev_set_closing_and_sync_blockdev(mddev, 1);
7712 		if (err)
7713 			return err;
7714 	}
7715 
7716 	if (!md_is_rdwr(mddev))
7717 		flush_work(&mddev->sync_work);
7718 
7719 	err = md_ioctl_need_suspend(cmd) ? mddev_suspend_and_lock(mddev) :
7720 					   mddev_lock(mddev);
7721 	if (err) {
7722 		pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7723 			 err, cmd);
7724 		goto out;
7725 	}
7726 
7727 	if (cmd == SET_ARRAY_INFO) {
7728 		err = __md_set_array_info(mddev, argp);
7729 		goto unlock;
7730 	}
7731 
7732 	/*
7733 	 * Commands querying/configuring an existing array:
7734 	 */
7735 	/* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
7736 	 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
7737 	if ((!mddev->raid_disks && !mddev->external)
7738 	    && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7739 	    && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7740 	    && cmd != GET_BITMAP_FILE) {
7741 		err = -ENODEV;
7742 		goto unlock;
7743 	}
7744 
7745 	/*
7746 	 * Commands even a read-only array can execute:
7747 	 */
7748 	switch (cmd) {
7749 	case RESTART_ARRAY_RW:
7750 		err = restart_array(mddev);
7751 		goto unlock;
7752 
7753 	case STOP_ARRAY:
7754 		err = do_md_stop(mddev, 0);
7755 		goto unlock;
7756 
7757 	case STOP_ARRAY_RO:
7758 		if (mddev->pers)
7759 			err = md_set_readonly(mddev);
7760 		goto unlock;
7761 
7762 	case HOT_REMOVE_DISK:
7763 		err = hot_remove_disk(mddev, new_decode_dev(arg));
7764 		goto unlock;
7765 
7766 	case ADD_NEW_DISK:
7767 		/* We can support ADD_NEW_DISK on read-only arrays
7768 		 * only if we are re-adding a preexisting device.
7769 		 * So require mddev->pers and MD_DISK_SYNC.
7770 		 */
7771 		if (mddev->pers) {
7772 			mdu_disk_info_t info;
7773 			if (copy_from_user(&info, argp, sizeof(info)))
7774 				err = -EFAULT;
7775 			else if (!(info.state & (1<<MD_DISK_SYNC)))
7776 				/* Need to clear read-only for this */
7777 				break;
7778 			else
7779 				err = md_add_new_disk(mddev, &info);
7780 			goto unlock;
7781 		}
7782 		break;
7783 	}
7784 
7785 	/*
7786 	 * The remaining ioctls are changing the state of the
7787 	 * superblock, so we do not allow them on read-only arrays.
7788 	 */
7789 	if (!md_is_rdwr(mddev) && mddev->pers) {
7790 		if (mddev->ro != MD_AUTO_READ) {
7791 			err = -EROFS;
7792 			goto unlock;
7793 		}
7794 		mddev->ro = MD_RDWR;
7795 		sysfs_notify_dirent_safe(mddev->sysfs_state);
7796 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7797 		/* mddev_unlock will wake thread */
7798 		/* If a device failed while we were read-only, we
7799 		 * need to make sure the metadata is updated now.
7800 		 */
7801 		if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
7802 			mddev_unlock(mddev);
7803 			wait_event(mddev->sb_wait,
7804 				   !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7805 				   !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
7806 			mddev_lock_nointr(mddev);
7807 		}
7808 	}
7809 
7810 	switch (cmd) {
7811 	case ADD_NEW_DISK:
7812 	{
7813 		mdu_disk_info_t info;
7814 		if (copy_from_user(&info, argp, sizeof(info)))
7815 			err = -EFAULT;
7816 		else
7817 			err = md_add_new_disk(mddev, &info);
7818 		goto unlock;
7819 	}
7820 
7821 	case CLUSTERED_DISK_NACK:
7822 		if (mddev_is_clustered(mddev))
7823 			md_cluster_ops->new_disk_ack(mddev, false);
7824 		else
7825 			err = -EINVAL;
7826 		goto unlock;
7827 
7828 	case HOT_ADD_DISK:
7829 		err = hot_add_disk(mddev, new_decode_dev(arg));
7830 		goto unlock;
7831 
7832 	case RUN_ARRAY:
7833 		err = do_md_run(mddev);
7834 		goto unlock;
7835 
7836 	case SET_BITMAP_FILE:
7837 		err = set_bitmap_file(mddev, (int)arg);
7838 		goto unlock;
7839 
7840 	default:
7841 		err = -EINVAL;
7842 		goto unlock;
7843 	}
7844 
7845 unlock:
7846 	if (mddev->hold_active == UNTIL_IOCTL &&
7847 	    err != -EINVAL)
7848 		mddev->hold_active = 0;
7849 
7850 	md_ioctl_need_suspend(cmd) ? mddev_unlock_and_resume(mddev) :
7851 				     mddev_unlock(mddev);
7852 
7853 out:
7854 	if (cmd == STOP_ARRAY_RO || (err && cmd == STOP_ARRAY))
7855 		clear_bit(MD_CLOSING, &mddev->flags);
7856 	return err;
7857 }
7858 #ifdef CONFIG_COMPAT
7859 static int md_compat_ioctl(struct block_device *bdev, blk_mode_t mode,
7860 		    unsigned int cmd, unsigned long arg)
7861 {
7862 	switch (cmd) {
7863 	case HOT_REMOVE_DISK:
7864 	case HOT_ADD_DISK:
7865 	case SET_DISK_FAULTY:
7866 	case SET_BITMAP_FILE:
7867 		/* These take in integer arg, do not convert */
7868 		break;
7869 	default:
7870 		arg = (unsigned long)compat_ptr(arg);
7871 		break;
7872 	}
7873 
7874 	return md_ioctl(bdev, mode, cmd, arg);
7875 }
7876 #endif /* CONFIG_COMPAT */
7877 
7878 static int md_set_read_only(struct block_device *bdev, bool ro)
7879 {
7880 	struct mddev *mddev = bdev->bd_disk->private_data;
7881 	int err;
7882 
7883 	err = mddev_lock(mddev);
7884 	if (err)
7885 		return err;
7886 
7887 	if (!mddev->raid_disks && !mddev->external) {
7888 		err = -ENODEV;
7889 		goto out_unlock;
7890 	}
7891 
7892 	/*
7893 	 * Transitioning to read-auto need only happen for arrays that call
7894 	 * md_write_start and which are not ready for writes yet.
7895 	 */
7896 	if (!ro && mddev->ro == MD_RDONLY && mddev->pers) {
7897 		err = restart_array(mddev);
7898 		if (err)
7899 			goto out_unlock;
7900 		mddev->ro = MD_AUTO_READ;
7901 	}
7902 
7903 out_unlock:
7904 	mddev_unlock(mddev);
7905 	return err;
7906 }
7907 
7908 static int md_open(struct gendisk *disk, blk_mode_t mode)
7909 {
7910 	struct mddev *mddev;
7911 	int err;
7912 
7913 	spin_lock(&all_mddevs_lock);
7914 	mddev = mddev_get(disk->private_data);
7915 	spin_unlock(&all_mddevs_lock);
7916 	if (!mddev)
7917 		return -ENODEV;
7918 
7919 	err = mutex_lock_interruptible(&mddev->open_mutex);
7920 	if (err)
7921 		goto out;
7922 
7923 	err = -ENODEV;
7924 	if (test_bit(MD_CLOSING, &mddev->flags))
7925 		goto out_unlock;
7926 
7927 	atomic_inc(&mddev->openers);
7928 	mutex_unlock(&mddev->open_mutex);
7929 
7930 	disk_check_media_change(disk);
7931 	return 0;
7932 
7933 out_unlock:
7934 	mutex_unlock(&mddev->open_mutex);
7935 out:
7936 	mddev_put(mddev);
7937 	return err;
7938 }
7939 
7940 static void md_release(struct gendisk *disk)
7941 {
7942 	struct mddev *mddev = disk->private_data;
7943 
7944 	BUG_ON(!mddev);
7945 	atomic_dec(&mddev->openers);
7946 	mddev_put(mddev);
7947 }
7948 
7949 static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing)
7950 {
7951 	struct mddev *mddev = disk->private_data;
7952 	unsigned int ret = 0;
7953 
7954 	if (mddev->changed)
7955 		ret = DISK_EVENT_MEDIA_CHANGE;
7956 	mddev->changed = 0;
7957 	return ret;
7958 }
7959 
7960 static void md_free_disk(struct gendisk *disk)
7961 {
7962 	struct mddev *mddev = disk->private_data;
7963 
7964 	mddev_free(mddev);
7965 }
7966 
7967 const struct block_device_operations md_fops =
7968 {
7969 	.owner		= THIS_MODULE,
7970 	.submit_bio	= md_submit_bio,
7971 	.open		= md_open,
7972 	.release	= md_release,
7973 	.ioctl		= md_ioctl,
7974 #ifdef CONFIG_COMPAT
7975 	.compat_ioctl	= md_compat_ioctl,
7976 #endif
7977 	.getgeo		= md_getgeo,
7978 	.check_events	= md_check_events,
7979 	.set_read_only	= md_set_read_only,
7980 	.free_disk	= md_free_disk,
7981 };
7982 
7983 static int md_thread(void *arg)
7984 {
7985 	struct md_thread *thread = arg;
7986 
7987 	/*
7988 	 * md_thread is a 'system-thread', it's priority should be very
7989 	 * high. We avoid resource deadlocks individually in each
7990 	 * raid personality. (RAID5 does preallocation) We also use RR and
7991 	 * the very same RT priority as kswapd, thus we will never get
7992 	 * into a priority inversion deadlock.
7993 	 *
7994 	 * we definitely have to have equal or higher priority than
7995 	 * bdflush, otherwise bdflush will deadlock if there are too
7996 	 * many dirty RAID5 blocks.
7997 	 */
7998 
7999 	allow_signal(SIGKILL);
8000 	while (!kthread_should_stop()) {
8001 
8002 		/* We need to wait INTERRUPTIBLE so that
8003 		 * we don't add to the load-average.
8004 		 * That means we need to be sure no signals are
8005 		 * pending
8006 		 */
8007 		if (signal_pending(current))
8008 			flush_signals(current);
8009 
8010 		wait_event_interruptible_timeout
8011 			(thread->wqueue,
8012 			 test_bit(THREAD_WAKEUP, &thread->flags)
8013 			 || kthread_should_stop() || kthread_should_park(),
8014 			 thread->timeout);
8015 
8016 		clear_bit(THREAD_WAKEUP, &thread->flags);
8017 		if (kthread_should_park())
8018 			kthread_parkme();
8019 		if (!kthread_should_stop())
8020 			thread->run(thread);
8021 	}
8022 
8023 	return 0;
8024 }
8025 
8026 static void md_wakeup_thread_directly(struct md_thread __rcu *thread)
8027 {
8028 	struct md_thread *t;
8029 
8030 	rcu_read_lock();
8031 	t = rcu_dereference(thread);
8032 	if (t)
8033 		wake_up_process(t->tsk);
8034 	rcu_read_unlock();
8035 }
8036 
8037 void md_wakeup_thread(struct md_thread __rcu *thread)
8038 {
8039 	struct md_thread *t;
8040 
8041 	rcu_read_lock();
8042 	t = rcu_dereference(thread);
8043 	if (t) {
8044 		pr_debug("md: waking up MD thread %s.\n", t->tsk->comm);
8045 		set_bit(THREAD_WAKEUP, &t->flags);
8046 		if (wq_has_sleeper(&t->wqueue))
8047 			wake_up(&t->wqueue);
8048 	}
8049 	rcu_read_unlock();
8050 }
8051 EXPORT_SYMBOL(md_wakeup_thread);
8052 
8053 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
8054 		struct mddev *mddev, const char *name)
8055 {
8056 	struct md_thread *thread;
8057 
8058 	thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
8059 	if (!thread)
8060 		return NULL;
8061 
8062 	init_waitqueue_head(&thread->wqueue);
8063 
8064 	thread->run = run;
8065 	thread->mddev = mddev;
8066 	thread->timeout = MAX_SCHEDULE_TIMEOUT;
8067 	thread->tsk = kthread_run(md_thread, thread,
8068 				  "%s_%s",
8069 				  mdname(thread->mddev),
8070 				  name);
8071 	if (IS_ERR(thread->tsk)) {
8072 		kfree(thread);
8073 		return NULL;
8074 	}
8075 	return thread;
8076 }
8077 EXPORT_SYMBOL(md_register_thread);
8078 
8079 void md_unregister_thread(struct mddev *mddev, struct md_thread __rcu **threadp)
8080 {
8081 	struct md_thread *thread = rcu_dereference_protected(*threadp,
8082 					lockdep_is_held(&mddev->reconfig_mutex));
8083 
8084 	if (!thread)
8085 		return;
8086 
8087 	rcu_assign_pointer(*threadp, NULL);
8088 	synchronize_rcu();
8089 
8090 	pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
8091 	kthread_stop(thread->tsk);
8092 	kfree(thread);
8093 }
8094 EXPORT_SYMBOL(md_unregister_thread);
8095 
8096 void md_error(struct mddev *mddev, struct md_rdev *rdev)
8097 {
8098 	if (!rdev || test_bit(Faulty, &rdev->flags))
8099 		return;
8100 
8101 	if (!mddev->pers || !mddev->pers->error_handler)
8102 		return;
8103 	mddev->pers->error_handler(mddev, rdev);
8104 
8105 	if (mddev->pers->level == 0)
8106 		return;
8107 
8108 	if (mddev->degraded && !test_bit(MD_BROKEN, &mddev->flags))
8109 		set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
8110 	sysfs_notify_dirent_safe(rdev->sysfs_state);
8111 	set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8112 	if (!test_bit(MD_BROKEN, &mddev->flags)) {
8113 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8114 		md_wakeup_thread(mddev->thread);
8115 	}
8116 	if (mddev->event_work.func)
8117 		queue_work(md_misc_wq, &mddev->event_work);
8118 	md_new_event();
8119 }
8120 EXPORT_SYMBOL(md_error);
8121 
8122 /* seq_file implementation /proc/mdstat */
8123 
8124 static void status_unused(struct seq_file *seq)
8125 {
8126 	int i = 0;
8127 	struct md_rdev *rdev;
8128 
8129 	seq_printf(seq, "unused devices: ");
8130 
8131 	list_for_each_entry(rdev, &pending_raid_disks, same_set) {
8132 		i++;
8133 		seq_printf(seq, "%pg ", rdev->bdev);
8134 	}
8135 	if (!i)
8136 		seq_printf(seq, "<none>");
8137 
8138 	seq_printf(seq, "\n");
8139 }
8140 
8141 static void status_personalities(struct seq_file *seq)
8142 {
8143 	struct md_personality *pers;
8144 
8145 	seq_puts(seq, "Personalities : ");
8146 	spin_lock(&pers_lock);
8147 	list_for_each_entry(pers, &pers_list, list)
8148 		seq_printf(seq, "[%s] ", pers->name);
8149 
8150 	spin_unlock(&pers_lock);
8151 	seq_puts(seq, "\n");
8152 }
8153 
8154 static int status_resync(struct seq_file *seq, struct mddev *mddev)
8155 {
8156 	sector_t max_sectors, resync, res;
8157 	unsigned long dt, db = 0;
8158 	sector_t rt, curr_mark_cnt, resync_mark_cnt;
8159 	int scale, recovery_active;
8160 	unsigned int per_milli;
8161 
8162 	if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8163 	    test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8164 		max_sectors = mddev->resync_max_sectors;
8165 	else
8166 		max_sectors = mddev->dev_sectors;
8167 
8168 	resync = mddev->curr_resync;
8169 	if (resync < MD_RESYNC_ACTIVE) {
8170 		if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8171 			/* Still cleaning up */
8172 			resync = max_sectors;
8173 	} else if (resync > max_sectors) {
8174 		resync = max_sectors;
8175 	} else {
8176 		res = atomic_read(&mddev->recovery_active);
8177 		/*
8178 		 * Resync has started, but the subtraction has overflowed or
8179 		 * yielded one of the special values. Force it to active to
8180 		 * ensure the status reports an active resync.
8181 		 */
8182 		if (resync < res || resync - res < MD_RESYNC_ACTIVE)
8183 			resync = MD_RESYNC_ACTIVE;
8184 		else
8185 			resync -= res;
8186 	}
8187 
8188 	if (resync == MD_RESYNC_NONE) {
8189 		if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8190 			struct md_rdev *rdev;
8191 
8192 			rdev_for_each(rdev, mddev)
8193 				if (rdev->raid_disk >= 0 &&
8194 				    !test_bit(Faulty, &rdev->flags) &&
8195 				    rdev->recovery_offset != MaxSector &&
8196 				    rdev->recovery_offset) {
8197 					seq_printf(seq, "\trecover=REMOTE");
8198 					return 1;
8199 				}
8200 			if (mddev->reshape_position != MaxSector)
8201 				seq_printf(seq, "\treshape=REMOTE");
8202 			else
8203 				seq_printf(seq, "\tresync=REMOTE");
8204 			return 1;
8205 		}
8206 		if (mddev->recovery_cp < MaxSector) {
8207 			seq_printf(seq, "\tresync=PENDING");
8208 			return 1;
8209 		}
8210 		return 0;
8211 	}
8212 	if (resync < MD_RESYNC_ACTIVE) {
8213 		seq_printf(seq, "\tresync=DELAYED");
8214 		return 1;
8215 	}
8216 
8217 	WARN_ON(max_sectors == 0);
8218 	/* Pick 'scale' such that (resync>>scale)*1000 will fit
8219 	 * in a sector_t, and (max_sectors>>scale) will fit in a
8220 	 * u32, as those are the requirements for sector_div.
8221 	 * Thus 'scale' must be at least 10
8222 	 */
8223 	scale = 10;
8224 	if (sizeof(sector_t) > sizeof(unsigned long)) {
8225 		while ( max_sectors/2 > (1ULL<<(scale+32)))
8226 			scale++;
8227 	}
8228 	res = (resync>>scale)*1000;
8229 	sector_div(res, (u32)((max_sectors>>scale)+1));
8230 
8231 	per_milli = res;
8232 	{
8233 		int i, x = per_milli/50, y = 20-x;
8234 		seq_printf(seq, "[");
8235 		for (i = 0; i < x; i++)
8236 			seq_printf(seq, "=");
8237 		seq_printf(seq, ">");
8238 		for (i = 0; i < y; i++)
8239 			seq_printf(seq, ".");
8240 		seq_printf(seq, "] ");
8241 	}
8242 	seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
8243 		   (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8244 		    "reshape" :
8245 		    (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8246 		     "check" :
8247 		     (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8248 		      "resync" : "recovery"))),
8249 		   per_milli/10, per_milli % 10,
8250 		   (unsigned long long) resync/2,
8251 		   (unsigned long long) max_sectors/2);
8252 
8253 	/*
8254 	 * dt: time from mark until now
8255 	 * db: blocks written from mark until now
8256 	 * rt: remaining time
8257 	 *
8258 	 * rt is a sector_t, which is always 64bit now. We are keeping
8259 	 * the original algorithm, but it is not really necessary.
8260 	 *
8261 	 * Original algorithm:
8262 	 *   So we divide before multiply in case it is 32bit and close
8263 	 *   to the limit.
8264 	 *   We scale the divisor (db) by 32 to avoid losing precision
8265 	 *   near the end of resync when the number of remaining sectors
8266 	 *   is close to 'db'.
8267 	 *   We then divide rt by 32 after multiplying by db to compensate.
8268 	 *   The '+1' avoids division by zero if db is very small.
8269 	 */
8270 	dt = ((jiffies - mddev->resync_mark) / HZ);
8271 	if (!dt) dt++;
8272 
8273 	curr_mark_cnt = mddev->curr_mark_cnt;
8274 	recovery_active = atomic_read(&mddev->recovery_active);
8275 	resync_mark_cnt = mddev->resync_mark_cnt;
8276 
8277 	if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8278 		db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
8279 
8280 	rt = max_sectors - resync;    /* number of remaining sectors */
8281 	rt = div64_u64(rt, db/32+1);
8282 	rt *= dt;
8283 	rt >>= 5;
8284 
8285 	seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8286 		   ((unsigned long)rt % 60)/6);
8287 
8288 	seq_printf(seq, " speed=%ldK/sec", db/2/dt);
8289 	return 1;
8290 }
8291 
8292 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8293 	__acquires(&all_mddevs_lock)
8294 {
8295 	seq->poll_event = atomic_read(&md_event_count);
8296 	spin_lock(&all_mddevs_lock);
8297 
8298 	return seq_list_start_head(&all_mddevs, *pos);
8299 }
8300 
8301 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8302 {
8303 	return seq_list_next(v, &all_mddevs, pos);
8304 }
8305 
8306 static void md_seq_stop(struct seq_file *seq, void *v)
8307 	__releases(&all_mddevs_lock)
8308 {
8309 	spin_unlock(&all_mddevs_lock);
8310 }
8311 
8312 static void md_bitmap_status(struct seq_file *seq, struct mddev *mddev)
8313 {
8314 	struct md_bitmap_stats stats;
8315 	unsigned long used_pages;
8316 	unsigned long chunk_kb;
8317 	int err;
8318 
8319 	err = mddev->bitmap_ops->get_stats(mddev->bitmap, &stats);
8320 	if (err)
8321 		return;
8322 
8323 	chunk_kb = mddev->bitmap_info.chunksize >> 10;
8324 	used_pages = stats.pages - stats.missing_pages;
8325 
8326 	seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], %lu%s chunk",
8327 		   used_pages, stats.pages, used_pages << (PAGE_SHIFT - 10),
8328 		   chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
8329 		   chunk_kb ? "KB" : "B");
8330 
8331 	if (stats.file) {
8332 		seq_puts(seq, ", file: ");
8333 		seq_file_path(seq, stats.file, " \t\n");
8334 	}
8335 
8336 	seq_putc(seq, '\n');
8337 }
8338 
8339 static int md_seq_show(struct seq_file *seq, void *v)
8340 {
8341 	struct mddev *mddev;
8342 	sector_t sectors;
8343 	struct md_rdev *rdev;
8344 
8345 	if (v == &all_mddevs) {
8346 		status_personalities(seq);
8347 		if (list_empty(&all_mddevs))
8348 			status_unused(seq);
8349 		return 0;
8350 	}
8351 
8352 	mddev = list_entry(v, struct mddev, all_mddevs);
8353 	if (!mddev_get(mddev))
8354 		return 0;
8355 
8356 	spin_unlock(&all_mddevs_lock);
8357 	spin_lock(&mddev->lock);
8358 	if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8359 		seq_printf(seq, "%s : ", mdname(mddev));
8360 		if (mddev->pers) {
8361 			if (test_bit(MD_BROKEN, &mddev->flags))
8362 				seq_printf(seq, "broken");
8363 			else
8364 				seq_printf(seq, "active");
8365 			if (mddev->ro == MD_RDONLY)
8366 				seq_printf(seq, " (read-only)");
8367 			if (mddev->ro == MD_AUTO_READ)
8368 				seq_printf(seq, " (auto-read-only)");
8369 			seq_printf(seq, " %s", mddev->pers->name);
8370 		} else {
8371 			seq_printf(seq, "inactive");
8372 		}
8373 
8374 		sectors = 0;
8375 		rcu_read_lock();
8376 		rdev_for_each_rcu(rdev, mddev) {
8377 			seq_printf(seq, " %pg[%d]", rdev->bdev, rdev->desc_nr);
8378 
8379 			if (test_bit(WriteMostly, &rdev->flags))
8380 				seq_printf(seq, "(W)");
8381 			if (test_bit(Journal, &rdev->flags))
8382 				seq_printf(seq, "(J)");
8383 			if (test_bit(Faulty, &rdev->flags)) {
8384 				seq_printf(seq, "(F)");
8385 				continue;
8386 			}
8387 			if (rdev->raid_disk < 0)
8388 				seq_printf(seq, "(S)"); /* spare */
8389 			if (test_bit(Replacement, &rdev->flags))
8390 				seq_printf(seq, "(R)");
8391 			sectors += rdev->sectors;
8392 		}
8393 		rcu_read_unlock();
8394 
8395 		if (!list_empty(&mddev->disks)) {
8396 			if (mddev->pers)
8397 				seq_printf(seq, "\n      %llu blocks",
8398 					   (unsigned long long)
8399 					   mddev->array_sectors / 2);
8400 			else
8401 				seq_printf(seq, "\n      %llu blocks",
8402 					   (unsigned long long)sectors / 2);
8403 		}
8404 		if (mddev->persistent) {
8405 			if (mddev->major_version != 0 ||
8406 			    mddev->minor_version != 90) {
8407 				seq_printf(seq," super %d.%d",
8408 					   mddev->major_version,
8409 					   mddev->minor_version);
8410 			}
8411 		} else if (mddev->external)
8412 			seq_printf(seq, " super external:%s",
8413 				   mddev->metadata_type);
8414 		else
8415 			seq_printf(seq, " super non-persistent");
8416 
8417 		if (mddev->pers) {
8418 			mddev->pers->status(seq, mddev);
8419 			seq_printf(seq, "\n      ");
8420 			if (mddev->pers->sync_request) {
8421 				if (status_resync(seq, mddev))
8422 					seq_printf(seq, "\n      ");
8423 			}
8424 		} else
8425 			seq_printf(seq, "\n       ");
8426 
8427 		md_bitmap_status(seq, mddev);
8428 
8429 		seq_printf(seq, "\n");
8430 	}
8431 	spin_unlock(&mddev->lock);
8432 	spin_lock(&all_mddevs_lock);
8433 
8434 	if (mddev == list_last_entry(&all_mddevs, struct mddev, all_mddevs))
8435 		status_unused(seq);
8436 
8437 	if (atomic_dec_and_test(&mddev->active))
8438 		__mddev_put(mddev);
8439 
8440 	return 0;
8441 }
8442 
8443 static const struct seq_operations md_seq_ops = {
8444 	.start  = md_seq_start,
8445 	.next   = md_seq_next,
8446 	.stop   = md_seq_stop,
8447 	.show   = md_seq_show,
8448 };
8449 
8450 static int md_seq_open(struct inode *inode, struct file *file)
8451 {
8452 	struct seq_file *seq;
8453 	int error;
8454 
8455 	error = seq_open(file, &md_seq_ops);
8456 	if (error)
8457 		return error;
8458 
8459 	seq = file->private_data;
8460 	seq->poll_event = atomic_read(&md_event_count);
8461 	return error;
8462 }
8463 
8464 static int md_unloading;
8465 static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
8466 {
8467 	struct seq_file *seq = filp->private_data;
8468 	__poll_t mask;
8469 
8470 	if (md_unloading)
8471 		return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
8472 	poll_wait(filp, &md_event_waiters, wait);
8473 
8474 	/* always allow read */
8475 	mask = EPOLLIN | EPOLLRDNORM;
8476 
8477 	if (seq->poll_event != atomic_read(&md_event_count))
8478 		mask |= EPOLLERR | EPOLLPRI;
8479 	return mask;
8480 }
8481 
8482 static const struct proc_ops mdstat_proc_ops = {
8483 	.proc_open	= md_seq_open,
8484 	.proc_read	= seq_read,
8485 	.proc_lseek	= seq_lseek,
8486 	.proc_release	= seq_release,
8487 	.proc_poll	= mdstat_poll,
8488 };
8489 
8490 int register_md_personality(struct md_personality *p)
8491 {
8492 	pr_debug("md: %s personality registered for level %d\n",
8493 		 p->name, p->level);
8494 	spin_lock(&pers_lock);
8495 	list_add_tail(&p->list, &pers_list);
8496 	spin_unlock(&pers_lock);
8497 	return 0;
8498 }
8499 EXPORT_SYMBOL(register_md_personality);
8500 
8501 int unregister_md_personality(struct md_personality *p)
8502 {
8503 	pr_debug("md: %s personality unregistered\n", p->name);
8504 	spin_lock(&pers_lock);
8505 	list_del_init(&p->list);
8506 	spin_unlock(&pers_lock);
8507 	return 0;
8508 }
8509 EXPORT_SYMBOL(unregister_md_personality);
8510 
8511 int register_md_cluster_operations(const struct md_cluster_operations *ops,
8512 				   struct module *module)
8513 {
8514 	int ret = 0;
8515 	spin_lock(&pers_lock);
8516 	if (md_cluster_ops != NULL)
8517 		ret = -EALREADY;
8518 	else {
8519 		md_cluster_ops = ops;
8520 		md_cluster_mod = module;
8521 	}
8522 	spin_unlock(&pers_lock);
8523 	return ret;
8524 }
8525 EXPORT_SYMBOL(register_md_cluster_operations);
8526 
8527 int unregister_md_cluster_operations(void)
8528 {
8529 	spin_lock(&pers_lock);
8530 	md_cluster_ops = NULL;
8531 	spin_unlock(&pers_lock);
8532 	return 0;
8533 }
8534 EXPORT_SYMBOL(unregister_md_cluster_operations);
8535 
8536 int md_setup_cluster(struct mddev *mddev, int nodes)
8537 {
8538 	int ret;
8539 	if (!md_cluster_ops)
8540 		request_module("md-cluster");
8541 	spin_lock(&pers_lock);
8542 	/* ensure module won't be unloaded */
8543 	if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
8544 		pr_warn("can't find md-cluster module or get its reference.\n");
8545 		spin_unlock(&pers_lock);
8546 		return -ENOENT;
8547 	}
8548 	spin_unlock(&pers_lock);
8549 
8550 	ret = md_cluster_ops->join(mddev, nodes);
8551 	if (!ret)
8552 		mddev->safemode_delay = 0;
8553 	return ret;
8554 }
8555 
8556 void md_cluster_stop(struct mddev *mddev)
8557 {
8558 	if (!md_cluster_ops)
8559 		return;
8560 	md_cluster_ops->leave(mddev);
8561 	module_put(md_cluster_mod);
8562 }
8563 
8564 static int is_mddev_idle(struct mddev *mddev, int init)
8565 {
8566 	struct md_rdev *rdev;
8567 	int idle;
8568 	int curr_events;
8569 
8570 	idle = 1;
8571 	rcu_read_lock();
8572 	rdev_for_each_rcu(rdev, mddev) {
8573 		struct gendisk *disk = rdev->bdev->bd_disk;
8574 
8575 		if (!init && !blk_queue_io_stat(disk->queue))
8576 			continue;
8577 
8578 		curr_events = (int)part_stat_read_accum(disk->part0, sectors) -
8579 			      atomic_read(&disk->sync_io);
8580 		/* sync IO will cause sync_io to increase before the disk_stats
8581 		 * as sync_io is counted when a request starts, and
8582 		 * disk_stats is counted when it completes.
8583 		 * So resync activity will cause curr_events to be smaller than
8584 		 * when there was no such activity.
8585 		 * non-sync IO will cause disk_stat to increase without
8586 		 * increasing sync_io so curr_events will (eventually)
8587 		 * be larger than it was before.  Once it becomes
8588 		 * substantially larger, the test below will cause
8589 		 * the array to appear non-idle, and resync will slow
8590 		 * down.
8591 		 * If there is a lot of outstanding resync activity when
8592 		 * we set last_event to curr_events, then all that activity
8593 		 * completing might cause the array to appear non-idle
8594 		 * and resync will be slowed down even though there might
8595 		 * not have been non-resync activity.  This will only
8596 		 * happen once though.  'last_events' will soon reflect
8597 		 * the state where there is little or no outstanding
8598 		 * resync requests, and further resync activity will
8599 		 * always make curr_events less than last_events.
8600 		 *
8601 		 */
8602 		if (init || curr_events - rdev->last_events > 64) {
8603 			rdev->last_events = curr_events;
8604 			idle = 0;
8605 		}
8606 	}
8607 	rcu_read_unlock();
8608 	return idle;
8609 }
8610 
8611 void md_done_sync(struct mddev *mddev, int blocks, int ok)
8612 {
8613 	/* another "blocks" (512byte) blocks have been synced */
8614 	atomic_sub(blocks, &mddev->recovery_active);
8615 	wake_up(&mddev->recovery_wait);
8616 	if (!ok) {
8617 		set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8618 		set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
8619 		md_wakeup_thread(mddev->thread);
8620 		// stop recovery, signal do_sync ....
8621 	}
8622 }
8623 EXPORT_SYMBOL(md_done_sync);
8624 
8625 /* md_write_start(mddev, bi)
8626  * If we need to update some array metadata (e.g. 'active' flag
8627  * in superblock) before writing, schedule a superblock update
8628  * and wait for it to complete.
8629  * A return value of 'false' means that the write wasn't recorded
8630  * and cannot proceed as the array is being suspend.
8631  */
8632 void md_write_start(struct mddev *mddev, struct bio *bi)
8633 {
8634 	int did_change = 0;
8635 
8636 	if (bio_data_dir(bi) != WRITE)
8637 		return;
8638 
8639 	BUG_ON(mddev->ro == MD_RDONLY);
8640 	if (mddev->ro == MD_AUTO_READ) {
8641 		/* need to switch to read/write */
8642 		mddev->ro = MD_RDWR;
8643 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8644 		md_wakeup_thread(mddev->thread);
8645 		md_wakeup_thread(mddev->sync_thread);
8646 		did_change = 1;
8647 	}
8648 	rcu_read_lock();
8649 	percpu_ref_get(&mddev->writes_pending);
8650 	smp_mb(); /* Match smp_mb in set_in_sync() */
8651 	if (mddev->safemode == 1)
8652 		mddev->safemode = 0;
8653 	/* sync_checkers is always 0 when writes_pending is in per-cpu mode */
8654 	if (mddev->in_sync || mddev->sync_checkers) {
8655 		spin_lock(&mddev->lock);
8656 		if (mddev->in_sync) {
8657 			mddev->in_sync = 0;
8658 			set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8659 			set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8660 			md_wakeup_thread(mddev->thread);
8661 			did_change = 1;
8662 		}
8663 		spin_unlock(&mddev->lock);
8664 	}
8665 	rcu_read_unlock();
8666 	if (did_change)
8667 		sysfs_notify_dirent_safe(mddev->sysfs_state);
8668 	if (!mddev->has_superblocks)
8669 		return;
8670 	wait_event(mddev->sb_wait,
8671 		   !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
8672 }
8673 EXPORT_SYMBOL(md_write_start);
8674 
8675 /* md_write_inc can only be called when md_write_start() has
8676  * already been called at least once of the current request.
8677  * It increments the counter and is useful when a single request
8678  * is split into several parts.  Each part causes an increment and
8679  * so needs a matching md_write_end().
8680  * Unlike md_write_start(), it is safe to call md_write_inc() inside
8681  * a spinlocked region.
8682  */
8683 void md_write_inc(struct mddev *mddev, struct bio *bi)
8684 {
8685 	if (bio_data_dir(bi) != WRITE)
8686 		return;
8687 	WARN_ON_ONCE(mddev->in_sync || !md_is_rdwr(mddev));
8688 	percpu_ref_get(&mddev->writes_pending);
8689 }
8690 EXPORT_SYMBOL(md_write_inc);
8691 
8692 void md_write_end(struct mddev *mddev)
8693 {
8694 	percpu_ref_put(&mddev->writes_pending);
8695 
8696 	if (mddev->safemode == 2)
8697 		md_wakeup_thread(mddev->thread);
8698 	else if (mddev->safemode_delay)
8699 		/* The roundup() ensures this only performs locking once
8700 		 * every ->safemode_delay jiffies
8701 		 */
8702 		mod_timer(&mddev->safemode_timer,
8703 			  roundup(jiffies, mddev->safemode_delay) +
8704 			  mddev->safemode_delay);
8705 }
8706 
8707 EXPORT_SYMBOL(md_write_end);
8708 
8709 /* This is used by raid0 and raid10 */
8710 void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
8711 			struct bio *bio, sector_t start, sector_t size)
8712 {
8713 	struct bio *discard_bio = NULL;
8714 
8715 	if (__blkdev_issue_discard(rdev->bdev, start, size, GFP_NOIO,
8716 			&discard_bio) || !discard_bio)
8717 		return;
8718 
8719 	bio_chain(discard_bio, bio);
8720 	bio_clone_blkg_association(discard_bio, bio);
8721 	mddev_trace_remap(mddev, discard_bio, bio->bi_iter.bi_sector);
8722 	submit_bio_noacct(discard_bio);
8723 }
8724 EXPORT_SYMBOL_GPL(md_submit_discard_bio);
8725 
8726 static void md_end_clone_io(struct bio *bio)
8727 {
8728 	struct md_io_clone *md_io_clone = bio->bi_private;
8729 	struct bio *orig_bio = md_io_clone->orig_bio;
8730 	struct mddev *mddev = md_io_clone->mddev;
8731 
8732 	if (bio->bi_status && !orig_bio->bi_status)
8733 		orig_bio->bi_status = bio->bi_status;
8734 
8735 	if (md_io_clone->start_time)
8736 		bio_end_io_acct(orig_bio, md_io_clone->start_time);
8737 
8738 	bio_put(bio);
8739 	bio_endio(orig_bio);
8740 	percpu_ref_put(&mddev->active_io);
8741 }
8742 
8743 static void md_clone_bio(struct mddev *mddev, struct bio **bio)
8744 {
8745 	struct block_device *bdev = (*bio)->bi_bdev;
8746 	struct md_io_clone *md_io_clone;
8747 	struct bio *clone =
8748 		bio_alloc_clone(bdev, *bio, GFP_NOIO, &mddev->io_clone_set);
8749 
8750 	md_io_clone = container_of(clone, struct md_io_clone, bio_clone);
8751 	md_io_clone->orig_bio = *bio;
8752 	md_io_clone->mddev = mddev;
8753 	if (blk_queue_io_stat(bdev->bd_disk->queue))
8754 		md_io_clone->start_time = bio_start_io_acct(*bio);
8755 
8756 	clone->bi_end_io = md_end_clone_io;
8757 	clone->bi_private = md_io_clone;
8758 	*bio = clone;
8759 }
8760 
8761 void md_account_bio(struct mddev *mddev, struct bio **bio)
8762 {
8763 	percpu_ref_get(&mddev->active_io);
8764 	md_clone_bio(mddev, bio);
8765 }
8766 EXPORT_SYMBOL_GPL(md_account_bio);
8767 
8768 void md_free_cloned_bio(struct bio *bio)
8769 {
8770 	struct md_io_clone *md_io_clone = bio->bi_private;
8771 	struct bio *orig_bio = md_io_clone->orig_bio;
8772 	struct mddev *mddev = md_io_clone->mddev;
8773 
8774 	if (bio->bi_status && !orig_bio->bi_status)
8775 		orig_bio->bi_status = bio->bi_status;
8776 
8777 	if (md_io_clone->start_time)
8778 		bio_end_io_acct(orig_bio, md_io_clone->start_time);
8779 
8780 	bio_put(bio);
8781 	percpu_ref_put(&mddev->active_io);
8782 }
8783 EXPORT_SYMBOL_GPL(md_free_cloned_bio);
8784 
8785 /* md_allow_write(mddev)
8786  * Calling this ensures that the array is marked 'active' so that writes
8787  * may proceed without blocking.  It is important to call this before
8788  * attempting a GFP_KERNEL allocation while holding the mddev lock.
8789  * Must be called with mddev_lock held.
8790  */
8791 void md_allow_write(struct mddev *mddev)
8792 {
8793 	if (!mddev->pers)
8794 		return;
8795 	if (!md_is_rdwr(mddev))
8796 		return;
8797 	if (!mddev->pers->sync_request)
8798 		return;
8799 
8800 	spin_lock(&mddev->lock);
8801 	if (mddev->in_sync) {
8802 		mddev->in_sync = 0;
8803 		set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8804 		set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
8805 		if (mddev->safemode_delay &&
8806 		    mddev->safemode == 0)
8807 			mddev->safemode = 1;
8808 		spin_unlock(&mddev->lock);
8809 		md_update_sb(mddev, 0);
8810 		sysfs_notify_dirent_safe(mddev->sysfs_state);
8811 		/* wait for the dirty state to be recorded in the metadata */
8812 		wait_event(mddev->sb_wait,
8813 			   !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
8814 	} else
8815 		spin_unlock(&mddev->lock);
8816 }
8817 EXPORT_SYMBOL_GPL(md_allow_write);
8818 
8819 static sector_t md_sync_max_sectors(struct mddev *mddev,
8820 				    enum sync_action action)
8821 {
8822 	switch (action) {
8823 	case ACTION_RESYNC:
8824 	case ACTION_CHECK:
8825 	case ACTION_REPAIR:
8826 		atomic64_set(&mddev->resync_mismatches, 0);
8827 		fallthrough;
8828 	case ACTION_RESHAPE:
8829 		return mddev->resync_max_sectors;
8830 	case ACTION_RECOVER:
8831 		return mddev->dev_sectors;
8832 	default:
8833 		return 0;
8834 	}
8835 }
8836 
8837 static sector_t md_sync_position(struct mddev *mddev, enum sync_action action)
8838 {
8839 	sector_t start = 0;
8840 	struct md_rdev *rdev;
8841 
8842 	switch (action) {
8843 	case ACTION_CHECK:
8844 	case ACTION_REPAIR:
8845 		return mddev->resync_min;
8846 	case ACTION_RESYNC:
8847 		if (!mddev->bitmap)
8848 			return mddev->recovery_cp;
8849 		return 0;
8850 	case ACTION_RESHAPE:
8851 		/*
8852 		 * If the original node aborts reshaping then we continue the
8853 		 * reshaping, so set again to avoid restart reshape from the
8854 		 * first beginning
8855 		 */
8856 		if (mddev_is_clustered(mddev) &&
8857 		    mddev->reshape_position != MaxSector)
8858 			return mddev->reshape_position;
8859 		return 0;
8860 	case ACTION_RECOVER:
8861 		start = MaxSector;
8862 		rcu_read_lock();
8863 		rdev_for_each_rcu(rdev, mddev)
8864 			if (rdev->raid_disk >= 0 &&
8865 			    !test_bit(Journal, &rdev->flags) &&
8866 			    !test_bit(Faulty, &rdev->flags) &&
8867 			    !test_bit(In_sync, &rdev->flags) &&
8868 			    rdev->recovery_offset < start)
8869 				start = rdev->recovery_offset;
8870 		rcu_read_unlock();
8871 
8872 		/* If there is a bitmap, we need to make sure all
8873 		 * writes that started before we added a spare
8874 		 * complete before we start doing a recovery.
8875 		 * Otherwise the write might complete and (via
8876 		 * bitmap_endwrite) set a bit in the bitmap after the
8877 		 * recovery has checked that bit and skipped that
8878 		 * region.
8879 		 */
8880 		if (mddev->bitmap) {
8881 			mddev->pers->quiesce(mddev, 1);
8882 			mddev->pers->quiesce(mddev, 0);
8883 		}
8884 		return start;
8885 	default:
8886 		return MaxSector;
8887 	}
8888 }
8889 
8890 #define SYNC_MARKS	10
8891 #define	SYNC_MARK_STEP	(3*HZ)
8892 #define UPDATE_FREQUENCY (5*60*HZ)
8893 void md_do_sync(struct md_thread *thread)
8894 {
8895 	struct mddev *mddev = thread->mddev;
8896 	struct mddev *mddev2;
8897 	unsigned int currspeed = 0, window;
8898 	sector_t max_sectors,j, io_sectors, recovery_done;
8899 	unsigned long mark[SYNC_MARKS];
8900 	unsigned long update_time;
8901 	sector_t mark_cnt[SYNC_MARKS];
8902 	int last_mark,m;
8903 	sector_t last_check;
8904 	int skipped = 0;
8905 	struct md_rdev *rdev;
8906 	enum sync_action action;
8907 	const char *desc;
8908 	struct blk_plug plug;
8909 	int ret;
8910 
8911 	/* just incase thread restarts... */
8912 	if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8913 		return;
8914 
8915 	if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8916 		goto skip;
8917 
8918 	if (test_bit(MD_RECOVERY_WAIT, &mddev->recovery) ||
8919 	    !md_is_rdwr(mddev)) {/* never try to sync a read-only array */
8920 		set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8921 		goto skip;
8922 	}
8923 
8924 	if (mddev_is_clustered(mddev)) {
8925 		ret = md_cluster_ops->resync_start(mddev);
8926 		if (ret)
8927 			goto skip;
8928 
8929 		set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
8930 		if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8931 			test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8932 			test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8933 		     && ((unsigned long long)mddev->curr_resync_completed
8934 			 < (unsigned long long)mddev->resync_max_sectors))
8935 			goto skip;
8936 	}
8937 
8938 	action = md_sync_action(mddev);
8939 	desc = md_sync_action_name(action);
8940 	mddev->last_sync_action = action;
8941 
8942 	/*
8943 	 * Before starting a resync we must have set curr_resync to
8944 	 * 2, and then checked that every "conflicting" array has curr_resync
8945 	 * less than ours.  When we find one that is the same or higher
8946 	 * we wait on resync_wait.  To avoid deadlock, we reduce curr_resync
8947 	 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8948 	 * This will mean we have to start checking from the beginning again.
8949 	 *
8950 	 */
8951 	if (mddev_is_clustered(mddev))
8952 		md_cluster_ops->resync_start_notify(mddev);
8953 	do {
8954 		int mddev2_minor = -1;
8955 		mddev->curr_resync = MD_RESYNC_DELAYED;
8956 
8957 	try_again:
8958 		if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8959 			goto skip;
8960 		spin_lock(&all_mddevs_lock);
8961 		list_for_each_entry(mddev2, &all_mddevs, all_mddevs) {
8962 			if (test_bit(MD_DELETED, &mddev2->flags))
8963 				continue;
8964 			if (mddev2 == mddev)
8965 				continue;
8966 			if (!mddev->parallel_resync
8967 			&&  mddev2->curr_resync
8968 			&&  match_mddev_units(mddev, mddev2)) {
8969 				DEFINE_WAIT(wq);
8970 				if (mddev < mddev2 &&
8971 				    mddev->curr_resync == MD_RESYNC_DELAYED) {
8972 					/* arbitrarily yield */
8973 					mddev->curr_resync = MD_RESYNC_YIELDED;
8974 					wake_up(&resync_wait);
8975 				}
8976 				if (mddev > mddev2 &&
8977 				    mddev->curr_resync == MD_RESYNC_YIELDED)
8978 					/* no need to wait here, we can wait the next
8979 					 * time 'round when curr_resync == 2
8980 					 */
8981 					continue;
8982 				/* We need to wait 'interruptible' so as not to
8983 				 * contribute to the load average, and not to
8984 				 * be caught by 'softlockup'
8985 				 */
8986 				prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
8987 				if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8988 				    mddev2->curr_resync >= mddev->curr_resync) {
8989 					if (mddev2_minor != mddev2->md_minor) {
8990 						mddev2_minor = mddev2->md_minor;
8991 						pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8992 							desc, mdname(mddev),
8993 							mdname(mddev2));
8994 					}
8995 					spin_unlock(&all_mddevs_lock);
8996 
8997 					if (signal_pending(current))
8998 						flush_signals(current);
8999 					schedule();
9000 					finish_wait(&resync_wait, &wq);
9001 					goto try_again;
9002 				}
9003 				finish_wait(&resync_wait, &wq);
9004 			}
9005 		}
9006 		spin_unlock(&all_mddevs_lock);
9007 	} while (mddev->curr_resync < MD_RESYNC_DELAYED);
9008 
9009 	max_sectors = md_sync_max_sectors(mddev, action);
9010 	j = md_sync_position(mddev, action);
9011 
9012 	pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
9013 	pr_debug("md: minimum _guaranteed_  speed: %d KB/sec/disk.\n", speed_min(mddev));
9014 	pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
9015 		 speed_max(mddev), desc);
9016 
9017 	is_mddev_idle(mddev, 1); /* this initializes IO event counters */
9018 
9019 	io_sectors = 0;
9020 	for (m = 0; m < SYNC_MARKS; m++) {
9021 		mark[m] = jiffies;
9022 		mark_cnt[m] = io_sectors;
9023 	}
9024 	last_mark = 0;
9025 	mddev->resync_mark = mark[last_mark];
9026 	mddev->resync_mark_cnt = mark_cnt[last_mark];
9027 
9028 	/*
9029 	 * Tune reconstruction:
9030 	 */
9031 	window = 32 * (PAGE_SIZE / 512);
9032 	pr_debug("md: using %dk window, over a total of %lluk.\n",
9033 		 window/2, (unsigned long long)max_sectors/2);
9034 
9035 	atomic_set(&mddev->recovery_active, 0);
9036 	last_check = 0;
9037 
9038 	if (j >= MD_RESYNC_ACTIVE) {
9039 		pr_debug("md: resuming %s of %s from checkpoint.\n",
9040 			 desc, mdname(mddev));
9041 		mddev->curr_resync = j;
9042 	} else
9043 		mddev->curr_resync = MD_RESYNC_ACTIVE; /* no longer delayed */
9044 	mddev->curr_resync_completed = j;
9045 	sysfs_notify_dirent_safe(mddev->sysfs_completed);
9046 	md_new_event();
9047 	update_time = jiffies;
9048 
9049 	blk_start_plug(&plug);
9050 	while (j < max_sectors) {
9051 		sector_t sectors;
9052 
9053 		skipped = 0;
9054 
9055 		if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9056 		    ((mddev->curr_resync > mddev->curr_resync_completed &&
9057 		      (mddev->curr_resync - mddev->curr_resync_completed)
9058 		      > (max_sectors >> 4)) ||
9059 		     time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
9060 		     (j - mddev->curr_resync_completed)*2
9061 		     >= mddev->resync_max - mddev->curr_resync_completed ||
9062 		     mddev->curr_resync_completed > mddev->resync_max
9063 			    )) {
9064 			/* time to update curr_resync_completed */
9065 			wait_event(mddev->recovery_wait,
9066 				   atomic_read(&mddev->recovery_active) == 0);
9067 			mddev->curr_resync_completed = j;
9068 			if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
9069 			    j > mddev->recovery_cp)
9070 				mddev->recovery_cp = j;
9071 			update_time = jiffies;
9072 			set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
9073 			sysfs_notify_dirent_safe(mddev->sysfs_completed);
9074 		}
9075 
9076 		while (j >= mddev->resync_max &&
9077 		       !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9078 			/* As this condition is controlled by user-space,
9079 			 * we can block indefinitely, so use '_interruptible'
9080 			 * to avoid triggering warnings.
9081 			 */
9082 			flush_signals(current); /* just in case */
9083 			wait_event_interruptible(mddev->recovery_wait,
9084 						 mddev->resync_max > j
9085 						 || test_bit(MD_RECOVERY_INTR,
9086 							     &mddev->recovery));
9087 		}
9088 
9089 		if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9090 			break;
9091 
9092 		sectors = mddev->pers->sync_request(mddev, j, max_sectors,
9093 						    &skipped);
9094 		if (sectors == 0) {
9095 			set_bit(MD_RECOVERY_INTR, &mddev->recovery);
9096 			break;
9097 		}
9098 
9099 		if (!skipped) { /* actual IO requested */
9100 			io_sectors += sectors;
9101 			atomic_add(sectors, &mddev->recovery_active);
9102 		}
9103 
9104 		if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9105 			break;
9106 
9107 		j += sectors;
9108 		if (j > max_sectors)
9109 			/* when skipping, extra large numbers can be returned. */
9110 			j = max_sectors;
9111 		if (j >= MD_RESYNC_ACTIVE)
9112 			mddev->curr_resync = j;
9113 		mddev->curr_mark_cnt = io_sectors;
9114 		if (last_check == 0)
9115 			/* this is the earliest that rebuild will be
9116 			 * visible in /proc/mdstat
9117 			 */
9118 			md_new_event();
9119 
9120 		if (last_check + window > io_sectors || j == max_sectors)
9121 			continue;
9122 
9123 		last_check = io_sectors;
9124 	repeat:
9125 		if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
9126 			/* step marks */
9127 			int next = (last_mark+1) % SYNC_MARKS;
9128 
9129 			mddev->resync_mark = mark[next];
9130 			mddev->resync_mark_cnt = mark_cnt[next];
9131 			mark[next] = jiffies;
9132 			mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
9133 			last_mark = next;
9134 		}
9135 
9136 		if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9137 			break;
9138 
9139 		/*
9140 		 * this loop exits only if either when we are slower than
9141 		 * the 'hard' speed limit, or the system was IO-idle for
9142 		 * a jiffy.
9143 		 * the system might be non-idle CPU-wise, but we only care
9144 		 * about not overloading the IO subsystem. (things like an
9145 		 * e2fsck being done on the RAID array should execute fast)
9146 		 */
9147 		cond_resched();
9148 
9149 		recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
9150 		currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
9151 			/((jiffies-mddev->resync_mark)/HZ +1) +1;
9152 
9153 		if (currspeed > speed_min(mddev)) {
9154 			if (currspeed > speed_max(mddev)) {
9155 				msleep(500);
9156 				goto repeat;
9157 			}
9158 			if (!is_mddev_idle(mddev, 0)) {
9159 				/*
9160 				 * Give other IO more of a chance.
9161 				 * The faster the devices, the less we wait.
9162 				 */
9163 				wait_event(mddev->recovery_wait,
9164 					   !atomic_read(&mddev->recovery_active));
9165 			}
9166 		}
9167 	}
9168 	pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
9169 		test_bit(MD_RECOVERY_INTR, &mddev->recovery)
9170 		? "interrupted" : "done");
9171 	/*
9172 	 * this also signals 'finished resyncing' to md_stop
9173 	 */
9174 	blk_finish_plug(&plug);
9175 	wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
9176 
9177 	if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9178 	    !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9179 	    mddev->curr_resync >= MD_RESYNC_ACTIVE) {
9180 		mddev->curr_resync_completed = mddev->curr_resync;
9181 		sysfs_notify_dirent_safe(mddev->sysfs_completed);
9182 	}
9183 	mddev->pers->sync_request(mddev, max_sectors, max_sectors, &skipped);
9184 
9185 	if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
9186 	    mddev->curr_resync > MD_RESYNC_ACTIVE) {
9187 		if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
9188 			if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9189 				if (mddev->curr_resync >= mddev->recovery_cp) {
9190 					pr_debug("md: checkpointing %s of %s.\n",
9191 						 desc, mdname(mddev));
9192 					if (test_bit(MD_RECOVERY_ERROR,
9193 						&mddev->recovery))
9194 						mddev->recovery_cp =
9195 							mddev->curr_resync_completed;
9196 					else
9197 						mddev->recovery_cp =
9198 							mddev->curr_resync;
9199 				}
9200 			} else
9201 				mddev->recovery_cp = MaxSector;
9202 		} else {
9203 			if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9204 				mddev->curr_resync = MaxSector;
9205 			if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9206 			    test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
9207 				rcu_read_lock();
9208 				rdev_for_each_rcu(rdev, mddev)
9209 					if (rdev->raid_disk >= 0 &&
9210 					    mddev->delta_disks >= 0 &&
9211 					    !test_bit(Journal, &rdev->flags) &&
9212 					    !test_bit(Faulty, &rdev->flags) &&
9213 					    !test_bit(In_sync, &rdev->flags) &&
9214 					    rdev->recovery_offset < mddev->curr_resync)
9215 						rdev->recovery_offset = mddev->curr_resync;
9216 				rcu_read_unlock();
9217 			}
9218 		}
9219 	}
9220  skip:
9221 	/* set CHANGE_PENDING here since maybe another update is needed,
9222 	 * so other nodes are informed. It should be harmless for normal
9223 	 * raid */
9224 	set_mask_bits(&mddev->sb_flags, 0,
9225 		      BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
9226 
9227 	if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9228 			!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9229 			mddev->delta_disks > 0 &&
9230 			mddev->pers->finish_reshape &&
9231 			mddev->pers->size &&
9232 			!mddev_is_dm(mddev)) {
9233 		mddev_lock_nointr(mddev);
9234 		md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
9235 		mddev_unlock(mddev);
9236 		if (!mddev_is_clustered(mddev))
9237 			set_capacity_and_notify(mddev->gendisk,
9238 						mddev->array_sectors);
9239 	}
9240 
9241 	spin_lock(&mddev->lock);
9242 	if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9243 		/* We completed so min/max setting can be forgotten if used. */
9244 		if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9245 			mddev->resync_min = 0;
9246 		mddev->resync_max = MaxSector;
9247 	} else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9248 		mddev->resync_min = mddev->curr_resync_completed;
9249 	set_bit(MD_RECOVERY_DONE, &mddev->recovery);
9250 	mddev->curr_resync = MD_RESYNC_NONE;
9251 	spin_unlock(&mddev->lock);
9252 
9253 	wake_up(&resync_wait);
9254 	md_wakeup_thread(mddev->thread);
9255 	return;
9256 }
9257 EXPORT_SYMBOL_GPL(md_do_sync);
9258 
9259 static bool rdev_removeable(struct md_rdev *rdev)
9260 {
9261 	/* rdev is not used. */
9262 	if (rdev->raid_disk < 0)
9263 		return false;
9264 
9265 	/* There are still inflight io, don't remove this rdev. */
9266 	if (atomic_read(&rdev->nr_pending))
9267 		return false;
9268 
9269 	/*
9270 	 * An error occurred but has not yet been acknowledged by the metadata
9271 	 * handler, don't remove this rdev.
9272 	 */
9273 	if (test_bit(Blocked, &rdev->flags))
9274 		return false;
9275 
9276 	/* Fautly rdev is not used, it's safe to remove it. */
9277 	if (test_bit(Faulty, &rdev->flags))
9278 		return true;
9279 
9280 	/* Journal disk can only be removed if it's faulty. */
9281 	if (test_bit(Journal, &rdev->flags))
9282 		return false;
9283 
9284 	/*
9285 	 * 'In_sync' is cleared while 'raid_disk' is valid, which means
9286 	 * replacement has just become active from pers->spare_active(), and
9287 	 * then pers->hot_remove_disk() will replace this rdev with replacement.
9288 	 */
9289 	if (!test_bit(In_sync, &rdev->flags))
9290 		return true;
9291 
9292 	return false;
9293 }
9294 
9295 static bool rdev_is_spare(struct md_rdev *rdev)
9296 {
9297 	return !test_bit(Candidate, &rdev->flags) && rdev->raid_disk >= 0 &&
9298 	       !test_bit(In_sync, &rdev->flags) &&
9299 	       !test_bit(Journal, &rdev->flags) &&
9300 	       !test_bit(Faulty, &rdev->flags);
9301 }
9302 
9303 static bool rdev_addable(struct md_rdev *rdev)
9304 {
9305 	/* rdev is already used, don't add it again. */
9306 	if (test_bit(Candidate, &rdev->flags) || rdev->raid_disk >= 0 ||
9307 	    test_bit(Faulty, &rdev->flags))
9308 		return false;
9309 
9310 	/* Allow to add journal disk. */
9311 	if (test_bit(Journal, &rdev->flags))
9312 		return true;
9313 
9314 	/* Allow to add if array is read-write. */
9315 	if (md_is_rdwr(rdev->mddev))
9316 		return true;
9317 
9318 	/*
9319 	 * For read-only array, only allow to readd a rdev. And if bitmap is
9320 	 * used, don't allow to readd a rdev that is too old.
9321 	 */
9322 	if (rdev->saved_raid_disk >= 0 && !test_bit(Bitmap_sync, &rdev->flags))
9323 		return true;
9324 
9325 	return false;
9326 }
9327 
9328 static bool md_spares_need_change(struct mddev *mddev)
9329 {
9330 	struct md_rdev *rdev;
9331 
9332 	rcu_read_lock();
9333 	rdev_for_each_rcu(rdev, mddev) {
9334 		if (rdev_removeable(rdev) || rdev_addable(rdev)) {
9335 			rcu_read_unlock();
9336 			return true;
9337 		}
9338 	}
9339 	rcu_read_unlock();
9340 	return false;
9341 }
9342 
9343 static int remove_and_add_spares(struct mddev *mddev,
9344 				 struct md_rdev *this)
9345 {
9346 	struct md_rdev *rdev;
9347 	int spares = 0;
9348 	int removed = 0;
9349 
9350 	if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
9351 		/* Mustn't remove devices when resync thread is running */
9352 		return 0;
9353 
9354 	rdev_for_each(rdev, mddev) {
9355 		if ((this == NULL || rdev == this) && rdev_removeable(rdev) &&
9356 		    !mddev->pers->hot_remove_disk(mddev, rdev)) {
9357 			sysfs_unlink_rdev(mddev, rdev);
9358 			rdev->saved_raid_disk = rdev->raid_disk;
9359 			rdev->raid_disk = -1;
9360 			removed++;
9361 		}
9362 	}
9363 
9364 	if (removed && mddev->kobj.sd)
9365 		sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9366 
9367 	if (this && removed)
9368 		goto no_add;
9369 
9370 	rdev_for_each(rdev, mddev) {
9371 		if (this && this != rdev)
9372 			continue;
9373 		if (rdev_is_spare(rdev))
9374 			spares++;
9375 		if (!rdev_addable(rdev))
9376 			continue;
9377 		if (!test_bit(Journal, &rdev->flags))
9378 			rdev->recovery_offset = 0;
9379 		if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
9380 			/* failure here is OK */
9381 			sysfs_link_rdev(mddev, rdev);
9382 			if (!test_bit(Journal, &rdev->flags))
9383 				spares++;
9384 			md_new_event();
9385 			set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9386 		}
9387 	}
9388 no_add:
9389 	if (removed)
9390 		set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9391 	return spares;
9392 }
9393 
9394 static bool md_choose_sync_action(struct mddev *mddev, int *spares)
9395 {
9396 	/* Check if reshape is in progress first. */
9397 	if (mddev->reshape_position != MaxSector) {
9398 		if (mddev->pers->check_reshape == NULL ||
9399 		    mddev->pers->check_reshape(mddev) != 0)
9400 			return false;
9401 
9402 		set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9403 		clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9404 		return true;
9405 	}
9406 
9407 	/*
9408 	 * Remove any failed drives, then add spares if possible. Spares are
9409 	 * also removed and re-added, to allow the personality to fail the
9410 	 * re-add.
9411 	 */
9412 	*spares = remove_and_add_spares(mddev, NULL);
9413 	if (*spares) {
9414 		clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9415 		clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9416 		clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9417 
9418 		/* Start new recovery. */
9419 		set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9420 		return true;
9421 	}
9422 
9423 	/* Check if recovery is in progress. */
9424 	if (mddev->recovery_cp < MaxSector) {
9425 		set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9426 		clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9427 		return true;
9428 	}
9429 
9430 	/* Delay to choose resync/check/repair in md_do_sync(). */
9431 	if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9432 		return true;
9433 
9434 	/* Nothing to be done */
9435 	return false;
9436 }
9437 
9438 static void md_start_sync(struct work_struct *ws)
9439 {
9440 	struct mddev *mddev = container_of(ws, struct mddev, sync_work);
9441 	int spares = 0;
9442 	bool suspend = false;
9443 	char *name;
9444 
9445 	/*
9446 	 * If reshape is still in progress, spares won't be added or removed
9447 	 * from conf until reshape is done.
9448 	 */
9449 	if (mddev->reshape_position == MaxSector &&
9450 	    md_spares_need_change(mddev)) {
9451 		suspend = true;
9452 		mddev_suspend(mddev, false);
9453 	}
9454 
9455 	mddev_lock_nointr(mddev);
9456 	if (!md_is_rdwr(mddev)) {
9457 		/*
9458 		 * On a read-only array we can:
9459 		 * - remove failed devices
9460 		 * - add already-in_sync devices if the array itself is in-sync.
9461 		 * As we only add devices that are already in-sync, we can
9462 		 * activate the spares immediately.
9463 		 */
9464 		remove_and_add_spares(mddev, NULL);
9465 		goto not_running;
9466 	}
9467 
9468 	if (!md_choose_sync_action(mddev, &spares))
9469 		goto not_running;
9470 
9471 	if (!mddev->pers->sync_request)
9472 		goto not_running;
9473 
9474 	/*
9475 	 * We are adding a device or devices to an array which has the bitmap
9476 	 * stored on all devices. So make sure all bitmap pages get written.
9477 	 */
9478 	if (spares)
9479 		mddev->bitmap_ops->write_all(mddev);
9480 
9481 	name = test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ?
9482 			"reshape" : "resync";
9483 	rcu_assign_pointer(mddev->sync_thread,
9484 			   md_register_thread(md_do_sync, mddev, name));
9485 	if (!mddev->sync_thread) {
9486 		pr_warn("%s: could not start resync thread...\n",
9487 			mdname(mddev));
9488 		/* leave the spares where they are, it shouldn't hurt */
9489 		goto not_running;
9490 	}
9491 
9492 	mddev_unlock(mddev);
9493 	/*
9494 	 * md_start_sync was triggered by MD_RECOVERY_NEEDED, so we should
9495 	 * not set it again. Otherwise, we may cause issue like this one:
9496 	 *     https://bugzilla.kernel.org/show_bug.cgi?id=218200
9497 	 * Therefore, use __mddev_resume(mddev, false).
9498 	 */
9499 	if (suspend)
9500 		__mddev_resume(mddev, false);
9501 	md_wakeup_thread(mddev->sync_thread);
9502 	sysfs_notify_dirent_safe(mddev->sysfs_action);
9503 	md_new_event();
9504 	return;
9505 
9506 not_running:
9507 	clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9508 	clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9509 	clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9510 	clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9511 	clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9512 	mddev_unlock(mddev);
9513 	/*
9514 	 * md_start_sync was triggered by MD_RECOVERY_NEEDED, so we should
9515 	 * not set it again. Otherwise, we may cause issue like this one:
9516 	 *     https://bugzilla.kernel.org/show_bug.cgi?id=218200
9517 	 * Therefore, use __mddev_resume(mddev, false).
9518 	 */
9519 	if (suspend)
9520 		__mddev_resume(mddev, false);
9521 
9522 	wake_up(&resync_wait);
9523 	if (test_and_clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
9524 	    mddev->sysfs_action)
9525 		sysfs_notify_dirent_safe(mddev->sysfs_action);
9526 }
9527 
9528 static void unregister_sync_thread(struct mddev *mddev)
9529 {
9530 	if (!test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9531 		/* resync/recovery still happening */
9532 		clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9533 		return;
9534 	}
9535 
9536 	if (WARN_ON_ONCE(!mddev->sync_thread))
9537 		return;
9538 
9539 	md_reap_sync_thread(mddev);
9540 }
9541 
9542 /*
9543  * This routine is regularly called by all per-raid-array threads to
9544  * deal with generic issues like resync and super-block update.
9545  * Raid personalities that don't have a thread (linear/raid0) do not
9546  * need this as they never do any recovery or update the superblock.
9547  *
9548  * It does not do any resync itself, but rather "forks" off other threads
9549  * to do that as needed.
9550  * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9551  * "->recovery" and create a thread at ->sync_thread.
9552  * When the thread finishes it sets MD_RECOVERY_DONE
9553  * and wakeups up this thread which will reap the thread and finish up.
9554  * This thread also removes any faulty devices (with nr_pending == 0).
9555  *
9556  * The overall approach is:
9557  *  1/ if the superblock needs updating, update it.
9558  *  2/ If a recovery thread is running, don't do anything else.
9559  *  3/ If recovery has finished, clean up, possibly marking spares active.
9560  *  4/ If there are any faulty devices, remove them.
9561  *  5/ If array is degraded, try to add spares devices
9562  *  6/ If array has spares or is not in-sync, start a resync thread.
9563  */
9564 void md_check_recovery(struct mddev *mddev)
9565 {
9566 	if (mddev->bitmap)
9567 		mddev->bitmap_ops->daemon_work(mddev);
9568 
9569 	if (signal_pending(current)) {
9570 		if (mddev->pers->sync_request && !mddev->external) {
9571 			pr_debug("md: %s in immediate safe mode\n",
9572 				 mdname(mddev));
9573 			mddev->safemode = 2;
9574 		}
9575 		flush_signals(current);
9576 	}
9577 
9578 	if (!md_is_rdwr(mddev) &&
9579 	    !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
9580 	    !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
9581 		return;
9582 	if ( ! (
9583 		(mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
9584 		test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9585 		test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
9586 		(mddev->external == 0 && mddev->safemode == 1) ||
9587 		(mddev->safemode == 2
9588 		 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
9589 		))
9590 		return;
9591 
9592 	if (mddev_trylock(mddev)) {
9593 		bool try_set_sync = mddev->safemode != 0;
9594 
9595 		if (!mddev->external && mddev->safemode == 1)
9596 			mddev->safemode = 0;
9597 
9598 		if (!md_is_rdwr(mddev)) {
9599 			struct md_rdev *rdev;
9600 
9601 			if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
9602 				unregister_sync_thread(mddev);
9603 				goto unlock;
9604 			}
9605 
9606 			if (!mddev->external && mddev->in_sync)
9607 				/*
9608 				 * 'Blocked' flag not needed as failed devices
9609 				 * will be recorded if array switched to read/write.
9610 				 * Leaving it set will prevent the device
9611 				 * from being removed.
9612 				 */
9613 				rdev_for_each(rdev, mddev)
9614 					clear_bit(Blocked, &rdev->flags);
9615 
9616 			/*
9617 			 * There is no thread, but we need to call
9618 			 * ->spare_active and clear saved_raid_disk
9619 			 */
9620 			set_bit(MD_RECOVERY_INTR, &mddev->recovery);
9621 			md_reap_sync_thread(mddev);
9622 
9623 			/*
9624 			 * Let md_start_sync() to remove and add rdevs to the
9625 			 * array.
9626 			 */
9627 			if (md_spares_need_change(mddev)) {
9628 				set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9629 				queue_work(md_misc_wq, &mddev->sync_work);
9630 			}
9631 
9632 			clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
9633 			clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9634 			clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
9635 
9636 			goto unlock;
9637 		}
9638 
9639 		if (mddev_is_clustered(mddev)) {
9640 			struct md_rdev *rdev, *tmp;
9641 			/* kick the device if another node issued a
9642 			 * remove disk.
9643 			 */
9644 			rdev_for_each_safe(rdev, tmp, mddev) {
9645 				if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9646 						rdev->raid_disk < 0)
9647 					md_kick_rdev_from_array(rdev);
9648 			}
9649 		}
9650 
9651 		if (try_set_sync && !mddev->external && !mddev->in_sync) {
9652 			spin_lock(&mddev->lock);
9653 			set_in_sync(mddev);
9654 			spin_unlock(&mddev->lock);
9655 		}
9656 
9657 		if (mddev->sb_flags)
9658 			md_update_sb(mddev, 0);
9659 
9660 		/*
9661 		 * Never start a new sync thread if MD_RECOVERY_RUNNING is
9662 		 * still set.
9663 		 */
9664 		if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
9665 			unregister_sync_thread(mddev);
9666 			goto unlock;
9667 		}
9668 
9669 		/* Set RUNNING before clearing NEEDED to avoid
9670 		 * any transients in the value of "sync_action".
9671 		 */
9672 		mddev->curr_resync_completed = 0;
9673 		spin_lock(&mddev->lock);
9674 		set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9675 		spin_unlock(&mddev->lock);
9676 		/* Clear some bits that don't mean anything, but
9677 		 * might be left set
9678 		 */
9679 		clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9680 		clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9681 
9682 		if (test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
9683 		    !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
9684 			queue_work(md_misc_wq, &mddev->sync_work);
9685 		} else {
9686 			clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9687 			wake_up(&resync_wait);
9688 		}
9689 
9690 	unlock:
9691 		wake_up(&mddev->sb_wait);
9692 		mddev_unlock(mddev);
9693 	}
9694 }
9695 EXPORT_SYMBOL(md_check_recovery);
9696 
9697 void md_reap_sync_thread(struct mddev *mddev)
9698 {
9699 	struct md_rdev *rdev;
9700 	sector_t old_dev_sectors = mddev->dev_sectors;
9701 	bool is_reshaped = false;
9702 
9703 	/* resync has finished, collect result */
9704 	md_unregister_thread(mddev, &mddev->sync_thread);
9705 	atomic_inc(&mddev->sync_seq);
9706 
9707 	if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9708 	    !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9709 	    mddev->degraded != mddev->raid_disks) {
9710 		/* success...*/
9711 		/* activate any spares */
9712 		if (mddev->pers->spare_active(mddev)) {
9713 			sysfs_notify_dirent_safe(mddev->sysfs_degraded);
9714 			set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
9715 		}
9716 	}
9717 	if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9718 	    mddev->pers->finish_reshape) {
9719 		mddev->pers->finish_reshape(mddev);
9720 		if (mddev_is_clustered(mddev))
9721 			is_reshaped = true;
9722 	}
9723 
9724 	/* If array is no-longer degraded, then any saved_raid_disk
9725 	 * information must be scrapped.
9726 	 */
9727 	if (!mddev->degraded)
9728 		rdev_for_each(rdev, mddev)
9729 			rdev->saved_raid_disk = -1;
9730 
9731 	md_update_sb(mddev, 1);
9732 	/* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
9733 	 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9734 	 * clustered raid */
9735 	if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9736 		md_cluster_ops->resync_finish(mddev);
9737 	clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
9738 	clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
9739 	clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9740 	clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9741 	clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9742 	clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9743 	/*
9744 	 * We call md_cluster_ops->update_size here because sync_size could
9745 	 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9746 	 * so it is time to update size across cluster.
9747 	 */
9748 	if (mddev_is_clustered(mddev) && is_reshaped
9749 				      && !test_bit(MD_CLOSING, &mddev->flags))
9750 		md_cluster_ops->update_size(mddev, old_dev_sectors);
9751 	/* flag recovery needed just to double check */
9752 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9753 	sysfs_notify_dirent_safe(mddev->sysfs_completed);
9754 	sysfs_notify_dirent_safe(mddev->sysfs_action);
9755 	md_new_event();
9756 	if (mddev->event_work.func)
9757 		queue_work(md_misc_wq, &mddev->event_work);
9758 	wake_up(&resync_wait);
9759 }
9760 EXPORT_SYMBOL(md_reap_sync_thread);
9761 
9762 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
9763 {
9764 	sysfs_notify_dirent_safe(rdev->sysfs_state);
9765 	wait_event_timeout(rdev->blocked_wait,
9766 			   !test_bit(Blocked, &rdev->flags) &&
9767 			   !test_bit(BlockedBadBlocks, &rdev->flags),
9768 			   msecs_to_jiffies(5000));
9769 	rdev_dec_pending(rdev, mddev);
9770 }
9771 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9772 
9773 void md_finish_reshape(struct mddev *mddev)
9774 {
9775 	/* called be personality module when reshape completes. */
9776 	struct md_rdev *rdev;
9777 
9778 	rdev_for_each(rdev, mddev) {
9779 		if (rdev->data_offset > rdev->new_data_offset)
9780 			rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9781 		else
9782 			rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9783 		rdev->data_offset = rdev->new_data_offset;
9784 	}
9785 }
9786 EXPORT_SYMBOL(md_finish_reshape);
9787 
9788 /* Bad block management */
9789 
9790 /* Returns 1 on success, 0 on failure */
9791 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9792 		       int is_new)
9793 {
9794 	struct mddev *mddev = rdev->mddev;
9795 	int rv;
9796 	if (is_new)
9797 		s += rdev->new_data_offset;
9798 	else
9799 		s += rdev->data_offset;
9800 	rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9801 	if (rv == 0) {
9802 		/* Make sure they get written out promptly */
9803 		if (test_bit(ExternalBbl, &rdev->flags))
9804 			sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
9805 		sysfs_notify_dirent_safe(rdev->sysfs_state);
9806 		set_mask_bits(&mddev->sb_flags, 0,
9807 			      BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
9808 		md_wakeup_thread(rdev->mddev->thread);
9809 		return 1;
9810 	} else
9811 		return 0;
9812 }
9813 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9814 
9815 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9816 			 int is_new)
9817 {
9818 	int rv;
9819 	if (is_new)
9820 		s += rdev->new_data_offset;
9821 	else
9822 		s += rdev->data_offset;
9823 	rv = badblocks_clear(&rdev->badblocks, s, sectors);
9824 	if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9825 		sysfs_notify_dirent_safe(rdev->sysfs_badblocks);
9826 	return rv;
9827 }
9828 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9829 
9830 static int md_notify_reboot(struct notifier_block *this,
9831 			    unsigned long code, void *x)
9832 {
9833 	struct mddev *mddev, *n;
9834 	int need_delay = 0;
9835 
9836 	spin_lock(&all_mddevs_lock);
9837 	list_for_each_entry_safe(mddev, n, &all_mddevs, all_mddevs) {
9838 		if (!mddev_get(mddev))
9839 			continue;
9840 		spin_unlock(&all_mddevs_lock);
9841 		if (mddev_trylock(mddev)) {
9842 			if (mddev->pers)
9843 				__md_stop_writes(mddev);
9844 			if (mddev->persistent)
9845 				mddev->safemode = 2;
9846 			mddev_unlock(mddev);
9847 		}
9848 		need_delay = 1;
9849 		mddev_put(mddev);
9850 		spin_lock(&all_mddevs_lock);
9851 	}
9852 	spin_unlock(&all_mddevs_lock);
9853 
9854 	/*
9855 	 * certain more exotic SCSI devices are known to be
9856 	 * volatile wrt too early system reboots. While the
9857 	 * right place to handle this issue is the given
9858 	 * driver, we do want to have a safe RAID driver ...
9859 	 */
9860 	if (need_delay)
9861 		msleep(1000);
9862 
9863 	return NOTIFY_DONE;
9864 }
9865 
9866 static struct notifier_block md_notifier = {
9867 	.notifier_call	= md_notify_reboot,
9868 	.next		= NULL,
9869 	.priority	= INT_MAX, /* before any real devices */
9870 };
9871 
9872 static void md_geninit(void)
9873 {
9874 	pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
9875 
9876 	proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
9877 }
9878 
9879 static int __init md_init(void)
9880 {
9881 	int ret = -ENOMEM;
9882 
9883 	md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
9884 	if (!md_wq)
9885 		goto err_wq;
9886 
9887 	md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9888 	if (!md_misc_wq)
9889 		goto err_misc_wq;
9890 
9891 	md_bitmap_wq = alloc_workqueue("md_bitmap", WQ_MEM_RECLAIM | WQ_UNBOUND,
9892 				       0);
9893 	if (!md_bitmap_wq)
9894 		goto err_bitmap_wq;
9895 
9896 	ret = __register_blkdev(MD_MAJOR, "md", md_probe);
9897 	if (ret < 0)
9898 		goto err_md;
9899 
9900 	ret = __register_blkdev(0, "mdp", md_probe);
9901 	if (ret < 0)
9902 		goto err_mdp;
9903 	mdp_major = ret;
9904 
9905 	register_reboot_notifier(&md_notifier);
9906 	raid_table_header = register_sysctl("dev/raid", raid_table);
9907 
9908 	md_geninit();
9909 	return 0;
9910 
9911 err_mdp:
9912 	unregister_blkdev(MD_MAJOR, "md");
9913 err_md:
9914 	destroy_workqueue(md_bitmap_wq);
9915 err_bitmap_wq:
9916 	destroy_workqueue(md_misc_wq);
9917 err_misc_wq:
9918 	destroy_workqueue(md_wq);
9919 err_wq:
9920 	return ret;
9921 }
9922 
9923 static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
9924 {
9925 	struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9926 	struct md_rdev *rdev2, *tmp;
9927 	int role, ret;
9928 
9929 	/*
9930 	 * If size is changed in another node then we need to
9931 	 * do resize as well.
9932 	 */
9933 	if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9934 		ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9935 		if (ret)
9936 			pr_info("md-cluster: resize failed\n");
9937 		else
9938 			mddev->bitmap_ops->update_sb(mddev->bitmap);
9939 	}
9940 
9941 	/* Check for change of roles in the active devices */
9942 	rdev_for_each_safe(rdev2, tmp, mddev) {
9943 		if (test_bit(Faulty, &rdev2->flags))
9944 			continue;
9945 
9946 		/* Check if the roles changed */
9947 		role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
9948 
9949 		if (test_bit(Candidate, &rdev2->flags)) {
9950 			if (role == MD_DISK_ROLE_FAULTY) {
9951 				pr_info("md: Removing Candidate device %pg because add failed\n",
9952 					rdev2->bdev);
9953 				md_kick_rdev_from_array(rdev2);
9954 				continue;
9955 			}
9956 			else
9957 				clear_bit(Candidate, &rdev2->flags);
9958 		}
9959 
9960 		if (role != rdev2->raid_disk) {
9961 			/*
9962 			 * got activated except reshape is happening.
9963 			 */
9964 			if (rdev2->raid_disk == -1 && role != MD_DISK_ROLE_SPARE &&
9965 			    !(le32_to_cpu(sb->feature_map) &
9966 			      MD_FEATURE_RESHAPE_ACTIVE) &&
9967 			    !md_cluster_ops->resync_status_get(mddev)) {
9968 				/*
9969 				 * -1 to make raid1_add_disk() set conf->fullsync
9970 				 * to 1. This could avoid skipping sync when the
9971 				 * remote node is down during resyncing.
9972 				 */
9973 				if ((le32_to_cpu(sb->feature_map)
9974 				    & MD_FEATURE_RECOVERY_OFFSET))
9975 					rdev2->saved_raid_disk = -1;
9976 				else
9977 					rdev2->saved_raid_disk = role;
9978 				ret = remove_and_add_spares(mddev, rdev2);
9979 				pr_info("Activated spare: %pg\n",
9980 					rdev2->bdev);
9981 				/* wakeup mddev->thread here, so array could
9982 				 * perform resync with the new activated disk */
9983 				set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9984 				md_wakeup_thread(mddev->thread);
9985 			}
9986 			/* device faulty
9987 			 * We just want to do the minimum to mark the disk
9988 			 * as faulty. The recovery is performed by the
9989 			 * one who initiated the error.
9990 			 */
9991 			if (role == MD_DISK_ROLE_FAULTY ||
9992 			    role == MD_DISK_ROLE_JOURNAL) {
9993 				md_error(mddev, rdev2);
9994 				clear_bit(Blocked, &rdev2->flags);
9995 			}
9996 		}
9997 	}
9998 
9999 	if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) {
10000 		ret = update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
10001 		if (ret)
10002 			pr_warn("md: updating array disks failed. %d\n", ret);
10003 	}
10004 
10005 	/*
10006 	 * Since mddev->delta_disks has already updated in update_raid_disks,
10007 	 * so it is time to check reshape.
10008 	 */
10009 	if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
10010 	    (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
10011 		/*
10012 		 * reshape is happening in the remote node, we need to
10013 		 * update reshape_position and call start_reshape.
10014 		 */
10015 		mddev->reshape_position = le64_to_cpu(sb->reshape_position);
10016 		if (mddev->pers->update_reshape_pos)
10017 			mddev->pers->update_reshape_pos(mddev);
10018 		if (mddev->pers->start_reshape)
10019 			mddev->pers->start_reshape(mddev);
10020 	} else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
10021 		   mddev->reshape_position != MaxSector &&
10022 		   !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
10023 		/* reshape is just done in another node. */
10024 		mddev->reshape_position = MaxSector;
10025 		if (mddev->pers->update_reshape_pos)
10026 			mddev->pers->update_reshape_pos(mddev);
10027 	}
10028 
10029 	/* Finally set the event to be up to date */
10030 	mddev->events = le64_to_cpu(sb->events);
10031 }
10032 
10033 static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
10034 {
10035 	int err;
10036 	struct page *swapout = rdev->sb_page;
10037 	struct mdp_superblock_1 *sb;
10038 
10039 	/* Store the sb page of the rdev in the swapout temporary
10040 	 * variable in case we err in the future
10041 	 */
10042 	rdev->sb_page = NULL;
10043 	err = alloc_disk_sb(rdev);
10044 	if (err == 0) {
10045 		ClearPageUptodate(rdev->sb_page);
10046 		rdev->sb_loaded = 0;
10047 		err = super_types[mddev->major_version].
10048 			load_super(rdev, NULL, mddev->minor_version);
10049 	}
10050 	if (err < 0) {
10051 		pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
10052 				__func__, __LINE__, rdev->desc_nr, err);
10053 		if (rdev->sb_page)
10054 			put_page(rdev->sb_page);
10055 		rdev->sb_page = swapout;
10056 		rdev->sb_loaded = 1;
10057 		return err;
10058 	}
10059 
10060 	sb = page_address(rdev->sb_page);
10061 	/* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
10062 	 * is not set
10063 	 */
10064 
10065 	if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
10066 		rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
10067 
10068 	/* The other node finished recovery, call spare_active to set
10069 	 * device In_sync and mddev->degraded
10070 	 */
10071 	if (rdev->recovery_offset == MaxSector &&
10072 	    !test_bit(In_sync, &rdev->flags) &&
10073 	    mddev->pers->spare_active(mddev))
10074 		sysfs_notify_dirent_safe(mddev->sysfs_degraded);
10075 
10076 	put_page(swapout);
10077 	return 0;
10078 }
10079 
10080 void md_reload_sb(struct mddev *mddev, int nr)
10081 {
10082 	struct md_rdev *rdev = NULL, *iter;
10083 	int err;
10084 
10085 	/* Find the rdev */
10086 	rdev_for_each_rcu(iter, mddev) {
10087 		if (iter->desc_nr == nr) {
10088 			rdev = iter;
10089 			break;
10090 		}
10091 	}
10092 
10093 	if (!rdev) {
10094 		pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
10095 		return;
10096 	}
10097 
10098 	err = read_rdev(mddev, rdev);
10099 	if (err < 0)
10100 		return;
10101 
10102 	check_sb_changes(mddev, rdev);
10103 
10104 	/* Read all rdev's to update recovery_offset */
10105 	rdev_for_each_rcu(rdev, mddev) {
10106 		if (!test_bit(Faulty, &rdev->flags))
10107 			read_rdev(mddev, rdev);
10108 	}
10109 }
10110 EXPORT_SYMBOL(md_reload_sb);
10111 
10112 #ifndef MODULE
10113 
10114 /*
10115  * Searches all registered partitions for autorun RAID arrays
10116  * at boot time.
10117  */
10118 
10119 static DEFINE_MUTEX(detected_devices_mutex);
10120 static LIST_HEAD(all_detected_devices);
10121 struct detected_devices_node {
10122 	struct list_head list;
10123 	dev_t dev;
10124 };
10125 
10126 void md_autodetect_dev(dev_t dev)
10127 {
10128 	struct detected_devices_node *node_detected_dev;
10129 
10130 	node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
10131 	if (node_detected_dev) {
10132 		node_detected_dev->dev = dev;
10133 		mutex_lock(&detected_devices_mutex);
10134 		list_add_tail(&node_detected_dev->list, &all_detected_devices);
10135 		mutex_unlock(&detected_devices_mutex);
10136 	}
10137 }
10138 
10139 void md_autostart_arrays(int part)
10140 {
10141 	struct md_rdev *rdev;
10142 	struct detected_devices_node *node_detected_dev;
10143 	dev_t dev;
10144 	int i_scanned, i_passed;
10145 
10146 	i_scanned = 0;
10147 	i_passed = 0;
10148 
10149 	pr_info("md: Autodetecting RAID arrays.\n");
10150 
10151 	mutex_lock(&detected_devices_mutex);
10152 	while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
10153 		i_scanned++;
10154 		node_detected_dev = list_entry(all_detected_devices.next,
10155 					struct detected_devices_node, list);
10156 		list_del(&node_detected_dev->list);
10157 		dev = node_detected_dev->dev;
10158 		kfree(node_detected_dev);
10159 		mutex_unlock(&detected_devices_mutex);
10160 		rdev = md_import_device(dev,0, 90);
10161 		mutex_lock(&detected_devices_mutex);
10162 		if (IS_ERR(rdev))
10163 			continue;
10164 
10165 		if (test_bit(Faulty, &rdev->flags))
10166 			continue;
10167 
10168 		set_bit(AutoDetected, &rdev->flags);
10169 		list_add(&rdev->same_set, &pending_raid_disks);
10170 		i_passed++;
10171 	}
10172 	mutex_unlock(&detected_devices_mutex);
10173 
10174 	pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
10175 
10176 	autorun_devices(part);
10177 }
10178 
10179 #endif /* !MODULE */
10180 
10181 static __exit void md_exit(void)
10182 {
10183 	struct mddev *mddev, *n;
10184 	int delay = 1;
10185 
10186 	unregister_blkdev(MD_MAJOR,"md");
10187 	unregister_blkdev(mdp_major, "mdp");
10188 	unregister_reboot_notifier(&md_notifier);
10189 	unregister_sysctl_table(raid_table_header);
10190 
10191 	/* We cannot unload the modules while some process is
10192 	 * waiting for us in select() or poll() - wake them up
10193 	 */
10194 	md_unloading = 1;
10195 	while (waitqueue_active(&md_event_waiters)) {
10196 		/* not safe to leave yet */
10197 		wake_up(&md_event_waiters);
10198 		msleep(delay);
10199 		delay += delay;
10200 	}
10201 	remove_proc_entry("mdstat", NULL);
10202 
10203 	spin_lock(&all_mddevs_lock);
10204 	list_for_each_entry_safe(mddev, n, &all_mddevs, all_mddevs) {
10205 		if (!mddev_get(mddev))
10206 			continue;
10207 		spin_unlock(&all_mddevs_lock);
10208 		export_array(mddev);
10209 		mddev->ctime = 0;
10210 		mddev->hold_active = 0;
10211 		/*
10212 		 * As the mddev is now fully clear, mddev_put will schedule
10213 		 * the mddev for destruction by a workqueue, and the
10214 		 * destroy_workqueue() below will wait for that to complete.
10215 		 */
10216 		mddev_put(mddev);
10217 		spin_lock(&all_mddevs_lock);
10218 	}
10219 	spin_unlock(&all_mddevs_lock);
10220 
10221 	destroy_workqueue(md_misc_wq);
10222 	destroy_workqueue(md_bitmap_wq);
10223 	destroy_workqueue(md_wq);
10224 }
10225 
10226 subsys_initcall(md_init);
10227 module_exit(md_exit)
10228 
10229 static int get_ro(char *buffer, const struct kernel_param *kp)
10230 {
10231 	return sprintf(buffer, "%d\n", start_readonly);
10232 }
10233 static int set_ro(const char *val, const struct kernel_param *kp)
10234 {
10235 	return kstrtouint(val, 10, (unsigned int *)&start_readonly);
10236 }
10237 
10238 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
10239 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
10240 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
10241 module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
10242 
10243 MODULE_LICENSE("GPL");
10244 MODULE_DESCRIPTION("MD RAID framework");
10245 MODULE_ALIAS("md");
10246 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);
10247