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