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