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