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