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