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