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