]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | md.c : Multiple Devices driver for Linux | |
f72ffdd6 | 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
1da177e4 LT |
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" |
edb39c9d | 56 | #include "md-cluster.h" |
1da177e4 | 57 | |
1da177e4 | 58 | #ifndef MODULE |
d710e138 | 59 | static void autostart_arrays(int part); |
1da177e4 LT |
60 | #endif |
61 | ||
01f96c0a N |
62 | /* pers_list is a list of registered personalities protected |
63 | * by pers_lock. | |
64 | * pers_lock does extra service to protect accesses to | |
65 | * mddev->thread when the mutex cannot be held. | |
66 | */ | |
2604b703 | 67 | static LIST_HEAD(pers_list); |
1da177e4 LT |
68 | static DEFINE_SPINLOCK(pers_lock); |
69 | ||
edb39c9d | 70 | struct md_cluster_operations *md_cluster_ops; |
589a1c49 | 71 | EXPORT_SYMBOL(md_cluster_ops); |
edb39c9d GR |
72 | struct module *md_cluster_mod; |
73 | EXPORT_SYMBOL(md_cluster_mod); | |
74 | ||
90b08710 | 75 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
e804ac78 TH |
76 | static struct workqueue_struct *md_wq; |
77 | static struct workqueue_struct *md_misc_wq; | |
90b08710 | 78 | |
746d3207 N |
79 | static int remove_and_add_spares(struct mddev *mddev, |
80 | struct md_rdev *this); | |
5aa61f42 | 81 | static void mddev_detach(struct mddev *mddev); |
746d3207 | 82 | |
1e50915f RB |
83 | /* |
84 | * Default number of read corrections we'll attempt on an rdev | |
85 | * before ejecting it from the array. We divide the read error | |
86 | * count by 2 for every hour elapsed between read errors. | |
87 | */ | |
88 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 | |
1da177e4 LT |
89 | /* |
90 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' | |
91 | * is 1000 KB/sec, so the extra system load does not show up that much. | |
92 | * Increase it if you want to have more _guaranteed_ speed. Note that | |
338cec32 | 93 | * the RAID driver will use the maximum available bandwidth if the IO |
1da177e4 LT |
94 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
95 | * speed limit - in case reconstruction slows down your system despite | |
96 | * idle IO detection. | |
97 | * | |
98 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. | |
88202a0c | 99 | * or /sys/block/mdX/md/sync_speed_{min,max} |
1da177e4 LT |
100 | */ |
101 | ||
102 | static int sysctl_speed_limit_min = 1000; | |
103 | static int sysctl_speed_limit_max = 200000; | |
fd01b88c | 104 | static inline int speed_min(struct mddev *mddev) |
88202a0c N |
105 | { |
106 | return mddev->sync_speed_min ? | |
107 | mddev->sync_speed_min : sysctl_speed_limit_min; | |
108 | } | |
109 | ||
fd01b88c | 110 | static inline int speed_max(struct mddev *mddev) |
88202a0c N |
111 | { |
112 | return mddev->sync_speed_max ? | |
113 | mddev->sync_speed_max : sysctl_speed_limit_max; | |
114 | } | |
1da177e4 LT |
115 | |
116 | static struct ctl_table_header *raid_table_header; | |
117 | ||
82592c38 | 118 | static struct ctl_table raid_table[] = { |
1da177e4 | 119 | { |
1da177e4 LT |
120 | .procname = "speed_limit_min", |
121 | .data = &sysctl_speed_limit_min, | |
122 | .maxlen = sizeof(int), | |
80ca3a44 | 123 | .mode = S_IRUGO|S_IWUSR, |
6d456111 | 124 | .proc_handler = proc_dointvec, |
1da177e4 LT |
125 | }, |
126 | { | |
1da177e4 LT |
127 | .procname = "speed_limit_max", |
128 | .data = &sysctl_speed_limit_max, | |
129 | .maxlen = sizeof(int), | |
80ca3a44 | 130 | .mode = S_IRUGO|S_IWUSR, |
6d456111 | 131 | .proc_handler = proc_dointvec, |
1da177e4 | 132 | }, |
894d2491 | 133 | { } |
1da177e4 LT |
134 | }; |
135 | ||
82592c38 | 136 | static struct ctl_table raid_dir_table[] = { |
1da177e4 | 137 | { |
1da177e4 LT |
138 | .procname = "raid", |
139 | .maxlen = 0, | |
80ca3a44 | 140 | .mode = S_IRUGO|S_IXUGO, |
1da177e4 LT |
141 | .child = raid_table, |
142 | }, | |
894d2491 | 143 | { } |
1da177e4 LT |
144 | }; |
145 | ||
82592c38 | 146 | static struct ctl_table raid_root_table[] = { |
1da177e4 | 147 | { |
1da177e4 LT |
148 | .procname = "dev", |
149 | .maxlen = 0, | |
150 | .mode = 0555, | |
151 | .child = raid_dir_table, | |
152 | }, | |
894d2491 | 153 | { } |
1da177e4 LT |
154 | }; |
155 | ||
83d5cde4 | 156 | static const struct block_device_operations md_fops; |
1da177e4 | 157 | |
f91de92e N |
158 | static int start_readonly; |
159 | ||
a167f663 N |
160 | /* bio_clone_mddev |
161 | * like bio_clone, but with a local bio set | |
162 | */ | |
163 | ||
a167f663 | 164 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
fd01b88c | 165 | struct mddev *mddev) |
a167f663 N |
166 | { |
167 | struct bio *b; | |
a167f663 N |
168 | |
169 | if (!mddev || !mddev->bio_set) | |
170 | return bio_alloc(gfp_mask, nr_iovecs); | |
171 | ||
395c72a7 | 172 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
a167f663 N |
173 | if (!b) |
174 | return NULL; | |
a167f663 N |
175 | return b; |
176 | } | |
177 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); | |
178 | ||
179 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, | |
fd01b88c | 180 | struct mddev *mddev) |
a167f663 | 181 | { |
a167f663 N |
182 | if (!mddev || !mddev->bio_set) |
183 | return bio_clone(bio, gfp_mask); | |
184 | ||
bf800ef1 | 185 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
a167f663 N |
186 | } |
187 | EXPORT_SYMBOL_GPL(bio_clone_mddev); | |
188 | ||
d7603b7e N |
189 | /* |
190 | * We have a system wide 'event count' that is incremented | |
191 | * on any 'interesting' event, and readers of /proc/mdstat | |
192 | * can use 'poll' or 'select' to find out when the event | |
193 | * count increases. | |
194 | * | |
195 | * Events are: | |
196 | * start array, stop array, error, add device, remove device, | |
197 | * start build, activate spare | |
198 | */ | |
2989ddbd | 199 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
d7603b7e | 200 | static atomic_t md_event_count; |
fd01b88c | 201 | void md_new_event(struct mddev *mddev) |
d7603b7e N |
202 | { |
203 | atomic_inc(&md_event_count); | |
204 | wake_up(&md_event_waiters); | |
205 | } | |
29269553 | 206 | EXPORT_SYMBOL_GPL(md_new_event); |
d7603b7e | 207 | |
c331eb04 N |
208 | /* Alternate version that can be called from interrupts |
209 | * when calling sysfs_notify isn't needed. | |
210 | */ | |
fd01b88c | 211 | static void md_new_event_inintr(struct mddev *mddev) |
c331eb04 N |
212 | { |
213 | atomic_inc(&md_event_count); | |
214 | wake_up(&md_event_waiters); | |
215 | } | |
216 | ||
1da177e4 LT |
217 | /* |
218 | * Enables to iterate over all existing md arrays | |
219 | * all_mddevs_lock protects this list. | |
220 | */ | |
221 | static LIST_HEAD(all_mddevs); | |
222 | static DEFINE_SPINLOCK(all_mddevs_lock); | |
223 | ||
1da177e4 LT |
224 | /* |
225 | * iterates through all used mddevs in the system. | |
226 | * We take care to grab the all_mddevs_lock whenever navigating | |
227 | * the list, and to always hold a refcount when unlocked. | |
228 | * Any code which breaks out of this loop while own | |
229 | * a reference to the current mddev and must mddev_put it. | |
230 | */ | |
fd01b88c | 231 | #define for_each_mddev(_mddev,_tmp) \ |
1da177e4 | 232 | \ |
f72ffdd6 | 233 | for (({ spin_lock(&all_mddevs_lock); \ |
fd01b88c N |
234 | _tmp = all_mddevs.next; \ |
235 | _mddev = NULL;}); \ | |
236 | ({ if (_tmp != &all_mddevs) \ | |
237 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ | |
1da177e4 | 238 | spin_unlock(&all_mddevs_lock); \ |
fd01b88c N |
239 | if (_mddev) mddev_put(_mddev); \ |
240 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ | |
241 | _tmp != &all_mddevs;}); \ | |
1da177e4 | 242 | ({ spin_lock(&all_mddevs_lock); \ |
fd01b88c | 243 | _tmp = _tmp->next;}) \ |
1da177e4 LT |
244 | ) |
245 | ||
409c57f3 N |
246 | /* Rather than calling directly into the personality make_request function, |
247 | * IO requests come here first so that we can check if the device is | |
248 | * being suspended pending a reconfiguration. | |
249 | * We hold a refcount over the call to ->make_request. By the time that | |
250 | * call has finished, the bio has been linked into some internal structure | |
251 | * and so is visible to ->quiesce(), so we don't need the refcount any more. | |
252 | */ | |
dece1635 | 253 | static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio) |
1da177e4 | 254 | { |
49077326 | 255 | const int rw = bio_data_dir(bio); |
fd01b88c | 256 | struct mddev *mddev = q->queuedata; |
e91ece55 | 257 | unsigned int sectors; |
74672d06 | 258 | int cpu; |
49077326 | 259 | |
54efd50b KO |
260 | blk_queue_split(q, &bio, q->bio_split); |
261 | ||
0ca69886 N |
262 | if (mddev == NULL || mddev->pers == NULL |
263 | || !mddev->ready) { | |
409c57f3 | 264 | bio_io_error(bio); |
dece1635 | 265 | return BLK_QC_T_NONE; |
409c57f3 | 266 | } |
bbfa57c0 | 267 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
4246a0b6 CH |
268 | if (bio_sectors(bio) != 0) |
269 | bio->bi_error = -EROFS; | |
270 | bio_endio(bio); | |
dece1635 | 271 | return BLK_QC_T_NONE; |
bbfa57c0 | 272 | } |
0ca69886 | 273 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
409c57f3 | 274 | rcu_read_lock(); |
e9c7469b | 275 | if (mddev->suspended) { |
409c57f3 N |
276 | DEFINE_WAIT(__wait); |
277 | for (;;) { | |
278 | prepare_to_wait(&mddev->sb_wait, &__wait, | |
279 | TASK_UNINTERRUPTIBLE); | |
e9c7469b | 280 | if (!mddev->suspended) |
409c57f3 N |
281 | break; |
282 | rcu_read_unlock(); | |
283 | schedule(); | |
284 | rcu_read_lock(); | |
285 | } | |
286 | finish_wait(&mddev->sb_wait, &__wait); | |
287 | } | |
288 | atomic_inc(&mddev->active_io); | |
289 | rcu_read_unlock(); | |
49077326 | 290 | |
e91ece55 CM |
291 | /* |
292 | * save the sectors now since our bio can | |
293 | * go away inside make_request | |
294 | */ | |
295 | sectors = bio_sectors(bio); | |
5a7bbad2 | 296 | mddev->pers->make_request(mddev, bio); |
49077326 | 297 | |
74672d06 GZ |
298 | cpu = part_stat_lock(); |
299 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); | |
300 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); | |
301 | part_stat_unlock(); | |
49077326 | 302 | |
409c57f3 N |
303 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
304 | wake_up(&mddev->sb_wait); | |
dece1635 JA |
305 | |
306 | return BLK_QC_T_NONE; | |
409c57f3 N |
307 | } |
308 | ||
9e35b99c N |
309 | /* mddev_suspend makes sure no new requests are submitted |
310 | * to the device, and that any requests that have been submitted | |
311 | * are completely handled. | |
afa0f557 N |
312 | * Once mddev_detach() is called and completes, the module will be |
313 | * completely unused. | |
9e35b99c | 314 | */ |
fd01b88c | 315 | void mddev_suspend(struct mddev *mddev) |
409c57f3 | 316 | { |
0dc10e50 MP |
317 | if (mddev->suspended++) |
318 | return; | |
409c57f3 N |
319 | synchronize_rcu(); |
320 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); | |
321 | mddev->pers->quiesce(mddev, 1); | |
0d9f4f13 JB |
322 | |
323 | del_timer_sync(&mddev->safemode_timer); | |
409c57f3 | 324 | } |
390ee602 | 325 | EXPORT_SYMBOL_GPL(mddev_suspend); |
409c57f3 | 326 | |
fd01b88c | 327 | void mddev_resume(struct mddev *mddev) |
409c57f3 | 328 | { |
0dc10e50 MP |
329 | if (--mddev->suspended) |
330 | return; | |
409c57f3 N |
331 | wake_up(&mddev->sb_wait); |
332 | mddev->pers->quiesce(mddev, 0); | |
0fd018af | 333 | |
47525e59 | 334 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
0fd018af JB |
335 | md_wakeup_thread(mddev->thread); |
336 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ | |
1da177e4 | 337 | } |
390ee602 | 338 | EXPORT_SYMBOL_GPL(mddev_resume); |
1da177e4 | 339 | |
fd01b88c | 340 | int mddev_congested(struct mddev *mddev, int bits) |
3fa841d7 | 341 | { |
5c675f83 N |
342 | struct md_personality *pers = mddev->pers; |
343 | int ret = 0; | |
344 | ||
345 | rcu_read_lock(); | |
346 | if (mddev->suspended) | |
347 | ret = 1; | |
348 | else if (pers && pers->congested) | |
349 | ret = pers->congested(mddev, bits); | |
350 | rcu_read_unlock(); | |
351 | return ret; | |
352 | } | |
353 | EXPORT_SYMBOL_GPL(mddev_congested); | |
354 | static int md_congested(void *data, int bits) | |
355 | { | |
356 | struct mddev *mddev = data; | |
357 | return mddev_congested(mddev, bits); | |
3fa841d7 | 358 | } |
3fa841d7 | 359 | |
a2826aa9 | 360 | /* |
e9c7469b | 361 | * Generic flush handling for md |
a2826aa9 N |
362 | */ |
363 | ||
4246a0b6 | 364 | static void md_end_flush(struct bio *bio) |
a2826aa9 | 365 | { |
3cb03002 | 366 | struct md_rdev *rdev = bio->bi_private; |
fd01b88c | 367 | struct mddev *mddev = rdev->mddev; |
a2826aa9 N |
368 | |
369 | rdev_dec_pending(rdev, mddev); | |
370 | ||
371 | if (atomic_dec_and_test(&mddev->flush_pending)) { | |
e9c7469b | 372 | /* The pre-request flush has finished */ |
e804ac78 | 373 | queue_work(md_wq, &mddev->flush_work); |
a2826aa9 N |
374 | } |
375 | bio_put(bio); | |
376 | } | |
377 | ||
a7a07e69 N |
378 | static void md_submit_flush_data(struct work_struct *ws); |
379 | ||
a035fc3e | 380 | static void submit_flushes(struct work_struct *ws) |
a2826aa9 | 381 | { |
fd01b88c | 382 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
3cb03002 | 383 | struct md_rdev *rdev; |
a2826aa9 | 384 | |
a7a07e69 N |
385 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
386 | atomic_set(&mddev->flush_pending, 1); | |
a2826aa9 | 387 | rcu_read_lock(); |
dafb20fa | 388 | rdev_for_each_rcu(rdev, mddev) |
a2826aa9 N |
389 | if (rdev->raid_disk >= 0 && |
390 | !test_bit(Faulty, &rdev->flags)) { | |
391 | /* Take two references, one is dropped | |
392 | * when request finishes, one after | |
393 | * we reclaim rcu_read_lock | |
394 | */ | |
395 | struct bio *bi; | |
396 | atomic_inc(&rdev->nr_pending); | |
397 | atomic_inc(&rdev->nr_pending); | |
398 | rcu_read_unlock(); | |
b5e1b8ce | 399 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
e9c7469b | 400 | bi->bi_end_io = md_end_flush; |
a2826aa9 N |
401 | bi->bi_private = rdev; |
402 | bi->bi_bdev = rdev->bdev; | |
403 | atomic_inc(&mddev->flush_pending); | |
e9c7469b | 404 | submit_bio(WRITE_FLUSH, bi); |
a2826aa9 N |
405 | rcu_read_lock(); |
406 | rdev_dec_pending(rdev, mddev); | |
407 | } | |
408 | rcu_read_unlock(); | |
a7a07e69 N |
409 | if (atomic_dec_and_test(&mddev->flush_pending)) |
410 | queue_work(md_wq, &mddev->flush_work); | |
a2826aa9 N |
411 | } |
412 | ||
e9c7469b | 413 | static void md_submit_flush_data(struct work_struct *ws) |
a2826aa9 | 414 | { |
fd01b88c | 415 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
e9c7469b | 416 | struct bio *bio = mddev->flush_bio; |
a2826aa9 | 417 | |
4f024f37 | 418 | if (bio->bi_iter.bi_size == 0) |
a2826aa9 | 419 | /* an empty barrier - all done */ |
4246a0b6 | 420 | bio_endio(bio); |
a2826aa9 | 421 | else { |
e9c7469b | 422 | bio->bi_rw &= ~REQ_FLUSH; |
5a7bbad2 | 423 | mddev->pers->make_request(mddev, bio); |
a2826aa9 | 424 | } |
2b74e12e N |
425 | |
426 | mddev->flush_bio = NULL; | |
427 | wake_up(&mddev->sb_wait); | |
a2826aa9 N |
428 | } |
429 | ||
fd01b88c | 430 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
a2826aa9 | 431 | { |
85572d7c | 432 | spin_lock_irq(&mddev->lock); |
a2826aa9 | 433 | wait_event_lock_irq(mddev->sb_wait, |
e9c7469b | 434 | !mddev->flush_bio, |
85572d7c | 435 | mddev->lock); |
e9c7469b | 436 | mddev->flush_bio = bio; |
85572d7c | 437 | spin_unlock_irq(&mddev->lock); |
a2826aa9 | 438 | |
a035fc3e N |
439 | INIT_WORK(&mddev->flush_work, submit_flushes); |
440 | queue_work(md_wq, &mddev->flush_work); | |
a2826aa9 | 441 | } |
e9c7469b | 442 | EXPORT_SYMBOL(md_flush_request); |
409c57f3 | 443 | |
74018dc3 | 444 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
97658cdd | 445 | { |
9cbb1750 N |
446 | struct mddev *mddev = cb->data; |
447 | md_wakeup_thread(mddev->thread); | |
448 | kfree(cb); | |
97658cdd | 449 | } |
9cbb1750 | 450 | EXPORT_SYMBOL(md_unplug); |
2ac87401 | 451 | |
fd01b88c | 452 | static inline struct mddev *mddev_get(struct mddev *mddev) |
1da177e4 LT |
453 | { |
454 | atomic_inc(&mddev->active); | |
455 | return mddev; | |
456 | } | |
457 | ||
5fd3a17e | 458 | static void mddev_delayed_delete(struct work_struct *ws); |
d3374825 | 459 | |
fd01b88c | 460 | static void mddev_put(struct mddev *mddev) |
1da177e4 | 461 | { |
a167f663 N |
462 | struct bio_set *bs = NULL; |
463 | ||
1da177e4 LT |
464 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
465 | return; | |
d3374825 | 466 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
cbd19983 N |
467 | mddev->ctime == 0 && !mddev->hold_active) { |
468 | /* Array is not configured at all, and not held active, | |
469 | * so destroy it */ | |
af8a2434 | 470 | list_del_init(&mddev->all_mddevs); |
a167f663 N |
471 | bs = mddev->bio_set; |
472 | mddev->bio_set = NULL; | |
d3374825 | 473 | if (mddev->gendisk) { |
e804ac78 TH |
474 | /* We did a probe so need to clean up. Call |
475 | * queue_work inside the spinlock so that | |
476 | * flush_workqueue() after mddev_find will | |
477 | * succeed in waiting for the work to be done. | |
d3374825 N |
478 | */ |
479 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); | |
e804ac78 | 480 | queue_work(md_misc_wq, &mddev->del_work); |
d3374825 N |
481 | } else |
482 | kfree(mddev); | |
483 | } | |
484 | spin_unlock(&all_mddevs_lock); | |
a167f663 N |
485 | if (bs) |
486 | bioset_free(bs); | |
1da177e4 LT |
487 | } |
488 | ||
25b2edfa SL |
489 | static void md_safemode_timeout(unsigned long data); |
490 | ||
fd01b88c | 491 | void mddev_init(struct mddev *mddev) |
fafd7fb0 N |
492 | { |
493 | mutex_init(&mddev->open_mutex); | |
494 | mutex_init(&mddev->reconfig_mutex); | |
495 | mutex_init(&mddev->bitmap_info.mutex); | |
496 | INIT_LIST_HEAD(&mddev->disks); | |
497 | INIT_LIST_HEAD(&mddev->all_mddevs); | |
25b2edfa SL |
498 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
499 | (unsigned long) mddev); | |
fafd7fb0 N |
500 | atomic_set(&mddev->active, 1); |
501 | atomic_set(&mddev->openers, 0); | |
502 | atomic_set(&mddev->active_io, 0); | |
85572d7c | 503 | spin_lock_init(&mddev->lock); |
fafd7fb0 N |
504 | atomic_set(&mddev->flush_pending, 0); |
505 | init_waitqueue_head(&mddev->sb_wait); | |
506 | init_waitqueue_head(&mddev->recovery_wait); | |
507 | mddev->reshape_position = MaxSector; | |
2c810cdd | 508 | mddev->reshape_backwards = 0; |
c4a39551 | 509 | mddev->last_sync_action = "none"; |
fafd7fb0 N |
510 | mddev->resync_min = 0; |
511 | mddev->resync_max = MaxSector; | |
512 | mddev->level = LEVEL_NONE; | |
513 | } | |
390ee602 | 514 | EXPORT_SYMBOL_GPL(mddev_init); |
fafd7fb0 | 515 | |
f72ffdd6 | 516 | static struct mddev *mddev_find(dev_t unit) |
1da177e4 | 517 | { |
fd01b88c | 518 | struct mddev *mddev, *new = NULL; |
1da177e4 | 519 | |
8f5f02c4 N |
520 | if (unit && MAJOR(unit) != MD_MAJOR) |
521 | unit &= ~((1<<MdpMinorShift)-1); | |
522 | ||
1da177e4 LT |
523 | retry: |
524 | spin_lock(&all_mddevs_lock); | |
efeb53c0 N |
525 | |
526 | if (unit) { | |
527 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) | |
528 | if (mddev->unit == unit) { | |
529 | mddev_get(mddev); | |
530 | spin_unlock(&all_mddevs_lock); | |
531 | kfree(new); | |
532 | return mddev; | |
533 | } | |
534 | ||
535 | if (new) { | |
536 | list_add(&new->all_mddevs, &all_mddevs); | |
1da177e4 | 537 | spin_unlock(&all_mddevs_lock); |
efeb53c0 N |
538 | new->hold_active = UNTIL_IOCTL; |
539 | return new; | |
1da177e4 | 540 | } |
efeb53c0 N |
541 | } else if (new) { |
542 | /* find an unused unit number */ | |
543 | static int next_minor = 512; | |
544 | int start = next_minor; | |
545 | int is_free = 0; | |
546 | int dev = 0; | |
547 | while (!is_free) { | |
548 | dev = MKDEV(MD_MAJOR, next_minor); | |
549 | next_minor++; | |
550 | if (next_minor > MINORMASK) | |
551 | next_minor = 0; | |
552 | if (next_minor == start) { | |
553 | /* Oh dear, all in use. */ | |
554 | spin_unlock(&all_mddevs_lock); | |
555 | kfree(new); | |
556 | return NULL; | |
557 | } | |
f72ffdd6 | 558 | |
efeb53c0 N |
559 | is_free = 1; |
560 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) | |
561 | if (mddev->unit == dev) { | |
562 | is_free = 0; | |
563 | break; | |
564 | } | |
565 | } | |
566 | new->unit = dev; | |
567 | new->md_minor = MINOR(dev); | |
568 | new->hold_active = UNTIL_STOP; | |
1da177e4 LT |
569 | list_add(&new->all_mddevs, &all_mddevs); |
570 | spin_unlock(&all_mddevs_lock); | |
571 | return new; | |
572 | } | |
573 | spin_unlock(&all_mddevs_lock); | |
574 | ||
9ffae0cf | 575 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
1da177e4 LT |
576 | if (!new) |
577 | return NULL; | |
578 | ||
1da177e4 LT |
579 | new->unit = unit; |
580 | if (MAJOR(unit) == MD_MAJOR) | |
581 | new->md_minor = MINOR(unit); | |
582 | else | |
583 | new->md_minor = MINOR(unit) >> MdpMinorShift; | |
584 | ||
fafd7fb0 | 585 | mddev_init(new); |
1da177e4 | 586 | |
1da177e4 LT |
587 | goto retry; |
588 | } | |
589 | ||
b6eb127d N |
590 | static struct attribute_group md_redundancy_group; |
591 | ||
5c47daf6 | 592 | void mddev_unlock(struct mddev *mddev) |
1da177e4 | 593 | { |
a64c876f | 594 | if (mddev->to_remove) { |
b6eb127d N |
595 | /* These cannot be removed under reconfig_mutex as |
596 | * an access to the files will try to take reconfig_mutex | |
597 | * while holding the file unremovable, which leads to | |
598 | * a deadlock. | |
bb4f1e9d N |
599 | * So hold set sysfs_active while the remove in happeing, |
600 | * and anything else which might set ->to_remove or my | |
601 | * otherwise change the sysfs namespace will fail with | |
602 | * -EBUSY if sysfs_active is still set. | |
603 | * We set sysfs_active under reconfig_mutex and elsewhere | |
604 | * test it under the same mutex to ensure its correct value | |
605 | * is seen. | |
b6eb127d | 606 | */ |
a64c876f N |
607 | struct attribute_group *to_remove = mddev->to_remove; |
608 | mddev->to_remove = NULL; | |
bb4f1e9d | 609 | mddev->sysfs_active = 1; |
b6eb127d N |
610 | mutex_unlock(&mddev->reconfig_mutex); |
611 | ||
00bcb4ac N |
612 | if (mddev->kobj.sd) { |
613 | if (to_remove != &md_redundancy_group) | |
614 | sysfs_remove_group(&mddev->kobj, to_remove); | |
615 | if (mddev->pers == NULL || | |
616 | mddev->pers->sync_request == NULL) { | |
617 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); | |
618 | if (mddev->sysfs_action) | |
619 | sysfs_put(mddev->sysfs_action); | |
620 | mddev->sysfs_action = NULL; | |
621 | } | |
a64c876f | 622 | } |
bb4f1e9d | 623 | mddev->sysfs_active = 0; |
b6eb127d N |
624 | } else |
625 | mutex_unlock(&mddev->reconfig_mutex); | |
1da177e4 | 626 | |
751e67ca CD |
627 | /* As we've dropped the mutex we need a spinlock to |
628 | * make sure the thread doesn't disappear | |
01f96c0a N |
629 | */ |
630 | spin_lock(&pers_lock); | |
005eca5e | 631 | md_wakeup_thread(mddev->thread); |
01f96c0a | 632 | spin_unlock(&pers_lock); |
1da177e4 | 633 | } |
5c47daf6 | 634 | EXPORT_SYMBOL_GPL(mddev_unlock); |
1da177e4 | 635 | |
57d051dc | 636 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
1ca69c4b N |
637 | { |
638 | struct md_rdev *rdev; | |
639 | ||
640 | rdev_for_each_rcu(rdev, mddev) | |
641 | if (rdev->desc_nr == nr) | |
642 | return rdev; | |
643 | ||
644 | return NULL; | |
645 | } | |
57d051dc | 646 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
1ca69c4b N |
647 | |
648 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) | |
1da177e4 | 649 | { |
3cb03002 | 650 | struct md_rdev *rdev; |
1da177e4 | 651 | |
dafb20fa | 652 | rdev_for_each(rdev, mddev) |
1da177e4 LT |
653 | if (rdev->bdev->bd_dev == dev) |
654 | return rdev; | |
159ec1fc | 655 | |
1da177e4 LT |
656 | return NULL; |
657 | } | |
658 | ||
1ca69c4b N |
659 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
660 | { | |
661 | struct md_rdev *rdev; | |
662 | ||
663 | rdev_for_each_rcu(rdev, mddev) | |
664 | if (rdev->bdev->bd_dev == dev) | |
665 | return rdev; | |
666 | ||
667 | return NULL; | |
668 | } | |
669 | ||
84fc4b56 | 670 | static struct md_personality *find_pers(int level, char *clevel) |
2604b703 | 671 | { |
84fc4b56 | 672 | struct md_personality *pers; |
d9d166c2 N |
673 | list_for_each_entry(pers, &pers_list, list) { |
674 | if (level != LEVEL_NONE && pers->level == level) | |
2604b703 | 675 | return pers; |
d9d166c2 N |
676 | if (strcmp(pers->name, clevel)==0) |
677 | return pers; | |
678 | } | |
2604b703 N |
679 | return NULL; |
680 | } | |
681 | ||
b73df2d3 | 682 | /* return the offset of the super block in 512byte sectors */ |
3cb03002 | 683 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
1da177e4 | 684 | { |
57b2caa3 | 685 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
b73df2d3 | 686 | return MD_NEW_SIZE_SECTORS(num_sectors); |
1da177e4 LT |
687 | } |
688 | ||
f72ffdd6 | 689 | static int alloc_disk_sb(struct md_rdev *rdev) |
1da177e4 | 690 | { |
1da177e4 LT |
691 | rdev->sb_page = alloc_page(GFP_KERNEL); |
692 | if (!rdev->sb_page) { | |
693 | printk(KERN_ALERT "md: out of memory.\n"); | |
ebc24337 | 694 | return -ENOMEM; |
1da177e4 LT |
695 | } |
696 | ||
697 | return 0; | |
698 | } | |
699 | ||
545c8795 | 700 | void md_rdev_clear(struct md_rdev *rdev) |
1da177e4 LT |
701 | { |
702 | if (rdev->sb_page) { | |
2d1f3b5d | 703 | put_page(rdev->sb_page); |
1da177e4 LT |
704 | rdev->sb_loaded = 0; |
705 | rdev->sb_page = NULL; | |
0f420358 | 706 | rdev->sb_start = 0; |
dd8ac336 | 707 | rdev->sectors = 0; |
1da177e4 | 708 | } |
2699b672 N |
709 | if (rdev->bb_page) { |
710 | put_page(rdev->bb_page); | |
711 | rdev->bb_page = NULL; | |
712 | } | |
4fa2f327 N |
713 | kfree(rdev->badblocks.page); |
714 | rdev->badblocks.page = NULL; | |
1da177e4 | 715 | } |
545c8795 | 716 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
1da177e4 | 717 | |
4246a0b6 | 718 | static void super_written(struct bio *bio) |
7bfa19f2 | 719 | { |
3cb03002 | 720 | struct md_rdev *rdev = bio->bi_private; |
fd01b88c | 721 | struct mddev *mddev = rdev->mddev; |
7bfa19f2 | 722 | |
4246a0b6 CH |
723 | if (bio->bi_error) { |
724 | printk("md: super_written gets error=%d\n", bio->bi_error); | |
a9701a30 | 725 | md_error(mddev, rdev); |
3a0f5bbb | 726 | } |
7bfa19f2 | 727 | |
a9701a30 N |
728 | if (atomic_dec_and_test(&mddev->pending_writes)) |
729 | wake_up(&mddev->sb_wait); | |
f8b58edf | 730 | bio_put(bio); |
7bfa19f2 N |
731 | } |
732 | ||
fd01b88c | 733 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
7bfa19f2 N |
734 | sector_t sector, int size, struct page *page) |
735 | { | |
736 | /* write first size bytes of page to sector of rdev | |
737 | * Increment mddev->pending_writes before returning | |
738 | * and decrement it on completion, waking up sb_wait | |
739 | * if zero is reached. | |
740 | * If an error occurred, call md_error | |
741 | */ | |
a167f663 | 742 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
7bfa19f2 | 743 | |
a6ff7e08 | 744 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
4f024f37 | 745 | bio->bi_iter.bi_sector = sector; |
7bfa19f2 N |
746 | bio_add_page(bio, page, size, 0); |
747 | bio->bi_private = rdev; | |
748 | bio->bi_end_io = super_written; | |
a9701a30 | 749 | |
7bfa19f2 | 750 | atomic_inc(&mddev->pending_writes); |
a5bf4df0 | 751 | submit_bio(WRITE_FLUSH_FUA, bio); |
a9701a30 N |
752 | } |
753 | ||
fd01b88c | 754 | void md_super_wait(struct mddev *mddev) |
a9701a30 | 755 | { |
e9c7469b | 756 | /* wait for all superblock writes that were scheduled to complete */ |
1967cd56 | 757 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
7bfa19f2 N |
758 | } |
759 | ||
3cb03002 | 760 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
ccebd4c4 | 761 | struct page *page, int rw, bool metadata_op) |
1da177e4 | 762 | { |
a167f663 | 763 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
1da177e4 LT |
764 | int ret; |
765 | ||
a6ff7e08 JB |
766 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
767 | rdev->meta_bdev : rdev->bdev; | |
ccebd4c4 | 768 | if (metadata_op) |
4f024f37 | 769 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
1fdd6fc9 N |
770 | else if (rdev->mddev->reshape_position != MaxSector && |
771 | (rdev->mddev->reshape_backwards == | |
772 | (sector >= rdev->mddev->reshape_position))) | |
4f024f37 | 773 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
ccebd4c4 | 774 | else |
4f024f37 | 775 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
1da177e4 | 776 | bio_add_page(bio, page, size, 0); |
c170bbb4 | 777 | submit_bio_wait(rw, bio); |
1da177e4 | 778 | |
4246a0b6 | 779 | ret = !bio->bi_error; |
1da177e4 LT |
780 | bio_put(bio); |
781 | return ret; | |
782 | } | |
a8745db2 | 783 | EXPORT_SYMBOL_GPL(sync_page_io); |
1da177e4 | 784 | |
f72ffdd6 | 785 | static int read_disk_sb(struct md_rdev *rdev, int size) |
1da177e4 LT |
786 | { |
787 | char b[BDEVNAME_SIZE]; | |
403df478 | 788 | |
1da177e4 LT |
789 | if (rdev->sb_loaded) |
790 | return 0; | |
791 | ||
ccebd4c4 | 792 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
1da177e4 LT |
793 | goto fail; |
794 | rdev->sb_loaded = 1; | |
795 | return 0; | |
796 | ||
797 | fail: | |
798 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", | |
799 | bdevname(rdev->bdev,b)); | |
800 | return -EINVAL; | |
801 | } | |
802 | ||
803 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) | |
804 | { | |
f72ffdd6 | 805 | return sb1->set_uuid0 == sb2->set_uuid0 && |
05710466 AN |
806 | sb1->set_uuid1 == sb2->set_uuid1 && |
807 | sb1->set_uuid2 == sb2->set_uuid2 && | |
808 | sb1->set_uuid3 == sb2->set_uuid3; | |
1da177e4 LT |
809 | } |
810 | ||
1da177e4 LT |
811 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
812 | { | |
813 | int ret; | |
814 | mdp_super_t *tmp1, *tmp2; | |
815 | ||
816 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); | |
817 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); | |
818 | ||
819 | if (!tmp1 || !tmp2) { | |
820 | ret = 0; | |
35020f1a | 821 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
1da177e4 LT |
822 | goto abort; |
823 | } | |
824 | ||
825 | *tmp1 = *sb1; | |
826 | *tmp2 = *sb2; | |
827 | ||
828 | /* | |
829 | * nr_disks is not constant | |
830 | */ | |
831 | tmp1->nr_disks = 0; | |
832 | tmp2->nr_disks = 0; | |
833 | ||
ce0c8e05 | 834 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
1da177e4 | 835 | abort: |
990a8baf JJ |
836 | kfree(tmp1); |
837 | kfree(tmp2); | |
1da177e4 LT |
838 | return ret; |
839 | } | |
840 | ||
4d167f09 N |
841 | static u32 md_csum_fold(u32 csum) |
842 | { | |
843 | csum = (csum & 0xffff) + (csum >> 16); | |
844 | return (csum & 0xffff) + (csum >> 16); | |
845 | } | |
846 | ||
f72ffdd6 | 847 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
1da177e4 | 848 | { |
4d167f09 N |
849 | u64 newcsum = 0; |
850 | u32 *sb32 = (u32*)sb; | |
851 | int i; | |
1da177e4 LT |
852 | unsigned int disk_csum, csum; |
853 | ||
854 | disk_csum = sb->sb_csum; | |
855 | sb->sb_csum = 0; | |
4d167f09 N |
856 | |
857 | for (i = 0; i < MD_SB_BYTES/4 ; i++) | |
858 | newcsum += sb32[i]; | |
859 | csum = (newcsum & 0xffffffff) + (newcsum>>32); | |
860 | ||
4d167f09 N |
861 | #ifdef CONFIG_ALPHA |
862 | /* This used to use csum_partial, which was wrong for several | |
863 | * reasons including that different results are returned on | |
864 | * different architectures. It isn't critical that we get exactly | |
865 | * the same return value as before (we always csum_fold before | |
866 | * testing, and that removes any differences). However as we | |
867 | * know that csum_partial always returned a 16bit value on | |
868 | * alphas, do a fold to maximise conformity to previous behaviour. | |
869 | */ | |
870 | sb->sb_csum = md_csum_fold(disk_csum); | |
871 | #else | |
1da177e4 | 872 | sb->sb_csum = disk_csum; |
4d167f09 | 873 | #endif |
1da177e4 LT |
874 | return csum; |
875 | } | |
876 | ||
1da177e4 LT |
877 | /* |
878 | * Handle superblock details. | |
879 | * We want to be able to handle multiple superblock formats | |
880 | * so we have a common interface to them all, and an array of | |
881 | * different handlers. | |
882 | * We rely on user-space to write the initial superblock, and support | |
883 | * reading and updating of superblocks. | |
884 | * Interface methods are: | |
3cb03002 | 885 | * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
886 | * loads and validates a superblock on dev. |
887 | * if refdev != NULL, compare superblocks on both devices | |
888 | * Return: | |
889 | * 0 - dev has a superblock that is compatible with refdev | |
890 | * 1 - dev has a superblock that is compatible and newer than refdev | |
891 | * so dev should be used as the refdev in future | |
892 | * -EINVAL superblock incompatible or invalid | |
893 | * -othererror e.g. -EIO | |
894 | * | |
fd01b88c | 895 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
1da177e4 LT |
896 | * Verify that dev is acceptable into mddev. |
897 | * The first time, mddev->raid_disks will be 0, and data from | |
898 | * dev should be merged in. Subsequent calls check that dev | |
899 | * is new enough. Return 0 or -EINVAL | |
900 | * | |
fd01b88c | 901 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
1da177e4 LT |
902 | * Update the superblock for rdev with data in mddev |
903 | * This does not write to disc. | |
904 | * | |
905 | */ | |
906 | ||
907 | struct super_type { | |
0cd17fec CW |
908 | char *name; |
909 | struct module *owner; | |
c6563a8c N |
910 | int (*load_super)(struct md_rdev *rdev, |
911 | struct md_rdev *refdev, | |
0cd17fec | 912 | int minor_version); |
c6563a8c N |
913 | int (*validate_super)(struct mddev *mddev, |
914 | struct md_rdev *rdev); | |
915 | void (*sync_super)(struct mddev *mddev, | |
916 | struct md_rdev *rdev); | |
3cb03002 | 917 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
15f4a5fd | 918 | sector_t num_sectors); |
c6563a8c N |
919 | int (*allow_new_offset)(struct md_rdev *rdev, |
920 | unsigned long long new_offset); | |
1da177e4 LT |
921 | }; |
922 | ||
0894cc30 AN |
923 | /* |
924 | * Check that the given mddev has no bitmap. | |
925 | * | |
926 | * This function is called from the run method of all personalities that do not | |
927 | * support bitmaps. It prints an error message and returns non-zero if mddev | |
928 | * has a bitmap. Otherwise, it returns 0. | |
929 | * | |
930 | */ | |
fd01b88c | 931 | int md_check_no_bitmap(struct mddev *mddev) |
0894cc30 | 932 | { |
c3d9714e | 933 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
0894cc30 AN |
934 | return 0; |
935 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", | |
936 | mdname(mddev), mddev->pers->name); | |
937 | return 1; | |
938 | } | |
939 | EXPORT_SYMBOL(md_check_no_bitmap); | |
940 | ||
1da177e4 | 941 | /* |
f72ffdd6 | 942 | * load_super for 0.90.0 |
1da177e4 | 943 | */ |
3cb03002 | 944 | static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
945 | { |
946 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
947 | mdp_super_t *sb; | |
948 | int ret; | |
1da177e4 LT |
949 | |
950 | /* | |
0f420358 | 951 | * Calculate the position of the superblock (512byte sectors), |
1da177e4 LT |
952 | * it's at the end of the disk. |
953 | * | |
954 | * It also happens to be a multiple of 4Kb. | |
955 | */ | |
57b2caa3 | 956 | rdev->sb_start = calc_dev_sboffset(rdev); |
1da177e4 | 957 | |
0002b271 | 958 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
1da177e4 LT |
959 | if (ret) return ret; |
960 | ||
961 | ret = -EINVAL; | |
962 | ||
963 | bdevname(rdev->bdev, b); | |
65a06f06 | 964 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
965 | |
966 | if (sb->md_magic != MD_SB_MAGIC) { | |
967 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", | |
968 | b); | |
969 | goto abort; | |
970 | } | |
971 | ||
972 | if (sb->major_version != 0 || | |
f6705578 N |
973 | sb->minor_version < 90 || |
974 | sb->minor_version > 91) { | |
1da177e4 LT |
975 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
976 | sb->major_version, sb->minor_version, | |
977 | b); | |
978 | goto abort; | |
979 | } | |
980 | ||
981 | if (sb->raid_disks <= 0) | |
982 | goto abort; | |
983 | ||
4d167f09 | 984 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
1da177e4 LT |
985 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
986 | b); | |
987 | goto abort; | |
988 | } | |
989 | ||
990 | rdev->preferred_minor = sb->md_minor; | |
991 | rdev->data_offset = 0; | |
c6563a8c | 992 | rdev->new_data_offset = 0; |
0002b271 | 993 | rdev->sb_size = MD_SB_BYTES; |
9f2f3830 | 994 | rdev->badblocks.shift = -1; |
1da177e4 LT |
995 | |
996 | if (sb->level == LEVEL_MULTIPATH) | |
997 | rdev->desc_nr = -1; | |
998 | else | |
999 | rdev->desc_nr = sb->this_disk.number; | |
1000 | ||
9a7b2b0f | 1001 | if (!refdev) { |
1da177e4 | 1002 | ret = 1; |
9a7b2b0f | 1003 | } else { |
1da177e4 | 1004 | __u64 ev1, ev2; |
65a06f06 | 1005 | mdp_super_t *refsb = page_address(refdev->sb_page); |
1da177e4 LT |
1006 | if (!uuid_equal(refsb, sb)) { |
1007 | printk(KERN_WARNING "md: %s has different UUID to %s\n", | |
1008 | b, bdevname(refdev->bdev,b2)); | |
1009 | goto abort; | |
1010 | } | |
1011 | if (!sb_equal(refsb, sb)) { | |
1012 | printk(KERN_WARNING "md: %s has same UUID" | |
1013 | " but different superblock to %s\n", | |
1014 | b, bdevname(refdev->bdev, b2)); | |
1015 | goto abort; | |
1016 | } | |
1017 | ev1 = md_event(sb); | |
1018 | ev2 = md_event(refsb); | |
1019 | if (ev1 > ev2) | |
1020 | ret = 1; | |
f72ffdd6 | 1021 | else |
1da177e4 LT |
1022 | ret = 0; |
1023 | } | |
8190e754 | 1024 | rdev->sectors = rdev->sb_start; |
667a5313 N |
1025 | /* Limit to 4TB as metadata cannot record more than that. |
1026 | * (not needed for Linear and RAID0 as metadata doesn't | |
1027 | * record this size) | |
1028 | */ | |
3312c951 AB |
1029 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) && |
1030 | sb->level >= 1) | |
1031 | rdev->sectors = (sector_t)(2ULL << 32) - 2; | |
1da177e4 | 1032 | |
27a7b260 | 1033 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
2bf071bf N |
1034 | /* "this cannot possibly happen" ... */ |
1035 | ret = -EINVAL; | |
1036 | ||
1da177e4 LT |
1037 | abort: |
1038 | return ret; | |
1039 | } | |
1040 | ||
1041 | /* | |
1042 | * validate_super for 0.90.0 | |
1043 | */ | |
fd01b88c | 1044 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1045 | { |
1046 | mdp_disk_t *desc; | |
65a06f06 | 1047 | mdp_super_t *sb = page_address(rdev->sb_page); |
07d84d10 | 1048 | __u64 ev1 = md_event(sb); |
1da177e4 | 1049 | |
41158c7e | 1050 | rdev->raid_disk = -1; |
c5d79adb N |
1051 | clear_bit(Faulty, &rdev->flags); |
1052 | clear_bit(In_sync, &rdev->flags); | |
8313b8e5 | 1053 | clear_bit(Bitmap_sync, &rdev->flags); |
c5d79adb | 1054 | clear_bit(WriteMostly, &rdev->flags); |
c5d79adb | 1055 | |
1da177e4 LT |
1056 | if (mddev->raid_disks == 0) { |
1057 | mddev->major_version = 0; | |
1058 | mddev->minor_version = sb->minor_version; | |
1059 | mddev->patch_version = sb->patch_version; | |
e691063a | 1060 | mddev->external = 0; |
9d8f0363 | 1061 | mddev->chunk_sectors = sb->chunk_size >> 9; |
1da177e4 LT |
1062 | mddev->ctime = sb->ctime; |
1063 | mddev->utime = sb->utime; | |
1064 | mddev->level = sb->level; | |
d9d166c2 | 1065 | mddev->clevel[0] = 0; |
1da177e4 LT |
1066 | mddev->layout = sb->layout; |
1067 | mddev->raid_disks = sb->raid_disks; | |
27a7b260 | 1068 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
07d84d10 | 1069 | mddev->events = ev1; |
c3d9714e | 1070 | mddev->bitmap_info.offset = 0; |
6409bb05 N |
1071 | mddev->bitmap_info.space = 0; |
1072 | /* bitmap can use 60 K after the 4K superblocks */ | |
c3d9714e | 1073 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
6409bb05 | 1074 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
2c810cdd | 1075 | mddev->reshape_backwards = 0; |
1da177e4 | 1076 | |
f6705578 N |
1077 | if (mddev->minor_version >= 91) { |
1078 | mddev->reshape_position = sb->reshape_position; | |
1079 | mddev->delta_disks = sb->delta_disks; | |
1080 | mddev->new_level = sb->new_level; | |
1081 | mddev->new_layout = sb->new_layout; | |
664e7c41 | 1082 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
2c810cdd N |
1083 | if (mddev->delta_disks < 0) |
1084 | mddev->reshape_backwards = 1; | |
f6705578 N |
1085 | } else { |
1086 | mddev->reshape_position = MaxSector; | |
1087 | mddev->delta_disks = 0; | |
1088 | mddev->new_level = mddev->level; | |
1089 | mddev->new_layout = mddev->layout; | |
664e7c41 | 1090 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
1091 | } |
1092 | ||
1da177e4 LT |
1093 | if (sb->state & (1<<MD_SB_CLEAN)) |
1094 | mddev->recovery_cp = MaxSector; | |
1095 | else { | |
f72ffdd6 | 1096 | if (sb->events_hi == sb->cp_events_hi && |
1da177e4 LT |
1097 | sb->events_lo == sb->cp_events_lo) { |
1098 | mddev->recovery_cp = sb->recovery_cp; | |
1099 | } else | |
1100 | mddev->recovery_cp = 0; | |
1101 | } | |
1102 | ||
1103 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); | |
1104 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); | |
1105 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); | |
1106 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); | |
1107 | ||
1108 | mddev->max_disks = MD_SB_DISKS; | |
a654b9d8 N |
1109 | |
1110 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && | |
6409bb05 | 1111 | mddev->bitmap_info.file == NULL) { |
c3d9714e N |
1112 | mddev->bitmap_info.offset = |
1113 | mddev->bitmap_info.default_offset; | |
6409bb05 | 1114 | mddev->bitmap_info.space = |
c9ad020f | 1115 | mddev->bitmap_info.default_space; |
6409bb05 | 1116 | } |
a654b9d8 | 1117 | |
41158c7e | 1118 | } else if (mddev->pers == NULL) { |
be6800a7 N |
1119 | /* Insist on good event counter while assembling, except |
1120 | * for spares (which don't need an event count) */ | |
1da177e4 | 1121 | ++ev1; |
be6800a7 N |
1122 | if (sb->disks[rdev->desc_nr].state & ( |
1123 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) | |
f72ffdd6 | 1124 | if (ev1 < mddev->events) |
be6800a7 | 1125 | return -EINVAL; |
41158c7e N |
1126 | } else if (mddev->bitmap) { |
1127 | /* if adding to array with a bitmap, then we can accept an | |
1128 | * older device ... but not too old. | |
1129 | */ | |
41158c7e N |
1130 | if (ev1 < mddev->bitmap->events_cleared) |
1131 | return 0; | |
8313b8e5 N |
1132 | if (ev1 < mddev->events) |
1133 | set_bit(Bitmap_sync, &rdev->flags); | |
07d84d10 N |
1134 | } else { |
1135 | if (ev1 < mddev->events) | |
1136 | /* just a hot-add of a new device, leave raid_disk at -1 */ | |
1137 | return 0; | |
1138 | } | |
41158c7e | 1139 | |
1da177e4 | 1140 | if (mddev->level != LEVEL_MULTIPATH) { |
1da177e4 LT |
1141 | desc = sb->disks + rdev->desc_nr; |
1142 | ||
1143 | if (desc->state & (1<<MD_DISK_FAULTY)) | |
b2d444d7 | 1144 | set_bit(Faulty, &rdev->flags); |
7c7546cc N |
1145 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
1146 | desc->raid_disk < mddev->raid_disks */) { | |
b2d444d7 | 1147 | set_bit(In_sync, &rdev->flags); |
1da177e4 | 1148 | rdev->raid_disk = desc->raid_disk; |
f466722c | 1149 | rdev->saved_raid_disk = desc->raid_disk; |
0261cd9f N |
1150 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
1151 | /* active but not in sync implies recovery up to | |
1152 | * reshape position. We don't know exactly where | |
1153 | * that is, so set to zero for now */ | |
1154 | if (mddev->minor_version >= 91) { | |
1155 | rdev->recovery_offset = 0; | |
1156 | rdev->raid_disk = desc->raid_disk; | |
1157 | } | |
1da177e4 | 1158 | } |
8ddf9efe N |
1159 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
1160 | set_bit(WriteMostly, &rdev->flags); | |
41158c7e | 1161 | } else /* MULTIPATH are always insync */ |
b2d444d7 | 1162 | set_bit(In_sync, &rdev->flags); |
1da177e4 LT |
1163 | return 0; |
1164 | } | |
1165 | ||
1166 | /* | |
1167 | * sync_super for 0.90.0 | |
1168 | */ | |
fd01b88c | 1169 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1170 | { |
1171 | mdp_super_t *sb; | |
3cb03002 | 1172 | struct md_rdev *rdev2; |
1da177e4 | 1173 | int next_spare = mddev->raid_disks; |
19133a42 | 1174 | |
1da177e4 LT |
1175 | /* make rdev->sb match mddev data.. |
1176 | * | |
1177 | * 1/ zero out disks | |
1178 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); | |
1179 | * 3/ any empty disks < next_spare become removed | |
1180 | * | |
1181 | * disks[0] gets initialised to REMOVED because | |
1182 | * we cannot be sure from other fields if it has | |
1183 | * been initialised or not. | |
1184 | */ | |
1185 | int i; | |
1186 | int active=0, working=0,failed=0,spare=0,nr_disks=0; | |
1187 | ||
61181565 N |
1188 | rdev->sb_size = MD_SB_BYTES; |
1189 | ||
65a06f06 | 1190 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1191 | |
1192 | memset(sb, 0, sizeof(*sb)); | |
1193 | ||
1194 | sb->md_magic = MD_SB_MAGIC; | |
1195 | sb->major_version = mddev->major_version; | |
1da177e4 LT |
1196 | sb->patch_version = mddev->patch_version; |
1197 | sb->gvalid_words = 0; /* ignored */ | |
1198 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); | |
1199 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); | |
1200 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); | |
1201 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); | |
1202 | ||
9ebc6ef1 | 1203 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
1da177e4 | 1204 | sb->level = mddev->level; |
58c0fed4 | 1205 | sb->size = mddev->dev_sectors / 2; |
1da177e4 LT |
1206 | sb->raid_disks = mddev->raid_disks; |
1207 | sb->md_minor = mddev->md_minor; | |
e691063a | 1208 | sb->not_persistent = 0; |
9ebc6ef1 | 1209 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
1da177e4 LT |
1210 | sb->state = 0; |
1211 | sb->events_hi = (mddev->events>>32); | |
1212 | sb->events_lo = (u32)mddev->events; | |
1213 | ||
f6705578 N |
1214 | if (mddev->reshape_position == MaxSector) |
1215 | sb->minor_version = 90; | |
1216 | else { | |
1217 | sb->minor_version = 91; | |
1218 | sb->reshape_position = mddev->reshape_position; | |
1219 | sb->new_level = mddev->new_level; | |
1220 | sb->delta_disks = mddev->delta_disks; | |
1221 | sb->new_layout = mddev->new_layout; | |
664e7c41 | 1222 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
f6705578 N |
1223 | } |
1224 | mddev->minor_version = sb->minor_version; | |
1da177e4 LT |
1225 | if (mddev->in_sync) |
1226 | { | |
1227 | sb->recovery_cp = mddev->recovery_cp; | |
1228 | sb->cp_events_hi = (mddev->events>>32); | |
1229 | sb->cp_events_lo = (u32)mddev->events; | |
1230 | if (mddev->recovery_cp == MaxSector) | |
1231 | sb->state = (1<< MD_SB_CLEAN); | |
1232 | } else | |
1233 | sb->recovery_cp = 0; | |
1234 | ||
1235 | sb->layout = mddev->layout; | |
9d8f0363 | 1236 | sb->chunk_size = mddev->chunk_sectors << 9; |
1da177e4 | 1237 | |
c3d9714e | 1238 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
a654b9d8 N |
1239 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
1240 | ||
1da177e4 | 1241 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
dafb20fa | 1242 | rdev_for_each(rdev2, mddev) { |
1da177e4 | 1243 | mdp_disk_t *d; |
86e6ffdd | 1244 | int desc_nr; |
0261cd9f N |
1245 | int is_active = test_bit(In_sync, &rdev2->flags); |
1246 | ||
1247 | if (rdev2->raid_disk >= 0 && | |
1248 | sb->minor_version >= 91) | |
1249 | /* we have nowhere to store the recovery_offset, | |
1250 | * but if it is not below the reshape_position, | |
1251 | * we can piggy-back on that. | |
1252 | */ | |
1253 | is_active = 1; | |
1254 | if (rdev2->raid_disk < 0 || | |
1255 | test_bit(Faulty, &rdev2->flags)) | |
1256 | is_active = 0; | |
1257 | if (is_active) | |
86e6ffdd | 1258 | desc_nr = rdev2->raid_disk; |
1da177e4 | 1259 | else |
86e6ffdd | 1260 | desc_nr = next_spare++; |
19133a42 | 1261 | rdev2->desc_nr = desc_nr; |
1da177e4 LT |
1262 | d = &sb->disks[rdev2->desc_nr]; |
1263 | nr_disks++; | |
1264 | d->number = rdev2->desc_nr; | |
1265 | d->major = MAJOR(rdev2->bdev->bd_dev); | |
1266 | d->minor = MINOR(rdev2->bdev->bd_dev); | |
0261cd9f | 1267 | if (is_active) |
1da177e4 LT |
1268 | d->raid_disk = rdev2->raid_disk; |
1269 | else | |
1270 | d->raid_disk = rdev2->desc_nr; /* compatibility */ | |
1be7892f | 1271 | if (test_bit(Faulty, &rdev2->flags)) |
1da177e4 | 1272 | d->state = (1<<MD_DISK_FAULTY); |
0261cd9f | 1273 | else if (is_active) { |
1da177e4 | 1274 | d->state = (1<<MD_DISK_ACTIVE); |
0261cd9f N |
1275 | if (test_bit(In_sync, &rdev2->flags)) |
1276 | d->state |= (1<<MD_DISK_SYNC); | |
1da177e4 LT |
1277 | active++; |
1278 | working++; | |
1279 | } else { | |
1280 | d->state = 0; | |
1281 | spare++; | |
1282 | working++; | |
1283 | } | |
8ddf9efe N |
1284 | if (test_bit(WriteMostly, &rdev2->flags)) |
1285 | d->state |= (1<<MD_DISK_WRITEMOSTLY); | |
1da177e4 | 1286 | } |
1da177e4 LT |
1287 | /* now set the "removed" and "faulty" bits on any missing devices */ |
1288 | for (i=0 ; i < mddev->raid_disks ; i++) { | |
1289 | mdp_disk_t *d = &sb->disks[i]; | |
1290 | if (d->state == 0 && d->number == 0) { | |
1291 | d->number = i; | |
1292 | d->raid_disk = i; | |
1293 | d->state = (1<<MD_DISK_REMOVED); | |
1294 | d->state |= (1<<MD_DISK_FAULTY); | |
1295 | failed++; | |
1296 | } | |
1297 | } | |
1298 | sb->nr_disks = nr_disks; | |
1299 | sb->active_disks = active; | |
1300 | sb->working_disks = working; | |
1301 | sb->failed_disks = failed; | |
1302 | sb->spare_disks = spare; | |
1303 | ||
1304 | sb->this_disk = sb->disks[rdev->desc_nr]; | |
1305 | sb->sb_csum = calc_sb_csum(sb); | |
1306 | } | |
1307 | ||
0cd17fec CW |
1308 | /* |
1309 | * rdev_size_change for 0.90.0 | |
1310 | */ | |
1311 | static unsigned long long | |
3cb03002 | 1312 | super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
0cd17fec | 1313 | { |
58c0fed4 | 1314 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
0cd17fec | 1315 | return 0; /* component must fit device */ |
c3d9714e | 1316 | if (rdev->mddev->bitmap_info.offset) |
0cd17fec | 1317 | return 0; /* can't move bitmap */ |
57b2caa3 | 1318 | rdev->sb_start = calc_dev_sboffset(rdev); |
15f4a5fd AN |
1319 | if (!num_sectors || num_sectors > rdev->sb_start) |
1320 | num_sectors = rdev->sb_start; | |
27a7b260 N |
1321 | /* Limit to 4TB as metadata cannot record more than that. |
1322 | * 4TB == 2^32 KB, or 2*2^32 sectors. | |
1323 | */ | |
3312c951 AB |
1324 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) && |
1325 | rdev->mddev->level >= 1) | |
1326 | num_sectors = (sector_t)(2ULL << 32) - 2; | |
0f420358 | 1327 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
0cd17fec CW |
1328 | rdev->sb_page); |
1329 | md_super_wait(rdev->mddev); | |
c26a44ed | 1330 | return num_sectors; |
0cd17fec CW |
1331 | } |
1332 | ||
c6563a8c N |
1333 | static int |
1334 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) | |
1335 | { | |
1336 | /* non-zero offset changes not possible with v0.90 */ | |
1337 | return new_offset == 0; | |
1338 | } | |
0cd17fec | 1339 | |
1da177e4 LT |
1340 | /* |
1341 | * version 1 superblock | |
1342 | */ | |
1343 | ||
f72ffdd6 | 1344 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
1da177e4 | 1345 | { |
1c05b4bc N |
1346 | __le32 disk_csum; |
1347 | u32 csum; | |
1da177e4 LT |
1348 | unsigned long long newcsum; |
1349 | int size = 256 + le32_to_cpu(sb->max_dev)*2; | |
1c05b4bc | 1350 | __le32 *isuper = (__le32*)sb; |
1da177e4 LT |
1351 | |
1352 | disk_csum = sb->sb_csum; | |
1353 | sb->sb_csum = 0; | |
1354 | newcsum = 0; | |
1f3c9907 | 1355 | for (; size >= 4; size -= 4) |
1da177e4 LT |
1356 | newcsum += le32_to_cpu(*isuper++); |
1357 | ||
1358 | if (size == 2) | |
1c05b4bc | 1359 | newcsum += le16_to_cpu(*(__le16*) isuper); |
1da177e4 LT |
1360 | |
1361 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); | |
1362 | sb->sb_csum = disk_csum; | |
1363 | return cpu_to_le32(csum); | |
1364 | } | |
1365 | ||
2699b672 N |
1366 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
1367 | int acknowledged); | |
3cb03002 | 1368 | static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
1369 | { |
1370 | struct mdp_superblock_1 *sb; | |
1371 | int ret; | |
0f420358 | 1372 | sector_t sb_start; |
c6563a8c | 1373 | sector_t sectors; |
1da177e4 | 1374 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
0002b271 | 1375 | int bmask; |
1da177e4 LT |
1376 | |
1377 | /* | |
0f420358 | 1378 | * Calculate the position of the superblock in 512byte sectors. |
1da177e4 LT |
1379 | * It is always aligned to a 4K boundary and |
1380 | * depeding on minor_version, it can be: | |
1381 | * 0: At least 8K, but less than 12K, from end of device | |
1382 | * 1: At start of device | |
1383 | * 2: 4K from start of device. | |
1384 | */ | |
1385 | switch(minor_version) { | |
1386 | case 0: | |
77304d2a | 1387 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
0f420358 AN |
1388 | sb_start -= 8*2; |
1389 | sb_start &= ~(sector_t)(4*2-1); | |
1da177e4 LT |
1390 | break; |
1391 | case 1: | |
0f420358 | 1392 | sb_start = 0; |
1da177e4 LT |
1393 | break; |
1394 | case 2: | |
0f420358 | 1395 | sb_start = 8; |
1da177e4 LT |
1396 | break; |
1397 | default: | |
1398 | return -EINVAL; | |
1399 | } | |
0f420358 | 1400 | rdev->sb_start = sb_start; |
1da177e4 | 1401 | |
0002b271 N |
1402 | /* superblock is rarely larger than 1K, but it can be larger, |
1403 | * and it is safe to read 4k, so we do that | |
1404 | */ | |
1405 | ret = read_disk_sb(rdev, 4096); | |
1da177e4 LT |
1406 | if (ret) return ret; |
1407 | ||
65a06f06 | 1408 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1409 | |
1410 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || | |
1411 | sb->major_version != cpu_to_le32(1) || | |
1412 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || | |
0f420358 | 1413 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
71c0805c | 1414 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
1da177e4 LT |
1415 | return -EINVAL; |
1416 | ||
1417 | if (calc_sb_1_csum(sb) != sb->sb_csum) { | |
1418 | printk("md: invalid superblock checksum on %s\n", | |
1419 | bdevname(rdev->bdev,b)); | |
1420 | return -EINVAL; | |
1421 | } | |
1422 | if (le64_to_cpu(sb->data_size) < 10) { | |
1423 | printk("md: data_size too small on %s\n", | |
1424 | bdevname(rdev->bdev,b)); | |
1425 | return -EINVAL; | |
1426 | } | |
c6563a8c N |
1427 | if (sb->pad0 || |
1428 | sb->pad3[0] || | |
1429 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) | |
1430 | /* Some padding is non-zero, might be a new feature */ | |
1431 | return -EINVAL; | |
e11e93fa | 1432 | |
1da177e4 LT |
1433 | rdev->preferred_minor = 0xffff; |
1434 | rdev->data_offset = le64_to_cpu(sb->data_offset); | |
c6563a8c N |
1435 | rdev->new_data_offset = rdev->data_offset; |
1436 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && | |
1437 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) | |
1438 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); | |
4dbcdc75 | 1439 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
1da177e4 | 1440 | |
0002b271 | 1441 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
e1defc4f | 1442 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
0002b271 | 1443 | if (rdev->sb_size & bmask) |
a1801f85 N |
1444 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
1445 | ||
1446 | if (minor_version | |
0f420358 | 1447 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
a1801f85 | 1448 | return -EINVAL; |
c6563a8c N |
1449 | if (minor_version |
1450 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) | |
1451 | return -EINVAL; | |
0002b271 | 1452 | |
31b65a0d N |
1453 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
1454 | rdev->desc_nr = -1; | |
1455 | else | |
1456 | rdev->desc_nr = le32_to_cpu(sb->dev_number); | |
1457 | ||
2699b672 N |
1458 | if (!rdev->bb_page) { |
1459 | rdev->bb_page = alloc_page(GFP_KERNEL); | |
1460 | if (!rdev->bb_page) | |
1461 | return -ENOMEM; | |
1462 | } | |
1463 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && | |
1464 | rdev->badblocks.count == 0) { | |
1465 | /* need to load the bad block list. | |
1466 | * Currently we limit it to one page. | |
1467 | */ | |
1468 | s32 offset; | |
1469 | sector_t bb_sector; | |
1470 | u64 *bbp; | |
1471 | int i; | |
1472 | int sectors = le16_to_cpu(sb->bblog_size); | |
1473 | if (sectors > (PAGE_SIZE / 512)) | |
1474 | return -EINVAL; | |
1475 | offset = le32_to_cpu(sb->bblog_offset); | |
1476 | if (offset == 0) | |
1477 | return -EINVAL; | |
1478 | bb_sector = (long long)offset; | |
1479 | if (!sync_page_io(rdev, bb_sector, sectors << 9, | |
1480 | rdev->bb_page, READ, true)) | |
1481 | return -EIO; | |
1482 | bbp = (u64 *)page_address(rdev->bb_page); | |
1483 | rdev->badblocks.shift = sb->bblog_shift; | |
1484 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { | |
1485 | u64 bb = le64_to_cpu(*bbp); | |
1486 | int count = bb & (0x3ff); | |
1487 | u64 sector = bb >> 10; | |
1488 | sector <<= sb->bblog_shift; | |
1489 | count <<= sb->bblog_shift; | |
1490 | if (bb + 1 == 0) | |
1491 | break; | |
1492 | if (md_set_badblocks(&rdev->badblocks, | |
1493 | sector, count, 1) == 0) | |
1494 | return -EINVAL; | |
1495 | } | |
486adf72 N |
1496 | } else if (sb->bblog_offset != 0) |
1497 | rdev->badblocks.shift = 0; | |
2699b672 | 1498 | |
9a7b2b0f | 1499 | if (!refdev) { |
8ed75463 | 1500 | ret = 1; |
9a7b2b0f | 1501 | } else { |
1da177e4 | 1502 | __u64 ev1, ev2; |
65a06f06 | 1503 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
1da177e4 LT |
1504 | |
1505 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || | |
1506 | sb->level != refsb->level || | |
1507 | sb->layout != refsb->layout || | |
1508 | sb->chunksize != refsb->chunksize) { | |
1509 | printk(KERN_WARNING "md: %s has strangely different" | |
1510 | " superblock to %s\n", | |
1511 | bdevname(rdev->bdev,b), | |
1512 | bdevname(refdev->bdev,b2)); | |
1513 | return -EINVAL; | |
1514 | } | |
1515 | ev1 = le64_to_cpu(sb->events); | |
1516 | ev2 = le64_to_cpu(refsb->events); | |
1517 | ||
1518 | if (ev1 > ev2) | |
8ed75463 N |
1519 | ret = 1; |
1520 | else | |
1521 | ret = 0; | |
1da177e4 | 1522 | } |
c6563a8c N |
1523 | if (minor_version) { |
1524 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); | |
1525 | sectors -= rdev->data_offset; | |
1526 | } else | |
1527 | sectors = rdev->sb_start; | |
1528 | if (sectors < le64_to_cpu(sb->data_size)) | |
1da177e4 | 1529 | return -EINVAL; |
dd8ac336 | 1530 | rdev->sectors = le64_to_cpu(sb->data_size); |
8ed75463 | 1531 | return ret; |
1da177e4 LT |
1532 | } |
1533 | ||
fd01b88c | 1534 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1535 | { |
65a06f06 | 1536 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
07d84d10 | 1537 | __u64 ev1 = le64_to_cpu(sb->events); |
1da177e4 | 1538 | |
41158c7e | 1539 | rdev->raid_disk = -1; |
c5d79adb N |
1540 | clear_bit(Faulty, &rdev->flags); |
1541 | clear_bit(In_sync, &rdev->flags); | |
8313b8e5 | 1542 | clear_bit(Bitmap_sync, &rdev->flags); |
c5d79adb | 1543 | clear_bit(WriteMostly, &rdev->flags); |
c5d79adb | 1544 | |
1da177e4 LT |
1545 | if (mddev->raid_disks == 0) { |
1546 | mddev->major_version = 1; | |
1547 | mddev->patch_version = 0; | |
e691063a | 1548 | mddev->external = 0; |
9d8f0363 | 1549 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
9ebc6ef1 DD |
1550 | mddev->ctime = le64_to_cpu(sb->ctime); |
1551 | mddev->utime = le64_to_cpu(sb->utime); | |
1da177e4 | 1552 | mddev->level = le32_to_cpu(sb->level); |
d9d166c2 | 1553 | mddev->clevel[0] = 0; |
1da177e4 LT |
1554 | mddev->layout = le32_to_cpu(sb->layout); |
1555 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); | |
58c0fed4 | 1556 | mddev->dev_sectors = le64_to_cpu(sb->size); |
07d84d10 | 1557 | mddev->events = ev1; |
c3d9714e | 1558 | mddev->bitmap_info.offset = 0; |
6409bb05 N |
1559 | mddev->bitmap_info.space = 0; |
1560 | /* Default location for bitmap is 1K after superblock | |
1561 | * using 3K - total of 4K | |
1562 | */ | |
c3d9714e | 1563 | mddev->bitmap_info.default_offset = 1024 >> 9; |
6409bb05 | 1564 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
2c810cdd N |
1565 | mddev->reshape_backwards = 0; |
1566 | ||
1da177e4 LT |
1567 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
1568 | memcpy(mddev->uuid, sb->set_uuid, 16); | |
1569 | ||
1570 | mddev->max_disks = (4096-256)/2; | |
a654b9d8 | 1571 | |
71c0805c | 1572 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
6409bb05 | 1573 | mddev->bitmap_info.file == NULL) { |
c3d9714e N |
1574 | mddev->bitmap_info.offset = |
1575 | (__s32)le32_to_cpu(sb->bitmap_offset); | |
6409bb05 N |
1576 | /* Metadata doesn't record how much space is available. |
1577 | * For 1.0, we assume we can use up to the superblock | |
1578 | * if before, else to 4K beyond superblock. | |
1579 | * For others, assume no change is possible. | |
1580 | */ | |
1581 | if (mddev->minor_version > 0) | |
1582 | mddev->bitmap_info.space = 0; | |
1583 | else if (mddev->bitmap_info.offset > 0) | |
1584 | mddev->bitmap_info.space = | |
1585 | 8 - mddev->bitmap_info.offset; | |
1586 | else | |
1587 | mddev->bitmap_info.space = | |
1588 | -mddev->bitmap_info.offset; | |
1589 | } | |
e11e93fa | 1590 | |
f6705578 N |
1591 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
1592 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); | |
1593 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); | |
1594 | mddev->new_level = le32_to_cpu(sb->new_level); | |
1595 | mddev->new_layout = le32_to_cpu(sb->new_layout); | |
664e7c41 | 1596 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
2c810cdd N |
1597 | if (mddev->delta_disks < 0 || |
1598 | (mddev->delta_disks == 0 && | |
1599 | (le32_to_cpu(sb->feature_map) | |
1600 | & MD_FEATURE_RESHAPE_BACKWARDS))) | |
1601 | mddev->reshape_backwards = 1; | |
f6705578 N |
1602 | } else { |
1603 | mddev->reshape_position = MaxSector; | |
1604 | mddev->delta_disks = 0; | |
1605 | mddev->new_level = mddev->level; | |
1606 | mddev->new_layout = mddev->layout; | |
664e7c41 | 1607 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
1608 | } |
1609 | ||
41158c7e | 1610 | } else if (mddev->pers == NULL) { |
be6800a7 N |
1611 | /* Insist of good event counter while assembling, except for |
1612 | * spares (which don't need an event count) */ | |
1da177e4 | 1613 | ++ev1; |
be6800a7 N |
1614 | if (rdev->desc_nr >= 0 && |
1615 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && | |
a3dfbdaa SL |
1616 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
1617 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) | |
be6800a7 N |
1618 | if (ev1 < mddev->events) |
1619 | return -EINVAL; | |
41158c7e N |
1620 | } else if (mddev->bitmap) { |
1621 | /* If adding to array with a bitmap, then we can accept an | |
1622 | * older device, but not too old. | |
1623 | */ | |
41158c7e N |
1624 | if (ev1 < mddev->bitmap->events_cleared) |
1625 | return 0; | |
8313b8e5 N |
1626 | if (ev1 < mddev->events) |
1627 | set_bit(Bitmap_sync, &rdev->flags); | |
07d84d10 N |
1628 | } else { |
1629 | if (ev1 < mddev->events) | |
1630 | /* just a hot-add of a new device, leave raid_disk at -1 */ | |
1631 | return 0; | |
1632 | } | |
1da177e4 LT |
1633 | if (mddev->level != LEVEL_MULTIPATH) { |
1634 | int role; | |
3673f305 N |
1635 | if (rdev->desc_nr < 0 || |
1636 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { | |
c4d4c91b | 1637 | role = MD_DISK_ROLE_SPARE; |
3673f305 N |
1638 | rdev->desc_nr = -1; |
1639 | } else | |
1640 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); | |
1da177e4 | 1641 | switch(role) { |
c4d4c91b | 1642 | case MD_DISK_ROLE_SPARE: /* spare */ |
1da177e4 | 1643 | break; |
c4d4c91b | 1644 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
b2d444d7 | 1645 | set_bit(Faulty, &rdev->flags); |
1da177e4 | 1646 | break; |
bac624f3 SL |
1647 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
1648 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { | |
1649 | /* journal device without journal feature */ | |
1650 | printk(KERN_WARNING | |
1651 | "md: journal device provided without journal feature, ignoring the device\n"); | |
1652 | return -EINVAL; | |
1653 | } | |
1654 | set_bit(Journal, &rdev->flags); | |
3069aa8d | 1655 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
bd18f646 SL |
1656 | if (mddev->recovery_cp == MaxSector) |
1657 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); | |
9b15603d | 1658 | rdev->raid_disk = 0; |
bac624f3 | 1659 | break; |
1da177e4 | 1660 | default: |
f466722c | 1661 | rdev->saved_raid_disk = role; |
5fd6c1dc | 1662 | if ((le32_to_cpu(sb->feature_map) & |
f466722c | 1663 | MD_FEATURE_RECOVERY_OFFSET)) { |
5fd6c1dc | 1664 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
f466722c N |
1665 | if (!(le32_to_cpu(sb->feature_map) & |
1666 | MD_FEATURE_RECOVERY_BITMAP)) | |
1667 | rdev->saved_raid_disk = -1; | |
1668 | } else | |
5fd6c1dc | 1669 | set_bit(In_sync, &rdev->flags); |
1da177e4 LT |
1670 | rdev->raid_disk = role; |
1671 | break; | |
1672 | } | |
8ddf9efe N |
1673 | if (sb->devflags & WriteMostly1) |
1674 | set_bit(WriteMostly, &rdev->flags); | |
2d78f8c4 N |
1675 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
1676 | set_bit(Replacement, &rdev->flags); | |
a97b7896 SL |
1677 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
1678 | set_bit(MD_HAS_JOURNAL, &mddev->flags); | |
41158c7e | 1679 | } else /* MULTIPATH are always insync */ |
b2d444d7 | 1680 | set_bit(In_sync, &rdev->flags); |
41158c7e | 1681 | |
1da177e4 LT |
1682 | return 0; |
1683 | } | |
1684 | ||
fd01b88c | 1685 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1686 | { |
1687 | struct mdp_superblock_1 *sb; | |
3cb03002 | 1688 | struct md_rdev *rdev2; |
1da177e4 LT |
1689 | int max_dev, i; |
1690 | /* make rdev->sb match mddev and rdev data. */ | |
1691 | ||
65a06f06 | 1692 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1693 | |
1694 | sb->feature_map = 0; | |
1695 | sb->pad0 = 0; | |
5fd6c1dc | 1696 | sb->recovery_offset = cpu_to_le64(0); |
1da177e4 LT |
1697 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
1698 | ||
1699 | sb->utime = cpu_to_le64((__u64)mddev->utime); | |
1700 | sb->events = cpu_to_le64(mddev->events); | |
1701 | if (mddev->in_sync) | |
1702 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); | |
bd18f646 SL |
1703 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
1704 | sb->resync_offset = cpu_to_le64(MaxSector); | |
1da177e4 LT |
1705 | else |
1706 | sb->resync_offset = cpu_to_le64(0); | |
1707 | ||
1c05b4bc | 1708 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
4dbcdc75 | 1709 | |
f0ca340c | 1710 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
58c0fed4 | 1711 | sb->size = cpu_to_le64(mddev->dev_sectors); |
9d8f0363 | 1712 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
62e1e389 N |
1713 | sb->level = cpu_to_le32(mddev->level); |
1714 | sb->layout = cpu_to_le32(mddev->layout); | |
f0ca340c | 1715 | |
aeb9b211 N |
1716 | if (test_bit(WriteMostly, &rdev->flags)) |
1717 | sb->devflags |= WriteMostly1; | |
1718 | else | |
1719 | sb->devflags &= ~WriteMostly1; | |
c6563a8c N |
1720 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
1721 | sb->data_size = cpu_to_le64(rdev->sectors); | |
aeb9b211 | 1722 | |
c3d9714e N |
1723 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
1724 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); | |
71c0805c | 1725 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
a654b9d8 | 1726 | } |
5fd6c1dc | 1727 | |
f2076e7d | 1728 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
97e4f42d | 1729 | !test_bit(In_sync, &rdev->flags)) { |
93be75ff N |
1730 | sb->feature_map |= |
1731 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); | |
1732 | sb->recovery_offset = | |
1733 | cpu_to_le64(rdev->recovery_offset); | |
f466722c N |
1734 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
1735 | sb->feature_map |= | |
1736 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); | |
5fd6c1dc | 1737 | } |
3069aa8d SL |
1738 | /* Note: recovery_offset and journal_tail share space */ |
1739 | if (test_bit(Journal, &rdev->flags)) | |
1740 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); | |
2d78f8c4 N |
1741 | if (test_bit(Replacement, &rdev->flags)) |
1742 | sb->feature_map |= | |
1743 | cpu_to_le32(MD_FEATURE_REPLACEMENT); | |
5fd6c1dc | 1744 | |
f6705578 N |
1745 | if (mddev->reshape_position != MaxSector) { |
1746 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); | |
1747 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); | |
1748 | sb->new_layout = cpu_to_le32(mddev->new_layout); | |
1749 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); | |
1750 | sb->new_level = cpu_to_le32(mddev->new_level); | |
664e7c41 | 1751 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
2c810cdd N |
1752 | if (mddev->delta_disks == 0 && |
1753 | mddev->reshape_backwards) | |
1754 | sb->feature_map | |
1755 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); | |
c6563a8c N |
1756 | if (rdev->new_data_offset != rdev->data_offset) { |
1757 | sb->feature_map | |
1758 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); | |
1759 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset | |
1760 | - rdev->data_offset)); | |
1761 | } | |
f6705578 | 1762 | } |
a654b9d8 | 1763 | |
3c462c88 GR |
1764 | if (mddev_is_clustered(mddev)) |
1765 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); | |
1766 | ||
2699b672 N |
1767 | if (rdev->badblocks.count == 0) |
1768 | /* Nothing to do for bad blocks*/ ; | |
1769 | else if (sb->bblog_offset == 0) | |
1770 | /* Cannot record bad blocks on this device */ | |
1771 | md_error(mddev, rdev); | |
1772 | else { | |
1773 | struct badblocks *bb = &rdev->badblocks; | |
1774 | u64 *bbp = (u64 *)page_address(rdev->bb_page); | |
1775 | u64 *p = bb->page; | |
1776 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); | |
1777 | if (bb->changed) { | |
1778 | unsigned seq; | |
1779 | ||
1780 | retry: | |
1781 | seq = read_seqbegin(&bb->lock); | |
1782 | ||
1783 | memset(bbp, 0xff, PAGE_SIZE); | |
1784 | ||
1785 | for (i = 0 ; i < bb->count ; i++) { | |
35f9ac2d | 1786 | u64 internal_bb = p[i]; |
2699b672 N |
1787 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
1788 | | BB_LEN(internal_bb)); | |
35f9ac2d | 1789 | bbp[i] = cpu_to_le64(store_bb); |
2699b672 | 1790 | } |
d0962936 | 1791 | bb->changed = 0; |
2699b672 N |
1792 | if (read_seqretry(&bb->lock, seq)) |
1793 | goto retry; | |
1794 | ||
1795 | bb->sector = (rdev->sb_start + | |
1796 | (int)le32_to_cpu(sb->bblog_offset)); | |
1797 | bb->size = le16_to_cpu(sb->bblog_size); | |
2699b672 N |
1798 | } |
1799 | } | |
1800 | ||
1da177e4 | 1801 | max_dev = 0; |
dafb20fa | 1802 | rdev_for_each(rdev2, mddev) |
1da177e4 LT |
1803 | if (rdev2->desc_nr+1 > max_dev) |
1804 | max_dev = rdev2->desc_nr+1; | |
a778b73f | 1805 | |
70471daf N |
1806 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
1807 | int bmask; | |
a778b73f | 1808 | sb->max_dev = cpu_to_le32(max_dev); |
70471daf N |
1809 | rdev->sb_size = max_dev * 2 + 256; |
1810 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; | |
1811 | if (rdev->sb_size & bmask) | |
1812 | rdev->sb_size = (rdev->sb_size | bmask) + 1; | |
ddcf3522 N |
1813 | } else |
1814 | max_dev = le32_to_cpu(sb->max_dev); | |
1815 | ||
1da177e4 | 1816 | for (i=0; i<max_dev;i++) |
c4d4c91b | 1817 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
f72ffdd6 | 1818 | |
a97b7896 SL |
1819 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
1820 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); | |
f72ffdd6 | 1821 | |
dafb20fa | 1822 | rdev_for_each(rdev2, mddev) { |
1da177e4 | 1823 | i = rdev2->desc_nr; |
b2d444d7 | 1824 | if (test_bit(Faulty, &rdev2->flags)) |
c4d4c91b | 1825 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
b2d444d7 | 1826 | else if (test_bit(In_sync, &rdev2->flags)) |
1da177e4 | 1827 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
a97b7896 | 1828 | else if (test_bit(Journal, &rdev2->flags)) |
bac624f3 | 1829 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
93be75ff | 1830 | else if (rdev2->raid_disk >= 0) |
5fd6c1dc | 1831 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
1da177e4 | 1832 | else |
c4d4c91b | 1833 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
1da177e4 LT |
1834 | } |
1835 | ||
1da177e4 LT |
1836 | sb->sb_csum = calc_sb_1_csum(sb); |
1837 | } | |
1838 | ||
0cd17fec | 1839 | static unsigned long long |
3cb03002 | 1840 | super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
0cd17fec CW |
1841 | { |
1842 | struct mdp_superblock_1 *sb; | |
15f4a5fd | 1843 | sector_t max_sectors; |
58c0fed4 | 1844 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
0cd17fec | 1845 | return 0; /* component must fit device */ |
c6563a8c N |
1846 | if (rdev->data_offset != rdev->new_data_offset) |
1847 | return 0; /* too confusing */ | |
0f420358 | 1848 | if (rdev->sb_start < rdev->data_offset) { |
0cd17fec | 1849 | /* minor versions 1 and 2; superblock before data */ |
77304d2a | 1850 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
15f4a5fd AN |
1851 | max_sectors -= rdev->data_offset; |
1852 | if (!num_sectors || num_sectors > max_sectors) | |
1853 | num_sectors = max_sectors; | |
c3d9714e | 1854 | } else if (rdev->mddev->bitmap_info.offset) { |
0cd17fec CW |
1855 | /* minor version 0 with bitmap we can't move */ |
1856 | return 0; | |
1857 | } else { | |
1858 | /* minor version 0; superblock after data */ | |
0f420358 | 1859 | sector_t sb_start; |
77304d2a | 1860 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
0f420358 | 1861 | sb_start &= ~(sector_t)(4*2 - 1); |
dd8ac336 | 1862 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
15f4a5fd AN |
1863 | if (!num_sectors || num_sectors > max_sectors) |
1864 | num_sectors = max_sectors; | |
0f420358 | 1865 | rdev->sb_start = sb_start; |
0cd17fec | 1866 | } |
65a06f06 | 1867 | sb = page_address(rdev->sb_page); |
15f4a5fd | 1868 | sb->data_size = cpu_to_le64(num_sectors); |
0f420358 | 1869 | sb->super_offset = rdev->sb_start; |
0cd17fec | 1870 | sb->sb_csum = calc_sb_1_csum(sb); |
0f420358 | 1871 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
0cd17fec CW |
1872 | rdev->sb_page); |
1873 | md_super_wait(rdev->mddev); | |
c26a44ed | 1874 | return num_sectors; |
c6563a8c N |
1875 | |
1876 | } | |
1877 | ||
1878 | static int | |
1879 | super_1_allow_new_offset(struct md_rdev *rdev, | |
1880 | unsigned long long new_offset) | |
1881 | { | |
1882 | /* All necessary checks on new >= old have been done */ | |
1883 | struct bitmap *bitmap; | |
1884 | if (new_offset >= rdev->data_offset) | |
1885 | return 1; | |
1886 | ||
1887 | /* with 1.0 metadata, there is no metadata to tread on | |
1888 | * so we can always move back */ | |
1889 | if (rdev->mddev->minor_version == 0) | |
1890 | return 1; | |
1891 | ||
1892 | /* otherwise we must be sure not to step on | |
1893 | * any metadata, so stay: | |
1894 | * 36K beyond start of superblock | |
1895 | * beyond end of badblocks | |
1896 | * beyond write-intent bitmap | |
1897 | */ | |
1898 | if (rdev->sb_start + (32+4)*2 > new_offset) | |
1899 | return 0; | |
1900 | bitmap = rdev->mddev->bitmap; | |
1901 | if (bitmap && !rdev->mddev->bitmap_info.file && | |
1902 | rdev->sb_start + rdev->mddev->bitmap_info.offset + | |
1ec885cd | 1903 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
c6563a8c N |
1904 | return 0; |
1905 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) | |
1906 | return 0; | |
1907 | ||
1908 | return 1; | |
0cd17fec | 1909 | } |
1da177e4 | 1910 | |
75c96f85 | 1911 | static struct super_type super_types[] = { |
1da177e4 LT |
1912 | [0] = { |
1913 | .name = "0.90.0", | |
1914 | .owner = THIS_MODULE, | |
0cd17fec CW |
1915 | .load_super = super_90_load, |
1916 | .validate_super = super_90_validate, | |
1917 | .sync_super = super_90_sync, | |
1918 | .rdev_size_change = super_90_rdev_size_change, | |
c6563a8c | 1919 | .allow_new_offset = super_90_allow_new_offset, |
1da177e4 LT |
1920 | }, |
1921 | [1] = { | |
1922 | .name = "md-1", | |
1923 | .owner = THIS_MODULE, | |
0cd17fec CW |
1924 | .load_super = super_1_load, |
1925 | .validate_super = super_1_validate, | |
1926 | .sync_super = super_1_sync, | |
1927 | .rdev_size_change = super_1_rdev_size_change, | |
c6563a8c | 1928 | .allow_new_offset = super_1_allow_new_offset, |
1da177e4 LT |
1929 | }, |
1930 | }; | |
1da177e4 | 1931 | |
fd01b88c | 1932 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
076f968b JB |
1933 | { |
1934 | if (mddev->sync_super) { | |
1935 | mddev->sync_super(mddev, rdev); | |
1936 | return; | |
1937 | } | |
1938 | ||
1939 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); | |
1940 | ||
1941 | super_types[mddev->major_version].sync_super(mddev, rdev); | |
1942 | } | |
1943 | ||
fd01b88c | 1944 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
1da177e4 | 1945 | { |
3cb03002 | 1946 | struct md_rdev *rdev, *rdev2; |
1da177e4 | 1947 | |
4b80991c | 1948 | rcu_read_lock(); |
0b020e85 SL |
1949 | rdev_for_each_rcu(rdev, mddev1) { |
1950 | if (test_bit(Faulty, &rdev->flags) || | |
1951 | test_bit(Journal, &rdev->flags) || | |
1952 | rdev->raid_disk == -1) | |
1953 | continue; | |
1954 | rdev_for_each_rcu(rdev2, mddev2) { | |
1955 | if (test_bit(Faulty, &rdev2->flags) || | |
1956 | test_bit(Journal, &rdev2->flags) || | |
1957 | rdev2->raid_disk == -1) | |
1958 | continue; | |
7dd5e7c3 | 1959 | if (rdev->bdev->bd_contains == |
4b80991c N |
1960 | rdev2->bdev->bd_contains) { |
1961 | rcu_read_unlock(); | |
7dd5e7c3 | 1962 | return 1; |
4b80991c | 1963 | } |
0b020e85 SL |
1964 | } |
1965 | } | |
4b80991c | 1966 | rcu_read_unlock(); |
1da177e4 LT |
1967 | return 0; |
1968 | } | |
1969 | ||
1970 | static LIST_HEAD(pending_raid_disks); | |
1971 | ||
ac5e7113 AN |
1972 | /* |
1973 | * Try to register data integrity profile for an mddev | |
1974 | * | |
1975 | * This is called when an array is started and after a disk has been kicked | |
1976 | * from the array. It only succeeds if all working and active component devices | |
1977 | * are integrity capable with matching profiles. | |
1978 | */ | |
fd01b88c | 1979 | int md_integrity_register(struct mddev *mddev) |
ac5e7113 | 1980 | { |
3cb03002 | 1981 | struct md_rdev *rdev, *reference = NULL; |
ac5e7113 AN |
1982 | |
1983 | if (list_empty(&mddev->disks)) | |
1984 | return 0; /* nothing to do */ | |
629acb6a JB |
1985 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
1986 | return 0; /* shouldn't register, or already is */ | |
dafb20fa | 1987 | rdev_for_each(rdev, mddev) { |
ac5e7113 AN |
1988 | /* skip spares and non-functional disks */ |
1989 | if (test_bit(Faulty, &rdev->flags)) | |
1990 | continue; | |
1991 | if (rdev->raid_disk < 0) | |
1992 | continue; | |
ac5e7113 AN |
1993 | if (!reference) { |
1994 | /* Use the first rdev as the reference */ | |
1995 | reference = rdev; | |
1996 | continue; | |
1997 | } | |
1998 | /* does this rdev's profile match the reference profile? */ | |
1999 | if (blk_integrity_compare(reference->bdev->bd_disk, | |
2000 | rdev->bdev->bd_disk) < 0) | |
2001 | return -EINVAL; | |
2002 | } | |
89078d57 MP |
2003 | if (!reference || !bdev_get_integrity(reference->bdev)) |
2004 | return 0; | |
ac5e7113 AN |
2005 | /* |
2006 | * All component devices are integrity capable and have matching | |
2007 | * profiles, register the common profile for the md device. | |
2008 | */ | |
25520d55 MP |
2009 | blk_integrity_register(mddev->gendisk, |
2010 | bdev_get_integrity(reference->bdev)); | |
2011 | ||
a91a2785 MP |
2012 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
2013 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { | |
2014 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", | |
2015 | mdname(mddev)); | |
2016 | return -EINVAL; | |
2017 | } | |
ac5e7113 AN |
2018 | return 0; |
2019 | } | |
2020 | EXPORT_SYMBOL(md_integrity_register); | |
2021 | ||
2022 | /* Disable data integrity if non-capable/non-matching disk is being added */ | |
fd01b88c | 2023 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
3f9d99c1 | 2024 | { |
2863b9eb JB |
2025 | struct blk_integrity *bi_rdev; |
2026 | struct blk_integrity *bi_mddev; | |
2027 | ||
2028 | if (!mddev->gendisk) | |
2029 | return; | |
2030 | ||
2031 | bi_rdev = bdev_get_integrity(rdev->bdev); | |
2032 | bi_mddev = blk_get_integrity(mddev->gendisk); | |
3f9d99c1 | 2033 | |
ac5e7113 | 2034 | if (!bi_mddev) /* nothing to do */ |
3f9d99c1 | 2035 | return; |
ac5e7113 | 2036 | if (rdev->raid_disk < 0) /* skip spares */ |
3f9d99c1 | 2037 | return; |
ac5e7113 AN |
2038 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
2039 | rdev->bdev->bd_disk) >= 0) | |
2040 | return; | |
c7bfced9 | 2041 | WARN_ON_ONCE(!mddev->suspended); |
ac5e7113 AN |
2042 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
2043 | blk_integrity_unregister(mddev->gendisk); | |
3f9d99c1 | 2044 | } |
ac5e7113 | 2045 | EXPORT_SYMBOL(md_integrity_add_rdev); |
3f9d99c1 | 2046 | |
f72ffdd6 | 2047 | static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev) |
1da177e4 | 2048 | { |
7dd5e7c3 | 2049 | char b[BDEVNAME_SIZE]; |
f637b9f9 | 2050 | struct kobject *ko; |
5e55e2f5 | 2051 | int err; |
1da177e4 | 2052 | |
11e2ede0 DW |
2053 | /* prevent duplicates */ |
2054 | if (find_rdev(mddev, rdev->bdev->bd_dev)) | |
2055 | return -EEXIST; | |
2056 | ||
dd8ac336 AN |
2057 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
2058 | if (rdev->sectors && (mddev->dev_sectors == 0 || | |
2059 | rdev->sectors < mddev->dev_sectors)) { | |
a778b73f N |
2060 | if (mddev->pers) { |
2061 | /* Cannot change size, so fail | |
2062 | * If mddev->level <= 0, then we don't care | |
2063 | * about aligning sizes (e.g. linear) | |
2064 | */ | |
2065 | if (mddev->level > 0) | |
2066 | return -ENOSPC; | |
2067 | } else | |
dd8ac336 | 2068 | mddev->dev_sectors = rdev->sectors; |
2bf071bf | 2069 | } |
1da177e4 LT |
2070 | |
2071 | /* Verify rdev->desc_nr is unique. | |
2072 | * If it is -1, assign a free number, else | |
2073 | * check number is not in use | |
2074 | */ | |
4878e9eb | 2075 | rcu_read_lock(); |
1da177e4 LT |
2076 | if (rdev->desc_nr < 0) { |
2077 | int choice = 0; | |
4878e9eb N |
2078 | if (mddev->pers) |
2079 | choice = mddev->raid_disks; | |
57d051dc | 2080 | while (md_find_rdev_nr_rcu(mddev, choice)) |
1da177e4 LT |
2081 | choice++; |
2082 | rdev->desc_nr = choice; | |
2083 | } else { | |
57d051dc | 2084 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
4878e9eb | 2085 | rcu_read_unlock(); |
1da177e4 | 2086 | return -EBUSY; |
4878e9eb | 2087 | } |
1da177e4 | 2088 | } |
4878e9eb | 2089 | rcu_read_unlock(); |
de01dfad N |
2090 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
2091 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", | |
2092 | mdname(mddev), mddev->max_disks); | |
2093 | return -EBUSY; | |
2094 | } | |
19133a42 | 2095 | bdevname(rdev->bdev,b); |
90a9befb | 2096 | strreplace(b, '/', '!'); |
649316b2 | 2097 | |
1da177e4 | 2098 | rdev->mddev = mddev; |
19133a42 | 2099 | printk(KERN_INFO "md: bind<%s>\n", b); |
86e6ffdd | 2100 | |
b2d6db58 | 2101 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
5e55e2f5 | 2102 | goto fail; |
86e6ffdd | 2103 | |
0762b8bd | 2104 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
00bcb4ac N |
2105 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
2106 | /* failure here is OK */; | |
2107 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); | |
3c0ee63a | 2108 | |
4b80991c | 2109 | list_add_rcu(&rdev->same_set, &mddev->disks); |
e09b457b | 2110 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
4044ba58 N |
2111 | |
2112 | /* May as well allow recovery to be retried once */ | |
5389042f | 2113 | mddev->recovery_disabled++; |
3f9d99c1 | 2114 | |
1da177e4 | 2115 | return 0; |
5e55e2f5 N |
2116 | |
2117 | fail: | |
2118 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", | |
2119 | b, mdname(mddev)); | |
2120 | return err; | |
1da177e4 LT |
2121 | } |
2122 | ||
177a99b2 | 2123 | static void md_delayed_delete(struct work_struct *ws) |
5792a285 | 2124 | { |
3cb03002 | 2125 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
5792a285 | 2126 | kobject_del(&rdev->kobj); |
177a99b2 | 2127 | kobject_put(&rdev->kobj); |
5792a285 N |
2128 | } |
2129 | ||
f72ffdd6 | 2130 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
1da177e4 LT |
2131 | { |
2132 | char b[BDEVNAME_SIZE]; | |
403df478 | 2133 | |
49731baa | 2134 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
4b80991c | 2135 | list_del_rcu(&rdev->same_set); |
1da177e4 LT |
2136 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
2137 | rdev->mddev = NULL; | |
86e6ffdd | 2138 | sysfs_remove_link(&rdev->kobj, "block"); |
3c0ee63a N |
2139 | sysfs_put(rdev->sysfs_state); |
2140 | rdev->sysfs_state = NULL; | |
2230dfe4 | 2141 | rdev->badblocks.count = 0; |
5792a285 | 2142 | /* We need to delay this, otherwise we can deadlock when |
4b80991c N |
2143 | * writing to 'remove' to "dev/state". We also need |
2144 | * to delay it due to rcu usage. | |
5792a285 | 2145 | */ |
4b80991c | 2146 | synchronize_rcu(); |
177a99b2 N |
2147 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
2148 | kobject_get(&rdev->kobj); | |
e804ac78 | 2149 | queue_work(md_misc_wq, &rdev->del_work); |
1da177e4 LT |
2150 | } |
2151 | ||
2152 | /* | |
2153 | * prevent the device from being mounted, repartitioned or | |
2154 | * otherwise reused by a RAID array (or any other kernel | |
2155 | * subsystem), by bd_claiming the device. | |
2156 | */ | |
3cb03002 | 2157 | static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared) |
1da177e4 LT |
2158 | { |
2159 | int err = 0; | |
2160 | struct block_device *bdev; | |
2161 | char b[BDEVNAME_SIZE]; | |
2162 | ||
d4d77629 | 2163 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
3cb03002 | 2164 | shared ? (struct md_rdev *)lock_rdev : rdev); |
1da177e4 LT |
2165 | if (IS_ERR(bdev)) { |
2166 | printk(KERN_ERR "md: could not open %s.\n", | |
2167 | __bdevname(dev, b)); | |
2168 | return PTR_ERR(bdev); | |
2169 | } | |
1da177e4 LT |
2170 | rdev->bdev = bdev; |
2171 | return err; | |
2172 | } | |
2173 | ||
3cb03002 | 2174 | static void unlock_rdev(struct md_rdev *rdev) |
1da177e4 LT |
2175 | { |
2176 | struct block_device *bdev = rdev->bdev; | |
2177 | rdev->bdev = NULL; | |
e525fd89 | 2178 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
1da177e4 LT |
2179 | } |
2180 | ||
2181 | void md_autodetect_dev(dev_t dev); | |
2182 | ||
f72ffdd6 | 2183 | static void export_rdev(struct md_rdev *rdev) |
1da177e4 LT |
2184 | { |
2185 | char b[BDEVNAME_SIZE]; | |
403df478 | 2186 | |
1da177e4 LT |
2187 | printk(KERN_INFO "md: export_rdev(%s)\n", |
2188 | bdevname(rdev->bdev,b)); | |
545c8795 | 2189 | md_rdev_clear(rdev); |
1da177e4 | 2190 | #ifndef MODULE |
d0fae18f N |
2191 | if (test_bit(AutoDetected, &rdev->flags)) |
2192 | md_autodetect_dev(rdev->bdev->bd_dev); | |
1da177e4 LT |
2193 | #endif |
2194 | unlock_rdev(rdev); | |
86e6ffdd | 2195 | kobject_put(&rdev->kobj); |
1da177e4 LT |
2196 | } |
2197 | ||
fb56dfef | 2198 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
1da177e4 LT |
2199 | { |
2200 | unbind_rdev_from_array(rdev); | |
2201 | export_rdev(rdev); | |
2202 | } | |
fb56dfef | 2203 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
1da177e4 | 2204 | |
fd01b88c | 2205 | static void export_array(struct mddev *mddev) |
1da177e4 | 2206 | { |
0638bb0e | 2207 | struct md_rdev *rdev; |
1da177e4 | 2208 | |
0638bb0e N |
2209 | while (!list_empty(&mddev->disks)) { |
2210 | rdev = list_first_entry(&mddev->disks, struct md_rdev, | |
2211 | same_set); | |
fb56dfef | 2212 | md_kick_rdev_from_array(rdev); |
1da177e4 | 2213 | } |
1da177e4 LT |
2214 | mddev->raid_disks = 0; |
2215 | mddev->major_version = 0; | |
2216 | } | |
2217 | ||
f72ffdd6 | 2218 | static void sync_sbs(struct mddev *mddev, int nospares) |
1da177e4 | 2219 | { |
42543769 N |
2220 | /* Update each superblock (in-memory image), but |
2221 | * if we are allowed to, skip spares which already | |
2222 | * have the right event counter, or have one earlier | |
2223 | * (which would mean they aren't being marked as dirty | |
2224 | * with the rest of the array) | |
2225 | */ | |
3cb03002 | 2226 | struct md_rdev *rdev; |
dafb20fa | 2227 | rdev_for_each(rdev, mddev) { |
42543769 N |
2228 | if (rdev->sb_events == mddev->events || |
2229 | (nospares && | |
2230 | rdev->raid_disk < 0 && | |
42543769 N |
2231 | rdev->sb_events+1 == mddev->events)) { |
2232 | /* Don't update this superblock */ | |
2233 | rdev->sb_loaded = 2; | |
2234 | } else { | |
076f968b | 2235 | sync_super(mddev, rdev); |
42543769 N |
2236 | rdev->sb_loaded = 1; |
2237 | } | |
1da177e4 LT |
2238 | } |
2239 | } | |
2240 | ||
2aa82191 GR |
2241 | static bool does_sb_need_changing(struct mddev *mddev) |
2242 | { | |
2243 | struct md_rdev *rdev; | |
2244 | struct mdp_superblock_1 *sb; | |
2245 | int role; | |
2246 | ||
2247 | /* Find a good rdev */ | |
2248 | rdev_for_each(rdev, mddev) | |
2249 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) | |
2250 | break; | |
2251 | ||
2252 | /* No good device found. */ | |
2253 | if (!rdev) | |
2254 | return false; | |
2255 | ||
2256 | sb = page_address(rdev->sb_page); | |
2257 | /* Check if a device has become faulty or a spare become active */ | |
2258 | rdev_for_each(rdev, mddev) { | |
2259 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); | |
2260 | /* Device activated? */ | |
2261 | if (role == 0xffff && rdev->raid_disk >=0 && | |
2262 | !test_bit(Faulty, &rdev->flags)) | |
2263 | return true; | |
2264 | /* Device turned faulty? */ | |
2265 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) | |
2266 | return true; | |
2267 | } | |
2268 | ||
2269 | /* Check if any mddev parameters have changed */ | |
2270 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || | |
2271 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || | |
2aa82191 GR |
2272 | (mddev->layout != le64_to_cpu(sb->layout)) || |
2273 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || | |
2274 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) | |
2275 | return true; | |
2276 | ||
2277 | return false; | |
2278 | } | |
2279 | ||
1aee41f6 | 2280 | void md_update_sb(struct mddev *mddev, int force_change) |
1da177e4 | 2281 | { |
3cb03002 | 2282 | struct md_rdev *rdev; |
06d91a5f | 2283 | int sync_req; |
42543769 | 2284 | int nospares = 0; |
2699b672 | 2285 | int any_badblocks_changed = 0; |
23b63f9f | 2286 | int ret = -1; |
1da177e4 | 2287 | |
d87f064f N |
2288 | if (mddev->ro) { |
2289 | if (force_change) | |
2290 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
2291 | return; | |
2292 | } | |
2aa82191 GR |
2293 | |
2294 | if (mddev_is_clustered(mddev)) { | |
2295 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) | |
2296 | force_change = 1; | |
23b63f9f | 2297 | ret = md_cluster_ops->metadata_update_start(mddev); |
2aa82191 GR |
2298 | /* Has someone else has updated the sb */ |
2299 | if (!does_sb_need_changing(mddev)) { | |
23b63f9f GJ |
2300 | if (ret == 0) |
2301 | md_cluster_ops->metadata_update_cancel(mddev); | |
2aa82191 GR |
2302 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
2303 | return; | |
2304 | } | |
2305 | } | |
1da177e4 | 2306 | repeat: |
3a3a5ddb | 2307 | /* First make sure individual recovery_offsets are correct */ |
dafb20fa | 2308 | rdev_for_each(rdev, mddev) { |
3a3a5ddb N |
2309 | if (rdev->raid_disk >= 0 && |
2310 | mddev->delta_disks >= 0 && | |
f2076e7d | 2311 | !test_bit(Journal, &rdev->flags) && |
3a3a5ddb N |
2312 | !test_bit(In_sync, &rdev->flags) && |
2313 | mddev->curr_resync_completed > rdev->recovery_offset) | |
2314 | rdev->recovery_offset = mddev->curr_resync_completed; | |
2315 | ||
f72ffdd6 | 2316 | } |
bd52b746 | 2317 | if (!mddev->persistent) { |
070dc6dd | 2318 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
3a3a5ddb | 2319 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
de393cde | 2320 | if (!mddev->external) { |
d97a41dc | 2321 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
dafb20fa | 2322 | rdev_for_each(rdev, mddev) { |
de393cde | 2323 | if (rdev->badblocks.changed) { |
d0962936 | 2324 | rdev->badblocks.changed = 0; |
de393cde N |
2325 | md_ack_all_badblocks(&rdev->badblocks); |
2326 | md_error(mddev, rdev); | |
2327 | } | |
2328 | clear_bit(Blocked, &rdev->flags); | |
2329 | clear_bit(BlockedBadBlocks, &rdev->flags); | |
2330 | wake_up(&rdev->blocked_wait); | |
2331 | } | |
2332 | } | |
3a3a5ddb N |
2333 | wake_up(&mddev->sb_wait); |
2334 | return; | |
2335 | } | |
2336 | ||
85572d7c | 2337 | spin_lock(&mddev->lock); |
84692195 | 2338 | |
9ebc6ef1 | 2339 | mddev->utime = ktime_get_real_seconds(); |
3a3a5ddb | 2340 | |
850b2b42 N |
2341 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
2342 | force_change = 1; | |
2343 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) | |
2344 | /* just a clean<-> dirty transition, possibly leave spares alone, | |
2345 | * though if events isn't the right even/odd, we will have to do | |
2346 | * spares after all | |
2347 | */ | |
2348 | nospares = 1; | |
2349 | if (force_change) | |
2350 | nospares = 0; | |
2351 | if (mddev->degraded) | |
84692195 N |
2352 | /* If the array is degraded, then skipping spares is both |
2353 | * dangerous and fairly pointless. | |
2354 | * Dangerous because a device that was removed from the array | |
2355 | * might have a event_count that still looks up-to-date, | |
2356 | * so it can be re-added without a resync. | |
2357 | * Pointless because if there are any spares to skip, | |
2358 | * then a recovery will happen and soon that array won't | |
2359 | * be degraded any more and the spare can go back to sleep then. | |
2360 | */ | |
850b2b42 | 2361 | nospares = 0; |
84692195 | 2362 | |
06d91a5f | 2363 | sync_req = mddev->in_sync; |
42543769 N |
2364 | |
2365 | /* If this is just a dirty<->clean transition, and the array is clean | |
2366 | * and 'events' is odd, we can roll back to the previous clean state */ | |
850b2b42 | 2367 | if (nospares |
42543769 | 2368 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
a8707c08 N |
2369 | && mddev->can_decrease_events |
2370 | && mddev->events != 1) { | |
42543769 | 2371 | mddev->events--; |
a8707c08 N |
2372 | mddev->can_decrease_events = 0; |
2373 | } else { | |
42543769 N |
2374 | /* otherwise we have to go forward and ... */ |
2375 | mddev->events ++; | |
a8707c08 | 2376 | mddev->can_decrease_events = nospares; |
42543769 | 2377 | } |
1da177e4 | 2378 | |
403df478 N |
2379 | /* |
2380 | * This 64-bit counter should never wrap. | |
2381 | * Either we are in around ~1 trillion A.C., assuming | |
2382 | * 1 reboot per second, or we have a bug... | |
2383 | */ | |
2384 | WARN_ON(mddev->events == 0); | |
2699b672 | 2385 | |
dafb20fa | 2386 | rdev_for_each(rdev, mddev) { |
2699b672 N |
2387 | if (rdev->badblocks.changed) |
2388 | any_badblocks_changed++; | |
de393cde N |
2389 | if (test_bit(Faulty, &rdev->flags)) |
2390 | set_bit(FaultRecorded, &rdev->flags); | |
2391 | } | |
2699b672 | 2392 | |
e691063a | 2393 | sync_sbs(mddev, nospares); |
85572d7c | 2394 | spin_unlock(&mddev->lock); |
1da177e4 | 2395 | |
36a4e1fe N |
2396 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
2397 | mdname(mddev), mddev->in_sync); | |
1da177e4 | 2398 | |
4ad13663 | 2399 | bitmap_update_sb(mddev->bitmap); |
dafb20fa | 2400 | rdev_for_each(rdev, mddev) { |
1da177e4 | 2401 | char b[BDEVNAME_SIZE]; |
36a4e1fe | 2402 | |
42543769 N |
2403 | if (rdev->sb_loaded != 1) |
2404 | continue; /* no noise on spare devices */ | |
1da177e4 | 2405 | |
f466722c | 2406 | if (!test_bit(Faulty, &rdev->flags)) { |
7bfa19f2 | 2407 | md_super_write(mddev,rdev, |
0f420358 | 2408 | rdev->sb_start, rdev->sb_size, |
7bfa19f2 | 2409 | rdev->sb_page); |
36a4e1fe N |
2410 | pr_debug("md: (write) %s's sb offset: %llu\n", |
2411 | bdevname(rdev->bdev, b), | |
2412 | (unsigned long long)rdev->sb_start); | |
42543769 | 2413 | rdev->sb_events = mddev->events; |
2699b672 N |
2414 | if (rdev->badblocks.size) { |
2415 | md_super_write(mddev, rdev, | |
2416 | rdev->badblocks.sector, | |
2417 | rdev->badblocks.size << 9, | |
2418 | rdev->bb_page); | |
2419 | rdev->badblocks.size = 0; | |
2420 | } | |
7bfa19f2 | 2421 | |
f466722c | 2422 | } else |
36a4e1fe N |
2423 | pr_debug("md: %s (skipping faulty)\n", |
2424 | bdevname(rdev->bdev, b)); | |
d70ed2e4 | 2425 | |
7bfa19f2 | 2426 | if (mddev->level == LEVEL_MULTIPATH) |
1da177e4 LT |
2427 | /* only need to write one superblock... */ |
2428 | break; | |
2429 | } | |
a9701a30 | 2430 | md_super_wait(mddev); |
850b2b42 | 2431 | /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */ |
7bfa19f2 | 2432 | |
85572d7c | 2433 | spin_lock(&mddev->lock); |
850b2b42 N |
2434 | if (mddev->in_sync != sync_req || |
2435 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { | |
06d91a5f | 2436 | /* have to write it out again */ |
85572d7c | 2437 | spin_unlock(&mddev->lock); |
06d91a5f N |
2438 | goto repeat; |
2439 | } | |
850b2b42 | 2440 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
85572d7c | 2441 | spin_unlock(&mddev->lock); |
3d310eb7 | 2442 | wake_up(&mddev->sb_wait); |
acb180b0 N |
2443 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
2444 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); | |
06d91a5f | 2445 | |
dafb20fa | 2446 | rdev_for_each(rdev, mddev) { |
de393cde N |
2447 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
2448 | clear_bit(Blocked, &rdev->flags); | |
2449 | ||
2450 | if (any_badblocks_changed) | |
2699b672 | 2451 | md_ack_all_badblocks(&rdev->badblocks); |
de393cde N |
2452 | clear_bit(BlockedBadBlocks, &rdev->flags); |
2453 | wake_up(&rdev->blocked_wait); | |
2454 | } | |
2aa82191 | 2455 | |
23b63f9f | 2456 | if (mddev_is_clustered(mddev) && ret == 0) |
2aa82191 | 2457 | md_cluster_ops->metadata_update_finish(mddev); |
1da177e4 | 2458 | } |
1aee41f6 | 2459 | EXPORT_SYMBOL(md_update_sb); |
1da177e4 | 2460 | |
a6da4ef8 GR |
2461 | static int add_bound_rdev(struct md_rdev *rdev) |
2462 | { | |
2463 | struct mddev *mddev = rdev->mddev; | |
2464 | int err = 0; | |
2465 | ||
2466 | if (!mddev->pers->hot_remove_disk) { | |
2467 | /* If there is hot_add_disk but no hot_remove_disk | |
2468 | * then added disks for geometry changes, | |
2469 | * and should be added immediately. | |
2470 | */ | |
2471 | super_types[mddev->major_version]. | |
2472 | validate_super(mddev, rdev); | |
2473 | err = mddev->pers->hot_add_disk(mddev, rdev); | |
2474 | if (err) { | |
2475 | unbind_rdev_from_array(rdev); | |
2476 | export_rdev(rdev); | |
2477 | return err; | |
2478 | } | |
2479 | } | |
2480 | sysfs_notify_dirent_safe(rdev->sysfs_state); | |
2481 | ||
2482 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
2483 | if (mddev->degraded) | |
2484 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
2485 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
2486 | md_new_event(mddev); | |
2487 | md_wakeup_thread(mddev->thread); | |
2488 | return 0; | |
2489 | } | |
1da177e4 | 2490 | |
7f6ce769 | 2491 | /* words written to sysfs files may, or may not, be \n terminated. |
bce74dac N |
2492 | * We want to accept with case. For this we use cmd_match. |
2493 | */ | |
2494 | static int cmd_match(const char *cmd, const char *str) | |
2495 | { | |
2496 | /* See if cmd, written into a sysfs file, matches | |
2497 | * str. They must either be the same, or cmd can | |
2498 | * have a trailing newline | |
2499 | */ | |
2500 | while (*cmd && *str && *cmd == *str) { | |
2501 | cmd++; | |
2502 | str++; | |
2503 | } | |
2504 | if (*cmd == '\n') | |
2505 | cmd++; | |
2506 | if (*str || *cmd) | |
2507 | return 0; | |
2508 | return 1; | |
2509 | } | |
2510 | ||
86e6ffdd N |
2511 | struct rdev_sysfs_entry { |
2512 | struct attribute attr; | |
3cb03002 N |
2513 | ssize_t (*show)(struct md_rdev *, char *); |
2514 | ssize_t (*store)(struct md_rdev *, const char *, size_t); | |
86e6ffdd N |
2515 | }; |
2516 | ||
2517 | static ssize_t | |
3cb03002 | 2518 | state_show(struct md_rdev *rdev, char *page) |
86e6ffdd N |
2519 | { |
2520 | char *sep = ""; | |
20a49ff6 | 2521 | size_t len = 0; |
758bfc8a | 2522 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
86e6ffdd | 2523 | |
758bfc8a | 2524 | if (test_bit(Faulty, &flags) || |
de393cde | 2525 | rdev->badblocks.unacked_exist) { |
86e6ffdd N |
2526 | len+= sprintf(page+len, "%sfaulty",sep); |
2527 | sep = ","; | |
2528 | } | |
758bfc8a | 2529 | if (test_bit(In_sync, &flags)) { |
86e6ffdd N |
2530 | len += sprintf(page+len, "%sin_sync",sep); |
2531 | sep = ","; | |
2532 | } | |
ac6096e9 SL |
2533 | if (test_bit(Journal, &flags)) { |
2534 | len += sprintf(page+len, "%sjournal",sep); | |
2535 | sep = ","; | |
2536 | } | |
758bfc8a | 2537 | if (test_bit(WriteMostly, &flags)) { |
f655675b N |
2538 | len += sprintf(page+len, "%swrite_mostly",sep); |
2539 | sep = ","; | |
2540 | } | |
758bfc8a | 2541 | if (test_bit(Blocked, &flags) || |
52c64152 | 2542 | (rdev->badblocks.unacked_exist |
758bfc8a | 2543 | && !test_bit(Faulty, &flags))) { |
6bfe0b49 DW |
2544 | len += sprintf(page+len, "%sblocked", sep); |
2545 | sep = ","; | |
2546 | } | |
758bfc8a | 2547 | if (!test_bit(Faulty, &flags) && |
f2076e7d | 2548 | !test_bit(Journal, &flags) && |
758bfc8a | 2549 | !test_bit(In_sync, &flags)) { |
86e6ffdd N |
2550 | len += sprintf(page+len, "%sspare", sep); |
2551 | sep = ","; | |
2552 | } | |
758bfc8a | 2553 | if (test_bit(WriteErrorSeen, &flags)) { |
d7a9d443 N |
2554 | len += sprintf(page+len, "%swrite_error", sep); |
2555 | sep = ","; | |
2556 | } | |
758bfc8a | 2557 | if (test_bit(WantReplacement, &flags)) { |
2d78f8c4 N |
2558 | len += sprintf(page+len, "%swant_replacement", sep); |
2559 | sep = ","; | |
2560 | } | |
758bfc8a | 2561 | if (test_bit(Replacement, &flags)) { |
2d78f8c4 N |
2562 | len += sprintf(page+len, "%sreplacement", sep); |
2563 | sep = ","; | |
2564 | } | |
2565 | ||
86e6ffdd N |
2566 | return len+sprintf(page+len, "\n"); |
2567 | } | |
2568 | ||
45dc2de1 | 2569 | static ssize_t |
3cb03002 | 2570 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
45dc2de1 N |
2571 | { |
2572 | /* can write | |
de393cde | 2573 | * faulty - simulates an error |
45dc2de1 | 2574 | * remove - disconnects the device |
f655675b N |
2575 | * writemostly - sets write_mostly |
2576 | * -writemostly - clears write_mostly | |
de393cde N |
2577 | * blocked - sets the Blocked flags |
2578 | * -blocked - clears the Blocked and possibly simulates an error | |
6d56e278 | 2579 | * insync - sets Insync providing device isn't active |
f466722c N |
2580 | * -insync - clear Insync for a device with a slot assigned, |
2581 | * so that it gets rebuilt based on bitmap | |
d7a9d443 N |
2582 | * write_error - sets WriteErrorSeen |
2583 | * -write_error - clears WriteErrorSeen | |
45dc2de1 N |
2584 | */ |
2585 | int err = -EINVAL; | |
2586 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { | |
2587 | md_error(rdev->mddev, rdev); | |
5ef56c8f N |
2588 | if (test_bit(Faulty, &rdev->flags)) |
2589 | err = 0; | |
2590 | else | |
2591 | err = -EBUSY; | |
45dc2de1 N |
2592 | } else if (cmd_match(buf, "remove")) { |
2593 | if (rdev->raid_disk >= 0) | |
2594 | err = -EBUSY; | |
2595 | else { | |
fd01b88c | 2596 | struct mddev *mddev = rdev->mddev; |
45dc2de1 | 2597 | err = 0; |
a9720903 GJ |
2598 | if (mddev_is_clustered(mddev)) |
2599 | err = md_cluster_ops->remove_disk(mddev, rdev); | |
2600 | ||
2601 | if (err == 0) { | |
2602 | md_kick_rdev_from_array(rdev); | |
2603 | if (mddev->pers) | |
2604 | md_update_sb(mddev, 1); | |
2605 | md_new_event(mddev); | |
2606 | } | |
45dc2de1 | 2607 | } |
f655675b N |
2608 | } else if (cmd_match(buf, "writemostly")) { |
2609 | set_bit(WriteMostly, &rdev->flags); | |
2610 | err = 0; | |
2611 | } else if (cmd_match(buf, "-writemostly")) { | |
2612 | clear_bit(WriteMostly, &rdev->flags); | |
6bfe0b49 DW |
2613 | err = 0; |
2614 | } else if (cmd_match(buf, "blocked")) { | |
2615 | set_bit(Blocked, &rdev->flags); | |
2616 | err = 0; | |
2617 | } else if (cmd_match(buf, "-blocked")) { | |
de393cde | 2618 | if (!test_bit(Faulty, &rdev->flags) && |
7da64a0a | 2619 | rdev->badblocks.unacked_exist) { |
de393cde N |
2620 | /* metadata handler doesn't understand badblocks, |
2621 | * so we need to fail the device | |
2622 | */ | |
2623 | md_error(rdev->mddev, rdev); | |
2624 | } | |
6bfe0b49 | 2625 | clear_bit(Blocked, &rdev->flags); |
de393cde | 2626 | clear_bit(BlockedBadBlocks, &rdev->flags); |
6bfe0b49 DW |
2627 | wake_up(&rdev->blocked_wait); |
2628 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); | |
2629 | md_wakeup_thread(rdev->mddev->thread); | |
2630 | ||
6d56e278 N |
2631 | err = 0; |
2632 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { | |
2633 | set_bit(In_sync, &rdev->flags); | |
f655675b | 2634 | err = 0; |
f2076e7d SL |
2635 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
2636 | !test_bit(Journal, &rdev->flags)) { | |
e1960f8c N |
2637 | if (rdev->mddev->pers == NULL) { |
2638 | clear_bit(In_sync, &rdev->flags); | |
2639 | rdev->saved_raid_disk = rdev->raid_disk; | |
2640 | rdev->raid_disk = -1; | |
2641 | err = 0; | |
2642 | } | |
d7a9d443 N |
2643 | } else if (cmd_match(buf, "write_error")) { |
2644 | set_bit(WriteErrorSeen, &rdev->flags); | |
2645 | err = 0; | |
2646 | } else if (cmd_match(buf, "-write_error")) { | |
2647 | clear_bit(WriteErrorSeen, &rdev->flags); | |
2648 | err = 0; | |
2d78f8c4 N |
2649 | } else if (cmd_match(buf, "want_replacement")) { |
2650 | /* Any non-spare device that is not a replacement can | |
2651 | * become want_replacement at any time, but we then need to | |
2652 | * check if recovery is needed. | |
2653 | */ | |
2654 | if (rdev->raid_disk >= 0 && | |
f2076e7d | 2655 | !test_bit(Journal, &rdev->flags) && |
2d78f8c4 N |
2656 | !test_bit(Replacement, &rdev->flags)) |
2657 | set_bit(WantReplacement, &rdev->flags); | |
2658 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); | |
2659 | md_wakeup_thread(rdev->mddev->thread); | |
2660 | err = 0; | |
2661 | } else if (cmd_match(buf, "-want_replacement")) { | |
2662 | /* Clearing 'want_replacement' is always allowed. | |
2663 | * Once replacements starts it is too late though. | |
2664 | */ | |
2665 | err = 0; | |
2666 | clear_bit(WantReplacement, &rdev->flags); | |
2667 | } else if (cmd_match(buf, "replacement")) { | |
2668 | /* Can only set a device as a replacement when array has not | |
2669 | * yet been started. Once running, replacement is automatic | |
2670 | * from spares, or by assigning 'slot'. | |
2671 | */ | |
2672 | if (rdev->mddev->pers) | |
2673 | err = -EBUSY; | |
2674 | else { | |
2675 | set_bit(Replacement, &rdev->flags); | |
2676 | err = 0; | |
2677 | } | |
2678 | } else if (cmd_match(buf, "-replacement")) { | |
2679 | /* Similarly, can only clear Replacement before start */ | |
2680 | if (rdev->mddev->pers) | |
2681 | err = -EBUSY; | |
2682 | else { | |
2683 | clear_bit(Replacement, &rdev->flags); | |
2684 | err = 0; | |
2685 | } | |
a6da4ef8 GR |
2686 | } else if (cmd_match(buf, "re-add")) { |
2687 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { | |
97f6cd39 GR |
2688 | /* clear_bit is performed _after_ all the devices |
2689 | * have their local Faulty bit cleared. If any writes | |
2690 | * happen in the meantime in the local node, they | |
2691 | * will land in the local bitmap, which will be synced | |
2692 | * by this node eventually | |
2693 | */ | |
2694 | if (!mddev_is_clustered(rdev->mddev) || | |
2695 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { | |
2696 | clear_bit(Faulty, &rdev->flags); | |
2697 | err = add_bound_rdev(rdev); | |
2698 | } | |
a6da4ef8 GR |
2699 | } else |
2700 | err = -EBUSY; | |
45dc2de1 | 2701 | } |
00bcb4ac N |
2702 | if (!err) |
2703 | sysfs_notify_dirent_safe(rdev->sysfs_state); | |
45dc2de1 N |
2704 | return err ? err : len; |
2705 | } | |
80ca3a44 | 2706 | static struct rdev_sysfs_entry rdev_state = |
750f199e | 2707 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
86e6ffdd | 2708 | |
4dbcdc75 | 2709 | static ssize_t |
3cb03002 | 2710 | errors_show(struct md_rdev *rdev, char *page) |
4dbcdc75 N |
2711 | { |
2712 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); | |
2713 | } | |
2714 | ||
2715 | static ssize_t | |
3cb03002 | 2716 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
4dbcdc75 | 2717 | { |
4c9309c0 AD |
2718 | unsigned int n; |
2719 | int rv; | |
2720 | ||
2721 | rv = kstrtouint(buf, 10, &n); | |
2722 | if (rv < 0) | |
2723 | return rv; | |
2724 | atomic_set(&rdev->corrected_errors, n); | |
2725 | return len; | |
4dbcdc75 N |
2726 | } |
2727 | static struct rdev_sysfs_entry rdev_errors = | |
80ca3a44 | 2728 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
4dbcdc75 | 2729 | |
014236d2 | 2730 | static ssize_t |
3cb03002 | 2731 | slot_show(struct md_rdev *rdev, char *page) |
014236d2 | 2732 | { |
f2076e7d SL |
2733 | if (test_bit(Journal, &rdev->flags)) |
2734 | return sprintf(page, "journal\n"); | |
2735 | else if (rdev->raid_disk < 0) | |
014236d2 N |
2736 | return sprintf(page, "none\n"); |
2737 | else | |
2738 | return sprintf(page, "%d\n", rdev->raid_disk); | |
2739 | } | |
2740 | ||
2741 | static ssize_t | |
3cb03002 | 2742 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
014236d2 | 2743 | { |
4c9309c0 | 2744 | int slot; |
c303da6d | 2745 | int err; |
4c9309c0 | 2746 | |
f2076e7d SL |
2747 | if (test_bit(Journal, &rdev->flags)) |
2748 | return -EBUSY; | |
014236d2 N |
2749 | if (strncmp(buf, "none", 4)==0) |
2750 | slot = -1; | |
4c9309c0 AD |
2751 | else { |
2752 | err = kstrtouint(buf, 10, (unsigned int *)&slot); | |
2753 | if (err < 0) | |
2754 | return err; | |
2755 | } | |
6c2fce2e | 2756 | if (rdev->mddev->pers && slot == -1) { |
c303da6d N |
2757 | /* Setting 'slot' on an active array requires also |
2758 | * updating the 'rd%d' link, and communicating | |
2759 | * with the personality with ->hot_*_disk. | |
2760 | * For now we only support removing | |
2761 | * failed/spare devices. This normally happens automatically, | |
2762 | * but not when the metadata is externally managed. | |
2763 | */ | |
c303da6d N |
2764 | if (rdev->raid_disk == -1) |
2765 | return -EEXIST; | |
2766 | /* personality does all needed checks */ | |
01393f3d | 2767 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
c303da6d | 2768 | return -EINVAL; |
746d3207 N |
2769 | clear_bit(Blocked, &rdev->flags); |
2770 | remove_and_add_spares(rdev->mddev, rdev); | |
2771 | if (rdev->raid_disk >= 0) | |
2772 | return -EBUSY; | |
c303da6d N |
2773 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
2774 | md_wakeup_thread(rdev->mddev->thread); | |
6c2fce2e | 2775 | } else if (rdev->mddev->pers) { |
6c2fce2e | 2776 | /* Activating a spare .. or possibly reactivating |
6d56e278 | 2777 | * if we ever get bitmaps working here. |
6c2fce2e | 2778 | */ |
cb01c549 | 2779 | int err; |
6c2fce2e NB |
2780 | |
2781 | if (rdev->raid_disk != -1) | |
2782 | return -EBUSY; | |
2783 | ||
c6751b2b N |
2784 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
2785 | return -EBUSY; | |
2786 | ||
6c2fce2e NB |
2787 | if (rdev->mddev->pers->hot_add_disk == NULL) |
2788 | return -EINVAL; | |
2789 | ||
ba1b41b6 N |
2790 | if (slot >= rdev->mddev->raid_disks && |
2791 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) | |
2792 | return -ENOSPC; | |
2793 | ||
6c2fce2e NB |
2794 | rdev->raid_disk = slot; |
2795 | if (test_bit(In_sync, &rdev->flags)) | |
2796 | rdev->saved_raid_disk = slot; | |
2797 | else | |
2798 | rdev->saved_raid_disk = -1; | |
d30519fc | 2799 | clear_bit(In_sync, &rdev->flags); |
8313b8e5 | 2800 | clear_bit(Bitmap_sync, &rdev->flags); |
cb01c549 GR |
2801 | err = rdev->mddev->pers-> |
2802 | hot_add_disk(rdev->mddev, rdev); | |
2803 | if (err) { | |
2804 | rdev->raid_disk = -1; | |
2805 | return err; | |
2806 | } else | |
2807 | sysfs_notify_dirent_safe(rdev->sysfs_state); | |
2808 | if (sysfs_link_rdev(rdev->mddev, rdev)) | |
2809 | /* failure here is OK */; | |
6c2fce2e | 2810 | /* don't wakeup anyone, leave that to userspace. */ |
c303da6d | 2811 | } else { |
ba1b41b6 N |
2812 | if (slot >= rdev->mddev->raid_disks && |
2813 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) | |
c303da6d N |
2814 | return -ENOSPC; |
2815 | rdev->raid_disk = slot; | |
2816 | /* assume it is working */ | |
c5d79adb N |
2817 | clear_bit(Faulty, &rdev->flags); |
2818 | clear_bit(WriteMostly, &rdev->flags); | |
c303da6d | 2819 | set_bit(In_sync, &rdev->flags); |
00bcb4ac | 2820 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
c303da6d | 2821 | } |
014236d2 N |
2822 | return len; |
2823 | } | |
2824 | ||
014236d2 | 2825 | static struct rdev_sysfs_entry rdev_slot = |
80ca3a44 | 2826 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
014236d2 | 2827 | |
93c8cad0 | 2828 | static ssize_t |
3cb03002 | 2829 | offset_show(struct md_rdev *rdev, char *page) |
93c8cad0 | 2830 | { |
6961ece4 | 2831 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
93c8cad0 N |
2832 | } |
2833 | ||
2834 | static ssize_t | |
3cb03002 | 2835 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
93c8cad0 | 2836 | { |
c6563a8c | 2837 | unsigned long long offset; |
b29bebd6 | 2838 | if (kstrtoull(buf, 10, &offset) < 0) |
93c8cad0 | 2839 | return -EINVAL; |
8ed0a521 | 2840 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
93c8cad0 | 2841 | return -EBUSY; |
dd8ac336 | 2842 | if (rdev->sectors && rdev->mddev->external) |
c5d79adb N |
2843 | /* Must set offset before size, so overlap checks |
2844 | * can be sane */ | |
2845 | return -EBUSY; | |
93c8cad0 | 2846 | rdev->data_offset = offset; |
25f7fd47 | 2847 | rdev->new_data_offset = offset; |
93c8cad0 N |
2848 | return len; |
2849 | } | |
2850 | ||
2851 | static struct rdev_sysfs_entry rdev_offset = | |
80ca3a44 | 2852 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
93c8cad0 | 2853 | |
c6563a8c N |
2854 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
2855 | { | |
2856 | return sprintf(page, "%llu\n", | |
2857 | (unsigned long long)rdev->new_data_offset); | |
2858 | } | |
2859 | ||
2860 | static ssize_t new_offset_store(struct md_rdev *rdev, | |
2861 | const char *buf, size_t len) | |
2862 | { | |
2863 | unsigned long long new_offset; | |
2864 | struct mddev *mddev = rdev->mddev; | |
2865 | ||
b29bebd6 | 2866 | if (kstrtoull(buf, 10, &new_offset) < 0) |
c6563a8c N |
2867 | return -EINVAL; |
2868 | ||
f851b60d N |
2869 | if (mddev->sync_thread || |
2870 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) | |
c6563a8c N |
2871 | return -EBUSY; |
2872 | if (new_offset == rdev->data_offset) | |
2873 | /* reset is always permitted */ | |
2874 | ; | |
2875 | else if (new_offset > rdev->data_offset) { | |
2876 | /* must not push array size beyond rdev_sectors */ | |
2877 | if (new_offset - rdev->data_offset | |
2878 | + mddev->dev_sectors > rdev->sectors) | |
2879 | return -E2BIG; | |
2880 | } | |
2881 | /* Metadata worries about other space details. */ | |
2882 | ||
2883 | /* decreasing the offset is inconsistent with a backwards | |
2884 | * reshape. | |
2885 | */ | |
2886 | if (new_offset < rdev->data_offset && | |
2887 | mddev->reshape_backwards) | |
2888 | return -EINVAL; | |
2889 | /* Increasing offset is inconsistent with forwards | |
2890 | * reshape. reshape_direction should be set to | |
2891 | * 'backwards' first. | |
2892 | */ | |
2893 | if (new_offset > rdev->data_offset && | |
2894 | !mddev->reshape_backwards) | |
2895 | return -EINVAL; | |
2896 | ||
2897 | if (mddev->pers && mddev->persistent && | |
2898 | !super_types[mddev->major_version] | |
2899 | .allow_new_offset(rdev, new_offset)) | |
2900 | return -E2BIG; | |
2901 | rdev->new_data_offset = new_offset; | |
2902 | if (new_offset > rdev->data_offset) | |
2903 | mddev->reshape_backwards = 1; | |
2904 | else if (new_offset < rdev->data_offset) | |
2905 | mddev->reshape_backwards = 0; | |
2906 | ||
2907 | return len; | |
2908 | } | |
2909 | static struct rdev_sysfs_entry rdev_new_offset = | |
2910 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); | |
2911 | ||
83303b61 | 2912 | static ssize_t |
3cb03002 | 2913 | rdev_size_show(struct md_rdev *rdev, char *page) |
83303b61 | 2914 | { |
dd8ac336 | 2915 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
83303b61 N |
2916 | } |
2917 | ||
c5d79adb N |
2918 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
2919 | { | |
2920 | /* check if two start/length pairs overlap */ | |
2921 | if (s1+l1 <= s2) | |
2922 | return 0; | |
2923 | if (s2+l2 <= s1) | |
2924 | return 0; | |
2925 | return 1; | |
2926 | } | |
2927 | ||
b522adcd DW |
2928 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
2929 | { | |
2930 | unsigned long long blocks; | |
2931 | sector_t new; | |
2932 | ||
b29bebd6 | 2933 | if (kstrtoull(buf, 10, &blocks) < 0) |
b522adcd DW |
2934 | return -EINVAL; |
2935 | ||
2936 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) | |
2937 | return -EINVAL; /* sector conversion overflow */ | |
2938 | ||
2939 | new = blocks * 2; | |
2940 | if (new != blocks * 2) | |
2941 | return -EINVAL; /* unsigned long long to sector_t overflow */ | |
2942 | ||
2943 | *sectors = new; | |
2944 | return 0; | |
2945 | } | |
2946 | ||
83303b61 | 2947 | static ssize_t |
3cb03002 | 2948 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
83303b61 | 2949 | { |
fd01b88c | 2950 | struct mddev *my_mddev = rdev->mddev; |
dd8ac336 | 2951 | sector_t oldsectors = rdev->sectors; |
b522adcd | 2952 | sector_t sectors; |
27c529bb | 2953 | |
f2076e7d SL |
2954 | if (test_bit(Journal, &rdev->flags)) |
2955 | return -EBUSY; | |
b522adcd | 2956 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
d7027458 | 2957 | return -EINVAL; |
c6563a8c N |
2958 | if (rdev->data_offset != rdev->new_data_offset) |
2959 | return -EINVAL; /* too confusing */ | |
0cd17fec | 2960 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
d7027458 | 2961 | if (my_mddev->persistent) { |
dd8ac336 AN |
2962 | sectors = super_types[my_mddev->major_version]. |
2963 | rdev_size_change(rdev, sectors); | |
2964 | if (!sectors) | |
0cd17fec | 2965 | return -EBUSY; |
dd8ac336 | 2966 | } else if (!sectors) |
77304d2a | 2967 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
dd8ac336 | 2968 | rdev->data_offset; |
a6468539 N |
2969 | if (!my_mddev->pers->resize) |
2970 | /* Cannot change size for RAID0 or Linear etc */ | |
2971 | return -EINVAL; | |
0cd17fec | 2972 | } |
dd8ac336 | 2973 | if (sectors < my_mddev->dev_sectors) |
7d3c6f87 | 2974 | return -EINVAL; /* component must fit device */ |
0cd17fec | 2975 | |
dd8ac336 AN |
2976 | rdev->sectors = sectors; |
2977 | if (sectors > oldsectors && my_mddev->external) { | |
8b1afc3d N |
2978 | /* Need to check that all other rdevs with the same |
2979 | * ->bdev do not overlap. 'rcu' is sufficient to walk | |
2980 | * the rdev lists safely. | |
2981 | * This check does not provide a hard guarantee, it | |
2982 | * just helps avoid dangerous mistakes. | |
c5d79adb | 2983 | */ |
fd01b88c | 2984 | struct mddev *mddev; |
c5d79adb | 2985 | int overlap = 0; |
159ec1fc | 2986 | struct list_head *tmp; |
c5d79adb | 2987 | |
8b1afc3d | 2988 | rcu_read_lock(); |
29ac4aa3 | 2989 | for_each_mddev(mddev, tmp) { |
3cb03002 | 2990 | struct md_rdev *rdev2; |
c5d79adb | 2991 | |
dafb20fa | 2992 | rdev_for_each(rdev2, mddev) |
f21e9ff7 N |
2993 | if (rdev->bdev == rdev2->bdev && |
2994 | rdev != rdev2 && | |
2995 | overlaps(rdev->data_offset, rdev->sectors, | |
2996 | rdev2->data_offset, | |
2997 | rdev2->sectors)) { | |
c5d79adb N |
2998 | overlap = 1; |
2999 | break; | |
3000 | } | |
c5d79adb N |
3001 | if (overlap) { |
3002 | mddev_put(mddev); | |
3003 | break; | |
3004 | } | |
3005 | } | |
8b1afc3d | 3006 | rcu_read_unlock(); |
c5d79adb N |
3007 | if (overlap) { |
3008 | /* Someone else could have slipped in a size | |
3009 | * change here, but doing so is just silly. | |
dd8ac336 | 3010 | * We put oldsectors back because we *know* it is |
c5d79adb N |
3011 | * safe, and trust userspace not to race with |
3012 | * itself | |
3013 | */ | |
dd8ac336 | 3014 | rdev->sectors = oldsectors; |
c5d79adb N |
3015 | return -EBUSY; |
3016 | } | |
3017 | } | |
83303b61 N |
3018 | return len; |
3019 | } | |
3020 | ||
3021 | static struct rdev_sysfs_entry rdev_size = | |
80ca3a44 | 3022 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
83303b61 | 3023 | |
3cb03002 | 3024 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
06e3c817 DW |
3025 | { |
3026 | unsigned long long recovery_start = rdev->recovery_offset; | |
3027 | ||
3028 | if (test_bit(In_sync, &rdev->flags) || | |
3029 | recovery_start == MaxSector) | |
3030 | return sprintf(page, "none\n"); | |
3031 | ||
3032 | return sprintf(page, "%llu\n", recovery_start); | |
3033 | } | |
3034 | ||
3cb03002 | 3035 | static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len) |
06e3c817 DW |
3036 | { |
3037 | unsigned long long recovery_start; | |
3038 | ||
3039 | if (cmd_match(buf, "none")) | |
3040 | recovery_start = MaxSector; | |
b29bebd6 | 3041 | else if (kstrtoull(buf, 10, &recovery_start)) |
06e3c817 DW |
3042 | return -EINVAL; |
3043 | ||
3044 | if (rdev->mddev->pers && | |
3045 | rdev->raid_disk >= 0) | |
3046 | return -EBUSY; | |
3047 | ||
3048 | rdev->recovery_offset = recovery_start; | |
3049 | if (recovery_start == MaxSector) | |
3050 | set_bit(In_sync, &rdev->flags); | |
3051 | else | |
3052 | clear_bit(In_sync, &rdev->flags); | |
3053 | return len; | |
3054 | } | |
3055 | ||
3056 | static struct rdev_sysfs_entry rdev_recovery_start = | |
3057 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); | |
3058 | ||
16c791a5 N |
3059 | static ssize_t |
3060 | badblocks_show(struct badblocks *bb, char *page, int unack); | |
3061 | static ssize_t | |
3062 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); | |
3063 | ||
3cb03002 | 3064 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
16c791a5 N |
3065 | { |
3066 | return badblocks_show(&rdev->badblocks, page, 0); | |
3067 | } | |
3cb03002 | 3068 | static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len) |
16c791a5 | 3069 | { |
de393cde N |
3070 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
3071 | /* Maybe that ack was all we needed */ | |
3072 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) | |
3073 | wake_up(&rdev->blocked_wait); | |
3074 | return rv; | |
16c791a5 N |
3075 | } |
3076 | static struct rdev_sysfs_entry rdev_bad_blocks = | |
3077 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); | |
3078 | ||
3cb03002 | 3079 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
16c791a5 N |
3080 | { |
3081 | return badblocks_show(&rdev->badblocks, page, 1); | |
3082 | } | |
3cb03002 | 3083 | static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len) |
16c791a5 N |
3084 | { |
3085 | return badblocks_store(&rdev->badblocks, page, len, 1); | |
3086 | } | |
3087 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = | |
3088 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); | |
3089 | ||
86e6ffdd N |
3090 | static struct attribute *rdev_default_attrs[] = { |
3091 | &rdev_state.attr, | |
4dbcdc75 | 3092 | &rdev_errors.attr, |
014236d2 | 3093 | &rdev_slot.attr, |
93c8cad0 | 3094 | &rdev_offset.attr, |
c6563a8c | 3095 | &rdev_new_offset.attr, |
83303b61 | 3096 | &rdev_size.attr, |
06e3c817 | 3097 | &rdev_recovery_start.attr, |
16c791a5 N |
3098 | &rdev_bad_blocks.attr, |
3099 | &rdev_unack_bad_blocks.attr, | |
86e6ffdd N |
3100 | NULL, |
3101 | }; | |
3102 | static ssize_t | |
3103 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
3104 | { | |
3105 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); | |
3cb03002 | 3106 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
86e6ffdd N |
3107 | |
3108 | if (!entry->show) | |
3109 | return -EIO; | |
758bfc8a N |
3110 | if (!rdev->mddev) |
3111 | return -EBUSY; | |
3112 | return entry->show(rdev, page); | |
86e6ffdd N |
3113 | } |
3114 | ||
3115 | static ssize_t | |
3116 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, | |
3117 | const char *page, size_t length) | |
3118 | { | |
3119 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); | |
3cb03002 | 3120 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
27c529bb | 3121 | ssize_t rv; |
fd01b88c | 3122 | struct mddev *mddev = rdev->mddev; |
86e6ffdd N |
3123 | |
3124 | if (!entry->store) | |
3125 | return -EIO; | |
67463acb N |
3126 | if (!capable(CAP_SYS_ADMIN)) |
3127 | return -EACCES; | |
27c529bb | 3128 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
ca388059 | 3129 | if (!rv) { |
27c529bb N |
3130 | if (rdev->mddev == NULL) |
3131 | rv = -EBUSY; | |
3132 | else | |
3133 | rv = entry->store(rdev, page, length); | |
6a51830e | 3134 | mddev_unlock(mddev); |
ca388059 N |
3135 | } |
3136 | return rv; | |
86e6ffdd N |
3137 | } |
3138 | ||
3139 | static void rdev_free(struct kobject *ko) | |
3140 | { | |
3cb03002 | 3141 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
86e6ffdd N |
3142 | kfree(rdev); |
3143 | } | |
52cf25d0 | 3144 | static const struct sysfs_ops rdev_sysfs_ops = { |
86e6ffdd N |
3145 | .show = rdev_attr_show, |
3146 | .store = rdev_attr_store, | |
3147 | }; | |
3148 | static struct kobj_type rdev_ktype = { | |
3149 | .release = rdev_free, | |
3150 | .sysfs_ops = &rdev_sysfs_ops, | |
3151 | .default_attrs = rdev_default_attrs, | |
3152 | }; | |
3153 | ||
3cb03002 | 3154 | int md_rdev_init(struct md_rdev *rdev) |
e8bb9a83 N |
3155 | { |
3156 | rdev->desc_nr = -1; | |
3157 | rdev->saved_raid_disk = -1; | |
3158 | rdev->raid_disk = -1; | |
3159 | rdev->flags = 0; | |
3160 | rdev->data_offset = 0; | |
c6563a8c | 3161 | rdev->new_data_offset = 0; |
e8bb9a83 N |
3162 | rdev->sb_events = 0; |
3163 | rdev->last_read_error.tv_sec = 0; | |
3164 | rdev->last_read_error.tv_nsec = 0; | |
2699b672 N |
3165 | rdev->sb_loaded = 0; |
3166 | rdev->bb_page = NULL; | |
e8bb9a83 N |
3167 | atomic_set(&rdev->nr_pending, 0); |
3168 | atomic_set(&rdev->read_errors, 0); | |
3169 | atomic_set(&rdev->corrected_errors, 0); | |
3170 | ||
3171 | INIT_LIST_HEAD(&rdev->same_set); | |
3172 | init_waitqueue_head(&rdev->blocked_wait); | |
2230dfe4 N |
3173 | |
3174 | /* Add space to store bad block list. | |
3175 | * This reserves the space even on arrays where it cannot | |
3176 | * be used - I wonder if that matters | |
3177 | */ | |
3178 | rdev->badblocks.count = 0; | |
486adf72 | 3179 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
2230dfe4 N |
3180 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
3181 | seqlock_init(&rdev->badblocks.lock); | |
3182 | if (rdev->badblocks.page == NULL) | |
3183 | return -ENOMEM; | |
3184 | ||
3185 | return 0; | |
e8bb9a83 N |
3186 | } |
3187 | EXPORT_SYMBOL_GPL(md_rdev_init); | |
1da177e4 LT |
3188 | /* |
3189 | * Import a device. If 'super_format' >= 0, then sanity check the superblock | |
3190 | * | |
3191 | * mark the device faulty if: | |
3192 | * | |
3193 | * - the device is nonexistent (zero size) | |
3194 | * - the device has no valid superblock | |
3195 | * | |
3196 | * a faulty rdev _never_ has rdev->sb set. | |
3197 | */ | |
3cb03002 | 3198 | static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor) |
1da177e4 LT |
3199 | { |
3200 | char b[BDEVNAME_SIZE]; | |
3201 | int err; | |
3cb03002 | 3202 | struct md_rdev *rdev; |
1da177e4 LT |
3203 | sector_t size; |
3204 | ||
9ffae0cf | 3205 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
1da177e4 LT |
3206 | if (!rdev) { |
3207 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); | |
3208 | return ERR_PTR(-ENOMEM); | |
3209 | } | |
1da177e4 | 3210 | |
2230dfe4 N |
3211 | err = md_rdev_init(rdev); |
3212 | if (err) | |
3213 | goto abort_free; | |
3214 | err = alloc_disk_sb(rdev); | |
3215 | if (err) | |
1da177e4 LT |
3216 | goto abort_free; |
3217 | ||
c5d79adb | 3218 | err = lock_rdev(rdev, newdev, super_format == -2); |
1da177e4 LT |
3219 | if (err) |
3220 | goto abort_free; | |
3221 | ||
f9cb074b | 3222 | kobject_init(&rdev->kobj, &rdev_ktype); |
86e6ffdd | 3223 | |
77304d2a | 3224 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
1da177e4 | 3225 | if (!size) { |
f72ffdd6 | 3226 | printk(KERN_WARNING |
1da177e4 LT |
3227 | "md: %s has zero or unknown size, marking faulty!\n", |
3228 | bdevname(rdev->bdev,b)); | |
3229 | err = -EINVAL; | |
3230 | goto abort_free; | |
3231 | } | |
3232 | ||
3233 | if (super_format >= 0) { | |
3234 | err = super_types[super_format]. | |
3235 | load_super(rdev, NULL, super_minor); | |
3236 | if (err == -EINVAL) { | |
df968c4e N |
3237 | printk(KERN_WARNING |
3238 | "md: %s does not have a valid v%d.%d " | |
3239 | "superblock, not importing!\n", | |
3240 | bdevname(rdev->bdev,b), | |
3241 | super_format, super_minor); | |
1da177e4 LT |
3242 | goto abort_free; |
3243 | } | |
3244 | if (err < 0) { | |
f72ffdd6 | 3245 | printk(KERN_WARNING |
1da177e4 LT |
3246 | "md: could not read %s's sb, not importing!\n", |
3247 | bdevname(rdev->bdev,b)); | |
3248 | goto abort_free; | |
3249 | } | |
3250 | } | |
6bfe0b49 | 3251 | |
1da177e4 LT |
3252 | return rdev; |
3253 | ||
3254 | abort_free: | |
2699b672 N |
3255 | if (rdev->bdev) |
3256 | unlock_rdev(rdev); | |
545c8795 | 3257 | md_rdev_clear(rdev); |
1da177e4 LT |
3258 | kfree(rdev); |
3259 | return ERR_PTR(err); | |
3260 | } | |
3261 | ||
3262 | /* | |
3263 | * Check a full RAID array for plausibility | |
3264 | */ | |
3265 | ||
f72ffdd6 | 3266 | static void analyze_sbs(struct mddev *mddev) |
1da177e4 LT |
3267 | { |
3268 | int i; | |
3cb03002 | 3269 | struct md_rdev *rdev, *freshest, *tmp; |
1da177e4 LT |
3270 | char b[BDEVNAME_SIZE]; |
3271 | ||
3272 | freshest = NULL; | |
dafb20fa | 3273 | rdev_for_each_safe(rdev, tmp, mddev) |
1da177e4 LT |
3274 | switch (super_types[mddev->major_version]. |
3275 | load_super(rdev, freshest, mddev->minor_version)) { | |
3276 | case 1: | |
3277 | freshest = rdev; | |
3278 | break; | |
3279 | case 0: | |
3280 | break; | |
3281 | default: | |
3282 | printk( KERN_ERR \ | |
3283 | "md: fatal superblock inconsistency in %s" | |
f72ffdd6 | 3284 | " -- removing from array\n", |
1da177e4 | 3285 | bdevname(rdev->bdev,b)); |
fb56dfef | 3286 | md_kick_rdev_from_array(rdev); |
1da177e4 LT |
3287 | } |
3288 | ||
1da177e4 LT |
3289 | super_types[mddev->major_version]. |
3290 | validate_super(mddev, freshest); | |
3291 | ||
3292 | i = 0; | |
dafb20fa | 3293 | rdev_for_each_safe(rdev, tmp, mddev) { |
233fca36 N |
3294 | if (mddev->max_disks && |
3295 | (rdev->desc_nr >= mddev->max_disks || | |
3296 | i > mddev->max_disks)) { | |
de01dfad N |
3297 | printk(KERN_WARNING |
3298 | "md: %s: %s: only %d devices permitted\n", | |
3299 | mdname(mddev), bdevname(rdev->bdev, b), | |
3300 | mddev->max_disks); | |
fb56dfef | 3301 | md_kick_rdev_from_array(rdev); |
de01dfad N |
3302 | continue; |
3303 | } | |
1aee41f6 | 3304 | if (rdev != freshest) { |
1da177e4 LT |
3305 | if (super_types[mddev->major_version]. |
3306 | validate_super(mddev, rdev)) { | |
3307 | printk(KERN_WARNING "md: kicking non-fresh %s" | |
3308 | " from array!\n", | |
3309 | bdevname(rdev->bdev,b)); | |
fb56dfef | 3310 | md_kick_rdev_from_array(rdev); |
1da177e4 LT |
3311 | continue; |
3312 | } | |
1aee41f6 | 3313 | } |
1da177e4 LT |
3314 | if (mddev->level == LEVEL_MULTIPATH) { |
3315 | rdev->desc_nr = i++; | |
3316 | rdev->raid_disk = rdev->desc_nr; | |
b2d444d7 | 3317 | set_bit(In_sync, &rdev->flags); |
f2076e7d SL |
3318 | } else if (rdev->raid_disk >= |
3319 | (mddev->raid_disks - min(0, mddev->delta_disks)) && | |
3320 | !test_bit(Journal, &rdev->flags)) { | |
a778b73f N |
3321 | rdev->raid_disk = -1; |
3322 | clear_bit(In_sync, &rdev->flags); | |
1da177e4 LT |
3323 | } |
3324 | } | |
1da177e4 LT |
3325 | } |
3326 | ||
72e02075 N |
3327 | /* Read a fixed-point number. |
3328 | * Numbers in sysfs attributes should be in "standard" units where | |
3329 | * possible, so time should be in seconds. | |
f72ffdd6 | 3330 | * However we internally use a a much smaller unit such as |
72e02075 N |
3331 | * milliseconds or jiffies. |
3332 | * This function takes a decimal number with a possible fractional | |
3333 | * component, and produces an integer which is the result of | |
3334 | * multiplying that number by 10^'scale'. | |
3335 | * all without any floating-point arithmetic. | |
3336 | */ | |
3337 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) | |
3338 | { | |
3339 | unsigned long result = 0; | |
3340 | long decimals = -1; | |
3341 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { | |
3342 | if (*cp == '.') | |
3343 | decimals = 0; | |
3344 | else if (decimals < scale) { | |
3345 | unsigned int value; | |
3346 | value = *cp - '0'; | |
3347 | result = result * 10 + value; | |
3348 | if (decimals >= 0) | |
3349 | decimals++; | |
3350 | } | |
3351 | cp++; | |
3352 | } | |
3353 | if (*cp == '\n') | |
3354 | cp++; | |
3355 | if (*cp) | |
3356 | return -EINVAL; | |
3357 | if (decimals < 0) | |
3358 | decimals = 0; | |
3359 | while (decimals < scale) { | |
3360 | result *= 10; | |
3361 | decimals ++; | |
3362 | } | |
3363 | *res = result; | |
3364 | return 0; | |
3365 | } | |
3366 | ||
16f17b39 | 3367 | static ssize_t |
fd01b88c | 3368 | safe_delay_show(struct mddev *mddev, char *page) |
16f17b39 N |
3369 | { |
3370 | int msec = (mddev->safemode_delay*1000)/HZ; | |
3371 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); | |
3372 | } | |
3373 | static ssize_t | |
fd01b88c | 3374 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
16f17b39 | 3375 | { |
16f17b39 | 3376 | unsigned long msec; |
97ce0a7f | 3377 | |
28c1b9fd GR |
3378 | if (mddev_is_clustered(mddev)) { |
3379 | pr_info("md: Safemode is disabled for clustered mode\n"); | |
3380 | return -EINVAL; | |
3381 | } | |
3382 | ||
72e02075 | 3383 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
16f17b39 | 3384 | return -EINVAL; |
16f17b39 N |
3385 | if (msec == 0) |
3386 | mddev->safemode_delay = 0; | |
3387 | else { | |
19052c0e | 3388 | unsigned long old_delay = mddev->safemode_delay; |
1b30e66f N |
3389 | unsigned long new_delay = (msec*HZ)/1000; |
3390 | ||
3391 | if (new_delay == 0) | |
3392 | new_delay = 1; | |
3393 | mddev->safemode_delay = new_delay; | |
3394 | if (new_delay < old_delay || old_delay == 0) | |
3395 | mod_timer(&mddev->safemode_timer, jiffies+1); | |
16f17b39 N |
3396 | } |
3397 | return len; | |
3398 | } | |
3399 | static struct md_sysfs_entry md_safe_delay = | |
80ca3a44 | 3400 | __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store); |
16f17b39 | 3401 | |
eae1701f | 3402 | static ssize_t |
fd01b88c | 3403 | level_show(struct mddev *mddev, char *page) |
eae1701f | 3404 | { |
36d091f4 N |
3405 | struct md_personality *p; |
3406 | int ret; | |
3407 | spin_lock(&mddev->lock); | |
3408 | p = mddev->pers; | |
d9d166c2 | 3409 | if (p) |
36d091f4 | 3410 | ret = sprintf(page, "%s\n", p->name); |
d9d166c2 | 3411 | else if (mddev->clevel[0]) |
36d091f4 | 3412 | ret = sprintf(page, "%s\n", mddev->clevel); |
d9d166c2 | 3413 | else if (mddev->level != LEVEL_NONE) |
36d091f4 | 3414 | ret = sprintf(page, "%d\n", mddev->level); |
d9d166c2 | 3415 | else |
36d091f4 N |
3416 | ret = 0; |
3417 | spin_unlock(&mddev->lock); | |
3418 | return ret; | |
eae1701f N |
3419 | } |
3420 | ||
d9d166c2 | 3421 | static ssize_t |
fd01b88c | 3422 | level_store(struct mddev *mddev, const char *buf, size_t len) |
d9d166c2 | 3423 | { |
f2859af6 | 3424 | char clevel[16]; |
6791875e N |
3425 | ssize_t rv; |
3426 | size_t slen = len; | |
db721d32 | 3427 | struct md_personality *pers, *oldpers; |
f2859af6 | 3428 | long level; |
db721d32 | 3429 | void *priv, *oldpriv; |
3cb03002 | 3430 | struct md_rdev *rdev; |
245f46c2 | 3431 | |
6791875e N |
3432 | if (slen == 0 || slen >= sizeof(clevel)) |
3433 | return -EINVAL; | |
3434 | ||
3435 | rv = mddev_lock(mddev); | |
3436 | if (rv) | |
3437 | return rv; | |
3438 | ||
245f46c2 | 3439 | if (mddev->pers == NULL) { |
6791875e N |
3440 | strncpy(mddev->clevel, buf, slen); |
3441 | if (mddev->clevel[slen-1] == '\n') | |
3442 | slen--; | |
3443 | mddev->clevel[slen] = 0; | |
245f46c2 | 3444 | mddev->level = LEVEL_NONE; |
6791875e N |
3445 | rv = len; |
3446 | goto out_unlock; | |
245f46c2 | 3447 | } |
6791875e | 3448 | rv = -EROFS; |
bd8839e0 | 3449 | if (mddev->ro) |
6791875e | 3450 | goto out_unlock; |
245f46c2 N |
3451 | |
3452 | /* request to change the personality. Need to ensure: | |
3453 | * - array is not engaged in resync/recovery/reshape | |
3454 | * - old personality can be suspended | |
3455 | * - new personality will access other array. | |
3456 | */ | |
3457 | ||
6791875e | 3458 | rv = -EBUSY; |
bb4f1e9d | 3459 | if (mddev->sync_thread || |
f851b60d | 3460 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
bb4f1e9d N |
3461 | mddev->reshape_position != MaxSector || |
3462 | mddev->sysfs_active) | |
6791875e | 3463 | goto out_unlock; |
245f46c2 | 3464 | |
6791875e | 3465 | rv = -EINVAL; |
245f46c2 N |
3466 | if (!mddev->pers->quiesce) { |
3467 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", | |
3468 | mdname(mddev), mddev->pers->name); | |
6791875e | 3469 | goto out_unlock; |
245f46c2 N |
3470 | } |
3471 | ||
3472 | /* Now find the new personality */ | |
6791875e N |
3473 | strncpy(clevel, buf, slen); |
3474 | if (clevel[slen-1] == '\n') | |
3475 | slen--; | |
3476 | clevel[slen] = 0; | |
b29bebd6 | 3477 | if (kstrtol(clevel, 10, &level)) |
f2859af6 | 3478 | level = LEVEL_NONE; |
245f46c2 | 3479 | |
f2859af6 DW |
3480 | if (request_module("md-%s", clevel) != 0) |
3481 | request_module("md-level-%s", clevel); | |
245f46c2 | 3482 | spin_lock(&pers_lock); |
f2859af6 | 3483 | pers = find_pers(level, clevel); |
245f46c2 N |
3484 | if (!pers || !try_module_get(pers->owner)) { |
3485 | spin_unlock(&pers_lock); | |
f2859af6 | 3486 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
6791875e N |
3487 | rv = -EINVAL; |
3488 | goto out_unlock; | |
245f46c2 N |
3489 | } |
3490 | spin_unlock(&pers_lock); | |
3491 | ||
3492 | if (pers == mddev->pers) { | |
3493 | /* Nothing to do! */ | |
3494 | module_put(pers->owner); | |
6791875e N |
3495 | rv = len; |
3496 | goto out_unlock; | |
245f46c2 N |
3497 | } |
3498 | if (!pers->takeover) { | |
3499 | module_put(pers->owner); | |
3500 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", | |
f2859af6 | 3501 | mdname(mddev), clevel); |
6791875e N |
3502 | rv = -EINVAL; |
3503 | goto out_unlock; | |
245f46c2 N |
3504 | } |
3505 | ||
dafb20fa | 3506 | rdev_for_each(rdev, mddev) |
e93f68a1 N |
3507 | rdev->new_raid_disk = rdev->raid_disk; |
3508 | ||
245f46c2 N |
3509 | /* ->takeover must set new_* and/or delta_disks |
3510 | * if it succeeds, and may set them when it fails. | |
3511 | */ | |
3512 | priv = pers->takeover(mddev); | |
3513 | if (IS_ERR(priv)) { | |
3514 | mddev->new_level = mddev->level; | |
3515 | mddev->new_layout = mddev->layout; | |
664e7c41 | 3516 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
245f46c2 N |
3517 | mddev->raid_disks -= mddev->delta_disks; |
3518 | mddev->delta_disks = 0; | |
2c810cdd | 3519 | mddev->reshape_backwards = 0; |
245f46c2 N |
3520 | module_put(pers->owner); |
3521 | printk(KERN_WARNING "md: %s: %s would not accept array\n", | |
f2859af6 | 3522 | mdname(mddev), clevel); |
6791875e N |
3523 | rv = PTR_ERR(priv); |
3524 | goto out_unlock; | |
245f46c2 N |
3525 | } |
3526 | ||
3527 | /* Looks like we have a winner */ | |
3528 | mddev_suspend(mddev); | |
5aa61f42 | 3529 | mddev_detach(mddev); |
36d091f4 N |
3530 | |
3531 | spin_lock(&mddev->lock); | |
db721d32 N |
3532 | oldpers = mddev->pers; |
3533 | oldpriv = mddev->private; | |
3534 | mddev->pers = pers; | |
3535 | mddev->private = priv; | |
3536 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); | |
3537 | mddev->level = mddev->new_level; | |
3538 | mddev->layout = mddev->new_layout; | |
3539 | mddev->chunk_sectors = mddev->new_chunk_sectors; | |
3540 | mddev->delta_disks = 0; | |
3541 | mddev->reshape_backwards = 0; | |
3542 | mddev->degraded = 0; | |
36d091f4 | 3543 | spin_unlock(&mddev->lock); |
db721d32 N |
3544 | |
3545 | if (oldpers->sync_request == NULL && | |
3546 | mddev->external) { | |
3547 | /* We are converting from a no-redundancy array | |
3548 | * to a redundancy array and metadata is managed | |
3549 | * externally so we need to be sure that writes | |
3550 | * won't block due to a need to transition | |
3551 | * clean->dirty | |
3552 | * until external management is started. | |
3553 | */ | |
3554 | mddev->in_sync = 0; | |
3555 | mddev->safemode_delay = 0; | |
3556 | mddev->safemode = 0; | |
3557 | } | |
f72ffdd6 | 3558 | |
db721d32 N |
3559 | oldpers->free(mddev, oldpriv); |
3560 | ||
3561 | if (oldpers->sync_request == NULL && | |
a64c876f N |
3562 | pers->sync_request != NULL) { |
3563 | /* need to add the md_redundancy_group */ | |
3564 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) | |
3565 | printk(KERN_WARNING | |
3566 | "md: cannot register extra attributes for %s\n", | |
3567 | mdname(mddev)); | |
388975cc | 3568 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
f72ffdd6 | 3569 | } |
db721d32 | 3570 | if (oldpers->sync_request != NULL && |
a64c876f N |
3571 | pers->sync_request == NULL) { |
3572 | /* need to remove the md_redundancy_group */ | |
3573 | if (mddev->to_remove == NULL) | |
3574 | mddev->to_remove = &md_redundancy_group; | |
3575 | } | |
3576 | ||
dafb20fa | 3577 | rdev_for_each(rdev, mddev) { |
e93f68a1 N |
3578 | if (rdev->raid_disk < 0) |
3579 | continue; | |
bf2cb0da | 3580 | if (rdev->new_raid_disk >= mddev->raid_disks) |
e93f68a1 N |
3581 | rdev->new_raid_disk = -1; |
3582 | if (rdev->new_raid_disk == rdev->raid_disk) | |
3583 | continue; | |
36fad858 | 3584 | sysfs_unlink_rdev(mddev, rdev); |
e93f68a1 | 3585 | } |
dafb20fa | 3586 | rdev_for_each(rdev, mddev) { |
e93f68a1 N |
3587 | if (rdev->raid_disk < 0) |
3588 | continue; | |
3589 | if (rdev->new_raid_disk == rdev->raid_disk) | |
3590 | continue; | |
3591 | rdev->raid_disk = rdev->new_raid_disk; | |
3592 | if (rdev->raid_disk < 0) | |
3a981b03 | 3593 | clear_bit(In_sync, &rdev->flags); |
e93f68a1 | 3594 | else { |
36fad858 NK |
3595 | if (sysfs_link_rdev(mddev, rdev)) |
3596 | printk(KERN_WARNING "md: cannot register rd%d" | |
3597 | " for %s after level change\n", | |
3598 | rdev->raid_disk, mdname(mddev)); | |
3a981b03 | 3599 | } |
e93f68a1 N |
3600 | } |
3601 | ||
db721d32 | 3602 | if (pers->sync_request == NULL) { |
9af204cf TM |
3603 | /* this is now an array without redundancy, so |
3604 | * it must always be in_sync | |
3605 | */ | |
3606 | mddev->in_sync = 1; | |
3607 | del_timer_sync(&mddev->safemode_timer); | |
3608 | } | |
02e5f5c0 | 3609 | blk_set_stacking_limits(&mddev->queue->limits); |
245f46c2 | 3610 | pers->run(mddev); |
245f46c2 | 3611 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
47525e59 | 3612 | mddev_resume(mddev); |
830778a1 N |
3613 | if (!mddev->thread) |
3614 | md_update_sb(mddev, 1); | |
5cac7861 | 3615 | sysfs_notify(&mddev->kobj, NULL, "level"); |
bb7f8d22 | 3616 | md_new_event(mddev); |
6791875e N |
3617 | rv = len; |
3618 | out_unlock: | |
3619 | mddev_unlock(mddev); | |
d9d166c2 N |
3620 | return rv; |
3621 | } | |
3622 | ||
3623 | static struct md_sysfs_entry md_level = | |
80ca3a44 | 3624 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
eae1701f | 3625 | |
d4dbd025 | 3626 | static ssize_t |
fd01b88c | 3627 | layout_show(struct mddev *mddev, char *page) |
d4dbd025 N |
3628 | { |
3629 | /* just a number, not meaningful for all levels */ | |
08a02ecd N |
3630 | if (mddev->reshape_position != MaxSector && |
3631 | mddev->layout != mddev->new_layout) | |
3632 | return sprintf(page, "%d (%d)\n", | |
3633 | mddev->new_layout, mddev->layout); | |
d4dbd025 N |
3634 | return sprintf(page, "%d\n", mddev->layout); |
3635 | } | |
3636 | ||
3637 | static ssize_t | |
fd01b88c | 3638 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
d4dbd025 | 3639 | { |
4c9309c0 | 3640 | unsigned int n; |
6791875e | 3641 | int err; |
d4dbd025 | 3642 | |
4c9309c0 AD |
3643 | err = kstrtouint(buf, 10, &n); |
3644 | if (err < 0) | |
3645 | return err; | |
6791875e N |
3646 | err = mddev_lock(mddev); |
3647 | if (err) | |
3648 | return err; | |
d4dbd025 | 3649 | |
b3546035 | 3650 | if (mddev->pers) { |
50ac168a | 3651 | if (mddev->pers->check_reshape == NULL) |
6791875e N |
3652 | err = -EBUSY; |
3653 | else if (mddev->ro) | |
3654 | err = -EROFS; | |
3655 | else { | |
3656 | mddev->new_layout = n; | |
3657 | err = mddev->pers->check_reshape(mddev); | |
3658 | if (err) | |
3659 | mddev->new_layout = mddev->layout; | |
597a711b | 3660 | } |
b3546035 | 3661 | } else { |
08a02ecd | 3662 | mddev->new_layout = n; |
b3546035 N |
3663 | if (mddev->reshape_position == MaxSector) |
3664 | mddev->layout = n; | |
3665 | } | |
6791875e N |
3666 | mddev_unlock(mddev); |
3667 | return err ?: len; | |
d4dbd025 N |
3668 | } |
3669 | static struct md_sysfs_entry md_layout = | |
80ca3a44 | 3670 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
d4dbd025 | 3671 | |
eae1701f | 3672 | static ssize_t |
fd01b88c | 3673 | raid_disks_show(struct mddev *mddev, char *page) |
eae1701f | 3674 | { |
bb636547 N |
3675 | if (mddev->raid_disks == 0) |
3676 | return 0; | |
08a02ecd N |
3677 | if (mddev->reshape_position != MaxSector && |
3678 | mddev->delta_disks != 0) | |
3679 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, | |
3680 | mddev->raid_disks - mddev->delta_disks); | |
eae1701f N |
3681 | return sprintf(page, "%d\n", mddev->raid_disks); |
3682 | } | |
3683 | ||
fd01b88c | 3684 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
da943b99 N |
3685 | |
3686 | static ssize_t | |
fd01b88c | 3687 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
da943b99 | 3688 | { |
4c9309c0 | 3689 | unsigned int n; |
6791875e | 3690 | int err; |
da943b99 | 3691 | |
4c9309c0 AD |
3692 | err = kstrtouint(buf, 10, &n); |
3693 | if (err < 0) | |
3694 | return err; | |
da943b99 | 3695 | |
6791875e N |
3696 | err = mddev_lock(mddev); |
3697 | if (err) | |
3698 | return err; | |
da943b99 | 3699 | if (mddev->pers) |
6791875e | 3700 | err = update_raid_disks(mddev, n); |
08a02ecd | 3701 | else if (mddev->reshape_position != MaxSector) { |
c6563a8c | 3702 | struct md_rdev *rdev; |
08a02ecd | 3703 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
c6563a8c | 3704 | |
6791875e | 3705 | err = -EINVAL; |
c6563a8c N |
3706 | rdev_for_each(rdev, mddev) { |
3707 | if (olddisks < n && | |
3708 | rdev->data_offset < rdev->new_data_offset) | |
6791875e | 3709 | goto out_unlock; |
c6563a8c N |
3710 | if (olddisks > n && |
3711 | rdev->data_offset > rdev->new_data_offset) | |
6791875e | 3712 | goto out_unlock; |
c6563a8c | 3713 | } |
6791875e | 3714 | err = 0; |
08a02ecd N |
3715 | mddev->delta_disks = n - olddisks; |
3716 | mddev->raid_disks = n; | |
2c810cdd | 3717 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
08a02ecd | 3718 | } else |
da943b99 | 3719 | mddev->raid_disks = n; |
6791875e N |
3720 | out_unlock: |
3721 | mddev_unlock(mddev); | |
3722 | return err ? err : len; | |
da943b99 N |
3723 | } |
3724 | static struct md_sysfs_entry md_raid_disks = | |
80ca3a44 | 3725 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
eae1701f | 3726 | |
3b34380a | 3727 | static ssize_t |
fd01b88c | 3728 | chunk_size_show(struct mddev *mddev, char *page) |
3b34380a | 3729 | { |
08a02ecd | 3730 | if (mddev->reshape_position != MaxSector && |
664e7c41 AN |
3731 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
3732 | return sprintf(page, "%d (%d)\n", | |
3733 | mddev->new_chunk_sectors << 9, | |
9d8f0363 AN |
3734 | mddev->chunk_sectors << 9); |
3735 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); | |
3b34380a N |
3736 | } |
3737 | ||
3738 | static ssize_t | |
fd01b88c | 3739 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
3b34380a | 3740 | { |
4c9309c0 | 3741 | unsigned long n; |
6791875e | 3742 | int err; |
3b34380a | 3743 | |
4c9309c0 AD |
3744 | err = kstrtoul(buf, 10, &n); |
3745 | if (err < 0) | |
3746 | return err; | |
3b34380a | 3747 | |
6791875e N |
3748 | err = mddev_lock(mddev); |
3749 | if (err) | |
3750 | return err; | |
b3546035 | 3751 | if (mddev->pers) { |
50ac168a | 3752 | if (mddev->pers->check_reshape == NULL) |
6791875e N |
3753 | err = -EBUSY; |
3754 | else if (mddev->ro) | |
3755 | err = -EROFS; | |
3756 | else { | |
3757 | mddev->new_chunk_sectors = n >> 9; | |
3758 | err = mddev->pers->check_reshape(mddev); | |
3759 | if (err) | |
3760 | mddev->new_chunk_sectors = mddev->chunk_sectors; | |
597a711b | 3761 | } |
b3546035 | 3762 | } else { |
664e7c41 | 3763 | mddev->new_chunk_sectors = n >> 9; |
b3546035 | 3764 | if (mddev->reshape_position == MaxSector) |
9d8f0363 | 3765 | mddev->chunk_sectors = n >> 9; |
b3546035 | 3766 | } |
6791875e N |
3767 | mddev_unlock(mddev); |
3768 | return err ?: len; | |
3b34380a N |
3769 | } |
3770 | static struct md_sysfs_entry md_chunk_size = | |
80ca3a44 | 3771 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
3b34380a | 3772 | |
a94213b1 | 3773 | static ssize_t |
fd01b88c | 3774 | resync_start_show(struct mddev *mddev, char *page) |
a94213b1 | 3775 | { |
d1a7c503 N |
3776 | if (mddev->recovery_cp == MaxSector) |
3777 | return sprintf(page, "none\n"); | |
a94213b1 N |
3778 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
3779 | } | |
3780 | ||
3781 | static ssize_t | |
fd01b88c | 3782 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
a94213b1 | 3783 | { |
4c9309c0 | 3784 | unsigned long long n; |
6791875e | 3785 | int err; |
4c9309c0 AD |
3786 | |
3787 | if (cmd_match(buf, "none")) | |
3788 | n = MaxSector; | |
3789 | else { | |
3790 | err = kstrtoull(buf, 10, &n); | |
3791 | if (err < 0) | |
3792 | return err; | |
3793 | if (n != (sector_t)n) | |
3794 | return -EINVAL; | |
3795 | } | |
a94213b1 | 3796 | |
6791875e N |
3797 | err = mddev_lock(mddev); |
3798 | if (err) | |
3799 | return err; | |
b098636c | 3800 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
6791875e | 3801 | err = -EBUSY; |
a94213b1 | 3802 | |
6791875e N |
3803 | if (!err) { |
3804 | mddev->recovery_cp = n; | |
3805 | if (mddev->pers) | |
3806 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); | |
3807 | } | |
3808 | mddev_unlock(mddev); | |
3809 | return err ?: len; | |
a94213b1 N |
3810 | } |
3811 | static struct md_sysfs_entry md_resync_start = | |
750f199e N |
3812 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
3813 | resync_start_show, resync_start_store); | |
a94213b1 | 3814 | |
9e653b63 N |
3815 | /* |
3816 | * The array state can be: | |
3817 | * | |
3818 | * clear | |
3819 | * No devices, no size, no level | |
3820 | * Equivalent to STOP_ARRAY ioctl | |
3821 | * inactive | |
3822 | * May have some settings, but array is not active | |
3823 | * all IO results in error | |
3824 | * When written, doesn't tear down array, but just stops it | |
3825 | * suspended (not supported yet) | |
3826 | * All IO requests will block. The array can be reconfigured. | |
910d8cb3 | 3827 | * Writing this, if accepted, will block until array is quiescent |
9e653b63 N |
3828 | * readonly |
3829 | * no resync can happen. no superblocks get written. | |
3830 | * write requests fail | |
3831 | * read-auto | |
3832 | * like readonly, but behaves like 'clean' on a write request. | |
3833 | * | |
3834 | * clean - no pending writes, but otherwise active. | |
3835 | * When written to inactive array, starts without resync | |
3836 | * If a write request arrives then | |
3837 | * if metadata is known, mark 'dirty' and switch to 'active'. | |
3838 | * if not known, block and switch to write-pending | |
3839 | * If written to an active array that has pending writes, then fails. | |
3840 | * active | |
3841 | * fully active: IO and resync can be happening. | |
3842 | * When written to inactive array, starts with resync | |
3843 | * | |
3844 | * write-pending | |
3845 | * clean, but writes are blocked waiting for 'active' to be written. | |
3846 | * | |
3847 | * active-idle | |
3848 | * like active, but no writes have been seen for a while (100msec). | |
3849 | * | |
3850 | */ | |
3851 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, | |
3852 | write_pending, active_idle, bad_word}; | |
05381954 | 3853 | static char *array_states[] = { |
9e653b63 N |
3854 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
3855 | "write-pending", "active-idle", NULL }; | |
3856 | ||
3857 | static int match_word(const char *word, char **list) | |
3858 | { | |
3859 | int n; | |
3860 | for (n=0; list[n]; n++) | |
3861 | if (cmd_match(word, list[n])) | |
3862 | break; | |
3863 | return n; | |
3864 | } | |
3865 | ||
3866 | static ssize_t | |
fd01b88c | 3867 | array_state_show(struct mddev *mddev, char *page) |
9e653b63 N |
3868 | { |
3869 | enum array_state st = inactive; | |
3870 | ||
3871 | if (mddev->pers) | |
3872 | switch(mddev->ro) { | |
3873 | case 1: | |
3874 | st = readonly; | |
3875 | break; | |
3876 | case 2: | |
3877 | st = read_auto; | |
3878 | break; | |
3879 | case 0: | |
3880 | if (mddev->in_sync) | |
3881 | st = clean; | |
070dc6dd | 3882 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
e691063a | 3883 | st = write_pending; |
9e653b63 N |
3884 | else if (mddev->safemode) |
3885 | st = active_idle; | |
3886 | else | |
3887 | st = active; | |
3888 | } | |
3889 | else { | |
3890 | if (list_empty(&mddev->disks) && | |
3891 | mddev->raid_disks == 0 && | |
58c0fed4 | 3892 | mddev->dev_sectors == 0) |
9e653b63 N |
3893 | st = clear; |
3894 | else | |
3895 | st = inactive; | |
3896 | } | |
3897 | return sprintf(page, "%s\n", array_states[st]); | |
3898 | } | |
3899 | ||
f72ffdd6 N |
3900 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
3901 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); | |
3902 | static int do_md_run(struct mddev *mddev); | |
fd01b88c | 3903 | static int restart_array(struct mddev *mddev); |
9e653b63 N |
3904 | |
3905 | static ssize_t | |
fd01b88c | 3906 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
9e653b63 | 3907 | { |
6791875e | 3908 | int err; |
9e653b63 | 3909 | enum array_state st = match_word(buf, array_states); |
6791875e N |
3910 | |
3911 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { | |
3912 | /* don't take reconfig_mutex when toggling between | |
3913 | * clean and active | |
3914 | */ | |
3915 | spin_lock(&mddev->lock); | |
3916 | if (st == active) { | |
3917 | restart_array(mddev); | |
3918 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); | |
3919 | wake_up(&mddev->sb_wait); | |
3920 | err = 0; | |
3921 | } else /* st == clean */ { | |
3922 | restart_array(mddev); | |
3923 | if (atomic_read(&mddev->writes_pending) == 0) { | |
3924 | if (mddev->in_sync == 0) { | |
3925 | mddev->in_sync = 1; | |
3926 | if (mddev->safemode == 1) | |
3927 | mddev->safemode = 0; | |
3928 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); | |
3929 | } | |
3930 | err = 0; | |
3931 | } else | |
3932 | err = -EBUSY; | |
3933 | } | |
3934 | spin_unlock(&mddev->lock); | |
c008f1d3 | 3935 | return err ?: len; |
6791875e N |
3936 | } |
3937 | err = mddev_lock(mddev); | |
3938 | if (err) | |
3939 | return err; | |
3940 | err = -EINVAL; | |
9e653b63 N |
3941 | switch(st) { |
3942 | case bad_word: | |
3943 | break; | |
3944 | case clear: | |
3945 | /* stopping an active array */ | |
a05b7ea0 | 3946 | err = do_md_stop(mddev, 0, NULL); |
9e653b63 N |
3947 | break; |
3948 | case inactive: | |
3949 | /* stopping an active array */ | |
90cf195d | 3950 | if (mddev->pers) |
a05b7ea0 | 3951 | err = do_md_stop(mddev, 2, NULL); |
90cf195d | 3952 | else |
e691063a | 3953 | err = 0; /* already inactive */ |
9e653b63 N |
3954 | break; |
3955 | case suspended: | |
3956 | break; /* not supported yet */ | |
3957 | case readonly: | |
3958 | if (mddev->pers) | |
a05b7ea0 | 3959 | err = md_set_readonly(mddev, NULL); |
9e653b63 N |
3960 | else { |
3961 | mddev->ro = 1; | |
648b629e | 3962 | set_disk_ro(mddev->gendisk, 1); |
9e653b63 N |
3963 | err = do_md_run(mddev); |
3964 | } | |
3965 | break; | |
3966 | case read_auto: | |
9e653b63 | 3967 | if (mddev->pers) { |
80268ee9 | 3968 | if (mddev->ro == 0) |
a05b7ea0 | 3969 | err = md_set_readonly(mddev, NULL); |
80268ee9 | 3970 | else if (mddev->ro == 1) |
648b629e N |
3971 | err = restart_array(mddev); |
3972 | if (err == 0) { | |
3973 | mddev->ro = 2; | |
3974 | set_disk_ro(mddev->gendisk, 0); | |
3975 | } | |
9e653b63 N |
3976 | } else { |
3977 | mddev->ro = 2; | |
3978 | err = do_md_run(mddev); | |
3979 | } | |
3980 | break; | |
3981 | case clean: | |
3982 | if (mddev->pers) { | |
339421de SL |
3983 | err = restart_array(mddev); |
3984 | if (err) | |
3985 | break; | |
85572d7c | 3986 | spin_lock(&mddev->lock); |
9e653b63 | 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; | |
85572d7c | 3997 | spin_unlock(&mddev->lock); |
5bf29597 N |
3998 | } else |
3999 | err = -EINVAL; | |
9e653b63 N |
4000 | break; |
4001 | case active: | |
4002 | if (mddev->pers) { | |
339421de SL |
4003 | err = restart_array(mddev); |
4004 | if (err) | |
4005 | break; | |
070dc6dd | 4006 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
9e653b63 N |
4007 | wake_up(&mddev->sb_wait); |
4008 | err = 0; | |
4009 | } else { | |
4010 | mddev->ro = 0; | |
648b629e | 4011 | set_disk_ro(mddev->gendisk, 0); |
9e653b63 N |
4012 | err = do_md_run(mddev); |
4013 | } | |
4014 | break; | |
4015 | case write_pending: | |
4016 | case active_idle: | |
4017 | /* these cannot be set */ | |
4018 | break; | |
4019 | } | |
6791875e N |
4020 | |
4021 | if (!err) { | |
1d23f178 N |
4022 | if (mddev->hold_active == UNTIL_IOCTL) |
4023 | mddev->hold_active = 0; | |
00bcb4ac | 4024 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0fd62b86 | 4025 | } |
6791875e N |
4026 | mddev_unlock(mddev); |
4027 | return err ?: len; | |
9e653b63 | 4028 | } |
80ca3a44 | 4029 | static struct md_sysfs_entry md_array_state = |
750f199e | 4030 | __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
9e653b63 | 4031 | |
1e50915f | 4032 | static ssize_t |
fd01b88c | 4033 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
1e50915f RB |
4034 | return sprintf(page, "%d\n", |
4035 | atomic_read(&mddev->max_corr_read_errors)); | |
4036 | } | |
4037 | ||
4038 | static ssize_t | |
fd01b88c | 4039 | max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len) |
1e50915f | 4040 | { |
4c9309c0 AD |
4041 | unsigned int n; |
4042 | int rv; | |
1e50915f | 4043 | |
4c9309c0 AD |
4044 | rv = kstrtouint(buf, 10, &n); |
4045 | if (rv < 0) | |
4046 | return rv; | |
4047 | atomic_set(&mddev->max_corr_read_errors, n); | |
4048 | return len; | |
1e50915f RB |
4049 | } |
4050 | ||
4051 | static struct md_sysfs_entry max_corr_read_errors = | |
4052 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, | |
4053 | max_corrected_read_errors_store); | |
4054 | ||
6d7ff738 | 4055 | static ssize_t |
fd01b88c | 4056 | null_show(struct mddev *mddev, char *page) |
6d7ff738 N |
4057 | { |
4058 | return -EINVAL; | |
4059 | } | |
4060 | ||
4061 | static ssize_t | |
fd01b88c | 4062 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
6d7ff738 N |
4063 | { |
4064 | /* buf must be %d:%d\n? giving major and minor numbers */ | |
4065 | /* The new device is added to the array. | |
4066 | * If the array has a persistent superblock, we read the | |
4067 | * superblock to initialise info and check validity. | |
4068 | * Otherwise, only checking done is that in bind_rdev_to_array, | |
4069 | * which mainly checks size. | |
4070 | */ | |
4071 | char *e; | |
4072 | int major = simple_strtoul(buf, &e, 10); | |
4073 | int minor; | |
4074 | dev_t dev; | |
3cb03002 | 4075 | struct md_rdev *rdev; |
6d7ff738 N |
4076 | int err; |
4077 | ||
4078 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') | |
4079 | return -EINVAL; | |
4080 | minor = simple_strtoul(e+1, &e, 10); | |
4081 | if (*e && *e != '\n') | |
4082 | return -EINVAL; | |
4083 | dev = MKDEV(major, minor); | |
4084 | if (major != MAJOR(dev) || | |
4085 | minor != MINOR(dev)) | |
4086 | return -EOVERFLOW; | |
4087 | ||
6791875e N |
4088 | flush_workqueue(md_misc_wq); |
4089 | ||
4090 | err = mddev_lock(mddev); | |
4091 | if (err) | |
4092 | return err; | |
6d7ff738 N |
4093 | if (mddev->persistent) { |
4094 | rdev = md_import_device(dev, mddev->major_version, | |
4095 | mddev->minor_version); | |
4096 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { | |
3cb03002 N |
4097 | struct md_rdev *rdev0 |
4098 | = list_entry(mddev->disks.next, | |
4099 | struct md_rdev, same_set); | |
6d7ff738 N |
4100 | err = super_types[mddev->major_version] |
4101 | .load_super(rdev, rdev0, mddev->minor_version); | |
4102 | if (err < 0) | |
4103 | goto out; | |
4104 | } | |
c5d79adb N |
4105 | } else if (mddev->external) |
4106 | rdev = md_import_device(dev, -2, -1); | |
4107 | else | |
6d7ff738 N |
4108 | rdev = md_import_device(dev, -1, -1); |
4109 | ||
9a8c0fa8 N |
4110 | if (IS_ERR(rdev)) { |
4111 | mddev_unlock(mddev); | |
6d7ff738 | 4112 | return PTR_ERR(rdev); |
9a8c0fa8 | 4113 | } |
6d7ff738 N |
4114 | err = bind_rdev_to_array(rdev, mddev); |
4115 | out: | |
4116 | if (err) | |
4117 | export_rdev(rdev); | |
6791875e | 4118 | mddev_unlock(mddev); |
6d7ff738 N |
4119 | return err ? err : len; |
4120 | } | |
4121 | ||
4122 | static struct md_sysfs_entry md_new_device = | |
80ca3a44 | 4123 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
3b34380a | 4124 | |
9b1d1dac | 4125 | static ssize_t |
fd01b88c | 4126 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
9b1d1dac PC |
4127 | { |
4128 | char *end; | |
4129 | unsigned long chunk, end_chunk; | |
6791875e | 4130 | int err; |
9b1d1dac | 4131 | |
6791875e N |
4132 | err = mddev_lock(mddev); |
4133 | if (err) | |
4134 | return err; | |
9b1d1dac PC |
4135 | if (!mddev->bitmap) |
4136 | goto out; | |
4137 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ | |
4138 | while (*buf) { | |
4139 | chunk = end_chunk = simple_strtoul(buf, &end, 0); | |
4140 | if (buf == end) break; | |
4141 | if (*end == '-') { /* range */ | |
4142 | buf = end + 1; | |
4143 | end_chunk = simple_strtoul(buf, &end, 0); | |
4144 | if (buf == end) break; | |
4145 | } | |
4146 | if (*end && !isspace(*end)) break; | |
4147 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); | |
e7d2860b | 4148 | buf = skip_spaces(end); |
9b1d1dac PC |
4149 | } |
4150 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ | |
4151 | out: | |
6791875e | 4152 | mddev_unlock(mddev); |
9b1d1dac PC |
4153 | return len; |
4154 | } | |
4155 | ||
4156 | static struct md_sysfs_entry md_bitmap = | |
4157 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); | |
4158 | ||
a35b0d69 | 4159 | static ssize_t |
fd01b88c | 4160 | size_show(struct mddev *mddev, char *page) |
a35b0d69 | 4161 | { |
58c0fed4 AN |
4162 | return sprintf(page, "%llu\n", |
4163 | (unsigned long long)mddev->dev_sectors / 2); | |
a35b0d69 N |
4164 | } |
4165 | ||
fd01b88c | 4166 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
a35b0d69 N |
4167 | |
4168 | static ssize_t | |
fd01b88c | 4169 | size_store(struct mddev *mddev, const char *buf, size_t len) |
a35b0d69 N |
4170 | { |
4171 | /* If array is inactive, we can reduce the component size, but | |
4172 | * not increase it (except from 0). | |
4173 | * If array is active, we can try an on-line resize | |
4174 | */ | |
b522adcd DW |
4175 | sector_t sectors; |
4176 | int err = strict_blocks_to_sectors(buf, §ors); | |
a35b0d69 | 4177 | |
58c0fed4 AN |
4178 | if (err < 0) |
4179 | return err; | |
6791875e N |
4180 | err = mddev_lock(mddev); |
4181 | if (err) | |
4182 | return err; | |
a35b0d69 | 4183 | if (mddev->pers) { |
58c0fed4 | 4184 | err = update_size(mddev, sectors); |
850b2b42 | 4185 | md_update_sb(mddev, 1); |
a35b0d69 | 4186 | } else { |
58c0fed4 AN |
4187 | if (mddev->dev_sectors == 0 || |
4188 | mddev->dev_sectors > sectors) | |
4189 | mddev->dev_sectors = sectors; | |
a35b0d69 N |
4190 | else |
4191 | err = -ENOSPC; | |
4192 | } | |
6791875e | 4193 | mddev_unlock(mddev); |
a35b0d69 N |
4194 | return err ? err : len; |
4195 | } | |
4196 | ||
4197 | static struct md_sysfs_entry md_size = | |
80ca3a44 | 4198 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
a35b0d69 | 4199 | |
83f0d77a | 4200 | /* Metadata version. |
e691063a N |
4201 | * This is one of |
4202 | * 'none' for arrays with no metadata (good luck...) | |
4203 | * 'external' for arrays with externally managed metadata, | |
8bb93aac N |
4204 | * or N.M for internally known formats |
4205 | */ | |
4206 | static ssize_t | |
fd01b88c | 4207 | metadata_show(struct mddev *mddev, char *page) |
8bb93aac N |
4208 | { |
4209 | if (mddev->persistent) | |
4210 | return sprintf(page, "%d.%d\n", | |
4211 | mddev->major_version, mddev->minor_version); | |
e691063a N |
4212 | else if (mddev->external) |
4213 | return sprintf(page, "external:%s\n", mddev->metadata_type); | |
8bb93aac N |
4214 | else |
4215 | return sprintf(page, "none\n"); | |
4216 | } | |
4217 | ||
4218 | static ssize_t | |
fd01b88c | 4219 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
8bb93aac N |
4220 | { |
4221 | int major, minor; | |
4222 | char *e; | |
6791875e | 4223 | int err; |
ea43ddd8 N |
4224 | /* Changing the details of 'external' metadata is |
4225 | * always permitted. Otherwise there must be | |
4226 | * no devices attached to the array. | |
4227 | */ | |
6791875e N |
4228 | |
4229 | err = mddev_lock(mddev); | |
4230 | if (err) | |
4231 | return err; | |
4232 | err = -EBUSY; | |
ea43ddd8 N |
4233 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
4234 | ; | |
4235 | else if (!list_empty(&mddev->disks)) | |
6791875e | 4236 | goto out_unlock; |
8bb93aac | 4237 | |
6791875e | 4238 | err = 0; |
8bb93aac N |
4239 | if (cmd_match(buf, "none")) { |
4240 | mddev->persistent = 0; | |
e691063a N |
4241 | mddev->external = 0; |
4242 | mddev->major_version = 0; | |
4243 | mddev->minor_version = 90; | |
6791875e | 4244 | goto out_unlock; |
e691063a N |
4245 | } |
4246 | if (strncmp(buf, "external:", 9) == 0) { | |
20a49ff6 | 4247 | size_t namelen = len-9; |
e691063a N |
4248 | if (namelen >= sizeof(mddev->metadata_type)) |
4249 | namelen = sizeof(mddev->metadata_type)-1; | |
4250 | strncpy(mddev->metadata_type, buf+9, namelen); | |
4251 | mddev->metadata_type[namelen] = 0; | |
4252 | if (namelen && mddev->metadata_type[namelen-1] == '\n') | |
4253 | mddev->metadata_type[--namelen] = 0; | |
4254 | mddev->persistent = 0; | |
4255 | mddev->external = 1; | |
8bb93aac N |
4256 | mddev->major_version = 0; |
4257 | mddev->minor_version = 90; | |
6791875e | 4258 | goto out_unlock; |
8bb93aac N |
4259 | } |
4260 | major = simple_strtoul(buf, &e, 10); | |
6791875e | 4261 | err = -EINVAL; |
8bb93aac | 4262 | if (e==buf || *e != '.') |
6791875e | 4263 | goto out_unlock; |
8bb93aac N |
4264 | buf = e+1; |
4265 | minor = simple_strtoul(buf, &e, 10); | |
3f9d7b0d | 4266 | if (e==buf || (*e && *e != '\n') ) |
6791875e N |
4267 | goto out_unlock; |
4268 | err = -ENOENT; | |
50511da3 | 4269 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
6791875e | 4270 | goto out_unlock; |
8bb93aac N |
4271 | mddev->major_version = major; |
4272 | mddev->minor_version = minor; | |
4273 | mddev->persistent = 1; | |
e691063a | 4274 | mddev->external = 0; |
6791875e N |
4275 | err = 0; |
4276 | out_unlock: | |
4277 | mddev_unlock(mddev); | |
4278 | return err ?: len; | |
8bb93aac N |
4279 | } |
4280 | ||
4281 | static struct md_sysfs_entry md_metadata = | |
750f199e | 4282 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
8bb93aac | 4283 | |
24dd469d | 4284 | static ssize_t |
fd01b88c | 4285 | action_show(struct mddev *mddev, char *page) |
24dd469d | 4286 | { |
7eec314d | 4287 | char *type = "idle"; |
b7b17c9b N |
4288 | unsigned long recovery = mddev->recovery; |
4289 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) | |
b6a9ce68 | 4290 | type = "frozen"; |
b7b17c9b N |
4291 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
4292 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { | |
4293 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) | |
ccfcc3c1 | 4294 | type = "reshape"; |
b7b17c9b N |
4295 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
4296 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) | |
24dd469d | 4297 | type = "resync"; |
b7b17c9b | 4298 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
24dd469d N |
4299 | type = "check"; |
4300 | else | |
4301 | type = "repair"; | |
b7b17c9b | 4302 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
24dd469d | 4303 | type = "recover"; |
985ca973 N |
4304 | else if (mddev->reshape_position != MaxSector) |
4305 | type = "reshape"; | |
24dd469d N |
4306 | } |
4307 | return sprintf(page, "%s\n", type); | |
4308 | } | |
4309 | ||
4310 | static ssize_t | |
fd01b88c | 4311 | action_store(struct mddev *mddev, const char *page, size_t len) |
24dd469d | 4312 | { |
7eec314d N |
4313 | if (!mddev->pers || !mddev->pers->sync_request) |
4314 | return -EINVAL; | |
4315 | ||
b6a9ce68 N |
4316 | |
4317 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { | |
56ccc112 N |
4318 | if (cmd_match(page, "frozen")) |
4319 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
4320 | else | |
4321 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
8e8e2518 N |
4322 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
4323 | mddev_lock(mddev) == 0) { | |
4324 | flush_workqueue(md_misc_wq); | |
4325 | if (mddev->sync_thread) { | |
4326 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
6791875e | 4327 | md_reap_sync_thread(mddev); |
6791875e | 4328 | } |
8e8e2518 | 4329 | mddev_unlock(mddev); |
7eec314d | 4330 | } |
312045ee | 4331 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
24dd469d | 4332 | return -EBUSY; |
72a23c21 | 4333 | else if (cmd_match(page, "resync")) |
56ccc112 | 4334 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
72a23c21 | 4335 | else if (cmd_match(page, "recover")) { |
56ccc112 | 4336 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
72a23c21 | 4337 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
72a23c21 | 4338 | } else if (cmd_match(page, "reshape")) { |
16484bf5 N |
4339 | int err; |
4340 | if (mddev->pers->start_reshape == NULL) | |
4341 | return -EINVAL; | |
6791875e N |
4342 | err = mddev_lock(mddev); |
4343 | if (!err) { | |
312045ee N |
4344 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
4345 | err = -EBUSY; | |
4346 | else { | |
4347 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
4348 | err = mddev->pers->start_reshape(mddev); | |
4349 | } | |
6791875e N |
4350 | mddev_unlock(mddev); |
4351 | } | |
16484bf5 N |
4352 | if (err) |
4353 | return err; | |
a99ac971 | 4354 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
16484bf5 | 4355 | } else { |
bce74dac | 4356 | if (cmd_match(page, "check")) |
7eec314d | 4357 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
2adc7d47 | 4358 | else if (!cmd_match(page, "repair")) |
7eec314d | 4359 | return -EINVAL; |
56ccc112 | 4360 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
7eec314d N |
4361 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
4362 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
7eec314d | 4363 | } |
48c26ddc N |
4364 | if (mddev->ro == 2) { |
4365 | /* A write to sync_action is enough to justify | |
4366 | * canceling read-auto mode | |
4367 | */ | |
4368 | mddev->ro = 0; | |
4369 | md_wakeup_thread(mddev->sync_thread); | |
4370 | } | |
03c902e1 | 4371 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
24dd469d | 4372 | md_wakeup_thread(mddev->thread); |
00bcb4ac | 4373 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
24dd469d N |
4374 | return len; |
4375 | } | |
4376 | ||
c4a39551 | 4377 | static struct md_sysfs_entry md_scan_mode = |
750f199e | 4378 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
c4a39551 JB |
4379 | |
4380 | static ssize_t | |
4381 | last_sync_action_show(struct mddev *mddev, char *page) | |
4382 | { | |
4383 | return sprintf(page, "%s\n", mddev->last_sync_action); | |
4384 | } | |
4385 | ||
4386 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); | |
4387 | ||
9d88883e | 4388 | static ssize_t |
fd01b88c | 4389 | mismatch_cnt_show(struct mddev *mddev, char *page) |
9d88883e N |
4390 | { |
4391 | return sprintf(page, "%llu\n", | |
7f7583d4 JM |
4392 | (unsigned long long) |
4393 | atomic64_read(&mddev->resync_mismatches)); | |
9d88883e N |
4394 | } |
4395 | ||
80ca3a44 | 4396 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
9d88883e | 4397 | |
88202a0c | 4398 | static ssize_t |
fd01b88c | 4399 | sync_min_show(struct mddev *mddev, char *page) |
88202a0c N |
4400 | { |
4401 | return sprintf(page, "%d (%s)\n", speed_min(mddev), | |
4402 | mddev->sync_speed_min ? "local": "system"); | |
4403 | } | |
4404 | ||
4405 | static ssize_t | |
fd01b88c | 4406 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
88202a0c | 4407 | { |
4c9309c0 AD |
4408 | unsigned int min; |
4409 | int rv; | |
4410 | ||
88202a0c | 4411 | if (strncmp(buf, "system", 6)==0) { |
4c9309c0 AD |
4412 | min = 0; |
4413 | } else { | |
4414 | rv = kstrtouint(buf, 10, &min); | |
4415 | if (rv < 0) | |
4416 | return rv; | |
4417 | if (min == 0) | |
4418 | return -EINVAL; | |
88202a0c | 4419 | } |
88202a0c N |
4420 | mddev->sync_speed_min = min; |
4421 | return len; | |
4422 | } | |
4423 | ||
4424 | static struct md_sysfs_entry md_sync_min = | |
4425 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); | |
4426 | ||
4427 | static ssize_t | |
fd01b88c | 4428 | sync_max_show(struct mddev *mddev, char *page) |
88202a0c N |
4429 | { |
4430 | return sprintf(page, "%d (%s)\n", speed_max(mddev), | |
4431 | mddev->sync_speed_max ? "local": "system"); | |
4432 | } | |
4433 | ||
4434 | static ssize_t | |
fd01b88c | 4435 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
88202a0c | 4436 | { |
4c9309c0 AD |
4437 | unsigned int max; |
4438 | int rv; | |
4439 | ||
88202a0c | 4440 | if (strncmp(buf, "system", 6)==0) { |
4c9309c0 AD |
4441 | max = 0; |
4442 | } else { | |
4443 | rv = kstrtouint(buf, 10, &max); | |
4444 | if (rv < 0) | |
4445 | return rv; | |
4446 | if (max == 0) | |
4447 | return -EINVAL; | |
88202a0c | 4448 | } |
88202a0c N |
4449 | mddev->sync_speed_max = max; |
4450 | return len; | |
4451 | } | |
4452 | ||
4453 | static struct md_sysfs_entry md_sync_max = | |
4454 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); | |
4455 | ||
d7f3d291 | 4456 | static ssize_t |
fd01b88c | 4457 | degraded_show(struct mddev *mddev, char *page) |
d7f3d291 IP |
4458 | { |
4459 | return sprintf(page, "%d\n", mddev->degraded); | |
4460 | } | |
4461 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); | |
88202a0c | 4462 | |
90b08710 | 4463 | static ssize_t |
fd01b88c | 4464 | sync_force_parallel_show(struct mddev *mddev, char *page) |
90b08710 BS |
4465 | { |
4466 | return sprintf(page, "%d\n", mddev->parallel_resync); | |
4467 | } | |
4468 | ||
4469 | static ssize_t | |
fd01b88c | 4470 | sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len) |
90b08710 BS |
4471 | { |
4472 | long n; | |
4473 | ||
b29bebd6 | 4474 | if (kstrtol(buf, 10, &n)) |
90b08710 BS |
4475 | return -EINVAL; |
4476 | ||
4477 | if (n != 0 && n != 1) | |
4478 | return -EINVAL; | |
4479 | ||
4480 | mddev->parallel_resync = n; | |
4481 | ||
4482 | if (mddev->sync_thread) | |
4483 | wake_up(&resync_wait); | |
4484 | ||
4485 | return len; | |
4486 | } | |
4487 | ||
4488 | /* force parallel resync, even with shared block devices */ | |
4489 | static struct md_sysfs_entry md_sync_force_parallel = | |
4490 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, | |
4491 | sync_force_parallel_show, sync_force_parallel_store); | |
4492 | ||
88202a0c | 4493 | static ssize_t |
fd01b88c | 4494 | sync_speed_show(struct mddev *mddev, char *page) |
88202a0c N |
4495 | { |
4496 | unsigned long resync, dt, db; | |
d1a7c503 N |
4497 | if (mddev->curr_resync == 0) |
4498 | return sprintf(page, "none\n"); | |
9687a60c AN |
4499 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
4500 | dt = (jiffies - mddev->resync_mark) / HZ; | |
88202a0c | 4501 | if (!dt) dt++; |
9687a60c AN |
4502 | db = resync - mddev->resync_mark_cnt; |
4503 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ | |
88202a0c N |
4504 | } |
4505 | ||
80ca3a44 | 4506 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
88202a0c N |
4507 | |
4508 | static ssize_t | |
fd01b88c | 4509 | sync_completed_show(struct mddev *mddev, char *page) |
88202a0c | 4510 | { |
13ae864b | 4511 | unsigned long long max_sectors, resync; |
88202a0c | 4512 | |
acb180b0 N |
4513 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
4514 | return sprintf(page, "none\n"); | |
4515 | ||
72f36d59 N |
4516 | if (mddev->curr_resync == 1 || |
4517 | mddev->curr_resync == 2) | |
4518 | return sprintf(page, "delayed\n"); | |
4519 | ||
c804cdec N |
4520 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
4521 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
58c0fed4 | 4522 | max_sectors = mddev->resync_max_sectors; |
88202a0c | 4523 | else |
58c0fed4 | 4524 | max_sectors = mddev->dev_sectors; |
88202a0c | 4525 | |
acb180b0 | 4526 | resync = mddev->curr_resync_completed; |
13ae864b | 4527 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
88202a0c N |
4528 | } |
4529 | ||
750f199e N |
4530 | static struct md_sysfs_entry md_sync_completed = |
4531 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); | |
88202a0c | 4532 | |
5e96ee65 | 4533 | static ssize_t |
fd01b88c | 4534 | min_sync_show(struct mddev *mddev, char *page) |
5e96ee65 NB |
4535 | { |
4536 | return sprintf(page, "%llu\n", | |
4537 | (unsigned long long)mddev->resync_min); | |
4538 | } | |
4539 | static ssize_t | |
fd01b88c | 4540 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
5e96ee65 NB |
4541 | { |
4542 | unsigned long long min; | |
23da422b | 4543 | int err; |
23da422b | 4544 | |
b29bebd6 | 4545 | if (kstrtoull(buf, 10, &min)) |
5e96ee65 | 4546 | return -EINVAL; |
23da422b N |
4547 | |
4548 | spin_lock(&mddev->lock); | |
4549 | err = -EINVAL; | |
5e96ee65 | 4550 | if (min > mddev->resync_max) |
23da422b N |
4551 | goto out_unlock; |
4552 | ||
4553 | err = -EBUSY; | |
5e96ee65 | 4554 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
23da422b | 4555 | goto out_unlock; |
5e96ee65 | 4556 | |
50c37b13 N |
4557 | /* Round down to multiple of 4K for safety */ |
4558 | mddev->resync_min = round_down(min, 8); | |
23da422b | 4559 | err = 0; |
5e96ee65 | 4560 | |
23da422b N |
4561 | out_unlock: |
4562 | spin_unlock(&mddev->lock); | |
4563 | return err ?: len; | |
5e96ee65 NB |
4564 | } |
4565 | ||
4566 | static struct md_sysfs_entry md_min_sync = | |
4567 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); | |
4568 | ||
c6207277 | 4569 | static ssize_t |
fd01b88c | 4570 | max_sync_show(struct mddev *mddev, char *page) |
c6207277 N |
4571 | { |
4572 | if (mddev->resync_max == MaxSector) | |
4573 | return sprintf(page, "max\n"); | |
4574 | else | |
4575 | return sprintf(page, "%llu\n", | |
4576 | (unsigned long long)mddev->resync_max); | |
4577 | } | |
4578 | static ssize_t | |
fd01b88c | 4579 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
c6207277 | 4580 | { |
23da422b N |
4581 | int err; |
4582 | spin_lock(&mddev->lock); | |
c6207277 N |
4583 | if (strncmp(buf, "max", 3) == 0) |
4584 | mddev->resync_max = MaxSector; | |
4585 | else { | |
5e96ee65 | 4586 | unsigned long long max; |
23da422b N |
4587 | int chunk; |
4588 | ||
4589 | err = -EINVAL; | |
b29bebd6 | 4590 | if (kstrtoull(buf, 10, &max)) |
23da422b | 4591 | goto out_unlock; |
5e96ee65 | 4592 | if (max < mddev->resync_min) |
23da422b N |
4593 | goto out_unlock; |
4594 | ||
4595 | err = -EBUSY; | |
c6207277 | 4596 | if (max < mddev->resync_max && |
4d484a4a | 4597 | mddev->ro == 0 && |
c6207277 | 4598 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
23da422b | 4599 | goto out_unlock; |
c6207277 N |
4600 | |
4601 | /* Must be a multiple of chunk_size */ | |
23da422b N |
4602 | chunk = mddev->chunk_sectors; |
4603 | if (chunk) { | |
2ac06c33 | 4604 | sector_t temp = max; |
23da422b N |
4605 | |
4606 | err = -EINVAL; | |
4607 | if (sector_div(temp, chunk)) | |
4608 | goto out_unlock; | |
c6207277 N |
4609 | } |
4610 | mddev->resync_max = max; | |
4611 | } | |
4612 | wake_up(&mddev->recovery_wait); | |
23da422b N |
4613 | err = 0; |
4614 | out_unlock: | |
4615 | spin_unlock(&mddev->lock); | |
4616 | return err ?: len; | |
c6207277 N |
4617 | } |
4618 | ||
4619 | static struct md_sysfs_entry md_max_sync = | |
4620 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); | |
4621 | ||
e464eafd | 4622 | static ssize_t |
fd01b88c | 4623 | suspend_lo_show(struct mddev *mddev, char *page) |
e464eafd N |
4624 | { |
4625 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); | |
4626 | } | |
4627 | ||
4628 | static ssize_t | |
fd01b88c | 4629 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
e464eafd | 4630 | { |
4c9309c0 | 4631 | unsigned long long old, new; |
6791875e | 4632 | int err; |
e464eafd | 4633 | |
4c9309c0 AD |
4634 | err = kstrtoull(buf, 10, &new); |
4635 | if (err < 0) | |
4636 | return err; | |
4637 | if (new != (sector_t)new) | |
e464eafd | 4638 | return -EINVAL; |
23ddff37 | 4639 | |
6791875e N |
4640 | err = mddev_lock(mddev); |
4641 | if (err) | |
4642 | return err; | |
4643 | err = -EINVAL; | |
4644 | if (mddev->pers == NULL || | |
4645 | mddev->pers->quiesce == NULL) | |
4646 | goto unlock; | |
4647 | old = mddev->suspend_lo; | |
23ddff37 N |
4648 | mddev->suspend_lo = new; |
4649 | if (new >= old) | |
4650 | /* Shrinking suspended region */ | |
e464eafd | 4651 | mddev->pers->quiesce(mddev, 2); |
23ddff37 N |
4652 | else { |
4653 | /* Expanding suspended region - need to wait */ | |
4654 | mddev->pers->quiesce(mddev, 1); | |
4655 | mddev->pers->quiesce(mddev, 0); | |
4656 | } | |
6791875e N |
4657 | err = 0; |
4658 | unlock: | |
4659 | mddev_unlock(mddev); | |
4660 | return err ?: len; | |
e464eafd N |
4661 | } |
4662 | static struct md_sysfs_entry md_suspend_lo = | |
4663 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); | |
4664 | ||
e464eafd | 4665 | static ssize_t |
fd01b88c | 4666 | suspend_hi_show(struct mddev *mddev, char *page) |
e464eafd N |
4667 | { |
4668 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); | |
4669 | } | |
4670 | ||
4671 | static ssize_t | |
fd01b88c | 4672 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
e464eafd | 4673 | { |
4c9309c0 | 4674 | unsigned long long old, new; |
6791875e | 4675 | int err; |
e464eafd | 4676 | |
4c9309c0 AD |
4677 | err = kstrtoull(buf, 10, &new); |
4678 | if (err < 0) | |
4679 | return err; | |
4680 | if (new != (sector_t)new) | |
e464eafd | 4681 | return -EINVAL; |
23ddff37 | 4682 | |
6791875e N |
4683 | err = mddev_lock(mddev); |
4684 | if (err) | |
4685 | return err; | |
4686 | err = -EINVAL; | |
4687 | if (mddev->pers == NULL || | |
4688 | mddev->pers->quiesce == NULL) | |
4689 | goto unlock; | |
4690 | old = mddev->suspend_hi; | |
23ddff37 N |
4691 | mddev->suspend_hi = new; |
4692 | if (new <= old) | |
4693 | /* Shrinking suspended region */ | |
4694 | mddev->pers->quiesce(mddev, 2); | |
4695 | else { | |
4696 | /* Expanding suspended region - need to wait */ | |
e464eafd N |
4697 | mddev->pers->quiesce(mddev, 1); |
4698 | mddev->pers->quiesce(mddev, 0); | |
23ddff37 | 4699 | } |
6791875e N |
4700 | err = 0; |
4701 | unlock: | |
4702 | mddev_unlock(mddev); | |
4703 | return err ?: len; | |
e464eafd N |
4704 | } |
4705 | static struct md_sysfs_entry md_suspend_hi = | |
4706 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); | |
4707 | ||
08a02ecd | 4708 | static ssize_t |
fd01b88c | 4709 | reshape_position_show(struct mddev *mddev, char *page) |
08a02ecd N |
4710 | { |
4711 | if (mddev->reshape_position != MaxSector) | |
4712 | return sprintf(page, "%llu\n", | |
4713 | (unsigned long long)mddev->reshape_position); | |
4714 | strcpy(page, "none\n"); | |
4715 | return 5; | |
4716 | } | |
4717 | ||
4718 | static ssize_t | |
fd01b88c | 4719 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
08a02ecd | 4720 | { |
c6563a8c | 4721 | struct md_rdev *rdev; |
4c9309c0 | 4722 | unsigned long long new; |
6791875e | 4723 | int err; |
6791875e | 4724 | |
4c9309c0 AD |
4725 | err = kstrtoull(buf, 10, &new); |
4726 | if (err < 0) | |
4727 | return err; | |
4728 | if (new != (sector_t)new) | |
08a02ecd | 4729 | return -EINVAL; |
6791875e N |
4730 | err = mddev_lock(mddev); |
4731 | if (err) | |
4732 | return err; | |
4733 | err = -EBUSY; | |
4734 | if (mddev->pers) | |
4735 | goto unlock; | |
08a02ecd N |
4736 | mddev->reshape_position = new; |
4737 | mddev->delta_disks = 0; | |
2c810cdd | 4738 | mddev->reshape_backwards = 0; |
08a02ecd N |
4739 | mddev->new_level = mddev->level; |
4740 | mddev->new_layout = mddev->layout; | |
664e7c41 | 4741 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
c6563a8c N |
4742 | rdev_for_each(rdev, mddev) |
4743 | rdev->new_data_offset = rdev->data_offset; | |
6791875e N |
4744 | err = 0; |
4745 | unlock: | |
4746 | mddev_unlock(mddev); | |
4747 | return err ?: len; | |
08a02ecd N |
4748 | } |
4749 | ||
4750 | static struct md_sysfs_entry md_reshape_position = | |
4751 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, | |
4752 | reshape_position_store); | |
4753 | ||
2c810cdd N |
4754 | static ssize_t |
4755 | reshape_direction_show(struct mddev *mddev, char *page) | |
4756 | { | |
4757 | return sprintf(page, "%s\n", | |
4758 | mddev->reshape_backwards ? "backwards" : "forwards"); | |
4759 | } | |
4760 | ||
4761 | static ssize_t | |
4762 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) | |
4763 | { | |
4764 | int backwards = 0; | |
6791875e N |
4765 | int err; |
4766 | ||
2c810cdd N |
4767 | if (cmd_match(buf, "forwards")) |
4768 | backwards = 0; | |
4769 | else if (cmd_match(buf, "backwards")) | |
4770 | backwards = 1; | |
4771 | else | |
4772 | return -EINVAL; | |
4773 | if (mddev->reshape_backwards == backwards) | |
4774 | return len; | |
4775 | ||
6791875e N |
4776 | err = mddev_lock(mddev); |
4777 | if (err) | |
4778 | return err; | |
2c810cdd N |
4779 | /* check if we are allowed to change */ |
4780 | if (mddev->delta_disks) | |
6791875e N |
4781 | err = -EBUSY; |
4782 | else if (mddev->persistent && | |
2c810cdd | 4783 | mddev->major_version == 0) |
6791875e N |
4784 | err = -EINVAL; |
4785 | else | |
4786 | mddev->reshape_backwards = backwards; | |
4787 | mddev_unlock(mddev); | |
4788 | return err ?: len; | |
2c810cdd N |
4789 | } |
4790 | ||
4791 | static struct md_sysfs_entry md_reshape_direction = | |
4792 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, | |
4793 | reshape_direction_store); | |
4794 | ||
b522adcd | 4795 | static ssize_t |
fd01b88c | 4796 | array_size_show(struct mddev *mddev, char *page) |
b522adcd DW |
4797 | { |
4798 | if (mddev->external_size) | |
4799 | return sprintf(page, "%llu\n", | |
4800 | (unsigned long long)mddev->array_sectors/2); | |
4801 | else | |
4802 | return sprintf(page, "default\n"); | |
4803 | } | |
4804 | ||
4805 | static ssize_t | |
fd01b88c | 4806 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
b522adcd DW |
4807 | { |
4808 | sector_t sectors; | |
6791875e N |
4809 | int err; |
4810 | ||
4811 | err = mddev_lock(mddev); | |
4812 | if (err) | |
4813 | return err; | |
b522adcd DW |
4814 | |
4815 | if (strncmp(buf, "default", 7) == 0) { | |
4816 | if (mddev->pers) | |
4817 | sectors = mddev->pers->size(mddev, 0, 0); | |
4818 | else | |
4819 | sectors = mddev->array_sectors; | |
4820 | ||
4821 | mddev->external_size = 0; | |
4822 | } else { | |
4823 | if (strict_blocks_to_sectors(buf, §ors) < 0) | |
6791875e N |
4824 | err = -EINVAL; |
4825 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) | |
4826 | err = -E2BIG; | |
4827 | else | |
4828 | mddev->external_size = 1; | |
b522adcd DW |
4829 | } |
4830 | ||
6791875e N |
4831 | if (!err) { |
4832 | mddev->array_sectors = sectors; | |
4833 | if (mddev->pers) { | |
4834 | set_capacity(mddev->gendisk, mddev->array_sectors); | |
4835 | revalidate_disk(mddev->gendisk); | |
4836 | } | |
cbe6ef1d | 4837 | } |
6791875e N |
4838 | mddev_unlock(mddev); |
4839 | return err ?: len; | |
b522adcd DW |
4840 | } |
4841 | ||
4842 | static struct md_sysfs_entry md_array_size = | |
4843 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, | |
4844 | array_size_store); | |
e464eafd | 4845 | |
eae1701f N |
4846 | static struct attribute *md_default_attrs[] = { |
4847 | &md_level.attr, | |
d4dbd025 | 4848 | &md_layout.attr, |
eae1701f | 4849 | &md_raid_disks.attr, |
3b34380a | 4850 | &md_chunk_size.attr, |
a35b0d69 | 4851 | &md_size.attr, |
a94213b1 | 4852 | &md_resync_start.attr, |
8bb93aac | 4853 | &md_metadata.attr, |
6d7ff738 | 4854 | &md_new_device.attr, |
16f17b39 | 4855 | &md_safe_delay.attr, |
9e653b63 | 4856 | &md_array_state.attr, |
08a02ecd | 4857 | &md_reshape_position.attr, |
2c810cdd | 4858 | &md_reshape_direction.attr, |
b522adcd | 4859 | &md_array_size.attr, |
1e50915f | 4860 | &max_corr_read_errors.attr, |
411036fa N |
4861 | NULL, |
4862 | }; | |
4863 | ||
4864 | static struct attribute *md_redundancy_attrs[] = { | |
24dd469d | 4865 | &md_scan_mode.attr, |
c4a39551 | 4866 | &md_last_scan_mode.attr, |
9d88883e | 4867 | &md_mismatches.attr, |
88202a0c N |
4868 | &md_sync_min.attr, |
4869 | &md_sync_max.attr, | |
4870 | &md_sync_speed.attr, | |
90b08710 | 4871 | &md_sync_force_parallel.attr, |
88202a0c | 4872 | &md_sync_completed.attr, |
5e96ee65 | 4873 | &md_min_sync.attr, |
c6207277 | 4874 | &md_max_sync.attr, |
e464eafd N |
4875 | &md_suspend_lo.attr, |
4876 | &md_suspend_hi.attr, | |
9b1d1dac | 4877 | &md_bitmap.attr, |
d7f3d291 | 4878 | &md_degraded.attr, |
eae1701f N |
4879 | NULL, |
4880 | }; | |
411036fa N |
4881 | static struct attribute_group md_redundancy_group = { |
4882 | .name = NULL, | |
4883 | .attrs = md_redundancy_attrs, | |
4884 | }; | |
4885 | ||
eae1701f N |
4886 | static ssize_t |
4887 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
4888 | { | |
4889 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); | |
fd01b88c | 4890 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
96de1e66 | 4891 | ssize_t rv; |
eae1701f N |
4892 | |
4893 | if (!entry->show) | |
4894 | return -EIO; | |
af8a2434 N |
4895 | spin_lock(&all_mddevs_lock); |
4896 | if (list_empty(&mddev->all_mddevs)) { | |
4897 | spin_unlock(&all_mddevs_lock); | |
4898 | return -EBUSY; | |
4899 | } | |
4900 | mddev_get(mddev); | |
4901 | spin_unlock(&all_mddevs_lock); | |
4902 | ||
b7b17c9b | 4903 | rv = entry->show(mddev, page); |
af8a2434 | 4904 | mddev_put(mddev); |
96de1e66 | 4905 | return rv; |
eae1701f N |
4906 | } |
4907 | ||
4908 | static ssize_t | |
4909 | md_attr_store(struct kobject *kobj, struct attribute *attr, | |
4910 | const char *page, size_t length) | |
4911 | { | |
4912 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); | |
fd01b88c | 4913 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
96de1e66 | 4914 | ssize_t rv; |
eae1701f N |
4915 | |
4916 | if (!entry->store) | |
4917 | return -EIO; | |
67463acb N |
4918 | if (!capable(CAP_SYS_ADMIN)) |
4919 | return -EACCES; | |
af8a2434 N |
4920 | spin_lock(&all_mddevs_lock); |
4921 | if (list_empty(&mddev->all_mddevs)) { | |
4922 | spin_unlock(&all_mddevs_lock); | |
4923 | return -EBUSY; | |
4924 | } | |
4925 | mddev_get(mddev); | |
4926 | spin_unlock(&all_mddevs_lock); | |
6791875e | 4927 | rv = entry->store(mddev, page, length); |
af8a2434 | 4928 | mddev_put(mddev); |
96de1e66 | 4929 | return rv; |
eae1701f N |
4930 | } |
4931 | ||
4932 | static void md_free(struct kobject *ko) | |
4933 | { | |
fd01b88c | 4934 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
a21d1504 N |
4935 | |
4936 | if (mddev->sysfs_state) | |
4937 | sysfs_put(mddev->sysfs_state); | |
4938 | ||
6cd18e71 N |
4939 | if (mddev->queue) |
4940 | blk_cleanup_queue(mddev->queue); | |
a21d1504 N |
4941 | if (mddev->gendisk) { |
4942 | del_gendisk(mddev->gendisk); | |
4943 | put_disk(mddev->gendisk); | |
4944 | } | |
a21d1504 | 4945 | |
eae1701f N |
4946 | kfree(mddev); |
4947 | } | |
4948 | ||
52cf25d0 | 4949 | static const struct sysfs_ops md_sysfs_ops = { |
eae1701f N |
4950 | .show = md_attr_show, |
4951 | .store = md_attr_store, | |
4952 | }; | |
4953 | static struct kobj_type md_ktype = { | |
4954 | .release = md_free, | |
4955 | .sysfs_ops = &md_sysfs_ops, | |
4956 | .default_attrs = md_default_attrs, | |
4957 | }; | |
4958 | ||
1da177e4 LT |
4959 | int mdp_major = 0; |
4960 | ||
5fd3a17e DW |
4961 | static void mddev_delayed_delete(struct work_struct *ws) |
4962 | { | |
fd01b88c | 4963 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
5fd3a17e | 4964 | |
43a70507 | 4965 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
5fd3a17e DW |
4966 | kobject_del(&mddev->kobj); |
4967 | kobject_put(&mddev->kobj); | |
4968 | } | |
4969 | ||
efeb53c0 | 4970 | static int md_alloc(dev_t dev, char *name) |
1da177e4 | 4971 | { |
48c9c27b | 4972 | static DEFINE_MUTEX(disks_mutex); |
fd01b88c | 4973 | struct mddev *mddev = mddev_find(dev); |
1da177e4 | 4974 | struct gendisk *disk; |
efeb53c0 N |
4975 | int partitioned; |
4976 | int shift; | |
4977 | int unit; | |
3830c62f | 4978 | int error; |
1da177e4 LT |
4979 | |
4980 | if (!mddev) | |
efeb53c0 N |
4981 | return -ENODEV; |
4982 | ||
4983 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); | |
4984 | shift = partitioned ? MdpMinorShift : 0; | |
4985 | unit = MINOR(mddev->unit) >> shift; | |
1da177e4 | 4986 | |
e804ac78 TH |
4987 | /* wait for any previous instance of this device to be |
4988 | * completely removed (mddev_delayed_delete). | |
d3374825 | 4989 | */ |
e804ac78 | 4990 | flush_workqueue(md_misc_wq); |
d3374825 | 4991 | |
48c9c27b | 4992 | mutex_lock(&disks_mutex); |
0909dc44 N |
4993 | error = -EEXIST; |
4994 | if (mddev->gendisk) | |
4995 | goto abort; | |
efeb53c0 N |
4996 | |
4997 | if (name) { | |
4998 | /* Need to ensure that 'name' is not a duplicate. | |
4999 | */ | |
fd01b88c | 5000 | struct mddev *mddev2; |
efeb53c0 N |
5001 | spin_lock(&all_mddevs_lock); |
5002 | ||
5003 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) | |
5004 | if (mddev2->gendisk && | |
5005 | strcmp(mddev2->gendisk->disk_name, name) == 0) { | |
5006 | spin_unlock(&all_mddevs_lock); | |
0909dc44 | 5007 | goto abort; |
efeb53c0 N |
5008 | } |
5009 | spin_unlock(&all_mddevs_lock); | |
1da177e4 | 5010 | } |
8b765398 | 5011 | |
0909dc44 | 5012 | error = -ENOMEM; |
8b765398 | 5013 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
0909dc44 N |
5014 | if (!mddev->queue) |
5015 | goto abort; | |
409c57f3 N |
5016 | mddev->queue->queuedata = mddev; |
5017 | ||
409c57f3 | 5018 | blk_queue_make_request(mddev->queue, md_make_request); |
b1bd055d | 5019 | blk_set_stacking_limits(&mddev->queue->limits); |
8b765398 | 5020 | |
1da177e4 LT |
5021 | disk = alloc_disk(1 << shift); |
5022 | if (!disk) { | |
8b765398 N |
5023 | blk_cleanup_queue(mddev->queue); |
5024 | mddev->queue = NULL; | |
0909dc44 | 5025 | goto abort; |
1da177e4 | 5026 | } |
efeb53c0 | 5027 | disk->major = MAJOR(mddev->unit); |
1da177e4 | 5028 | disk->first_minor = unit << shift; |
efeb53c0 N |
5029 | if (name) |
5030 | strcpy(disk->disk_name, name); | |
5031 | else if (partitioned) | |
1da177e4 | 5032 | sprintf(disk->disk_name, "md_d%d", unit); |
ce7b0f46 | 5033 | else |
1da177e4 | 5034 | sprintf(disk->disk_name, "md%d", unit); |
1da177e4 LT |
5035 | disk->fops = &md_fops; |
5036 | disk->private_data = mddev; | |
5037 | disk->queue = mddev->queue; | |
b0140891 | 5038 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
92850bbd | 5039 | /* Allow extended partitions. This makes the |
d3374825 | 5040 | * 'mdp' device redundant, but we can't really |
92850bbd N |
5041 | * remove it now. |
5042 | */ | |
5043 | disk->flags |= GENHD_FL_EXT_DEVT; | |
1da177e4 | 5044 | mddev->gendisk = disk; |
b0140891 N |
5045 | /* As soon as we call add_disk(), another thread could get |
5046 | * through to md_open, so make sure it doesn't get too far | |
5047 | */ | |
5048 | mutex_lock(&mddev->open_mutex); | |
5049 | add_disk(disk); | |
5050 | ||
ed9e1982 TH |
5051 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
5052 | &disk_to_dev(disk)->kobj, "%s", "md"); | |
0909dc44 N |
5053 | if (error) { |
5054 | /* This isn't possible, but as kobject_init_and_add is marked | |
5055 | * __must_check, we must do something with the result | |
5056 | */ | |
5e55e2f5 N |
5057 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
5058 | disk->disk_name); | |
0909dc44 N |
5059 | error = 0; |
5060 | } | |
00bcb4ac N |
5061 | if (mddev->kobj.sd && |
5062 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) | |
43a70507 | 5063 | printk(KERN_DEBUG "pointless warning\n"); |
b0140891 | 5064 | mutex_unlock(&mddev->open_mutex); |
0909dc44 N |
5065 | abort: |
5066 | mutex_unlock(&disks_mutex); | |
00bcb4ac | 5067 | if (!error && mddev->kobj.sd) { |
3830c62f | 5068 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
00bcb4ac | 5069 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
b62b7590 | 5070 | } |
d3374825 | 5071 | mddev_put(mddev); |
0909dc44 | 5072 | return error; |
efeb53c0 N |
5073 | } |
5074 | ||
5075 | static struct kobject *md_probe(dev_t dev, int *part, void *data) | |
5076 | { | |
5077 | md_alloc(dev, NULL); | |
1da177e4 LT |
5078 | return NULL; |
5079 | } | |
5080 | ||
efeb53c0 N |
5081 | static int add_named_array(const char *val, struct kernel_param *kp) |
5082 | { | |
5083 | /* val must be "md_*" where * is not all digits. | |
5084 | * We allocate an array with a large free minor number, and | |
5085 | * set the name to val. val must not already be an active name. | |
5086 | */ | |
5087 | int len = strlen(val); | |
5088 | char buf[DISK_NAME_LEN]; | |
5089 | ||
5090 | while (len && val[len-1] == '\n') | |
5091 | len--; | |
5092 | if (len >= DISK_NAME_LEN) | |
5093 | return -E2BIG; | |
5094 | strlcpy(buf, val, len+1); | |
5095 | if (strncmp(buf, "md_", 3) != 0) | |
5096 | return -EINVAL; | |
5097 | return md_alloc(0, buf); | |
5098 | } | |
5099 | ||
1da177e4 LT |
5100 | static void md_safemode_timeout(unsigned long data) |
5101 | { | |
fd01b88c | 5102 | struct mddev *mddev = (struct mddev *) data; |
1da177e4 | 5103 | |
0fd62b86 NB |
5104 | if (!atomic_read(&mddev->writes_pending)) { |
5105 | mddev->safemode = 1; | |
5106 | if (mddev->external) | |
00bcb4ac | 5107 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0fd62b86 | 5108 | } |
1da177e4 LT |
5109 | md_wakeup_thread(mddev->thread); |
5110 | } | |
5111 | ||
6ff8d8ec | 5112 | static int start_dirty_degraded; |
1da177e4 | 5113 | |
fd01b88c | 5114 | int md_run(struct mddev *mddev) |
1da177e4 | 5115 | { |
2604b703 | 5116 | int err; |
3cb03002 | 5117 | struct md_rdev *rdev; |
84fc4b56 | 5118 | struct md_personality *pers; |
1da177e4 | 5119 | |
a757e64c N |
5120 | if (list_empty(&mddev->disks)) |
5121 | /* cannot run an array with no devices.. */ | |
1da177e4 | 5122 | return -EINVAL; |
1da177e4 LT |
5123 | |
5124 | if (mddev->pers) | |
5125 | return -EBUSY; | |
bb4f1e9d N |
5126 | /* Cannot run until previous stop completes properly */ |
5127 | if (mddev->sysfs_active) | |
5128 | return -EBUSY; | |
b6eb127d | 5129 | |
1da177e4 LT |
5130 | /* |
5131 | * Analyze all RAID superblock(s) | |
5132 | */ | |
1ec4a939 N |
5133 | if (!mddev->raid_disks) { |
5134 | if (!mddev->persistent) | |
5135 | return -EINVAL; | |
a757e64c | 5136 | analyze_sbs(mddev); |
1ec4a939 | 5137 | } |
1da177e4 | 5138 | |
d9d166c2 N |
5139 | if (mddev->level != LEVEL_NONE) |
5140 | request_module("md-level-%d", mddev->level); | |
5141 | else if (mddev->clevel[0]) | |
5142 | request_module("md-%s", mddev->clevel); | |
1da177e4 LT |
5143 | |
5144 | /* | |
5145 | * Drop all container device buffers, from now on | |
5146 | * the only valid external interface is through the md | |
5147 | * device. | |
1da177e4 | 5148 | */ |
dafb20fa | 5149 | rdev_for_each(rdev, mddev) { |
b2d444d7 | 5150 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 LT |
5151 | continue; |
5152 | sync_blockdev(rdev->bdev); | |
f98393a6 | 5153 | invalidate_bdev(rdev->bdev); |
f0d76d70 N |
5154 | |
5155 | /* perform some consistency tests on the device. | |
5156 | * We don't want the data to overlap the metadata, | |
58c0fed4 | 5157 | * Internal Bitmap issues have been handled elsewhere. |
f0d76d70 | 5158 | */ |
a6ff7e08 JB |
5159 | if (rdev->meta_bdev) { |
5160 | /* Nothing to check */; | |
5161 | } else if (rdev->data_offset < rdev->sb_start) { | |
58c0fed4 AN |
5162 | if (mddev->dev_sectors && |
5163 | rdev->data_offset + mddev->dev_sectors | |
0f420358 | 5164 | > rdev->sb_start) { |
f0d76d70 N |
5165 | printk("md: %s: data overlaps metadata\n", |
5166 | mdname(mddev)); | |
5167 | return -EINVAL; | |
5168 | } | |
5169 | } else { | |
0f420358 | 5170 | if (rdev->sb_start + rdev->sb_size/512 |
f0d76d70 N |
5171 | > rdev->data_offset) { |
5172 | printk("md: %s: metadata overlaps data\n", | |
5173 | mdname(mddev)); | |
5174 | return -EINVAL; | |
5175 | } | |
5176 | } | |
00bcb4ac | 5177 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
1da177e4 LT |
5178 | } |
5179 | ||
a167f663 | 5180 | if (mddev->bio_set == NULL) |
395c72a7 | 5181 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
a167f663 | 5182 | |
1da177e4 | 5183 | spin_lock(&pers_lock); |
d9d166c2 | 5184 | pers = find_pers(mddev->level, mddev->clevel); |
2604b703 | 5185 | if (!pers || !try_module_get(pers->owner)) { |
1da177e4 | 5186 | spin_unlock(&pers_lock); |
d9d166c2 N |
5187 | if (mddev->level != LEVEL_NONE) |
5188 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", | |
5189 | mddev->level); | |
5190 | else | |
5191 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", | |
5192 | mddev->clevel); | |
1da177e4 LT |
5193 | return -EINVAL; |
5194 | } | |
1da177e4 | 5195 | spin_unlock(&pers_lock); |
34817e8c N |
5196 | if (mddev->level != pers->level) { |
5197 | mddev->level = pers->level; | |
5198 | mddev->new_level = pers->level; | |
5199 | } | |
d9d166c2 | 5200 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
1da177e4 | 5201 | |
f6705578 | 5202 | if (mddev->reshape_position != MaxSector && |
63c70c4f | 5203 | pers->start_reshape == NULL) { |
f6705578 | 5204 | /* This personality cannot handle reshaping... */ |
f6705578 N |
5205 | module_put(pers->owner); |
5206 | return -EINVAL; | |
5207 | } | |
5208 | ||
7dd5e7c3 N |
5209 | if (pers->sync_request) { |
5210 | /* Warn if this is a potentially silly | |
5211 | * configuration. | |
5212 | */ | |
5213 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
3cb03002 | 5214 | struct md_rdev *rdev2; |
7dd5e7c3 | 5215 | int warned = 0; |
159ec1fc | 5216 | |
dafb20fa N |
5217 | rdev_for_each(rdev, mddev) |
5218 | rdev_for_each(rdev2, mddev) { | |
7dd5e7c3 N |
5219 | if (rdev < rdev2 && |
5220 | rdev->bdev->bd_contains == | |
5221 | rdev2->bdev->bd_contains) { | |
5222 | printk(KERN_WARNING | |
5223 | "%s: WARNING: %s appears to be" | |
5224 | " on the same physical disk as" | |
5225 | " %s.\n", | |
5226 | mdname(mddev), | |
5227 | bdevname(rdev->bdev,b), | |
5228 | bdevname(rdev2->bdev,b2)); | |
5229 | warned = 1; | |
5230 | } | |
5231 | } | |
159ec1fc | 5232 | |
7dd5e7c3 N |
5233 | if (warned) |
5234 | printk(KERN_WARNING | |
5235 | "True protection against single-disk" | |
5236 | " failure might be compromised.\n"); | |
5237 | } | |
5238 | ||
657390d2 | 5239 | mddev->recovery = 0; |
58c0fed4 AN |
5240 | /* may be over-ridden by personality */ |
5241 | mddev->resync_max_sectors = mddev->dev_sectors; | |
5242 | ||
6ff8d8ec | 5243 | mddev->ok_start_degraded = start_dirty_degraded; |
1da177e4 | 5244 | |
0f9552b5 | 5245 | if (start_readonly && mddev->ro == 0) |
f91de92e N |
5246 | mddev->ro = 2; /* read-only, but switch on first write */ |
5247 | ||
36d091f4 | 5248 | err = pers->run(mddev); |
13e53df3 AN |
5249 | if (err) |
5250 | printk(KERN_ERR "md: pers->run() failed ...\n"); | |
36d091f4 | 5251 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
b522adcd DW |
5252 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
5253 | " but 'external_size' not in effect?\n", __func__); | |
5254 | printk(KERN_ERR | |
5255 | "md: invalid array_size %llu > default size %llu\n", | |
5256 | (unsigned long long)mddev->array_sectors / 2, | |
36d091f4 | 5257 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
b522adcd | 5258 | err = -EINVAL; |
b522adcd | 5259 | } |
36d091f4 | 5260 | if (err == 0 && pers->sync_request && |
ef99bf48 | 5261 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
f9209a32 GR |
5262 | struct bitmap *bitmap; |
5263 | ||
5264 | bitmap = bitmap_create(mddev, -1); | |
5265 | if (IS_ERR(bitmap)) { | |
5266 | err = PTR_ERR(bitmap); | |
b15c2e57 N |
5267 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
5268 | mdname(mddev), err); | |
f9209a32 GR |
5269 | } else |
5270 | mddev->bitmap = bitmap; | |
5271 | ||
b15c2e57 | 5272 | } |
1da177e4 | 5273 | if (err) { |
5aa61f42 | 5274 | mddev_detach(mddev); |
0c35bd47 N |
5275 | if (mddev->private) |
5276 | pers->free(mddev, mddev->private); | |
bd691922 | 5277 | mddev->private = NULL; |
36d091f4 | 5278 | module_put(pers->owner); |
32a7627c N |
5279 | bitmap_destroy(mddev); |
5280 | return err; | |
1da177e4 | 5281 | } |
5c675f83 N |
5282 | if (mddev->queue) { |
5283 | mddev->queue->backing_dev_info.congested_data = mddev; | |
5284 | mddev->queue->backing_dev_info.congested_fn = md_congested; | |
5285 | } | |
36d091f4 | 5286 | if (pers->sync_request) { |
00bcb4ac N |
5287 | if (mddev->kobj.sd && |
5288 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) | |
5e55e2f5 N |
5289 | printk(KERN_WARNING |
5290 | "md: cannot register extra attributes for %s\n", | |
5291 | mdname(mddev)); | |
00bcb4ac | 5292 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
5e55e2f5 | 5293 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
fd9d49ca N |
5294 | mddev->ro = 0; |
5295 | ||
f72ffdd6 | 5296 | atomic_set(&mddev->writes_pending,0); |
1e50915f RB |
5297 | atomic_set(&mddev->max_corr_read_errors, |
5298 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); | |
1da177e4 | 5299 | mddev->safemode = 0; |
28c1b9fd GR |
5300 | if (mddev_is_clustered(mddev)) |
5301 | mddev->safemode_delay = 0; | |
5302 | else | |
5303 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ | |
1da177e4 | 5304 | mddev->in_sync = 1; |
0ca69886 | 5305 | smp_wmb(); |
36d091f4 N |
5306 | spin_lock(&mddev->lock); |
5307 | mddev->pers = pers; | |
0ca69886 | 5308 | mddev->ready = 1; |
36d091f4 | 5309 | spin_unlock(&mddev->lock); |
dafb20fa | 5310 | rdev_for_each(rdev, mddev) |
36fad858 NK |
5311 | if (rdev->raid_disk >= 0) |
5312 | if (sysfs_link_rdev(mddev, rdev)) | |
00bcb4ac | 5313 | /* failure here is OK */; |
f72ffdd6 | 5314 | |
a4a3d26d N |
5315 | if (mddev->degraded && !mddev->ro) |
5316 | /* This ensures that recovering status is reported immediately | |
5317 | * via sysfs - until a lack of spares is confirmed. | |
5318 | */ | |
5319 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
1da177e4 | 5320 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
f72ffdd6 | 5321 | |
7a0a5355 | 5322 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
850b2b42 | 5323 | md_update_sb(mddev, 0); |
1da177e4 | 5324 | |
d7603b7e | 5325 | md_new_event(mddev); |
00bcb4ac N |
5326 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
5327 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
a99ac971 | 5328 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
1da177e4 LT |
5329 | return 0; |
5330 | } | |
390ee602 | 5331 | EXPORT_SYMBOL_GPL(md_run); |
1da177e4 | 5332 | |
fd01b88c | 5333 | static int do_md_run(struct mddev *mddev) |
fe60b014 N |
5334 | { |
5335 | int err; | |
5336 | ||
5337 | err = md_run(mddev); | |
5338 | if (err) | |
5339 | goto out; | |
69e51b44 N |
5340 | err = bitmap_load(mddev); |
5341 | if (err) { | |
5342 | bitmap_destroy(mddev); | |
5343 | goto out; | |
5344 | } | |
0fd018af | 5345 | |
28c1b9fd GR |
5346 | if (mddev_is_clustered(mddev)) |
5347 | md_allow_write(mddev); | |
5348 | ||
0fd018af JB |
5349 | md_wakeup_thread(mddev->thread); |
5350 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ | |
5351 | ||
fe60b014 N |
5352 | set_capacity(mddev->gendisk, mddev->array_sectors); |
5353 | revalidate_disk(mddev->gendisk); | |
f0b4f7e2 | 5354 | mddev->changed = 1; |
fe60b014 N |
5355 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
5356 | out: | |
5357 | return err; | |
5358 | } | |
5359 | ||
fd01b88c | 5360 | static int restart_array(struct mddev *mddev) |
1da177e4 LT |
5361 | { |
5362 | struct gendisk *disk = mddev->gendisk; | |
1da177e4 | 5363 | |
80fab1d7 | 5364 | /* Complain if it has no devices */ |
1da177e4 | 5365 | if (list_empty(&mddev->disks)) |
80fab1d7 AN |
5366 | return -ENXIO; |
5367 | if (!mddev->pers) | |
5368 | return -EINVAL; | |
5369 | if (!mddev->ro) | |
5370 | return -EBUSY; | |
339421de SL |
5371 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
5372 | struct md_rdev *rdev; | |
5373 | bool has_journal = false; | |
5374 | ||
5375 | rcu_read_lock(); | |
5376 | rdev_for_each_rcu(rdev, mddev) { | |
5377 | if (test_bit(Journal, &rdev->flags) && | |
5378 | !test_bit(Faulty, &rdev->flags)) { | |
5379 | has_journal = true; | |
5380 | break; | |
5381 | } | |
5382 | } | |
5383 | rcu_read_unlock(); | |
5384 | ||
5385 | /* Don't restart rw with journal missing/faulty */ | |
5386 | if (!has_journal) | |
5387 | return -EINVAL; | |
5388 | } | |
5389 | ||
80fab1d7 AN |
5390 | mddev->safemode = 0; |
5391 | mddev->ro = 0; | |
5392 | set_disk_ro(disk, 0); | |
5393 | printk(KERN_INFO "md: %s switched to read-write mode.\n", | |
5394 | mdname(mddev)); | |
5395 | /* Kick recovery or resync if necessary */ | |
5396 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
5397 | md_wakeup_thread(mddev->thread); | |
5398 | md_wakeup_thread(mddev->sync_thread); | |
00bcb4ac | 5399 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
80fab1d7 | 5400 | return 0; |
1da177e4 LT |
5401 | } |
5402 | ||
fd01b88c | 5403 | static void md_clean(struct mddev *mddev) |
6177b472 N |
5404 | { |
5405 | mddev->array_sectors = 0; | |
5406 | mddev->external_size = 0; | |
5407 | mddev->dev_sectors = 0; | |
5408 | mddev->raid_disks = 0; | |
5409 | mddev->recovery_cp = 0; | |
5410 | mddev->resync_min = 0; | |
5411 | mddev->resync_max = MaxSector; | |
5412 | mddev->reshape_position = MaxSector; | |
5413 | mddev->external = 0; | |
5414 | mddev->persistent = 0; | |
5415 | mddev->level = LEVEL_NONE; | |
5416 | mddev->clevel[0] = 0; | |
5417 | mddev->flags = 0; | |
5418 | mddev->ro = 0; | |
5419 | mddev->metadata_type[0] = 0; | |
5420 | mddev->chunk_sectors = 0; | |
5421 | mddev->ctime = mddev->utime = 0; | |
5422 | mddev->layout = 0; | |
5423 | mddev->max_disks = 0; | |
5424 | mddev->events = 0; | |
a8707c08 | 5425 | mddev->can_decrease_events = 0; |
6177b472 | 5426 | mddev->delta_disks = 0; |
2c810cdd | 5427 | mddev->reshape_backwards = 0; |
6177b472 N |
5428 | mddev->new_level = LEVEL_NONE; |
5429 | mddev->new_layout = 0; | |
5430 | mddev->new_chunk_sectors = 0; | |
5431 | mddev->curr_resync = 0; | |
7f7583d4 | 5432 | atomic64_set(&mddev->resync_mismatches, 0); |
6177b472 N |
5433 | mddev->suspend_lo = mddev->suspend_hi = 0; |
5434 | mddev->sync_speed_min = mddev->sync_speed_max = 0; | |
5435 | mddev->recovery = 0; | |
5436 | mddev->in_sync = 0; | |
f0b4f7e2 | 5437 | mddev->changed = 0; |
6177b472 | 5438 | mddev->degraded = 0; |
6177b472 | 5439 | mddev->safemode = 0; |
bd691922 | 5440 | mddev->private = NULL; |
6177b472 N |
5441 | mddev->bitmap_info.offset = 0; |
5442 | mddev->bitmap_info.default_offset = 0; | |
6409bb05 | 5443 | mddev->bitmap_info.default_space = 0; |
6177b472 N |
5444 | mddev->bitmap_info.chunksize = 0; |
5445 | mddev->bitmap_info.daemon_sleep = 0; | |
5446 | mddev->bitmap_info.max_write_behind = 0; | |
5447 | } | |
5448 | ||
fd01b88c | 5449 | static void __md_stop_writes(struct mddev *mddev) |
a047e125 | 5450 | { |
6b6204ee | 5451 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
f851b60d | 5452 | flush_workqueue(md_misc_wq); |
a047e125 | 5453 | if (mddev->sync_thread) { |
a047e125 | 5454 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
a91d5ac0 | 5455 | md_reap_sync_thread(mddev); |
a047e125 N |
5456 | } |
5457 | ||
5458 | del_timer_sync(&mddev->safemode_timer); | |
5459 | ||
5460 | bitmap_flush(mddev); | |
5461 | md_super_wait(mddev); | |
5462 | ||
b6d428c6 | 5463 | if (mddev->ro == 0 && |
28c1b9fd GR |
5464 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
5465 | (mddev->flags & MD_UPDATE_SB_FLAGS))) { | |
a047e125 | 5466 | /* mark array as shutdown cleanly */ |
28c1b9fd GR |
5467 | if (!mddev_is_clustered(mddev)) |
5468 | mddev->in_sync = 1; | |
a047e125 N |
5469 | md_update_sb(mddev, 1); |
5470 | } | |
5471 | } | |
defad61a | 5472 | |
fd01b88c | 5473 | void md_stop_writes(struct mddev *mddev) |
defad61a | 5474 | { |
29f097c4 | 5475 | mddev_lock_nointr(mddev); |
defad61a N |
5476 | __md_stop_writes(mddev); |
5477 | mddev_unlock(mddev); | |
5478 | } | |
390ee602 | 5479 | EXPORT_SYMBOL_GPL(md_stop_writes); |
a047e125 | 5480 | |
5aa61f42 N |
5481 | static void mddev_detach(struct mddev *mddev) |
5482 | { | |
5483 | struct bitmap *bitmap = mddev->bitmap; | |
5484 | /* wait for behind writes to complete */ | |
5485 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { | |
5486 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", | |
5487 | mdname(mddev)); | |
5488 | /* need to kick something here to make sure I/O goes? */ | |
5489 | wait_event(bitmap->behind_wait, | |
5490 | atomic_read(&bitmap->behind_writes) == 0); | |
5491 | } | |
36d091f4 | 5492 | if (mddev->pers && mddev->pers->quiesce) { |
5aa61f42 N |
5493 | mddev->pers->quiesce(mddev, 1); |
5494 | mddev->pers->quiesce(mddev, 0); | |
5495 | } | |
5496 | md_unregister_thread(&mddev->thread); | |
5497 | if (mddev->queue) | |
5498 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ | |
5499 | } | |
5500 | ||
5eff3c43 | 5501 | static void __md_stop(struct mddev *mddev) |
6177b472 | 5502 | { |
36d091f4 | 5503 | struct md_personality *pers = mddev->pers; |
5aa61f42 | 5504 | mddev_detach(mddev); |
ee5d004f N |
5505 | /* Ensure ->event_work is done */ |
5506 | flush_workqueue(md_misc_wq); | |
36d091f4 N |
5507 | spin_lock(&mddev->lock); |
5508 | mddev->ready = 0; | |
6177b472 | 5509 | mddev->pers = NULL; |
36d091f4 N |
5510 | spin_unlock(&mddev->lock); |
5511 | pers->free(mddev, mddev->private); | |
bd691922 | 5512 | mddev->private = NULL; |
36d091f4 N |
5513 | if (pers->sync_request && mddev->to_remove == NULL) |
5514 | mddev->to_remove = &md_redundancy_group; | |
5515 | module_put(pers->owner); | |
cca9cf90 | 5516 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
6177b472 | 5517 | } |
5eff3c43 N |
5518 | |
5519 | void md_stop(struct mddev *mddev) | |
5520 | { | |
5521 | /* stop the array and free an attached data structures. | |
5522 | * This is called from dm-raid | |
5523 | */ | |
5524 | __md_stop(mddev); | |
5525 | bitmap_destroy(mddev); | |
5526 | if (mddev->bio_set) | |
5527 | bioset_free(mddev->bio_set); | |
5528 | } | |
5529 | ||
390ee602 | 5530 | EXPORT_SYMBOL_GPL(md_stop); |
6177b472 | 5531 | |
a05b7ea0 | 5532 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
a4bd82d0 N |
5533 | { |
5534 | int err = 0; | |
30b8feb7 N |
5535 | int did_freeze = 0; |
5536 | ||
5537 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { | |
5538 | did_freeze = 1; | |
5539 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
5540 | md_wakeup_thread(mddev->thread); | |
5541 | } | |
f851b60d | 5542 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
30b8feb7 | 5543 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
f851b60d | 5544 | if (mddev->sync_thread) |
30b8feb7 N |
5545 | /* Thread might be blocked waiting for metadata update |
5546 | * which will now never happen */ | |
5547 | wake_up_process(mddev->sync_thread->tsk); | |
f851b60d | 5548 | |
88724bfa N |
5549 | if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
5550 | return -EBUSY; | |
30b8feb7 | 5551 | mddev_unlock(mddev); |
f851b60d N |
5552 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
5553 | &mddev->recovery)); | |
88724bfa N |
5554 | wait_event(mddev->sb_wait, |
5555 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); | |
30b8feb7 N |
5556 | mddev_lock_nointr(mddev); |
5557 | ||
a4bd82d0 | 5558 | mutex_lock(&mddev->open_mutex); |
9ba3b7f5 | 5559 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
30b8feb7 | 5560 | mddev->sync_thread || |
f851b60d | 5561 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
30b8feb7 | 5562 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
a4bd82d0 | 5563 | printk("md: %s still in use.\n",mdname(mddev)); |
30b8feb7 N |
5564 | if (did_freeze) { |
5565 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
45eaf45d | 5566 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
30b8feb7 N |
5567 | md_wakeup_thread(mddev->thread); |
5568 | } | |
a4bd82d0 N |
5569 | err = -EBUSY; |
5570 | goto out; | |
5571 | } | |
5572 | if (mddev->pers) { | |
defad61a | 5573 | __md_stop_writes(mddev); |
a4bd82d0 N |
5574 | |
5575 | err = -ENXIO; | |
5576 | if (mddev->ro==1) | |
5577 | goto out; | |
5578 | mddev->ro = 1; | |
5579 | set_disk_ro(mddev->gendisk, 1); | |
5580 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
45eaf45d N |
5581 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
5582 | md_wakeup_thread(mddev->thread); | |
00bcb4ac | 5583 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
30b8feb7 | 5584 | err = 0; |
a4bd82d0 N |
5585 | } |
5586 | out: | |
5587 | mutex_unlock(&mddev->open_mutex); | |
5588 | return err; | |
5589 | } | |
5590 | ||
9e653b63 N |
5591 | /* mode: |
5592 | * 0 - completely stop and dis-assemble array | |
9e653b63 N |
5593 | * 2 - stop but do not disassemble array |
5594 | */ | |
f72ffdd6 | 5595 | static int do_md_stop(struct mddev *mddev, int mode, |
a05b7ea0 | 5596 | struct block_device *bdev) |
1da177e4 | 5597 | { |
1da177e4 | 5598 | struct gendisk *disk = mddev->gendisk; |
3cb03002 | 5599 | struct md_rdev *rdev; |
30b8feb7 N |
5600 | int did_freeze = 0; |
5601 | ||
5602 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { | |
5603 | did_freeze = 1; | |
5604 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
5605 | md_wakeup_thread(mddev->thread); | |
5606 | } | |
f851b60d | 5607 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
30b8feb7 | 5608 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
f851b60d | 5609 | if (mddev->sync_thread) |
30b8feb7 N |
5610 | /* Thread might be blocked waiting for metadata update |
5611 | * which will now never happen */ | |
5612 | wake_up_process(mddev->sync_thread->tsk); | |
f851b60d | 5613 | |
30b8feb7 | 5614 | mddev_unlock(mddev); |
f851b60d N |
5615 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
5616 | !test_bit(MD_RECOVERY_RUNNING, | |
5617 | &mddev->recovery))); | |
30b8feb7 | 5618 | mddev_lock_nointr(mddev); |
1da177e4 | 5619 | |
c8c00a69 | 5620 | mutex_lock(&mddev->open_mutex); |
9ba3b7f5 | 5621 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
30b8feb7 N |
5622 | mddev->sysfs_active || |
5623 | mddev->sync_thread || | |
f851b60d | 5624 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
30b8feb7 | 5625 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
df5b20cf | 5626 | printk("md: %s still in use.\n",mdname(mddev)); |
6e17b027 | 5627 | mutex_unlock(&mddev->open_mutex); |
30b8feb7 N |
5628 | if (did_freeze) { |
5629 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
45eaf45d | 5630 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
30b8feb7 N |
5631 | md_wakeup_thread(mddev->thread); |
5632 | } | |
260fa034 N |
5633 | return -EBUSY; |
5634 | } | |
6e17b027 | 5635 | if (mddev->pers) { |
a4bd82d0 N |
5636 | if (mddev->ro) |
5637 | set_disk_ro(disk, 0); | |
409c57f3 | 5638 | |
defad61a | 5639 | __md_stop_writes(mddev); |
5eff3c43 | 5640 | __md_stop(mddev); |
a4bd82d0 | 5641 | mddev->queue->backing_dev_info.congested_fn = NULL; |
6177b472 | 5642 | |
a4bd82d0 | 5643 | /* tell userspace to handle 'inactive' */ |
00bcb4ac | 5644 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0d4ca600 | 5645 | |
dafb20fa | 5646 | rdev_for_each(rdev, mddev) |
36fad858 NK |
5647 | if (rdev->raid_disk >= 0) |
5648 | sysfs_unlink_rdev(mddev, rdev); | |
c4647292 | 5649 | |
a4bd82d0 | 5650 | set_capacity(disk, 0); |
6e17b027 | 5651 | mutex_unlock(&mddev->open_mutex); |
f0b4f7e2 | 5652 | mddev->changed = 1; |
a4bd82d0 | 5653 | revalidate_disk(disk); |
0d4ca600 | 5654 | |
a4bd82d0 N |
5655 | if (mddev->ro) |
5656 | mddev->ro = 0; | |
6e17b027 N |
5657 | } else |
5658 | mutex_unlock(&mddev->open_mutex); | |
1da177e4 LT |
5659 | /* |
5660 | * Free resources if final stop | |
5661 | */ | |
9e653b63 | 5662 | if (mode == 0) { |
1da177e4 LT |
5663 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
5664 | ||
978f946b | 5665 | bitmap_destroy(mddev); |
c3d9714e | 5666 | if (mddev->bitmap_info.file) { |
4af1a041 N |
5667 | struct file *f = mddev->bitmap_info.file; |
5668 | spin_lock(&mddev->lock); | |
c3d9714e | 5669 | mddev->bitmap_info.file = NULL; |
4af1a041 N |
5670 | spin_unlock(&mddev->lock); |
5671 | fput(f); | |
978f946b | 5672 | } |
c3d9714e | 5673 | mddev->bitmap_info.offset = 0; |
978f946b | 5674 | |
1da177e4 LT |
5675 | export_array(mddev); |
5676 | ||
6177b472 | 5677 | md_clean(mddev); |
934d9c23 | 5678 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
efeb53c0 N |
5679 | if (mddev->hold_active == UNTIL_STOP) |
5680 | mddev->hold_active = 0; | |
a4bd82d0 | 5681 | } |
d7603b7e | 5682 | md_new_event(mddev); |
00bcb4ac | 5683 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
6e17b027 | 5684 | return 0; |
1da177e4 LT |
5685 | } |
5686 | ||
fdee8ae4 | 5687 | #ifndef MODULE |
fd01b88c | 5688 | static void autorun_array(struct mddev *mddev) |
1da177e4 | 5689 | { |
3cb03002 | 5690 | struct md_rdev *rdev; |
1da177e4 LT |
5691 | int err; |
5692 | ||
a757e64c | 5693 | if (list_empty(&mddev->disks)) |
1da177e4 | 5694 | return; |
1da177e4 LT |
5695 | |
5696 | printk(KERN_INFO "md: running: "); | |
5697 | ||
dafb20fa | 5698 | rdev_for_each(rdev, mddev) { |
1da177e4 LT |
5699 | char b[BDEVNAME_SIZE]; |
5700 | printk("<%s>", bdevname(rdev->bdev,b)); | |
5701 | } | |
5702 | printk("\n"); | |
5703 | ||
d710e138 | 5704 | err = do_md_run(mddev); |
1da177e4 LT |
5705 | if (err) { |
5706 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); | |
a05b7ea0 | 5707 | do_md_stop(mddev, 0, NULL); |
1da177e4 LT |
5708 | } |
5709 | } | |
5710 | ||
5711 | /* | |
5712 | * lets try to run arrays based on all disks that have arrived | |
5713 | * until now. (those are in pending_raid_disks) | |
5714 | * | |
5715 | * the method: pick the first pending disk, collect all disks with | |
5716 | * the same UUID, remove all from the pending list and put them into | |
5717 | * the 'same_array' list. Then order this list based on superblock | |
5718 | * update time (freshest comes first), kick out 'old' disks and | |
5719 | * compare superblocks. If everything's fine then run it. | |
5720 | * | |
5721 | * If "unit" is allocated, then bump its reference count | |
5722 | */ | |
5723 | static void autorun_devices(int part) | |
5724 | { | |
3cb03002 | 5725 | struct md_rdev *rdev0, *rdev, *tmp; |
fd01b88c | 5726 | struct mddev *mddev; |
1da177e4 LT |
5727 | char b[BDEVNAME_SIZE]; |
5728 | ||
5729 | printk(KERN_INFO "md: autorun ...\n"); | |
5730 | while (!list_empty(&pending_raid_disks)) { | |
e8703fe1 | 5731 | int unit; |
1da177e4 | 5732 | dev_t dev; |
ad01c9e3 | 5733 | LIST_HEAD(candidates); |
1da177e4 | 5734 | rdev0 = list_entry(pending_raid_disks.next, |
3cb03002 | 5735 | struct md_rdev, same_set); |
1da177e4 LT |
5736 | |
5737 | printk(KERN_INFO "md: considering %s ...\n", | |
5738 | bdevname(rdev0->bdev,b)); | |
5739 | INIT_LIST_HEAD(&candidates); | |
159ec1fc | 5740 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
1da177e4 LT |
5741 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
5742 | printk(KERN_INFO "md: adding %s ...\n", | |
5743 | bdevname(rdev->bdev,b)); | |
5744 | list_move(&rdev->same_set, &candidates); | |
5745 | } | |
5746 | /* | |
5747 | * now we have a set of devices, with all of them having | |
5748 | * mostly sane superblocks. It's time to allocate the | |
5749 | * mddev. | |
5750 | */ | |
e8703fe1 N |
5751 | if (part) { |
5752 | dev = MKDEV(mdp_major, | |
5753 | rdev0->preferred_minor << MdpMinorShift); | |
5754 | unit = MINOR(dev) >> MdpMinorShift; | |
5755 | } else { | |
5756 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); | |
5757 | unit = MINOR(dev); | |
5758 | } | |
5759 | if (rdev0->preferred_minor != unit) { | |
1da177e4 LT |
5760 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
5761 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); | |
5762 | break; | |
5763 | } | |
1da177e4 LT |
5764 | |
5765 | md_probe(dev, NULL, NULL); | |
5766 | mddev = mddev_find(dev); | |
9bbbca3a NB |
5767 | if (!mddev || !mddev->gendisk) { |
5768 | if (mddev) | |
5769 | mddev_put(mddev); | |
5770 | printk(KERN_ERR | |
1da177e4 LT |
5771 | "md: cannot allocate memory for md drive.\n"); |
5772 | break; | |
5773 | } | |
f72ffdd6 | 5774 | if (mddev_lock(mddev)) |
1da177e4 LT |
5775 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
5776 | mdname(mddev)); | |
5777 | else if (mddev->raid_disks || mddev->major_version | |
5778 | || !list_empty(&mddev->disks)) { | |
f72ffdd6 | 5779 | printk(KERN_WARNING |
1da177e4 LT |
5780 | "md: %s already running, cannot run %s\n", |
5781 | mdname(mddev), bdevname(rdev0->bdev,b)); | |
5782 | mddev_unlock(mddev); | |
5783 | } else { | |
5784 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); | |
1ec4a939 | 5785 | mddev->persistent = 1; |
159ec1fc | 5786 | rdev_for_each_list(rdev, tmp, &candidates) { |
1da177e4 LT |
5787 | list_del_init(&rdev->same_set); |
5788 | if (bind_rdev_to_array(rdev, mddev)) | |
5789 | export_rdev(rdev); | |
5790 | } | |
5791 | autorun_array(mddev); | |
5792 | mddev_unlock(mddev); | |
5793 | } | |
5794 | /* on success, candidates will be empty, on error | |
5795 | * it won't... | |
5796 | */ | |
159ec1fc | 5797 | rdev_for_each_list(rdev, tmp, &candidates) { |
4b80991c | 5798 | list_del_init(&rdev->same_set); |
1da177e4 | 5799 | export_rdev(rdev); |
4b80991c | 5800 | } |
1da177e4 LT |
5801 | mddev_put(mddev); |
5802 | } | |
5803 | printk(KERN_INFO "md: ... autorun DONE.\n"); | |
5804 | } | |
fdee8ae4 | 5805 | #endif /* !MODULE */ |
1da177e4 | 5806 | |
f72ffdd6 | 5807 | static int get_version(void __user *arg) |
1da177e4 LT |
5808 | { |
5809 | mdu_version_t ver; | |
5810 | ||
5811 | ver.major = MD_MAJOR_VERSION; | |
5812 | ver.minor = MD_MINOR_VERSION; | |
5813 | ver.patchlevel = MD_PATCHLEVEL_VERSION; | |
5814 | ||
5815 | if (copy_to_user(arg, &ver, sizeof(ver))) | |
5816 | return -EFAULT; | |
5817 | ||
5818 | return 0; | |
5819 | } | |
5820 | ||
f72ffdd6 | 5821 | static int get_array_info(struct mddev *mddev, void __user *arg) |
1da177e4 LT |
5822 | { |
5823 | mdu_array_info_t info; | |
a9f326eb | 5824 | int nr,working,insync,failed,spare; |
3cb03002 | 5825 | struct md_rdev *rdev; |
1da177e4 | 5826 | |
1ca69c4b N |
5827 | nr = working = insync = failed = spare = 0; |
5828 | rcu_read_lock(); | |
5829 | rdev_for_each_rcu(rdev, mddev) { | |
1da177e4 | 5830 | nr++; |
b2d444d7 | 5831 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 LT |
5832 | failed++; |
5833 | else { | |
5834 | working++; | |
b2d444d7 | 5835 | if (test_bit(In_sync, &rdev->flags)) |
f72ffdd6 | 5836 | insync++; |
1da177e4 LT |
5837 | else |
5838 | spare++; | |
5839 | } | |
5840 | } | |
1ca69c4b | 5841 | rcu_read_unlock(); |
1da177e4 LT |
5842 | |
5843 | info.major_version = mddev->major_version; | |
5844 | info.minor_version = mddev->minor_version; | |
5845 | info.patch_version = MD_PATCHLEVEL_VERSION; | |
9ebc6ef1 | 5846 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
1da177e4 | 5847 | info.level = mddev->level; |
58c0fed4 AN |
5848 | info.size = mddev->dev_sectors / 2; |
5849 | if (info.size != mddev->dev_sectors / 2) /* overflow */ | |
284ae7ca | 5850 | info.size = -1; |
1da177e4 LT |
5851 | info.nr_disks = nr; |
5852 | info.raid_disks = mddev->raid_disks; | |
5853 | info.md_minor = mddev->md_minor; | |
5854 | info.not_persistent= !mddev->persistent; | |
5855 | ||
9ebc6ef1 | 5856 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
1da177e4 LT |
5857 | info.state = 0; |
5858 | if (mddev->in_sync) | |
5859 | info.state = (1<<MD_SB_CLEAN); | |
c3d9714e | 5860 | if (mddev->bitmap && mddev->bitmap_info.offset) |
9bd35920 | 5861 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
ca8895d9 GR |
5862 | if (mddev_is_clustered(mddev)) |
5863 | info.state |= (1<<MD_SB_CLUSTERED); | |
a9f326eb | 5864 | info.active_disks = insync; |
1da177e4 LT |
5865 | info.working_disks = working; |
5866 | info.failed_disks = failed; | |
5867 | info.spare_disks = spare; | |
5868 | ||
5869 | info.layout = mddev->layout; | |
9d8f0363 | 5870 | info.chunk_size = mddev->chunk_sectors << 9; |
1da177e4 LT |
5871 | |
5872 | if (copy_to_user(arg, &info, sizeof(info))) | |
5873 | return -EFAULT; | |
5874 | ||
5875 | return 0; | |
5876 | } | |
5877 | ||
f72ffdd6 | 5878 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
32a7627c N |
5879 | { |
5880 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ | |
f4ad3d38 | 5881 | char *ptr; |
4af1a041 | 5882 | int err; |
32a7627c | 5883 | |
b6878d9e | 5884 | file = kzalloc(sizeof(*file), GFP_NOIO); |
32a7627c | 5885 | if (!file) |
4af1a041 | 5886 | return -ENOMEM; |
32a7627c | 5887 | |
4af1a041 N |
5888 | err = 0; |
5889 | spin_lock(&mddev->lock); | |
25eafe1a BR |
5890 | /* bitmap enabled */ |
5891 | if (mddev->bitmap_info.file) { | |
5892 | ptr = file_path(mddev->bitmap_info.file, file->pathname, | |
5893 | sizeof(file->pathname)); | |
5894 | if (IS_ERR(ptr)) | |
5895 | err = PTR_ERR(ptr); | |
5896 | else | |
5897 | memmove(file->pathname, ptr, | |
5898 | sizeof(file->pathname)-(ptr-file->pathname)); | |
5899 | } | |
4af1a041 | 5900 | spin_unlock(&mddev->lock); |
32a7627c | 5901 | |
4af1a041 N |
5902 | if (err == 0 && |
5903 | copy_to_user(arg, file, sizeof(*file))) | |
32a7627c | 5904 | err = -EFAULT; |
4af1a041 | 5905 | |
32a7627c N |
5906 | kfree(file); |
5907 | return err; | |
5908 | } | |
5909 | ||
f72ffdd6 | 5910 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
1da177e4 LT |
5911 | { |
5912 | mdu_disk_info_t info; | |
3cb03002 | 5913 | struct md_rdev *rdev; |
1da177e4 LT |
5914 | |
5915 | if (copy_from_user(&info, arg, sizeof(info))) | |
5916 | return -EFAULT; | |
5917 | ||
1ca69c4b | 5918 | rcu_read_lock(); |
57d051dc | 5919 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
1da177e4 LT |
5920 | if (rdev) { |
5921 | info.major = MAJOR(rdev->bdev->bd_dev); | |
5922 | info.minor = MINOR(rdev->bdev->bd_dev); | |
5923 | info.raid_disk = rdev->raid_disk; | |
5924 | info.state = 0; | |
b2d444d7 | 5925 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 | 5926 | info.state |= (1<<MD_DISK_FAULTY); |
b2d444d7 | 5927 | else if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 LT |
5928 | info.state |= (1<<MD_DISK_ACTIVE); |
5929 | info.state |= (1<<MD_DISK_SYNC); | |
5930 | } | |
9efdca16 | 5931 | if (test_bit(Journal, &rdev->flags)) |
bac624f3 | 5932 | info.state |= (1<<MD_DISK_JOURNAL); |
8ddf9efe N |
5933 | if (test_bit(WriteMostly, &rdev->flags)) |
5934 | info.state |= (1<<MD_DISK_WRITEMOSTLY); | |
1da177e4 LT |
5935 | } else { |
5936 | info.major = info.minor = 0; | |
5937 | info.raid_disk = -1; | |
5938 | info.state = (1<<MD_DISK_REMOVED); | |
5939 | } | |
1ca69c4b | 5940 | rcu_read_unlock(); |
1da177e4 LT |
5941 | |
5942 | if (copy_to_user(arg, &info, sizeof(info))) | |
5943 | return -EFAULT; | |
5944 | ||
5945 | return 0; | |
5946 | } | |
5947 | ||
f72ffdd6 | 5948 | static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info) |
1da177e4 LT |
5949 | { |
5950 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
3cb03002 | 5951 | struct md_rdev *rdev; |
1da177e4 LT |
5952 | dev_t dev = MKDEV(info->major,info->minor); |
5953 | ||
1aee41f6 GR |
5954 | if (mddev_is_clustered(mddev) && |
5955 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { | |
fa8259da | 5956 | pr_err("%s: Cannot add to clustered mddev.\n", |
1aee41f6 GR |
5957 | mdname(mddev)); |
5958 | return -EINVAL; | |
5959 | } | |
5960 | ||
1da177e4 LT |
5961 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
5962 | return -EOVERFLOW; | |
5963 | ||
5964 | if (!mddev->raid_disks) { | |
5965 | int err; | |
5966 | /* expecting a device which has a superblock */ | |
5967 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); | |
5968 | if (IS_ERR(rdev)) { | |
f72ffdd6 | 5969 | printk(KERN_WARNING |
1da177e4 LT |
5970 | "md: md_import_device returned %ld\n", |
5971 | PTR_ERR(rdev)); | |
5972 | return PTR_ERR(rdev); | |
5973 | } | |
5974 | if (!list_empty(&mddev->disks)) { | |
3cb03002 N |
5975 | struct md_rdev *rdev0 |
5976 | = list_entry(mddev->disks.next, | |
5977 | struct md_rdev, same_set); | |
a9f326eb | 5978 | err = super_types[mddev->major_version] |
1da177e4 LT |
5979 | .load_super(rdev, rdev0, mddev->minor_version); |
5980 | if (err < 0) { | |
f72ffdd6 | 5981 | printk(KERN_WARNING |
1da177e4 | 5982 | "md: %s has different UUID to %s\n", |
f72ffdd6 | 5983 | bdevname(rdev->bdev,b), |
1da177e4 LT |
5984 | bdevname(rdev0->bdev,b2)); |
5985 | export_rdev(rdev); | |
5986 | return -EINVAL; | |
5987 | } | |
5988 | } | |
5989 | err = bind_rdev_to_array(rdev, mddev); | |
5990 | if (err) | |
5991 | export_rdev(rdev); | |
5992 | return err; | |
5993 | } | |
5994 | ||
5995 | /* | |
5996 | * add_new_disk can be used once the array is assembled | |
5997 | * to add "hot spares". They must already have a superblock | |
5998 | * written | |
5999 | */ | |
6000 | if (mddev->pers) { | |
6001 | int err; | |
6002 | if (!mddev->pers->hot_add_disk) { | |
f72ffdd6 | 6003 | printk(KERN_WARNING |
1da177e4 LT |
6004 | "%s: personality does not support diskops!\n", |
6005 | mdname(mddev)); | |
6006 | return -EINVAL; | |
6007 | } | |
7b1e35f6 N |
6008 | if (mddev->persistent) |
6009 | rdev = md_import_device(dev, mddev->major_version, | |
6010 | mddev->minor_version); | |
6011 | else | |
6012 | rdev = md_import_device(dev, -1, -1); | |
1da177e4 | 6013 | if (IS_ERR(rdev)) { |
f72ffdd6 | 6014 | printk(KERN_WARNING |
1da177e4 LT |
6015 | "md: md_import_device returned %ld\n", |
6016 | PTR_ERR(rdev)); | |
6017 | return PTR_ERR(rdev); | |
6018 | } | |
1a855a06 | 6019 | /* set saved_raid_disk if appropriate */ |
41158c7e N |
6020 | if (!mddev->persistent) { |
6021 | if (info->state & (1<<MD_DISK_SYNC) && | |
bf572541 | 6022 | info->raid_disk < mddev->raid_disks) { |
41158c7e | 6023 | rdev->raid_disk = info->raid_disk; |
bf572541 | 6024 | set_bit(In_sync, &rdev->flags); |
8313b8e5 | 6025 | clear_bit(Bitmap_sync, &rdev->flags); |
bf572541 | 6026 | } else |
41158c7e | 6027 | rdev->raid_disk = -1; |
f466722c | 6028 | rdev->saved_raid_disk = rdev->raid_disk; |
41158c7e N |
6029 | } else |
6030 | super_types[mddev->major_version]. | |
6031 | validate_super(mddev, rdev); | |
bedd86b7 | 6032 | if ((info->state & (1<<MD_DISK_SYNC)) && |
f4563091 | 6033 | rdev->raid_disk != info->raid_disk) { |
bedd86b7 N |
6034 | /* This was a hot-add request, but events doesn't |
6035 | * match, so reject it. | |
6036 | */ | |
6037 | export_rdev(rdev); | |
6038 | return -EINVAL; | |
6039 | } | |
6040 | ||
b2d444d7 | 6041 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
8ddf9efe N |
6042 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
6043 | set_bit(WriteMostly, &rdev->flags); | |
575a80fa N |
6044 | else |
6045 | clear_bit(WriteMostly, &rdev->flags); | |
8ddf9efe | 6046 | |
bac624f3 SL |
6047 | if (info->state & (1<<MD_DISK_JOURNAL)) |
6048 | set_bit(Journal, &rdev->flags); | |
1aee41f6 GR |
6049 | /* |
6050 | * check whether the device shows up in other nodes | |
6051 | */ | |
6052 | if (mddev_is_clustered(mddev)) { | |
dbb64f86 | 6053 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
1aee41f6 | 6054 | set_bit(Candidate, &rdev->flags); |
dbb64f86 | 6055 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
1aee41f6 | 6056 | /* --add initiated by this node */ |
dbb64f86 | 6057 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
1aee41f6 | 6058 | if (err) { |
1aee41f6 GR |
6059 | export_rdev(rdev); |
6060 | return err; | |
6061 | } | |
6062 | } | |
6063 | } | |
6064 | ||
1da177e4 LT |
6065 | rdev->raid_disk = -1; |
6066 | err = bind_rdev_to_array(rdev, mddev); | |
dbb64f86 | 6067 | |
1da177e4 LT |
6068 | if (err) |
6069 | export_rdev(rdev); | |
dbb64f86 GR |
6070 | |
6071 | if (mddev_is_clustered(mddev)) { | |
6072 | if (info->state & (1 << MD_DISK_CANDIDATE)) | |
6073 | md_cluster_ops->new_disk_ack(mddev, (err == 0)); | |
6074 | else { | |
6075 | if (err) | |
6076 | md_cluster_ops->add_new_disk_cancel(mddev); | |
6077 | else | |
6078 | err = add_bound_rdev(rdev); | |
6079 | } | |
6080 | ||
6081 | } else if (!err) | |
a6da4ef8 | 6082 | err = add_bound_rdev(rdev); |
dbb64f86 | 6083 | |
1da177e4 LT |
6084 | return err; |
6085 | } | |
6086 | ||
6087 | /* otherwise, add_new_disk is only allowed | |
6088 | * for major_version==0 superblocks | |
6089 | */ | |
6090 | if (mddev->major_version != 0) { | |
6091 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", | |
6092 | mdname(mddev)); | |
6093 | return -EINVAL; | |
6094 | } | |
6095 | ||
6096 | if (!(info->state & (1<<MD_DISK_FAULTY))) { | |
6097 | int err; | |
d710e138 | 6098 | rdev = md_import_device(dev, -1, 0); |
1da177e4 | 6099 | if (IS_ERR(rdev)) { |
f72ffdd6 | 6100 | printk(KERN_WARNING |
1da177e4 LT |
6101 | "md: error, md_import_device() returned %ld\n", |
6102 | PTR_ERR(rdev)); | |
6103 | return PTR_ERR(rdev); | |
6104 | } | |
6105 | rdev->desc_nr = info->number; | |
6106 | if (info->raid_disk < mddev->raid_disks) | |
6107 | rdev->raid_disk = info->raid_disk; | |
6108 | else | |
6109 | rdev->raid_disk = -1; | |
6110 | ||
1da177e4 | 6111 | if (rdev->raid_disk < mddev->raid_disks) |
b2d444d7 N |
6112 | if (info->state & (1<<MD_DISK_SYNC)) |
6113 | set_bit(In_sync, &rdev->flags); | |
1da177e4 | 6114 | |
8ddf9efe N |
6115 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
6116 | set_bit(WriteMostly, &rdev->flags); | |
6117 | ||
1da177e4 LT |
6118 | if (!mddev->persistent) { |
6119 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); | |
77304d2a MS |
6120 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
6121 | } else | |
57b2caa3 | 6122 | rdev->sb_start = calc_dev_sboffset(rdev); |
8190e754 | 6123 | rdev->sectors = rdev->sb_start; |
1da177e4 | 6124 | |
2bf071bf N |
6125 | err = bind_rdev_to_array(rdev, mddev); |
6126 | if (err) { | |
6127 | export_rdev(rdev); | |
6128 | return err; | |
6129 | } | |
1da177e4 LT |
6130 | } |
6131 | ||
6132 | return 0; | |
6133 | } | |
6134 | ||
f72ffdd6 | 6135 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
1da177e4 LT |
6136 | { |
6137 | char b[BDEVNAME_SIZE]; | |
3cb03002 | 6138 | struct md_rdev *rdev; |
1da177e4 | 6139 | |
1da177e4 LT |
6140 | rdev = find_rdev(mddev, dev); |
6141 | if (!rdev) | |
6142 | return -ENXIO; | |
6143 | ||
2910ff17 GR |
6144 | if (rdev->raid_disk < 0) |
6145 | goto kick_rdev; | |
293467aa | 6146 | |
3ea8929d N |
6147 | clear_bit(Blocked, &rdev->flags); |
6148 | remove_and_add_spares(mddev, rdev); | |
6149 | ||
1da177e4 LT |
6150 | if (rdev->raid_disk >= 0) |
6151 | goto busy; | |
6152 | ||
2910ff17 | 6153 | kick_rdev: |
54a88392 | 6154 | if (mddev_is_clustered(mddev)) |
88bcfef7 GR |
6155 | md_cluster_ops->remove_disk(mddev, rdev); |
6156 | ||
fb56dfef | 6157 | md_kick_rdev_from_array(rdev); |
850b2b42 | 6158 | md_update_sb(mddev, 1); |
d7603b7e | 6159 | md_new_event(mddev); |
1da177e4 LT |
6160 | |
6161 | return 0; | |
6162 | busy: | |
fdefa4d8 | 6163 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
1da177e4 LT |
6164 | bdevname(rdev->bdev,b), mdname(mddev)); |
6165 | return -EBUSY; | |
6166 | } | |
6167 | ||
f72ffdd6 | 6168 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
1da177e4 LT |
6169 | { |
6170 | char b[BDEVNAME_SIZE]; | |
6171 | int err; | |
3cb03002 | 6172 | struct md_rdev *rdev; |
1da177e4 LT |
6173 | |
6174 | if (!mddev->pers) | |
6175 | return -ENODEV; | |
6176 | ||
6177 | if (mddev->major_version != 0) { | |
6178 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" | |
6179 | " version-0 superblocks.\n", | |
6180 | mdname(mddev)); | |
6181 | return -EINVAL; | |
6182 | } | |
6183 | if (!mddev->pers->hot_add_disk) { | |
f72ffdd6 | 6184 | printk(KERN_WARNING |
1da177e4 LT |
6185 | "%s: personality does not support diskops!\n", |
6186 | mdname(mddev)); | |
6187 | return -EINVAL; | |
6188 | } | |
6189 | ||
d710e138 | 6190 | rdev = md_import_device(dev, -1, 0); |
1da177e4 | 6191 | if (IS_ERR(rdev)) { |
f72ffdd6 | 6192 | printk(KERN_WARNING |
1da177e4 LT |
6193 | "md: error, md_import_device() returned %ld\n", |
6194 | PTR_ERR(rdev)); | |
6195 | return -EINVAL; | |
6196 | } | |
6197 | ||
6198 | if (mddev->persistent) | |
57b2caa3 | 6199 | rdev->sb_start = calc_dev_sboffset(rdev); |
1da177e4 | 6200 | else |
77304d2a | 6201 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
1da177e4 | 6202 | |
8190e754 | 6203 | rdev->sectors = rdev->sb_start; |
1da177e4 | 6204 | |
b2d444d7 | 6205 | if (test_bit(Faulty, &rdev->flags)) { |
f72ffdd6 | 6206 | printk(KERN_WARNING |
1da177e4 LT |
6207 | "md: can not hot-add faulty %s disk to %s!\n", |
6208 | bdevname(rdev->bdev,b), mdname(mddev)); | |
6209 | err = -EINVAL; | |
6210 | goto abort_export; | |
6211 | } | |
293467aa | 6212 | |
b2d444d7 | 6213 | clear_bit(In_sync, &rdev->flags); |
1da177e4 | 6214 | rdev->desc_nr = -1; |
5842730d | 6215 | rdev->saved_raid_disk = -1; |
2bf071bf N |
6216 | err = bind_rdev_to_array(rdev, mddev); |
6217 | if (err) | |
2aa82191 | 6218 | goto abort_export; |
1da177e4 LT |
6219 | |
6220 | /* | |
6221 | * The rest should better be atomic, we can have disk failures | |
6222 | * noticed in interrupt contexts ... | |
6223 | */ | |
6224 | ||
1da177e4 LT |
6225 | rdev->raid_disk = -1; |
6226 | ||
850b2b42 | 6227 | md_update_sb(mddev, 1); |
1da177e4 LT |
6228 | /* |
6229 | * Kick recovery, maybe this spare has to be added to the | |
6230 | * array immediately. | |
6231 | */ | |
6232 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
6233 | md_wakeup_thread(mddev->thread); | |
d7603b7e | 6234 | md_new_event(mddev); |
1da177e4 LT |
6235 | return 0; |
6236 | ||
1da177e4 LT |
6237 | abort_export: |
6238 | export_rdev(rdev); | |
6239 | return err; | |
6240 | } | |
6241 | ||
fd01b88c | 6242 | static int set_bitmap_file(struct mddev *mddev, int fd) |
32a7627c | 6243 | { |
035328c2 | 6244 | int err = 0; |
32a7627c | 6245 | |
36fa3063 | 6246 | if (mddev->pers) { |
d66b1b39 | 6247 | if (!mddev->pers->quiesce || !mddev->thread) |
36fa3063 N |
6248 | return -EBUSY; |
6249 | if (mddev->recovery || mddev->sync_thread) | |
6250 | return -EBUSY; | |
6251 | /* we should be able to change the bitmap.. */ | |
6252 | } | |
32a7627c | 6253 | |
36fa3063 | 6254 | if (fd >= 0) { |
035328c2 | 6255 | struct inode *inode; |
1e594bb2 N |
6256 | struct file *f; |
6257 | ||
6258 | if (mddev->bitmap || mddev->bitmap_info.file) | |
36fa3063 | 6259 | return -EEXIST; /* cannot add when bitmap is present */ |
1e594bb2 | 6260 | f = fget(fd); |
32a7627c | 6261 | |
1e594bb2 | 6262 | if (f == NULL) { |
36fa3063 N |
6263 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
6264 | mdname(mddev)); | |
6265 | return -EBADF; | |
6266 | } | |
6267 | ||
1e594bb2 | 6268 | inode = f->f_mapping->host; |
035328c2 N |
6269 | if (!S_ISREG(inode->i_mode)) { |
6270 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", | |
6271 | mdname(mddev)); | |
6272 | err = -EBADF; | |
1e594bb2 | 6273 | } else if (!(f->f_mode & FMODE_WRITE)) { |
035328c2 N |
6274 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
6275 | mdname(mddev)); | |
6276 | err = -EBADF; | |
6277 | } else if (atomic_read(&inode->i_writecount) != 1) { | |
36fa3063 N |
6278 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
6279 | mdname(mddev)); | |
035328c2 N |
6280 | err = -EBUSY; |
6281 | } | |
6282 | if (err) { | |
1e594bb2 | 6283 | fput(f); |
36fa3063 N |
6284 | return err; |
6285 | } | |
1e594bb2 | 6286 | mddev->bitmap_info.file = f; |
c3d9714e | 6287 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
36fa3063 N |
6288 | } else if (mddev->bitmap == NULL) |
6289 | return -ENOENT; /* cannot remove what isn't there */ | |
6290 | err = 0; | |
6291 | if (mddev->pers) { | |
6292 | mddev->pers->quiesce(mddev, 1); | |
69e51b44 | 6293 | if (fd >= 0) { |
f9209a32 GR |
6294 | struct bitmap *bitmap; |
6295 | ||
6296 | bitmap = bitmap_create(mddev, -1); | |
6297 | if (!IS_ERR(bitmap)) { | |
6298 | mddev->bitmap = bitmap; | |
69e51b44 | 6299 | err = bitmap_load(mddev); |
ba599aca N |
6300 | } else |
6301 | err = PTR_ERR(bitmap); | |
69e51b44 | 6302 | } |
d7375ab3 | 6303 | if (fd < 0 || err) { |
36fa3063 | 6304 | bitmap_destroy(mddev); |
d7375ab3 N |
6305 | fd = -1; /* make sure to put the file */ |
6306 | } | |
36fa3063 | 6307 | mddev->pers->quiesce(mddev, 0); |
d7375ab3 N |
6308 | } |
6309 | if (fd < 0) { | |
4af1a041 N |
6310 | struct file *f = mddev->bitmap_info.file; |
6311 | if (f) { | |
6312 | spin_lock(&mddev->lock); | |
6313 | mddev->bitmap_info.file = NULL; | |
6314 | spin_unlock(&mddev->lock); | |
6315 | fput(f); | |
6316 | } | |
36fa3063 N |
6317 | } |
6318 | ||
32a7627c N |
6319 | return err; |
6320 | } | |
6321 | ||
1da177e4 LT |
6322 | /* |
6323 | * set_array_info is used two different ways | |
6324 | * The original usage is when creating a new array. | |
6325 | * In this usage, raid_disks is > 0 and it together with | |
6326 | * level, size, not_persistent,layout,chunksize determine the | |
6327 | * shape of the array. | |
6328 | * This will always create an array with a type-0.90.0 superblock. | |
6329 | * The newer usage is when assembling an array. | |
6330 | * In this case raid_disks will be 0, and the major_version field is | |
6331 | * use to determine which style super-blocks are to be found on the devices. | |
6332 | * The minor and patch _version numbers are also kept incase the | |
6333 | * super_block handler wishes to interpret them. | |
6334 | */ | |
f72ffdd6 | 6335 | static int set_array_info(struct mddev *mddev, mdu_array_info_t *info) |
1da177e4 LT |
6336 | { |
6337 | ||
6338 | if (info->raid_disks == 0) { | |
6339 | /* just setting version number for superblock loading */ | |
6340 | if (info->major_version < 0 || | |
50511da3 | 6341 | info->major_version >= ARRAY_SIZE(super_types) || |
1da177e4 LT |
6342 | super_types[info->major_version].name == NULL) { |
6343 | /* maybe try to auto-load a module? */ | |
f72ffdd6 | 6344 | printk(KERN_INFO |
1da177e4 LT |
6345 | "md: superblock version %d not known\n", |
6346 | info->major_version); | |
6347 | return -EINVAL; | |
6348 | } | |
6349 | mddev->major_version = info->major_version; | |
6350 | mddev->minor_version = info->minor_version; | |
6351 | mddev->patch_version = info->patch_version; | |
3f9d7b0d | 6352 | mddev->persistent = !info->not_persistent; |
cbd19983 N |
6353 | /* ensure mddev_put doesn't delete this now that there |
6354 | * is some minimal configuration. | |
6355 | */ | |
9ebc6ef1 | 6356 | mddev->ctime = ktime_get_real_seconds(); |
1da177e4 LT |
6357 | return 0; |
6358 | } | |
6359 | mddev->major_version = MD_MAJOR_VERSION; | |
6360 | mddev->minor_version = MD_MINOR_VERSION; | |
6361 | mddev->patch_version = MD_PATCHLEVEL_VERSION; | |
9ebc6ef1 | 6362 | mddev->ctime = ktime_get_real_seconds(); |
1da177e4 LT |
6363 | |
6364 | mddev->level = info->level; | |
17115e03 | 6365 | mddev->clevel[0] = 0; |
58c0fed4 | 6366 | mddev->dev_sectors = 2 * (sector_t)info->size; |
1da177e4 LT |
6367 | mddev->raid_disks = info->raid_disks; |
6368 | /* don't set md_minor, it is determined by which /dev/md* was | |
6369 | * openned | |
6370 | */ | |
6371 | if (info->state & (1<<MD_SB_CLEAN)) | |
6372 | mddev->recovery_cp = MaxSector; | |
6373 | else | |
6374 | mddev->recovery_cp = 0; | |
6375 | mddev->persistent = ! info->not_persistent; | |
e691063a | 6376 | mddev->external = 0; |
1da177e4 LT |
6377 | |
6378 | mddev->layout = info->layout; | |
9d8f0363 | 6379 | mddev->chunk_sectors = info->chunk_size >> 9; |
1da177e4 LT |
6380 | |
6381 | mddev->max_disks = MD_SB_DISKS; | |
6382 | ||
e691063a N |
6383 | if (mddev->persistent) |
6384 | mddev->flags = 0; | |
850b2b42 | 6385 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
1da177e4 | 6386 | |
c3d9714e | 6387 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
6409bb05 | 6388 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
c3d9714e | 6389 | mddev->bitmap_info.offset = 0; |
b2a2703c | 6390 | |
f6705578 N |
6391 | mddev->reshape_position = MaxSector; |
6392 | ||
1da177e4 LT |
6393 | /* |
6394 | * Generate a 128 bit UUID | |
6395 | */ | |
6396 | get_random_bytes(mddev->uuid, 16); | |
6397 | ||
f6705578 | 6398 | mddev->new_level = mddev->level; |
664e7c41 | 6399 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
6400 | mddev->new_layout = mddev->layout; |
6401 | mddev->delta_disks = 0; | |
2c810cdd | 6402 | mddev->reshape_backwards = 0; |
f6705578 | 6403 | |
1da177e4 LT |
6404 | return 0; |
6405 | } | |
6406 | ||
fd01b88c | 6407 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
1f403624 | 6408 | { |
b522adcd DW |
6409 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
6410 | ||
6411 | if (mddev->external_size) | |
6412 | return; | |
6413 | ||
1f403624 DW |
6414 | mddev->array_sectors = array_sectors; |
6415 | } | |
6416 | EXPORT_SYMBOL(md_set_array_sectors); | |
6417 | ||
fd01b88c | 6418 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
a35b0d69 | 6419 | { |
3cb03002 | 6420 | struct md_rdev *rdev; |
a35b0d69 | 6421 | int rv; |
d71f9f88 | 6422 | int fit = (num_sectors == 0); |
a35b0d69 N |
6423 | |
6424 | if (mddev->pers->resize == NULL) | |
6425 | return -EINVAL; | |
d71f9f88 AN |
6426 | /* The "num_sectors" is the number of sectors of each device that |
6427 | * is used. This can only make sense for arrays with redundancy. | |
6428 | * linear and raid0 always use whatever space is available. We can only | |
6429 | * consider changing this number if no resync or reconstruction is | |
6430 | * happening, and if the new size is acceptable. It must fit before the | |
0f420358 | 6431 | * sb_start or, if that is <data_offset, it must fit before the size |
d71f9f88 AN |
6432 | * of each device. If num_sectors is zero, we find the largest size |
6433 | * that fits. | |
a35b0d69 | 6434 | */ |
f851b60d N |
6435 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
6436 | mddev->sync_thread) | |
a35b0d69 | 6437 | return -EBUSY; |
bd8839e0 N |
6438 | if (mddev->ro) |
6439 | return -EROFS; | |
a4a6125a | 6440 | |
dafb20fa | 6441 | rdev_for_each(rdev, mddev) { |
dd8ac336 | 6442 | sector_t avail = rdev->sectors; |
01ab5662 | 6443 | |
d71f9f88 AN |
6444 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
6445 | num_sectors = avail; | |
6446 | if (avail < num_sectors) | |
a35b0d69 N |
6447 | return -ENOSPC; |
6448 | } | |
d71f9f88 | 6449 | rv = mddev->pers->resize(mddev, num_sectors); |
449aad3e N |
6450 | if (!rv) |
6451 | revalidate_disk(mddev->gendisk); | |
a35b0d69 N |
6452 | return rv; |
6453 | } | |
6454 | ||
fd01b88c | 6455 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
da943b99 N |
6456 | { |
6457 | int rv; | |
c6563a8c | 6458 | struct md_rdev *rdev; |
da943b99 | 6459 | /* change the number of raid disks */ |
63c70c4f | 6460 | if (mddev->pers->check_reshape == NULL) |
da943b99 | 6461 | return -EINVAL; |
bd8839e0 N |
6462 | if (mddev->ro) |
6463 | return -EROFS; | |
da943b99 | 6464 | if (raid_disks <= 0 || |
233fca36 | 6465 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
da943b99 | 6466 | return -EINVAL; |
f851b60d N |
6467 | if (mddev->sync_thread || |
6468 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || | |
6469 | mddev->reshape_position != MaxSector) | |
da943b99 | 6470 | return -EBUSY; |
c6563a8c N |
6471 | |
6472 | rdev_for_each(rdev, mddev) { | |
6473 | if (mddev->raid_disks < raid_disks && | |
6474 | rdev->data_offset < rdev->new_data_offset) | |
6475 | return -EINVAL; | |
6476 | if (mddev->raid_disks > raid_disks && | |
6477 | rdev->data_offset > rdev->new_data_offset) | |
6478 | return -EINVAL; | |
6479 | } | |
6480 | ||
63c70c4f | 6481 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
2c810cdd N |
6482 | if (mddev->delta_disks < 0) |
6483 | mddev->reshape_backwards = 1; | |
6484 | else if (mddev->delta_disks > 0) | |
6485 | mddev->reshape_backwards = 0; | |
63c70c4f N |
6486 | |
6487 | rv = mddev->pers->check_reshape(mddev); | |
2c810cdd | 6488 | if (rv < 0) { |
de171cb9 | 6489 | mddev->delta_disks = 0; |
2c810cdd N |
6490 | mddev->reshape_backwards = 0; |
6491 | } | |
da943b99 N |
6492 | return rv; |
6493 | } | |
6494 | ||
1da177e4 LT |
6495 | /* |
6496 | * update_array_info is used to change the configuration of an | |
6497 | * on-line array. | |
6498 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size | |
6499 | * fields in the info are checked against the array. | |
6500 | * Any differences that cannot be handled will cause an error. | |
6501 | * Normally, only one change can be managed at a time. | |
6502 | */ | |
fd01b88c | 6503 | static int update_array_info(struct mddev *mddev, mdu_array_info_t *info) |
1da177e4 LT |
6504 | { |
6505 | int rv = 0; | |
6506 | int cnt = 0; | |
36fa3063 N |
6507 | int state = 0; |
6508 | ||
6509 | /* calculate expected state,ignoring low bits */ | |
c3d9714e | 6510 | if (mddev->bitmap && mddev->bitmap_info.offset) |
36fa3063 | 6511 | state |= (1 << MD_SB_BITMAP_PRESENT); |
1da177e4 LT |
6512 | |
6513 | if (mddev->major_version != info->major_version || | |
6514 | mddev->minor_version != info->minor_version || | |
6515 | /* mddev->patch_version != info->patch_version || */ | |
6516 | mddev->ctime != info->ctime || | |
6517 | mddev->level != info->level || | |
6518 | /* mddev->layout != info->layout || */ | |
4e023612 | 6519 | mddev->persistent != !info->not_persistent || |
9d8f0363 | 6520 | mddev->chunk_sectors != info->chunk_size >> 9 || |
36fa3063 N |
6521 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
6522 | ((state^info->state) & 0xfffffe00) | |
6523 | ) | |
1da177e4 LT |
6524 | return -EINVAL; |
6525 | /* Check there is only one change */ | |
58c0fed4 AN |
6526 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
6527 | cnt++; | |
6528 | if (mddev->raid_disks != info->raid_disks) | |
6529 | cnt++; | |
6530 | if (mddev->layout != info->layout) | |
6531 | cnt++; | |
6532 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) | |
6533 | cnt++; | |
6534 | if (cnt == 0) | |
6535 | return 0; | |
6536 | if (cnt > 1) | |
6537 | return -EINVAL; | |
1da177e4 LT |
6538 | |
6539 | if (mddev->layout != info->layout) { | |
6540 | /* Change layout | |
6541 | * we don't need to do anything at the md level, the | |
6542 | * personality will take care of it all. | |
6543 | */ | |
50ac168a | 6544 | if (mddev->pers->check_reshape == NULL) |
1da177e4 | 6545 | return -EINVAL; |
597a711b N |
6546 | else { |
6547 | mddev->new_layout = info->layout; | |
50ac168a | 6548 | rv = mddev->pers->check_reshape(mddev); |
597a711b N |
6549 | if (rv) |
6550 | mddev->new_layout = mddev->layout; | |
6551 | return rv; | |
6552 | } | |
1da177e4 | 6553 | } |
58c0fed4 | 6554 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
d71f9f88 | 6555 | rv = update_size(mddev, (sector_t)info->size * 2); |
a35b0d69 | 6556 | |
da943b99 N |
6557 | if (mddev->raid_disks != info->raid_disks) |
6558 | rv = update_raid_disks(mddev, info->raid_disks); | |
6559 | ||
36fa3063 | 6560 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
293467aa GR |
6561 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
6562 | rv = -EINVAL; | |
6563 | goto err; | |
6564 | } | |
6565 | if (mddev->recovery || mddev->sync_thread) { | |
6566 | rv = -EBUSY; | |
6567 | goto err; | |
6568 | } | |
36fa3063 | 6569 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
f9209a32 | 6570 | struct bitmap *bitmap; |
36fa3063 | 6571 | /* add the bitmap */ |
293467aa GR |
6572 | if (mddev->bitmap) { |
6573 | rv = -EEXIST; | |
6574 | goto err; | |
6575 | } | |
6576 | if (mddev->bitmap_info.default_offset == 0) { | |
6577 | rv = -EINVAL; | |
6578 | goto err; | |
6579 | } | |
c3d9714e N |
6580 | mddev->bitmap_info.offset = |
6581 | mddev->bitmap_info.default_offset; | |
6409bb05 N |
6582 | mddev->bitmap_info.space = |
6583 | mddev->bitmap_info.default_space; | |
36fa3063 | 6584 | mddev->pers->quiesce(mddev, 1); |
f9209a32 GR |
6585 | bitmap = bitmap_create(mddev, -1); |
6586 | if (!IS_ERR(bitmap)) { | |
6587 | mddev->bitmap = bitmap; | |
69e51b44 | 6588 | rv = bitmap_load(mddev); |
ba599aca N |
6589 | } else |
6590 | rv = PTR_ERR(bitmap); | |
36fa3063 N |
6591 | if (rv) |
6592 | bitmap_destroy(mddev); | |
6593 | mddev->pers->quiesce(mddev, 0); | |
6594 | } else { | |
6595 | /* remove the bitmap */ | |
293467aa GR |
6596 | if (!mddev->bitmap) { |
6597 | rv = -ENOENT; | |
6598 | goto err; | |
6599 | } | |
6600 | if (mddev->bitmap->storage.file) { | |
6601 | rv = -EINVAL; | |
6602 | goto err; | |
6603 | } | |
f6a2dc64 GJ |
6604 | if (mddev->bitmap_info.nodes) { |
6605 | /* hold PW on all the bitmap lock */ | |
6606 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { | |
6607 | printk("md: can't change bitmap to none since the" | |
6608 | " array is in use by more than one node\n"); | |
6609 | rv = -EPERM; | |
6610 | md_cluster_ops->unlock_all_bitmaps(mddev); | |
6611 | goto err; | |
6612 | } | |
6613 | ||
6614 | mddev->bitmap_info.nodes = 0; | |
6615 | md_cluster_ops->leave(mddev); | |
6616 | } | |
36fa3063 N |
6617 | mddev->pers->quiesce(mddev, 1); |
6618 | bitmap_destroy(mddev); | |
6619 | mddev->pers->quiesce(mddev, 0); | |
c3d9714e | 6620 | mddev->bitmap_info.offset = 0; |
36fa3063 N |
6621 | } |
6622 | } | |
850b2b42 | 6623 | md_update_sb(mddev, 1); |
293467aa GR |
6624 | return rv; |
6625 | err: | |
1da177e4 LT |
6626 | return rv; |
6627 | } | |
6628 | ||
fd01b88c | 6629 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
1da177e4 | 6630 | { |
3cb03002 | 6631 | struct md_rdev *rdev; |
1ca69c4b | 6632 | int err = 0; |
1da177e4 LT |
6633 | |
6634 | if (mddev->pers == NULL) | |
6635 | return -ENODEV; | |
6636 | ||
1ca69c4b N |
6637 | rcu_read_lock(); |
6638 | rdev = find_rdev_rcu(mddev, dev); | |
1da177e4 | 6639 | if (!rdev) |
1ca69c4b N |
6640 | err = -ENODEV; |
6641 | else { | |
6642 | md_error(mddev, rdev); | |
6643 | if (!test_bit(Faulty, &rdev->flags)) | |
6644 | err = -EBUSY; | |
6645 | } | |
6646 | rcu_read_unlock(); | |
6647 | return err; | |
1da177e4 LT |
6648 | } |
6649 | ||
2f9618ce AN |
6650 | /* |
6651 | * We have a problem here : there is no easy way to give a CHS | |
6652 | * virtual geometry. We currently pretend that we have a 2 heads | |
6653 | * 4 sectors (with a BIG number of cylinders...). This drives | |
6654 | * dosfs just mad... ;-) | |
6655 | */ | |
a885c8c4 CH |
6656 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
6657 | { | |
fd01b88c | 6658 | struct mddev *mddev = bdev->bd_disk->private_data; |
a885c8c4 CH |
6659 | |
6660 | geo->heads = 2; | |
6661 | geo->sectors = 4; | |
49ce6cea | 6662 | geo->cylinders = mddev->array_sectors / 8; |
a885c8c4 CH |
6663 | return 0; |
6664 | } | |
6665 | ||
cb335f88 NS |
6666 | static inline bool md_ioctl_valid(unsigned int cmd) |
6667 | { | |
6668 | switch (cmd) { | |
6669 | case ADD_NEW_DISK: | |
6670 | case BLKROSET: | |
6671 | case GET_ARRAY_INFO: | |
6672 | case GET_BITMAP_FILE: | |
6673 | case GET_DISK_INFO: | |
6674 | case HOT_ADD_DISK: | |
6675 | case HOT_REMOVE_DISK: | |
cb335f88 NS |
6676 | case RAID_AUTORUN: |
6677 | case RAID_VERSION: | |
6678 | case RESTART_ARRAY_RW: | |
6679 | case RUN_ARRAY: | |
6680 | case SET_ARRAY_INFO: | |
6681 | case SET_BITMAP_FILE: | |
6682 | case SET_DISK_FAULTY: | |
6683 | case STOP_ARRAY: | |
6684 | case STOP_ARRAY_RO: | |
1aee41f6 | 6685 | case CLUSTERED_DISK_NACK: |
cb335f88 NS |
6686 | return true; |
6687 | default: | |
6688 | return false; | |
6689 | } | |
6690 | } | |
6691 | ||
a39907fa | 6692 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
1da177e4 LT |
6693 | unsigned int cmd, unsigned long arg) |
6694 | { | |
6695 | int err = 0; | |
6696 | void __user *argp = (void __user *)arg; | |
fd01b88c | 6697 | struct mddev *mddev = NULL; |
e2218350 | 6698 | int ro; |
1da177e4 | 6699 | |
cb335f88 NS |
6700 | if (!md_ioctl_valid(cmd)) |
6701 | return -ENOTTY; | |
6702 | ||
506c9e44 N |
6703 | switch (cmd) { |
6704 | case RAID_VERSION: | |
6705 | case GET_ARRAY_INFO: | |
6706 | case GET_DISK_INFO: | |
6707 | break; | |
6708 | default: | |
6709 | if (!capable(CAP_SYS_ADMIN)) | |
6710 | return -EACCES; | |
6711 | } | |
1da177e4 LT |
6712 | |
6713 | /* | |
6714 | * Commands dealing with the RAID driver but not any | |
6715 | * particular array: | |
6716 | */ | |
c02c0aeb N |
6717 | switch (cmd) { |
6718 | case RAID_VERSION: | |
6719 | err = get_version(argp); | |
3adc28d8 | 6720 | goto out; |
1da177e4 | 6721 | |
1da177e4 | 6722 | #ifndef MODULE |
c02c0aeb N |
6723 | case RAID_AUTORUN: |
6724 | err = 0; | |
6725 | autostart_arrays(arg); | |
3adc28d8 | 6726 | goto out; |
1da177e4 | 6727 | #endif |
c02c0aeb | 6728 | default:; |
1da177e4 LT |
6729 | } |
6730 | ||
6731 | /* | |
6732 | * Commands creating/starting a new array: | |
6733 | */ | |
6734 | ||
a39907fa | 6735 | mddev = bdev->bd_disk->private_data; |
1da177e4 LT |
6736 | |
6737 | if (!mddev) { | |
6738 | BUG(); | |
3adc28d8 | 6739 | goto out; |
1da177e4 LT |
6740 | } |
6741 | ||
1ca69c4b N |
6742 | /* Some actions do not requires the mutex */ |
6743 | switch (cmd) { | |
6744 | case GET_ARRAY_INFO: | |
6745 | if (!mddev->raid_disks && !mddev->external) | |
6746 | err = -ENODEV; | |
6747 | else | |
6748 | err = get_array_info(mddev, argp); | |
3adc28d8 | 6749 | goto out; |
1ca69c4b N |
6750 | |
6751 | case GET_DISK_INFO: | |
6752 | if (!mddev->raid_disks && !mddev->external) | |
6753 | err = -ENODEV; | |
6754 | else | |
6755 | err = get_disk_info(mddev, argp); | |
3adc28d8 | 6756 | goto out; |
1ca69c4b N |
6757 | |
6758 | case SET_DISK_FAULTY: | |
6759 | err = set_disk_faulty(mddev, new_decode_dev(arg)); | |
3adc28d8 | 6760 | goto out; |
4af1a041 N |
6761 | |
6762 | case GET_BITMAP_FILE: | |
6763 | err = get_bitmap_file(mddev, argp); | |
6764 | goto out; | |
6765 | ||
1ca69c4b N |
6766 | } |
6767 | ||
a7a3f08d N |
6768 | if (cmd == ADD_NEW_DISK) |
6769 | /* need to ensure md_delayed_delete() has completed */ | |
6770 | flush_workqueue(md_misc_wq); | |
6771 | ||
90f5f7ad HR |
6772 | if (cmd == HOT_REMOVE_DISK) |
6773 | /* need to ensure recovery thread has run */ | |
6774 | wait_event_interruptible_timeout(mddev->sb_wait, | |
6775 | !test_bit(MD_RECOVERY_NEEDED, | |
6776 | &mddev->flags), | |
6777 | msecs_to_jiffies(5000)); | |
260fa034 N |
6778 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
6779 | /* Need to flush page cache, and ensure no-one else opens | |
6780 | * and writes | |
6781 | */ | |
6782 | mutex_lock(&mddev->open_mutex); | |
9ba3b7f5 | 6783 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
260fa034 N |
6784 | mutex_unlock(&mddev->open_mutex); |
6785 | err = -EBUSY; | |
3adc28d8 | 6786 | goto out; |
260fa034 N |
6787 | } |
6788 | set_bit(MD_STILL_CLOSED, &mddev->flags); | |
6789 | mutex_unlock(&mddev->open_mutex); | |
6790 | sync_blockdev(bdev); | |
6791 | } | |
1da177e4 LT |
6792 | err = mddev_lock(mddev); |
6793 | if (err) { | |
f72ffdd6 | 6794 | printk(KERN_INFO |
1da177e4 LT |
6795 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
6796 | err, cmd); | |
3adc28d8 | 6797 | goto out; |
1da177e4 LT |
6798 | } |
6799 | ||
c02c0aeb N |
6800 | if (cmd == SET_ARRAY_INFO) { |
6801 | mdu_array_info_t info; | |
6802 | if (!arg) | |
6803 | memset(&info, 0, sizeof(info)); | |
6804 | else if (copy_from_user(&info, argp, sizeof(info))) { | |
6805 | err = -EFAULT; | |
3adc28d8 | 6806 | goto unlock; |
c02c0aeb N |
6807 | } |
6808 | if (mddev->pers) { | |
6809 | err = update_array_info(mddev, &info); | |
6810 | if (err) { | |
6811 | printk(KERN_WARNING "md: couldn't update" | |
6812 | " array info. %d\n", err); | |
3adc28d8 | 6813 | goto unlock; |
1da177e4 | 6814 | } |
3adc28d8 | 6815 | goto unlock; |
c02c0aeb N |
6816 | } |
6817 | if (!list_empty(&mddev->disks)) { | |
6818 | printk(KERN_WARNING | |
6819 | "md: array %s already has disks!\n", | |
6820 | mdname(mddev)); | |
6821 | err = -EBUSY; | |
3adc28d8 | 6822 | goto unlock; |
c02c0aeb N |
6823 | } |
6824 | if (mddev->raid_disks) { | |
6825 | printk(KERN_WARNING | |
6826 | "md: array %s already initialised!\n", | |
6827 | mdname(mddev)); | |
6828 | err = -EBUSY; | |
3adc28d8 | 6829 | goto unlock; |
c02c0aeb N |
6830 | } |
6831 | err = set_array_info(mddev, &info); | |
6832 | if (err) { | |
6833 | printk(KERN_WARNING "md: couldn't set" | |
6834 | " array info. %d\n", err); | |
3adc28d8 | 6835 | goto unlock; |
c02c0aeb | 6836 | } |
3adc28d8 | 6837 | goto unlock; |
1da177e4 LT |
6838 | } |
6839 | ||
6840 | /* | |
6841 | * Commands querying/configuring an existing array: | |
6842 | */ | |
32a7627c | 6843 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
3f9d7b0d | 6844 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
a17184a9 N |
6845 | if ((!mddev->raid_disks && !mddev->external) |
6846 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY | |
6847 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE | |
6848 | && cmd != GET_BITMAP_FILE) { | |
1da177e4 | 6849 | err = -ENODEV; |
3adc28d8 | 6850 | goto unlock; |
1da177e4 LT |
6851 | } |
6852 | ||
6853 | /* | |
6854 | * Commands even a read-only array can execute: | |
6855 | */ | |
c02c0aeb | 6856 | switch (cmd) { |
c02c0aeb N |
6857 | case RESTART_ARRAY_RW: |
6858 | err = restart_array(mddev); | |
3adc28d8 | 6859 | goto unlock; |
1da177e4 | 6860 | |
c02c0aeb N |
6861 | case STOP_ARRAY: |
6862 | err = do_md_stop(mddev, 0, bdev); | |
3adc28d8 | 6863 | goto unlock; |
1da177e4 | 6864 | |
c02c0aeb N |
6865 | case STOP_ARRAY_RO: |
6866 | err = md_set_readonly(mddev, bdev); | |
3adc28d8 | 6867 | goto unlock; |
1da177e4 | 6868 | |
3ea8929d N |
6869 | case HOT_REMOVE_DISK: |
6870 | err = hot_remove_disk(mddev, new_decode_dev(arg)); | |
3adc28d8 | 6871 | goto unlock; |
3ea8929d | 6872 | |
7ceb17e8 N |
6873 | case ADD_NEW_DISK: |
6874 | /* We can support ADD_NEW_DISK on read-only arrays | |
6875 | * on if we are re-adding a preexisting device. | |
6876 | * So require mddev->pers and MD_DISK_SYNC. | |
6877 | */ | |
6878 | if (mddev->pers) { | |
6879 | mdu_disk_info_t info; | |
6880 | if (copy_from_user(&info, argp, sizeof(info))) | |
6881 | err = -EFAULT; | |
6882 | else if (!(info.state & (1<<MD_DISK_SYNC))) | |
6883 | /* Need to clear read-only for this */ | |
6884 | break; | |
6885 | else | |
6886 | err = add_new_disk(mddev, &info); | |
3adc28d8 | 6887 | goto unlock; |
7ceb17e8 N |
6888 | } |
6889 | break; | |
6890 | ||
c02c0aeb N |
6891 | case BLKROSET: |
6892 | if (get_user(ro, (int __user *)(arg))) { | |
6893 | err = -EFAULT; | |
3adc28d8 | 6894 | goto unlock; |
c02c0aeb N |
6895 | } |
6896 | err = -EINVAL; | |
e2218350 | 6897 | |
c02c0aeb N |
6898 | /* if the bdev is going readonly the value of mddev->ro |
6899 | * does not matter, no writes are coming | |
6900 | */ | |
6901 | if (ro) | |
3adc28d8 | 6902 | goto unlock; |
e2218350 | 6903 | |
c02c0aeb N |
6904 | /* are we are already prepared for writes? */ |
6905 | if (mddev->ro != 1) | |
3adc28d8 | 6906 | goto unlock; |
e2218350 | 6907 | |
c02c0aeb N |
6908 | /* transitioning to readauto need only happen for |
6909 | * arrays that call md_write_start | |
6910 | */ | |
6911 | if (mddev->pers) { | |
6912 | err = restart_array(mddev); | |
6913 | if (err == 0) { | |
6914 | mddev->ro = 2; | |
6915 | set_disk_ro(mddev->gendisk, 0); | |
e2218350 | 6916 | } |
c02c0aeb | 6917 | } |
3adc28d8 | 6918 | goto unlock; |
1da177e4 LT |
6919 | } |
6920 | ||
6921 | /* | |
6922 | * The remaining ioctls are changing the state of the | |
f91de92e | 6923 | * superblock, so we do not allow them on read-only arrays. |
1da177e4 | 6924 | */ |
326eb17d | 6925 | if (mddev->ro && mddev->pers) { |
f91de92e N |
6926 | if (mddev->ro == 2) { |
6927 | mddev->ro = 0; | |
00bcb4ac | 6928 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0fd62b86 | 6929 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
f3378b48 N |
6930 | /* mddev_unlock will wake thread */ |
6931 | /* If a device failed while we were read-only, we | |
6932 | * need to make sure the metadata is updated now. | |
6933 | */ | |
6934 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { | |
6935 | mddev_unlock(mddev); | |
6936 | wait_event(mddev->sb_wait, | |
6937 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && | |
6938 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); | |
29f097c4 | 6939 | mddev_lock_nointr(mddev); |
f3378b48 | 6940 | } |
f91de92e N |
6941 | } else { |
6942 | err = -EROFS; | |
3adc28d8 | 6943 | goto unlock; |
f91de92e | 6944 | } |
1da177e4 LT |
6945 | } |
6946 | ||
c02c0aeb N |
6947 | switch (cmd) { |
6948 | case ADD_NEW_DISK: | |
1da177e4 | 6949 | { |
c02c0aeb N |
6950 | mdu_disk_info_t info; |
6951 | if (copy_from_user(&info, argp, sizeof(info))) | |
6952 | err = -EFAULT; | |
6953 | else | |
6954 | err = add_new_disk(mddev, &info); | |
3adc28d8 | 6955 | goto unlock; |
c02c0aeb | 6956 | } |
1da177e4 | 6957 | |
1aee41f6 GR |
6958 | case CLUSTERED_DISK_NACK: |
6959 | if (mddev_is_clustered(mddev)) | |
6960 | md_cluster_ops->new_disk_ack(mddev, false); | |
6961 | else | |
6962 | err = -EINVAL; | |
6963 | goto unlock; | |
6964 | ||
c02c0aeb N |
6965 | case HOT_ADD_DISK: |
6966 | err = hot_add_disk(mddev, new_decode_dev(arg)); | |
3adc28d8 | 6967 | goto unlock; |
1da177e4 | 6968 | |
c02c0aeb N |
6969 | case RUN_ARRAY: |
6970 | err = do_md_run(mddev); | |
3adc28d8 | 6971 | goto unlock; |
1da177e4 | 6972 | |
c02c0aeb N |
6973 | case SET_BITMAP_FILE: |
6974 | err = set_bitmap_file(mddev, (int)arg); | |
3adc28d8 | 6975 | goto unlock; |
32a7627c | 6976 | |
c02c0aeb N |
6977 | default: |
6978 | err = -EINVAL; | |
3adc28d8 | 6979 | goto unlock; |
1da177e4 LT |
6980 | } |
6981 | ||
3adc28d8 | 6982 | unlock: |
d3374825 N |
6983 | if (mddev->hold_active == UNTIL_IOCTL && |
6984 | err != -EINVAL) | |
6985 | mddev->hold_active = 0; | |
1da177e4 | 6986 | mddev_unlock(mddev); |
3adc28d8 | 6987 | out: |
1da177e4 LT |
6988 | return err; |
6989 | } | |
aa98aa31 AB |
6990 | #ifdef CONFIG_COMPAT |
6991 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
6992 | unsigned int cmd, unsigned long arg) | |
6993 | { | |
6994 | switch (cmd) { | |
6995 | case HOT_REMOVE_DISK: | |
6996 | case HOT_ADD_DISK: | |
6997 | case SET_DISK_FAULTY: | |
6998 | case SET_BITMAP_FILE: | |
6999 | /* These take in integer arg, do not convert */ | |
7000 | break; | |
7001 | default: | |
7002 | arg = (unsigned long)compat_ptr(arg); | |
7003 | break; | |
7004 | } | |
7005 | ||
7006 | return md_ioctl(bdev, mode, cmd, arg); | |
7007 | } | |
7008 | #endif /* CONFIG_COMPAT */ | |
1da177e4 | 7009 | |
a39907fa | 7010 | static int md_open(struct block_device *bdev, fmode_t mode) |
1da177e4 LT |
7011 | { |
7012 | /* | |
7013 | * Succeed if we can lock the mddev, which confirms that | |
7014 | * it isn't being stopped right now. | |
7015 | */ | |
fd01b88c | 7016 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
1da177e4 LT |
7017 | int err; |
7018 | ||
0c098220 YL |
7019 | if (!mddev) |
7020 | return -ENODEV; | |
7021 | ||
d3374825 N |
7022 | if (mddev->gendisk != bdev->bd_disk) { |
7023 | /* we are racing with mddev_put which is discarding this | |
7024 | * bd_disk. | |
7025 | */ | |
7026 | mddev_put(mddev); | |
7027 | /* Wait until bdev->bd_disk is definitely gone */ | |
e804ac78 | 7028 | flush_workqueue(md_misc_wq); |
d3374825 N |
7029 | /* Then retry the open from the top */ |
7030 | return -ERESTARTSYS; | |
7031 | } | |
7032 | BUG_ON(mddev != bdev->bd_disk->private_data); | |
7033 | ||
c8c00a69 | 7034 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
1da177e4 LT |
7035 | goto out; |
7036 | ||
7037 | err = 0; | |
f2ea68cf | 7038 | atomic_inc(&mddev->openers); |
260fa034 | 7039 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
c8c00a69 | 7040 | mutex_unlock(&mddev->open_mutex); |
1da177e4 | 7041 | |
f0b4f7e2 | 7042 | check_disk_change(bdev); |
1da177e4 LT |
7043 | out: |
7044 | return err; | |
7045 | } | |
7046 | ||
db2a144b | 7047 | static void md_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 7048 | { |
f72ffdd6 | 7049 | struct mddev *mddev = disk->private_data; |
1da177e4 | 7050 | |
52e5f9d1 | 7051 | BUG_ON(!mddev); |
f2ea68cf | 7052 | atomic_dec(&mddev->openers); |
1da177e4 | 7053 | mddev_put(mddev); |
1da177e4 | 7054 | } |
f0b4f7e2 N |
7055 | |
7056 | static int md_media_changed(struct gendisk *disk) | |
7057 | { | |
fd01b88c | 7058 | struct mddev *mddev = disk->private_data; |
f0b4f7e2 N |
7059 | |
7060 | return mddev->changed; | |
7061 | } | |
7062 | ||
7063 | static int md_revalidate(struct gendisk *disk) | |
7064 | { | |
fd01b88c | 7065 | struct mddev *mddev = disk->private_data; |
f0b4f7e2 N |
7066 | |
7067 | mddev->changed = 0; | |
7068 | return 0; | |
7069 | } | |
83d5cde4 | 7070 | static const struct block_device_operations md_fops = |
1da177e4 LT |
7071 | { |
7072 | .owner = THIS_MODULE, | |
a39907fa AV |
7073 | .open = md_open, |
7074 | .release = md_release, | |
b492b852 | 7075 | .ioctl = md_ioctl, |
aa98aa31 AB |
7076 | #ifdef CONFIG_COMPAT |
7077 | .compat_ioctl = md_compat_ioctl, | |
7078 | #endif | |
a885c8c4 | 7079 | .getgeo = md_getgeo, |
f0b4f7e2 N |
7080 | .media_changed = md_media_changed, |
7081 | .revalidate_disk= md_revalidate, | |
1da177e4 LT |
7082 | }; |
7083 | ||
f72ffdd6 | 7084 | static int md_thread(void *arg) |
1da177e4 | 7085 | { |
2b8bf345 | 7086 | struct md_thread *thread = arg; |
1da177e4 | 7087 | |
1da177e4 LT |
7088 | /* |
7089 | * md_thread is a 'system-thread', it's priority should be very | |
7090 | * high. We avoid resource deadlocks individually in each | |
7091 | * raid personality. (RAID5 does preallocation) We also use RR and | |
7092 | * the very same RT priority as kswapd, thus we will never get | |
7093 | * into a priority inversion deadlock. | |
7094 | * | |
7095 | * we definitely have to have equal or higher priority than | |
7096 | * bdflush, otherwise bdflush will deadlock if there are too | |
7097 | * many dirty RAID5 blocks. | |
7098 | */ | |
1da177e4 | 7099 | |
6985c43f | 7100 | allow_signal(SIGKILL); |
a6fb0934 | 7101 | while (!kthread_should_stop()) { |
1da177e4 | 7102 | |
93588e22 N |
7103 | /* We need to wait INTERRUPTIBLE so that |
7104 | * we don't add to the load-average. | |
7105 | * That means we need to be sure no signals are | |
7106 | * pending | |
7107 | */ | |
7108 | if (signal_pending(current)) | |
7109 | flush_signals(current); | |
7110 | ||
7111 | wait_event_interruptible_timeout | |
7112 | (thread->wqueue, | |
7113 | test_bit(THREAD_WAKEUP, &thread->flags) | |
7114 | || kthread_should_stop(), | |
7115 | thread->timeout); | |
1da177e4 | 7116 | |
6c987910 N |
7117 | clear_bit(THREAD_WAKEUP, &thread->flags); |
7118 | if (!kthread_should_stop()) | |
4ed8731d | 7119 | thread->run(thread); |
1da177e4 | 7120 | } |
a6fb0934 | 7121 | |
1da177e4 LT |
7122 | return 0; |
7123 | } | |
7124 | ||
2b8bf345 | 7125 | void md_wakeup_thread(struct md_thread *thread) |
1da177e4 LT |
7126 | { |
7127 | if (thread) { | |
36a4e1fe | 7128 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
1da177e4 LT |
7129 | set_bit(THREAD_WAKEUP, &thread->flags); |
7130 | wake_up(&thread->wqueue); | |
7131 | } | |
7132 | } | |
6c144d31 | 7133 | EXPORT_SYMBOL(md_wakeup_thread); |
1da177e4 | 7134 | |
4ed8731d SL |
7135 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
7136 | struct mddev *mddev, const char *name) | |
1da177e4 | 7137 | { |
2b8bf345 | 7138 | struct md_thread *thread; |
1da177e4 | 7139 | |
2b8bf345 | 7140 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
1da177e4 LT |
7141 | if (!thread) |
7142 | return NULL; | |
7143 | ||
1da177e4 LT |
7144 | init_waitqueue_head(&thread->wqueue); |
7145 | ||
1da177e4 LT |
7146 | thread->run = run; |
7147 | thread->mddev = mddev; | |
32a7627c | 7148 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
0da3c619 N |
7149 | thread->tsk = kthread_run(md_thread, thread, |
7150 | "%s_%s", | |
7151 | mdname(thread->mddev), | |
0232605d | 7152 | name); |
a6fb0934 | 7153 | if (IS_ERR(thread->tsk)) { |
1da177e4 LT |
7154 | kfree(thread); |
7155 | return NULL; | |
7156 | } | |
1da177e4 LT |
7157 | return thread; |
7158 | } | |
6c144d31 | 7159 | EXPORT_SYMBOL(md_register_thread); |
1da177e4 | 7160 | |
2b8bf345 | 7161 | void md_unregister_thread(struct md_thread **threadp) |
1da177e4 | 7162 | { |
2b8bf345 | 7163 | struct md_thread *thread = *threadp; |
e0cf8f04 N |
7164 | if (!thread) |
7165 | return; | |
36a4e1fe | 7166 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
01f96c0a N |
7167 | /* Locking ensures that mddev_unlock does not wake_up a |
7168 | * non-existent thread | |
7169 | */ | |
7170 | spin_lock(&pers_lock); | |
7171 | *threadp = NULL; | |
7172 | spin_unlock(&pers_lock); | |
a6fb0934 N |
7173 | |
7174 | kthread_stop(thread->tsk); | |
1da177e4 LT |
7175 | kfree(thread); |
7176 | } | |
6c144d31 | 7177 | EXPORT_SYMBOL(md_unregister_thread); |
1da177e4 | 7178 | |
fd01b88c | 7179 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 7180 | { |
b2d444d7 | 7181 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
1da177e4 | 7182 | return; |
6bfe0b49 | 7183 | |
de393cde | 7184 | if (!mddev->pers || !mddev->pers->error_handler) |
1da177e4 LT |
7185 | return; |
7186 | mddev->pers->error_handler(mddev,rdev); | |
72a23c21 NB |
7187 | if (mddev->degraded) |
7188 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
00bcb4ac | 7189 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
1da177e4 LT |
7190 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
7191 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
7192 | md_wakeup_thread(mddev->thread); | |
768a418d | 7193 | if (mddev->event_work.func) |
e804ac78 | 7194 | queue_work(md_misc_wq, &mddev->event_work); |
c331eb04 | 7195 | md_new_event_inintr(mddev); |
1da177e4 | 7196 | } |
6c144d31 | 7197 | EXPORT_SYMBOL(md_error); |
1da177e4 LT |
7198 | |
7199 | /* seq_file implementation /proc/mdstat */ | |
7200 | ||
7201 | static void status_unused(struct seq_file *seq) | |
7202 | { | |
7203 | int i = 0; | |
3cb03002 | 7204 | struct md_rdev *rdev; |
1da177e4 LT |
7205 | |
7206 | seq_printf(seq, "unused devices: "); | |
7207 | ||
159ec1fc | 7208 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
1da177e4 LT |
7209 | char b[BDEVNAME_SIZE]; |
7210 | i++; | |
7211 | seq_printf(seq, "%s ", | |
7212 | bdevname(rdev->bdev,b)); | |
7213 | } | |
7214 | if (!i) | |
7215 | seq_printf(seq, "<none>"); | |
7216 | ||
7217 | seq_printf(seq, "\n"); | |
7218 | } | |
7219 | ||
f7851be7 | 7220 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 7221 | { |
dd71cf6b N |
7222 | sector_t max_sectors, resync, res; |
7223 | unsigned long dt, db; | |
7224 | sector_t rt; | |
4588b42e N |
7225 | int scale; |
7226 | unsigned int per_milli; | |
1da177e4 | 7227 | |
c804cdec N |
7228 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
7229 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
dd71cf6b | 7230 | max_sectors = mddev->resync_max_sectors; |
1da177e4 | 7231 | else |
dd71cf6b | 7232 | max_sectors = mddev->dev_sectors; |
1da177e4 | 7233 | |
f7851be7 N |
7234 | resync = mddev->curr_resync; |
7235 | if (resync <= 3) { | |
7236 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) | |
7237 | /* Still cleaning up */ | |
7238 | resync = max_sectors; | |
7239 | } else | |
7240 | resync -= atomic_read(&mddev->recovery_active); | |
7241 | ||
7242 | if (resync == 0) { | |
7243 | if (mddev->recovery_cp < MaxSector) { | |
7244 | seq_printf(seq, "\tresync=PENDING"); | |
7245 | return 1; | |
7246 | } | |
7247 | return 0; | |
7248 | } | |
7249 | if (resync < 3) { | |
7250 | seq_printf(seq, "\tresync=DELAYED"); | |
7251 | return 1; | |
7252 | } | |
7253 | ||
403df478 | 7254 | WARN_ON(max_sectors == 0); |
4588b42e | 7255 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
dd71cf6b | 7256 | * in a sector_t, and (max_sectors>>scale) will fit in a |
4588b42e N |
7257 | * u32, as those are the requirements for sector_div. |
7258 | * Thus 'scale' must be at least 10 | |
7259 | */ | |
7260 | scale = 10; | |
7261 | if (sizeof(sector_t) > sizeof(unsigned long)) { | |
dd71cf6b | 7262 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
4588b42e N |
7263 | scale++; |
7264 | } | |
7265 | res = (resync>>scale)*1000; | |
dd71cf6b | 7266 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
4588b42e N |
7267 | |
7268 | per_milli = res; | |
1da177e4 | 7269 | { |
4588b42e | 7270 | int i, x = per_milli/50, y = 20-x; |
1da177e4 LT |
7271 | seq_printf(seq, "["); |
7272 | for (i = 0; i < x; i++) | |
7273 | seq_printf(seq, "="); | |
7274 | seq_printf(seq, ">"); | |
7275 | for (i = 0; i < y; i++) | |
7276 | seq_printf(seq, "."); | |
7277 | seq_printf(seq, "] "); | |
7278 | } | |
4588b42e | 7279 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
ccfcc3c1 N |
7280 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
7281 | "reshape" : | |
61df9d91 N |
7282 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
7283 | "check" : | |
7284 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? | |
7285 | "resync" : "recovery"))), | |
7286 | per_milli/10, per_milli % 10, | |
dd71cf6b N |
7287 | (unsigned long long) resync/2, |
7288 | (unsigned long long) max_sectors/2); | |
1da177e4 LT |
7289 | |
7290 | /* | |
1da177e4 LT |
7291 | * dt: time from mark until now |
7292 | * db: blocks written from mark until now | |
7293 | * rt: remaining time | |
dd71cf6b N |
7294 | * |
7295 | * rt is a sector_t, so could be 32bit or 64bit. | |
7296 | * So we divide before multiply in case it is 32bit and close | |
7297 | * to the limit. | |
25985edc | 7298 | * We scale the divisor (db) by 32 to avoid losing precision |
dd71cf6b N |
7299 | * near the end of resync when the number of remaining sectors |
7300 | * is close to 'db'. | |
7301 | * We then divide rt by 32 after multiplying by db to compensate. | |
7302 | * The '+1' avoids division by zero if db is very small. | |
1da177e4 LT |
7303 | */ |
7304 | dt = ((jiffies - mddev->resync_mark) / HZ); | |
7305 | if (!dt) dt++; | |
ff4e8d9a N |
7306 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
7307 | - mddev->resync_mark_cnt; | |
1da177e4 | 7308 | |
dd71cf6b N |
7309 | rt = max_sectors - resync; /* number of remaining sectors */ |
7310 | sector_div(rt, db/32+1); | |
7311 | rt *= dt; | |
7312 | rt >>= 5; | |
7313 | ||
7314 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, | |
7315 | ((unsigned long)rt % 60)/6); | |
1da177e4 | 7316 | |
ff4e8d9a | 7317 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
f7851be7 | 7318 | return 1; |
1da177e4 LT |
7319 | } |
7320 | ||
7321 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) | |
7322 | { | |
7323 | struct list_head *tmp; | |
7324 | loff_t l = *pos; | |
fd01b88c | 7325 | struct mddev *mddev; |
1da177e4 LT |
7326 | |
7327 | if (l >= 0x10000) | |
7328 | return NULL; | |
7329 | if (!l--) | |
7330 | /* header */ | |
7331 | return (void*)1; | |
7332 | ||
7333 | spin_lock(&all_mddevs_lock); | |
7334 | list_for_each(tmp,&all_mddevs) | |
7335 | if (!l--) { | |
fd01b88c | 7336 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
1da177e4 LT |
7337 | mddev_get(mddev); |
7338 | spin_unlock(&all_mddevs_lock); | |
7339 | return mddev; | |
7340 | } | |
7341 | spin_unlock(&all_mddevs_lock); | |
7342 | if (!l--) | |
7343 | return (void*)2;/* tail */ | |
7344 | return NULL; | |
7345 | } | |
7346 | ||
7347 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
7348 | { | |
7349 | struct list_head *tmp; | |
fd01b88c | 7350 | struct mddev *next_mddev, *mddev = v; |
f72ffdd6 | 7351 | |
1da177e4 LT |
7352 | ++*pos; |
7353 | if (v == (void*)2) | |
7354 | return NULL; | |
7355 | ||
7356 | spin_lock(&all_mddevs_lock); | |
7357 | if (v == (void*)1) | |
7358 | tmp = all_mddevs.next; | |
7359 | else | |
7360 | tmp = mddev->all_mddevs.next; | |
7361 | if (tmp != &all_mddevs) | |
fd01b88c | 7362 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
1da177e4 LT |
7363 | else { |
7364 | next_mddev = (void*)2; | |
7365 | *pos = 0x10000; | |
f72ffdd6 | 7366 | } |
1da177e4 LT |
7367 | spin_unlock(&all_mddevs_lock); |
7368 | ||
7369 | if (v != (void*)1) | |
7370 | mddev_put(mddev); | |
7371 | return next_mddev; | |
7372 | ||
7373 | } | |
7374 | ||
7375 | static void md_seq_stop(struct seq_file *seq, void *v) | |
7376 | { | |
fd01b88c | 7377 | struct mddev *mddev = v; |
1da177e4 LT |
7378 | |
7379 | if (mddev && v != (void*)1 && v != (void*)2) | |
7380 | mddev_put(mddev); | |
7381 | } | |
7382 | ||
7383 | static int md_seq_show(struct seq_file *seq, void *v) | |
7384 | { | |
fd01b88c | 7385 | struct mddev *mddev = v; |
dd8ac336 | 7386 | sector_t sectors; |
3cb03002 | 7387 | struct md_rdev *rdev; |
1da177e4 LT |
7388 | |
7389 | if (v == (void*)1) { | |
84fc4b56 | 7390 | struct md_personality *pers; |
1da177e4 LT |
7391 | seq_printf(seq, "Personalities : "); |
7392 | spin_lock(&pers_lock); | |
2604b703 N |
7393 | list_for_each_entry(pers, &pers_list, list) |
7394 | seq_printf(seq, "[%s] ", pers->name); | |
1da177e4 LT |
7395 | |
7396 | spin_unlock(&pers_lock); | |
7397 | seq_printf(seq, "\n"); | |
f1514638 | 7398 | seq->poll_event = atomic_read(&md_event_count); |
1da177e4 LT |
7399 | return 0; |
7400 | } | |
7401 | if (v == (void*)2) { | |
7402 | status_unused(seq); | |
7403 | return 0; | |
7404 | } | |
7405 | ||
36d091f4 | 7406 | spin_lock(&mddev->lock); |
1da177e4 LT |
7407 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
7408 | seq_printf(seq, "%s : %sactive", mdname(mddev), | |
7409 | mddev->pers ? "" : "in"); | |
7410 | if (mddev->pers) { | |
f91de92e | 7411 | if (mddev->ro==1) |
1da177e4 | 7412 | seq_printf(seq, " (read-only)"); |
f91de92e | 7413 | if (mddev->ro==2) |
52720ae7 | 7414 | seq_printf(seq, " (auto-read-only)"); |
1da177e4 LT |
7415 | seq_printf(seq, " %s", mddev->pers->name); |
7416 | } | |
7417 | ||
dd8ac336 | 7418 | sectors = 0; |
f97fcad3 N |
7419 | rcu_read_lock(); |
7420 | rdev_for_each_rcu(rdev, mddev) { | |
1da177e4 LT |
7421 | char b[BDEVNAME_SIZE]; |
7422 | seq_printf(seq, " %s[%d]", | |
7423 | bdevname(rdev->bdev,b), rdev->desc_nr); | |
8ddf9efe N |
7424 | if (test_bit(WriteMostly, &rdev->flags)) |
7425 | seq_printf(seq, "(W)"); | |
9efdca16 SL |
7426 | if (test_bit(Journal, &rdev->flags)) |
7427 | seq_printf(seq, "(J)"); | |
b2d444d7 | 7428 | if (test_bit(Faulty, &rdev->flags)) { |
1da177e4 LT |
7429 | seq_printf(seq, "(F)"); |
7430 | continue; | |
2d78f8c4 N |
7431 | } |
7432 | if (rdev->raid_disk < 0) | |
b325a32e | 7433 | seq_printf(seq, "(S)"); /* spare */ |
2d78f8c4 N |
7434 | if (test_bit(Replacement, &rdev->flags)) |
7435 | seq_printf(seq, "(R)"); | |
dd8ac336 | 7436 | sectors += rdev->sectors; |
1da177e4 | 7437 | } |
f97fcad3 | 7438 | rcu_read_unlock(); |
1da177e4 LT |
7439 | |
7440 | if (!list_empty(&mddev->disks)) { | |
7441 | if (mddev->pers) | |
7442 | seq_printf(seq, "\n %llu blocks", | |
f233ea5c AN |
7443 | (unsigned long long) |
7444 | mddev->array_sectors / 2); | |
1da177e4 LT |
7445 | else |
7446 | seq_printf(seq, "\n %llu blocks", | |
dd8ac336 | 7447 | (unsigned long long)sectors / 2); |
1da177e4 | 7448 | } |
1cd6bf19 N |
7449 | if (mddev->persistent) { |
7450 | if (mddev->major_version != 0 || | |
7451 | mddev->minor_version != 90) { | |
7452 | seq_printf(seq," super %d.%d", | |
7453 | mddev->major_version, | |
7454 | mddev->minor_version); | |
7455 | } | |
e691063a N |
7456 | } else if (mddev->external) |
7457 | seq_printf(seq, " super external:%s", | |
7458 | mddev->metadata_type); | |
7459 | else | |
1cd6bf19 | 7460 | seq_printf(seq, " super non-persistent"); |
1da177e4 LT |
7461 | |
7462 | if (mddev->pers) { | |
d710e138 | 7463 | mddev->pers->status(seq, mddev); |
f72ffdd6 | 7464 | seq_printf(seq, "\n "); |
8e1b39d6 | 7465 | if (mddev->pers->sync_request) { |
f7851be7 | 7466 | if (status_resync(seq, mddev)) |
8e1b39d6 | 7467 | seq_printf(seq, "\n "); |
8e1b39d6 | 7468 | } |
32a7627c N |
7469 | } else |
7470 | seq_printf(seq, "\n "); | |
7471 | ||
57148964 | 7472 | bitmap_status(seq, mddev->bitmap); |
1da177e4 LT |
7473 | |
7474 | seq_printf(seq, "\n"); | |
7475 | } | |
36d091f4 | 7476 | spin_unlock(&mddev->lock); |
f72ffdd6 | 7477 | |
1da177e4 LT |
7478 | return 0; |
7479 | } | |
7480 | ||
110518bc | 7481 | static const struct seq_operations md_seq_ops = { |
1da177e4 LT |
7482 | .start = md_seq_start, |
7483 | .next = md_seq_next, | |
7484 | .stop = md_seq_stop, | |
7485 | .show = md_seq_show, | |
7486 | }; | |
7487 | ||
7488 | static int md_seq_open(struct inode *inode, struct file *file) | |
7489 | { | |
f1514638 | 7490 | struct seq_file *seq; |
1da177e4 LT |
7491 | int error; |
7492 | ||
7493 | error = seq_open(file, &md_seq_ops); | |
d7603b7e | 7494 | if (error) |
f1514638 KS |
7495 | return error; |
7496 | ||
7497 | seq = file->private_data; | |
7498 | seq->poll_event = atomic_read(&md_event_count); | |
1da177e4 LT |
7499 | return error; |
7500 | } | |
7501 | ||
e2f23b60 | 7502 | static int md_unloading; |
d7603b7e N |
7503 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
7504 | { | |
f1514638 | 7505 | struct seq_file *seq = filp->private_data; |
d7603b7e N |
7506 | int mask; |
7507 | ||
e2f23b60 | 7508 | if (md_unloading) |
7d7e64f2 | 7509 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
d7603b7e N |
7510 | poll_wait(filp, &md_event_waiters, wait); |
7511 | ||
7512 | /* always allow read */ | |
7513 | mask = POLLIN | POLLRDNORM; | |
7514 | ||
f1514638 | 7515 | if (seq->poll_event != atomic_read(&md_event_count)) |
d7603b7e N |
7516 | mask |= POLLERR | POLLPRI; |
7517 | return mask; | |
7518 | } | |
7519 | ||
fa027c2a | 7520 | static const struct file_operations md_seq_fops = { |
e24650c2 | 7521 | .owner = THIS_MODULE, |
1da177e4 LT |
7522 | .open = md_seq_open, |
7523 | .read = seq_read, | |
7524 | .llseek = seq_lseek, | |
c3f94b40 | 7525 | .release = seq_release_private, |
d7603b7e | 7526 | .poll = mdstat_poll, |
1da177e4 LT |
7527 | }; |
7528 | ||
84fc4b56 | 7529 | int register_md_personality(struct md_personality *p) |
1da177e4 | 7530 | { |
50bd3774 CY |
7531 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
7532 | p->name, p->level); | |
1da177e4 | 7533 | spin_lock(&pers_lock); |
2604b703 | 7534 | list_add_tail(&p->list, &pers_list); |
1da177e4 LT |
7535 | spin_unlock(&pers_lock); |
7536 | return 0; | |
7537 | } | |
6c144d31 | 7538 | EXPORT_SYMBOL(register_md_personality); |
1da177e4 | 7539 | |
84fc4b56 | 7540 | int unregister_md_personality(struct md_personality *p) |
1da177e4 | 7541 | { |
2604b703 | 7542 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
1da177e4 | 7543 | spin_lock(&pers_lock); |
2604b703 | 7544 | list_del_init(&p->list); |
1da177e4 LT |
7545 | spin_unlock(&pers_lock); |
7546 | return 0; | |
7547 | } | |
6c144d31 | 7548 | EXPORT_SYMBOL(unregister_md_personality); |
1da177e4 | 7549 | |
6022e75b N |
7550 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
7551 | struct module *module) | |
edb39c9d | 7552 | { |
6022e75b | 7553 | int ret = 0; |
edb39c9d | 7554 | spin_lock(&pers_lock); |
6022e75b N |
7555 | if (md_cluster_ops != NULL) |
7556 | ret = -EALREADY; | |
7557 | else { | |
7558 | md_cluster_ops = ops; | |
7559 | md_cluster_mod = module; | |
7560 | } | |
edb39c9d | 7561 | spin_unlock(&pers_lock); |
6022e75b | 7562 | return ret; |
edb39c9d GR |
7563 | } |
7564 | EXPORT_SYMBOL(register_md_cluster_operations); | |
7565 | ||
7566 | int unregister_md_cluster_operations(void) | |
7567 | { | |
7568 | spin_lock(&pers_lock); | |
7569 | md_cluster_ops = NULL; | |
7570 | spin_unlock(&pers_lock); | |
7571 | return 0; | |
7572 | } | |
7573 | EXPORT_SYMBOL(unregister_md_cluster_operations); | |
7574 | ||
7575 | int md_setup_cluster(struct mddev *mddev, int nodes) | |
7576 | { | |
7577 | int err; | |
7578 | ||
7579 | err = request_module("md-cluster"); | |
7580 | if (err) { | |
7581 | pr_err("md-cluster module not found.\n"); | |
b0c26a79 | 7582 | return -ENOENT; |
edb39c9d GR |
7583 | } |
7584 | ||
7585 | spin_lock(&pers_lock); | |
7586 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { | |
7587 | spin_unlock(&pers_lock); | |
7588 | return -ENOENT; | |
7589 | } | |
7590 | spin_unlock(&pers_lock); | |
7591 | ||
cf921cc1 | 7592 | return md_cluster_ops->join(mddev, nodes); |
edb39c9d GR |
7593 | } |
7594 | ||
7595 | void md_cluster_stop(struct mddev *mddev) | |
7596 | { | |
c4ce867f GR |
7597 | if (!md_cluster_ops) |
7598 | return; | |
edb39c9d GR |
7599 | md_cluster_ops->leave(mddev); |
7600 | module_put(md_cluster_mod); | |
7601 | } | |
7602 | ||
fd01b88c | 7603 | static int is_mddev_idle(struct mddev *mddev, int init) |
1da177e4 | 7604 | { |
f72ffdd6 | 7605 | struct md_rdev *rdev; |
1da177e4 | 7606 | int idle; |
eea1bf38 | 7607 | int curr_events; |
1da177e4 LT |
7608 | |
7609 | idle = 1; | |
4b80991c N |
7610 | rcu_read_lock(); |
7611 | rdev_for_each_rcu(rdev, mddev) { | |
1da177e4 | 7612 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
eea1bf38 N |
7613 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
7614 | (int)part_stat_read(&disk->part0, sectors[1]) - | |
7615 | atomic_read(&disk->sync_io); | |
713f6ab1 N |
7616 | /* sync IO will cause sync_io to increase before the disk_stats |
7617 | * as sync_io is counted when a request starts, and | |
7618 | * disk_stats is counted when it completes. | |
7619 | * So resync activity will cause curr_events to be smaller than | |
7620 | * when there was no such activity. | |
7621 | * non-sync IO will cause disk_stat to increase without | |
7622 | * increasing sync_io so curr_events will (eventually) | |
7623 | * be larger than it was before. Once it becomes | |
7624 | * substantially larger, the test below will cause | |
7625 | * the array to appear non-idle, and resync will slow | |
7626 | * down. | |
7627 | * If there is a lot of outstanding resync activity when | |
7628 | * we set last_event to curr_events, then all that activity | |
7629 | * completing might cause the array to appear non-idle | |
7630 | * and resync will be slowed down even though there might | |
7631 | * not have been non-resync activity. This will only | |
7632 | * happen once though. 'last_events' will soon reflect | |
7633 | * the state where there is little or no outstanding | |
7634 | * resync requests, and further resync activity will | |
7635 | * always make curr_events less than last_events. | |
c0e48521 | 7636 | * |
1da177e4 | 7637 | */ |
eea1bf38 | 7638 | if (init || curr_events - rdev->last_events > 64) { |
1da177e4 LT |
7639 | rdev->last_events = curr_events; |
7640 | idle = 0; | |
7641 | } | |
7642 | } | |
4b80991c | 7643 | rcu_read_unlock(); |
1da177e4 LT |
7644 | return idle; |
7645 | } | |
7646 | ||
fd01b88c | 7647 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
1da177e4 LT |
7648 | { |
7649 | /* another "blocks" (512byte) blocks have been synced */ | |
7650 | atomic_sub(blocks, &mddev->recovery_active); | |
7651 | wake_up(&mddev->recovery_wait); | |
7652 | if (!ok) { | |
dfc70645 | 7653 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
0a19caab | 7654 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
1da177e4 LT |
7655 | md_wakeup_thread(mddev->thread); |
7656 | // stop recovery, signal do_sync .... | |
7657 | } | |
7658 | } | |
6c144d31 | 7659 | EXPORT_SYMBOL(md_done_sync); |
1da177e4 | 7660 | |
06d91a5f N |
7661 | /* md_write_start(mddev, bi) |
7662 | * If we need to update some array metadata (e.g. 'active' flag | |
3d310eb7 N |
7663 | * in superblock) before writing, schedule a superblock update |
7664 | * and wait for it to complete. | |
06d91a5f | 7665 | */ |
fd01b88c | 7666 | void md_write_start(struct mddev *mddev, struct bio *bi) |
1da177e4 | 7667 | { |
0fd62b86 | 7668 | int did_change = 0; |
06d91a5f | 7669 | if (bio_data_dir(bi) != WRITE) |
3d310eb7 | 7670 | return; |
06d91a5f | 7671 | |
f91de92e N |
7672 | BUG_ON(mddev->ro == 1); |
7673 | if (mddev->ro == 2) { | |
7674 | /* need to switch to read/write */ | |
7675 | mddev->ro = 0; | |
7676 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
7677 | md_wakeup_thread(mddev->thread); | |
25156198 | 7678 | md_wakeup_thread(mddev->sync_thread); |
0fd62b86 | 7679 | did_change = 1; |
f91de92e | 7680 | } |
06d91a5f | 7681 | atomic_inc(&mddev->writes_pending); |
31a59e34 N |
7682 | if (mddev->safemode == 1) |
7683 | mddev->safemode = 0; | |
06d91a5f | 7684 | if (mddev->in_sync) { |
85572d7c | 7685 | spin_lock(&mddev->lock); |
3d310eb7 N |
7686 | if (mddev->in_sync) { |
7687 | mddev->in_sync = 0; | |
850b2b42 | 7688 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
070dc6dd | 7689 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
3d310eb7 | 7690 | md_wakeup_thread(mddev->thread); |
0fd62b86 | 7691 | did_change = 1; |
3d310eb7 | 7692 | } |
85572d7c | 7693 | spin_unlock(&mddev->lock); |
06d91a5f | 7694 | } |
0fd62b86 | 7695 | if (did_change) |
00bcb4ac | 7696 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
09a44cc1 | 7697 | wait_event(mddev->sb_wait, |
09a44cc1 | 7698 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
1da177e4 | 7699 | } |
6c144d31 | 7700 | EXPORT_SYMBOL(md_write_start); |
1da177e4 | 7701 | |
fd01b88c | 7702 | void md_write_end(struct mddev *mddev) |
1da177e4 LT |
7703 | { |
7704 | if (atomic_dec_and_test(&mddev->writes_pending)) { | |
7705 | if (mddev->safemode == 2) | |
7706 | md_wakeup_thread(mddev->thread); | |
16f17b39 | 7707 | else if (mddev->safemode_delay) |
1da177e4 LT |
7708 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
7709 | } | |
7710 | } | |
6c144d31 | 7711 | EXPORT_SYMBOL(md_write_end); |
1da177e4 | 7712 | |
2a2275d6 N |
7713 | /* md_allow_write(mddev) |
7714 | * Calling this ensures that the array is marked 'active' so that writes | |
7715 | * may proceed without blocking. It is important to call this before | |
7716 | * attempting a GFP_KERNEL allocation while holding the mddev lock. | |
7717 | * Must be called with mddev_lock held. | |
b5470dc5 | 7718 | * |
abf3508d | 7719 | * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock |
b5470dc5 | 7720 | * is dropped, so return -EAGAIN after notifying userspace. |
2a2275d6 | 7721 | */ |
fd01b88c | 7722 | int md_allow_write(struct mddev *mddev) |
2a2275d6 N |
7723 | { |
7724 | if (!mddev->pers) | |
b5470dc5 | 7725 | return 0; |
2a2275d6 | 7726 | if (mddev->ro) |
b5470dc5 | 7727 | return 0; |
1a0fd497 | 7728 | if (!mddev->pers->sync_request) |
b5470dc5 | 7729 | return 0; |
2a2275d6 | 7730 | |
85572d7c | 7731 | spin_lock(&mddev->lock); |
2a2275d6 N |
7732 | if (mddev->in_sync) { |
7733 | mddev->in_sync = 0; | |
7734 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); | |
070dc6dd | 7735 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
2a2275d6 N |
7736 | if (mddev->safemode_delay && |
7737 | mddev->safemode == 0) | |
7738 | mddev->safemode = 1; | |
85572d7c | 7739 | spin_unlock(&mddev->lock); |
2a2275d6 | 7740 | md_update_sb(mddev, 0); |
00bcb4ac | 7741 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
2a2275d6 | 7742 | } else |
85572d7c | 7743 | spin_unlock(&mddev->lock); |
b5470dc5 | 7744 | |
070dc6dd | 7745 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
b5470dc5 DW |
7746 | return -EAGAIN; |
7747 | else | |
7748 | return 0; | |
2a2275d6 N |
7749 | } |
7750 | EXPORT_SYMBOL_GPL(md_allow_write); | |
7751 | ||
1da177e4 LT |
7752 | #define SYNC_MARKS 10 |
7753 | #define SYNC_MARK_STEP (3*HZ) | |
54f89341 | 7754 | #define UPDATE_FREQUENCY (5*60*HZ) |
4ed8731d | 7755 | void md_do_sync(struct md_thread *thread) |
1da177e4 | 7756 | { |
4ed8731d | 7757 | struct mddev *mddev = thread->mddev; |
fd01b88c | 7758 | struct mddev *mddev2; |
1da177e4 LT |
7759 | unsigned int currspeed = 0, |
7760 | window; | |
ac7e50a3 | 7761 | sector_t max_sectors,j, io_sectors, recovery_done; |
1da177e4 | 7762 | unsigned long mark[SYNC_MARKS]; |
54f89341 | 7763 | unsigned long update_time; |
1da177e4 LT |
7764 | sector_t mark_cnt[SYNC_MARKS]; |
7765 | int last_mark,m; | |
7766 | struct list_head *tmp; | |
7767 | sector_t last_check; | |
57afd89f | 7768 | int skipped = 0; |
3cb03002 | 7769 | struct md_rdev *rdev; |
c4a39551 | 7770 | char *desc, *action = NULL; |
7c2c57c9 | 7771 | struct blk_plug plug; |
c186b128 | 7772 | bool cluster_resync_finished = false; |
1da177e4 LT |
7773 | |
7774 | /* just incase thread restarts... */ | |
7775 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) | |
7776 | return; | |
3991b31e N |
7777 | if (mddev->ro) {/* never try to sync a read-only array */ |
7778 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
5fd6c1dc | 7779 | return; |
3991b31e | 7780 | } |
1da177e4 | 7781 | |
61df9d91 | 7782 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
c4a39551 | 7783 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
61df9d91 | 7784 | desc = "data-check"; |
c4a39551 JB |
7785 | action = "check"; |
7786 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { | |
61df9d91 | 7787 | desc = "requested-resync"; |
c4a39551 JB |
7788 | action = "repair"; |
7789 | } else | |
61df9d91 N |
7790 | desc = "resync"; |
7791 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
7792 | desc = "reshape"; | |
7793 | else | |
7794 | desc = "recovery"; | |
7795 | ||
c4a39551 JB |
7796 | mddev->last_sync_action = action ?: desc; |
7797 | ||
1da177e4 LT |
7798 | /* we overload curr_resync somewhat here. |
7799 | * 0 == not engaged in resync at all | |
7800 | * 2 == checking that there is no conflict with another sync | |
7801 | * 1 == like 2, but have yielded to allow conflicting resync to | |
7802 | * commense | |
7803 | * other == active in resync - this many blocks | |
7804 | * | |
7805 | * Before starting a resync we must have set curr_resync to | |
7806 | * 2, and then checked that every "conflicting" array has curr_resync | |
7807 | * less than ours. When we find one that is the same or higher | |
7808 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync | |
7809 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). | |
7810 | * This will mean we have to start checking from the beginning again. | |
7811 | * | |
7812 | */ | |
7813 | ||
7814 | do { | |
7815 | mddev->curr_resync = 2; | |
7816 | ||
7817 | try_again: | |
404e4b43 | 7818 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
1da177e4 | 7819 | goto skip; |
29ac4aa3 | 7820 | for_each_mddev(mddev2, tmp) { |
1da177e4 LT |
7821 | if (mddev2 == mddev) |
7822 | continue; | |
90b08710 BS |
7823 | if (!mddev->parallel_resync |
7824 | && mddev2->curr_resync | |
7825 | && match_mddev_units(mddev, mddev2)) { | |
1da177e4 LT |
7826 | DEFINE_WAIT(wq); |
7827 | if (mddev < mddev2 && mddev->curr_resync == 2) { | |
7828 | /* arbitrarily yield */ | |
7829 | mddev->curr_resync = 1; | |
7830 | wake_up(&resync_wait); | |
7831 | } | |
7832 | if (mddev > mddev2 && mddev->curr_resync == 1) | |
7833 | /* no need to wait here, we can wait the next | |
7834 | * time 'round when curr_resync == 2 | |
7835 | */ | |
7836 | continue; | |
9744197c N |
7837 | /* We need to wait 'interruptible' so as not to |
7838 | * contribute to the load average, and not to | |
7839 | * be caught by 'softlockup' | |
7840 | */ | |
7841 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); | |
c91abf5a | 7842 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
8712e553 | 7843 | mddev2->curr_resync >= mddev->curr_resync) { |
61df9d91 N |
7844 | printk(KERN_INFO "md: delaying %s of %s" |
7845 | " until %s has finished (they" | |
1da177e4 | 7846 | " share one or more physical units)\n", |
61df9d91 | 7847 | desc, mdname(mddev), mdname(mddev2)); |
1da177e4 | 7848 | mddev_put(mddev2); |
9744197c N |
7849 | if (signal_pending(current)) |
7850 | flush_signals(current); | |
1da177e4 LT |
7851 | schedule(); |
7852 | finish_wait(&resync_wait, &wq); | |
7853 | goto try_again; | |
7854 | } | |
7855 | finish_wait(&resync_wait, &wq); | |
7856 | } | |
7857 | } | |
7858 | } while (mddev->curr_resync < 2); | |
7859 | ||
5fd6c1dc | 7860 | j = 0; |
9d88883e | 7861 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
1da177e4 | 7862 | /* resync follows the size requested by the personality, |
57afd89f | 7863 | * which defaults to physical size, but can be virtual size |
1da177e4 LT |
7864 | */ |
7865 | max_sectors = mddev->resync_max_sectors; | |
7f7583d4 | 7866 | atomic64_set(&mddev->resync_mismatches, 0); |
5fd6c1dc | 7867 | /* we don't use the checkpoint if there's a bitmap */ |
5e96ee65 NB |
7868 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
7869 | j = mddev->resync_min; | |
7870 | else if (!mddev->bitmap) | |
5fd6c1dc | 7871 | j = mddev->recovery_cp; |
5e96ee65 | 7872 | |
ccfcc3c1 | 7873 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
c804cdec | 7874 | max_sectors = mddev->resync_max_sectors; |
5fd6c1dc | 7875 | else { |
1da177e4 | 7876 | /* recovery follows the physical size of devices */ |
58c0fed4 | 7877 | max_sectors = mddev->dev_sectors; |
5fd6c1dc | 7878 | j = MaxSector; |
4e59ca7d | 7879 | rcu_read_lock(); |
dafb20fa | 7880 | rdev_for_each_rcu(rdev, mddev) |
5fd6c1dc | 7881 | if (rdev->raid_disk >= 0 && |
f2076e7d | 7882 | !test_bit(Journal, &rdev->flags) && |
5fd6c1dc N |
7883 | !test_bit(Faulty, &rdev->flags) && |
7884 | !test_bit(In_sync, &rdev->flags) && | |
7885 | rdev->recovery_offset < j) | |
7886 | j = rdev->recovery_offset; | |
4e59ca7d | 7887 | rcu_read_unlock(); |
133d4527 N |
7888 | |
7889 | /* If there is a bitmap, we need to make sure all | |
7890 | * writes that started before we added a spare | |
7891 | * complete before we start doing a recovery. | |
7892 | * Otherwise the write might complete and (via | |
7893 | * bitmap_endwrite) set a bit in the bitmap after the | |
7894 | * recovery has checked that bit and skipped that | |
7895 | * region. | |
7896 | */ | |
7897 | if (mddev->bitmap) { | |
7898 | mddev->pers->quiesce(mddev, 1); | |
7899 | mddev->pers->quiesce(mddev, 0); | |
7900 | } | |
5fd6c1dc | 7901 | } |
1da177e4 | 7902 | |
61df9d91 N |
7903 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
7904 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" | |
7905 | " %d KB/sec/disk.\n", speed_min(mddev)); | |
338cec32 | 7906 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
61df9d91 N |
7907 | "(but not more than %d KB/sec) for %s.\n", |
7908 | speed_max(mddev), desc); | |
1da177e4 | 7909 | |
eea1bf38 | 7910 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
5fd6c1dc | 7911 | |
57afd89f | 7912 | io_sectors = 0; |
1da177e4 LT |
7913 | for (m = 0; m < SYNC_MARKS; m++) { |
7914 | mark[m] = jiffies; | |
57afd89f | 7915 | mark_cnt[m] = io_sectors; |
1da177e4 LT |
7916 | } |
7917 | last_mark = 0; | |
7918 | mddev->resync_mark = mark[last_mark]; | |
7919 | mddev->resync_mark_cnt = mark_cnt[last_mark]; | |
7920 | ||
7921 | /* | |
7922 | * Tune reconstruction: | |
7923 | */ | |
7924 | window = 32*(PAGE_SIZE/512); | |
ac42450c JB |
7925 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
7926 | window/2, (unsigned long long)max_sectors/2); | |
1da177e4 LT |
7927 | |
7928 | atomic_set(&mddev->recovery_active, 0); | |
1da177e4 LT |
7929 | last_check = 0; |
7930 | ||
7931 | if (j>2) { | |
c91abf5a | 7932 | printk(KERN_INFO |
61df9d91 N |
7933 | "md: resuming %s of %s from checkpoint.\n", |
7934 | desc, mdname(mddev)); | |
1da177e4 | 7935 | mddev->curr_resync = j; |
72f36d59 N |
7936 | } else |
7937 | mddev->curr_resync = 3; /* no longer delayed */ | |
75d3da43 | 7938 | mddev->curr_resync_completed = j; |
72f36d59 N |
7939 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
7940 | md_new_event(mddev); | |
54f89341 | 7941 | update_time = jiffies; |
1da177e4 | 7942 | |
7c2c57c9 | 7943 | blk_start_plug(&plug); |
1da177e4 | 7944 | while (j < max_sectors) { |
57afd89f | 7945 | sector_t sectors; |
1da177e4 | 7946 | |
57afd89f | 7947 | skipped = 0; |
97e4f42d | 7948 | |
7a91ee1f N |
7949 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
7950 | ((mddev->curr_resync > mddev->curr_resync_completed && | |
7951 | (mddev->curr_resync - mddev->curr_resync_completed) | |
7952 | > (max_sectors >> 4)) || | |
54f89341 | 7953 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
7a91ee1f | 7954 | (j - mddev->curr_resync_completed)*2 |
c5e19d90 N |
7955 | >= mddev->resync_max - mddev->curr_resync_completed || |
7956 | mddev->curr_resync_completed > mddev->resync_max | |
7a91ee1f | 7957 | )) { |
97e4f42d | 7958 | /* time to update curr_resync_completed */ |
97e4f42d N |
7959 | wait_event(mddev->recovery_wait, |
7960 | atomic_read(&mddev->recovery_active) == 0); | |
75d3da43 | 7961 | mddev->curr_resync_completed = j; |
35d78c66 | 7962 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
7963 | j > mddev->recovery_cp) | |
7964 | mddev->recovery_cp = j; | |
54f89341 | 7965 | update_time = jiffies; |
070dc6dd | 7966 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
acb180b0 | 7967 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
97e4f42d | 7968 | } |
acb180b0 | 7969 | |
c91abf5a N |
7970 | while (j >= mddev->resync_max && |
7971 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
e62e58a5 N |
7972 | /* As this condition is controlled by user-space, |
7973 | * we can block indefinitely, so use '_interruptible' | |
7974 | * to avoid triggering warnings. | |
7975 | */ | |
7976 | flush_signals(current); /* just in case */ | |
7977 | wait_event_interruptible(mddev->recovery_wait, | |
7978 | mddev->resync_max > j | |
c91abf5a N |
7979 | || test_bit(MD_RECOVERY_INTR, |
7980 | &mddev->recovery)); | |
e62e58a5 | 7981 | } |
acb180b0 | 7982 | |
c91abf5a N |
7983 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
7984 | break; | |
acb180b0 | 7985 | |
09314799 | 7986 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
57afd89f | 7987 | if (sectors == 0) { |
dfc70645 | 7988 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
c91abf5a | 7989 | break; |
1da177e4 | 7990 | } |
57afd89f N |
7991 | |
7992 | if (!skipped) { /* actual IO requested */ | |
7993 | io_sectors += sectors; | |
7994 | atomic_add(sectors, &mddev->recovery_active); | |
7995 | } | |
7996 | ||
e875ecea N |
7997 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
7998 | break; | |
7999 | ||
1da177e4 | 8000 | j += sectors; |
5ed1df2e N |
8001 | if (j > max_sectors) |
8002 | /* when skipping, extra large numbers can be returned. */ | |
8003 | j = max_sectors; | |
72f36d59 N |
8004 | if (j > 2) |
8005 | mddev->curr_resync = j; | |
ff4e8d9a | 8006 | mddev->curr_mark_cnt = io_sectors; |
d7603b7e | 8007 | if (last_check == 0) |
e875ecea | 8008 | /* this is the earliest that rebuild will be |
d7603b7e N |
8009 | * visible in /proc/mdstat |
8010 | */ | |
8011 | md_new_event(mddev); | |
57afd89f N |
8012 | |
8013 | if (last_check + window > io_sectors || j == max_sectors) | |
1da177e4 LT |
8014 | continue; |
8015 | ||
57afd89f | 8016 | last_check = io_sectors; |
1da177e4 LT |
8017 | repeat: |
8018 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { | |
8019 | /* step marks */ | |
8020 | int next = (last_mark+1) % SYNC_MARKS; | |
8021 | ||
8022 | mddev->resync_mark = mark[next]; | |
8023 | mddev->resync_mark_cnt = mark_cnt[next]; | |
8024 | mark[next] = jiffies; | |
57afd89f | 8025 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
1da177e4 LT |
8026 | last_mark = next; |
8027 | } | |
8028 | ||
c91abf5a N |
8029 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
8030 | break; | |
1da177e4 LT |
8031 | |
8032 | /* | |
8033 | * this loop exits only if either when we are slower than | |
8034 | * the 'hard' speed limit, or the system was IO-idle for | |
8035 | * a jiffy. | |
8036 | * the system might be non-idle CPU-wise, but we only care | |
8037 | * about not overloading the IO subsystem. (things like an | |
8038 | * e2fsck being done on the RAID array should execute fast) | |
8039 | */ | |
1da177e4 LT |
8040 | cond_resched(); |
8041 | ||
ac7e50a3 XN |
8042 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
8043 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 | |
57afd89f | 8044 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
1da177e4 | 8045 | |
88202a0c | 8046 | if (currspeed > speed_min(mddev)) { |
ac8fa419 | 8047 | if (currspeed > speed_max(mddev)) { |
c0e48521 | 8048 | msleep(500); |
1da177e4 LT |
8049 | goto repeat; |
8050 | } | |
ac8fa419 N |
8051 | if (!is_mddev_idle(mddev, 0)) { |
8052 | /* | |
8053 | * Give other IO more of a chance. | |
8054 | * The faster the devices, the less we wait. | |
8055 | */ | |
8056 | wait_event(mddev->recovery_wait, | |
8057 | !atomic_read(&mddev->recovery_active)); | |
8058 | } | |
1da177e4 LT |
8059 | } |
8060 | } | |
c91abf5a N |
8061 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
8062 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) | |
8063 | ? "interrupted" : "done"); | |
1da177e4 LT |
8064 | /* |
8065 | * this also signals 'finished resyncing' to md_stop | |
8066 | */ | |
7c2c57c9 | 8067 | blk_finish_plug(&plug); |
1da177e4 LT |
8068 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
8069 | ||
5ed1df2e N |
8070 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
8071 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && | |
8072 | mddev->curr_resync > 2) { | |
8073 | mddev->curr_resync_completed = mddev->curr_resync; | |
8074 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); | |
8075 | } | |
c186b128 GR |
8076 | /* tell personality and other nodes that we are finished */ |
8077 | if (mddev_is_clustered(mddev)) { | |
8078 | md_cluster_ops->resync_finish(mddev); | |
8079 | cluster_resync_finished = true; | |
8080 | } | |
09314799 | 8081 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
1da177e4 | 8082 | |
dfc70645 | 8083 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
5fd6c1dc N |
8084 | mddev->curr_resync > 2) { |
8085 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | |
8086 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
8087 | if (mddev->curr_resync >= mddev->recovery_cp) { | |
8088 | printk(KERN_INFO | |
61df9d91 N |
8089 | "md: checkpointing %s of %s.\n", |
8090 | desc, mdname(mddev)); | |
0a19caab | 8091 | if (test_bit(MD_RECOVERY_ERROR, |
8092 | &mddev->recovery)) | |
8093 | mddev->recovery_cp = | |
8094 | mddev->curr_resync_completed; | |
8095 | else | |
8096 | mddev->recovery_cp = | |
8097 | mddev->curr_resync; | |
5fd6c1dc N |
8098 | } |
8099 | } else | |
8100 | mddev->recovery_cp = MaxSector; | |
8101 | } else { | |
8102 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) | |
8103 | mddev->curr_resync = MaxSector; | |
4e59ca7d | 8104 | rcu_read_lock(); |
dafb20fa | 8105 | rdev_for_each_rcu(rdev, mddev) |
5fd6c1dc | 8106 | if (rdev->raid_disk >= 0 && |
70fffd0b | 8107 | mddev->delta_disks >= 0 && |
f2076e7d | 8108 | !test_bit(Journal, &rdev->flags) && |
5fd6c1dc N |
8109 | !test_bit(Faulty, &rdev->flags) && |
8110 | !test_bit(In_sync, &rdev->flags) && | |
8111 | rdev->recovery_offset < mddev->curr_resync) | |
8112 | rdev->recovery_offset = mddev->curr_resync; | |
4e59ca7d | 8113 | rcu_read_unlock(); |
5fd6c1dc | 8114 | } |
1da177e4 | 8115 | } |
db91ff55 | 8116 | skip: |
17571284 | 8117 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
1da177e4 | 8118 | |
c186b128 GR |
8119 | if (mddev_is_clustered(mddev) && |
8120 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) && | |
8121 | !cluster_resync_finished) | |
8122 | md_cluster_ops->resync_finish(mddev); | |
8123 | ||
23da422b | 8124 | spin_lock(&mddev->lock); |
c07b70ad N |
8125 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
8126 | /* We completed so min/max setting can be forgotten if used. */ | |
8127 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) | |
8128 | mddev->resync_min = 0; | |
8129 | mddev->resync_max = MaxSector; | |
8130 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) | |
8131 | mddev->resync_min = mddev->curr_resync_completed; | |
f7851be7 | 8132 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
1da177e4 | 8133 | mddev->curr_resync = 0; |
23da422b N |
8134 | spin_unlock(&mddev->lock); |
8135 | ||
1da177e4 | 8136 | wake_up(&resync_wait); |
1da177e4 | 8137 | md_wakeup_thread(mddev->thread); |
c6207277 | 8138 | return; |
1da177e4 | 8139 | } |
29269553 | 8140 | EXPORT_SYMBOL_GPL(md_do_sync); |
1da177e4 | 8141 | |
746d3207 N |
8142 | static int remove_and_add_spares(struct mddev *mddev, |
8143 | struct md_rdev *this) | |
b4c4c7b8 | 8144 | { |
3cb03002 | 8145 | struct md_rdev *rdev; |
b4c4c7b8 | 8146 | int spares = 0; |
f2a371c5 | 8147 | int removed = 0; |
b4c4c7b8 | 8148 | |
dafb20fa | 8149 | rdev_for_each(rdev, mddev) |
746d3207 N |
8150 | if ((this == NULL || rdev == this) && |
8151 | rdev->raid_disk >= 0 && | |
6bfe0b49 | 8152 | !test_bit(Blocked, &rdev->flags) && |
b4c4c7b8 | 8153 | (test_bit(Faulty, &rdev->flags) || |
f2076e7d SL |
8154 | (!test_bit(In_sync, &rdev->flags) && |
8155 | !test_bit(Journal, &rdev->flags))) && | |
b4c4c7b8 N |
8156 | atomic_read(&rdev->nr_pending)==0) { |
8157 | if (mddev->pers->hot_remove_disk( | |
b8321b68 | 8158 | mddev, rdev) == 0) { |
36fad858 | 8159 | sysfs_unlink_rdev(mddev, rdev); |
b4c4c7b8 | 8160 | rdev->raid_disk = -1; |
f2a371c5 | 8161 | removed++; |
b4c4c7b8 N |
8162 | } |
8163 | } | |
90584fc9 JB |
8164 | if (removed && mddev->kobj.sd) |
8165 | sysfs_notify(&mddev->kobj, NULL, "degraded"); | |
b4c4c7b8 | 8166 | |
2910ff17 | 8167 | if (this && removed) |
746d3207 N |
8168 | goto no_add; |
8169 | ||
dafb20fa | 8170 | rdev_for_each(rdev, mddev) { |
2910ff17 GR |
8171 | if (this && this != rdev) |
8172 | continue; | |
dbb64f86 GR |
8173 | if (test_bit(Candidate, &rdev->flags)) |
8174 | continue; | |
7bfec5f3 N |
8175 | if (rdev->raid_disk >= 0 && |
8176 | !test_bit(In_sync, &rdev->flags) && | |
f2076e7d | 8177 | !test_bit(Journal, &rdev->flags) && |
7bfec5f3 N |
8178 | !test_bit(Faulty, &rdev->flags)) |
8179 | spares++; | |
7ceb17e8 N |
8180 | if (rdev->raid_disk >= 0) |
8181 | continue; | |
8182 | if (test_bit(Faulty, &rdev->flags)) | |
8183 | continue; | |
3069aa8d SL |
8184 | if (test_bit(Journal, &rdev->flags)) |
8185 | continue; | |
7ceb17e8 | 8186 | if (mddev->ro && |
8313b8e5 N |
8187 | ! (rdev->saved_raid_disk >= 0 && |
8188 | !test_bit(Bitmap_sync, &rdev->flags))) | |
7ceb17e8 N |
8189 | continue; |
8190 | ||
d01552a7 | 8191 | rdev->recovery_offset = 0; |
7ceb17e8 N |
8192 | if (mddev->pers-> |
8193 | hot_add_disk(mddev, rdev) == 0) { | |
8194 | if (sysfs_link_rdev(mddev, rdev)) | |
8195 | /* failure here is OK */; | |
8196 | spares++; | |
8197 | md_new_event(mddev); | |
8198 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
dfc70645 | 8199 | } |
b4c4c7b8 | 8200 | } |
746d3207 | 8201 | no_add: |
6dafab6b N |
8202 | if (removed) |
8203 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
b4c4c7b8 N |
8204 | return spares; |
8205 | } | |
7ebc0be7 | 8206 | |
ac05f256 N |
8207 | static void md_start_sync(struct work_struct *ws) |
8208 | { | |
8209 | struct mddev *mddev = container_of(ws, struct mddev, del_work); | |
c186b128 GR |
8210 | int ret = 0; |
8211 | ||
8212 | if (mddev_is_clustered(mddev)) { | |
8213 | ret = md_cluster_ops->resync_start(mddev); | |
8214 | if (ret) { | |
8215 | mddev->sync_thread = NULL; | |
8216 | goto out; | |
8217 | } | |
8218 | } | |
ac05f256 N |
8219 | |
8220 | mddev->sync_thread = md_register_thread(md_do_sync, | |
8221 | mddev, | |
8222 | "resync"); | |
c186b128 | 8223 | out: |
ac05f256 | 8224 | if (!mddev->sync_thread) { |
c186b128 GR |
8225 | if (!(mddev_is_clustered(mddev) && ret == -EAGAIN)) |
8226 | printk(KERN_ERR "%s: could not start resync" | |
8227 | " thread...\n", | |
8228 | mdname(mddev)); | |
ac05f256 N |
8229 | /* leave the spares where they are, it shouldn't hurt */ |
8230 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
8231 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
8232 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); | |
8233 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
8234 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
f851b60d | 8235 | wake_up(&resync_wait); |
ac05f256 N |
8236 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
8237 | &mddev->recovery)) | |
8238 | if (mddev->sysfs_action) | |
8239 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
8240 | } else | |
8241 | md_wakeup_thread(mddev->sync_thread); | |
8242 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
8243 | md_new_event(mddev); | |
8244 | } | |
8245 | ||
1da177e4 LT |
8246 | /* |
8247 | * This routine is regularly called by all per-raid-array threads to | |
8248 | * deal with generic issues like resync and super-block update. | |
8249 | * Raid personalities that don't have a thread (linear/raid0) do not | |
8250 | * need this as they never do any recovery or update the superblock. | |
8251 | * | |
8252 | * It does not do any resync itself, but rather "forks" off other threads | |
8253 | * to do that as needed. | |
8254 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in | |
8255 | * "->recovery" and create a thread at ->sync_thread. | |
dfc70645 | 8256 | * When the thread finishes it sets MD_RECOVERY_DONE |
1da177e4 LT |
8257 | * and wakeups up this thread which will reap the thread and finish up. |
8258 | * This thread also removes any faulty devices (with nr_pending == 0). | |
8259 | * | |
8260 | * The overall approach is: | |
8261 | * 1/ if the superblock needs updating, update it. | |
8262 | * 2/ If a recovery thread is running, don't do anything else. | |
8263 | * 3/ If recovery has finished, clean up, possibly marking spares active. | |
8264 | * 4/ If there are any faulty devices, remove them. | |
8265 | * 5/ If array is degraded, try to add spares devices | |
8266 | * 6/ If array has spares or is not in-sync, start a resync thread. | |
8267 | */ | |
fd01b88c | 8268 | void md_check_recovery(struct mddev *mddev) |
1da177e4 | 8269 | { |
68866e42 JB |
8270 | if (mddev->suspended) |
8271 | return; | |
8272 | ||
5f40402d | 8273 | if (mddev->bitmap) |
aa5cbd10 | 8274 | bitmap_daemon_work(mddev); |
1da177e4 | 8275 | |
fca4d848 | 8276 | if (signal_pending(current)) { |
31a59e34 | 8277 | if (mddev->pers->sync_request && !mddev->external) { |
fca4d848 N |
8278 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
8279 | mdname(mddev)); | |
8280 | mddev->safemode = 2; | |
8281 | } | |
8282 | flush_signals(current); | |
8283 | } | |
8284 | ||
c89a8eee N |
8285 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
8286 | return; | |
1da177e4 | 8287 | if ( ! ( |
142d44c3 | 8288 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
1da177e4 | 8289 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
fca4d848 | 8290 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
15858fa5 | 8291 | test_bit(MD_RELOAD_SB, &mddev->flags) || |
31a59e34 | 8292 | (mddev->external == 0 && mddev->safemode == 1) || |
fca4d848 N |
8293 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
8294 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) | |
1da177e4 LT |
8295 | )) |
8296 | return; | |
fca4d848 | 8297 | |
df5b89b3 | 8298 | if (mddev_trylock(mddev)) { |
b4c4c7b8 | 8299 | int spares = 0; |
fca4d848 | 8300 | |
c89a8eee | 8301 | if (mddev->ro) { |
ab16bfc7 NB |
8302 | struct md_rdev *rdev; |
8303 | if (!mddev->external && mddev->in_sync) | |
8304 | /* 'Blocked' flag not needed as failed devices | |
8305 | * will be recorded if array switched to read/write. | |
8306 | * Leaving it set will prevent the device | |
8307 | * from being removed. | |
8308 | */ | |
8309 | rdev_for_each(rdev, mddev) | |
8310 | clear_bit(Blocked, &rdev->flags); | |
7ceb17e8 N |
8311 | /* On a read-only array we can: |
8312 | * - remove failed devices | |
8313 | * - add already-in_sync devices if the array itself | |
8314 | * is in-sync. | |
8315 | * As we only add devices that are already in-sync, | |
8316 | * we can activate the spares immediately. | |
c89a8eee | 8317 | */ |
7ceb17e8 | 8318 | remove_and_add_spares(mddev, NULL); |
8313b8e5 N |
8319 | /* There is no thread, but we need to call |
8320 | * ->spare_active and clear saved_raid_disk | |
8321 | */ | |
2ac295a5 | 8322 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
8313b8e5 | 8323 | md_reap_sync_thread(mddev); |
a4a3d26d | 8324 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
8313b8e5 | 8325 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
d4929add | 8326 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
c89a8eee N |
8327 | goto unlock; |
8328 | } | |
8329 | ||
659b254f GJ |
8330 | if (mddev_is_clustered(mddev)) { |
8331 | struct md_rdev *rdev; | |
8332 | /* kick the device if another node issued a | |
8333 | * remove disk. | |
8334 | */ | |
8335 | rdev_for_each(rdev, mddev) { | |
8336 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && | |
8337 | rdev->raid_disk < 0) | |
8338 | md_kick_rdev_from_array(rdev); | |
8339 | } | |
15858fa5 GJ |
8340 | |
8341 | if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags)) | |
8342 | md_reload_sb(mddev, mddev->good_device_nr); | |
659b254f GJ |
8343 | } |
8344 | ||
31a59e34 | 8345 | if (!mddev->external) { |
0fd62b86 | 8346 | int did_change = 0; |
85572d7c | 8347 | spin_lock(&mddev->lock); |
31a59e34 N |
8348 | if (mddev->safemode && |
8349 | !atomic_read(&mddev->writes_pending) && | |
8350 | !mddev->in_sync && | |
8351 | mddev->recovery_cp == MaxSector) { | |
8352 | mddev->in_sync = 1; | |
0fd62b86 | 8353 | did_change = 1; |
070dc6dd | 8354 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
31a59e34 N |
8355 | } |
8356 | if (mddev->safemode == 1) | |
8357 | mddev->safemode = 0; | |
85572d7c | 8358 | spin_unlock(&mddev->lock); |
0fd62b86 | 8359 | if (did_change) |
00bcb4ac | 8360 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
fca4d848 | 8361 | } |
fca4d848 | 8362 | |
2aa82191 | 8363 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
850b2b42 | 8364 | md_update_sb(mddev, 0); |
06d91a5f | 8365 | |
1da177e4 LT |
8366 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
8367 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { | |
8368 | /* resync/recovery still happening */ | |
8369 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
8370 | goto unlock; | |
8371 | } | |
8372 | if (mddev->sync_thread) { | |
a91d5ac0 | 8373 | md_reap_sync_thread(mddev); |
1da177e4 LT |
8374 | goto unlock; |
8375 | } | |
72a23c21 NB |
8376 | /* Set RUNNING before clearing NEEDED to avoid |
8377 | * any transients in the value of "sync_action". | |
8378 | */ | |
72f36d59 | 8379 | mddev->curr_resync_completed = 0; |
23da422b | 8380 | spin_lock(&mddev->lock); |
72a23c21 | 8381 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
23da422b | 8382 | spin_unlock(&mddev->lock); |
24dd469d N |
8383 | /* Clear some bits that don't mean anything, but |
8384 | * might be left set | |
8385 | */ | |
24dd469d N |
8386 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
8387 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); | |
1da177e4 | 8388 | |
ed209584 N |
8389 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
8390 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) | |
ac05f256 | 8391 | goto not_running; |
1da177e4 LT |
8392 | /* no recovery is running. |
8393 | * remove any failed drives, then | |
8394 | * add spares if possible. | |
72f36d59 | 8395 | * Spares are also removed and re-added, to allow |
1da177e4 LT |
8396 | * the personality to fail the re-add. |
8397 | */ | |
1da177e4 | 8398 | |
b4c4c7b8 | 8399 | if (mddev->reshape_position != MaxSector) { |
50ac168a N |
8400 | if (mddev->pers->check_reshape == NULL || |
8401 | mddev->pers->check_reshape(mddev) != 0) | |
b4c4c7b8 | 8402 | /* Cannot proceed */ |
ac05f256 | 8403 | goto not_running; |
b4c4c7b8 | 8404 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
72a23c21 | 8405 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
746d3207 | 8406 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
24dd469d N |
8407 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
8408 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
56ac36d7 | 8409 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
72a23c21 | 8410 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
24dd469d N |
8411 | } else if (mddev->recovery_cp < MaxSector) { |
8412 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
72a23c21 | 8413 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
24dd469d N |
8414 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
8415 | /* nothing to be done ... */ | |
ac05f256 | 8416 | goto not_running; |
24dd469d | 8417 | |
1da177e4 | 8418 | if (mddev->pers->sync_request) { |
ef99bf48 | 8419 | if (spares) { |
a654b9d8 N |
8420 | /* We are adding a device or devices to an array |
8421 | * which has the bitmap stored on all devices. | |
8422 | * So make sure all bitmap pages get written | |
8423 | */ | |
8424 | bitmap_write_all(mddev->bitmap); | |
8425 | } | |
ac05f256 N |
8426 | INIT_WORK(&mddev->del_work, md_start_sync); |
8427 | queue_work(md_misc_wq, &mddev->del_work); | |
8428 | goto unlock; | |
1da177e4 | 8429 | } |
ac05f256 | 8430 | not_running: |
72a23c21 NB |
8431 | if (!mddev->sync_thread) { |
8432 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
f851b60d | 8433 | wake_up(&resync_wait); |
72a23c21 NB |
8434 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
8435 | &mddev->recovery)) | |
0c3573f1 | 8436 | if (mddev->sysfs_action) |
00bcb4ac | 8437 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
72a23c21 | 8438 | } |
ac05f256 N |
8439 | unlock: |
8440 | wake_up(&mddev->sb_wait); | |
1da177e4 LT |
8441 | mddev_unlock(mddev); |
8442 | } | |
8443 | } | |
6c144d31 | 8444 | EXPORT_SYMBOL(md_check_recovery); |
1da177e4 | 8445 | |
a91d5ac0 JB |
8446 | void md_reap_sync_thread(struct mddev *mddev) |
8447 | { | |
8448 | struct md_rdev *rdev; | |
8449 | ||
8450 | /* resync has finished, collect result */ | |
8451 | md_unregister_thread(&mddev->sync_thread); | |
8452 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && | |
8453 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { | |
8454 | /* success...*/ | |
8455 | /* activate any spares */ | |
8456 | if (mddev->pers->spare_active(mddev)) { | |
8457 | sysfs_notify(&mddev->kobj, NULL, | |
8458 | "degraded"); | |
8459 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
8460 | } | |
8461 | } | |
8462 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && | |
8463 | mddev->pers->finish_reshape) | |
8464 | mddev->pers->finish_reshape(mddev); | |
8465 | ||
8466 | /* If array is no-longer degraded, then any saved_raid_disk | |
f466722c | 8467 | * information must be scrapped. |
a91d5ac0 | 8468 | */ |
f466722c N |
8469 | if (!mddev->degraded) |
8470 | rdev_for_each(rdev, mddev) | |
a91d5ac0 JB |
8471 | rdev->saved_raid_disk = -1; |
8472 | ||
8473 | md_update_sb(mddev, 1); | |
8474 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
ea358cd0 | 8475 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
a91d5ac0 JB |
8476 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
8477 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
8478 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); | |
8479 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
f851b60d | 8480 | wake_up(&resync_wait); |
a91d5ac0 JB |
8481 | /* flag recovery needed just to double check */ |
8482 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
8483 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
8484 | md_new_event(mddev); | |
8485 | if (mddev->event_work.func) | |
8486 | queue_work(md_misc_wq, &mddev->event_work); | |
8487 | } | |
6c144d31 | 8488 | EXPORT_SYMBOL(md_reap_sync_thread); |
a91d5ac0 | 8489 | |
fd01b88c | 8490 | void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev) |
6bfe0b49 | 8491 | { |
00bcb4ac | 8492 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
6bfe0b49 | 8493 | wait_event_timeout(rdev->blocked_wait, |
de393cde N |
8494 | !test_bit(Blocked, &rdev->flags) && |
8495 | !test_bit(BlockedBadBlocks, &rdev->flags), | |
6bfe0b49 DW |
8496 | msecs_to_jiffies(5000)); |
8497 | rdev_dec_pending(rdev, mddev); | |
8498 | } | |
8499 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); | |
8500 | ||
c6563a8c N |
8501 | void md_finish_reshape(struct mddev *mddev) |
8502 | { | |
8503 | /* called be personality module when reshape completes. */ | |
8504 | struct md_rdev *rdev; | |
8505 | ||
8506 | rdev_for_each(rdev, mddev) { | |
8507 | if (rdev->data_offset > rdev->new_data_offset) | |
8508 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; | |
8509 | else | |
8510 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; | |
8511 | rdev->data_offset = rdev->new_data_offset; | |
8512 | } | |
8513 | } | |
8514 | EXPORT_SYMBOL(md_finish_reshape); | |
2230dfe4 N |
8515 | |
8516 | /* Bad block management. | |
8517 | * We can record which blocks on each device are 'bad' and so just | |
8518 | * fail those blocks, or that stripe, rather than the whole device. | |
8519 | * Entries in the bad-block table are 64bits wide. This comprises: | |
8520 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 | |
8521 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) | |
8522 | * A 'shift' can be set so that larger blocks are tracked and | |
8523 | * consequently larger devices can be covered. | |
8524 | * 'Acknowledged' flag - 1 bit. - the most significant bit. | |
8525 | * | |
8526 | * Locking of the bad-block table uses a seqlock so md_is_badblock | |
8527 | * might need to retry if it is very unlucky. | |
8528 | * We will sometimes want to check for bad blocks in a bi_end_io function, | |
8529 | * so we use the write_seqlock_irq variant. | |
8530 | * | |
8531 | * When looking for a bad block we specify a range and want to | |
8532 | * know if any block in the range is bad. So we binary-search | |
8533 | * to the last range that starts at-or-before the given endpoint, | |
8534 | * (or "before the sector after the target range") | |
8535 | * then see if it ends after the given start. | |
8536 | * We return | |
8537 | * 0 if there are no known bad blocks in the range | |
8538 | * 1 if there are known bad block which are all acknowledged | |
8539 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. | |
8540 | * plus the start/length of the first bad section we overlap. | |
8541 | */ | |
8542 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, | |
8543 | sector_t *first_bad, int *bad_sectors) | |
8544 | { | |
8545 | int hi; | |
ab05613a | 8546 | int lo; |
2230dfe4 | 8547 | u64 *p = bb->page; |
ab05613a | 8548 | int rv; |
2230dfe4 N |
8549 | sector_t target = s + sectors; |
8550 | unsigned seq; | |
8551 | ||
8552 | if (bb->shift > 0) { | |
8553 | /* round the start down, and the end up */ | |
8554 | s >>= bb->shift; | |
8555 | target += (1<<bb->shift) - 1; | |
8556 | target >>= bb->shift; | |
8557 | sectors = target - s; | |
8558 | } | |
8559 | /* 'target' is now the first block after the bad range */ | |
8560 | ||
8561 | retry: | |
8562 | seq = read_seqbegin(&bb->lock); | |
ab05613a | 8563 | lo = 0; |
8564 | rv = 0; | |
2230dfe4 N |
8565 | hi = bb->count; |
8566 | ||
8567 | /* Binary search between lo and hi for 'target' | |
8568 | * i.e. for the last range that starts before 'target' | |
8569 | */ | |
8570 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' | |
8571 | * are known not to be the last range before target. | |
8572 | * VARIANT: hi-lo is the number of possible | |
8573 | * ranges, and decreases until it reaches 1 | |
8574 | */ | |
8575 | while (hi - lo > 1) { | |
8576 | int mid = (lo + hi) / 2; | |
8577 | sector_t a = BB_OFFSET(p[mid]); | |
8578 | if (a < target) | |
8579 | /* This could still be the one, earlier ranges | |
8580 | * could not. */ | |
8581 | lo = mid; | |
8582 | else | |
8583 | /* This and later ranges are definitely out. */ | |
8584 | hi = mid; | |
8585 | } | |
8586 | /* 'lo' might be the last that started before target, but 'hi' isn't */ | |
8587 | if (hi > lo) { | |
8588 | /* need to check all range that end after 's' to see if | |
8589 | * any are unacknowledged. | |
8590 | */ | |
8591 | while (lo >= 0 && | |
8592 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { | |
8593 | if (BB_OFFSET(p[lo]) < target) { | |
8594 | /* starts before the end, and finishes after | |
8595 | * the start, so they must overlap | |
8596 | */ | |
8597 | if (rv != -1 && BB_ACK(p[lo])) | |
8598 | rv = 1; | |
8599 | else | |
8600 | rv = -1; | |
8601 | *first_bad = BB_OFFSET(p[lo]); | |
8602 | *bad_sectors = BB_LEN(p[lo]); | |
8603 | } | |
8604 | lo--; | |
8605 | } | |
8606 | } | |
8607 | ||
8608 | if (read_seqretry(&bb->lock, seq)) | |
8609 | goto retry; | |
8610 | ||
8611 | return rv; | |
8612 | } | |
8613 | EXPORT_SYMBOL_GPL(md_is_badblock); | |
8614 | ||
8615 | /* | |
8616 | * Add a range of bad blocks to the table. | |
8617 | * This might extend the table, or might contract it | |
8618 | * if two adjacent ranges can be merged. | |
8619 | * We binary-search to find the 'insertion' point, then | |
8620 | * decide how best to handle it. | |
8621 | */ | |
8622 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, | |
8623 | int acknowledged) | |
8624 | { | |
8625 | u64 *p; | |
8626 | int lo, hi; | |
8627 | int rv = 1; | |
905b0297 | 8628 | unsigned long flags; |
2230dfe4 N |
8629 | |
8630 | if (bb->shift < 0) | |
8631 | /* badblocks are disabled */ | |
8632 | return 0; | |
8633 | ||
8634 | if (bb->shift) { | |
8635 | /* round the start down, and the end up */ | |
8636 | sector_t next = s + sectors; | |
8637 | s >>= bb->shift; | |
8638 | next += (1<<bb->shift) - 1; | |
8639 | next >>= bb->shift; | |
8640 | sectors = next - s; | |
8641 | } | |
8642 | ||
905b0297 | 8643 | write_seqlock_irqsave(&bb->lock, flags); |
2230dfe4 N |
8644 | |
8645 | p = bb->page; | |
8646 | lo = 0; | |
8647 | hi = bb->count; | |
8648 | /* Find the last range that starts at-or-before 's' */ | |
8649 | while (hi - lo > 1) { | |
8650 | int mid = (lo + hi) / 2; | |
8651 | sector_t a = BB_OFFSET(p[mid]); | |
8652 | if (a <= s) | |
8653 | lo = mid; | |
8654 | else | |
8655 | hi = mid; | |
8656 | } | |
8657 | if (hi > lo && BB_OFFSET(p[lo]) > s) | |
8658 | hi = lo; | |
8659 | ||
8660 | if (hi > lo) { | |
8661 | /* we found a range that might merge with the start | |
8662 | * of our new range | |
8663 | */ | |
8664 | sector_t a = BB_OFFSET(p[lo]); | |
8665 | sector_t e = a + BB_LEN(p[lo]); | |
8666 | int ack = BB_ACK(p[lo]); | |
8667 | if (e >= s) { | |
8668 | /* Yes, we can merge with a previous range */ | |
8669 | if (s == a && s + sectors >= e) | |
8670 | /* new range covers old */ | |
8671 | ack = acknowledged; | |
8672 | else | |
8673 | ack = ack && acknowledged; | |
8674 | ||
8675 | if (e < s + sectors) | |
8676 | e = s + sectors; | |
8677 | if (e - a <= BB_MAX_LEN) { | |
8678 | p[lo] = BB_MAKE(a, e-a, ack); | |
8679 | s = e; | |
8680 | } else { | |
8681 | /* does not all fit in one range, | |
8682 | * make p[lo] maximal | |
8683 | */ | |
8684 | if (BB_LEN(p[lo]) != BB_MAX_LEN) | |
8685 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); | |
8686 | s = a + BB_MAX_LEN; | |
8687 | } | |
8688 | sectors = e - s; | |
8689 | } | |
8690 | } | |
8691 | if (sectors && hi < bb->count) { | |
8692 | /* 'hi' points to the first range that starts after 's'. | |
8693 | * Maybe we can merge with the start of that range */ | |
8694 | sector_t a = BB_OFFSET(p[hi]); | |
8695 | sector_t e = a + BB_LEN(p[hi]); | |
8696 | int ack = BB_ACK(p[hi]); | |
8697 | if (a <= s + sectors) { | |
8698 | /* merging is possible */ | |
8699 | if (e <= s + sectors) { | |
8700 | /* full overlap */ | |
8701 | e = s + sectors; | |
8702 | ack = acknowledged; | |
8703 | } else | |
8704 | ack = ack && acknowledged; | |
8705 | ||
8706 | a = s; | |
8707 | if (e - a <= BB_MAX_LEN) { | |
8708 | p[hi] = BB_MAKE(a, e-a, ack); | |
8709 | s = e; | |
8710 | } else { | |
8711 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); | |
8712 | s = a + BB_MAX_LEN; | |
8713 | } | |
8714 | sectors = e - s; | |
8715 | lo = hi; | |
8716 | hi++; | |
8717 | } | |
8718 | } | |
8719 | if (sectors == 0 && hi < bb->count) { | |
8720 | /* we might be able to combine lo and hi */ | |
8721 | /* Note: 's' is at the end of 'lo' */ | |
8722 | sector_t a = BB_OFFSET(p[hi]); | |
8723 | int lolen = BB_LEN(p[lo]); | |
8724 | int hilen = BB_LEN(p[hi]); | |
8725 | int newlen = lolen + hilen - (s - a); | |
8726 | if (s >= a && newlen < BB_MAX_LEN) { | |
8727 | /* yes, we can combine them */ | |
8728 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); | |
8729 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); | |
8730 | memmove(p + hi, p + hi + 1, | |
8731 | (bb->count - hi - 1) * 8); | |
8732 | bb->count--; | |
8733 | } | |
8734 | } | |
8735 | while (sectors) { | |
8736 | /* didn't merge (it all). | |
8737 | * Need to add a range just before 'hi' */ | |
8738 | if (bb->count >= MD_MAX_BADBLOCKS) { | |
8739 | /* No room for more */ | |
8740 | rv = 0; | |
8741 | break; | |
8742 | } else { | |
8743 | int this_sectors = sectors; | |
8744 | memmove(p + hi + 1, p + hi, | |
8745 | (bb->count - hi) * 8); | |
8746 | bb->count++; | |
8747 | ||
8748 | if (this_sectors > BB_MAX_LEN) | |
8749 | this_sectors = BB_MAX_LEN; | |
8750 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); | |
8751 | sectors -= this_sectors; | |
8752 | s += this_sectors; | |
8753 | } | |
8754 | } | |
8755 | ||
8756 | bb->changed = 1; | |
de393cde N |
8757 | if (!acknowledged) |
8758 | bb->unacked_exist = 1; | |
905b0297 | 8759 | write_sequnlock_irqrestore(&bb->lock, flags); |
2230dfe4 N |
8760 | |
8761 | return rv; | |
8762 | } | |
8763 | ||
3cb03002 | 8764 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
c6563a8c | 8765 | int is_new) |
2230dfe4 | 8766 | { |
c6563a8c N |
8767 | int rv; |
8768 | if (is_new) | |
8769 | s += rdev->new_data_offset; | |
8770 | else | |
8771 | s += rdev->data_offset; | |
8772 | rv = md_set_badblocks(&rdev->badblocks, | |
8773 | s, sectors, 0); | |
2230dfe4 N |
8774 | if (rv) { |
8775 | /* Make sure they get written out promptly */ | |
8bd2f0a0 | 8776 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
2230dfe4 | 8777 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
55ce74d4 | 8778 | set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags); |
2230dfe4 N |
8779 | md_wakeup_thread(rdev->mddev->thread); |
8780 | } | |
8781 | return rv; | |
8782 | } | |
8783 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); | |
8784 | ||
8785 | /* | |
8786 | * Remove a range of bad blocks from the table. | |
8787 | * This may involve extending the table if we spilt a region, | |
8788 | * but it must not fail. So if the table becomes full, we just | |
8789 | * drop the remove request. | |
8790 | */ | |
8791 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) | |
8792 | { | |
8793 | u64 *p; | |
8794 | int lo, hi; | |
8795 | sector_t target = s + sectors; | |
8796 | int rv = 0; | |
8797 | ||
8798 | if (bb->shift > 0) { | |
8799 | /* When clearing we round the start up and the end down. | |
8800 | * This should not matter as the shift should align with | |
8801 | * the block size and no rounding should ever be needed. | |
8802 | * However it is better the think a block is bad when it | |
8803 | * isn't than to think a block is not bad when it is. | |
8804 | */ | |
8805 | s += (1<<bb->shift) - 1; | |
8806 | s >>= bb->shift; | |
8807 | target >>= bb->shift; | |
8808 | sectors = target - s; | |
8809 | } | |
8810 | ||
8811 | write_seqlock_irq(&bb->lock); | |
8812 | ||
8813 | p = bb->page; | |
8814 | lo = 0; | |
8815 | hi = bb->count; | |
8816 | /* Find the last range that starts before 'target' */ | |
8817 | while (hi - lo > 1) { | |
8818 | int mid = (lo + hi) / 2; | |
8819 | sector_t a = BB_OFFSET(p[mid]); | |
8820 | if (a < target) | |
8821 | lo = mid; | |
8822 | else | |
8823 | hi = mid; | |
8824 | } | |
8825 | if (hi > lo) { | |
8826 | /* p[lo] is the last range that could overlap the | |
8827 | * current range. Earlier ranges could also overlap, | |
8828 | * but only this one can overlap the end of the range. | |
8829 | */ | |
8830 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { | |
8831 | /* Partial overlap, leave the tail of this range */ | |
8832 | int ack = BB_ACK(p[lo]); | |
8833 | sector_t a = BB_OFFSET(p[lo]); | |
8834 | sector_t end = a + BB_LEN(p[lo]); | |
8835 | ||
8836 | if (a < s) { | |
8837 | /* we need to split this range */ | |
8838 | if (bb->count >= MD_MAX_BADBLOCKS) { | |
8b32bf5e | 8839 | rv = -ENOSPC; |
2230dfe4 N |
8840 | goto out; |
8841 | } | |
8842 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); | |
8843 | bb->count++; | |
8844 | p[lo] = BB_MAKE(a, s-a, ack); | |
8845 | lo++; | |
8846 | } | |
8847 | p[lo] = BB_MAKE(target, end - target, ack); | |
8848 | /* there is no longer an overlap */ | |
8849 | hi = lo; | |
8850 | lo--; | |
8851 | } | |
8852 | while (lo >= 0 && | |
8853 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { | |
8854 | /* This range does overlap */ | |
8855 | if (BB_OFFSET(p[lo]) < s) { | |
8856 | /* Keep the early parts of this range. */ | |
8857 | int ack = BB_ACK(p[lo]); | |
8858 | sector_t start = BB_OFFSET(p[lo]); | |
8859 | p[lo] = BB_MAKE(start, s - start, ack); | |
8860 | /* now low doesn't overlap, so.. */ | |
8861 | break; | |
8862 | } | |
8863 | lo--; | |
8864 | } | |
8865 | /* 'lo' is strictly before, 'hi' is strictly after, | |
8866 | * anything between needs to be discarded | |
8867 | */ | |
8868 | if (hi - lo > 1) { | |
8869 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); | |
8870 | bb->count -= (hi - lo - 1); | |
8871 | } | |
8872 | } | |
8873 | ||
8874 | bb->changed = 1; | |
8875 | out: | |
8876 | write_sequnlock_irq(&bb->lock); | |
8877 | return rv; | |
8878 | } | |
8879 | ||
c6563a8c N |
8880 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
8881 | int is_new) | |
2230dfe4 | 8882 | { |
c6563a8c N |
8883 | if (is_new) |
8884 | s += rdev->new_data_offset; | |
8885 | else | |
8886 | s += rdev->data_offset; | |
2230dfe4 | 8887 | return md_clear_badblocks(&rdev->badblocks, |
c6563a8c | 8888 | s, sectors); |
2230dfe4 N |
8889 | } |
8890 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); | |
8891 | ||
8892 | /* | |
8893 | * Acknowledge all bad blocks in a list. | |
8894 | * This only succeeds if ->changed is clear. It is used by | |
8895 | * in-kernel metadata updates | |
8896 | */ | |
8897 | void md_ack_all_badblocks(struct badblocks *bb) | |
8898 | { | |
8899 | if (bb->page == NULL || bb->changed) | |
8900 | /* no point even trying */ | |
8901 | return; | |
8902 | write_seqlock_irq(&bb->lock); | |
8903 | ||
ecb178bb | 8904 | if (bb->changed == 0 && bb->unacked_exist) { |
2230dfe4 N |
8905 | u64 *p = bb->page; |
8906 | int i; | |
8907 | for (i = 0; i < bb->count ; i++) { | |
8908 | if (!BB_ACK(p[i])) { | |
8909 | sector_t start = BB_OFFSET(p[i]); | |
8910 | int len = BB_LEN(p[i]); | |
8911 | p[i] = BB_MAKE(start, len, 1); | |
8912 | } | |
8913 | } | |
de393cde | 8914 | bb->unacked_exist = 0; |
2230dfe4 N |
8915 | } |
8916 | write_sequnlock_irq(&bb->lock); | |
8917 | } | |
8918 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); | |
8919 | ||
16c791a5 N |
8920 | /* sysfs access to bad-blocks list. |
8921 | * We present two files. | |
8922 | * 'bad-blocks' lists sector numbers and lengths of ranges that | |
8923 | * are recorded as bad. The list is truncated to fit within | |
8924 | * the one-page limit of sysfs. | |
8925 | * Writing "sector length" to this file adds an acknowledged | |
8926 | * bad block list. | |
8927 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet | |
8928 | * been acknowledged. Writing to this file adds bad blocks | |
8929 | * without acknowledging them. This is largely for testing. | |
8930 | */ | |
8931 | ||
8932 | static ssize_t | |
8933 | badblocks_show(struct badblocks *bb, char *page, int unack) | |
8934 | { | |
8935 | size_t len; | |
8936 | int i; | |
8937 | u64 *p = bb->page; | |
8938 | unsigned seq; | |
8939 | ||
8940 | if (bb->shift < 0) | |
8941 | return 0; | |
8942 | ||
8943 | retry: | |
8944 | seq = read_seqbegin(&bb->lock); | |
8945 | ||
8946 | len = 0; | |
8947 | i = 0; | |
8948 | ||
8949 | while (len < PAGE_SIZE && i < bb->count) { | |
8950 | sector_t s = BB_OFFSET(p[i]); | |
8951 | unsigned int length = BB_LEN(p[i]); | |
8952 | int ack = BB_ACK(p[i]); | |
8953 | i++; | |
8954 | ||
8955 | if (unack && ack) | |
8956 | continue; | |
8957 | ||
8958 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", | |
8959 | (unsigned long long)s << bb->shift, | |
8960 | length << bb->shift); | |
8961 | } | |
de393cde N |
8962 | if (unack && len == 0) |
8963 | bb->unacked_exist = 0; | |
16c791a5 N |
8964 | |
8965 | if (read_seqretry(&bb->lock, seq)) | |
8966 | goto retry; | |
8967 | ||
8968 | return len; | |
8969 | } | |
8970 | ||
8971 | #define DO_DEBUG 1 | |
8972 | ||
8973 | static ssize_t | |
8974 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) | |
8975 | { | |
8976 | unsigned long long sector; | |
8977 | int length; | |
8978 | char newline; | |
8979 | #ifdef DO_DEBUG | |
8980 | /* Allow clearing via sysfs *only* for testing/debugging. | |
8981 | * Normally only a successful write may clear a badblock | |
8982 | */ | |
8983 | int clear = 0; | |
8984 | if (page[0] == '-') { | |
8985 | clear = 1; | |
8986 | page++; | |
8987 | } | |
8988 | #endif /* DO_DEBUG */ | |
8989 | ||
8990 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { | |
8991 | case 3: | |
8992 | if (newline != '\n') | |
8993 | return -EINVAL; | |
8994 | case 2: | |
8995 | if (length <= 0) | |
8996 | return -EINVAL; | |
8997 | break; | |
8998 | default: | |
8999 | return -EINVAL; | |
9000 | } | |
9001 | ||
9002 | #ifdef DO_DEBUG | |
9003 | if (clear) { | |
9004 | md_clear_badblocks(bb, sector, length); | |
9005 | return len; | |
9006 | } | |
9007 | #endif /* DO_DEBUG */ | |
9008 | if (md_set_badblocks(bb, sector, length, !unack)) | |
9009 | return len; | |
9010 | else | |
9011 | return -ENOSPC; | |
9012 | } | |
9013 | ||
75c96f85 AB |
9014 | static int md_notify_reboot(struct notifier_block *this, |
9015 | unsigned long code, void *x) | |
1da177e4 LT |
9016 | { |
9017 | struct list_head *tmp; | |
fd01b88c | 9018 | struct mddev *mddev; |
2dba6a91 | 9019 | int need_delay = 0; |
1da177e4 | 9020 | |
c744a65c N |
9021 | for_each_mddev(mddev, tmp) { |
9022 | if (mddev_trylock(mddev)) { | |
30b8aa91 N |
9023 | if (mddev->pers) |
9024 | __md_stop_writes(mddev); | |
0f62fb22 N |
9025 | if (mddev->persistent) |
9026 | mddev->safemode = 2; | |
c744a65c | 9027 | mddev_unlock(mddev); |
2dba6a91 | 9028 | } |
c744a65c | 9029 | need_delay = 1; |
1da177e4 | 9030 | } |
c744a65c N |
9031 | /* |
9032 | * certain more exotic SCSI devices are known to be | |
9033 | * volatile wrt too early system reboots. While the | |
9034 | * right place to handle this issue is the given | |
9035 | * driver, we do want to have a safe RAID driver ... | |
9036 | */ | |
9037 | if (need_delay) | |
9038 | mdelay(1000*1); | |
9039 | ||
1da177e4 LT |
9040 | return NOTIFY_DONE; |
9041 | } | |
9042 | ||
75c96f85 | 9043 | static struct notifier_block md_notifier = { |
1da177e4 LT |
9044 | .notifier_call = md_notify_reboot, |
9045 | .next = NULL, | |
9046 | .priority = INT_MAX, /* before any real devices */ | |
9047 | }; | |
9048 | ||
9049 | static void md_geninit(void) | |
9050 | { | |
36a4e1fe | 9051 | pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
1da177e4 | 9052 | |
c7705f34 | 9053 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
1da177e4 LT |
9054 | } |
9055 | ||
75c96f85 | 9056 | static int __init md_init(void) |
1da177e4 | 9057 | { |
e804ac78 TH |
9058 | int ret = -ENOMEM; |
9059 | ||
ada609ee | 9060 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
e804ac78 TH |
9061 | if (!md_wq) |
9062 | goto err_wq; | |
9063 | ||
9064 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); | |
9065 | if (!md_misc_wq) | |
9066 | goto err_misc_wq; | |
9067 | ||
9068 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) | |
9069 | goto err_md; | |
9070 | ||
9071 | if ((ret = register_blkdev(0, "mdp")) < 0) | |
9072 | goto err_mdp; | |
9073 | mdp_major = ret; | |
9074 | ||
af5628f0 | 9075 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
e8703fe1 N |
9076 | md_probe, NULL, NULL); |
9077 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, | |
1da177e4 LT |
9078 | md_probe, NULL, NULL); |
9079 | ||
1da177e4 | 9080 | register_reboot_notifier(&md_notifier); |
0b4d4147 | 9081 | raid_table_header = register_sysctl_table(raid_root_table); |
1da177e4 LT |
9082 | |
9083 | md_geninit(); | |
d710e138 | 9084 | return 0; |
1da177e4 | 9085 | |
e804ac78 TH |
9086 | err_mdp: |
9087 | unregister_blkdev(MD_MAJOR, "md"); | |
9088 | err_md: | |
9089 | destroy_workqueue(md_misc_wq); | |
9090 | err_misc_wq: | |
9091 | destroy_workqueue(md_wq); | |
9092 | err_wq: | |
9093 | return ret; | |
9094 | } | |
1da177e4 | 9095 | |
70bcecdb | 9096 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
1d7e3e96 | 9097 | { |
70bcecdb GR |
9098 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
9099 | struct md_rdev *rdev2; | |
9100 | int role, ret; | |
9101 | char b[BDEVNAME_SIZE]; | |
1d7e3e96 | 9102 | |
70bcecdb GR |
9103 | /* Check for change of roles in the active devices */ |
9104 | rdev_for_each(rdev2, mddev) { | |
9105 | if (test_bit(Faulty, &rdev2->flags)) | |
9106 | continue; | |
9107 | ||
9108 | /* Check if the roles changed */ | |
9109 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); | |
dbb64f86 GR |
9110 | |
9111 | if (test_bit(Candidate, &rdev2->flags)) { | |
9112 | if (role == 0xfffe) { | |
9113 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); | |
9114 | md_kick_rdev_from_array(rdev2); | |
9115 | continue; | |
9116 | } | |
9117 | else | |
9118 | clear_bit(Candidate, &rdev2->flags); | |
9119 | } | |
9120 | ||
70bcecdb GR |
9121 | if (role != rdev2->raid_disk) { |
9122 | /* got activated */ | |
9123 | if (rdev2->raid_disk == -1 && role != 0xffff) { | |
9124 | rdev2->saved_raid_disk = role; | |
9125 | ret = remove_and_add_spares(mddev, rdev2); | |
9126 | pr_info("Activated spare: %s\n", | |
9127 | bdevname(rdev2->bdev,b)); | |
70bcecdb GR |
9128 | } |
9129 | /* device faulty | |
9130 | * We just want to do the minimum to mark the disk | |
9131 | * as faulty. The recovery is performed by the | |
9132 | * one who initiated the error. | |
9133 | */ | |
9134 | if ((role == 0xfffe) || (role == 0xfffd)) { | |
9135 | md_error(mddev, rdev2); | |
9136 | clear_bit(Blocked, &rdev2->flags); | |
9137 | } | |
9138 | } | |
1d7e3e96 | 9139 | } |
70bcecdb | 9140 | |
28c1b9fd GR |
9141 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
9142 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); | |
70bcecdb GR |
9143 | |
9144 | /* Finally set the event to be up to date */ | |
9145 | mddev->events = le64_to_cpu(sb->events); | |
9146 | } | |
9147 | ||
9148 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) | |
9149 | { | |
9150 | int err; | |
9151 | struct page *swapout = rdev->sb_page; | |
9152 | struct mdp_superblock_1 *sb; | |
9153 | ||
9154 | /* Store the sb page of the rdev in the swapout temporary | |
9155 | * variable in case we err in the future | |
9156 | */ | |
9157 | rdev->sb_page = NULL; | |
9158 | alloc_disk_sb(rdev); | |
9159 | ClearPageUptodate(rdev->sb_page); | |
9160 | rdev->sb_loaded = 0; | |
9161 | err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version); | |
9162 | ||
9163 | if (err < 0) { | |
9164 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", | |
9165 | __func__, __LINE__, rdev->desc_nr, err); | |
9166 | put_page(rdev->sb_page); | |
9167 | rdev->sb_page = swapout; | |
9168 | rdev->sb_loaded = 1; | |
9169 | return err; | |
1d7e3e96 GR |
9170 | } |
9171 | ||
70bcecdb GR |
9172 | sb = page_address(rdev->sb_page); |
9173 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET | |
9174 | * is not set | |
9175 | */ | |
9176 | ||
9177 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) | |
9178 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); | |
9179 | ||
9180 | /* The other node finished recovery, call spare_active to set | |
9181 | * device In_sync and mddev->degraded | |
9182 | */ | |
9183 | if (rdev->recovery_offset == MaxSector && | |
9184 | !test_bit(In_sync, &rdev->flags) && | |
9185 | mddev->pers->spare_active(mddev)) | |
9186 | sysfs_notify(&mddev->kobj, NULL, "degraded"); | |
9187 | ||
9188 | put_page(swapout); | |
9189 | return 0; | |
9190 | } | |
9191 | ||
9192 | void md_reload_sb(struct mddev *mddev, int nr) | |
9193 | { | |
9194 | struct md_rdev *rdev; | |
9195 | int err; | |
9196 | ||
9197 | /* Find the rdev */ | |
9198 | rdev_for_each_rcu(rdev, mddev) { | |
9199 | if (rdev->desc_nr == nr) | |
9200 | break; | |
9201 | } | |
9202 | ||
9203 | if (!rdev || rdev->desc_nr != nr) { | |
9204 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); | |
9205 | return; | |
9206 | } | |
9207 | ||
9208 | err = read_rdev(mddev, rdev); | |
9209 | if (err < 0) | |
9210 | return; | |
9211 | ||
9212 | check_sb_changes(mddev, rdev); | |
9213 | ||
9214 | /* Read all rdev's to update recovery_offset */ | |
9215 | rdev_for_each_rcu(rdev, mddev) | |
9216 | read_rdev(mddev, rdev); | |
1d7e3e96 GR |
9217 | } |
9218 | EXPORT_SYMBOL(md_reload_sb); | |
9219 | ||
1da177e4 LT |
9220 | #ifndef MODULE |
9221 | ||
9222 | /* | |
9223 | * Searches all registered partitions for autorun RAID arrays | |
9224 | * at boot time. | |
9225 | */ | |
4d936ec1 ME |
9226 | |
9227 | static LIST_HEAD(all_detected_devices); | |
9228 | struct detected_devices_node { | |
9229 | struct list_head list; | |
9230 | dev_t dev; | |
9231 | }; | |
1da177e4 LT |
9232 | |
9233 | void md_autodetect_dev(dev_t dev) | |
9234 | { | |
4d936ec1 ME |
9235 | struct detected_devices_node *node_detected_dev; |
9236 | ||
9237 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); | |
9238 | if (node_detected_dev) { | |
9239 | node_detected_dev->dev = dev; | |
9240 | list_add_tail(&node_detected_dev->list, &all_detected_devices); | |
9241 | } else { | |
9242 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" | |
9243 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); | |
9244 | } | |
1da177e4 LT |
9245 | } |
9246 | ||
1da177e4 LT |
9247 | static void autostart_arrays(int part) |
9248 | { | |
3cb03002 | 9249 | struct md_rdev *rdev; |
4d936ec1 ME |
9250 | struct detected_devices_node *node_detected_dev; |
9251 | dev_t dev; | |
9252 | int i_scanned, i_passed; | |
1da177e4 | 9253 | |
4d936ec1 ME |
9254 | i_scanned = 0; |
9255 | i_passed = 0; | |
1da177e4 | 9256 | |
4d936ec1 | 9257 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
1da177e4 | 9258 | |
4d936ec1 ME |
9259 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
9260 | i_scanned++; | |
9261 | node_detected_dev = list_entry(all_detected_devices.next, | |
9262 | struct detected_devices_node, list); | |
9263 | list_del(&node_detected_dev->list); | |
9264 | dev = node_detected_dev->dev; | |
9265 | kfree(node_detected_dev); | |
df968c4e | 9266 | rdev = md_import_device(dev,0, 90); |
1da177e4 LT |
9267 | if (IS_ERR(rdev)) |
9268 | continue; | |
9269 | ||
403df478 | 9270 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 | 9271 | continue; |
403df478 | 9272 | |
d0fae18f | 9273 | set_bit(AutoDetected, &rdev->flags); |
1da177e4 | 9274 | list_add(&rdev->same_set, &pending_raid_disks); |
4d936ec1 | 9275 | i_passed++; |
1da177e4 | 9276 | } |
4d936ec1 ME |
9277 | |
9278 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", | |
9279 | i_scanned, i_passed); | |
1da177e4 LT |
9280 | |
9281 | autorun_devices(part); | |
9282 | } | |
9283 | ||
fdee8ae4 | 9284 | #endif /* !MODULE */ |
1da177e4 LT |
9285 | |
9286 | static __exit void md_exit(void) | |
9287 | { | |
fd01b88c | 9288 | struct mddev *mddev; |
1da177e4 | 9289 | struct list_head *tmp; |
e2f23b60 | 9290 | int delay = 1; |
8ab5e4c1 | 9291 | |
af5628f0 | 9292 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
e8703fe1 | 9293 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
1da177e4 | 9294 | |
3dbd8c2e | 9295 | unregister_blkdev(MD_MAJOR,"md"); |
1da177e4 LT |
9296 | unregister_blkdev(mdp_major, "mdp"); |
9297 | unregister_reboot_notifier(&md_notifier); | |
9298 | unregister_sysctl_table(raid_table_header); | |
e2f23b60 N |
9299 | |
9300 | /* We cannot unload the modules while some process is | |
9301 | * waiting for us in select() or poll() - wake them up | |
9302 | */ | |
9303 | md_unloading = 1; | |
9304 | while (waitqueue_active(&md_event_waiters)) { | |
9305 | /* not safe to leave yet */ | |
9306 | wake_up(&md_event_waiters); | |
9307 | msleep(delay); | |
9308 | delay += delay; | |
9309 | } | |
1da177e4 | 9310 | remove_proc_entry("mdstat", NULL); |
e2f23b60 | 9311 | |
29ac4aa3 | 9312 | for_each_mddev(mddev, tmp) { |
1da177e4 | 9313 | export_array(mddev); |
d3374825 | 9314 | mddev->hold_active = 0; |
1da177e4 | 9315 | } |
e804ac78 TH |
9316 | destroy_workqueue(md_misc_wq); |
9317 | destroy_workqueue(md_wq); | |
1da177e4 LT |
9318 | } |
9319 | ||
685784aa | 9320 | subsys_initcall(md_init); |
1da177e4 LT |
9321 | module_exit(md_exit) |
9322 | ||
f91de92e N |
9323 | static int get_ro(char *buffer, struct kernel_param *kp) |
9324 | { | |
9325 | return sprintf(buffer, "%d", start_readonly); | |
9326 | } | |
9327 | static int set_ro(const char *val, struct kernel_param *kp) | |
9328 | { | |
4c9309c0 | 9329 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
f91de92e N |
9330 | } |
9331 | ||
80ca3a44 N |
9332 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
9333 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); | |
efeb53c0 | 9334 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
f91de92e | 9335 | |
1da177e4 | 9336 | MODULE_LICENSE("GPL"); |
0efb9e61 | 9337 | MODULE_DESCRIPTION("MD RAID framework"); |
aa1595e9 | 9338 | MODULE_ALIAS("md"); |
72008652 | 9339 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |