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