]>
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 GR |
70 | struct md_cluster_operations *md_cluster_ops; |
71 | struct module *md_cluster_mod; | |
72 | EXPORT_SYMBOL(md_cluster_mod); | |
73 | ||
90b08710 | 74 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
e804ac78 TH |
75 | static struct workqueue_struct *md_wq; |
76 | static struct workqueue_struct *md_misc_wq; | |
90b08710 | 77 | |
746d3207 N |
78 | static int remove_and_add_spares(struct mddev *mddev, |
79 | struct md_rdev *this); | |
5aa61f42 | 80 | static void mddev_detach(struct mddev *mddev); |
746d3207 | 81 | |
1e50915f RB |
82 | /* |
83 | * Default number of read corrections we'll attempt on an rdev | |
84 | * before ejecting it from the array. We divide the read error | |
85 | * count by 2 for every hour elapsed between read errors. | |
86 | */ | |
87 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 | |
1da177e4 LT |
88 | /* |
89 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' | |
90 | * is 1000 KB/sec, so the extra system load does not show up that much. | |
91 | * Increase it if you want to have more _guaranteed_ speed. Note that | |
338cec32 | 92 | * the RAID driver will use the maximum available bandwidth if the IO |
1da177e4 LT |
93 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
94 | * speed limit - in case reconstruction slows down your system despite | |
95 | * idle IO detection. | |
96 | * | |
97 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. | |
88202a0c | 98 | * or /sys/block/mdX/md/sync_speed_{min,max} |
1da177e4 LT |
99 | */ |
100 | ||
101 | static int sysctl_speed_limit_min = 1000; | |
102 | static int sysctl_speed_limit_max = 200000; | |
fd01b88c | 103 | static inline int speed_min(struct mddev *mddev) |
88202a0c N |
104 | { |
105 | return mddev->sync_speed_min ? | |
106 | mddev->sync_speed_min : sysctl_speed_limit_min; | |
107 | } | |
108 | ||
fd01b88c | 109 | static inline int speed_max(struct mddev *mddev) |
88202a0c N |
110 | { |
111 | return mddev->sync_speed_max ? | |
112 | mddev->sync_speed_max : sysctl_speed_limit_max; | |
113 | } | |
1da177e4 LT |
114 | |
115 | static struct ctl_table_header *raid_table_header; | |
116 | ||
82592c38 | 117 | static struct ctl_table raid_table[] = { |
1da177e4 | 118 | { |
1da177e4 LT |
119 | .procname = "speed_limit_min", |
120 | .data = &sysctl_speed_limit_min, | |
121 | .maxlen = sizeof(int), | |
80ca3a44 | 122 | .mode = S_IRUGO|S_IWUSR, |
6d456111 | 123 | .proc_handler = proc_dointvec, |
1da177e4 LT |
124 | }, |
125 | { | |
1da177e4 LT |
126 | .procname = "speed_limit_max", |
127 | .data = &sysctl_speed_limit_max, | |
128 | .maxlen = sizeof(int), | |
80ca3a44 | 129 | .mode = S_IRUGO|S_IWUSR, |
6d456111 | 130 | .proc_handler = proc_dointvec, |
1da177e4 | 131 | }, |
894d2491 | 132 | { } |
1da177e4 LT |
133 | }; |
134 | ||
82592c38 | 135 | static struct ctl_table raid_dir_table[] = { |
1da177e4 | 136 | { |
1da177e4 LT |
137 | .procname = "raid", |
138 | .maxlen = 0, | |
80ca3a44 | 139 | .mode = S_IRUGO|S_IXUGO, |
1da177e4 LT |
140 | .child = raid_table, |
141 | }, | |
894d2491 | 142 | { } |
1da177e4 LT |
143 | }; |
144 | ||
82592c38 | 145 | static struct ctl_table raid_root_table[] = { |
1da177e4 | 146 | { |
1da177e4 LT |
147 | .procname = "dev", |
148 | .maxlen = 0, | |
149 | .mode = 0555, | |
150 | .child = raid_dir_table, | |
151 | }, | |
894d2491 | 152 | { } |
1da177e4 LT |
153 | }; |
154 | ||
83d5cde4 | 155 | static const struct block_device_operations md_fops; |
1da177e4 | 156 | |
f91de92e N |
157 | static int start_readonly; |
158 | ||
a167f663 N |
159 | /* bio_clone_mddev |
160 | * like bio_clone, but with a local bio set | |
161 | */ | |
162 | ||
a167f663 | 163 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
fd01b88c | 164 | struct mddev *mddev) |
a167f663 N |
165 | { |
166 | struct bio *b; | |
a167f663 N |
167 | |
168 | if (!mddev || !mddev->bio_set) | |
169 | return bio_alloc(gfp_mask, nr_iovecs); | |
170 | ||
395c72a7 | 171 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
a167f663 N |
172 | if (!b) |
173 | return NULL; | |
a167f663 N |
174 | return b; |
175 | } | |
176 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); | |
177 | ||
178 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, | |
fd01b88c | 179 | struct mddev *mddev) |
a167f663 | 180 | { |
a167f663 N |
181 | if (!mddev || !mddev->bio_set) |
182 | return bio_clone(bio, gfp_mask); | |
183 | ||
bf800ef1 | 184 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
a167f663 N |
185 | } |
186 | EXPORT_SYMBOL_GPL(bio_clone_mddev); | |
187 | ||
d7603b7e N |
188 | /* |
189 | * We have a system wide 'event count' that is incremented | |
190 | * on any 'interesting' event, and readers of /proc/mdstat | |
191 | * can use 'poll' or 'select' to find out when the event | |
192 | * count increases. | |
193 | * | |
194 | * Events are: | |
195 | * start array, stop array, error, add device, remove device, | |
196 | * start build, activate spare | |
197 | */ | |
2989ddbd | 198 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
d7603b7e | 199 | static atomic_t md_event_count; |
fd01b88c | 200 | void md_new_event(struct mddev *mddev) |
d7603b7e N |
201 | { |
202 | atomic_inc(&md_event_count); | |
203 | wake_up(&md_event_waiters); | |
204 | } | |
29269553 | 205 | EXPORT_SYMBOL_GPL(md_new_event); |
d7603b7e | 206 | |
c331eb04 N |
207 | /* Alternate version that can be called from interrupts |
208 | * when calling sysfs_notify isn't needed. | |
209 | */ | |
fd01b88c | 210 | static void md_new_event_inintr(struct mddev *mddev) |
c331eb04 N |
211 | { |
212 | atomic_inc(&md_event_count); | |
213 | wake_up(&md_event_waiters); | |
214 | } | |
215 | ||
1da177e4 LT |
216 | /* |
217 | * Enables to iterate over all existing md arrays | |
218 | * all_mddevs_lock protects this list. | |
219 | */ | |
220 | static LIST_HEAD(all_mddevs); | |
221 | static DEFINE_SPINLOCK(all_mddevs_lock); | |
222 | ||
1da177e4 LT |
223 | /* |
224 | * iterates through all used mddevs in the system. | |
225 | * We take care to grab the all_mddevs_lock whenever navigating | |
226 | * the list, and to always hold a refcount when unlocked. | |
227 | * Any code which breaks out of this loop while own | |
228 | * a reference to the current mddev and must mddev_put it. | |
229 | */ | |
fd01b88c | 230 | #define for_each_mddev(_mddev,_tmp) \ |
1da177e4 | 231 | \ |
f72ffdd6 | 232 | for (({ spin_lock(&all_mddevs_lock); \ |
fd01b88c N |
233 | _tmp = all_mddevs.next; \ |
234 | _mddev = NULL;}); \ | |
235 | ({ if (_tmp != &all_mddevs) \ | |
236 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ | |
1da177e4 | 237 | spin_unlock(&all_mddevs_lock); \ |
fd01b88c N |
238 | if (_mddev) mddev_put(_mddev); \ |
239 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ | |
240 | _tmp != &all_mddevs;}); \ | |
1da177e4 | 241 | ({ spin_lock(&all_mddevs_lock); \ |
fd01b88c | 242 | _tmp = _tmp->next;}) \ |
1da177e4 LT |
243 | ) |
244 | ||
409c57f3 N |
245 | /* Rather than calling directly into the personality make_request function, |
246 | * IO requests come here first so that we can check if the device is | |
247 | * being suspended pending a reconfiguration. | |
248 | * We hold a refcount over the call to ->make_request. By the time that | |
249 | * call has finished, the bio has been linked into some internal structure | |
250 | * and so is visible to ->quiesce(), so we don't need the refcount any more. | |
251 | */ | |
5a7bbad2 | 252 | static void md_make_request(struct request_queue *q, struct bio *bio) |
1da177e4 | 253 | { |
49077326 | 254 | const int rw = bio_data_dir(bio); |
fd01b88c | 255 | struct mddev *mddev = q->queuedata; |
e91ece55 | 256 | unsigned int sectors; |
49077326 | 257 | |
0ca69886 N |
258 | if (mddev == NULL || mddev->pers == NULL |
259 | || !mddev->ready) { | |
409c57f3 | 260 | bio_io_error(bio); |
5a7bbad2 | 261 | return; |
409c57f3 | 262 | } |
bbfa57c0 SR |
263 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
264 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); | |
265 | return; | |
266 | } | |
0ca69886 | 267 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
409c57f3 | 268 | rcu_read_lock(); |
e9c7469b | 269 | if (mddev->suspended) { |
409c57f3 N |
270 | DEFINE_WAIT(__wait); |
271 | for (;;) { | |
272 | prepare_to_wait(&mddev->sb_wait, &__wait, | |
273 | TASK_UNINTERRUPTIBLE); | |
e9c7469b | 274 | if (!mddev->suspended) |
409c57f3 N |
275 | break; |
276 | rcu_read_unlock(); | |
277 | schedule(); | |
278 | rcu_read_lock(); | |
279 | } | |
280 | finish_wait(&mddev->sb_wait, &__wait); | |
281 | } | |
282 | atomic_inc(&mddev->active_io); | |
283 | rcu_read_unlock(); | |
49077326 | 284 | |
e91ece55 CM |
285 | /* |
286 | * save the sectors now since our bio can | |
287 | * go away inside make_request | |
288 | */ | |
289 | sectors = bio_sectors(bio); | |
5a7bbad2 | 290 | mddev->pers->make_request(mddev, bio); |
49077326 | 291 | |
18c0b223 | 292 | generic_start_io_acct(rw, sectors, &mddev->gendisk->part0); |
49077326 | 293 | |
409c57f3 N |
294 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
295 | wake_up(&mddev->sb_wait); | |
409c57f3 N |
296 | } |
297 | ||
9e35b99c N |
298 | /* mddev_suspend makes sure no new requests are submitted |
299 | * to the device, and that any requests that have been submitted | |
300 | * are completely handled. | |
afa0f557 N |
301 | * Once mddev_detach() is called and completes, the module will be |
302 | * completely unused. | |
9e35b99c | 303 | */ |
fd01b88c | 304 | void mddev_suspend(struct mddev *mddev) |
409c57f3 N |
305 | { |
306 | BUG_ON(mddev->suspended); | |
307 | mddev->suspended = 1; | |
308 | synchronize_rcu(); | |
309 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); | |
310 | mddev->pers->quiesce(mddev, 1); | |
0d9f4f13 JB |
311 | |
312 | del_timer_sync(&mddev->safemode_timer); | |
409c57f3 | 313 | } |
390ee602 | 314 | EXPORT_SYMBOL_GPL(mddev_suspend); |
409c57f3 | 315 | |
fd01b88c | 316 | void mddev_resume(struct mddev *mddev) |
409c57f3 N |
317 | { |
318 | mddev->suspended = 0; | |
319 | wake_up(&mddev->sb_wait); | |
320 | mddev->pers->quiesce(mddev, 0); | |
0fd018af | 321 | |
47525e59 | 322 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
0fd018af JB |
323 | md_wakeup_thread(mddev->thread); |
324 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ | |
1da177e4 | 325 | } |
390ee602 | 326 | EXPORT_SYMBOL_GPL(mddev_resume); |
1da177e4 | 327 | |
fd01b88c | 328 | int mddev_congested(struct mddev *mddev, int bits) |
3fa841d7 | 329 | { |
5c675f83 N |
330 | struct md_personality *pers = mddev->pers; |
331 | int ret = 0; | |
332 | ||
333 | rcu_read_lock(); | |
334 | if (mddev->suspended) | |
335 | ret = 1; | |
336 | else if (pers && pers->congested) | |
337 | ret = pers->congested(mddev, bits); | |
338 | rcu_read_unlock(); | |
339 | return ret; | |
340 | } | |
341 | EXPORT_SYMBOL_GPL(mddev_congested); | |
342 | static int md_congested(void *data, int bits) | |
343 | { | |
344 | struct mddev *mddev = data; | |
345 | return mddev_congested(mddev, bits); | |
3fa841d7 | 346 | } |
3fa841d7 | 347 | |
64590f45 N |
348 | static int md_mergeable_bvec(struct request_queue *q, |
349 | struct bvec_merge_data *bvm, | |
350 | struct bio_vec *biovec) | |
351 | { | |
352 | struct mddev *mddev = q->queuedata; | |
353 | int ret; | |
354 | rcu_read_lock(); | |
355 | if (mddev->suspended) { | |
356 | /* Must always allow one vec */ | |
357 | if (bvm->bi_size == 0) | |
358 | ret = biovec->bv_len; | |
359 | else | |
360 | ret = 0; | |
361 | } else { | |
362 | struct md_personality *pers = mddev->pers; | |
363 | if (pers && pers->mergeable_bvec) | |
364 | ret = pers->mergeable_bvec(mddev, bvm, biovec); | |
365 | else | |
366 | ret = biovec->bv_len; | |
367 | } | |
368 | rcu_read_unlock(); | |
369 | return ret; | |
370 | } | |
a2826aa9 | 371 | /* |
e9c7469b | 372 | * Generic flush handling for md |
a2826aa9 N |
373 | */ |
374 | ||
e9c7469b | 375 | static void md_end_flush(struct bio *bio, int err) |
a2826aa9 | 376 | { |
3cb03002 | 377 | struct md_rdev *rdev = bio->bi_private; |
fd01b88c | 378 | struct mddev *mddev = rdev->mddev; |
a2826aa9 N |
379 | |
380 | rdev_dec_pending(rdev, mddev); | |
381 | ||
382 | if (atomic_dec_and_test(&mddev->flush_pending)) { | |
e9c7469b | 383 | /* The pre-request flush has finished */ |
e804ac78 | 384 | queue_work(md_wq, &mddev->flush_work); |
a2826aa9 N |
385 | } |
386 | bio_put(bio); | |
387 | } | |
388 | ||
a7a07e69 N |
389 | static void md_submit_flush_data(struct work_struct *ws); |
390 | ||
a035fc3e | 391 | static void submit_flushes(struct work_struct *ws) |
a2826aa9 | 392 | { |
fd01b88c | 393 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
3cb03002 | 394 | struct md_rdev *rdev; |
a2826aa9 | 395 | |
a7a07e69 N |
396 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
397 | atomic_set(&mddev->flush_pending, 1); | |
a2826aa9 | 398 | rcu_read_lock(); |
dafb20fa | 399 | rdev_for_each_rcu(rdev, mddev) |
a2826aa9 N |
400 | if (rdev->raid_disk >= 0 && |
401 | !test_bit(Faulty, &rdev->flags)) { | |
402 | /* Take two references, one is dropped | |
403 | * when request finishes, one after | |
404 | * we reclaim rcu_read_lock | |
405 | */ | |
406 | struct bio *bi; | |
407 | atomic_inc(&rdev->nr_pending); | |
408 | atomic_inc(&rdev->nr_pending); | |
409 | rcu_read_unlock(); | |
b5e1b8ce | 410 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
e9c7469b | 411 | bi->bi_end_io = md_end_flush; |
a2826aa9 N |
412 | bi->bi_private = rdev; |
413 | bi->bi_bdev = rdev->bdev; | |
414 | atomic_inc(&mddev->flush_pending); | |
e9c7469b | 415 | submit_bio(WRITE_FLUSH, bi); |
a2826aa9 N |
416 | rcu_read_lock(); |
417 | rdev_dec_pending(rdev, mddev); | |
418 | } | |
419 | rcu_read_unlock(); | |
a7a07e69 N |
420 | if (atomic_dec_and_test(&mddev->flush_pending)) |
421 | queue_work(md_wq, &mddev->flush_work); | |
a2826aa9 N |
422 | } |
423 | ||
e9c7469b | 424 | static void md_submit_flush_data(struct work_struct *ws) |
a2826aa9 | 425 | { |
fd01b88c | 426 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
e9c7469b | 427 | struct bio *bio = mddev->flush_bio; |
a2826aa9 | 428 | |
4f024f37 | 429 | if (bio->bi_iter.bi_size == 0) |
a2826aa9 N |
430 | /* an empty barrier - all done */ |
431 | bio_endio(bio, 0); | |
432 | else { | |
e9c7469b | 433 | bio->bi_rw &= ~REQ_FLUSH; |
5a7bbad2 | 434 | mddev->pers->make_request(mddev, bio); |
a2826aa9 | 435 | } |
2b74e12e N |
436 | |
437 | mddev->flush_bio = NULL; | |
438 | wake_up(&mddev->sb_wait); | |
a2826aa9 N |
439 | } |
440 | ||
fd01b88c | 441 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
a2826aa9 | 442 | { |
85572d7c | 443 | spin_lock_irq(&mddev->lock); |
a2826aa9 | 444 | wait_event_lock_irq(mddev->sb_wait, |
e9c7469b | 445 | !mddev->flush_bio, |
85572d7c | 446 | mddev->lock); |
e9c7469b | 447 | mddev->flush_bio = bio; |
85572d7c | 448 | spin_unlock_irq(&mddev->lock); |
a2826aa9 | 449 | |
a035fc3e N |
450 | INIT_WORK(&mddev->flush_work, submit_flushes); |
451 | queue_work(md_wq, &mddev->flush_work); | |
a2826aa9 | 452 | } |
e9c7469b | 453 | EXPORT_SYMBOL(md_flush_request); |
409c57f3 | 454 | |
74018dc3 | 455 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
97658cdd | 456 | { |
9cbb1750 N |
457 | struct mddev *mddev = cb->data; |
458 | md_wakeup_thread(mddev->thread); | |
459 | kfree(cb); | |
97658cdd | 460 | } |
9cbb1750 | 461 | EXPORT_SYMBOL(md_unplug); |
2ac87401 | 462 | |
fd01b88c | 463 | static inline struct mddev *mddev_get(struct mddev *mddev) |
1da177e4 LT |
464 | { |
465 | atomic_inc(&mddev->active); | |
466 | return mddev; | |
467 | } | |
468 | ||
5fd3a17e | 469 | static void mddev_delayed_delete(struct work_struct *ws); |
d3374825 | 470 | |
fd01b88c | 471 | static void mddev_put(struct mddev *mddev) |
1da177e4 | 472 | { |
a167f663 N |
473 | struct bio_set *bs = NULL; |
474 | ||
1da177e4 LT |
475 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
476 | return; | |
d3374825 | 477 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
cbd19983 N |
478 | mddev->ctime == 0 && !mddev->hold_active) { |
479 | /* Array is not configured at all, and not held active, | |
480 | * so destroy it */ | |
af8a2434 | 481 | list_del_init(&mddev->all_mddevs); |
a167f663 N |
482 | bs = mddev->bio_set; |
483 | mddev->bio_set = NULL; | |
d3374825 | 484 | if (mddev->gendisk) { |
e804ac78 TH |
485 | /* We did a probe so need to clean up. Call |
486 | * queue_work inside the spinlock so that | |
487 | * flush_workqueue() after mddev_find will | |
488 | * succeed in waiting for the work to be done. | |
d3374825 N |
489 | */ |
490 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); | |
e804ac78 | 491 | queue_work(md_misc_wq, &mddev->del_work); |
d3374825 N |
492 | } else |
493 | kfree(mddev); | |
494 | } | |
495 | spin_unlock(&all_mddevs_lock); | |
a167f663 N |
496 | if (bs) |
497 | bioset_free(bs); | |
1da177e4 LT |
498 | } |
499 | ||
fd01b88c | 500 | void mddev_init(struct mddev *mddev) |
fafd7fb0 N |
501 | { |
502 | mutex_init(&mddev->open_mutex); | |
503 | mutex_init(&mddev->reconfig_mutex); | |
504 | mutex_init(&mddev->bitmap_info.mutex); | |
505 | INIT_LIST_HEAD(&mddev->disks); | |
506 | INIT_LIST_HEAD(&mddev->all_mddevs); | |
507 | init_timer(&mddev->safemode_timer); | |
508 | atomic_set(&mddev->active, 1); | |
509 | atomic_set(&mddev->openers, 0); | |
510 | atomic_set(&mddev->active_io, 0); | |
85572d7c | 511 | spin_lock_init(&mddev->lock); |
fafd7fb0 N |
512 | atomic_set(&mddev->flush_pending, 0); |
513 | init_waitqueue_head(&mddev->sb_wait); | |
514 | init_waitqueue_head(&mddev->recovery_wait); | |
515 | mddev->reshape_position = MaxSector; | |
2c810cdd | 516 | mddev->reshape_backwards = 0; |
c4a39551 | 517 | mddev->last_sync_action = "none"; |
fafd7fb0 N |
518 | mddev->resync_min = 0; |
519 | mddev->resync_max = MaxSector; | |
520 | mddev->level = LEVEL_NONE; | |
521 | } | |
390ee602 | 522 | EXPORT_SYMBOL_GPL(mddev_init); |
fafd7fb0 | 523 | |
f72ffdd6 | 524 | static struct mddev *mddev_find(dev_t unit) |
1da177e4 | 525 | { |
fd01b88c | 526 | struct mddev *mddev, *new = NULL; |
1da177e4 | 527 | |
8f5f02c4 N |
528 | if (unit && MAJOR(unit) != MD_MAJOR) |
529 | unit &= ~((1<<MdpMinorShift)-1); | |
530 | ||
1da177e4 LT |
531 | retry: |
532 | spin_lock(&all_mddevs_lock); | |
efeb53c0 N |
533 | |
534 | if (unit) { | |
535 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) | |
536 | if (mddev->unit == unit) { | |
537 | mddev_get(mddev); | |
538 | spin_unlock(&all_mddevs_lock); | |
539 | kfree(new); | |
540 | return mddev; | |
541 | } | |
542 | ||
543 | if (new) { | |
544 | list_add(&new->all_mddevs, &all_mddevs); | |
1da177e4 | 545 | spin_unlock(&all_mddevs_lock); |
efeb53c0 N |
546 | new->hold_active = UNTIL_IOCTL; |
547 | return new; | |
1da177e4 | 548 | } |
efeb53c0 N |
549 | } else if (new) { |
550 | /* find an unused unit number */ | |
551 | static int next_minor = 512; | |
552 | int start = next_minor; | |
553 | int is_free = 0; | |
554 | int dev = 0; | |
555 | while (!is_free) { | |
556 | dev = MKDEV(MD_MAJOR, next_minor); | |
557 | next_minor++; | |
558 | if (next_minor > MINORMASK) | |
559 | next_minor = 0; | |
560 | if (next_minor == start) { | |
561 | /* Oh dear, all in use. */ | |
562 | spin_unlock(&all_mddevs_lock); | |
563 | kfree(new); | |
564 | return NULL; | |
565 | } | |
f72ffdd6 | 566 | |
efeb53c0 N |
567 | is_free = 1; |
568 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) | |
569 | if (mddev->unit == dev) { | |
570 | is_free = 0; | |
571 | break; | |
572 | } | |
573 | } | |
574 | new->unit = dev; | |
575 | new->md_minor = MINOR(dev); | |
576 | new->hold_active = UNTIL_STOP; | |
1da177e4 LT |
577 | list_add(&new->all_mddevs, &all_mddevs); |
578 | spin_unlock(&all_mddevs_lock); | |
579 | return new; | |
580 | } | |
581 | spin_unlock(&all_mddevs_lock); | |
582 | ||
9ffae0cf | 583 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
1da177e4 LT |
584 | if (!new) |
585 | return NULL; | |
586 | ||
1da177e4 LT |
587 | new->unit = unit; |
588 | if (MAJOR(unit) == MD_MAJOR) | |
589 | new->md_minor = MINOR(unit); | |
590 | else | |
591 | new->md_minor = MINOR(unit) >> MdpMinorShift; | |
592 | ||
fafd7fb0 | 593 | mddev_init(new); |
1da177e4 | 594 | |
1da177e4 LT |
595 | goto retry; |
596 | } | |
597 | ||
b6eb127d N |
598 | static struct attribute_group md_redundancy_group; |
599 | ||
5c47daf6 | 600 | void mddev_unlock(struct mddev *mddev) |
1da177e4 | 601 | { |
a64c876f | 602 | if (mddev->to_remove) { |
b6eb127d N |
603 | /* These cannot be removed under reconfig_mutex as |
604 | * an access to the files will try to take reconfig_mutex | |
605 | * while holding the file unremovable, which leads to | |
606 | * a deadlock. | |
bb4f1e9d N |
607 | * So hold set sysfs_active while the remove in happeing, |
608 | * and anything else which might set ->to_remove or my | |
609 | * otherwise change the sysfs namespace will fail with | |
610 | * -EBUSY if sysfs_active is still set. | |
611 | * We set sysfs_active under reconfig_mutex and elsewhere | |
612 | * test it under the same mutex to ensure its correct value | |
613 | * is seen. | |
b6eb127d | 614 | */ |
a64c876f N |
615 | struct attribute_group *to_remove = mddev->to_remove; |
616 | mddev->to_remove = NULL; | |
bb4f1e9d | 617 | mddev->sysfs_active = 1; |
b6eb127d N |
618 | mutex_unlock(&mddev->reconfig_mutex); |
619 | ||
00bcb4ac N |
620 | if (mddev->kobj.sd) { |
621 | if (to_remove != &md_redundancy_group) | |
622 | sysfs_remove_group(&mddev->kobj, to_remove); | |
623 | if (mddev->pers == NULL || | |
624 | mddev->pers->sync_request == NULL) { | |
625 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); | |
626 | if (mddev->sysfs_action) | |
627 | sysfs_put(mddev->sysfs_action); | |
628 | mddev->sysfs_action = NULL; | |
629 | } | |
a64c876f | 630 | } |
bb4f1e9d | 631 | mddev->sysfs_active = 0; |
b6eb127d N |
632 | } else |
633 | mutex_unlock(&mddev->reconfig_mutex); | |
1da177e4 | 634 | |
751e67ca CD |
635 | /* As we've dropped the mutex we need a spinlock to |
636 | * make sure the thread doesn't disappear | |
01f96c0a N |
637 | */ |
638 | spin_lock(&pers_lock); | |
005eca5e | 639 | md_wakeup_thread(mddev->thread); |
01f96c0a | 640 | spin_unlock(&pers_lock); |
1da177e4 | 641 | } |
5c47daf6 | 642 | EXPORT_SYMBOL_GPL(mddev_unlock); |
1da177e4 | 643 | |
1ca69c4b N |
644 | static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr) |
645 | { | |
646 | struct md_rdev *rdev; | |
647 | ||
648 | rdev_for_each_rcu(rdev, mddev) | |
649 | if (rdev->desc_nr == nr) | |
650 | return rdev; | |
651 | ||
652 | return NULL; | |
653 | } | |
654 | ||
655 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) | |
1da177e4 | 656 | { |
3cb03002 | 657 | struct md_rdev *rdev; |
1da177e4 | 658 | |
dafb20fa | 659 | rdev_for_each(rdev, mddev) |
1da177e4 LT |
660 | if (rdev->bdev->bd_dev == dev) |
661 | return rdev; | |
159ec1fc | 662 | |
1da177e4 LT |
663 | return NULL; |
664 | } | |
665 | ||
1ca69c4b N |
666 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
667 | { | |
668 | struct md_rdev *rdev; | |
669 | ||
670 | rdev_for_each_rcu(rdev, mddev) | |
671 | if (rdev->bdev->bd_dev == dev) | |
672 | return rdev; | |
673 | ||
674 | return NULL; | |
675 | } | |
676 | ||
84fc4b56 | 677 | static struct md_personality *find_pers(int level, char *clevel) |
2604b703 | 678 | { |
84fc4b56 | 679 | struct md_personality *pers; |
d9d166c2 N |
680 | list_for_each_entry(pers, &pers_list, list) { |
681 | if (level != LEVEL_NONE && pers->level == level) | |
2604b703 | 682 | return pers; |
d9d166c2 N |
683 | if (strcmp(pers->name, clevel)==0) |
684 | return pers; | |
685 | } | |
2604b703 N |
686 | return NULL; |
687 | } | |
688 | ||
b73df2d3 | 689 | /* return the offset of the super block in 512byte sectors */ |
3cb03002 | 690 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
1da177e4 | 691 | { |
57b2caa3 | 692 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
b73df2d3 | 693 | return MD_NEW_SIZE_SECTORS(num_sectors); |
1da177e4 LT |
694 | } |
695 | ||
f72ffdd6 | 696 | static int alloc_disk_sb(struct md_rdev *rdev) |
1da177e4 | 697 | { |
1da177e4 LT |
698 | rdev->sb_page = alloc_page(GFP_KERNEL); |
699 | if (!rdev->sb_page) { | |
700 | printk(KERN_ALERT "md: out of memory.\n"); | |
ebc24337 | 701 | return -ENOMEM; |
1da177e4 LT |
702 | } |
703 | ||
704 | return 0; | |
705 | } | |
706 | ||
545c8795 | 707 | void md_rdev_clear(struct md_rdev *rdev) |
1da177e4 LT |
708 | { |
709 | if (rdev->sb_page) { | |
2d1f3b5d | 710 | put_page(rdev->sb_page); |
1da177e4 LT |
711 | rdev->sb_loaded = 0; |
712 | rdev->sb_page = NULL; | |
0f420358 | 713 | rdev->sb_start = 0; |
dd8ac336 | 714 | rdev->sectors = 0; |
1da177e4 | 715 | } |
2699b672 N |
716 | if (rdev->bb_page) { |
717 | put_page(rdev->bb_page); | |
718 | rdev->bb_page = NULL; | |
719 | } | |
4fa2f327 N |
720 | kfree(rdev->badblocks.page); |
721 | rdev->badblocks.page = NULL; | |
1da177e4 | 722 | } |
545c8795 | 723 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
1da177e4 | 724 | |
6712ecf8 | 725 | static void super_written(struct bio *bio, int error) |
7bfa19f2 | 726 | { |
3cb03002 | 727 | struct md_rdev *rdev = bio->bi_private; |
fd01b88c | 728 | struct mddev *mddev = rdev->mddev; |
7bfa19f2 | 729 | |
3a0f5bbb N |
730 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
731 | printk("md: super_written gets error=%d, uptodate=%d\n", | |
732 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); | |
733 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); | |
a9701a30 | 734 | md_error(mddev, rdev); |
3a0f5bbb | 735 | } |
7bfa19f2 | 736 | |
a9701a30 N |
737 | if (atomic_dec_and_test(&mddev->pending_writes)) |
738 | wake_up(&mddev->sb_wait); | |
f8b58edf | 739 | bio_put(bio); |
7bfa19f2 N |
740 | } |
741 | ||
fd01b88c | 742 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
7bfa19f2 N |
743 | sector_t sector, int size, struct page *page) |
744 | { | |
745 | /* write first size bytes of page to sector of rdev | |
746 | * Increment mddev->pending_writes before returning | |
747 | * and decrement it on completion, waking up sb_wait | |
748 | * if zero is reached. | |
749 | * If an error occurred, call md_error | |
750 | */ | |
a167f663 | 751 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
7bfa19f2 | 752 | |
a6ff7e08 | 753 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
4f024f37 | 754 | bio->bi_iter.bi_sector = sector; |
7bfa19f2 N |
755 | bio_add_page(bio, page, size, 0); |
756 | bio->bi_private = rdev; | |
757 | bio->bi_end_io = super_written; | |
a9701a30 | 758 | |
7bfa19f2 | 759 | atomic_inc(&mddev->pending_writes); |
a5bf4df0 | 760 | submit_bio(WRITE_FLUSH_FUA, bio); |
a9701a30 N |
761 | } |
762 | ||
fd01b88c | 763 | void md_super_wait(struct mddev *mddev) |
a9701a30 | 764 | { |
e9c7469b | 765 | /* wait for all superblock writes that were scheduled to complete */ |
1967cd56 | 766 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
7bfa19f2 N |
767 | } |
768 | ||
3cb03002 | 769 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
ccebd4c4 | 770 | struct page *page, int rw, bool metadata_op) |
1da177e4 | 771 | { |
a167f663 | 772 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
1da177e4 LT |
773 | int ret; |
774 | ||
a6ff7e08 JB |
775 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
776 | rdev->meta_bdev : rdev->bdev; | |
ccebd4c4 | 777 | if (metadata_op) |
4f024f37 | 778 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
1fdd6fc9 N |
779 | else if (rdev->mddev->reshape_position != MaxSector && |
780 | (rdev->mddev->reshape_backwards == | |
781 | (sector >= rdev->mddev->reshape_position))) | |
4f024f37 | 782 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
ccebd4c4 | 783 | else |
4f024f37 | 784 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
1da177e4 | 785 | bio_add_page(bio, page, size, 0); |
c170bbb4 | 786 | submit_bio_wait(rw, bio); |
1da177e4 LT |
787 | |
788 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
789 | bio_put(bio); | |
790 | return ret; | |
791 | } | |
a8745db2 | 792 | EXPORT_SYMBOL_GPL(sync_page_io); |
1da177e4 | 793 | |
f72ffdd6 | 794 | static int read_disk_sb(struct md_rdev *rdev, int size) |
1da177e4 LT |
795 | { |
796 | char b[BDEVNAME_SIZE]; | |
403df478 | 797 | |
1da177e4 LT |
798 | if (rdev->sb_loaded) |
799 | return 0; | |
800 | ||
ccebd4c4 | 801 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
1da177e4 LT |
802 | goto fail; |
803 | rdev->sb_loaded = 1; | |
804 | return 0; | |
805 | ||
806 | fail: | |
807 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", | |
808 | bdevname(rdev->bdev,b)); | |
809 | return -EINVAL; | |
810 | } | |
811 | ||
812 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) | |
813 | { | |
f72ffdd6 | 814 | return sb1->set_uuid0 == sb2->set_uuid0 && |
05710466 AN |
815 | sb1->set_uuid1 == sb2->set_uuid1 && |
816 | sb1->set_uuid2 == sb2->set_uuid2 && | |
817 | sb1->set_uuid3 == sb2->set_uuid3; | |
1da177e4 LT |
818 | } |
819 | ||
1da177e4 LT |
820 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
821 | { | |
822 | int ret; | |
823 | mdp_super_t *tmp1, *tmp2; | |
824 | ||
825 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); | |
826 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); | |
827 | ||
828 | if (!tmp1 || !tmp2) { | |
829 | ret = 0; | |
35020f1a | 830 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
1da177e4 LT |
831 | goto abort; |
832 | } | |
833 | ||
834 | *tmp1 = *sb1; | |
835 | *tmp2 = *sb2; | |
836 | ||
837 | /* | |
838 | * nr_disks is not constant | |
839 | */ | |
840 | tmp1->nr_disks = 0; | |
841 | tmp2->nr_disks = 0; | |
842 | ||
ce0c8e05 | 843 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
1da177e4 | 844 | abort: |
990a8baf JJ |
845 | kfree(tmp1); |
846 | kfree(tmp2); | |
1da177e4 LT |
847 | return ret; |
848 | } | |
849 | ||
4d167f09 N |
850 | static u32 md_csum_fold(u32 csum) |
851 | { | |
852 | csum = (csum & 0xffff) + (csum >> 16); | |
853 | return (csum & 0xffff) + (csum >> 16); | |
854 | } | |
855 | ||
f72ffdd6 | 856 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
1da177e4 | 857 | { |
4d167f09 N |
858 | u64 newcsum = 0; |
859 | u32 *sb32 = (u32*)sb; | |
860 | int i; | |
1da177e4 LT |
861 | unsigned int disk_csum, csum; |
862 | ||
863 | disk_csum = sb->sb_csum; | |
864 | sb->sb_csum = 0; | |
4d167f09 N |
865 | |
866 | for (i = 0; i < MD_SB_BYTES/4 ; i++) | |
867 | newcsum += sb32[i]; | |
868 | csum = (newcsum & 0xffffffff) + (newcsum>>32); | |
869 | ||
4d167f09 N |
870 | #ifdef CONFIG_ALPHA |
871 | /* This used to use csum_partial, which was wrong for several | |
872 | * reasons including that different results are returned on | |
873 | * different architectures. It isn't critical that we get exactly | |
874 | * the same return value as before (we always csum_fold before | |
875 | * testing, and that removes any differences). However as we | |
876 | * know that csum_partial always returned a 16bit value on | |
877 | * alphas, do a fold to maximise conformity to previous behaviour. | |
878 | */ | |
879 | sb->sb_csum = md_csum_fold(disk_csum); | |
880 | #else | |
1da177e4 | 881 | sb->sb_csum = disk_csum; |
4d167f09 | 882 | #endif |
1da177e4 LT |
883 | return csum; |
884 | } | |
885 | ||
1da177e4 LT |
886 | /* |
887 | * Handle superblock details. | |
888 | * We want to be able to handle multiple superblock formats | |
889 | * so we have a common interface to them all, and an array of | |
890 | * different handlers. | |
891 | * We rely on user-space to write the initial superblock, and support | |
892 | * reading and updating of superblocks. | |
893 | * Interface methods are: | |
3cb03002 | 894 | * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
895 | * loads and validates a superblock on dev. |
896 | * if refdev != NULL, compare superblocks on both devices | |
897 | * Return: | |
898 | * 0 - dev has a superblock that is compatible with refdev | |
899 | * 1 - dev has a superblock that is compatible and newer than refdev | |
900 | * so dev should be used as the refdev in future | |
901 | * -EINVAL superblock incompatible or invalid | |
902 | * -othererror e.g. -EIO | |
903 | * | |
fd01b88c | 904 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
1da177e4 LT |
905 | * Verify that dev is acceptable into mddev. |
906 | * The first time, mddev->raid_disks will be 0, and data from | |
907 | * dev should be merged in. Subsequent calls check that dev | |
908 | * is new enough. Return 0 or -EINVAL | |
909 | * | |
fd01b88c | 910 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
1da177e4 LT |
911 | * Update the superblock for rdev with data in mddev |
912 | * This does not write to disc. | |
913 | * | |
914 | */ | |
915 | ||
916 | struct super_type { | |
0cd17fec CW |
917 | char *name; |
918 | struct module *owner; | |
c6563a8c N |
919 | int (*load_super)(struct md_rdev *rdev, |
920 | struct md_rdev *refdev, | |
0cd17fec | 921 | int minor_version); |
c6563a8c N |
922 | int (*validate_super)(struct mddev *mddev, |
923 | struct md_rdev *rdev); | |
924 | void (*sync_super)(struct mddev *mddev, | |
925 | struct md_rdev *rdev); | |
3cb03002 | 926 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
15f4a5fd | 927 | sector_t num_sectors); |
c6563a8c N |
928 | int (*allow_new_offset)(struct md_rdev *rdev, |
929 | unsigned long long new_offset); | |
1da177e4 LT |
930 | }; |
931 | ||
0894cc30 AN |
932 | /* |
933 | * Check that the given mddev has no bitmap. | |
934 | * | |
935 | * This function is called from the run method of all personalities that do not | |
936 | * support bitmaps. It prints an error message and returns non-zero if mddev | |
937 | * has a bitmap. Otherwise, it returns 0. | |
938 | * | |
939 | */ | |
fd01b88c | 940 | int md_check_no_bitmap(struct mddev *mddev) |
0894cc30 | 941 | { |
c3d9714e | 942 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
0894cc30 AN |
943 | return 0; |
944 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", | |
945 | mdname(mddev), mddev->pers->name); | |
946 | return 1; | |
947 | } | |
948 | EXPORT_SYMBOL(md_check_no_bitmap); | |
949 | ||
1da177e4 | 950 | /* |
f72ffdd6 | 951 | * load_super for 0.90.0 |
1da177e4 | 952 | */ |
3cb03002 | 953 | static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
954 | { |
955 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
956 | mdp_super_t *sb; | |
957 | int ret; | |
1da177e4 LT |
958 | |
959 | /* | |
0f420358 | 960 | * Calculate the position of the superblock (512byte sectors), |
1da177e4 LT |
961 | * it's at the end of the disk. |
962 | * | |
963 | * It also happens to be a multiple of 4Kb. | |
964 | */ | |
57b2caa3 | 965 | rdev->sb_start = calc_dev_sboffset(rdev); |
1da177e4 | 966 | |
0002b271 | 967 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
1da177e4 LT |
968 | if (ret) return ret; |
969 | ||
970 | ret = -EINVAL; | |
971 | ||
972 | bdevname(rdev->bdev, b); | |
65a06f06 | 973 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
974 | |
975 | if (sb->md_magic != MD_SB_MAGIC) { | |
976 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", | |
977 | b); | |
978 | goto abort; | |
979 | } | |
980 | ||
981 | if (sb->major_version != 0 || | |
f6705578 N |
982 | sb->minor_version < 90 || |
983 | sb->minor_version > 91) { | |
1da177e4 LT |
984 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
985 | sb->major_version, sb->minor_version, | |
986 | b); | |
987 | goto abort; | |
988 | } | |
989 | ||
990 | if (sb->raid_disks <= 0) | |
991 | goto abort; | |
992 | ||
4d167f09 | 993 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
1da177e4 LT |
994 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
995 | b); | |
996 | goto abort; | |
997 | } | |
998 | ||
999 | rdev->preferred_minor = sb->md_minor; | |
1000 | rdev->data_offset = 0; | |
c6563a8c | 1001 | rdev->new_data_offset = 0; |
0002b271 | 1002 | rdev->sb_size = MD_SB_BYTES; |
9f2f3830 | 1003 | rdev->badblocks.shift = -1; |
1da177e4 LT |
1004 | |
1005 | if (sb->level == LEVEL_MULTIPATH) | |
1006 | rdev->desc_nr = -1; | |
1007 | else | |
1008 | rdev->desc_nr = sb->this_disk.number; | |
1009 | ||
9a7b2b0f | 1010 | if (!refdev) { |
1da177e4 | 1011 | ret = 1; |
9a7b2b0f | 1012 | } else { |
1da177e4 | 1013 | __u64 ev1, ev2; |
65a06f06 | 1014 | mdp_super_t *refsb = page_address(refdev->sb_page); |
1da177e4 LT |
1015 | if (!uuid_equal(refsb, sb)) { |
1016 | printk(KERN_WARNING "md: %s has different UUID to %s\n", | |
1017 | b, bdevname(refdev->bdev,b2)); | |
1018 | goto abort; | |
1019 | } | |
1020 | if (!sb_equal(refsb, sb)) { | |
1021 | printk(KERN_WARNING "md: %s has same UUID" | |
1022 | " but different superblock to %s\n", | |
1023 | b, bdevname(refdev->bdev, b2)); | |
1024 | goto abort; | |
1025 | } | |
1026 | ev1 = md_event(sb); | |
1027 | ev2 = md_event(refsb); | |
1028 | if (ev1 > ev2) | |
1029 | ret = 1; | |
f72ffdd6 | 1030 | else |
1da177e4 LT |
1031 | ret = 0; |
1032 | } | |
8190e754 | 1033 | rdev->sectors = rdev->sb_start; |
667a5313 N |
1034 | /* Limit to 4TB as metadata cannot record more than that. |
1035 | * (not needed for Linear and RAID0 as metadata doesn't | |
1036 | * record this size) | |
1037 | */ | |
1038 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) | |
27a7b260 | 1039 | rdev->sectors = (2ULL << 32) - 2; |
1da177e4 | 1040 | |
27a7b260 | 1041 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
2bf071bf N |
1042 | /* "this cannot possibly happen" ... */ |
1043 | ret = -EINVAL; | |
1044 | ||
1da177e4 LT |
1045 | abort: |
1046 | return ret; | |
1047 | } | |
1048 | ||
1049 | /* | |
1050 | * validate_super for 0.90.0 | |
1051 | */ | |
fd01b88c | 1052 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1053 | { |
1054 | mdp_disk_t *desc; | |
65a06f06 | 1055 | mdp_super_t *sb = page_address(rdev->sb_page); |
07d84d10 | 1056 | __u64 ev1 = md_event(sb); |
1da177e4 | 1057 | |
41158c7e | 1058 | rdev->raid_disk = -1; |
c5d79adb N |
1059 | clear_bit(Faulty, &rdev->flags); |
1060 | clear_bit(In_sync, &rdev->flags); | |
8313b8e5 | 1061 | clear_bit(Bitmap_sync, &rdev->flags); |
c5d79adb | 1062 | clear_bit(WriteMostly, &rdev->flags); |
c5d79adb | 1063 | |
1da177e4 LT |
1064 | if (mddev->raid_disks == 0) { |
1065 | mddev->major_version = 0; | |
1066 | mddev->minor_version = sb->minor_version; | |
1067 | mddev->patch_version = sb->patch_version; | |
e691063a | 1068 | mddev->external = 0; |
9d8f0363 | 1069 | mddev->chunk_sectors = sb->chunk_size >> 9; |
1da177e4 LT |
1070 | mddev->ctime = sb->ctime; |
1071 | mddev->utime = sb->utime; | |
1072 | mddev->level = sb->level; | |
d9d166c2 | 1073 | mddev->clevel[0] = 0; |
1da177e4 LT |
1074 | mddev->layout = sb->layout; |
1075 | mddev->raid_disks = sb->raid_disks; | |
27a7b260 | 1076 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
07d84d10 | 1077 | mddev->events = ev1; |
c3d9714e | 1078 | mddev->bitmap_info.offset = 0; |
6409bb05 N |
1079 | mddev->bitmap_info.space = 0; |
1080 | /* bitmap can use 60 K after the 4K superblocks */ | |
c3d9714e | 1081 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
6409bb05 | 1082 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
2c810cdd | 1083 | mddev->reshape_backwards = 0; |
1da177e4 | 1084 | |
f6705578 N |
1085 | if (mddev->minor_version >= 91) { |
1086 | mddev->reshape_position = sb->reshape_position; | |
1087 | mddev->delta_disks = sb->delta_disks; | |
1088 | mddev->new_level = sb->new_level; | |
1089 | mddev->new_layout = sb->new_layout; | |
664e7c41 | 1090 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
2c810cdd N |
1091 | if (mddev->delta_disks < 0) |
1092 | mddev->reshape_backwards = 1; | |
f6705578 N |
1093 | } else { |
1094 | mddev->reshape_position = MaxSector; | |
1095 | mddev->delta_disks = 0; | |
1096 | mddev->new_level = mddev->level; | |
1097 | mddev->new_layout = mddev->layout; | |
664e7c41 | 1098 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
1099 | } |
1100 | ||
1da177e4 LT |
1101 | if (sb->state & (1<<MD_SB_CLEAN)) |
1102 | mddev->recovery_cp = MaxSector; | |
1103 | else { | |
f72ffdd6 | 1104 | if (sb->events_hi == sb->cp_events_hi && |
1da177e4 LT |
1105 | sb->events_lo == sb->cp_events_lo) { |
1106 | mddev->recovery_cp = sb->recovery_cp; | |
1107 | } else | |
1108 | mddev->recovery_cp = 0; | |
1109 | } | |
1110 | ||
1111 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); | |
1112 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); | |
1113 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); | |
1114 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); | |
1115 | ||
1116 | mddev->max_disks = MD_SB_DISKS; | |
a654b9d8 N |
1117 | |
1118 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && | |
6409bb05 | 1119 | mddev->bitmap_info.file == NULL) { |
c3d9714e N |
1120 | mddev->bitmap_info.offset = |
1121 | mddev->bitmap_info.default_offset; | |
6409bb05 | 1122 | mddev->bitmap_info.space = |
c9ad020f | 1123 | mddev->bitmap_info.default_space; |
6409bb05 | 1124 | } |
a654b9d8 | 1125 | |
41158c7e | 1126 | } else if (mddev->pers == NULL) { |
be6800a7 N |
1127 | /* Insist on good event counter while assembling, except |
1128 | * for spares (which don't need an event count) */ | |
1da177e4 | 1129 | ++ev1; |
be6800a7 N |
1130 | if (sb->disks[rdev->desc_nr].state & ( |
1131 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) | |
f72ffdd6 | 1132 | if (ev1 < mddev->events) |
be6800a7 | 1133 | return -EINVAL; |
41158c7e N |
1134 | } else if (mddev->bitmap) { |
1135 | /* if adding to array with a bitmap, then we can accept an | |
1136 | * older device ... but not too old. | |
1137 | */ | |
41158c7e N |
1138 | if (ev1 < mddev->bitmap->events_cleared) |
1139 | return 0; | |
8313b8e5 N |
1140 | if (ev1 < mddev->events) |
1141 | set_bit(Bitmap_sync, &rdev->flags); | |
07d84d10 N |
1142 | } else { |
1143 | if (ev1 < mddev->events) | |
1144 | /* just a hot-add of a new device, leave raid_disk at -1 */ | |
1145 | return 0; | |
1146 | } | |
41158c7e | 1147 | |
1da177e4 | 1148 | if (mddev->level != LEVEL_MULTIPATH) { |
1da177e4 LT |
1149 | desc = sb->disks + rdev->desc_nr; |
1150 | ||
1151 | if (desc->state & (1<<MD_DISK_FAULTY)) | |
b2d444d7 | 1152 | set_bit(Faulty, &rdev->flags); |
7c7546cc N |
1153 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
1154 | desc->raid_disk < mddev->raid_disks */) { | |
b2d444d7 | 1155 | set_bit(In_sync, &rdev->flags); |
1da177e4 | 1156 | rdev->raid_disk = desc->raid_disk; |
f466722c | 1157 | rdev->saved_raid_disk = desc->raid_disk; |
0261cd9f N |
1158 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
1159 | /* active but not in sync implies recovery up to | |
1160 | * reshape position. We don't know exactly where | |
1161 | * that is, so set to zero for now */ | |
1162 | if (mddev->minor_version >= 91) { | |
1163 | rdev->recovery_offset = 0; | |
1164 | rdev->raid_disk = desc->raid_disk; | |
1165 | } | |
1da177e4 | 1166 | } |
8ddf9efe N |
1167 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
1168 | set_bit(WriteMostly, &rdev->flags); | |
41158c7e | 1169 | } else /* MULTIPATH are always insync */ |
b2d444d7 | 1170 | set_bit(In_sync, &rdev->flags); |
1da177e4 LT |
1171 | return 0; |
1172 | } | |
1173 | ||
1174 | /* | |
1175 | * sync_super for 0.90.0 | |
1176 | */ | |
fd01b88c | 1177 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1178 | { |
1179 | mdp_super_t *sb; | |
3cb03002 | 1180 | struct md_rdev *rdev2; |
1da177e4 | 1181 | int next_spare = mddev->raid_disks; |
19133a42 | 1182 | |
1da177e4 LT |
1183 | /* make rdev->sb match mddev data.. |
1184 | * | |
1185 | * 1/ zero out disks | |
1186 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); | |
1187 | * 3/ any empty disks < next_spare become removed | |
1188 | * | |
1189 | * disks[0] gets initialised to REMOVED because | |
1190 | * we cannot be sure from other fields if it has | |
1191 | * been initialised or not. | |
1192 | */ | |
1193 | int i; | |
1194 | int active=0, working=0,failed=0,spare=0,nr_disks=0; | |
1195 | ||
61181565 N |
1196 | rdev->sb_size = MD_SB_BYTES; |
1197 | ||
65a06f06 | 1198 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1199 | |
1200 | memset(sb, 0, sizeof(*sb)); | |
1201 | ||
1202 | sb->md_magic = MD_SB_MAGIC; | |
1203 | sb->major_version = mddev->major_version; | |
1da177e4 LT |
1204 | sb->patch_version = mddev->patch_version; |
1205 | sb->gvalid_words = 0; /* ignored */ | |
1206 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); | |
1207 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); | |
1208 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); | |
1209 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); | |
1210 | ||
1211 | sb->ctime = mddev->ctime; | |
1212 | sb->level = mddev->level; | |
58c0fed4 | 1213 | sb->size = mddev->dev_sectors / 2; |
1da177e4 LT |
1214 | sb->raid_disks = mddev->raid_disks; |
1215 | sb->md_minor = mddev->md_minor; | |
e691063a | 1216 | sb->not_persistent = 0; |
1da177e4 LT |
1217 | sb->utime = mddev->utime; |
1218 | sb->state = 0; | |
1219 | sb->events_hi = (mddev->events>>32); | |
1220 | sb->events_lo = (u32)mddev->events; | |
1221 | ||
f6705578 N |
1222 | if (mddev->reshape_position == MaxSector) |
1223 | sb->minor_version = 90; | |
1224 | else { | |
1225 | sb->minor_version = 91; | |
1226 | sb->reshape_position = mddev->reshape_position; | |
1227 | sb->new_level = mddev->new_level; | |
1228 | sb->delta_disks = mddev->delta_disks; | |
1229 | sb->new_layout = mddev->new_layout; | |
664e7c41 | 1230 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
f6705578 N |
1231 | } |
1232 | mddev->minor_version = sb->minor_version; | |
1da177e4 LT |
1233 | if (mddev->in_sync) |
1234 | { | |
1235 | sb->recovery_cp = mddev->recovery_cp; | |
1236 | sb->cp_events_hi = (mddev->events>>32); | |
1237 | sb->cp_events_lo = (u32)mddev->events; | |
1238 | if (mddev->recovery_cp == MaxSector) | |
1239 | sb->state = (1<< MD_SB_CLEAN); | |
1240 | } else | |
1241 | sb->recovery_cp = 0; | |
1242 | ||
1243 | sb->layout = mddev->layout; | |
9d8f0363 | 1244 | sb->chunk_size = mddev->chunk_sectors << 9; |
1da177e4 | 1245 | |
c3d9714e | 1246 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
a654b9d8 N |
1247 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
1248 | ||
1da177e4 | 1249 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
dafb20fa | 1250 | rdev_for_each(rdev2, mddev) { |
1da177e4 | 1251 | mdp_disk_t *d; |
86e6ffdd | 1252 | int desc_nr; |
0261cd9f N |
1253 | int is_active = test_bit(In_sync, &rdev2->flags); |
1254 | ||
1255 | if (rdev2->raid_disk >= 0 && | |
1256 | sb->minor_version >= 91) | |
1257 | /* we have nowhere to store the recovery_offset, | |
1258 | * but if it is not below the reshape_position, | |
1259 | * we can piggy-back on that. | |
1260 | */ | |
1261 | is_active = 1; | |
1262 | if (rdev2->raid_disk < 0 || | |
1263 | test_bit(Faulty, &rdev2->flags)) | |
1264 | is_active = 0; | |
1265 | if (is_active) | |
86e6ffdd | 1266 | desc_nr = rdev2->raid_disk; |
1da177e4 | 1267 | else |
86e6ffdd | 1268 | desc_nr = next_spare++; |
19133a42 | 1269 | rdev2->desc_nr = desc_nr; |
1da177e4 LT |
1270 | d = &sb->disks[rdev2->desc_nr]; |
1271 | nr_disks++; | |
1272 | d->number = rdev2->desc_nr; | |
1273 | d->major = MAJOR(rdev2->bdev->bd_dev); | |
1274 | d->minor = MINOR(rdev2->bdev->bd_dev); | |
0261cd9f | 1275 | if (is_active) |
1da177e4 LT |
1276 | d->raid_disk = rdev2->raid_disk; |
1277 | else | |
1278 | d->raid_disk = rdev2->desc_nr; /* compatibility */ | |
1be7892f | 1279 | if (test_bit(Faulty, &rdev2->flags)) |
1da177e4 | 1280 | d->state = (1<<MD_DISK_FAULTY); |
0261cd9f | 1281 | else if (is_active) { |
1da177e4 | 1282 | d->state = (1<<MD_DISK_ACTIVE); |
0261cd9f N |
1283 | if (test_bit(In_sync, &rdev2->flags)) |
1284 | d->state |= (1<<MD_DISK_SYNC); | |
1da177e4 LT |
1285 | active++; |
1286 | working++; | |
1287 | } else { | |
1288 | d->state = 0; | |
1289 | spare++; | |
1290 | working++; | |
1291 | } | |
8ddf9efe N |
1292 | if (test_bit(WriteMostly, &rdev2->flags)) |
1293 | d->state |= (1<<MD_DISK_WRITEMOSTLY); | |
1da177e4 | 1294 | } |
1da177e4 LT |
1295 | /* now set the "removed" and "faulty" bits on any missing devices */ |
1296 | for (i=0 ; i < mddev->raid_disks ; i++) { | |
1297 | mdp_disk_t *d = &sb->disks[i]; | |
1298 | if (d->state == 0 && d->number == 0) { | |
1299 | d->number = i; | |
1300 | d->raid_disk = i; | |
1301 | d->state = (1<<MD_DISK_REMOVED); | |
1302 | d->state |= (1<<MD_DISK_FAULTY); | |
1303 | failed++; | |
1304 | } | |
1305 | } | |
1306 | sb->nr_disks = nr_disks; | |
1307 | sb->active_disks = active; | |
1308 | sb->working_disks = working; | |
1309 | sb->failed_disks = failed; | |
1310 | sb->spare_disks = spare; | |
1311 | ||
1312 | sb->this_disk = sb->disks[rdev->desc_nr]; | |
1313 | sb->sb_csum = calc_sb_csum(sb); | |
1314 | } | |
1315 | ||
0cd17fec CW |
1316 | /* |
1317 | * rdev_size_change for 0.90.0 | |
1318 | */ | |
1319 | static unsigned long long | |
3cb03002 | 1320 | super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
0cd17fec | 1321 | { |
58c0fed4 | 1322 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
0cd17fec | 1323 | return 0; /* component must fit device */ |
c3d9714e | 1324 | if (rdev->mddev->bitmap_info.offset) |
0cd17fec | 1325 | return 0; /* can't move bitmap */ |
57b2caa3 | 1326 | rdev->sb_start = calc_dev_sboffset(rdev); |
15f4a5fd AN |
1327 | if (!num_sectors || num_sectors > rdev->sb_start) |
1328 | num_sectors = rdev->sb_start; | |
27a7b260 N |
1329 | /* Limit to 4TB as metadata cannot record more than that. |
1330 | * 4TB == 2^32 KB, or 2*2^32 sectors. | |
1331 | */ | |
667a5313 | 1332 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
27a7b260 | 1333 | num_sectors = (2ULL << 32) - 2; |
0f420358 | 1334 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
0cd17fec CW |
1335 | rdev->sb_page); |
1336 | md_super_wait(rdev->mddev); | |
c26a44ed | 1337 | return num_sectors; |
0cd17fec CW |
1338 | } |
1339 | ||
c6563a8c N |
1340 | static int |
1341 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) | |
1342 | { | |
1343 | /* non-zero offset changes not possible with v0.90 */ | |
1344 | return new_offset == 0; | |
1345 | } | |
0cd17fec | 1346 | |
1da177e4 LT |
1347 | /* |
1348 | * version 1 superblock | |
1349 | */ | |
1350 | ||
f72ffdd6 | 1351 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
1da177e4 | 1352 | { |
1c05b4bc N |
1353 | __le32 disk_csum; |
1354 | u32 csum; | |
1da177e4 LT |
1355 | unsigned long long newcsum; |
1356 | int size = 256 + le32_to_cpu(sb->max_dev)*2; | |
1c05b4bc | 1357 | __le32 *isuper = (__le32*)sb; |
1da177e4 LT |
1358 | |
1359 | disk_csum = sb->sb_csum; | |
1360 | sb->sb_csum = 0; | |
1361 | newcsum = 0; | |
1f3c9907 | 1362 | for (; size >= 4; size -= 4) |
1da177e4 LT |
1363 | newcsum += le32_to_cpu(*isuper++); |
1364 | ||
1365 | if (size == 2) | |
1c05b4bc | 1366 | newcsum += le16_to_cpu(*(__le16*) isuper); |
1da177e4 LT |
1367 | |
1368 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); | |
1369 | sb->sb_csum = disk_csum; | |
1370 | return cpu_to_le32(csum); | |
1371 | } | |
1372 | ||
2699b672 N |
1373 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
1374 | int acknowledged); | |
3cb03002 | 1375 | static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
1376 | { |
1377 | struct mdp_superblock_1 *sb; | |
1378 | int ret; | |
0f420358 | 1379 | sector_t sb_start; |
c6563a8c | 1380 | sector_t sectors; |
1da177e4 | 1381 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
0002b271 | 1382 | int bmask; |
1da177e4 LT |
1383 | |
1384 | /* | |
0f420358 | 1385 | * Calculate the position of the superblock in 512byte sectors. |
1da177e4 LT |
1386 | * It is always aligned to a 4K boundary and |
1387 | * depeding on minor_version, it can be: | |
1388 | * 0: At least 8K, but less than 12K, from end of device | |
1389 | * 1: At start of device | |
1390 | * 2: 4K from start of device. | |
1391 | */ | |
1392 | switch(minor_version) { | |
1393 | case 0: | |
77304d2a | 1394 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
0f420358 AN |
1395 | sb_start -= 8*2; |
1396 | sb_start &= ~(sector_t)(4*2-1); | |
1da177e4 LT |
1397 | break; |
1398 | case 1: | |
0f420358 | 1399 | sb_start = 0; |
1da177e4 LT |
1400 | break; |
1401 | case 2: | |
0f420358 | 1402 | sb_start = 8; |
1da177e4 LT |
1403 | break; |
1404 | default: | |
1405 | return -EINVAL; | |
1406 | } | |
0f420358 | 1407 | rdev->sb_start = sb_start; |
1da177e4 | 1408 | |
0002b271 N |
1409 | /* superblock is rarely larger than 1K, but it can be larger, |
1410 | * and it is safe to read 4k, so we do that | |
1411 | */ | |
1412 | ret = read_disk_sb(rdev, 4096); | |
1da177e4 LT |
1413 | if (ret) return ret; |
1414 | ||
65a06f06 | 1415 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1416 | |
1417 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || | |
1418 | sb->major_version != cpu_to_le32(1) || | |
1419 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || | |
0f420358 | 1420 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
71c0805c | 1421 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
1da177e4 LT |
1422 | return -EINVAL; |
1423 | ||
1424 | if (calc_sb_1_csum(sb) != sb->sb_csum) { | |
1425 | printk("md: invalid superblock checksum on %s\n", | |
1426 | bdevname(rdev->bdev,b)); | |
1427 | return -EINVAL; | |
1428 | } | |
1429 | if (le64_to_cpu(sb->data_size) < 10) { | |
1430 | printk("md: data_size too small on %s\n", | |
1431 | bdevname(rdev->bdev,b)); | |
1432 | return -EINVAL; | |
1433 | } | |
c6563a8c N |
1434 | if (sb->pad0 || |
1435 | sb->pad3[0] || | |
1436 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) | |
1437 | /* Some padding is non-zero, might be a new feature */ | |
1438 | return -EINVAL; | |
e11e93fa | 1439 | |
1da177e4 LT |
1440 | rdev->preferred_minor = 0xffff; |
1441 | rdev->data_offset = le64_to_cpu(sb->data_offset); | |
c6563a8c N |
1442 | rdev->new_data_offset = rdev->data_offset; |
1443 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && | |
1444 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) | |
1445 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); | |
4dbcdc75 | 1446 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
1da177e4 | 1447 | |
0002b271 | 1448 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
e1defc4f | 1449 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
0002b271 | 1450 | if (rdev->sb_size & bmask) |
a1801f85 N |
1451 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
1452 | ||
1453 | if (minor_version | |
0f420358 | 1454 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
a1801f85 | 1455 | return -EINVAL; |
c6563a8c N |
1456 | if (minor_version |
1457 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) | |
1458 | return -EINVAL; | |
0002b271 | 1459 | |
31b65a0d N |
1460 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
1461 | rdev->desc_nr = -1; | |
1462 | else | |
1463 | rdev->desc_nr = le32_to_cpu(sb->dev_number); | |
1464 | ||
2699b672 N |
1465 | if (!rdev->bb_page) { |
1466 | rdev->bb_page = alloc_page(GFP_KERNEL); | |
1467 | if (!rdev->bb_page) | |
1468 | return -ENOMEM; | |
1469 | } | |
1470 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && | |
1471 | rdev->badblocks.count == 0) { | |
1472 | /* need to load the bad block list. | |
1473 | * Currently we limit it to one page. | |
1474 | */ | |
1475 | s32 offset; | |
1476 | sector_t bb_sector; | |
1477 | u64 *bbp; | |
1478 | int i; | |
1479 | int sectors = le16_to_cpu(sb->bblog_size); | |
1480 | if (sectors > (PAGE_SIZE / 512)) | |
1481 | return -EINVAL; | |
1482 | offset = le32_to_cpu(sb->bblog_offset); | |
1483 | if (offset == 0) | |
1484 | return -EINVAL; | |
1485 | bb_sector = (long long)offset; | |
1486 | if (!sync_page_io(rdev, bb_sector, sectors << 9, | |
1487 | rdev->bb_page, READ, true)) | |
1488 | return -EIO; | |
1489 | bbp = (u64 *)page_address(rdev->bb_page); | |
1490 | rdev->badblocks.shift = sb->bblog_shift; | |
1491 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { | |
1492 | u64 bb = le64_to_cpu(*bbp); | |
1493 | int count = bb & (0x3ff); | |
1494 | u64 sector = bb >> 10; | |
1495 | sector <<= sb->bblog_shift; | |
1496 | count <<= sb->bblog_shift; | |
1497 | if (bb + 1 == 0) | |
1498 | break; | |
1499 | if (md_set_badblocks(&rdev->badblocks, | |
1500 | sector, count, 1) == 0) | |
1501 | return -EINVAL; | |
1502 | } | |
486adf72 N |
1503 | } else if (sb->bblog_offset != 0) |
1504 | rdev->badblocks.shift = 0; | |
2699b672 | 1505 | |
9a7b2b0f | 1506 | if (!refdev) { |
8ed75463 | 1507 | ret = 1; |
9a7b2b0f | 1508 | } else { |
1da177e4 | 1509 | __u64 ev1, ev2; |
65a06f06 | 1510 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
1da177e4 LT |
1511 | |
1512 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || | |
1513 | sb->level != refsb->level || | |
1514 | sb->layout != refsb->layout || | |
1515 | sb->chunksize != refsb->chunksize) { | |
1516 | printk(KERN_WARNING "md: %s has strangely different" | |
1517 | " superblock to %s\n", | |
1518 | bdevname(rdev->bdev,b), | |
1519 | bdevname(refdev->bdev,b2)); | |
1520 | return -EINVAL; | |
1521 | } | |
1522 | ev1 = le64_to_cpu(sb->events); | |
1523 | ev2 = le64_to_cpu(refsb->events); | |
1524 | ||
1525 | if (ev1 > ev2) | |
8ed75463 N |
1526 | ret = 1; |
1527 | else | |
1528 | ret = 0; | |
1da177e4 | 1529 | } |
c6563a8c N |
1530 | if (minor_version) { |
1531 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); | |
1532 | sectors -= rdev->data_offset; | |
1533 | } else | |
1534 | sectors = rdev->sb_start; | |
1535 | if (sectors < le64_to_cpu(sb->data_size)) | |
1da177e4 | 1536 | return -EINVAL; |
dd8ac336 | 1537 | rdev->sectors = le64_to_cpu(sb->data_size); |
8ed75463 | 1538 | return ret; |
1da177e4 LT |
1539 | } |
1540 | ||
fd01b88c | 1541 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1542 | { |
65a06f06 | 1543 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
07d84d10 | 1544 | __u64 ev1 = le64_to_cpu(sb->events); |
1da177e4 | 1545 | |
41158c7e | 1546 | rdev->raid_disk = -1; |
c5d79adb N |
1547 | clear_bit(Faulty, &rdev->flags); |
1548 | clear_bit(In_sync, &rdev->flags); | |
8313b8e5 | 1549 | clear_bit(Bitmap_sync, &rdev->flags); |
c5d79adb | 1550 | clear_bit(WriteMostly, &rdev->flags); |
c5d79adb | 1551 | |
1da177e4 LT |
1552 | if (mddev->raid_disks == 0) { |
1553 | mddev->major_version = 1; | |
1554 | mddev->patch_version = 0; | |
e691063a | 1555 | mddev->external = 0; |
9d8f0363 | 1556 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
1da177e4 LT |
1557 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
1558 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); | |
1559 | mddev->level = le32_to_cpu(sb->level); | |
d9d166c2 | 1560 | mddev->clevel[0] = 0; |
1da177e4 LT |
1561 | mddev->layout = le32_to_cpu(sb->layout); |
1562 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); | |
58c0fed4 | 1563 | mddev->dev_sectors = le64_to_cpu(sb->size); |
07d84d10 | 1564 | mddev->events = ev1; |
c3d9714e | 1565 | mddev->bitmap_info.offset = 0; |
6409bb05 N |
1566 | mddev->bitmap_info.space = 0; |
1567 | /* Default location for bitmap is 1K after superblock | |
1568 | * using 3K - total of 4K | |
1569 | */ | |
c3d9714e | 1570 | mddev->bitmap_info.default_offset = 1024 >> 9; |
6409bb05 | 1571 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
2c810cdd N |
1572 | mddev->reshape_backwards = 0; |
1573 | ||
1da177e4 LT |
1574 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
1575 | memcpy(mddev->uuid, sb->set_uuid, 16); | |
1576 | ||
1577 | mddev->max_disks = (4096-256)/2; | |
a654b9d8 | 1578 | |
71c0805c | 1579 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
6409bb05 | 1580 | mddev->bitmap_info.file == NULL) { |
c3d9714e N |
1581 | mddev->bitmap_info.offset = |
1582 | (__s32)le32_to_cpu(sb->bitmap_offset); | |
6409bb05 N |
1583 | /* Metadata doesn't record how much space is available. |
1584 | * For 1.0, we assume we can use up to the superblock | |
1585 | * if before, else to 4K beyond superblock. | |
1586 | * For others, assume no change is possible. | |
1587 | */ | |
1588 | if (mddev->minor_version > 0) | |
1589 | mddev->bitmap_info.space = 0; | |
1590 | else if (mddev->bitmap_info.offset > 0) | |
1591 | mddev->bitmap_info.space = | |
1592 | 8 - mddev->bitmap_info.offset; | |
1593 | else | |
1594 | mddev->bitmap_info.space = | |
1595 | -mddev->bitmap_info.offset; | |
1596 | } | |
e11e93fa | 1597 | |
f6705578 N |
1598 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
1599 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); | |
1600 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); | |
1601 | mddev->new_level = le32_to_cpu(sb->new_level); | |
1602 | mddev->new_layout = le32_to_cpu(sb->new_layout); | |
664e7c41 | 1603 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
2c810cdd N |
1604 | if (mddev->delta_disks < 0 || |
1605 | (mddev->delta_disks == 0 && | |
1606 | (le32_to_cpu(sb->feature_map) | |
1607 | & MD_FEATURE_RESHAPE_BACKWARDS))) | |
1608 | mddev->reshape_backwards = 1; | |
f6705578 N |
1609 | } else { |
1610 | mddev->reshape_position = MaxSector; | |
1611 | mddev->delta_disks = 0; | |
1612 | mddev->new_level = mddev->level; | |
1613 | mddev->new_layout = mddev->layout; | |
664e7c41 | 1614 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
1615 | } |
1616 | ||
41158c7e | 1617 | } else if (mddev->pers == NULL) { |
be6800a7 N |
1618 | /* Insist of good event counter while assembling, except for |
1619 | * spares (which don't need an event count) */ | |
1da177e4 | 1620 | ++ev1; |
be6800a7 N |
1621 | if (rdev->desc_nr >= 0 && |
1622 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && | |
1623 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) | |
1624 | if (ev1 < mddev->events) | |
1625 | return -EINVAL; | |
41158c7e N |
1626 | } else if (mddev->bitmap) { |
1627 | /* If adding to array with a bitmap, then we can accept an | |
1628 | * older device, but not too old. | |
1629 | */ | |
41158c7e N |
1630 | if (ev1 < mddev->bitmap->events_cleared) |
1631 | return 0; | |
8313b8e5 N |
1632 | if (ev1 < mddev->events) |
1633 | set_bit(Bitmap_sync, &rdev->flags); | |
07d84d10 N |
1634 | } else { |
1635 | if (ev1 < mddev->events) | |
1636 | /* just a hot-add of a new device, leave raid_disk at -1 */ | |
1637 | return 0; | |
1638 | } | |
1da177e4 LT |
1639 | if (mddev->level != LEVEL_MULTIPATH) { |
1640 | int role; | |
3673f305 N |
1641 | if (rdev->desc_nr < 0 || |
1642 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { | |
1643 | role = 0xffff; | |
1644 | rdev->desc_nr = -1; | |
1645 | } else | |
1646 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); | |
1da177e4 LT |
1647 | switch(role) { |
1648 | case 0xffff: /* spare */ | |
1da177e4 LT |
1649 | break; |
1650 | case 0xfffe: /* faulty */ | |
b2d444d7 | 1651 | set_bit(Faulty, &rdev->flags); |
1da177e4 LT |
1652 | break; |
1653 | default: | |
f466722c | 1654 | rdev->saved_raid_disk = role; |
5fd6c1dc | 1655 | if ((le32_to_cpu(sb->feature_map) & |
f466722c | 1656 | MD_FEATURE_RECOVERY_OFFSET)) { |
5fd6c1dc | 1657 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
f466722c N |
1658 | if (!(le32_to_cpu(sb->feature_map) & |
1659 | MD_FEATURE_RECOVERY_BITMAP)) | |
1660 | rdev->saved_raid_disk = -1; | |
1661 | } else | |
5fd6c1dc | 1662 | set_bit(In_sync, &rdev->flags); |
1da177e4 LT |
1663 | rdev->raid_disk = role; |
1664 | break; | |
1665 | } | |
8ddf9efe N |
1666 | if (sb->devflags & WriteMostly1) |
1667 | set_bit(WriteMostly, &rdev->flags); | |
2d78f8c4 N |
1668 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
1669 | set_bit(Replacement, &rdev->flags); | |
41158c7e | 1670 | } else /* MULTIPATH are always insync */ |
b2d444d7 | 1671 | set_bit(In_sync, &rdev->flags); |
41158c7e | 1672 | |
1da177e4 LT |
1673 | return 0; |
1674 | } | |
1675 | ||
fd01b88c | 1676 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1677 | { |
1678 | struct mdp_superblock_1 *sb; | |
3cb03002 | 1679 | struct md_rdev *rdev2; |
1da177e4 LT |
1680 | int max_dev, i; |
1681 | /* make rdev->sb match mddev and rdev data. */ | |
1682 | ||
65a06f06 | 1683 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1684 | |
1685 | sb->feature_map = 0; | |
1686 | sb->pad0 = 0; | |
5fd6c1dc | 1687 | sb->recovery_offset = cpu_to_le64(0); |
1da177e4 LT |
1688 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
1689 | ||
1690 | sb->utime = cpu_to_le64((__u64)mddev->utime); | |
1691 | sb->events = cpu_to_le64(mddev->events); | |
1692 | if (mddev->in_sync) | |
1693 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); | |
1694 | else | |
1695 | sb->resync_offset = cpu_to_le64(0); | |
1696 | ||
1c05b4bc | 1697 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
4dbcdc75 | 1698 | |
f0ca340c | 1699 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
58c0fed4 | 1700 | sb->size = cpu_to_le64(mddev->dev_sectors); |
9d8f0363 | 1701 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
62e1e389 N |
1702 | sb->level = cpu_to_le32(mddev->level); |
1703 | sb->layout = cpu_to_le32(mddev->layout); | |
f0ca340c | 1704 | |
aeb9b211 N |
1705 | if (test_bit(WriteMostly, &rdev->flags)) |
1706 | sb->devflags |= WriteMostly1; | |
1707 | else | |
1708 | sb->devflags &= ~WriteMostly1; | |
c6563a8c N |
1709 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
1710 | sb->data_size = cpu_to_le64(rdev->sectors); | |
aeb9b211 | 1711 | |
c3d9714e N |
1712 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
1713 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); | |
71c0805c | 1714 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
a654b9d8 | 1715 | } |
5fd6c1dc N |
1716 | |
1717 | if (rdev->raid_disk >= 0 && | |
97e4f42d | 1718 | !test_bit(In_sync, &rdev->flags)) { |
93be75ff N |
1719 | sb->feature_map |= |
1720 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); | |
1721 | sb->recovery_offset = | |
1722 | cpu_to_le64(rdev->recovery_offset); | |
f466722c N |
1723 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
1724 | sb->feature_map |= | |
1725 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); | |
5fd6c1dc | 1726 | } |
2d78f8c4 N |
1727 | if (test_bit(Replacement, &rdev->flags)) |
1728 | sb->feature_map |= | |
1729 | cpu_to_le32(MD_FEATURE_REPLACEMENT); | |
5fd6c1dc | 1730 | |
f6705578 N |
1731 | if (mddev->reshape_position != MaxSector) { |
1732 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); | |
1733 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); | |
1734 | sb->new_layout = cpu_to_le32(mddev->new_layout); | |
1735 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); | |
1736 | sb->new_level = cpu_to_le32(mddev->new_level); | |
664e7c41 | 1737 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
2c810cdd N |
1738 | if (mddev->delta_disks == 0 && |
1739 | mddev->reshape_backwards) | |
1740 | sb->feature_map | |
1741 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); | |
c6563a8c N |
1742 | if (rdev->new_data_offset != rdev->data_offset) { |
1743 | sb->feature_map | |
1744 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); | |
1745 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset | |
1746 | - rdev->data_offset)); | |
1747 | } | |
f6705578 | 1748 | } |
a654b9d8 | 1749 | |
2699b672 N |
1750 | if (rdev->badblocks.count == 0) |
1751 | /* Nothing to do for bad blocks*/ ; | |
1752 | else if (sb->bblog_offset == 0) | |
1753 | /* Cannot record bad blocks on this device */ | |
1754 | md_error(mddev, rdev); | |
1755 | else { | |
1756 | struct badblocks *bb = &rdev->badblocks; | |
1757 | u64 *bbp = (u64 *)page_address(rdev->bb_page); | |
1758 | u64 *p = bb->page; | |
1759 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); | |
1760 | if (bb->changed) { | |
1761 | unsigned seq; | |
1762 | ||
1763 | retry: | |
1764 | seq = read_seqbegin(&bb->lock); | |
1765 | ||
1766 | memset(bbp, 0xff, PAGE_SIZE); | |
1767 | ||
1768 | for (i = 0 ; i < bb->count ; i++) { | |
35f9ac2d | 1769 | u64 internal_bb = p[i]; |
2699b672 N |
1770 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
1771 | | BB_LEN(internal_bb)); | |
35f9ac2d | 1772 | bbp[i] = cpu_to_le64(store_bb); |
2699b672 | 1773 | } |
d0962936 | 1774 | bb->changed = 0; |
2699b672 N |
1775 | if (read_seqretry(&bb->lock, seq)) |
1776 | goto retry; | |
1777 | ||
1778 | bb->sector = (rdev->sb_start + | |
1779 | (int)le32_to_cpu(sb->bblog_offset)); | |
1780 | bb->size = le16_to_cpu(sb->bblog_size); | |
2699b672 N |
1781 | } |
1782 | } | |
1783 | ||
1da177e4 | 1784 | max_dev = 0; |
dafb20fa | 1785 | rdev_for_each(rdev2, mddev) |
1da177e4 LT |
1786 | if (rdev2->desc_nr+1 > max_dev) |
1787 | max_dev = rdev2->desc_nr+1; | |
a778b73f | 1788 | |
70471daf N |
1789 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
1790 | int bmask; | |
a778b73f | 1791 | sb->max_dev = cpu_to_le32(max_dev); |
70471daf N |
1792 | rdev->sb_size = max_dev * 2 + 256; |
1793 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; | |
1794 | if (rdev->sb_size & bmask) | |
1795 | rdev->sb_size = (rdev->sb_size | bmask) + 1; | |
ddcf3522 N |
1796 | } else |
1797 | max_dev = le32_to_cpu(sb->max_dev); | |
1798 | ||
1da177e4 LT |
1799 | for (i=0; i<max_dev;i++) |
1800 | sb->dev_roles[i] = cpu_to_le16(0xfffe); | |
f72ffdd6 | 1801 | |
dafb20fa | 1802 | rdev_for_each(rdev2, mddev) { |
1da177e4 | 1803 | i = rdev2->desc_nr; |
b2d444d7 | 1804 | if (test_bit(Faulty, &rdev2->flags)) |
1da177e4 | 1805 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
b2d444d7 | 1806 | else if (test_bit(In_sync, &rdev2->flags)) |
1da177e4 | 1807 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
93be75ff | 1808 | else if (rdev2->raid_disk >= 0) |
5fd6c1dc | 1809 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
1da177e4 LT |
1810 | else |
1811 | sb->dev_roles[i] = cpu_to_le16(0xffff); | |
1812 | } | |
1813 | ||
1da177e4 LT |
1814 | sb->sb_csum = calc_sb_1_csum(sb); |
1815 | } | |
1816 | ||
0cd17fec | 1817 | static unsigned long long |
3cb03002 | 1818 | super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
0cd17fec CW |
1819 | { |
1820 | struct mdp_superblock_1 *sb; | |
15f4a5fd | 1821 | sector_t max_sectors; |
58c0fed4 | 1822 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
0cd17fec | 1823 | return 0; /* component must fit device */ |
c6563a8c N |
1824 | if (rdev->data_offset != rdev->new_data_offset) |
1825 | return 0; /* too confusing */ | |
0f420358 | 1826 | if (rdev->sb_start < rdev->data_offset) { |
0cd17fec | 1827 | /* minor versions 1 and 2; superblock before data */ |
77304d2a | 1828 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
15f4a5fd AN |
1829 | max_sectors -= rdev->data_offset; |
1830 | if (!num_sectors || num_sectors > max_sectors) | |
1831 | num_sectors = max_sectors; | |
c3d9714e | 1832 | } else if (rdev->mddev->bitmap_info.offset) { |
0cd17fec CW |
1833 | /* minor version 0 with bitmap we can't move */ |
1834 | return 0; | |
1835 | } else { | |
1836 | /* minor version 0; superblock after data */ | |
0f420358 | 1837 | sector_t sb_start; |
77304d2a | 1838 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
0f420358 | 1839 | sb_start &= ~(sector_t)(4*2 - 1); |
dd8ac336 | 1840 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
15f4a5fd AN |
1841 | if (!num_sectors || num_sectors > max_sectors) |
1842 | num_sectors = max_sectors; | |
0f420358 | 1843 | rdev->sb_start = sb_start; |
0cd17fec | 1844 | } |
65a06f06 | 1845 | sb = page_address(rdev->sb_page); |
15f4a5fd | 1846 | sb->data_size = cpu_to_le64(num_sectors); |
0f420358 | 1847 | sb->super_offset = rdev->sb_start; |
0cd17fec | 1848 | sb->sb_csum = calc_sb_1_csum(sb); |
0f420358 | 1849 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
0cd17fec CW |
1850 | rdev->sb_page); |
1851 | md_super_wait(rdev->mddev); | |
c26a44ed | 1852 | return num_sectors; |
c6563a8c N |
1853 | |
1854 | } | |
1855 | ||
1856 | static int | |
1857 | super_1_allow_new_offset(struct md_rdev *rdev, | |
1858 | unsigned long long new_offset) | |
1859 | { | |
1860 | /* All necessary checks on new >= old have been done */ | |
1861 | struct bitmap *bitmap; | |
1862 | if (new_offset >= rdev->data_offset) | |
1863 | return 1; | |
1864 | ||
1865 | /* with 1.0 metadata, there is no metadata to tread on | |
1866 | * so we can always move back */ | |
1867 | if (rdev->mddev->minor_version == 0) | |
1868 | return 1; | |
1869 | ||
1870 | /* otherwise we must be sure not to step on | |
1871 | * any metadata, so stay: | |
1872 | * 36K beyond start of superblock | |
1873 | * beyond end of badblocks | |
1874 | * beyond write-intent bitmap | |
1875 | */ | |
1876 | if (rdev->sb_start + (32+4)*2 > new_offset) | |
1877 | return 0; | |
1878 | bitmap = rdev->mddev->bitmap; | |
1879 | if (bitmap && !rdev->mddev->bitmap_info.file && | |
1880 | rdev->sb_start + rdev->mddev->bitmap_info.offset + | |
1ec885cd | 1881 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
c6563a8c N |
1882 | return 0; |
1883 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) | |
1884 | return 0; | |
1885 | ||
1886 | return 1; | |
0cd17fec | 1887 | } |
1da177e4 | 1888 | |
75c96f85 | 1889 | static struct super_type super_types[] = { |
1da177e4 LT |
1890 | [0] = { |
1891 | .name = "0.90.0", | |
1892 | .owner = THIS_MODULE, | |
0cd17fec CW |
1893 | .load_super = super_90_load, |
1894 | .validate_super = super_90_validate, | |
1895 | .sync_super = super_90_sync, | |
1896 | .rdev_size_change = super_90_rdev_size_change, | |
c6563a8c | 1897 | .allow_new_offset = super_90_allow_new_offset, |
1da177e4 LT |
1898 | }, |
1899 | [1] = { | |
1900 | .name = "md-1", | |
1901 | .owner = THIS_MODULE, | |
0cd17fec CW |
1902 | .load_super = super_1_load, |
1903 | .validate_super = super_1_validate, | |
1904 | .sync_super = super_1_sync, | |
1905 | .rdev_size_change = super_1_rdev_size_change, | |
c6563a8c | 1906 | .allow_new_offset = super_1_allow_new_offset, |
1da177e4 LT |
1907 | }, |
1908 | }; | |
1da177e4 | 1909 | |
fd01b88c | 1910 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
076f968b JB |
1911 | { |
1912 | if (mddev->sync_super) { | |
1913 | mddev->sync_super(mddev, rdev); | |
1914 | return; | |
1915 | } | |
1916 | ||
1917 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); | |
1918 | ||
1919 | super_types[mddev->major_version].sync_super(mddev, rdev); | |
1920 | } | |
1921 | ||
fd01b88c | 1922 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
1da177e4 | 1923 | { |
3cb03002 | 1924 | struct md_rdev *rdev, *rdev2; |
1da177e4 | 1925 | |
4b80991c N |
1926 | rcu_read_lock(); |
1927 | rdev_for_each_rcu(rdev, mddev1) | |
1928 | rdev_for_each_rcu(rdev2, mddev2) | |
7dd5e7c3 | 1929 | if (rdev->bdev->bd_contains == |
4b80991c N |
1930 | rdev2->bdev->bd_contains) { |
1931 | rcu_read_unlock(); | |
7dd5e7c3 | 1932 | return 1; |
4b80991c N |
1933 | } |
1934 | rcu_read_unlock(); | |
1da177e4 LT |
1935 | return 0; |
1936 | } | |
1937 | ||
1938 | static LIST_HEAD(pending_raid_disks); | |
1939 | ||
ac5e7113 AN |
1940 | /* |
1941 | * Try to register data integrity profile for an mddev | |
1942 | * | |
1943 | * This is called when an array is started and after a disk has been kicked | |
1944 | * from the array. It only succeeds if all working and active component devices | |
1945 | * are integrity capable with matching profiles. | |
1946 | */ | |
fd01b88c | 1947 | int md_integrity_register(struct mddev *mddev) |
ac5e7113 | 1948 | { |
3cb03002 | 1949 | struct md_rdev *rdev, *reference = NULL; |
ac5e7113 AN |
1950 | |
1951 | if (list_empty(&mddev->disks)) | |
1952 | return 0; /* nothing to do */ | |
629acb6a JB |
1953 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
1954 | return 0; /* shouldn't register, or already is */ | |
dafb20fa | 1955 | rdev_for_each(rdev, mddev) { |
ac5e7113 AN |
1956 | /* skip spares and non-functional disks */ |
1957 | if (test_bit(Faulty, &rdev->flags)) | |
1958 | continue; | |
1959 | if (rdev->raid_disk < 0) | |
1960 | continue; | |
ac5e7113 AN |
1961 | if (!reference) { |
1962 | /* Use the first rdev as the reference */ | |
1963 | reference = rdev; | |
1964 | continue; | |
1965 | } | |
1966 | /* does this rdev's profile match the reference profile? */ | |
1967 | if (blk_integrity_compare(reference->bdev->bd_disk, | |
1968 | rdev->bdev->bd_disk) < 0) | |
1969 | return -EINVAL; | |
1970 | } | |
89078d57 MP |
1971 | if (!reference || !bdev_get_integrity(reference->bdev)) |
1972 | return 0; | |
ac5e7113 AN |
1973 | /* |
1974 | * All component devices are integrity capable and have matching | |
1975 | * profiles, register the common profile for the md device. | |
1976 | */ | |
1977 | if (blk_integrity_register(mddev->gendisk, | |
1978 | bdev_get_integrity(reference->bdev)) != 0) { | |
1979 | printk(KERN_ERR "md: failed to register integrity for %s\n", | |
1980 | mdname(mddev)); | |
1981 | return -EINVAL; | |
1982 | } | |
a91a2785 MP |
1983 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
1984 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { | |
1985 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", | |
1986 | mdname(mddev)); | |
1987 | return -EINVAL; | |
1988 | } | |
ac5e7113 AN |
1989 | return 0; |
1990 | } | |
1991 | EXPORT_SYMBOL(md_integrity_register); | |
1992 | ||
1993 | /* Disable data integrity if non-capable/non-matching disk is being added */ | |
fd01b88c | 1994 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
3f9d99c1 | 1995 | { |
2863b9eb JB |
1996 | struct blk_integrity *bi_rdev; |
1997 | struct blk_integrity *bi_mddev; | |
1998 | ||
1999 | if (!mddev->gendisk) | |
2000 | return; | |
2001 | ||
2002 | bi_rdev = bdev_get_integrity(rdev->bdev); | |
2003 | bi_mddev = blk_get_integrity(mddev->gendisk); | |
3f9d99c1 | 2004 | |
ac5e7113 | 2005 | if (!bi_mddev) /* nothing to do */ |
3f9d99c1 | 2006 | return; |
ac5e7113 | 2007 | if (rdev->raid_disk < 0) /* skip spares */ |
3f9d99c1 | 2008 | return; |
ac5e7113 AN |
2009 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
2010 | rdev->bdev->bd_disk) >= 0) | |
2011 | return; | |
2012 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); | |
2013 | blk_integrity_unregister(mddev->gendisk); | |
3f9d99c1 | 2014 | } |
ac5e7113 | 2015 | EXPORT_SYMBOL(md_integrity_add_rdev); |
3f9d99c1 | 2016 | |
f72ffdd6 | 2017 | static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev) |
1da177e4 | 2018 | { |
7dd5e7c3 | 2019 | char b[BDEVNAME_SIZE]; |
f637b9f9 | 2020 | struct kobject *ko; |
1edf80d3 | 2021 | char *s; |
5e55e2f5 | 2022 | int err; |
1da177e4 | 2023 | |
11e2ede0 DW |
2024 | /* prevent duplicates */ |
2025 | if (find_rdev(mddev, rdev->bdev->bd_dev)) | |
2026 | return -EEXIST; | |
2027 | ||
dd8ac336 AN |
2028 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
2029 | if (rdev->sectors && (mddev->dev_sectors == 0 || | |
2030 | rdev->sectors < mddev->dev_sectors)) { | |
a778b73f N |
2031 | if (mddev->pers) { |
2032 | /* Cannot change size, so fail | |
2033 | * If mddev->level <= 0, then we don't care | |
2034 | * about aligning sizes (e.g. linear) | |
2035 | */ | |
2036 | if (mddev->level > 0) | |
2037 | return -ENOSPC; | |
2038 | } else | |
dd8ac336 | 2039 | mddev->dev_sectors = rdev->sectors; |
2bf071bf | 2040 | } |
1da177e4 LT |
2041 | |
2042 | /* Verify rdev->desc_nr is unique. | |
2043 | * If it is -1, assign a free number, else | |
2044 | * check number is not in use | |
2045 | */ | |
4878e9eb | 2046 | rcu_read_lock(); |
1da177e4 LT |
2047 | if (rdev->desc_nr < 0) { |
2048 | int choice = 0; | |
4878e9eb N |
2049 | if (mddev->pers) |
2050 | choice = mddev->raid_disks; | |
2051 | while (find_rdev_nr_rcu(mddev, choice)) | |
1da177e4 LT |
2052 | choice++; |
2053 | rdev->desc_nr = choice; | |
2054 | } else { | |
4878e9eb N |
2055 | if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
2056 | rcu_read_unlock(); | |
1da177e4 | 2057 | return -EBUSY; |
4878e9eb | 2058 | } |
1da177e4 | 2059 | } |
4878e9eb | 2060 | rcu_read_unlock(); |
de01dfad N |
2061 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
2062 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", | |
2063 | mdname(mddev), mddev->max_disks); | |
2064 | return -EBUSY; | |
2065 | } | |
19133a42 | 2066 | bdevname(rdev->bdev,b); |
649316b2 | 2067 | while ( (s=strchr(b, '/')) != NULL) |
1edf80d3 | 2068 | *s = '!'; |
649316b2 | 2069 | |
1da177e4 | 2070 | rdev->mddev = mddev; |
19133a42 | 2071 | printk(KERN_INFO "md: bind<%s>\n", b); |
86e6ffdd | 2072 | |
b2d6db58 | 2073 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
5e55e2f5 | 2074 | goto fail; |
86e6ffdd | 2075 | |
0762b8bd | 2076 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
00bcb4ac N |
2077 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
2078 | /* failure here is OK */; | |
2079 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); | |
3c0ee63a | 2080 | |
4b80991c | 2081 | list_add_rcu(&rdev->same_set, &mddev->disks); |
e09b457b | 2082 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
4044ba58 N |
2083 | |
2084 | /* May as well allow recovery to be retried once */ | |
5389042f | 2085 | mddev->recovery_disabled++; |
3f9d99c1 | 2086 | |
1da177e4 | 2087 | return 0; |
5e55e2f5 N |
2088 | |
2089 | fail: | |
2090 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", | |
2091 | b, mdname(mddev)); | |
2092 | return err; | |
1da177e4 LT |
2093 | } |
2094 | ||
177a99b2 | 2095 | static void md_delayed_delete(struct work_struct *ws) |
5792a285 | 2096 | { |
3cb03002 | 2097 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
5792a285 | 2098 | kobject_del(&rdev->kobj); |
177a99b2 | 2099 | kobject_put(&rdev->kobj); |
5792a285 N |
2100 | } |
2101 | ||
f72ffdd6 | 2102 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
1da177e4 LT |
2103 | { |
2104 | char b[BDEVNAME_SIZE]; | |
403df478 | 2105 | |
49731baa | 2106 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
4b80991c | 2107 | list_del_rcu(&rdev->same_set); |
1da177e4 LT |
2108 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
2109 | rdev->mddev = NULL; | |
86e6ffdd | 2110 | sysfs_remove_link(&rdev->kobj, "block"); |
3c0ee63a N |
2111 | sysfs_put(rdev->sysfs_state); |
2112 | rdev->sysfs_state = NULL; | |
2230dfe4 | 2113 | rdev->badblocks.count = 0; |
5792a285 | 2114 | /* We need to delay this, otherwise we can deadlock when |
4b80991c N |
2115 | * writing to 'remove' to "dev/state". We also need |
2116 | * to delay it due to rcu usage. | |
5792a285 | 2117 | */ |
4b80991c | 2118 | synchronize_rcu(); |
177a99b2 N |
2119 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
2120 | kobject_get(&rdev->kobj); | |
e804ac78 | 2121 | queue_work(md_misc_wq, &rdev->del_work); |
1da177e4 LT |
2122 | } |
2123 | ||
2124 | /* | |
2125 | * prevent the device from being mounted, repartitioned or | |
2126 | * otherwise reused by a RAID array (or any other kernel | |
2127 | * subsystem), by bd_claiming the device. | |
2128 | */ | |
3cb03002 | 2129 | static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared) |
1da177e4 LT |
2130 | { |
2131 | int err = 0; | |
2132 | struct block_device *bdev; | |
2133 | char b[BDEVNAME_SIZE]; | |
2134 | ||
d4d77629 | 2135 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
3cb03002 | 2136 | shared ? (struct md_rdev *)lock_rdev : rdev); |
1da177e4 LT |
2137 | if (IS_ERR(bdev)) { |
2138 | printk(KERN_ERR "md: could not open %s.\n", | |
2139 | __bdevname(dev, b)); | |
2140 | return PTR_ERR(bdev); | |
2141 | } | |
1da177e4 LT |
2142 | rdev->bdev = bdev; |
2143 | return err; | |
2144 | } | |
2145 | ||
3cb03002 | 2146 | static void unlock_rdev(struct md_rdev *rdev) |
1da177e4 LT |
2147 | { |
2148 | struct block_device *bdev = rdev->bdev; | |
2149 | rdev->bdev = NULL; | |
e525fd89 | 2150 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
1da177e4 LT |
2151 | } |
2152 | ||
2153 | void md_autodetect_dev(dev_t dev); | |
2154 | ||
f72ffdd6 | 2155 | static void export_rdev(struct md_rdev *rdev) |
1da177e4 LT |
2156 | { |
2157 | char b[BDEVNAME_SIZE]; | |
403df478 | 2158 | |
1da177e4 LT |
2159 | printk(KERN_INFO "md: export_rdev(%s)\n", |
2160 | bdevname(rdev->bdev,b)); | |
545c8795 | 2161 | md_rdev_clear(rdev); |
1da177e4 | 2162 | #ifndef MODULE |
d0fae18f N |
2163 | if (test_bit(AutoDetected, &rdev->flags)) |
2164 | md_autodetect_dev(rdev->bdev->bd_dev); | |
1da177e4 LT |
2165 | #endif |
2166 | unlock_rdev(rdev); | |
86e6ffdd | 2167 | kobject_put(&rdev->kobj); |
1da177e4 LT |
2168 | } |
2169 | ||
f72ffdd6 | 2170 | static void kick_rdev_from_array(struct md_rdev *rdev) |
1da177e4 LT |
2171 | { |
2172 | unbind_rdev_from_array(rdev); | |
2173 | export_rdev(rdev); | |
2174 | } | |
2175 | ||
fd01b88c | 2176 | static void export_array(struct mddev *mddev) |
1da177e4 | 2177 | { |
0638bb0e | 2178 | struct md_rdev *rdev; |
1da177e4 | 2179 | |
0638bb0e N |
2180 | while (!list_empty(&mddev->disks)) { |
2181 | rdev = list_first_entry(&mddev->disks, struct md_rdev, | |
2182 | same_set); | |
1da177e4 LT |
2183 | kick_rdev_from_array(rdev); |
2184 | } | |
1da177e4 LT |
2185 | mddev->raid_disks = 0; |
2186 | mddev->major_version = 0; | |
2187 | } | |
2188 | ||
f72ffdd6 | 2189 | static void sync_sbs(struct mddev *mddev, int nospares) |
1da177e4 | 2190 | { |
42543769 N |
2191 | /* Update each superblock (in-memory image), but |
2192 | * if we are allowed to, skip spares which already | |
2193 | * have the right event counter, or have one earlier | |
2194 | * (which would mean they aren't being marked as dirty | |
2195 | * with the rest of the array) | |
2196 | */ | |
3cb03002 | 2197 | struct md_rdev *rdev; |
dafb20fa | 2198 | rdev_for_each(rdev, mddev) { |
42543769 N |
2199 | if (rdev->sb_events == mddev->events || |
2200 | (nospares && | |
2201 | rdev->raid_disk < 0 && | |
42543769 N |
2202 | rdev->sb_events+1 == mddev->events)) { |
2203 | /* Don't update this superblock */ | |
2204 | rdev->sb_loaded = 2; | |
2205 | } else { | |
076f968b | 2206 | sync_super(mddev, rdev); |
42543769 N |
2207 | rdev->sb_loaded = 1; |
2208 | } | |
1da177e4 LT |
2209 | } |
2210 | } | |
2211 | ||
f72ffdd6 | 2212 | static void md_update_sb(struct mddev *mddev, int force_change) |
1da177e4 | 2213 | { |
3cb03002 | 2214 | struct md_rdev *rdev; |
06d91a5f | 2215 | int sync_req; |
42543769 | 2216 | int nospares = 0; |
2699b672 | 2217 | int any_badblocks_changed = 0; |
1da177e4 | 2218 | |
d87f064f N |
2219 | if (mddev->ro) { |
2220 | if (force_change) | |
2221 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
2222 | return; | |
2223 | } | |
1da177e4 | 2224 | repeat: |
3a3a5ddb | 2225 | /* First make sure individual recovery_offsets are correct */ |
dafb20fa | 2226 | rdev_for_each(rdev, mddev) { |
3a3a5ddb N |
2227 | if (rdev->raid_disk >= 0 && |
2228 | mddev->delta_disks >= 0 && | |
2229 | !test_bit(In_sync, &rdev->flags) && | |
2230 | mddev->curr_resync_completed > rdev->recovery_offset) | |
2231 | rdev->recovery_offset = mddev->curr_resync_completed; | |
2232 | ||
f72ffdd6 | 2233 | } |
bd52b746 | 2234 | if (!mddev->persistent) { |
070dc6dd | 2235 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
3a3a5ddb | 2236 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
de393cde | 2237 | if (!mddev->external) { |
d97a41dc | 2238 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
dafb20fa | 2239 | rdev_for_each(rdev, mddev) { |
de393cde | 2240 | if (rdev->badblocks.changed) { |
d0962936 | 2241 | rdev->badblocks.changed = 0; |
de393cde N |
2242 | md_ack_all_badblocks(&rdev->badblocks); |
2243 | md_error(mddev, rdev); | |
2244 | } | |
2245 | clear_bit(Blocked, &rdev->flags); | |
2246 | clear_bit(BlockedBadBlocks, &rdev->flags); | |
2247 | wake_up(&rdev->blocked_wait); | |
2248 | } | |
2249 | } | |
3a3a5ddb N |
2250 | wake_up(&mddev->sb_wait); |
2251 | return; | |
2252 | } | |
2253 | ||
85572d7c | 2254 | spin_lock(&mddev->lock); |
84692195 | 2255 | |
3a3a5ddb N |
2256 | mddev->utime = get_seconds(); |
2257 | ||
850b2b42 N |
2258 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
2259 | force_change = 1; | |
2260 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) | |
2261 | /* just a clean<-> dirty transition, possibly leave spares alone, | |
2262 | * though if events isn't the right even/odd, we will have to do | |
2263 | * spares after all | |
2264 | */ | |
2265 | nospares = 1; | |
2266 | if (force_change) | |
2267 | nospares = 0; | |
2268 | if (mddev->degraded) | |
84692195 N |
2269 | /* If the array is degraded, then skipping spares is both |
2270 | * dangerous and fairly pointless. | |
2271 | * Dangerous because a device that was removed from the array | |
2272 | * might have a event_count that still looks up-to-date, | |
2273 | * so it can be re-added without a resync. | |
2274 | * Pointless because if there are any spares to skip, | |
2275 | * then a recovery will happen and soon that array won't | |
2276 | * be degraded any more and the spare can go back to sleep then. | |
2277 | */ | |
850b2b42 | 2278 | nospares = 0; |
84692195 | 2279 | |
06d91a5f | 2280 | sync_req = mddev->in_sync; |
42543769 N |
2281 | |
2282 | /* If this is just a dirty<->clean transition, and the array is clean | |
2283 | * and 'events' is odd, we can roll back to the previous clean state */ | |
850b2b42 | 2284 | if (nospares |
42543769 | 2285 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
a8707c08 N |
2286 | && mddev->can_decrease_events |
2287 | && mddev->events != 1) { | |
42543769 | 2288 | mddev->events--; |
a8707c08 N |
2289 | mddev->can_decrease_events = 0; |
2290 | } else { | |
42543769 N |
2291 | /* otherwise we have to go forward and ... */ |
2292 | mddev->events ++; | |
a8707c08 | 2293 | mddev->can_decrease_events = nospares; |
42543769 | 2294 | } |
1da177e4 | 2295 | |
403df478 N |
2296 | /* |
2297 | * This 64-bit counter should never wrap. | |
2298 | * Either we are in around ~1 trillion A.C., assuming | |
2299 | * 1 reboot per second, or we have a bug... | |
2300 | */ | |
2301 | WARN_ON(mddev->events == 0); | |
2699b672 | 2302 | |
dafb20fa | 2303 | rdev_for_each(rdev, mddev) { |
2699b672 N |
2304 | if (rdev->badblocks.changed) |
2305 | any_badblocks_changed++; | |
de393cde N |
2306 | if (test_bit(Faulty, &rdev->flags)) |
2307 | set_bit(FaultRecorded, &rdev->flags); | |
2308 | } | |
2699b672 | 2309 | |
e691063a | 2310 | sync_sbs(mddev, nospares); |
85572d7c | 2311 | spin_unlock(&mddev->lock); |
1da177e4 | 2312 | |
36a4e1fe N |
2313 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
2314 | mdname(mddev), mddev->in_sync); | |
1da177e4 | 2315 | |
4ad13663 | 2316 | bitmap_update_sb(mddev->bitmap); |
dafb20fa | 2317 | rdev_for_each(rdev, mddev) { |
1da177e4 | 2318 | char b[BDEVNAME_SIZE]; |
36a4e1fe | 2319 | |
42543769 N |
2320 | if (rdev->sb_loaded != 1) |
2321 | continue; /* no noise on spare devices */ | |
1da177e4 | 2322 | |
f466722c | 2323 | if (!test_bit(Faulty, &rdev->flags)) { |
7bfa19f2 | 2324 | md_super_write(mddev,rdev, |
0f420358 | 2325 | rdev->sb_start, rdev->sb_size, |
7bfa19f2 | 2326 | rdev->sb_page); |
36a4e1fe N |
2327 | pr_debug("md: (write) %s's sb offset: %llu\n", |
2328 | bdevname(rdev->bdev, b), | |
2329 | (unsigned long long)rdev->sb_start); | |
42543769 | 2330 | rdev->sb_events = mddev->events; |
2699b672 N |
2331 | if (rdev->badblocks.size) { |
2332 | md_super_write(mddev, rdev, | |
2333 | rdev->badblocks.sector, | |
2334 | rdev->badblocks.size << 9, | |
2335 | rdev->bb_page); | |
2336 | rdev->badblocks.size = 0; | |
2337 | } | |
7bfa19f2 | 2338 | |
f466722c | 2339 | } else |
36a4e1fe N |
2340 | pr_debug("md: %s (skipping faulty)\n", |
2341 | bdevname(rdev->bdev, b)); | |
d70ed2e4 | 2342 | |
7bfa19f2 | 2343 | if (mddev->level == LEVEL_MULTIPATH) |
1da177e4 LT |
2344 | /* only need to write one superblock... */ |
2345 | break; | |
2346 | } | |
a9701a30 | 2347 | md_super_wait(mddev); |
850b2b42 | 2348 | /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */ |
7bfa19f2 | 2349 | |
85572d7c | 2350 | spin_lock(&mddev->lock); |
850b2b42 N |
2351 | if (mddev->in_sync != sync_req || |
2352 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { | |
06d91a5f | 2353 | /* have to write it out again */ |
85572d7c | 2354 | spin_unlock(&mddev->lock); |
06d91a5f N |
2355 | goto repeat; |
2356 | } | |
850b2b42 | 2357 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
85572d7c | 2358 | spin_unlock(&mddev->lock); |
3d310eb7 | 2359 | wake_up(&mddev->sb_wait); |
acb180b0 N |
2360 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
2361 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); | |
06d91a5f | 2362 | |
dafb20fa | 2363 | rdev_for_each(rdev, mddev) { |
de393cde N |
2364 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
2365 | clear_bit(Blocked, &rdev->flags); | |
2366 | ||
2367 | if (any_badblocks_changed) | |
2699b672 | 2368 | md_ack_all_badblocks(&rdev->badblocks); |
de393cde N |
2369 | clear_bit(BlockedBadBlocks, &rdev->flags); |
2370 | wake_up(&rdev->blocked_wait); | |
2371 | } | |
1da177e4 LT |
2372 | } |
2373 | ||
7f6ce769 | 2374 | /* words written to sysfs files may, or may not, be \n terminated. |
bce74dac N |
2375 | * We want to accept with case. For this we use cmd_match. |
2376 | */ | |
2377 | static int cmd_match(const char *cmd, const char *str) | |
2378 | { | |
2379 | /* See if cmd, written into a sysfs file, matches | |
2380 | * str. They must either be the same, or cmd can | |
2381 | * have a trailing newline | |
2382 | */ | |
2383 | while (*cmd && *str && *cmd == *str) { | |
2384 | cmd++; | |
2385 | str++; | |
2386 | } | |
2387 | if (*cmd == '\n') | |
2388 | cmd++; | |
2389 | if (*str || *cmd) | |
2390 | return 0; | |
2391 | return 1; | |
2392 | } | |
2393 | ||
86e6ffdd N |
2394 | struct rdev_sysfs_entry { |
2395 | struct attribute attr; | |
3cb03002 N |
2396 | ssize_t (*show)(struct md_rdev *, char *); |
2397 | ssize_t (*store)(struct md_rdev *, const char *, size_t); | |
86e6ffdd N |
2398 | }; |
2399 | ||
2400 | static ssize_t | |
3cb03002 | 2401 | state_show(struct md_rdev *rdev, char *page) |
86e6ffdd N |
2402 | { |
2403 | char *sep = ""; | |
20a49ff6 | 2404 | size_t len = 0; |
758bfc8a | 2405 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
86e6ffdd | 2406 | |
758bfc8a | 2407 | if (test_bit(Faulty, &flags) || |
de393cde | 2408 | rdev->badblocks.unacked_exist) { |
86e6ffdd N |
2409 | len+= sprintf(page+len, "%sfaulty",sep); |
2410 | sep = ","; | |
2411 | } | |
758bfc8a | 2412 | if (test_bit(In_sync, &flags)) { |
86e6ffdd N |
2413 | len += sprintf(page+len, "%sin_sync",sep); |
2414 | sep = ","; | |
2415 | } | |
758bfc8a | 2416 | if (test_bit(WriteMostly, &flags)) { |
f655675b N |
2417 | len += sprintf(page+len, "%swrite_mostly",sep); |
2418 | sep = ","; | |
2419 | } | |
758bfc8a | 2420 | if (test_bit(Blocked, &flags) || |
52c64152 | 2421 | (rdev->badblocks.unacked_exist |
758bfc8a | 2422 | && !test_bit(Faulty, &flags))) { |
6bfe0b49 DW |
2423 | len += sprintf(page+len, "%sblocked", sep); |
2424 | sep = ","; | |
2425 | } | |
758bfc8a N |
2426 | if (!test_bit(Faulty, &flags) && |
2427 | !test_bit(In_sync, &flags)) { | |
86e6ffdd N |
2428 | len += sprintf(page+len, "%sspare", sep); |
2429 | sep = ","; | |
2430 | } | |
758bfc8a | 2431 | if (test_bit(WriteErrorSeen, &flags)) { |
d7a9d443 N |
2432 | len += sprintf(page+len, "%swrite_error", sep); |
2433 | sep = ","; | |
2434 | } | |
758bfc8a | 2435 | if (test_bit(WantReplacement, &flags)) { |
2d78f8c4 N |
2436 | len += sprintf(page+len, "%swant_replacement", sep); |
2437 | sep = ","; | |
2438 | } | |
758bfc8a | 2439 | if (test_bit(Replacement, &flags)) { |
2d78f8c4 N |
2440 | len += sprintf(page+len, "%sreplacement", sep); |
2441 | sep = ","; | |
2442 | } | |
2443 | ||
86e6ffdd N |
2444 | return len+sprintf(page+len, "\n"); |
2445 | } | |
2446 | ||
45dc2de1 | 2447 | static ssize_t |
3cb03002 | 2448 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
45dc2de1 N |
2449 | { |
2450 | /* can write | |
de393cde | 2451 | * faulty - simulates an error |
45dc2de1 | 2452 | * remove - disconnects the device |
f655675b N |
2453 | * writemostly - sets write_mostly |
2454 | * -writemostly - clears write_mostly | |
de393cde N |
2455 | * blocked - sets the Blocked flags |
2456 | * -blocked - clears the Blocked and possibly simulates an error | |
6d56e278 | 2457 | * insync - sets Insync providing device isn't active |
f466722c N |
2458 | * -insync - clear Insync for a device with a slot assigned, |
2459 | * so that it gets rebuilt based on bitmap | |
d7a9d443 N |
2460 | * write_error - sets WriteErrorSeen |
2461 | * -write_error - clears WriteErrorSeen | |
45dc2de1 N |
2462 | */ |
2463 | int err = -EINVAL; | |
2464 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { | |
2465 | md_error(rdev->mddev, rdev); | |
5ef56c8f N |
2466 | if (test_bit(Faulty, &rdev->flags)) |
2467 | err = 0; | |
2468 | else | |
2469 | err = -EBUSY; | |
45dc2de1 N |
2470 | } else if (cmd_match(buf, "remove")) { |
2471 | if (rdev->raid_disk >= 0) | |
2472 | err = -EBUSY; | |
2473 | else { | |
fd01b88c | 2474 | struct mddev *mddev = rdev->mddev; |
293467aa GR |
2475 | if (mddev_is_clustered(mddev)) |
2476 | md_cluster_ops->metadata_update_start(mddev); | |
45dc2de1 | 2477 | kick_rdev_from_array(rdev); |
3f9d7b0d N |
2478 | if (mddev->pers) |
2479 | md_update_sb(mddev, 1); | |
45dc2de1 | 2480 | md_new_event(mddev); |
293467aa GR |
2481 | if (mddev_is_clustered(mddev)) |
2482 | md_cluster_ops->metadata_update_finish(mddev); | |
45dc2de1 N |
2483 | err = 0; |
2484 | } | |
f655675b N |
2485 | } else if (cmd_match(buf, "writemostly")) { |
2486 | set_bit(WriteMostly, &rdev->flags); | |
2487 | err = 0; | |
2488 | } else if (cmd_match(buf, "-writemostly")) { | |
2489 | clear_bit(WriteMostly, &rdev->flags); | |
6bfe0b49 DW |
2490 | err = 0; |
2491 | } else if (cmd_match(buf, "blocked")) { | |
2492 | set_bit(Blocked, &rdev->flags); | |
2493 | err = 0; | |
2494 | } else if (cmd_match(buf, "-blocked")) { | |
de393cde | 2495 | if (!test_bit(Faulty, &rdev->flags) && |
7da64a0a | 2496 | rdev->badblocks.unacked_exist) { |
de393cde N |
2497 | /* metadata handler doesn't understand badblocks, |
2498 | * so we need to fail the device | |
2499 | */ | |
2500 | md_error(rdev->mddev, rdev); | |
2501 | } | |
6bfe0b49 | 2502 | clear_bit(Blocked, &rdev->flags); |
de393cde | 2503 | clear_bit(BlockedBadBlocks, &rdev->flags); |
6bfe0b49 DW |
2504 | wake_up(&rdev->blocked_wait); |
2505 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); | |
2506 | md_wakeup_thread(rdev->mddev->thread); | |
2507 | ||
6d56e278 N |
2508 | err = 0; |
2509 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { | |
2510 | set_bit(In_sync, &rdev->flags); | |
f655675b | 2511 | err = 0; |
f466722c | 2512 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) { |
e1960f8c N |
2513 | if (rdev->mddev->pers == NULL) { |
2514 | clear_bit(In_sync, &rdev->flags); | |
2515 | rdev->saved_raid_disk = rdev->raid_disk; | |
2516 | rdev->raid_disk = -1; | |
2517 | err = 0; | |
2518 | } | |
d7a9d443 N |
2519 | } else if (cmd_match(buf, "write_error")) { |
2520 | set_bit(WriteErrorSeen, &rdev->flags); | |
2521 | err = 0; | |
2522 | } else if (cmd_match(buf, "-write_error")) { | |
2523 | clear_bit(WriteErrorSeen, &rdev->flags); | |
2524 | err = 0; | |
2d78f8c4 N |
2525 | } else if (cmd_match(buf, "want_replacement")) { |
2526 | /* Any non-spare device that is not a replacement can | |
2527 | * become want_replacement at any time, but we then need to | |
2528 | * check if recovery is needed. | |
2529 | */ | |
2530 | if (rdev->raid_disk >= 0 && | |
2531 | !test_bit(Replacement, &rdev->flags)) | |
2532 | set_bit(WantReplacement, &rdev->flags); | |
2533 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); | |
2534 | md_wakeup_thread(rdev->mddev->thread); | |
2535 | err = 0; | |
2536 | } else if (cmd_match(buf, "-want_replacement")) { | |
2537 | /* Clearing 'want_replacement' is always allowed. | |
2538 | * Once replacements starts it is too late though. | |
2539 | */ | |
2540 | err = 0; | |
2541 | clear_bit(WantReplacement, &rdev->flags); | |
2542 | } else if (cmd_match(buf, "replacement")) { | |
2543 | /* Can only set a device as a replacement when array has not | |
2544 | * yet been started. Once running, replacement is automatic | |
2545 | * from spares, or by assigning 'slot'. | |
2546 | */ | |
2547 | if (rdev->mddev->pers) | |
2548 | err = -EBUSY; | |
2549 | else { | |
2550 | set_bit(Replacement, &rdev->flags); | |
2551 | err = 0; | |
2552 | } | |
2553 | } else if (cmd_match(buf, "-replacement")) { | |
2554 | /* Similarly, can only clear Replacement before start */ | |
2555 | if (rdev->mddev->pers) | |
2556 | err = -EBUSY; | |
2557 | else { | |
2558 | clear_bit(Replacement, &rdev->flags); | |
2559 | err = 0; | |
2560 | } | |
45dc2de1 | 2561 | } |
00bcb4ac N |
2562 | if (!err) |
2563 | sysfs_notify_dirent_safe(rdev->sysfs_state); | |
45dc2de1 N |
2564 | return err ? err : len; |
2565 | } | |
80ca3a44 N |
2566 | static struct rdev_sysfs_entry rdev_state = |
2567 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); | |
86e6ffdd | 2568 | |
4dbcdc75 | 2569 | static ssize_t |
3cb03002 | 2570 | errors_show(struct md_rdev *rdev, char *page) |
4dbcdc75 N |
2571 | { |
2572 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); | |
2573 | } | |
2574 | ||
2575 | static ssize_t | |
3cb03002 | 2576 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
4dbcdc75 N |
2577 | { |
2578 | char *e; | |
2579 | unsigned long n = simple_strtoul(buf, &e, 10); | |
2580 | if (*buf && (*e == 0 || *e == '\n')) { | |
2581 | atomic_set(&rdev->corrected_errors, n); | |
2582 | return len; | |
2583 | } | |
2584 | return -EINVAL; | |
2585 | } | |
2586 | static struct rdev_sysfs_entry rdev_errors = | |
80ca3a44 | 2587 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
4dbcdc75 | 2588 | |
014236d2 | 2589 | static ssize_t |
3cb03002 | 2590 | slot_show(struct md_rdev *rdev, char *page) |
014236d2 N |
2591 | { |
2592 | if (rdev->raid_disk < 0) | |
2593 | return sprintf(page, "none\n"); | |
2594 | else | |
2595 | return sprintf(page, "%d\n", rdev->raid_disk); | |
2596 | } | |
2597 | ||
2598 | static ssize_t | |
3cb03002 | 2599 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
014236d2 N |
2600 | { |
2601 | char *e; | |
c303da6d | 2602 | int err; |
014236d2 N |
2603 | int slot = simple_strtoul(buf, &e, 10); |
2604 | if (strncmp(buf, "none", 4)==0) | |
2605 | slot = -1; | |
2606 | else if (e==buf || (*e && *e!= '\n')) | |
2607 | return -EINVAL; | |
6c2fce2e | 2608 | if (rdev->mddev->pers && slot == -1) { |
c303da6d N |
2609 | /* Setting 'slot' on an active array requires also |
2610 | * updating the 'rd%d' link, and communicating | |
2611 | * with the personality with ->hot_*_disk. | |
2612 | * For now we only support removing | |
2613 | * failed/spare devices. This normally happens automatically, | |
2614 | * but not when the metadata is externally managed. | |
2615 | */ | |
c303da6d N |
2616 | if (rdev->raid_disk == -1) |
2617 | return -EEXIST; | |
2618 | /* personality does all needed checks */ | |
01393f3d | 2619 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
c303da6d | 2620 | return -EINVAL; |
746d3207 N |
2621 | clear_bit(Blocked, &rdev->flags); |
2622 | remove_and_add_spares(rdev->mddev, rdev); | |
2623 | if (rdev->raid_disk >= 0) | |
2624 | return -EBUSY; | |
c303da6d N |
2625 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
2626 | md_wakeup_thread(rdev->mddev->thread); | |
6c2fce2e | 2627 | } else if (rdev->mddev->pers) { |
6c2fce2e | 2628 | /* Activating a spare .. or possibly reactivating |
6d56e278 | 2629 | * if we ever get bitmaps working here. |
6c2fce2e NB |
2630 | */ |
2631 | ||
2632 | if (rdev->raid_disk != -1) | |
2633 | return -EBUSY; | |
2634 | ||
c6751b2b N |
2635 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
2636 | return -EBUSY; | |
2637 | ||
6c2fce2e NB |
2638 | if (rdev->mddev->pers->hot_add_disk == NULL) |
2639 | return -EINVAL; | |
2640 | ||
ba1b41b6 N |
2641 | if (slot >= rdev->mddev->raid_disks && |
2642 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) | |
2643 | return -ENOSPC; | |
2644 | ||
6c2fce2e NB |
2645 | rdev->raid_disk = slot; |
2646 | if (test_bit(In_sync, &rdev->flags)) | |
2647 | rdev->saved_raid_disk = slot; | |
2648 | else | |
2649 | rdev->saved_raid_disk = -1; | |
d30519fc | 2650 | clear_bit(In_sync, &rdev->flags); |
8313b8e5 | 2651 | clear_bit(Bitmap_sync, &rdev->flags); |
6c2fce2e NB |
2652 | err = rdev->mddev->pers-> |
2653 | hot_add_disk(rdev->mddev, rdev); | |
199050ea | 2654 | if (err) { |
6c2fce2e | 2655 | rdev->raid_disk = -1; |
6c2fce2e | 2656 | return err; |
52664732 | 2657 | } else |
00bcb4ac | 2658 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
36fad858 | 2659 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
00bcb4ac | 2660 | /* failure here is OK */; |
6c2fce2e | 2661 | /* don't wakeup anyone, leave that to userspace. */ |
c303da6d | 2662 | } else { |
ba1b41b6 N |
2663 | if (slot >= rdev->mddev->raid_disks && |
2664 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) | |
c303da6d N |
2665 | return -ENOSPC; |
2666 | rdev->raid_disk = slot; | |
2667 | /* assume it is working */ | |
c5d79adb N |
2668 | clear_bit(Faulty, &rdev->flags); |
2669 | clear_bit(WriteMostly, &rdev->flags); | |
c303da6d | 2670 | set_bit(In_sync, &rdev->flags); |
00bcb4ac | 2671 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
c303da6d | 2672 | } |
014236d2 N |
2673 | return len; |
2674 | } | |
2675 | ||
014236d2 | 2676 | static struct rdev_sysfs_entry rdev_slot = |
80ca3a44 | 2677 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
014236d2 | 2678 | |
93c8cad0 | 2679 | static ssize_t |
3cb03002 | 2680 | offset_show(struct md_rdev *rdev, char *page) |
93c8cad0 | 2681 | { |
6961ece4 | 2682 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
93c8cad0 N |
2683 | } |
2684 | ||
2685 | static ssize_t | |
3cb03002 | 2686 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
93c8cad0 | 2687 | { |
c6563a8c | 2688 | unsigned long long offset; |
b29bebd6 | 2689 | if (kstrtoull(buf, 10, &offset) < 0) |
93c8cad0 | 2690 | return -EINVAL; |
8ed0a521 | 2691 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
93c8cad0 | 2692 | return -EBUSY; |
dd8ac336 | 2693 | if (rdev->sectors && rdev->mddev->external) |
c5d79adb N |
2694 | /* Must set offset before size, so overlap checks |
2695 | * can be sane */ | |
2696 | return -EBUSY; | |
93c8cad0 | 2697 | rdev->data_offset = offset; |
25f7fd47 | 2698 | rdev->new_data_offset = offset; |
93c8cad0 N |
2699 | return len; |
2700 | } | |
2701 | ||
2702 | static struct rdev_sysfs_entry rdev_offset = | |
80ca3a44 | 2703 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
93c8cad0 | 2704 | |
c6563a8c N |
2705 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
2706 | { | |
2707 | return sprintf(page, "%llu\n", | |
2708 | (unsigned long long)rdev->new_data_offset); | |
2709 | } | |
2710 | ||
2711 | static ssize_t new_offset_store(struct md_rdev *rdev, | |
2712 | const char *buf, size_t len) | |
2713 | { | |
2714 | unsigned long long new_offset; | |
2715 | struct mddev *mddev = rdev->mddev; | |
2716 | ||
b29bebd6 | 2717 | if (kstrtoull(buf, 10, &new_offset) < 0) |
c6563a8c N |
2718 | return -EINVAL; |
2719 | ||
f851b60d N |
2720 | if (mddev->sync_thread || |
2721 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) | |
c6563a8c N |
2722 | return -EBUSY; |
2723 | if (new_offset == rdev->data_offset) | |
2724 | /* reset is always permitted */ | |
2725 | ; | |
2726 | else if (new_offset > rdev->data_offset) { | |
2727 | /* must not push array size beyond rdev_sectors */ | |
2728 | if (new_offset - rdev->data_offset | |
2729 | + mddev->dev_sectors > rdev->sectors) | |
2730 | return -E2BIG; | |
2731 | } | |
2732 | /* Metadata worries about other space details. */ | |
2733 | ||
2734 | /* decreasing the offset is inconsistent with a backwards | |
2735 | * reshape. | |
2736 | */ | |
2737 | if (new_offset < rdev->data_offset && | |
2738 | mddev->reshape_backwards) | |
2739 | return -EINVAL; | |
2740 | /* Increasing offset is inconsistent with forwards | |
2741 | * reshape. reshape_direction should be set to | |
2742 | * 'backwards' first. | |
2743 | */ | |
2744 | if (new_offset > rdev->data_offset && | |
2745 | !mddev->reshape_backwards) | |
2746 | return -EINVAL; | |
2747 | ||
2748 | if (mddev->pers && mddev->persistent && | |
2749 | !super_types[mddev->major_version] | |
2750 | .allow_new_offset(rdev, new_offset)) | |
2751 | return -E2BIG; | |
2752 | rdev->new_data_offset = new_offset; | |
2753 | if (new_offset > rdev->data_offset) | |
2754 | mddev->reshape_backwards = 1; | |
2755 | else if (new_offset < rdev->data_offset) | |
2756 | mddev->reshape_backwards = 0; | |
2757 | ||
2758 | return len; | |
2759 | } | |
2760 | static struct rdev_sysfs_entry rdev_new_offset = | |
2761 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); | |
2762 | ||
83303b61 | 2763 | static ssize_t |
3cb03002 | 2764 | rdev_size_show(struct md_rdev *rdev, char *page) |
83303b61 | 2765 | { |
dd8ac336 | 2766 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
83303b61 N |
2767 | } |
2768 | ||
c5d79adb N |
2769 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
2770 | { | |
2771 | /* check if two start/length pairs overlap */ | |
2772 | if (s1+l1 <= s2) | |
2773 | return 0; | |
2774 | if (s2+l2 <= s1) | |
2775 | return 0; | |
2776 | return 1; | |
2777 | } | |
2778 | ||
b522adcd DW |
2779 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
2780 | { | |
2781 | unsigned long long blocks; | |
2782 | sector_t new; | |
2783 | ||
b29bebd6 | 2784 | if (kstrtoull(buf, 10, &blocks) < 0) |
b522adcd DW |
2785 | return -EINVAL; |
2786 | ||
2787 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) | |
2788 | return -EINVAL; /* sector conversion overflow */ | |
2789 | ||
2790 | new = blocks * 2; | |
2791 | if (new != blocks * 2) | |
2792 | return -EINVAL; /* unsigned long long to sector_t overflow */ | |
2793 | ||
2794 | *sectors = new; | |
2795 | return 0; | |
2796 | } | |
2797 | ||
83303b61 | 2798 | static ssize_t |
3cb03002 | 2799 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
83303b61 | 2800 | { |
fd01b88c | 2801 | struct mddev *my_mddev = rdev->mddev; |
dd8ac336 | 2802 | sector_t oldsectors = rdev->sectors; |
b522adcd | 2803 | sector_t sectors; |
27c529bb | 2804 | |
b522adcd | 2805 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
d7027458 | 2806 | return -EINVAL; |
c6563a8c N |
2807 | if (rdev->data_offset != rdev->new_data_offset) |
2808 | return -EINVAL; /* too confusing */ | |
0cd17fec | 2809 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
d7027458 | 2810 | if (my_mddev->persistent) { |
dd8ac336 AN |
2811 | sectors = super_types[my_mddev->major_version]. |
2812 | rdev_size_change(rdev, sectors); | |
2813 | if (!sectors) | |
0cd17fec | 2814 | return -EBUSY; |
dd8ac336 | 2815 | } else if (!sectors) |
77304d2a | 2816 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
dd8ac336 | 2817 | rdev->data_offset; |
a6468539 N |
2818 | if (!my_mddev->pers->resize) |
2819 | /* Cannot change size for RAID0 or Linear etc */ | |
2820 | return -EINVAL; | |
0cd17fec | 2821 | } |
dd8ac336 | 2822 | if (sectors < my_mddev->dev_sectors) |
7d3c6f87 | 2823 | return -EINVAL; /* component must fit device */ |
0cd17fec | 2824 | |
dd8ac336 AN |
2825 | rdev->sectors = sectors; |
2826 | if (sectors > oldsectors && my_mddev->external) { | |
8b1afc3d N |
2827 | /* Need to check that all other rdevs with the same |
2828 | * ->bdev do not overlap. 'rcu' is sufficient to walk | |
2829 | * the rdev lists safely. | |
2830 | * This check does not provide a hard guarantee, it | |
2831 | * just helps avoid dangerous mistakes. | |
c5d79adb | 2832 | */ |
fd01b88c | 2833 | struct mddev *mddev; |
c5d79adb | 2834 | int overlap = 0; |
159ec1fc | 2835 | struct list_head *tmp; |
c5d79adb | 2836 | |
8b1afc3d | 2837 | rcu_read_lock(); |
29ac4aa3 | 2838 | for_each_mddev(mddev, tmp) { |
3cb03002 | 2839 | struct md_rdev *rdev2; |
c5d79adb | 2840 | |
dafb20fa | 2841 | rdev_for_each(rdev2, mddev) |
f21e9ff7 N |
2842 | if (rdev->bdev == rdev2->bdev && |
2843 | rdev != rdev2 && | |
2844 | overlaps(rdev->data_offset, rdev->sectors, | |
2845 | rdev2->data_offset, | |
2846 | rdev2->sectors)) { | |
c5d79adb N |
2847 | overlap = 1; |
2848 | break; | |
2849 | } | |
c5d79adb N |
2850 | if (overlap) { |
2851 | mddev_put(mddev); | |
2852 | break; | |
2853 | } | |
2854 | } | |
8b1afc3d | 2855 | rcu_read_unlock(); |
c5d79adb N |
2856 | if (overlap) { |
2857 | /* Someone else could have slipped in a size | |
2858 | * change here, but doing so is just silly. | |
dd8ac336 | 2859 | * We put oldsectors back because we *know* it is |
c5d79adb N |
2860 | * safe, and trust userspace not to race with |
2861 | * itself | |
2862 | */ | |
dd8ac336 | 2863 | rdev->sectors = oldsectors; |
c5d79adb N |
2864 | return -EBUSY; |
2865 | } | |
2866 | } | |
83303b61 N |
2867 | return len; |
2868 | } | |
2869 | ||
2870 | static struct rdev_sysfs_entry rdev_size = | |
80ca3a44 | 2871 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
83303b61 | 2872 | |
3cb03002 | 2873 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
06e3c817 DW |
2874 | { |
2875 | unsigned long long recovery_start = rdev->recovery_offset; | |
2876 | ||
2877 | if (test_bit(In_sync, &rdev->flags) || | |
2878 | recovery_start == MaxSector) | |
2879 | return sprintf(page, "none\n"); | |
2880 | ||
2881 | return sprintf(page, "%llu\n", recovery_start); | |
2882 | } | |
2883 | ||
3cb03002 | 2884 | static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len) |
06e3c817 DW |
2885 | { |
2886 | unsigned long long recovery_start; | |
2887 | ||
2888 | if (cmd_match(buf, "none")) | |
2889 | recovery_start = MaxSector; | |
b29bebd6 | 2890 | else if (kstrtoull(buf, 10, &recovery_start)) |
06e3c817 DW |
2891 | return -EINVAL; |
2892 | ||
2893 | if (rdev->mddev->pers && | |
2894 | rdev->raid_disk >= 0) | |
2895 | return -EBUSY; | |
2896 | ||
2897 | rdev->recovery_offset = recovery_start; | |
2898 | if (recovery_start == MaxSector) | |
2899 | set_bit(In_sync, &rdev->flags); | |
2900 | else | |
2901 | clear_bit(In_sync, &rdev->flags); | |
2902 | return len; | |
2903 | } | |
2904 | ||
2905 | static struct rdev_sysfs_entry rdev_recovery_start = | |
2906 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); | |
2907 | ||
16c791a5 N |
2908 | static ssize_t |
2909 | badblocks_show(struct badblocks *bb, char *page, int unack); | |
2910 | static ssize_t | |
2911 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); | |
2912 | ||
3cb03002 | 2913 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
16c791a5 N |
2914 | { |
2915 | return badblocks_show(&rdev->badblocks, page, 0); | |
2916 | } | |
3cb03002 | 2917 | static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len) |
16c791a5 | 2918 | { |
de393cde N |
2919 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
2920 | /* Maybe that ack was all we needed */ | |
2921 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) | |
2922 | wake_up(&rdev->blocked_wait); | |
2923 | return rv; | |
16c791a5 N |
2924 | } |
2925 | static struct rdev_sysfs_entry rdev_bad_blocks = | |
2926 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); | |
2927 | ||
3cb03002 | 2928 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
16c791a5 N |
2929 | { |
2930 | return badblocks_show(&rdev->badblocks, page, 1); | |
2931 | } | |
3cb03002 | 2932 | static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len) |
16c791a5 N |
2933 | { |
2934 | return badblocks_store(&rdev->badblocks, page, len, 1); | |
2935 | } | |
2936 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = | |
2937 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); | |
2938 | ||
86e6ffdd N |
2939 | static struct attribute *rdev_default_attrs[] = { |
2940 | &rdev_state.attr, | |
4dbcdc75 | 2941 | &rdev_errors.attr, |
014236d2 | 2942 | &rdev_slot.attr, |
93c8cad0 | 2943 | &rdev_offset.attr, |
c6563a8c | 2944 | &rdev_new_offset.attr, |
83303b61 | 2945 | &rdev_size.attr, |
06e3c817 | 2946 | &rdev_recovery_start.attr, |
16c791a5 N |
2947 | &rdev_bad_blocks.attr, |
2948 | &rdev_unack_bad_blocks.attr, | |
86e6ffdd N |
2949 | NULL, |
2950 | }; | |
2951 | static ssize_t | |
2952 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
2953 | { | |
2954 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); | |
3cb03002 | 2955 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
86e6ffdd N |
2956 | |
2957 | if (!entry->show) | |
2958 | return -EIO; | |
758bfc8a N |
2959 | if (!rdev->mddev) |
2960 | return -EBUSY; | |
2961 | return entry->show(rdev, page); | |
86e6ffdd N |
2962 | } |
2963 | ||
2964 | static ssize_t | |
2965 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, | |
2966 | const char *page, size_t length) | |
2967 | { | |
2968 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); | |
3cb03002 | 2969 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
27c529bb | 2970 | ssize_t rv; |
fd01b88c | 2971 | struct mddev *mddev = rdev->mddev; |
86e6ffdd N |
2972 | |
2973 | if (!entry->store) | |
2974 | return -EIO; | |
67463acb N |
2975 | if (!capable(CAP_SYS_ADMIN)) |
2976 | return -EACCES; | |
27c529bb | 2977 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
ca388059 | 2978 | if (!rv) { |
27c529bb N |
2979 | if (rdev->mddev == NULL) |
2980 | rv = -EBUSY; | |
2981 | else | |
2982 | rv = entry->store(rdev, page, length); | |
6a51830e | 2983 | mddev_unlock(mddev); |
ca388059 N |
2984 | } |
2985 | return rv; | |
86e6ffdd N |
2986 | } |
2987 | ||
2988 | static void rdev_free(struct kobject *ko) | |
2989 | { | |
3cb03002 | 2990 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
86e6ffdd N |
2991 | kfree(rdev); |
2992 | } | |
52cf25d0 | 2993 | static const struct sysfs_ops rdev_sysfs_ops = { |
86e6ffdd N |
2994 | .show = rdev_attr_show, |
2995 | .store = rdev_attr_store, | |
2996 | }; | |
2997 | static struct kobj_type rdev_ktype = { | |
2998 | .release = rdev_free, | |
2999 | .sysfs_ops = &rdev_sysfs_ops, | |
3000 | .default_attrs = rdev_default_attrs, | |
3001 | }; | |
3002 | ||
3cb03002 | 3003 | int md_rdev_init(struct md_rdev *rdev) |
e8bb9a83 N |
3004 | { |
3005 | rdev->desc_nr = -1; | |
3006 | rdev->saved_raid_disk = -1; | |
3007 | rdev->raid_disk = -1; | |
3008 | rdev->flags = 0; | |
3009 | rdev->data_offset = 0; | |
c6563a8c | 3010 | rdev->new_data_offset = 0; |
e8bb9a83 N |
3011 | rdev->sb_events = 0; |
3012 | rdev->last_read_error.tv_sec = 0; | |
3013 | rdev->last_read_error.tv_nsec = 0; | |
2699b672 N |
3014 | rdev->sb_loaded = 0; |
3015 | rdev->bb_page = NULL; | |
e8bb9a83 N |
3016 | atomic_set(&rdev->nr_pending, 0); |
3017 | atomic_set(&rdev->read_errors, 0); | |
3018 | atomic_set(&rdev->corrected_errors, 0); | |
3019 | ||
3020 | INIT_LIST_HEAD(&rdev->same_set); | |
3021 | init_waitqueue_head(&rdev->blocked_wait); | |
2230dfe4 N |
3022 | |
3023 | /* Add space to store bad block list. | |
3024 | * This reserves the space even on arrays where it cannot | |
3025 | * be used - I wonder if that matters | |
3026 | */ | |
3027 | rdev->badblocks.count = 0; | |
486adf72 | 3028 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
2230dfe4 N |
3029 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
3030 | seqlock_init(&rdev->badblocks.lock); | |
3031 | if (rdev->badblocks.page == NULL) | |
3032 | return -ENOMEM; | |
3033 | ||
3034 | return 0; | |
e8bb9a83 N |
3035 | } |
3036 | EXPORT_SYMBOL_GPL(md_rdev_init); | |
1da177e4 LT |
3037 | /* |
3038 | * Import a device. If 'super_format' >= 0, then sanity check the superblock | |
3039 | * | |
3040 | * mark the device faulty if: | |
3041 | * | |
3042 | * - the device is nonexistent (zero size) | |
3043 | * - the device has no valid superblock | |
3044 | * | |
3045 | * a faulty rdev _never_ has rdev->sb set. | |
3046 | */ | |
3cb03002 | 3047 | static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor) |
1da177e4 LT |
3048 | { |
3049 | char b[BDEVNAME_SIZE]; | |
3050 | int err; | |
3cb03002 | 3051 | struct md_rdev *rdev; |
1da177e4 LT |
3052 | sector_t size; |
3053 | ||
9ffae0cf | 3054 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
1da177e4 LT |
3055 | if (!rdev) { |
3056 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); | |
3057 | return ERR_PTR(-ENOMEM); | |
3058 | } | |
1da177e4 | 3059 | |
2230dfe4 N |
3060 | err = md_rdev_init(rdev); |
3061 | if (err) | |
3062 | goto abort_free; | |
3063 | err = alloc_disk_sb(rdev); | |
3064 | if (err) | |
1da177e4 LT |
3065 | goto abort_free; |
3066 | ||
c5d79adb | 3067 | err = lock_rdev(rdev, newdev, super_format == -2); |
1da177e4 LT |
3068 | if (err) |
3069 | goto abort_free; | |
3070 | ||
f9cb074b | 3071 | kobject_init(&rdev->kobj, &rdev_ktype); |
86e6ffdd | 3072 | |
77304d2a | 3073 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
1da177e4 | 3074 | if (!size) { |
f72ffdd6 | 3075 | printk(KERN_WARNING |
1da177e4 LT |
3076 | "md: %s has zero or unknown size, marking faulty!\n", |
3077 | bdevname(rdev->bdev,b)); | |
3078 | err = -EINVAL; | |
3079 | goto abort_free; | |
3080 | } | |
3081 | ||
3082 | if (super_format >= 0) { | |
3083 | err = super_types[super_format]. | |
3084 | load_super(rdev, NULL, super_minor); | |
3085 | if (err == -EINVAL) { | |
df968c4e N |
3086 | printk(KERN_WARNING |
3087 | "md: %s does not have a valid v%d.%d " | |
3088 | "superblock, not importing!\n", | |
3089 | bdevname(rdev->bdev,b), | |
3090 | super_format, super_minor); | |
1da177e4 LT |
3091 | goto abort_free; |
3092 | } | |
3093 | if (err < 0) { | |
f72ffdd6 | 3094 | printk(KERN_WARNING |
1da177e4 LT |
3095 | "md: could not read %s's sb, not importing!\n", |
3096 | bdevname(rdev->bdev,b)); | |
3097 | goto abort_free; | |
3098 | } | |
3099 | } | |
6bfe0b49 | 3100 | |
1da177e4 LT |
3101 | return rdev; |
3102 | ||
3103 | abort_free: | |
2699b672 N |
3104 | if (rdev->bdev) |
3105 | unlock_rdev(rdev); | |
545c8795 | 3106 | md_rdev_clear(rdev); |
1da177e4 LT |
3107 | kfree(rdev); |
3108 | return ERR_PTR(err); | |
3109 | } | |
3110 | ||
3111 | /* | |
3112 | * Check a full RAID array for plausibility | |
3113 | */ | |
3114 | ||
f72ffdd6 | 3115 | static void analyze_sbs(struct mddev *mddev) |
1da177e4 LT |
3116 | { |
3117 | int i; | |
3cb03002 | 3118 | struct md_rdev *rdev, *freshest, *tmp; |
1da177e4 LT |
3119 | char b[BDEVNAME_SIZE]; |
3120 | ||
3121 | freshest = NULL; | |
dafb20fa | 3122 | rdev_for_each_safe(rdev, tmp, mddev) |
1da177e4 LT |
3123 | switch (super_types[mddev->major_version]. |
3124 | load_super(rdev, freshest, mddev->minor_version)) { | |
3125 | case 1: | |
3126 | freshest = rdev; | |
3127 | break; | |
3128 | case 0: | |
3129 | break; | |
3130 | default: | |
3131 | printk( KERN_ERR \ | |
3132 | "md: fatal superblock inconsistency in %s" | |
f72ffdd6 | 3133 | " -- removing from array\n", |
1da177e4 LT |
3134 | bdevname(rdev->bdev,b)); |
3135 | kick_rdev_from_array(rdev); | |
3136 | } | |
3137 | ||
1da177e4 LT |
3138 | super_types[mddev->major_version]. |
3139 | validate_super(mddev, freshest); | |
3140 | ||
3141 | i = 0; | |
dafb20fa | 3142 | rdev_for_each_safe(rdev, tmp, mddev) { |
233fca36 N |
3143 | if (mddev->max_disks && |
3144 | (rdev->desc_nr >= mddev->max_disks || | |
3145 | i > mddev->max_disks)) { | |
de01dfad N |
3146 | printk(KERN_WARNING |
3147 | "md: %s: %s: only %d devices permitted\n", | |
3148 | mdname(mddev), bdevname(rdev->bdev, b), | |
3149 | mddev->max_disks); | |
3150 | kick_rdev_from_array(rdev); | |
3151 | continue; | |
3152 | } | |
1da177e4 LT |
3153 | if (rdev != freshest) |
3154 | if (super_types[mddev->major_version]. | |
3155 | validate_super(mddev, rdev)) { | |
3156 | printk(KERN_WARNING "md: kicking non-fresh %s" | |
3157 | " from array!\n", | |
3158 | bdevname(rdev->bdev,b)); | |
3159 | kick_rdev_from_array(rdev); | |
3160 | continue; | |
3161 | } | |
3162 | if (mddev->level == LEVEL_MULTIPATH) { | |
3163 | rdev->desc_nr = i++; | |
3164 | rdev->raid_disk = rdev->desc_nr; | |
b2d444d7 | 3165 | set_bit(In_sync, &rdev->flags); |
5e5e3e78 | 3166 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
a778b73f N |
3167 | rdev->raid_disk = -1; |
3168 | clear_bit(In_sync, &rdev->flags); | |
1da177e4 LT |
3169 | } |
3170 | } | |
1da177e4 LT |
3171 | } |
3172 | ||
72e02075 N |
3173 | /* Read a fixed-point number. |
3174 | * Numbers in sysfs attributes should be in "standard" units where | |
3175 | * possible, so time should be in seconds. | |
f72ffdd6 | 3176 | * However we internally use a a much smaller unit such as |
72e02075 N |
3177 | * milliseconds or jiffies. |
3178 | * This function takes a decimal number with a possible fractional | |
3179 | * component, and produces an integer which is the result of | |
3180 | * multiplying that number by 10^'scale'. | |
3181 | * all without any floating-point arithmetic. | |
3182 | */ | |
3183 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) | |
3184 | { | |
3185 | unsigned long result = 0; | |
3186 | long decimals = -1; | |
3187 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { | |
3188 | if (*cp == '.') | |
3189 | decimals = 0; | |
3190 | else if (decimals < scale) { | |
3191 | unsigned int value; | |
3192 | value = *cp - '0'; | |
3193 | result = result * 10 + value; | |
3194 | if (decimals >= 0) | |
3195 | decimals++; | |
3196 | } | |
3197 | cp++; | |
3198 | } | |
3199 | if (*cp == '\n') | |
3200 | cp++; | |
3201 | if (*cp) | |
3202 | return -EINVAL; | |
3203 | if (decimals < 0) | |
3204 | decimals = 0; | |
3205 | while (decimals < scale) { | |
3206 | result *= 10; | |
3207 | decimals ++; | |
3208 | } | |
3209 | *res = result; | |
3210 | return 0; | |
3211 | } | |
3212 | ||
19052c0e N |
3213 | static void md_safemode_timeout(unsigned long data); |
3214 | ||
16f17b39 | 3215 | static ssize_t |
fd01b88c | 3216 | safe_delay_show(struct mddev *mddev, char *page) |
16f17b39 N |
3217 | { |
3218 | int msec = (mddev->safemode_delay*1000)/HZ; | |
3219 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); | |
3220 | } | |
3221 | static ssize_t | |
fd01b88c | 3222 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
16f17b39 | 3223 | { |
16f17b39 | 3224 | unsigned long msec; |
97ce0a7f | 3225 | |
72e02075 | 3226 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
16f17b39 | 3227 | return -EINVAL; |
16f17b39 N |
3228 | if (msec == 0) |
3229 | mddev->safemode_delay = 0; | |
3230 | else { | |
19052c0e | 3231 | unsigned long old_delay = mddev->safemode_delay; |
1b30e66f N |
3232 | unsigned long new_delay = (msec*HZ)/1000; |
3233 | ||
3234 | if (new_delay == 0) | |
3235 | new_delay = 1; | |
3236 | mddev->safemode_delay = new_delay; | |
3237 | if (new_delay < old_delay || old_delay == 0) | |
3238 | mod_timer(&mddev->safemode_timer, jiffies+1); | |
16f17b39 N |
3239 | } |
3240 | return len; | |
3241 | } | |
3242 | static struct md_sysfs_entry md_safe_delay = | |
80ca3a44 | 3243 | __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store); |
16f17b39 | 3244 | |
eae1701f | 3245 | static ssize_t |
fd01b88c | 3246 | level_show(struct mddev *mddev, char *page) |
eae1701f | 3247 | { |
36d091f4 N |
3248 | struct md_personality *p; |
3249 | int ret; | |
3250 | spin_lock(&mddev->lock); | |
3251 | p = mddev->pers; | |
d9d166c2 | 3252 | if (p) |
36d091f4 | 3253 | ret = sprintf(page, "%s\n", p->name); |
d9d166c2 | 3254 | else if (mddev->clevel[0]) |
36d091f4 | 3255 | ret = sprintf(page, "%s\n", mddev->clevel); |
d9d166c2 | 3256 | else if (mddev->level != LEVEL_NONE) |
36d091f4 | 3257 | ret = sprintf(page, "%d\n", mddev->level); |
d9d166c2 | 3258 | else |
36d091f4 N |
3259 | ret = 0; |
3260 | spin_unlock(&mddev->lock); | |
3261 | return ret; | |
eae1701f N |
3262 | } |
3263 | ||
d9d166c2 | 3264 | static ssize_t |
fd01b88c | 3265 | level_store(struct mddev *mddev, const char *buf, size_t len) |
d9d166c2 | 3266 | { |
f2859af6 | 3267 | char clevel[16]; |
6791875e N |
3268 | ssize_t rv; |
3269 | size_t slen = len; | |
db721d32 | 3270 | struct md_personality *pers, *oldpers; |
f2859af6 | 3271 | long level; |
db721d32 | 3272 | void *priv, *oldpriv; |
3cb03002 | 3273 | struct md_rdev *rdev; |
245f46c2 | 3274 | |
6791875e N |
3275 | if (slen == 0 || slen >= sizeof(clevel)) |
3276 | return -EINVAL; | |
3277 | ||
3278 | rv = mddev_lock(mddev); | |
3279 | if (rv) | |
3280 | return rv; | |
3281 | ||
245f46c2 | 3282 | if (mddev->pers == NULL) { |
6791875e N |
3283 | strncpy(mddev->clevel, buf, slen); |
3284 | if (mddev->clevel[slen-1] == '\n') | |
3285 | slen--; | |
3286 | mddev->clevel[slen] = 0; | |
245f46c2 | 3287 | mddev->level = LEVEL_NONE; |
6791875e N |
3288 | rv = len; |
3289 | goto out_unlock; | |
245f46c2 | 3290 | } |
6791875e | 3291 | rv = -EROFS; |
bd8839e0 | 3292 | if (mddev->ro) |
6791875e | 3293 | goto out_unlock; |
245f46c2 N |
3294 | |
3295 | /* request to change the personality. Need to ensure: | |
3296 | * - array is not engaged in resync/recovery/reshape | |
3297 | * - old personality can be suspended | |
3298 | * - new personality will access other array. | |
3299 | */ | |
3300 | ||
6791875e | 3301 | rv = -EBUSY; |
bb4f1e9d | 3302 | if (mddev->sync_thread || |
f851b60d | 3303 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
bb4f1e9d N |
3304 | mddev->reshape_position != MaxSector || |
3305 | mddev->sysfs_active) | |
6791875e | 3306 | goto out_unlock; |
245f46c2 | 3307 | |
6791875e | 3308 | rv = -EINVAL; |
245f46c2 N |
3309 | if (!mddev->pers->quiesce) { |
3310 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", | |
3311 | mdname(mddev), mddev->pers->name); | |
6791875e | 3312 | goto out_unlock; |
245f46c2 N |
3313 | } |
3314 | ||
3315 | /* Now find the new personality */ | |
6791875e N |
3316 | strncpy(clevel, buf, slen); |
3317 | if (clevel[slen-1] == '\n') | |
3318 | slen--; | |
3319 | clevel[slen] = 0; | |
b29bebd6 | 3320 | if (kstrtol(clevel, 10, &level)) |
f2859af6 | 3321 | level = LEVEL_NONE; |
245f46c2 | 3322 | |
f2859af6 DW |
3323 | if (request_module("md-%s", clevel) != 0) |
3324 | request_module("md-level-%s", clevel); | |
245f46c2 | 3325 | spin_lock(&pers_lock); |
f2859af6 | 3326 | pers = find_pers(level, clevel); |
245f46c2 N |
3327 | if (!pers || !try_module_get(pers->owner)) { |
3328 | spin_unlock(&pers_lock); | |
f2859af6 | 3329 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
6791875e N |
3330 | rv = -EINVAL; |
3331 | goto out_unlock; | |
245f46c2 N |
3332 | } |
3333 | spin_unlock(&pers_lock); | |
3334 | ||
3335 | if (pers == mddev->pers) { | |
3336 | /* Nothing to do! */ | |
3337 | module_put(pers->owner); | |
6791875e N |
3338 | rv = len; |
3339 | goto out_unlock; | |
245f46c2 N |
3340 | } |
3341 | if (!pers->takeover) { | |
3342 | module_put(pers->owner); | |
3343 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", | |
f2859af6 | 3344 | mdname(mddev), clevel); |
6791875e N |
3345 | rv = -EINVAL; |
3346 | goto out_unlock; | |
245f46c2 N |
3347 | } |
3348 | ||
dafb20fa | 3349 | rdev_for_each(rdev, mddev) |
e93f68a1 N |
3350 | rdev->new_raid_disk = rdev->raid_disk; |
3351 | ||
245f46c2 N |
3352 | /* ->takeover must set new_* and/or delta_disks |
3353 | * if it succeeds, and may set them when it fails. | |
3354 | */ | |
3355 | priv = pers->takeover(mddev); | |
3356 | if (IS_ERR(priv)) { | |
3357 | mddev->new_level = mddev->level; | |
3358 | mddev->new_layout = mddev->layout; | |
664e7c41 | 3359 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
245f46c2 N |
3360 | mddev->raid_disks -= mddev->delta_disks; |
3361 | mddev->delta_disks = 0; | |
2c810cdd | 3362 | mddev->reshape_backwards = 0; |
245f46c2 N |
3363 | module_put(pers->owner); |
3364 | printk(KERN_WARNING "md: %s: %s would not accept array\n", | |
f2859af6 | 3365 | mdname(mddev), clevel); |
6791875e N |
3366 | rv = PTR_ERR(priv); |
3367 | goto out_unlock; | |
245f46c2 N |
3368 | } |
3369 | ||
3370 | /* Looks like we have a winner */ | |
3371 | mddev_suspend(mddev); | |
5aa61f42 | 3372 | mddev_detach(mddev); |
36d091f4 N |
3373 | |
3374 | spin_lock(&mddev->lock); | |
db721d32 N |
3375 | oldpers = mddev->pers; |
3376 | oldpriv = mddev->private; | |
3377 | mddev->pers = pers; | |
3378 | mddev->private = priv; | |
3379 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); | |
3380 | mddev->level = mddev->new_level; | |
3381 | mddev->layout = mddev->new_layout; | |
3382 | mddev->chunk_sectors = mddev->new_chunk_sectors; | |
3383 | mddev->delta_disks = 0; | |
3384 | mddev->reshape_backwards = 0; | |
3385 | mddev->degraded = 0; | |
36d091f4 | 3386 | spin_unlock(&mddev->lock); |
db721d32 N |
3387 | |
3388 | if (oldpers->sync_request == NULL && | |
3389 | mddev->external) { | |
3390 | /* We are converting from a no-redundancy array | |
3391 | * to a redundancy array and metadata is managed | |
3392 | * externally so we need to be sure that writes | |
3393 | * won't block due to a need to transition | |
3394 | * clean->dirty | |
3395 | * until external management is started. | |
3396 | */ | |
3397 | mddev->in_sync = 0; | |
3398 | mddev->safemode_delay = 0; | |
3399 | mddev->safemode = 0; | |
3400 | } | |
f72ffdd6 | 3401 | |
db721d32 N |
3402 | oldpers->free(mddev, oldpriv); |
3403 | ||
3404 | if (oldpers->sync_request == NULL && | |
a64c876f N |
3405 | pers->sync_request != NULL) { |
3406 | /* need to add the md_redundancy_group */ | |
3407 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) | |
3408 | printk(KERN_WARNING | |
3409 | "md: cannot register extra attributes for %s\n", | |
3410 | mdname(mddev)); | |
388975cc | 3411 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
f72ffdd6 | 3412 | } |
db721d32 | 3413 | if (oldpers->sync_request != NULL && |
a64c876f N |
3414 | pers->sync_request == NULL) { |
3415 | /* need to remove the md_redundancy_group */ | |
3416 | if (mddev->to_remove == NULL) | |
3417 | mddev->to_remove = &md_redundancy_group; | |
3418 | } | |
3419 | ||
dafb20fa | 3420 | rdev_for_each(rdev, mddev) { |
e93f68a1 N |
3421 | if (rdev->raid_disk < 0) |
3422 | continue; | |
bf2cb0da | 3423 | if (rdev->new_raid_disk >= mddev->raid_disks) |
e93f68a1 N |
3424 | rdev->new_raid_disk = -1; |
3425 | if (rdev->new_raid_disk == rdev->raid_disk) | |
3426 | continue; | |
36fad858 | 3427 | sysfs_unlink_rdev(mddev, rdev); |
e93f68a1 | 3428 | } |
dafb20fa | 3429 | rdev_for_each(rdev, mddev) { |
e93f68a1 N |
3430 | if (rdev->raid_disk < 0) |
3431 | continue; | |
3432 | if (rdev->new_raid_disk == rdev->raid_disk) | |
3433 | continue; | |
3434 | rdev->raid_disk = rdev->new_raid_disk; | |
3435 | if (rdev->raid_disk < 0) | |
3a981b03 | 3436 | clear_bit(In_sync, &rdev->flags); |
e93f68a1 | 3437 | else { |
36fad858 NK |
3438 | if (sysfs_link_rdev(mddev, rdev)) |
3439 | printk(KERN_WARNING "md: cannot register rd%d" | |
3440 | " for %s after level change\n", | |
3441 | rdev->raid_disk, mdname(mddev)); | |
3a981b03 | 3442 | } |
e93f68a1 N |
3443 | } |
3444 | ||
db721d32 | 3445 | if (pers->sync_request == NULL) { |
9af204cf TM |
3446 | /* this is now an array without redundancy, so |
3447 | * it must always be in_sync | |
3448 | */ | |
3449 | mddev->in_sync = 1; | |
3450 | del_timer_sync(&mddev->safemode_timer); | |
3451 | } | |
02e5f5c0 | 3452 | blk_set_stacking_limits(&mddev->queue->limits); |
245f46c2 | 3453 | pers->run(mddev); |
245f46c2 | 3454 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
47525e59 | 3455 | mddev_resume(mddev); |
830778a1 N |
3456 | if (!mddev->thread) |
3457 | md_update_sb(mddev, 1); | |
5cac7861 | 3458 | sysfs_notify(&mddev->kobj, NULL, "level"); |
bb7f8d22 | 3459 | md_new_event(mddev); |
6791875e N |
3460 | rv = len; |
3461 | out_unlock: | |
3462 | mddev_unlock(mddev); | |
d9d166c2 N |
3463 | return rv; |
3464 | } | |
3465 | ||
3466 | static struct md_sysfs_entry md_level = | |
80ca3a44 | 3467 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
eae1701f | 3468 | |
d4dbd025 | 3469 | static ssize_t |
fd01b88c | 3470 | layout_show(struct mddev *mddev, char *page) |
d4dbd025 N |
3471 | { |
3472 | /* just a number, not meaningful for all levels */ | |
08a02ecd N |
3473 | if (mddev->reshape_position != MaxSector && |
3474 | mddev->layout != mddev->new_layout) | |
3475 | return sprintf(page, "%d (%d)\n", | |
3476 | mddev->new_layout, mddev->layout); | |
d4dbd025 N |
3477 | return sprintf(page, "%d\n", mddev->layout); |
3478 | } | |
3479 | ||
3480 | static ssize_t | |
fd01b88c | 3481 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
d4dbd025 N |
3482 | { |
3483 | char *e; | |
3484 | unsigned long n = simple_strtoul(buf, &e, 10); | |
6791875e | 3485 | int err; |
d4dbd025 N |
3486 | |
3487 | if (!*buf || (*e && *e != '\n')) | |
3488 | return -EINVAL; | |
6791875e N |
3489 | err = mddev_lock(mddev); |
3490 | if (err) | |
3491 | return err; | |
d4dbd025 | 3492 | |
b3546035 | 3493 | if (mddev->pers) { |
50ac168a | 3494 | if (mddev->pers->check_reshape == NULL) |
6791875e N |
3495 | err = -EBUSY; |
3496 | else if (mddev->ro) | |
3497 | err = -EROFS; | |
3498 | else { | |
3499 | mddev->new_layout = n; | |
3500 | err = mddev->pers->check_reshape(mddev); | |
3501 | if (err) | |
3502 | mddev->new_layout = mddev->layout; | |
597a711b | 3503 | } |
b3546035 | 3504 | } else { |
08a02ecd | 3505 | mddev->new_layout = n; |
b3546035 N |
3506 | if (mddev->reshape_position == MaxSector) |
3507 | mddev->layout = n; | |
3508 | } | |
6791875e N |
3509 | mddev_unlock(mddev); |
3510 | return err ?: len; | |
d4dbd025 N |
3511 | } |
3512 | static struct md_sysfs_entry md_layout = | |
80ca3a44 | 3513 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
d4dbd025 | 3514 | |
eae1701f | 3515 | static ssize_t |
fd01b88c | 3516 | raid_disks_show(struct mddev *mddev, char *page) |
eae1701f | 3517 | { |
bb636547 N |
3518 | if (mddev->raid_disks == 0) |
3519 | return 0; | |
08a02ecd N |
3520 | if (mddev->reshape_position != MaxSector && |
3521 | mddev->delta_disks != 0) | |
3522 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, | |
3523 | mddev->raid_disks - mddev->delta_disks); | |
eae1701f N |
3524 | return sprintf(page, "%d\n", mddev->raid_disks); |
3525 | } | |
3526 | ||
fd01b88c | 3527 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
da943b99 N |
3528 | |
3529 | static ssize_t | |
fd01b88c | 3530 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
da943b99 | 3531 | { |
da943b99 | 3532 | char *e; |
6791875e | 3533 | int err; |
da943b99 N |
3534 | unsigned long n = simple_strtoul(buf, &e, 10); |
3535 | ||
3536 | if (!*buf || (*e && *e != '\n')) | |
3537 | return -EINVAL; | |
3538 | ||
6791875e N |
3539 | err = mddev_lock(mddev); |
3540 | if (err) | |
3541 | return err; | |
da943b99 | 3542 | if (mddev->pers) |
6791875e | 3543 | err = update_raid_disks(mddev, n); |
08a02ecd | 3544 | else if (mddev->reshape_position != MaxSector) { |
c6563a8c | 3545 | struct md_rdev *rdev; |
08a02ecd | 3546 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
c6563a8c | 3547 | |
6791875e | 3548 | err = -EINVAL; |
c6563a8c N |
3549 | rdev_for_each(rdev, mddev) { |
3550 | if (olddisks < n && | |
3551 | rdev->data_offset < rdev->new_data_offset) | |
6791875e | 3552 | goto out_unlock; |
c6563a8c N |
3553 | if (olddisks > n && |
3554 | rdev->data_offset > rdev->new_data_offset) | |
6791875e | 3555 | goto out_unlock; |
c6563a8c | 3556 | } |
6791875e | 3557 | err = 0; |
08a02ecd N |
3558 | mddev->delta_disks = n - olddisks; |
3559 | mddev->raid_disks = n; | |
2c810cdd | 3560 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
08a02ecd | 3561 | } else |
da943b99 | 3562 | mddev->raid_disks = n; |
6791875e N |
3563 | out_unlock: |
3564 | mddev_unlock(mddev); | |
3565 | return err ? err : len; | |
da943b99 N |
3566 | } |
3567 | static struct md_sysfs_entry md_raid_disks = | |
80ca3a44 | 3568 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
eae1701f | 3569 | |
3b34380a | 3570 | static ssize_t |
fd01b88c | 3571 | chunk_size_show(struct mddev *mddev, char *page) |
3b34380a | 3572 | { |
08a02ecd | 3573 | if (mddev->reshape_position != MaxSector && |
664e7c41 AN |
3574 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
3575 | return sprintf(page, "%d (%d)\n", | |
3576 | mddev->new_chunk_sectors << 9, | |
9d8f0363 AN |
3577 | mddev->chunk_sectors << 9); |
3578 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); | |
3b34380a N |
3579 | } |
3580 | ||
3581 | static ssize_t | |
fd01b88c | 3582 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
3b34380a | 3583 | { |
6791875e | 3584 | int err; |
3b34380a N |
3585 | char *e; |
3586 | unsigned long n = simple_strtoul(buf, &e, 10); | |
3587 | ||
3b34380a N |
3588 | if (!*buf || (*e && *e != '\n')) |
3589 | return -EINVAL; | |
3590 | ||
6791875e N |
3591 | err = mddev_lock(mddev); |
3592 | if (err) | |
3593 | return err; | |
b3546035 | 3594 | if (mddev->pers) { |
50ac168a | 3595 | if (mddev->pers->check_reshape == NULL) |
6791875e N |
3596 | err = -EBUSY; |
3597 | else if (mddev->ro) | |
3598 | err = -EROFS; | |
3599 | else { | |
3600 | mddev->new_chunk_sectors = n >> 9; | |
3601 | err = mddev->pers->check_reshape(mddev); | |
3602 | if (err) | |
3603 | mddev->new_chunk_sectors = mddev->chunk_sectors; | |
597a711b | 3604 | } |
b3546035 | 3605 | } else { |
664e7c41 | 3606 | mddev->new_chunk_sectors = n >> 9; |
b3546035 | 3607 | if (mddev->reshape_position == MaxSector) |
9d8f0363 | 3608 | mddev->chunk_sectors = n >> 9; |
b3546035 | 3609 | } |
6791875e N |
3610 | mddev_unlock(mddev); |
3611 | return err ?: len; | |
3b34380a N |
3612 | } |
3613 | static struct md_sysfs_entry md_chunk_size = | |
80ca3a44 | 3614 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
3b34380a | 3615 | |
a94213b1 | 3616 | static ssize_t |
fd01b88c | 3617 | resync_start_show(struct mddev *mddev, char *page) |
a94213b1 | 3618 | { |
d1a7c503 N |
3619 | if (mddev->recovery_cp == MaxSector) |
3620 | return sprintf(page, "none\n"); | |
a94213b1 N |
3621 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
3622 | } | |
3623 | ||
3624 | static ssize_t | |
fd01b88c | 3625 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
a94213b1 | 3626 | { |
6791875e | 3627 | int err; |
a94213b1 N |
3628 | char *e; |
3629 | unsigned long long n = simple_strtoull(buf, &e, 10); | |
3630 | ||
6791875e N |
3631 | err = mddev_lock(mddev); |
3632 | if (err) | |
3633 | return err; | |
b098636c | 3634 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
6791875e N |
3635 | err = -EBUSY; |
3636 | else if (cmd_match(buf, "none")) | |
06e3c817 DW |
3637 | n = MaxSector; |
3638 | else if (!*buf || (*e && *e != '\n')) | |
6791875e | 3639 | err = -EINVAL; |
a94213b1 | 3640 | |
6791875e N |
3641 | if (!err) { |
3642 | mddev->recovery_cp = n; | |
3643 | if (mddev->pers) | |
3644 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); | |
3645 | } | |
3646 | mddev_unlock(mddev); | |
3647 | return err ?: len; | |
a94213b1 N |
3648 | } |
3649 | static struct md_sysfs_entry md_resync_start = | |
80ca3a44 | 3650 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
a94213b1 | 3651 | |
9e653b63 N |
3652 | /* |
3653 | * The array state can be: | |
3654 | * | |
3655 | * clear | |
3656 | * No devices, no size, no level | |
3657 | * Equivalent to STOP_ARRAY ioctl | |
3658 | * inactive | |
3659 | * May have some settings, but array is not active | |
3660 | * all IO results in error | |
3661 | * When written, doesn't tear down array, but just stops it | |
3662 | * suspended (not supported yet) | |
3663 | * All IO requests will block. The array can be reconfigured. | |
910d8cb3 | 3664 | * Writing this, if accepted, will block until array is quiescent |
9e653b63 N |
3665 | * readonly |
3666 | * no resync can happen. no superblocks get written. | |
3667 | * write requests fail | |
3668 | * read-auto | |
3669 | * like readonly, but behaves like 'clean' on a write request. | |
3670 | * | |
3671 | * clean - no pending writes, but otherwise active. | |
3672 | * When written to inactive array, starts without resync | |
3673 | * If a write request arrives then | |
3674 | * if metadata is known, mark 'dirty' and switch to 'active'. | |
3675 | * if not known, block and switch to write-pending | |
3676 | * If written to an active array that has pending writes, then fails. | |
3677 | * active | |
3678 | * fully active: IO and resync can be happening. | |
3679 | * When written to inactive array, starts with resync | |
3680 | * | |
3681 | * write-pending | |
3682 | * clean, but writes are blocked waiting for 'active' to be written. | |
3683 | * | |
3684 | * active-idle | |
3685 | * like active, but no writes have been seen for a while (100msec). | |
3686 | * | |
3687 | */ | |
3688 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, | |
3689 | write_pending, active_idle, bad_word}; | |
05381954 | 3690 | static char *array_states[] = { |
9e653b63 N |
3691 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
3692 | "write-pending", "active-idle", NULL }; | |
3693 | ||
3694 | static int match_word(const char *word, char **list) | |
3695 | { | |
3696 | int n; | |
3697 | for (n=0; list[n]; n++) | |
3698 | if (cmd_match(word, list[n])) | |
3699 | break; | |
3700 | return n; | |
3701 | } | |
3702 | ||
3703 | static ssize_t | |
fd01b88c | 3704 | array_state_show(struct mddev *mddev, char *page) |
9e653b63 N |
3705 | { |
3706 | enum array_state st = inactive; | |
3707 | ||
3708 | if (mddev->pers) | |
3709 | switch(mddev->ro) { | |
3710 | case 1: | |
3711 | st = readonly; | |
3712 | break; | |
3713 | case 2: | |
3714 | st = read_auto; | |
3715 | break; | |
3716 | case 0: | |
3717 | if (mddev->in_sync) | |
3718 | st = clean; | |
070dc6dd | 3719 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
e691063a | 3720 | st = write_pending; |
9e653b63 N |
3721 | else if (mddev->safemode) |
3722 | st = active_idle; | |
3723 | else | |
3724 | st = active; | |
3725 | } | |
3726 | else { | |
3727 | if (list_empty(&mddev->disks) && | |
3728 | mddev->raid_disks == 0 && | |
58c0fed4 | 3729 | mddev->dev_sectors == 0) |
9e653b63 N |
3730 | st = clear; |
3731 | else | |
3732 | st = inactive; | |
3733 | } | |
3734 | return sprintf(page, "%s\n", array_states[st]); | |
3735 | } | |
3736 | ||
f72ffdd6 N |
3737 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
3738 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); | |
3739 | static int do_md_run(struct mddev *mddev); | |
fd01b88c | 3740 | static int restart_array(struct mddev *mddev); |
9e653b63 N |
3741 | |
3742 | static ssize_t | |
fd01b88c | 3743 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
9e653b63 | 3744 | { |
6791875e | 3745 | int err; |
9e653b63 | 3746 | enum array_state st = match_word(buf, array_states); |
6791875e N |
3747 | |
3748 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { | |
3749 | /* don't take reconfig_mutex when toggling between | |
3750 | * clean and active | |
3751 | */ | |
3752 | spin_lock(&mddev->lock); | |
3753 | if (st == active) { | |
3754 | restart_array(mddev); | |
3755 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); | |
3756 | wake_up(&mddev->sb_wait); | |
3757 | err = 0; | |
3758 | } else /* st == clean */ { | |
3759 | restart_array(mddev); | |
3760 | if (atomic_read(&mddev->writes_pending) == 0) { | |
3761 | if (mddev->in_sync == 0) { | |
3762 | mddev->in_sync = 1; | |
3763 | if (mddev->safemode == 1) | |
3764 | mddev->safemode = 0; | |
3765 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); | |
3766 | } | |
3767 | err = 0; | |
3768 | } else | |
3769 | err = -EBUSY; | |
3770 | } | |
3771 | spin_unlock(&mddev->lock); | |
3772 | return err; | |
3773 | } | |
3774 | err = mddev_lock(mddev); | |
3775 | if (err) | |
3776 | return err; | |
3777 | err = -EINVAL; | |
9e653b63 N |
3778 | switch(st) { |
3779 | case bad_word: | |
3780 | break; | |
3781 | case clear: | |
3782 | /* stopping an active array */ | |
a05b7ea0 | 3783 | err = do_md_stop(mddev, 0, NULL); |
9e653b63 N |
3784 | break; |
3785 | case inactive: | |
3786 | /* stopping an active array */ | |
90cf195d | 3787 | if (mddev->pers) |
a05b7ea0 | 3788 | err = do_md_stop(mddev, 2, NULL); |
90cf195d | 3789 | else |
e691063a | 3790 | err = 0; /* already inactive */ |
9e653b63 N |
3791 | break; |
3792 | case suspended: | |
3793 | break; /* not supported yet */ | |
3794 | case readonly: | |
3795 | if (mddev->pers) | |
a05b7ea0 | 3796 | err = md_set_readonly(mddev, NULL); |
9e653b63 N |
3797 | else { |
3798 | mddev->ro = 1; | |
648b629e | 3799 | set_disk_ro(mddev->gendisk, 1); |
9e653b63 N |
3800 | err = do_md_run(mddev); |
3801 | } | |
3802 | break; | |
3803 | case read_auto: | |
9e653b63 | 3804 | if (mddev->pers) { |
80268ee9 | 3805 | if (mddev->ro == 0) |
a05b7ea0 | 3806 | err = md_set_readonly(mddev, NULL); |
80268ee9 | 3807 | else if (mddev->ro == 1) |
648b629e N |
3808 | err = restart_array(mddev); |
3809 | if (err == 0) { | |
3810 | mddev->ro = 2; | |
3811 | set_disk_ro(mddev->gendisk, 0); | |
3812 | } | |
9e653b63 N |
3813 | } else { |
3814 | mddev->ro = 2; | |
3815 | err = do_md_run(mddev); | |
3816 | } | |
3817 | break; | |
3818 | case clean: | |
3819 | if (mddev->pers) { | |
3820 | restart_array(mddev); | |
85572d7c | 3821 | spin_lock(&mddev->lock); |
9e653b63 | 3822 | if (atomic_read(&mddev->writes_pending) == 0) { |
e691063a N |
3823 | if (mddev->in_sync == 0) { |
3824 | mddev->in_sync = 1; | |
31a59e34 N |
3825 | if (mddev->safemode == 1) |
3826 | mddev->safemode = 0; | |
070dc6dd | 3827 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
e691063a N |
3828 | } |
3829 | err = 0; | |
3830 | } else | |
3831 | err = -EBUSY; | |
85572d7c | 3832 | spin_unlock(&mddev->lock); |
5bf29597 N |
3833 | } else |
3834 | err = -EINVAL; | |
9e653b63 N |
3835 | break; |
3836 | case active: | |
3837 | if (mddev->pers) { | |
3838 | restart_array(mddev); | |
070dc6dd | 3839 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
9e653b63 N |
3840 | wake_up(&mddev->sb_wait); |
3841 | err = 0; | |
3842 | } else { | |
3843 | mddev->ro = 0; | |
648b629e | 3844 | set_disk_ro(mddev->gendisk, 0); |
9e653b63 N |
3845 | err = do_md_run(mddev); |
3846 | } | |
3847 | break; | |
3848 | case write_pending: | |
3849 | case active_idle: | |
3850 | /* these cannot be set */ | |
3851 | break; | |
3852 | } | |
6791875e N |
3853 | |
3854 | if (!err) { | |
1d23f178 N |
3855 | if (mddev->hold_active == UNTIL_IOCTL) |
3856 | mddev->hold_active = 0; | |
00bcb4ac | 3857 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0fd62b86 | 3858 | } |
6791875e N |
3859 | mddev_unlock(mddev); |
3860 | return err ?: len; | |
9e653b63 | 3861 | } |
80ca3a44 N |
3862 | static struct md_sysfs_entry md_array_state = |
3863 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); | |
9e653b63 | 3864 | |
1e50915f | 3865 | static ssize_t |
fd01b88c | 3866 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
1e50915f RB |
3867 | return sprintf(page, "%d\n", |
3868 | atomic_read(&mddev->max_corr_read_errors)); | |
3869 | } | |
3870 | ||
3871 | static ssize_t | |
fd01b88c | 3872 | max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len) |
1e50915f RB |
3873 | { |
3874 | char *e; | |
3875 | unsigned long n = simple_strtoul(buf, &e, 10); | |
3876 | ||
3877 | if (*buf && (*e == 0 || *e == '\n')) { | |
3878 | atomic_set(&mddev->max_corr_read_errors, n); | |
3879 | return len; | |
3880 | } | |
3881 | return -EINVAL; | |
3882 | } | |
3883 | ||
3884 | static struct md_sysfs_entry max_corr_read_errors = | |
3885 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, | |
3886 | max_corrected_read_errors_store); | |
3887 | ||
6d7ff738 | 3888 | static ssize_t |
fd01b88c | 3889 | null_show(struct mddev *mddev, char *page) |
6d7ff738 N |
3890 | { |
3891 | return -EINVAL; | |
3892 | } | |
3893 | ||
3894 | static ssize_t | |
fd01b88c | 3895 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
6d7ff738 N |
3896 | { |
3897 | /* buf must be %d:%d\n? giving major and minor numbers */ | |
3898 | /* The new device is added to the array. | |
3899 | * If the array has a persistent superblock, we read the | |
3900 | * superblock to initialise info and check validity. | |
3901 | * Otherwise, only checking done is that in bind_rdev_to_array, | |
3902 | * which mainly checks size. | |
3903 | */ | |
3904 | char *e; | |
3905 | int major = simple_strtoul(buf, &e, 10); | |
3906 | int minor; | |
3907 | dev_t dev; | |
3cb03002 | 3908 | struct md_rdev *rdev; |
6d7ff738 N |
3909 | int err; |
3910 | ||
3911 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') | |
3912 | return -EINVAL; | |
3913 | minor = simple_strtoul(e+1, &e, 10); | |
3914 | if (*e && *e != '\n') | |
3915 | return -EINVAL; | |
3916 | dev = MKDEV(major, minor); | |
3917 | if (major != MAJOR(dev) || | |
3918 | minor != MINOR(dev)) | |
3919 | return -EOVERFLOW; | |
3920 | ||
6791875e N |
3921 | flush_workqueue(md_misc_wq); |
3922 | ||
3923 | err = mddev_lock(mddev); | |
3924 | if (err) | |
3925 | return err; | |
6d7ff738 N |
3926 | if (mddev->persistent) { |
3927 | rdev = md_import_device(dev, mddev->major_version, | |
3928 | mddev->minor_version); | |
3929 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { | |
3cb03002 N |
3930 | struct md_rdev *rdev0 |
3931 | = list_entry(mddev->disks.next, | |
3932 | struct md_rdev, same_set); | |
6d7ff738 N |
3933 | err = super_types[mddev->major_version] |
3934 | .load_super(rdev, rdev0, mddev->minor_version); | |
3935 | if (err < 0) | |
3936 | goto out; | |
3937 | } | |
c5d79adb N |
3938 | } else if (mddev->external) |
3939 | rdev = md_import_device(dev, -2, -1); | |
3940 | else | |
6d7ff738 N |
3941 | rdev = md_import_device(dev, -1, -1); |
3942 | ||
3943 | if (IS_ERR(rdev)) | |
3944 | return PTR_ERR(rdev); | |
3945 | err = bind_rdev_to_array(rdev, mddev); | |
3946 | out: | |
3947 | if (err) | |
3948 | export_rdev(rdev); | |
6791875e | 3949 | mddev_unlock(mddev); |
6d7ff738 N |
3950 | return err ? err : len; |
3951 | } | |
3952 | ||
3953 | static struct md_sysfs_entry md_new_device = | |
80ca3a44 | 3954 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
3b34380a | 3955 | |
9b1d1dac | 3956 | static ssize_t |
fd01b88c | 3957 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
9b1d1dac PC |
3958 | { |
3959 | char *end; | |
3960 | unsigned long chunk, end_chunk; | |
6791875e | 3961 | int err; |
9b1d1dac | 3962 | |
6791875e N |
3963 | err = mddev_lock(mddev); |
3964 | if (err) | |
3965 | return err; | |
9b1d1dac PC |
3966 | if (!mddev->bitmap) |
3967 | goto out; | |
3968 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ | |
3969 | while (*buf) { | |
3970 | chunk = end_chunk = simple_strtoul(buf, &end, 0); | |
3971 | if (buf == end) break; | |
3972 | if (*end == '-') { /* range */ | |
3973 | buf = end + 1; | |
3974 | end_chunk = simple_strtoul(buf, &end, 0); | |
3975 | if (buf == end) break; | |
3976 | } | |
3977 | if (*end && !isspace(*end)) break; | |
3978 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); | |
e7d2860b | 3979 | buf = skip_spaces(end); |
9b1d1dac PC |
3980 | } |
3981 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ | |
3982 | out: | |
6791875e | 3983 | mddev_unlock(mddev); |
9b1d1dac PC |
3984 | return len; |
3985 | } | |
3986 | ||
3987 | static struct md_sysfs_entry md_bitmap = | |
3988 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); | |
3989 | ||
a35b0d69 | 3990 | static ssize_t |
fd01b88c | 3991 | size_show(struct mddev *mddev, char *page) |
a35b0d69 | 3992 | { |
58c0fed4 AN |
3993 | return sprintf(page, "%llu\n", |
3994 | (unsigned long long)mddev->dev_sectors / 2); | |
a35b0d69 N |
3995 | } |
3996 | ||
fd01b88c | 3997 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
a35b0d69 N |
3998 | |
3999 | static ssize_t | |
fd01b88c | 4000 | size_store(struct mddev *mddev, const char *buf, size_t len) |
a35b0d69 N |
4001 | { |
4002 | /* If array is inactive, we can reduce the component size, but | |
4003 | * not increase it (except from 0). | |
4004 | * If array is active, we can try an on-line resize | |
4005 | */ | |
b522adcd DW |
4006 | sector_t sectors; |
4007 | int err = strict_blocks_to_sectors(buf, §ors); | |
a35b0d69 | 4008 | |
58c0fed4 AN |
4009 | if (err < 0) |
4010 | return err; | |
6791875e N |
4011 | err = mddev_lock(mddev); |
4012 | if (err) | |
4013 | return err; | |
a35b0d69 | 4014 | if (mddev->pers) { |
293467aa GR |
4015 | if (mddev_is_clustered(mddev)) |
4016 | md_cluster_ops->metadata_update_start(mddev); | |
58c0fed4 | 4017 | err = update_size(mddev, sectors); |
850b2b42 | 4018 | md_update_sb(mddev, 1); |
293467aa GR |
4019 | if (mddev_is_clustered(mddev)) |
4020 | md_cluster_ops->metadata_update_finish(mddev); | |
a35b0d69 | 4021 | } else { |
58c0fed4 AN |
4022 | if (mddev->dev_sectors == 0 || |
4023 | mddev->dev_sectors > sectors) | |
4024 | mddev->dev_sectors = sectors; | |
a35b0d69 N |
4025 | else |
4026 | err = -ENOSPC; | |
4027 | } | |
6791875e | 4028 | mddev_unlock(mddev); |
a35b0d69 N |
4029 | return err ? err : len; |
4030 | } | |
4031 | ||
4032 | static struct md_sysfs_entry md_size = | |
80ca3a44 | 4033 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
a35b0d69 | 4034 | |
83f0d77a | 4035 | /* Metadata version. |
e691063a N |
4036 | * This is one of |
4037 | * 'none' for arrays with no metadata (good luck...) | |
4038 | * 'external' for arrays with externally managed metadata, | |
8bb93aac N |
4039 | * or N.M for internally known formats |
4040 | */ | |
4041 | static ssize_t | |
fd01b88c | 4042 | metadata_show(struct mddev *mddev, char *page) |
8bb93aac N |
4043 | { |
4044 | if (mddev->persistent) | |
4045 | return sprintf(page, "%d.%d\n", | |
4046 | mddev->major_version, mddev->minor_version); | |
e691063a N |
4047 | else if (mddev->external) |
4048 | return sprintf(page, "external:%s\n", mddev->metadata_type); | |
8bb93aac N |
4049 | else |
4050 | return sprintf(page, "none\n"); | |
4051 | } | |
4052 | ||
4053 | static ssize_t | |
fd01b88c | 4054 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
8bb93aac N |
4055 | { |
4056 | int major, minor; | |
4057 | char *e; | |
6791875e | 4058 | int err; |
ea43ddd8 N |
4059 | /* Changing the details of 'external' metadata is |
4060 | * always permitted. Otherwise there must be | |
4061 | * no devices attached to the array. | |
4062 | */ | |
6791875e N |
4063 | |
4064 | err = mddev_lock(mddev); | |
4065 | if (err) | |
4066 | return err; | |
4067 | err = -EBUSY; | |
ea43ddd8 N |
4068 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
4069 | ; | |
4070 | else if (!list_empty(&mddev->disks)) | |
6791875e | 4071 | goto out_unlock; |
8bb93aac | 4072 | |
6791875e | 4073 | err = 0; |
8bb93aac N |
4074 | if (cmd_match(buf, "none")) { |
4075 | mddev->persistent = 0; | |
e691063a N |
4076 | mddev->external = 0; |
4077 | mddev->major_version = 0; | |
4078 | mddev->minor_version = 90; | |
6791875e | 4079 | goto out_unlock; |
e691063a N |
4080 | } |
4081 | if (strncmp(buf, "external:", 9) == 0) { | |
20a49ff6 | 4082 | size_t namelen = len-9; |
e691063a N |
4083 | if (namelen >= sizeof(mddev->metadata_type)) |
4084 | namelen = sizeof(mddev->metadata_type)-1; | |
4085 | strncpy(mddev->metadata_type, buf+9, namelen); | |
4086 | mddev->metadata_type[namelen] = 0; | |
4087 | if (namelen && mddev->metadata_type[namelen-1] == '\n') | |
4088 | mddev->metadata_type[--namelen] = 0; | |
4089 | mddev->persistent = 0; | |
4090 | mddev->external = 1; | |
8bb93aac N |
4091 | mddev->major_version = 0; |
4092 | mddev->minor_version = 90; | |
6791875e | 4093 | goto out_unlock; |
8bb93aac N |
4094 | } |
4095 | major = simple_strtoul(buf, &e, 10); | |
6791875e | 4096 | err = -EINVAL; |
8bb93aac | 4097 | if (e==buf || *e != '.') |
6791875e | 4098 | goto out_unlock; |
8bb93aac N |
4099 | buf = e+1; |
4100 | minor = simple_strtoul(buf, &e, 10); | |
3f9d7b0d | 4101 | if (e==buf || (*e && *e != '\n') ) |
6791875e N |
4102 | goto out_unlock; |
4103 | err = -ENOENT; | |
50511da3 | 4104 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
6791875e | 4105 | goto out_unlock; |
8bb93aac N |
4106 | mddev->major_version = major; |
4107 | mddev->minor_version = minor; | |
4108 | mddev->persistent = 1; | |
e691063a | 4109 | mddev->external = 0; |
6791875e N |
4110 | err = 0; |
4111 | out_unlock: | |
4112 | mddev_unlock(mddev); | |
4113 | return err ?: len; | |
8bb93aac N |
4114 | } |
4115 | ||
4116 | static struct md_sysfs_entry md_metadata = | |
80ca3a44 | 4117 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
8bb93aac | 4118 | |
24dd469d | 4119 | static ssize_t |
fd01b88c | 4120 | action_show(struct mddev *mddev, char *page) |
24dd469d | 4121 | { |
7eec314d | 4122 | char *type = "idle"; |
b7b17c9b N |
4123 | unsigned long recovery = mddev->recovery; |
4124 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) | |
b6a9ce68 | 4125 | type = "frozen"; |
b7b17c9b N |
4126 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
4127 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { | |
4128 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) | |
ccfcc3c1 | 4129 | type = "reshape"; |
b7b17c9b N |
4130 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
4131 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) | |
24dd469d | 4132 | type = "resync"; |
b7b17c9b | 4133 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
24dd469d N |
4134 | type = "check"; |
4135 | else | |
4136 | type = "repair"; | |
b7b17c9b | 4137 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
24dd469d N |
4138 | type = "recover"; |
4139 | } | |
4140 | return sprintf(page, "%s\n", type); | |
4141 | } | |
4142 | ||
4143 | static ssize_t | |
fd01b88c | 4144 | action_store(struct mddev *mddev, const char *page, size_t len) |
24dd469d | 4145 | { |
7eec314d N |
4146 | if (!mddev->pers || !mddev->pers->sync_request) |
4147 | return -EINVAL; | |
4148 | ||
b6a9ce68 N |
4149 | if (cmd_match(page, "frozen")) |
4150 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
4151 | else | |
4152 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
4153 | ||
4154 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { | |
f851b60d | 4155 | flush_workqueue(md_misc_wq); |
7eec314d N |
4156 | if (mddev->sync_thread) { |
4157 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
6791875e N |
4158 | if (mddev_lock(mddev) == 0) { |
4159 | md_reap_sync_thread(mddev); | |
4160 | mddev_unlock(mddev); | |
4161 | } | |
7eec314d | 4162 | } |
03c902e1 N |
4163 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
4164 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) | |
24dd469d | 4165 | return -EBUSY; |
72a23c21 NB |
4166 | else if (cmd_match(page, "resync")) |
4167 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
4168 | else if (cmd_match(page, "recover")) { | |
4169 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
7eec314d | 4170 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
72a23c21 | 4171 | } else if (cmd_match(page, "reshape")) { |
16484bf5 N |
4172 | int err; |
4173 | if (mddev->pers->start_reshape == NULL) | |
4174 | return -EINVAL; | |
6791875e N |
4175 | err = mddev_lock(mddev); |
4176 | if (!err) { | |
4177 | err = mddev->pers->start_reshape(mddev); | |
4178 | mddev_unlock(mddev); | |
4179 | } | |
16484bf5 N |
4180 | if (err) |
4181 | return err; | |
a99ac971 | 4182 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
16484bf5 | 4183 | } else { |
bce74dac | 4184 | if (cmd_match(page, "check")) |
7eec314d | 4185 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
2adc7d47 | 4186 | else if (!cmd_match(page, "repair")) |
7eec314d N |
4187 | return -EINVAL; |
4188 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); | |
4189 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
7eec314d | 4190 | } |
48c26ddc N |
4191 | if (mddev->ro == 2) { |
4192 | /* A write to sync_action is enough to justify | |
4193 | * canceling read-auto mode | |
4194 | */ | |
4195 | mddev->ro = 0; | |
4196 | md_wakeup_thread(mddev->sync_thread); | |
4197 | } | |
03c902e1 | 4198 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
24dd469d | 4199 | md_wakeup_thread(mddev->thread); |
00bcb4ac | 4200 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
24dd469d N |
4201 | return len; |
4202 | } | |
4203 | ||
c4a39551 JB |
4204 | static struct md_sysfs_entry md_scan_mode = |
4205 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); | |
4206 | ||
4207 | static ssize_t | |
4208 | last_sync_action_show(struct mddev *mddev, char *page) | |
4209 | { | |
4210 | return sprintf(page, "%s\n", mddev->last_sync_action); | |
4211 | } | |
4212 | ||
4213 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); | |
4214 | ||
9d88883e | 4215 | static ssize_t |
fd01b88c | 4216 | mismatch_cnt_show(struct mddev *mddev, char *page) |
9d88883e N |
4217 | { |
4218 | return sprintf(page, "%llu\n", | |
7f7583d4 JM |
4219 | (unsigned long long) |
4220 | atomic64_read(&mddev->resync_mismatches)); | |
9d88883e N |
4221 | } |
4222 | ||
80ca3a44 | 4223 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
9d88883e | 4224 | |
88202a0c | 4225 | static ssize_t |
fd01b88c | 4226 | sync_min_show(struct mddev *mddev, char *page) |
88202a0c N |
4227 | { |
4228 | return sprintf(page, "%d (%s)\n", speed_min(mddev), | |
4229 | mddev->sync_speed_min ? "local": "system"); | |
4230 | } | |
4231 | ||
4232 | static ssize_t | |
fd01b88c | 4233 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
88202a0c N |
4234 | { |
4235 | int min; | |
4236 | char *e; | |
4237 | if (strncmp(buf, "system", 6)==0) { | |
4238 | mddev->sync_speed_min = 0; | |
4239 | return len; | |
4240 | } | |
4241 | min = simple_strtoul(buf, &e, 10); | |
4242 | if (buf == e || (*e && *e != '\n') || min <= 0) | |
4243 | return -EINVAL; | |
4244 | mddev->sync_speed_min = min; | |
4245 | return len; | |
4246 | } | |
4247 | ||
4248 | static struct md_sysfs_entry md_sync_min = | |
4249 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); | |
4250 | ||
4251 | static ssize_t | |
fd01b88c | 4252 | sync_max_show(struct mddev *mddev, char *page) |
88202a0c N |
4253 | { |
4254 | return sprintf(page, "%d (%s)\n", speed_max(mddev), | |
4255 | mddev->sync_speed_max ? "local": "system"); | |
4256 | } | |
4257 | ||
4258 | static ssize_t | |
fd01b88c | 4259 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
88202a0c N |
4260 | { |
4261 | int max; | |
4262 | char *e; | |
4263 | if (strncmp(buf, "system", 6)==0) { | |
4264 | mddev->sync_speed_max = 0; | |
4265 | return len; | |
4266 | } | |
4267 | max = simple_strtoul(buf, &e, 10); | |
4268 | if (buf == e || (*e && *e != '\n') || max <= 0) | |
4269 | return -EINVAL; | |
4270 | mddev->sync_speed_max = max; | |
4271 | return len; | |
4272 | } | |
4273 | ||
4274 | static struct md_sysfs_entry md_sync_max = | |
4275 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); | |
4276 | ||
d7f3d291 | 4277 | static ssize_t |
fd01b88c | 4278 | degraded_show(struct mddev *mddev, char *page) |
d7f3d291 IP |
4279 | { |
4280 | return sprintf(page, "%d\n", mddev->degraded); | |
4281 | } | |
4282 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); | |
88202a0c | 4283 | |
90b08710 | 4284 | static ssize_t |
fd01b88c | 4285 | sync_force_parallel_show(struct mddev *mddev, char *page) |
90b08710 BS |
4286 | { |
4287 | return sprintf(page, "%d\n", mddev->parallel_resync); | |
4288 | } | |
4289 | ||
4290 | static ssize_t | |
fd01b88c | 4291 | sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len) |
90b08710 BS |
4292 | { |
4293 | long n; | |
4294 | ||
b29bebd6 | 4295 | if (kstrtol(buf, 10, &n)) |
90b08710 BS |
4296 | return -EINVAL; |
4297 | ||
4298 | if (n != 0 && n != 1) | |
4299 | return -EINVAL; | |
4300 | ||
4301 | mddev->parallel_resync = n; | |
4302 | ||
4303 | if (mddev->sync_thread) | |
4304 | wake_up(&resync_wait); | |
4305 | ||
4306 | return len; | |
4307 | } | |
4308 | ||
4309 | /* force parallel resync, even with shared block devices */ | |
4310 | static struct md_sysfs_entry md_sync_force_parallel = | |
4311 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, | |
4312 | sync_force_parallel_show, sync_force_parallel_store); | |
4313 | ||
88202a0c | 4314 | static ssize_t |
fd01b88c | 4315 | sync_speed_show(struct mddev *mddev, char *page) |
88202a0c N |
4316 | { |
4317 | unsigned long resync, dt, db; | |
d1a7c503 N |
4318 | if (mddev->curr_resync == 0) |
4319 | return sprintf(page, "none\n"); | |
9687a60c AN |
4320 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
4321 | dt = (jiffies - mddev->resync_mark) / HZ; | |
88202a0c | 4322 | if (!dt) dt++; |
9687a60c AN |
4323 | db = resync - mddev->resync_mark_cnt; |
4324 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ | |
88202a0c N |
4325 | } |
4326 | ||
80ca3a44 | 4327 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
88202a0c N |
4328 | |
4329 | static ssize_t | |
fd01b88c | 4330 | sync_completed_show(struct mddev *mddev, char *page) |
88202a0c | 4331 | { |
13ae864b | 4332 | unsigned long long max_sectors, resync; |
88202a0c | 4333 | |
acb180b0 N |
4334 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
4335 | return sprintf(page, "none\n"); | |
4336 | ||
72f36d59 N |
4337 | if (mddev->curr_resync == 1 || |
4338 | mddev->curr_resync == 2) | |
4339 | return sprintf(page, "delayed\n"); | |
4340 | ||
c804cdec N |
4341 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
4342 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
58c0fed4 | 4343 | max_sectors = mddev->resync_max_sectors; |
88202a0c | 4344 | else |
58c0fed4 | 4345 | max_sectors = mddev->dev_sectors; |
88202a0c | 4346 | |
acb180b0 | 4347 | resync = mddev->curr_resync_completed; |
13ae864b | 4348 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
88202a0c N |
4349 | } |
4350 | ||
80ca3a44 | 4351 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
88202a0c | 4352 | |
5e96ee65 | 4353 | static ssize_t |
fd01b88c | 4354 | min_sync_show(struct mddev *mddev, char *page) |
5e96ee65 NB |
4355 | { |
4356 | return sprintf(page, "%llu\n", | |
4357 | (unsigned long long)mddev->resync_min); | |
4358 | } | |
4359 | static ssize_t | |
fd01b88c | 4360 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
5e96ee65 NB |
4361 | { |
4362 | unsigned long long min; | |
23da422b N |
4363 | int err; |
4364 | int chunk; | |
4365 | ||
b29bebd6 | 4366 | if (kstrtoull(buf, 10, &min)) |
5e96ee65 | 4367 | return -EINVAL; |
23da422b N |
4368 | |
4369 | spin_lock(&mddev->lock); | |
4370 | err = -EINVAL; | |
5e96ee65 | 4371 | if (min > mddev->resync_max) |
23da422b N |
4372 | goto out_unlock; |
4373 | ||
4374 | err = -EBUSY; | |
5e96ee65 | 4375 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
23da422b | 4376 | goto out_unlock; |
5e96ee65 NB |
4377 | |
4378 | /* Must be a multiple of chunk_size */ | |
23da422b N |
4379 | chunk = mddev->chunk_sectors; |
4380 | if (chunk) { | |
2ac06c33 | 4381 | sector_t temp = min; |
23da422b N |
4382 | |
4383 | err = -EINVAL; | |
4384 | if (sector_div(temp, chunk)) | |
4385 | goto out_unlock; | |
5e96ee65 NB |
4386 | } |
4387 | mddev->resync_min = min; | |
23da422b | 4388 | err = 0; |
5e96ee65 | 4389 | |
23da422b N |
4390 | out_unlock: |
4391 | spin_unlock(&mddev->lock); | |
4392 | return err ?: len; | |
5e96ee65 NB |
4393 | } |
4394 | ||
4395 | static struct md_sysfs_entry md_min_sync = | |
4396 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); | |
4397 | ||
c6207277 | 4398 | static ssize_t |
fd01b88c | 4399 | max_sync_show(struct mddev *mddev, char *page) |
c6207277 N |
4400 | { |
4401 | if (mddev->resync_max == MaxSector) | |
4402 | return sprintf(page, "max\n"); | |
4403 | else | |
4404 | return sprintf(page, "%llu\n", | |
4405 | (unsigned long long)mddev->resync_max); | |
4406 | } | |
4407 | static ssize_t | |
fd01b88c | 4408 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
c6207277 | 4409 | { |
23da422b N |
4410 | int err; |
4411 | spin_lock(&mddev->lock); | |
c6207277 N |
4412 | if (strncmp(buf, "max", 3) == 0) |
4413 | mddev->resync_max = MaxSector; | |
4414 | else { | |
5e96ee65 | 4415 | unsigned long long max; |
23da422b N |
4416 | int chunk; |
4417 | ||
4418 | err = -EINVAL; | |
b29bebd6 | 4419 | if (kstrtoull(buf, 10, &max)) |
23da422b | 4420 | goto out_unlock; |
5e96ee65 | 4421 | if (max < mddev->resync_min) |
23da422b N |
4422 | goto out_unlock; |
4423 | ||
4424 | err = -EBUSY; | |
c6207277 | 4425 | if (max < mddev->resync_max && |
4d484a4a | 4426 | mddev->ro == 0 && |
c6207277 | 4427 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
23da422b | 4428 | goto out_unlock; |
c6207277 N |
4429 | |
4430 | /* Must be a multiple of chunk_size */ | |
23da422b N |
4431 | chunk = mddev->chunk_sectors; |
4432 | if (chunk) { | |
2ac06c33 | 4433 | sector_t temp = max; |
23da422b N |
4434 | |
4435 | err = -EINVAL; | |
4436 | if (sector_div(temp, chunk)) | |
4437 | goto out_unlock; | |
c6207277 N |
4438 | } |
4439 | mddev->resync_max = max; | |
4440 | } | |
4441 | wake_up(&mddev->recovery_wait); | |
23da422b N |
4442 | err = 0; |
4443 | out_unlock: | |
4444 | spin_unlock(&mddev->lock); | |
4445 | return err ?: len; | |
c6207277 N |
4446 | } |
4447 | ||
4448 | static struct md_sysfs_entry md_max_sync = | |
4449 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); | |
4450 | ||
e464eafd | 4451 | static ssize_t |
fd01b88c | 4452 | suspend_lo_show(struct mddev *mddev, char *page) |
e464eafd N |
4453 | { |
4454 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); | |
4455 | } | |
4456 | ||
4457 | static ssize_t | |
fd01b88c | 4458 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
e464eafd N |
4459 | { |
4460 | char *e; | |
4461 | unsigned long long new = simple_strtoull(buf, &e, 10); | |
6791875e N |
4462 | unsigned long long old; |
4463 | int err; | |
e464eafd | 4464 | |
e464eafd N |
4465 | if (buf == e || (*e && *e != '\n')) |
4466 | return -EINVAL; | |
23ddff37 | 4467 | |
6791875e N |
4468 | err = mddev_lock(mddev); |
4469 | if (err) | |
4470 | return err; | |
4471 | err = -EINVAL; | |
4472 | if (mddev->pers == NULL || | |
4473 | mddev->pers->quiesce == NULL) | |
4474 | goto unlock; | |
4475 | old = mddev->suspend_lo; | |
23ddff37 N |
4476 | mddev->suspend_lo = new; |
4477 | if (new >= old) | |
4478 | /* Shrinking suspended region */ | |
e464eafd | 4479 | mddev->pers->quiesce(mddev, 2); |
23ddff37 N |
4480 | else { |
4481 | /* Expanding suspended region - need to wait */ | |
4482 | mddev->pers->quiesce(mddev, 1); | |
4483 | mddev->pers->quiesce(mddev, 0); | |
4484 | } | |
6791875e N |
4485 | err = 0; |
4486 | unlock: | |
4487 | mddev_unlock(mddev); | |
4488 | return err ?: len; | |
e464eafd N |
4489 | } |
4490 | static struct md_sysfs_entry md_suspend_lo = | |
4491 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); | |
4492 | ||
e464eafd | 4493 | static ssize_t |
fd01b88c | 4494 | suspend_hi_show(struct mddev *mddev, char *page) |
e464eafd N |
4495 | { |
4496 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); | |
4497 | } | |
4498 | ||
4499 | static ssize_t | |
fd01b88c | 4500 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
e464eafd N |
4501 | { |
4502 | char *e; | |
4503 | unsigned long long new = simple_strtoull(buf, &e, 10); | |
6791875e N |
4504 | unsigned long long old; |
4505 | int err; | |
e464eafd | 4506 | |
e464eafd N |
4507 | if (buf == e || (*e && *e != '\n')) |
4508 | return -EINVAL; | |
23ddff37 | 4509 | |
6791875e N |
4510 | err = mddev_lock(mddev); |
4511 | if (err) | |
4512 | return err; | |
4513 | err = -EINVAL; | |
4514 | if (mddev->pers == NULL || | |
4515 | mddev->pers->quiesce == NULL) | |
4516 | goto unlock; | |
4517 | old = mddev->suspend_hi; | |
23ddff37 N |
4518 | mddev->suspend_hi = new; |
4519 | if (new <= old) | |
4520 | /* Shrinking suspended region */ | |
4521 | mddev->pers->quiesce(mddev, 2); | |
4522 | else { | |
4523 | /* Expanding suspended region - need to wait */ | |
e464eafd N |
4524 | mddev->pers->quiesce(mddev, 1); |
4525 | mddev->pers->quiesce(mddev, 0); | |
23ddff37 | 4526 | } |
6791875e N |
4527 | err = 0; |
4528 | unlock: | |
4529 | mddev_unlock(mddev); | |
4530 | return err ?: len; | |
e464eafd N |
4531 | } |
4532 | static struct md_sysfs_entry md_suspend_hi = | |
4533 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); | |
4534 | ||
08a02ecd | 4535 | static ssize_t |
fd01b88c | 4536 | reshape_position_show(struct mddev *mddev, char *page) |
08a02ecd N |
4537 | { |
4538 | if (mddev->reshape_position != MaxSector) | |
4539 | return sprintf(page, "%llu\n", | |
4540 | (unsigned long long)mddev->reshape_position); | |
4541 | strcpy(page, "none\n"); | |
4542 | return 5; | |
4543 | } | |
4544 | ||
4545 | static ssize_t | |
fd01b88c | 4546 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
08a02ecd | 4547 | { |
c6563a8c | 4548 | struct md_rdev *rdev; |
08a02ecd | 4549 | char *e; |
6791875e | 4550 | int err; |
08a02ecd | 4551 | unsigned long long new = simple_strtoull(buf, &e, 10); |
6791875e | 4552 | |
08a02ecd N |
4553 | if (buf == e || (*e && *e != '\n')) |
4554 | return -EINVAL; | |
6791875e N |
4555 | err = mddev_lock(mddev); |
4556 | if (err) | |
4557 | return err; | |
4558 | err = -EBUSY; | |
4559 | if (mddev->pers) | |
4560 | goto unlock; | |
08a02ecd N |
4561 | mddev->reshape_position = new; |
4562 | mddev->delta_disks = 0; | |
2c810cdd | 4563 | mddev->reshape_backwards = 0; |
08a02ecd N |
4564 | mddev->new_level = mddev->level; |
4565 | mddev->new_layout = mddev->layout; | |
664e7c41 | 4566 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
c6563a8c N |
4567 | rdev_for_each(rdev, mddev) |
4568 | rdev->new_data_offset = rdev->data_offset; | |
6791875e N |
4569 | err = 0; |
4570 | unlock: | |
4571 | mddev_unlock(mddev); | |
4572 | return err ?: len; | |
08a02ecd N |
4573 | } |
4574 | ||
4575 | static struct md_sysfs_entry md_reshape_position = | |
4576 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, | |
4577 | reshape_position_store); | |
4578 | ||
2c810cdd N |
4579 | static ssize_t |
4580 | reshape_direction_show(struct mddev *mddev, char *page) | |
4581 | { | |
4582 | return sprintf(page, "%s\n", | |
4583 | mddev->reshape_backwards ? "backwards" : "forwards"); | |
4584 | } | |
4585 | ||
4586 | static ssize_t | |
4587 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) | |
4588 | { | |
4589 | int backwards = 0; | |
6791875e N |
4590 | int err; |
4591 | ||
2c810cdd N |
4592 | if (cmd_match(buf, "forwards")) |
4593 | backwards = 0; | |
4594 | else if (cmd_match(buf, "backwards")) | |
4595 | backwards = 1; | |
4596 | else | |
4597 | return -EINVAL; | |
4598 | if (mddev->reshape_backwards == backwards) | |
4599 | return len; | |
4600 | ||
6791875e N |
4601 | err = mddev_lock(mddev); |
4602 | if (err) | |
4603 | return err; | |
2c810cdd N |
4604 | /* check if we are allowed to change */ |
4605 | if (mddev->delta_disks) | |
6791875e N |
4606 | err = -EBUSY; |
4607 | else if (mddev->persistent && | |
2c810cdd | 4608 | mddev->major_version == 0) |
6791875e N |
4609 | err = -EINVAL; |
4610 | else | |
4611 | mddev->reshape_backwards = backwards; | |
4612 | mddev_unlock(mddev); | |
4613 | return err ?: len; | |
2c810cdd N |
4614 | } |
4615 | ||
4616 | static struct md_sysfs_entry md_reshape_direction = | |
4617 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, | |
4618 | reshape_direction_store); | |
4619 | ||
b522adcd | 4620 | static ssize_t |
fd01b88c | 4621 | array_size_show(struct mddev *mddev, char *page) |
b522adcd DW |
4622 | { |
4623 | if (mddev->external_size) | |
4624 | return sprintf(page, "%llu\n", | |
4625 | (unsigned long long)mddev->array_sectors/2); | |
4626 | else | |
4627 | return sprintf(page, "default\n"); | |
4628 | } | |
4629 | ||
4630 | static ssize_t | |
fd01b88c | 4631 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
b522adcd DW |
4632 | { |
4633 | sector_t sectors; | |
6791875e N |
4634 | int err; |
4635 | ||
4636 | err = mddev_lock(mddev); | |
4637 | if (err) | |
4638 | return err; | |
b522adcd DW |
4639 | |
4640 | if (strncmp(buf, "default", 7) == 0) { | |
4641 | if (mddev->pers) | |
4642 | sectors = mddev->pers->size(mddev, 0, 0); | |
4643 | else | |
4644 | sectors = mddev->array_sectors; | |
4645 | ||
4646 | mddev->external_size = 0; | |
4647 | } else { | |
4648 | if (strict_blocks_to_sectors(buf, §ors) < 0) | |
6791875e N |
4649 | err = -EINVAL; |
4650 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) | |
4651 | err = -E2BIG; | |
4652 | else | |
4653 | mddev->external_size = 1; | |
b522adcd DW |
4654 | } |
4655 | ||
6791875e N |
4656 | if (!err) { |
4657 | mddev->array_sectors = sectors; | |
4658 | if (mddev->pers) { | |
4659 | set_capacity(mddev->gendisk, mddev->array_sectors); | |
4660 | revalidate_disk(mddev->gendisk); | |
4661 | } | |
cbe6ef1d | 4662 | } |
6791875e N |
4663 | mddev_unlock(mddev); |
4664 | return err ?: len; | |
b522adcd DW |
4665 | } |
4666 | ||
4667 | static struct md_sysfs_entry md_array_size = | |
4668 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, | |
4669 | array_size_store); | |
e464eafd | 4670 | |
eae1701f N |
4671 | static struct attribute *md_default_attrs[] = { |
4672 | &md_level.attr, | |
d4dbd025 | 4673 | &md_layout.attr, |
eae1701f | 4674 | &md_raid_disks.attr, |
3b34380a | 4675 | &md_chunk_size.attr, |
a35b0d69 | 4676 | &md_size.attr, |
a94213b1 | 4677 | &md_resync_start.attr, |
8bb93aac | 4678 | &md_metadata.attr, |
6d7ff738 | 4679 | &md_new_device.attr, |
16f17b39 | 4680 | &md_safe_delay.attr, |
9e653b63 | 4681 | &md_array_state.attr, |
08a02ecd | 4682 | &md_reshape_position.attr, |
2c810cdd | 4683 | &md_reshape_direction.attr, |
b522adcd | 4684 | &md_array_size.attr, |
1e50915f | 4685 | &max_corr_read_errors.attr, |
411036fa N |
4686 | NULL, |
4687 | }; | |
4688 | ||
4689 | static struct attribute *md_redundancy_attrs[] = { | |
24dd469d | 4690 | &md_scan_mode.attr, |
c4a39551 | 4691 | &md_last_scan_mode.attr, |
9d88883e | 4692 | &md_mismatches.attr, |
88202a0c N |
4693 | &md_sync_min.attr, |
4694 | &md_sync_max.attr, | |
4695 | &md_sync_speed.attr, | |
90b08710 | 4696 | &md_sync_force_parallel.attr, |
88202a0c | 4697 | &md_sync_completed.attr, |
5e96ee65 | 4698 | &md_min_sync.attr, |
c6207277 | 4699 | &md_max_sync.attr, |
e464eafd N |
4700 | &md_suspend_lo.attr, |
4701 | &md_suspend_hi.attr, | |
9b1d1dac | 4702 | &md_bitmap.attr, |
d7f3d291 | 4703 | &md_degraded.attr, |
eae1701f N |
4704 | NULL, |
4705 | }; | |
411036fa N |
4706 | static struct attribute_group md_redundancy_group = { |
4707 | .name = NULL, | |
4708 | .attrs = md_redundancy_attrs, | |
4709 | }; | |
4710 | ||
eae1701f N |
4711 | static ssize_t |
4712 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
4713 | { | |
4714 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); | |
fd01b88c | 4715 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
96de1e66 | 4716 | ssize_t rv; |
eae1701f N |
4717 | |
4718 | if (!entry->show) | |
4719 | return -EIO; | |
af8a2434 N |
4720 | spin_lock(&all_mddevs_lock); |
4721 | if (list_empty(&mddev->all_mddevs)) { | |
4722 | spin_unlock(&all_mddevs_lock); | |
4723 | return -EBUSY; | |
4724 | } | |
4725 | mddev_get(mddev); | |
4726 | spin_unlock(&all_mddevs_lock); | |
4727 | ||
b7b17c9b | 4728 | rv = entry->show(mddev, page); |
af8a2434 | 4729 | mddev_put(mddev); |
96de1e66 | 4730 | return rv; |
eae1701f N |
4731 | } |
4732 | ||
4733 | static ssize_t | |
4734 | md_attr_store(struct kobject *kobj, struct attribute *attr, | |
4735 | const char *page, size_t length) | |
4736 | { | |
4737 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); | |
fd01b88c | 4738 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
96de1e66 | 4739 | ssize_t rv; |
eae1701f N |
4740 | |
4741 | if (!entry->store) | |
4742 | return -EIO; | |
67463acb N |
4743 | if (!capable(CAP_SYS_ADMIN)) |
4744 | return -EACCES; | |
af8a2434 N |
4745 | spin_lock(&all_mddevs_lock); |
4746 | if (list_empty(&mddev->all_mddevs)) { | |
4747 | spin_unlock(&all_mddevs_lock); | |
4748 | return -EBUSY; | |
4749 | } | |
4750 | mddev_get(mddev); | |
4751 | spin_unlock(&all_mddevs_lock); | |
6791875e | 4752 | rv = entry->store(mddev, page, length); |
af8a2434 | 4753 | mddev_put(mddev); |
96de1e66 | 4754 | return rv; |
eae1701f N |
4755 | } |
4756 | ||
4757 | static void md_free(struct kobject *ko) | |
4758 | { | |
fd01b88c | 4759 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
a21d1504 N |
4760 | |
4761 | if (mddev->sysfs_state) | |
4762 | sysfs_put(mddev->sysfs_state); | |
4763 | ||
4764 | if (mddev->gendisk) { | |
4765 | del_gendisk(mddev->gendisk); | |
4766 | put_disk(mddev->gendisk); | |
4767 | } | |
4768 | if (mddev->queue) | |
4769 | blk_cleanup_queue(mddev->queue); | |
4770 | ||
eae1701f N |
4771 | kfree(mddev); |
4772 | } | |
4773 | ||
52cf25d0 | 4774 | static const struct sysfs_ops md_sysfs_ops = { |
eae1701f N |
4775 | .show = md_attr_show, |
4776 | .store = md_attr_store, | |
4777 | }; | |
4778 | static struct kobj_type md_ktype = { | |
4779 | .release = md_free, | |
4780 | .sysfs_ops = &md_sysfs_ops, | |
4781 | .default_attrs = md_default_attrs, | |
4782 | }; | |
4783 | ||
1da177e4 LT |
4784 | int mdp_major = 0; |
4785 | ||
5fd3a17e DW |
4786 | static void mddev_delayed_delete(struct work_struct *ws) |
4787 | { | |
fd01b88c | 4788 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
5fd3a17e | 4789 | |
43a70507 | 4790 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
5fd3a17e DW |
4791 | kobject_del(&mddev->kobj); |
4792 | kobject_put(&mddev->kobj); | |
4793 | } | |
4794 | ||
efeb53c0 | 4795 | static int md_alloc(dev_t dev, char *name) |
1da177e4 | 4796 | { |
48c9c27b | 4797 | static DEFINE_MUTEX(disks_mutex); |
fd01b88c | 4798 | struct mddev *mddev = mddev_find(dev); |
1da177e4 | 4799 | struct gendisk *disk; |
efeb53c0 N |
4800 | int partitioned; |
4801 | int shift; | |
4802 | int unit; | |
3830c62f | 4803 | int error; |
1da177e4 LT |
4804 | |
4805 | if (!mddev) | |
efeb53c0 N |
4806 | return -ENODEV; |
4807 | ||
4808 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); | |
4809 | shift = partitioned ? MdpMinorShift : 0; | |
4810 | unit = MINOR(mddev->unit) >> shift; | |
1da177e4 | 4811 | |
e804ac78 TH |
4812 | /* wait for any previous instance of this device to be |
4813 | * completely removed (mddev_delayed_delete). | |
d3374825 | 4814 | */ |
e804ac78 | 4815 | flush_workqueue(md_misc_wq); |
d3374825 | 4816 | |
48c9c27b | 4817 | mutex_lock(&disks_mutex); |
0909dc44 N |
4818 | error = -EEXIST; |
4819 | if (mddev->gendisk) | |
4820 | goto abort; | |
efeb53c0 N |
4821 | |
4822 | if (name) { | |
4823 | /* Need to ensure that 'name' is not a duplicate. | |
4824 | */ | |
fd01b88c | 4825 | struct mddev *mddev2; |
efeb53c0 N |
4826 | spin_lock(&all_mddevs_lock); |
4827 | ||
4828 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) | |
4829 | if (mddev2->gendisk && | |
4830 | strcmp(mddev2->gendisk->disk_name, name) == 0) { | |
4831 | spin_unlock(&all_mddevs_lock); | |
0909dc44 | 4832 | goto abort; |
efeb53c0 N |
4833 | } |
4834 | spin_unlock(&all_mddevs_lock); | |
1da177e4 | 4835 | } |
8b765398 | 4836 | |
0909dc44 | 4837 | error = -ENOMEM; |
8b765398 | 4838 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
0909dc44 N |
4839 | if (!mddev->queue) |
4840 | goto abort; | |
409c57f3 N |
4841 | mddev->queue->queuedata = mddev; |
4842 | ||
409c57f3 | 4843 | blk_queue_make_request(mddev->queue, md_make_request); |
b1bd055d | 4844 | blk_set_stacking_limits(&mddev->queue->limits); |
8b765398 | 4845 | |
1da177e4 LT |
4846 | disk = alloc_disk(1 << shift); |
4847 | if (!disk) { | |
8b765398 N |
4848 | blk_cleanup_queue(mddev->queue); |
4849 | mddev->queue = NULL; | |
0909dc44 | 4850 | goto abort; |
1da177e4 | 4851 | } |
efeb53c0 | 4852 | disk->major = MAJOR(mddev->unit); |
1da177e4 | 4853 | disk->first_minor = unit << shift; |
efeb53c0 N |
4854 | if (name) |
4855 | strcpy(disk->disk_name, name); | |
4856 | else if (partitioned) | |
1da177e4 | 4857 | sprintf(disk->disk_name, "md_d%d", unit); |
ce7b0f46 | 4858 | else |
1da177e4 | 4859 | sprintf(disk->disk_name, "md%d", unit); |
1da177e4 LT |
4860 | disk->fops = &md_fops; |
4861 | disk->private_data = mddev; | |
4862 | disk->queue = mddev->queue; | |
b0140891 | 4863 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
92850bbd | 4864 | /* Allow extended partitions. This makes the |
d3374825 | 4865 | * 'mdp' device redundant, but we can't really |
92850bbd N |
4866 | * remove it now. |
4867 | */ | |
4868 | disk->flags |= GENHD_FL_EXT_DEVT; | |
1da177e4 | 4869 | mddev->gendisk = disk; |
b0140891 N |
4870 | /* As soon as we call add_disk(), another thread could get |
4871 | * through to md_open, so make sure it doesn't get too far | |
4872 | */ | |
4873 | mutex_lock(&mddev->open_mutex); | |
4874 | add_disk(disk); | |
4875 | ||
ed9e1982 TH |
4876 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
4877 | &disk_to_dev(disk)->kobj, "%s", "md"); | |
0909dc44 N |
4878 | if (error) { |
4879 | /* This isn't possible, but as kobject_init_and_add is marked | |
4880 | * __must_check, we must do something with the result | |
4881 | */ | |
5e55e2f5 N |
4882 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
4883 | disk->disk_name); | |
0909dc44 N |
4884 | error = 0; |
4885 | } | |
00bcb4ac N |
4886 | if (mddev->kobj.sd && |
4887 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) | |
43a70507 | 4888 | printk(KERN_DEBUG "pointless warning\n"); |
b0140891 | 4889 | mutex_unlock(&mddev->open_mutex); |
0909dc44 N |
4890 | abort: |
4891 | mutex_unlock(&disks_mutex); | |
00bcb4ac | 4892 | if (!error && mddev->kobj.sd) { |
3830c62f | 4893 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
00bcb4ac | 4894 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
b62b7590 | 4895 | } |
d3374825 | 4896 | mddev_put(mddev); |
0909dc44 | 4897 | return error; |
efeb53c0 N |
4898 | } |
4899 | ||
4900 | static struct kobject *md_probe(dev_t dev, int *part, void *data) | |
4901 | { | |
4902 | md_alloc(dev, NULL); | |
1da177e4 LT |
4903 | return NULL; |
4904 | } | |
4905 | ||
efeb53c0 N |
4906 | static int add_named_array(const char *val, struct kernel_param *kp) |
4907 | { | |
4908 | /* val must be "md_*" where * is not all digits. | |
4909 | * We allocate an array with a large free minor number, and | |
4910 | * set the name to val. val must not already be an active name. | |
4911 | */ | |
4912 | int len = strlen(val); | |
4913 | char buf[DISK_NAME_LEN]; | |
4914 | ||
4915 | while (len && val[len-1] == '\n') | |
4916 | len--; | |
4917 | if (len >= DISK_NAME_LEN) | |
4918 | return -E2BIG; | |
4919 | strlcpy(buf, val, len+1); | |
4920 | if (strncmp(buf, "md_", 3) != 0) | |
4921 | return -EINVAL; | |
4922 | return md_alloc(0, buf); | |
4923 | } | |
4924 | ||
1da177e4 LT |
4925 | static void md_safemode_timeout(unsigned long data) |
4926 | { | |
fd01b88c | 4927 | struct mddev *mddev = (struct mddev *) data; |
1da177e4 | 4928 | |
0fd62b86 NB |
4929 | if (!atomic_read(&mddev->writes_pending)) { |
4930 | mddev->safemode = 1; | |
4931 | if (mddev->external) | |
00bcb4ac | 4932 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0fd62b86 | 4933 | } |
1da177e4 LT |
4934 | md_wakeup_thread(mddev->thread); |
4935 | } | |
4936 | ||
6ff8d8ec | 4937 | static int start_dirty_degraded; |
1da177e4 | 4938 | |
fd01b88c | 4939 | int md_run(struct mddev *mddev) |
1da177e4 | 4940 | { |
2604b703 | 4941 | int err; |
3cb03002 | 4942 | struct md_rdev *rdev; |
84fc4b56 | 4943 | struct md_personality *pers; |
1da177e4 | 4944 | |
a757e64c N |
4945 | if (list_empty(&mddev->disks)) |
4946 | /* cannot run an array with no devices.. */ | |
1da177e4 | 4947 | return -EINVAL; |
1da177e4 LT |
4948 | |
4949 | if (mddev->pers) | |
4950 | return -EBUSY; | |
bb4f1e9d N |
4951 | /* Cannot run until previous stop completes properly */ |
4952 | if (mddev->sysfs_active) | |
4953 | return -EBUSY; | |
b6eb127d | 4954 | |
1da177e4 LT |
4955 | /* |
4956 | * Analyze all RAID superblock(s) | |
4957 | */ | |
1ec4a939 N |
4958 | if (!mddev->raid_disks) { |
4959 | if (!mddev->persistent) | |
4960 | return -EINVAL; | |
a757e64c | 4961 | analyze_sbs(mddev); |
1ec4a939 | 4962 | } |
1da177e4 | 4963 | |
d9d166c2 N |
4964 | if (mddev->level != LEVEL_NONE) |
4965 | request_module("md-level-%d", mddev->level); | |
4966 | else if (mddev->clevel[0]) | |
4967 | request_module("md-%s", mddev->clevel); | |
1da177e4 LT |
4968 | |
4969 | /* | |
4970 | * Drop all container device buffers, from now on | |
4971 | * the only valid external interface is through the md | |
4972 | * device. | |
1da177e4 | 4973 | */ |
dafb20fa | 4974 | rdev_for_each(rdev, mddev) { |
b2d444d7 | 4975 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 LT |
4976 | continue; |
4977 | sync_blockdev(rdev->bdev); | |
f98393a6 | 4978 | invalidate_bdev(rdev->bdev); |
f0d76d70 N |
4979 | |
4980 | /* perform some consistency tests on the device. | |
4981 | * We don't want the data to overlap the metadata, | |
58c0fed4 | 4982 | * Internal Bitmap issues have been handled elsewhere. |
f0d76d70 | 4983 | */ |
a6ff7e08 JB |
4984 | if (rdev->meta_bdev) { |
4985 | /* Nothing to check */; | |
4986 | } else if (rdev->data_offset < rdev->sb_start) { | |
58c0fed4 AN |
4987 | if (mddev->dev_sectors && |
4988 | rdev->data_offset + mddev->dev_sectors | |
0f420358 | 4989 | > rdev->sb_start) { |
f0d76d70 N |
4990 | printk("md: %s: data overlaps metadata\n", |
4991 | mdname(mddev)); | |
4992 | return -EINVAL; | |
4993 | } | |
4994 | } else { | |
0f420358 | 4995 | if (rdev->sb_start + rdev->sb_size/512 |
f0d76d70 N |
4996 | > rdev->data_offset) { |
4997 | printk("md: %s: metadata overlaps data\n", | |
4998 | mdname(mddev)); | |
4999 | return -EINVAL; | |
5000 | } | |
5001 | } | |
00bcb4ac | 5002 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
1da177e4 LT |
5003 | } |
5004 | ||
a167f663 | 5005 | if (mddev->bio_set == NULL) |
395c72a7 | 5006 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
a167f663 | 5007 | |
1da177e4 | 5008 | spin_lock(&pers_lock); |
d9d166c2 | 5009 | pers = find_pers(mddev->level, mddev->clevel); |
2604b703 | 5010 | if (!pers || !try_module_get(pers->owner)) { |
1da177e4 | 5011 | spin_unlock(&pers_lock); |
d9d166c2 N |
5012 | if (mddev->level != LEVEL_NONE) |
5013 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", | |
5014 | mddev->level); | |
5015 | else | |
5016 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", | |
5017 | mddev->clevel); | |
1da177e4 LT |
5018 | return -EINVAL; |
5019 | } | |
1da177e4 | 5020 | spin_unlock(&pers_lock); |
34817e8c N |
5021 | if (mddev->level != pers->level) { |
5022 | mddev->level = pers->level; | |
5023 | mddev->new_level = pers->level; | |
5024 | } | |
d9d166c2 | 5025 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
1da177e4 | 5026 | |
f6705578 | 5027 | if (mddev->reshape_position != MaxSector && |
63c70c4f | 5028 | pers->start_reshape == NULL) { |
f6705578 | 5029 | /* This personality cannot handle reshaping... */ |
f6705578 N |
5030 | module_put(pers->owner); |
5031 | return -EINVAL; | |
5032 | } | |
5033 | ||
7dd5e7c3 N |
5034 | if (pers->sync_request) { |
5035 | /* Warn if this is a potentially silly | |
5036 | * configuration. | |
5037 | */ | |
5038 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
3cb03002 | 5039 | struct md_rdev *rdev2; |
7dd5e7c3 | 5040 | int warned = 0; |
159ec1fc | 5041 | |
dafb20fa N |
5042 | rdev_for_each(rdev, mddev) |
5043 | rdev_for_each(rdev2, mddev) { | |
7dd5e7c3 N |
5044 | if (rdev < rdev2 && |
5045 | rdev->bdev->bd_contains == | |
5046 | rdev2->bdev->bd_contains) { | |
5047 | printk(KERN_WARNING | |
5048 | "%s: WARNING: %s appears to be" | |
5049 | " on the same physical disk as" | |
5050 | " %s.\n", | |
5051 | mdname(mddev), | |
5052 | bdevname(rdev->bdev,b), | |
5053 | bdevname(rdev2->bdev,b2)); | |
5054 | warned = 1; | |
5055 | } | |
5056 | } | |
159ec1fc | 5057 | |
7dd5e7c3 N |
5058 | if (warned) |
5059 | printk(KERN_WARNING | |
5060 | "True protection against single-disk" | |
5061 | " failure might be compromised.\n"); | |
5062 | } | |
5063 | ||
657390d2 | 5064 | mddev->recovery = 0; |
58c0fed4 AN |
5065 | /* may be over-ridden by personality */ |
5066 | mddev->resync_max_sectors = mddev->dev_sectors; | |
5067 | ||
6ff8d8ec | 5068 | mddev->ok_start_degraded = start_dirty_degraded; |
1da177e4 | 5069 | |
0f9552b5 | 5070 | if (start_readonly && mddev->ro == 0) |
f91de92e N |
5071 | mddev->ro = 2; /* read-only, but switch on first write */ |
5072 | ||
36d091f4 | 5073 | err = pers->run(mddev); |
13e53df3 AN |
5074 | if (err) |
5075 | printk(KERN_ERR "md: pers->run() failed ...\n"); | |
36d091f4 | 5076 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
b522adcd DW |
5077 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
5078 | " but 'external_size' not in effect?\n", __func__); | |
5079 | printk(KERN_ERR | |
5080 | "md: invalid array_size %llu > default size %llu\n", | |
5081 | (unsigned long long)mddev->array_sectors / 2, | |
36d091f4 | 5082 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
b522adcd | 5083 | err = -EINVAL; |
b522adcd | 5084 | } |
36d091f4 | 5085 | if (err == 0 && pers->sync_request && |
ef99bf48 | 5086 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
f9209a32 GR |
5087 | struct bitmap *bitmap; |
5088 | ||
5089 | bitmap = bitmap_create(mddev, -1); | |
5090 | if (IS_ERR(bitmap)) { | |
5091 | err = PTR_ERR(bitmap); | |
b15c2e57 N |
5092 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
5093 | mdname(mddev), err); | |
f9209a32 GR |
5094 | } else |
5095 | mddev->bitmap = bitmap; | |
5096 | ||
b15c2e57 | 5097 | } |
1da177e4 | 5098 | if (err) { |
5aa61f42 | 5099 | mddev_detach(mddev); |
36d091f4 N |
5100 | pers->free(mddev, mddev->private); |
5101 | module_put(pers->owner); | |
32a7627c N |
5102 | bitmap_destroy(mddev); |
5103 | return err; | |
1da177e4 | 5104 | } |
5c675f83 N |
5105 | if (mddev->queue) { |
5106 | mddev->queue->backing_dev_info.congested_data = mddev; | |
5107 | mddev->queue->backing_dev_info.congested_fn = md_congested; | |
64590f45 | 5108 | blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec); |
5c675f83 | 5109 | } |
36d091f4 | 5110 | if (pers->sync_request) { |
00bcb4ac N |
5111 | if (mddev->kobj.sd && |
5112 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) | |
5e55e2f5 N |
5113 | printk(KERN_WARNING |
5114 | "md: cannot register extra attributes for %s\n", | |
5115 | mdname(mddev)); | |
00bcb4ac | 5116 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
5e55e2f5 | 5117 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
fd9d49ca N |
5118 | mddev->ro = 0; |
5119 | ||
f72ffdd6 | 5120 | atomic_set(&mddev->writes_pending,0); |
1e50915f RB |
5121 | atomic_set(&mddev->max_corr_read_errors, |
5122 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); | |
1da177e4 LT |
5123 | mddev->safemode = 0; |
5124 | mddev->safemode_timer.function = md_safemode_timeout; | |
5125 | mddev->safemode_timer.data = (unsigned long) mddev; | |
16f17b39 | 5126 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
1da177e4 | 5127 | mddev->in_sync = 1; |
0ca69886 | 5128 | smp_wmb(); |
36d091f4 N |
5129 | spin_lock(&mddev->lock); |
5130 | mddev->pers = pers; | |
0ca69886 | 5131 | mddev->ready = 1; |
36d091f4 | 5132 | spin_unlock(&mddev->lock); |
dafb20fa | 5133 | rdev_for_each(rdev, mddev) |
36fad858 NK |
5134 | if (rdev->raid_disk >= 0) |
5135 | if (sysfs_link_rdev(mddev, rdev)) | |
00bcb4ac | 5136 | /* failure here is OK */; |
f72ffdd6 | 5137 | |
1da177e4 | 5138 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
f72ffdd6 | 5139 | |
7a0a5355 | 5140 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
850b2b42 | 5141 | md_update_sb(mddev, 0); |
1da177e4 | 5142 | |
d7603b7e | 5143 | md_new_event(mddev); |
00bcb4ac N |
5144 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
5145 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
a99ac971 | 5146 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
1da177e4 LT |
5147 | return 0; |
5148 | } | |
390ee602 | 5149 | EXPORT_SYMBOL_GPL(md_run); |
1da177e4 | 5150 | |
fd01b88c | 5151 | static int do_md_run(struct mddev *mddev) |
fe60b014 N |
5152 | { |
5153 | int err; | |
5154 | ||
5155 | err = md_run(mddev); | |
5156 | if (err) | |
5157 | goto out; | |
69e51b44 N |
5158 | err = bitmap_load(mddev); |
5159 | if (err) { | |
5160 | bitmap_destroy(mddev); | |
5161 | goto out; | |
5162 | } | |
0fd018af JB |
5163 | |
5164 | md_wakeup_thread(mddev->thread); | |
5165 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ | |
5166 | ||
fe60b014 N |
5167 | set_capacity(mddev->gendisk, mddev->array_sectors); |
5168 | revalidate_disk(mddev->gendisk); | |
f0b4f7e2 | 5169 | mddev->changed = 1; |
fe60b014 N |
5170 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
5171 | out: | |
5172 | return err; | |
5173 | } | |
5174 | ||
fd01b88c | 5175 | static int restart_array(struct mddev *mddev) |
1da177e4 LT |
5176 | { |
5177 | struct gendisk *disk = mddev->gendisk; | |
1da177e4 | 5178 | |
80fab1d7 | 5179 | /* Complain if it has no devices */ |
1da177e4 | 5180 | if (list_empty(&mddev->disks)) |
80fab1d7 AN |
5181 | return -ENXIO; |
5182 | if (!mddev->pers) | |
5183 | return -EINVAL; | |
5184 | if (!mddev->ro) | |
5185 | return -EBUSY; | |
5186 | mddev->safemode = 0; | |
5187 | mddev->ro = 0; | |
5188 | set_disk_ro(disk, 0); | |
5189 | printk(KERN_INFO "md: %s switched to read-write mode.\n", | |
5190 | mdname(mddev)); | |
5191 | /* Kick recovery or resync if necessary */ | |
5192 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
5193 | md_wakeup_thread(mddev->thread); | |
5194 | md_wakeup_thread(mddev->sync_thread); | |
00bcb4ac | 5195 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
80fab1d7 | 5196 | return 0; |
1da177e4 LT |
5197 | } |
5198 | ||
fd01b88c | 5199 | static void md_clean(struct mddev *mddev) |
6177b472 N |
5200 | { |
5201 | mddev->array_sectors = 0; | |
5202 | mddev->external_size = 0; | |
5203 | mddev->dev_sectors = 0; | |
5204 | mddev->raid_disks = 0; | |
5205 | mddev->recovery_cp = 0; | |
5206 | mddev->resync_min = 0; | |
5207 | mddev->resync_max = MaxSector; | |
5208 | mddev->reshape_position = MaxSector; | |
5209 | mddev->external = 0; | |
5210 | mddev->persistent = 0; | |
5211 | mddev->level = LEVEL_NONE; | |
5212 | mddev->clevel[0] = 0; | |
5213 | mddev->flags = 0; | |
5214 | mddev->ro = 0; | |
5215 | mddev->metadata_type[0] = 0; | |
5216 | mddev->chunk_sectors = 0; | |
5217 | mddev->ctime = mddev->utime = 0; | |
5218 | mddev->layout = 0; | |
5219 | mddev->max_disks = 0; | |
5220 | mddev->events = 0; | |
a8707c08 | 5221 | mddev->can_decrease_events = 0; |
6177b472 | 5222 | mddev->delta_disks = 0; |
2c810cdd | 5223 | mddev->reshape_backwards = 0; |
6177b472 N |
5224 | mddev->new_level = LEVEL_NONE; |
5225 | mddev->new_layout = 0; | |
5226 | mddev->new_chunk_sectors = 0; | |
5227 | mddev->curr_resync = 0; | |
7f7583d4 | 5228 | atomic64_set(&mddev->resync_mismatches, 0); |
6177b472 N |
5229 | mddev->suspend_lo = mddev->suspend_hi = 0; |
5230 | mddev->sync_speed_min = mddev->sync_speed_max = 0; | |
5231 | mddev->recovery = 0; | |
5232 | mddev->in_sync = 0; | |
f0b4f7e2 | 5233 | mddev->changed = 0; |
6177b472 | 5234 | mddev->degraded = 0; |
6177b472 | 5235 | mddev->safemode = 0; |
050b6615 | 5236 | mddev->merge_check_needed = 0; |
6177b472 N |
5237 | mddev->bitmap_info.offset = 0; |
5238 | mddev->bitmap_info.default_offset = 0; | |
6409bb05 | 5239 | mddev->bitmap_info.default_space = 0; |
6177b472 N |
5240 | mddev->bitmap_info.chunksize = 0; |
5241 | mddev->bitmap_info.daemon_sleep = 0; | |
5242 | mddev->bitmap_info.max_write_behind = 0; | |
5243 | } | |
5244 | ||
fd01b88c | 5245 | static void __md_stop_writes(struct mddev *mddev) |
a047e125 | 5246 | { |
293467aa GR |
5247 | if (mddev_is_clustered(mddev)) |
5248 | md_cluster_ops->metadata_update_start(mddev); | |
6b6204ee | 5249 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
f851b60d | 5250 | flush_workqueue(md_misc_wq); |
a047e125 | 5251 | if (mddev->sync_thread) { |
a047e125 | 5252 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
a91d5ac0 | 5253 | md_reap_sync_thread(mddev); |
a047e125 N |
5254 | } |
5255 | ||
5256 | del_timer_sync(&mddev->safemode_timer); | |
5257 | ||
5258 | bitmap_flush(mddev); | |
5259 | md_super_wait(mddev); | |
5260 | ||
b6d428c6 | 5261 | if (mddev->ro == 0 && |
7a0a5355 | 5262 | (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
a047e125 N |
5263 | /* mark array as shutdown cleanly */ |
5264 | mddev->in_sync = 1; | |
5265 | md_update_sb(mddev, 1); | |
5266 | } | |
293467aa GR |
5267 | if (mddev_is_clustered(mddev)) |
5268 | md_cluster_ops->metadata_update_finish(mddev); | |
a047e125 | 5269 | } |
defad61a | 5270 | |
fd01b88c | 5271 | void md_stop_writes(struct mddev *mddev) |
defad61a | 5272 | { |
29f097c4 | 5273 | mddev_lock_nointr(mddev); |
defad61a N |
5274 | __md_stop_writes(mddev); |
5275 | mddev_unlock(mddev); | |
5276 | } | |
390ee602 | 5277 | EXPORT_SYMBOL_GPL(md_stop_writes); |
a047e125 | 5278 | |
5aa61f42 N |
5279 | static void mddev_detach(struct mddev *mddev) |
5280 | { | |
5281 | struct bitmap *bitmap = mddev->bitmap; | |
5282 | /* wait for behind writes to complete */ | |
5283 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { | |
5284 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", | |
5285 | mdname(mddev)); | |
5286 | /* need to kick something here to make sure I/O goes? */ | |
5287 | wait_event(bitmap->behind_wait, | |
5288 | atomic_read(&bitmap->behind_writes) == 0); | |
5289 | } | |
36d091f4 | 5290 | if (mddev->pers && mddev->pers->quiesce) { |
5aa61f42 N |
5291 | mddev->pers->quiesce(mddev, 1); |
5292 | mddev->pers->quiesce(mddev, 0); | |
5293 | } | |
5294 | md_unregister_thread(&mddev->thread); | |
5295 | if (mddev->queue) | |
5296 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ | |
5297 | } | |
5298 | ||
5eff3c43 | 5299 | static void __md_stop(struct mddev *mddev) |
6177b472 | 5300 | { |
36d091f4 | 5301 | struct md_personality *pers = mddev->pers; |
5aa61f42 | 5302 | mddev_detach(mddev); |
36d091f4 N |
5303 | spin_lock(&mddev->lock); |
5304 | mddev->ready = 0; | |
6177b472 | 5305 | mddev->pers = NULL; |
36d091f4 N |
5306 | spin_unlock(&mddev->lock); |
5307 | pers->free(mddev, mddev->private); | |
5308 | if (pers->sync_request && mddev->to_remove == NULL) | |
5309 | mddev->to_remove = &md_redundancy_group; | |
5310 | module_put(pers->owner); | |
cca9cf90 | 5311 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
6177b472 | 5312 | } |
5eff3c43 N |
5313 | |
5314 | void md_stop(struct mddev *mddev) | |
5315 | { | |
5316 | /* stop the array and free an attached data structures. | |
5317 | * This is called from dm-raid | |
5318 | */ | |
5319 | __md_stop(mddev); | |
5320 | bitmap_destroy(mddev); | |
5321 | if (mddev->bio_set) | |
5322 | bioset_free(mddev->bio_set); | |
5323 | } | |
5324 | ||
390ee602 | 5325 | EXPORT_SYMBOL_GPL(md_stop); |
6177b472 | 5326 | |
a05b7ea0 | 5327 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
a4bd82d0 N |
5328 | { |
5329 | int err = 0; | |
30b8feb7 N |
5330 | int did_freeze = 0; |
5331 | ||
5332 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { | |
5333 | did_freeze = 1; | |
5334 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
5335 | md_wakeup_thread(mddev->thread); | |
5336 | } | |
f851b60d | 5337 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
30b8feb7 | 5338 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
f851b60d | 5339 | if (mddev->sync_thread) |
30b8feb7 N |
5340 | /* Thread might be blocked waiting for metadata update |
5341 | * which will now never happen */ | |
5342 | wake_up_process(mddev->sync_thread->tsk); | |
f851b60d | 5343 | |
30b8feb7 | 5344 | mddev_unlock(mddev); |
f851b60d N |
5345 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
5346 | &mddev->recovery)); | |
30b8feb7 N |
5347 | mddev_lock_nointr(mddev); |
5348 | ||
a4bd82d0 | 5349 | mutex_lock(&mddev->open_mutex); |
9ba3b7f5 | 5350 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
30b8feb7 | 5351 | mddev->sync_thread || |
f851b60d | 5352 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
30b8feb7 | 5353 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
a4bd82d0 | 5354 | printk("md: %s still in use.\n",mdname(mddev)); |
30b8feb7 N |
5355 | if (did_freeze) { |
5356 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
45eaf45d | 5357 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
30b8feb7 N |
5358 | md_wakeup_thread(mddev->thread); |
5359 | } | |
a4bd82d0 N |
5360 | err = -EBUSY; |
5361 | goto out; | |
5362 | } | |
5363 | if (mddev->pers) { | |
defad61a | 5364 | __md_stop_writes(mddev); |
a4bd82d0 N |
5365 | |
5366 | err = -ENXIO; | |
5367 | if (mddev->ro==1) | |
5368 | goto out; | |
5369 | mddev->ro = 1; | |
5370 | set_disk_ro(mddev->gendisk, 1); | |
5371 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
45eaf45d N |
5372 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
5373 | md_wakeup_thread(mddev->thread); | |
00bcb4ac | 5374 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
30b8feb7 | 5375 | err = 0; |
a4bd82d0 N |
5376 | } |
5377 | out: | |
5378 | mutex_unlock(&mddev->open_mutex); | |
5379 | return err; | |
5380 | } | |
5381 | ||
9e653b63 N |
5382 | /* mode: |
5383 | * 0 - completely stop and dis-assemble array | |
9e653b63 N |
5384 | * 2 - stop but do not disassemble array |
5385 | */ | |
f72ffdd6 | 5386 | static int do_md_stop(struct mddev *mddev, int mode, |
a05b7ea0 | 5387 | struct block_device *bdev) |
1da177e4 | 5388 | { |
1da177e4 | 5389 | struct gendisk *disk = mddev->gendisk; |
3cb03002 | 5390 | struct md_rdev *rdev; |
30b8feb7 N |
5391 | int did_freeze = 0; |
5392 | ||
5393 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { | |
5394 | did_freeze = 1; | |
5395 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
5396 | md_wakeup_thread(mddev->thread); | |
5397 | } | |
f851b60d | 5398 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
30b8feb7 | 5399 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
f851b60d | 5400 | if (mddev->sync_thread) |
30b8feb7 N |
5401 | /* Thread might be blocked waiting for metadata update |
5402 | * which will now never happen */ | |
5403 | wake_up_process(mddev->sync_thread->tsk); | |
f851b60d | 5404 | |
30b8feb7 | 5405 | mddev_unlock(mddev); |
f851b60d N |
5406 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
5407 | !test_bit(MD_RECOVERY_RUNNING, | |
5408 | &mddev->recovery))); | |
30b8feb7 | 5409 | mddev_lock_nointr(mddev); |
1da177e4 | 5410 | |
c8c00a69 | 5411 | mutex_lock(&mddev->open_mutex); |
9ba3b7f5 | 5412 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
30b8feb7 N |
5413 | mddev->sysfs_active || |
5414 | mddev->sync_thread || | |
f851b60d | 5415 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
30b8feb7 | 5416 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
df5b20cf | 5417 | printk("md: %s still in use.\n",mdname(mddev)); |
6e17b027 | 5418 | mutex_unlock(&mddev->open_mutex); |
30b8feb7 N |
5419 | if (did_freeze) { |
5420 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
45eaf45d | 5421 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
30b8feb7 N |
5422 | md_wakeup_thread(mddev->thread); |
5423 | } | |
260fa034 N |
5424 | return -EBUSY; |
5425 | } | |
6e17b027 | 5426 | if (mddev->pers) { |
a4bd82d0 N |
5427 | if (mddev->ro) |
5428 | set_disk_ro(disk, 0); | |
409c57f3 | 5429 | |
defad61a | 5430 | __md_stop_writes(mddev); |
5eff3c43 | 5431 | __md_stop(mddev); |
a4bd82d0 | 5432 | mddev->queue->merge_bvec_fn = NULL; |
a4bd82d0 | 5433 | mddev->queue->backing_dev_info.congested_fn = NULL; |
6177b472 | 5434 | |
a4bd82d0 | 5435 | /* tell userspace to handle 'inactive' */ |
00bcb4ac | 5436 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0d4ca600 | 5437 | |
dafb20fa | 5438 | rdev_for_each(rdev, mddev) |
36fad858 NK |
5439 | if (rdev->raid_disk >= 0) |
5440 | sysfs_unlink_rdev(mddev, rdev); | |
c4647292 | 5441 | |
a4bd82d0 | 5442 | set_capacity(disk, 0); |
6e17b027 | 5443 | mutex_unlock(&mddev->open_mutex); |
f0b4f7e2 | 5444 | mddev->changed = 1; |
a4bd82d0 | 5445 | revalidate_disk(disk); |
0d4ca600 | 5446 | |
a4bd82d0 N |
5447 | if (mddev->ro) |
5448 | mddev->ro = 0; | |
6e17b027 N |
5449 | } else |
5450 | mutex_unlock(&mddev->open_mutex); | |
1da177e4 LT |
5451 | /* |
5452 | * Free resources if final stop | |
5453 | */ | |
9e653b63 | 5454 | if (mode == 0) { |
1da177e4 LT |
5455 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
5456 | ||
978f946b | 5457 | bitmap_destroy(mddev); |
c3d9714e | 5458 | if (mddev->bitmap_info.file) { |
4af1a041 N |
5459 | struct file *f = mddev->bitmap_info.file; |
5460 | spin_lock(&mddev->lock); | |
c3d9714e | 5461 | mddev->bitmap_info.file = NULL; |
4af1a041 N |
5462 | spin_unlock(&mddev->lock); |
5463 | fput(f); | |
978f946b | 5464 | } |
c3d9714e | 5465 | mddev->bitmap_info.offset = 0; |
978f946b | 5466 | |
1da177e4 LT |
5467 | export_array(mddev); |
5468 | ||
6177b472 | 5469 | md_clean(mddev); |
934d9c23 | 5470 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
efeb53c0 N |
5471 | if (mddev->hold_active == UNTIL_STOP) |
5472 | mddev->hold_active = 0; | |
a4bd82d0 | 5473 | } |
3f9d99c1 | 5474 | blk_integrity_unregister(disk); |
d7603b7e | 5475 | md_new_event(mddev); |
00bcb4ac | 5476 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
6e17b027 | 5477 | return 0; |
1da177e4 LT |
5478 | } |
5479 | ||
fdee8ae4 | 5480 | #ifndef MODULE |
fd01b88c | 5481 | static void autorun_array(struct mddev *mddev) |
1da177e4 | 5482 | { |
3cb03002 | 5483 | struct md_rdev *rdev; |
1da177e4 LT |
5484 | int err; |
5485 | ||
a757e64c | 5486 | if (list_empty(&mddev->disks)) |
1da177e4 | 5487 | return; |
1da177e4 LT |
5488 | |
5489 | printk(KERN_INFO "md: running: "); | |
5490 | ||
dafb20fa | 5491 | rdev_for_each(rdev, mddev) { |
1da177e4 LT |
5492 | char b[BDEVNAME_SIZE]; |
5493 | printk("<%s>", bdevname(rdev->bdev,b)); | |
5494 | } | |
5495 | printk("\n"); | |
5496 | ||
d710e138 | 5497 | err = do_md_run(mddev); |
1da177e4 LT |
5498 | if (err) { |
5499 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); | |
a05b7ea0 | 5500 | do_md_stop(mddev, 0, NULL); |
1da177e4 LT |
5501 | } |
5502 | } | |
5503 | ||
5504 | /* | |
5505 | * lets try to run arrays based on all disks that have arrived | |
5506 | * until now. (those are in pending_raid_disks) | |
5507 | * | |
5508 | * the method: pick the first pending disk, collect all disks with | |
5509 | * the same UUID, remove all from the pending list and put them into | |
5510 | * the 'same_array' list. Then order this list based on superblock | |
5511 | * update time (freshest comes first), kick out 'old' disks and | |
5512 | * compare superblocks. If everything's fine then run it. | |
5513 | * | |
5514 | * If "unit" is allocated, then bump its reference count | |
5515 | */ | |
5516 | static void autorun_devices(int part) | |
5517 | { | |
3cb03002 | 5518 | struct md_rdev *rdev0, *rdev, *tmp; |
fd01b88c | 5519 | struct mddev *mddev; |
1da177e4 LT |
5520 | char b[BDEVNAME_SIZE]; |
5521 | ||
5522 | printk(KERN_INFO "md: autorun ...\n"); | |
5523 | while (!list_empty(&pending_raid_disks)) { | |
e8703fe1 | 5524 | int unit; |
1da177e4 | 5525 | dev_t dev; |
ad01c9e3 | 5526 | LIST_HEAD(candidates); |
1da177e4 | 5527 | rdev0 = list_entry(pending_raid_disks.next, |
3cb03002 | 5528 | struct md_rdev, same_set); |
1da177e4 LT |
5529 | |
5530 | printk(KERN_INFO "md: considering %s ...\n", | |
5531 | bdevname(rdev0->bdev,b)); | |
5532 | INIT_LIST_HEAD(&candidates); | |
159ec1fc | 5533 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
1da177e4 LT |
5534 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
5535 | printk(KERN_INFO "md: adding %s ...\n", | |
5536 | bdevname(rdev->bdev,b)); | |
5537 | list_move(&rdev->same_set, &candidates); | |
5538 | } | |
5539 | /* | |
5540 | * now we have a set of devices, with all of them having | |
5541 | * mostly sane superblocks. It's time to allocate the | |
5542 | * mddev. | |
5543 | */ | |
e8703fe1 N |
5544 | if (part) { |
5545 | dev = MKDEV(mdp_major, | |
5546 | rdev0->preferred_minor << MdpMinorShift); | |
5547 | unit = MINOR(dev) >> MdpMinorShift; | |
5548 | } else { | |
5549 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); | |
5550 | unit = MINOR(dev); | |
5551 | } | |
5552 | if (rdev0->preferred_minor != unit) { | |
1da177e4 LT |
5553 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
5554 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); | |
5555 | break; | |
5556 | } | |
1da177e4 LT |
5557 | |
5558 | md_probe(dev, NULL, NULL); | |
5559 | mddev = mddev_find(dev); | |
9bbbca3a NB |
5560 | if (!mddev || !mddev->gendisk) { |
5561 | if (mddev) | |
5562 | mddev_put(mddev); | |
5563 | printk(KERN_ERR | |
1da177e4 LT |
5564 | "md: cannot allocate memory for md drive.\n"); |
5565 | break; | |
5566 | } | |
f72ffdd6 | 5567 | if (mddev_lock(mddev)) |
1da177e4 LT |
5568 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
5569 | mdname(mddev)); | |
5570 | else if (mddev->raid_disks || mddev->major_version | |
5571 | || !list_empty(&mddev->disks)) { | |
f72ffdd6 | 5572 | printk(KERN_WARNING |
1da177e4 LT |
5573 | "md: %s already running, cannot run %s\n", |
5574 | mdname(mddev), bdevname(rdev0->bdev,b)); | |
5575 | mddev_unlock(mddev); | |
5576 | } else { | |
5577 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); | |
1ec4a939 | 5578 | mddev->persistent = 1; |
159ec1fc | 5579 | rdev_for_each_list(rdev, tmp, &candidates) { |
1da177e4 LT |
5580 | list_del_init(&rdev->same_set); |
5581 | if (bind_rdev_to_array(rdev, mddev)) | |
5582 | export_rdev(rdev); | |
5583 | } | |
5584 | autorun_array(mddev); | |
5585 | mddev_unlock(mddev); | |
5586 | } | |
5587 | /* on success, candidates will be empty, on error | |
5588 | * it won't... | |
5589 | */ | |
159ec1fc | 5590 | rdev_for_each_list(rdev, tmp, &candidates) { |
4b80991c | 5591 | list_del_init(&rdev->same_set); |
1da177e4 | 5592 | export_rdev(rdev); |
4b80991c | 5593 | } |
1da177e4 LT |
5594 | mddev_put(mddev); |
5595 | } | |
5596 | printk(KERN_INFO "md: ... autorun DONE.\n"); | |
5597 | } | |
fdee8ae4 | 5598 | #endif /* !MODULE */ |
1da177e4 | 5599 | |
f72ffdd6 | 5600 | static int get_version(void __user *arg) |
1da177e4 LT |
5601 | { |
5602 | mdu_version_t ver; | |
5603 | ||
5604 | ver.major = MD_MAJOR_VERSION; | |
5605 | ver.minor = MD_MINOR_VERSION; | |
5606 | ver.patchlevel = MD_PATCHLEVEL_VERSION; | |
5607 | ||
5608 | if (copy_to_user(arg, &ver, sizeof(ver))) | |
5609 | return -EFAULT; | |
5610 | ||
5611 | return 0; | |
5612 | } | |
5613 | ||
f72ffdd6 | 5614 | static int get_array_info(struct mddev *mddev, void __user *arg) |
1da177e4 LT |
5615 | { |
5616 | mdu_array_info_t info; | |
a9f326eb | 5617 | int nr,working,insync,failed,spare; |
3cb03002 | 5618 | struct md_rdev *rdev; |
1da177e4 | 5619 | |
1ca69c4b N |
5620 | nr = working = insync = failed = spare = 0; |
5621 | rcu_read_lock(); | |
5622 | rdev_for_each_rcu(rdev, mddev) { | |
1da177e4 | 5623 | nr++; |
b2d444d7 | 5624 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 LT |
5625 | failed++; |
5626 | else { | |
5627 | working++; | |
b2d444d7 | 5628 | if (test_bit(In_sync, &rdev->flags)) |
f72ffdd6 | 5629 | insync++; |
1da177e4 LT |
5630 | else |
5631 | spare++; | |
5632 | } | |
5633 | } | |
1ca69c4b | 5634 | rcu_read_unlock(); |
1da177e4 LT |
5635 | |
5636 | info.major_version = mddev->major_version; | |
5637 | info.minor_version = mddev->minor_version; | |
5638 | info.patch_version = MD_PATCHLEVEL_VERSION; | |
5639 | info.ctime = mddev->ctime; | |
5640 | info.level = mddev->level; | |
58c0fed4 AN |
5641 | info.size = mddev->dev_sectors / 2; |
5642 | if (info.size != mddev->dev_sectors / 2) /* overflow */ | |
284ae7ca | 5643 | info.size = -1; |
1da177e4 LT |
5644 | info.nr_disks = nr; |
5645 | info.raid_disks = mddev->raid_disks; | |
5646 | info.md_minor = mddev->md_minor; | |
5647 | info.not_persistent= !mddev->persistent; | |
5648 | ||
5649 | info.utime = mddev->utime; | |
5650 | info.state = 0; | |
5651 | if (mddev->in_sync) | |
5652 | info.state = (1<<MD_SB_CLEAN); | |
c3d9714e | 5653 | if (mddev->bitmap && mddev->bitmap_info.offset) |
9bd35920 | 5654 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
ca8895d9 GR |
5655 | if (mddev_is_clustered(mddev)) |
5656 | info.state |= (1<<MD_SB_CLUSTERED); | |
5657 | ||
a9f326eb | 5658 | info.active_disks = insync; |
1da177e4 LT |
5659 | info.working_disks = working; |
5660 | info.failed_disks = failed; | |
5661 | info.spare_disks = spare; | |
5662 | ||
5663 | info.layout = mddev->layout; | |
9d8f0363 | 5664 | info.chunk_size = mddev->chunk_sectors << 9; |
1da177e4 LT |
5665 | |
5666 | if (copy_to_user(arg, &info, sizeof(info))) | |
5667 | return -EFAULT; | |
5668 | ||
5669 | return 0; | |
5670 | } | |
5671 | ||
f72ffdd6 | 5672 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
32a7627c N |
5673 | { |
5674 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ | |
f4ad3d38 | 5675 | char *ptr; |
4af1a041 | 5676 | int err; |
32a7627c | 5677 | |
60559da4 | 5678 | file = kmalloc(sizeof(*file), GFP_NOIO); |
32a7627c | 5679 | if (!file) |
4af1a041 | 5680 | return -ENOMEM; |
32a7627c | 5681 | |
4af1a041 N |
5682 | err = 0; |
5683 | spin_lock(&mddev->lock); | |
32a7627c | 5684 | /* bitmap disabled, zero the first byte and copy out */ |
4af1a041 | 5685 | if (!mddev->bitmap_info.file) |
32a7627c | 5686 | file->pathname[0] = '\0'; |
4af1a041 N |
5687 | else if ((ptr = d_path(&mddev->bitmap_info.file->f_path, |
5688 | file->pathname, sizeof(file->pathname))), | |
5689 | IS_ERR(ptr)) | |
5690 | err = PTR_ERR(ptr); | |
5691 | else | |
5692 | memmove(file->pathname, ptr, | |
5693 | sizeof(file->pathname)-(ptr-file->pathname)); | |
5694 | spin_unlock(&mddev->lock); | |
32a7627c | 5695 | |
4af1a041 N |
5696 | if (err == 0 && |
5697 | copy_to_user(arg, file, sizeof(*file))) | |
32a7627c | 5698 | err = -EFAULT; |
4af1a041 | 5699 | |
32a7627c N |
5700 | kfree(file); |
5701 | return err; | |
5702 | } | |
5703 | ||
f72ffdd6 | 5704 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
1da177e4 LT |
5705 | { |
5706 | mdu_disk_info_t info; | |
3cb03002 | 5707 | struct md_rdev *rdev; |
1da177e4 LT |
5708 | |
5709 | if (copy_from_user(&info, arg, sizeof(info))) | |
5710 | return -EFAULT; | |
5711 | ||
1ca69c4b N |
5712 | rcu_read_lock(); |
5713 | rdev = find_rdev_nr_rcu(mddev, info.number); | |
1da177e4 LT |
5714 | if (rdev) { |
5715 | info.major = MAJOR(rdev->bdev->bd_dev); | |
5716 | info.minor = MINOR(rdev->bdev->bd_dev); | |
5717 | info.raid_disk = rdev->raid_disk; | |
5718 | info.state = 0; | |
b2d444d7 | 5719 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 | 5720 | info.state |= (1<<MD_DISK_FAULTY); |
b2d444d7 | 5721 | else if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 LT |
5722 | info.state |= (1<<MD_DISK_ACTIVE); |
5723 | info.state |= (1<<MD_DISK_SYNC); | |
5724 | } | |
8ddf9efe N |
5725 | if (test_bit(WriteMostly, &rdev->flags)) |
5726 | info.state |= (1<<MD_DISK_WRITEMOSTLY); | |
1da177e4 LT |
5727 | } else { |
5728 | info.major = info.minor = 0; | |
5729 | info.raid_disk = -1; | |
5730 | info.state = (1<<MD_DISK_REMOVED); | |
5731 | } | |
1ca69c4b | 5732 | rcu_read_unlock(); |
1da177e4 LT |
5733 | |
5734 | if (copy_to_user(arg, &info, sizeof(info))) | |
5735 | return -EFAULT; | |
5736 | ||
5737 | return 0; | |
5738 | } | |
5739 | ||
f72ffdd6 | 5740 | static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info) |
1da177e4 LT |
5741 | { |
5742 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
3cb03002 | 5743 | struct md_rdev *rdev; |
1da177e4 LT |
5744 | dev_t dev = MKDEV(info->major,info->minor); |
5745 | ||
5746 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) | |
5747 | return -EOVERFLOW; | |
5748 | ||
5749 | if (!mddev->raid_disks) { | |
5750 | int err; | |
5751 | /* expecting a device which has a superblock */ | |
5752 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); | |
5753 | if (IS_ERR(rdev)) { | |
f72ffdd6 | 5754 | printk(KERN_WARNING |
1da177e4 LT |
5755 | "md: md_import_device returned %ld\n", |
5756 | PTR_ERR(rdev)); | |
5757 | return PTR_ERR(rdev); | |
5758 | } | |
5759 | if (!list_empty(&mddev->disks)) { | |
3cb03002 N |
5760 | struct md_rdev *rdev0 |
5761 | = list_entry(mddev->disks.next, | |
5762 | struct md_rdev, same_set); | |
a9f326eb | 5763 | err = super_types[mddev->major_version] |
1da177e4 LT |
5764 | .load_super(rdev, rdev0, mddev->minor_version); |
5765 | if (err < 0) { | |
f72ffdd6 | 5766 | printk(KERN_WARNING |
1da177e4 | 5767 | "md: %s has different UUID to %s\n", |
f72ffdd6 | 5768 | bdevname(rdev->bdev,b), |
1da177e4 LT |
5769 | bdevname(rdev0->bdev,b2)); |
5770 | export_rdev(rdev); | |
5771 | return -EINVAL; | |
5772 | } | |
5773 | } | |
5774 | err = bind_rdev_to_array(rdev, mddev); | |
5775 | if (err) | |
5776 | export_rdev(rdev); | |
5777 | return err; | |
5778 | } | |
5779 | ||
5780 | /* | |
5781 | * add_new_disk can be used once the array is assembled | |
5782 | * to add "hot spares". They must already have a superblock | |
5783 | * written | |
5784 | */ | |
5785 | if (mddev->pers) { | |
5786 | int err; | |
5787 | if (!mddev->pers->hot_add_disk) { | |
f72ffdd6 | 5788 | printk(KERN_WARNING |
1da177e4 LT |
5789 | "%s: personality does not support diskops!\n", |
5790 | mdname(mddev)); | |
5791 | return -EINVAL; | |
5792 | } | |
7b1e35f6 N |
5793 | if (mddev->persistent) |
5794 | rdev = md_import_device(dev, mddev->major_version, | |
5795 | mddev->minor_version); | |
5796 | else | |
5797 | rdev = md_import_device(dev, -1, -1); | |
1da177e4 | 5798 | if (IS_ERR(rdev)) { |
f72ffdd6 | 5799 | printk(KERN_WARNING |
1da177e4 LT |
5800 | "md: md_import_device returned %ld\n", |
5801 | PTR_ERR(rdev)); | |
5802 | return PTR_ERR(rdev); | |
5803 | } | |
1a855a06 | 5804 | /* set saved_raid_disk if appropriate */ |
41158c7e N |
5805 | if (!mddev->persistent) { |
5806 | if (info->state & (1<<MD_DISK_SYNC) && | |
bf572541 | 5807 | info->raid_disk < mddev->raid_disks) { |
41158c7e | 5808 | rdev->raid_disk = info->raid_disk; |
bf572541 | 5809 | set_bit(In_sync, &rdev->flags); |
8313b8e5 | 5810 | clear_bit(Bitmap_sync, &rdev->flags); |
bf572541 | 5811 | } else |
41158c7e | 5812 | rdev->raid_disk = -1; |
f466722c | 5813 | rdev->saved_raid_disk = rdev->raid_disk; |
41158c7e N |
5814 | } else |
5815 | super_types[mddev->major_version]. | |
5816 | validate_super(mddev, rdev); | |
bedd86b7 | 5817 | if ((info->state & (1<<MD_DISK_SYNC)) && |
f4563091 | 5818 | rdev->raid_disk != info->raid_disk) { |
bedd86b7 N |
5819 | /* This was a hot-add request, but events doesn't |
5820 | * match, so reject it. | |
5821 | */ | |
5822 | export_rdev(rdev); | |
5823 | return -EINVAL; | |
5824 | } | |
5825 | ||
b2d444d7 | 5826 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
8ddf9efe N |
5827 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
5828 | set_bit(WriteMostly, &rdev->flags); | |
575a80fa N |
5829 | else |
5830 | clear_bit(WriteMostly, &rdev->flags); | |
8ddf9efe | 5831 | |
1da177e4 LT |
5832 | rdev->raid_disk = -1; |
5833 | err = bind_rdev_to_array(rdev, mddev); | |
7c7546cc N |
5834 | if (!err && !mddev->pers->hot_remove_disk) { |
5835 | /* If there is hot_add_disk but no hot_remove_disk | |
5836 | * then added disks for geometry changes, | |
5837 | * and should be added immediately. | |
5838 | */ | |
5839 | super_types[mddev->major_version]. | |
5840 | validate_super(mddev, rdev); | |
5841 | err = mddev->pers->hot_add_disk(mddev, rdev); | |
5842 | if (err) | |
5843 | unbind_rdev_from_array(rdev); | |
5844 | } | |
1da177e4 LT |
5845 | if (err) |
5846 | export_rdev(rdev); | |
52664732 | 5847 | else |
00bcb4ac | 5848 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
c361777f | 5849 | |
7ceb17e8 | 5850 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
72a23c21 NB |
5851 | if (mddev->degraded) |
5852 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
c361777f | 5853 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
9864c005 | 5854 | if (!err) |
5855 | md_new_event(mddev); | |
005eca5e | 5856 | md_wakeup_thread(mddev->thread); |
1da177e4 LT |
5857 | return err; |
5858 | } | |
5859 | ||
5860 | /* otherwise, add_new_disk is only allowed | |
5861 | * for major_version==0 superblocks | |
5862 | */ | |
5863 | if (mddev->major_version != 0) { | |
5864 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", | |
5865 | mdname(mddev)); | |
5866 | return -EINVAL; | |
5867 | } | |
5868 | ||
5869 | if (!(info->state & (1<<MD_DISK_FAULTY))) { | |
5870 | int err; | |
d710e138 | 5871 | rdev = md_import_device(dev, -1, 0); |
1da177e4 | 5872 | if (IS_ERR(rdev)) { |
f72ffdd6 | 5873 | printk(KERN_WARNING |
1da177e4 LT |
5874 | "md: error, md_import_device() returned %ld\n", |
5875 | PTR_ERR(rdev)); | |
5876 | return PTR_ERR(rdev); | |
5877 | } | |
5878 | rdev->desc_nr = info->number; | |
5879 | if (info->raid_disk < mddev->raid_disks) | |
5880 | rdev->raid_disk = info->raid_disk; | |
5881 | else | |
5882 | rdev->raid_disk = -1; | |
5883 | ||
1da177e4 | 5884 | if (rdev->raid_disk < mddev->raid_disks) |
b2d444d7 N |
5885 | if (info->state & (1<<MD_DISK_SYNC)) |
5886 | set_bit(In_sync, &rdev->flags); | |
1da177e4 | 5887 | |
8ddf9efe N |
5888 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
5889 | set_bit(WriteMostly, &rdev->flags); | |
5890 | ||
1da177e4 LT |
5891 | if (!mddev->persistent) { |
5892 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); | |
77304d2a MS |
5893 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
5894 | } else | |
57b2caa3 | 5895 | rdev->sb_start = calc_dev_sboffset(rdev); |
8190e754 | 5896 | rdev->sectors = rdev->sb_start; |
1da177e4 | 5897 | |
2bf071bf N |
5898 | err = bind_rdev_to_array(rdev, mddev); |
5899 | if (err) { | |
5900 | export_rdev(rdev); | |
5901 | return err; | |
5902 | } | |
1da177e4 LT |
5903 | } |
5904 | ||
5905 | return 0; | |
5906 | } | |
5907 | ||
f72ffdd6 | 5908 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
1da177e4 LT |
5909 | { |
5910 | char b[BDEVNAME_SIZE]; | |
3cb03002 | 5911 | struct md_rdev *rdev; |
1da177e4 | 5912 | |
1da177e4 LT |
5913 | rdev = find_rdev(mddev, dev); |
5914 | if (!rdev) | |
5915 | return -ENXIO; | |
5916 | ||
293467aa GR |
5917 | if (mddev_is_clustered(mddev)) |
5918 | md_cluster_ops->metadata_update_start(mddev); | |
5919 | ||
3ea8929d N |
5920 | clear_bit(Blocked, &rdev->flags); |
5921 | remove_and_add_spares(mddev, rdev); | |
5922 | ||
1da177e4 LT |
5923 | if (rdev->raid_disk >= 0) |
5924 | goto busy; | |
5925 | ||
5926 | kick_rdev_from_array(rdev); | |
850b2b42 | 5927 | md_update_sb(mddev, 1); |
d7603b7e | 5928 | md_new_event(mddev); |
1da177e4 | 5929 | |
293467aa GR |
5930 | if (mddev_is_clustered(mddev)) |
5931 | md_cluster_ops->metadata_update_finish(mddev); | |
5932 | ||
1da177e4 LT |
5933 | return 0; |
5934 | busy: | |
293467aa GR |
5935 | if (mddev_is_clustered(mddev)) |
5936 | md_cluster_ops->metadata_update_cancel(mddev); | |
fdefa4d8 | 5937 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
1da177e4 LT |
5938 | bdevname(rdev->bdev,b), mdname(mddev)); |
5939 | return -EBUSY; | |
5940 | } | |
5941 | ||
f72ffdd6 | 5942 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
1da177e4 LT |
5943 | { |
5944 | char b[BDEVNAME_SIZE]; | |
5945 | int err; | |
3cb03002 | 5946 | struct md_rdev *rdev; |
1da177e4 LT |
5947 | |
5948 | if (!mddev->pers) | |
5949 | return -ENODEV; | |
5950 | ||
5951 | if (mddev->major_version != 0) { | |
5952 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" | |
5953 | " version-0 superblocks.\n", | |
5954 | mdname(mddev)); | |
5955 | return -EINVAL; | |
5956 | } | |
5957 | if (!mddev->pers->hot_add_disk) { | |
f72ffdd6 | 5958 | printk(KERN_WARNING |
1da177e4 LT |
5959 | "%s: personality does not support diskops!\n", |
5960 | mdname(mddev)); | |
5961 | return -EINVAL; | |
5962 | } | |
5963 | ||
d710e138 | 5964 | rdev = md_import_device(dev, -1, 0); |
1da177e4 | 5965 | if (IS_ERR(rdev)) { |
f72ffdd6 | 5966 | printk(KERN_WARNING |
1da177e4 LT |
5967 | "md: error, md_import_device() returned %ld\n", |
5968 | PTR_ERR(rdev)); | |
5969 | return -EINVAL; | |
5970 | } | |
5971 | ||
5972 | if (mddev->persistent) | |
57b2caa3 | 5973 | rdev->sb_start = calc_dev_sboffset(rdev); |
1da177e4 | 5974 | else |
77304d2a | 5975 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
1da177e4 | 5976 | |
8190e754 | 5977 | rdev->sectors = rdev->sb_start; |
1da177e4 | 5978 | |
b2d444d7 | 5979 | if (test_bit(Faulty, &rdev->flags)) { |
f72ffdd6 | 5980 | printk(KERN_WARNING |
1da177e4 LT |
5981 | "md: can not hot-add faulty %s disk to %s!\n", |
5982 | bdevname(rdev->bdev,b), mdname(mddev)); | |
5983 | err = -EINVAL; | |
5984 | goto abort_export; | |
5985 | } | |
293467aa GR |
5986 | |
5987 | if (mddev_is_clustered(mddev)) | |
5988 | md_cluster_ops->metadata_update_start(mddev); | |
b2d444d7 | 5989 | clear_bit(In_sync, &rdev->flags); |
1da177e4 | 5990 | rdev->desc_nr = -1; |
5842730d | 5991 | rdev->saved_raid_disk = -1; |
2bf071bf N |
5992 | err = bind_rdev_to_array(rdev, mddev); |
5993 | if (err) | |
293467aa | 5994 | goto abort_clustered; |
1da177e4 LT |
5995 | |
5996 | /* | |
5997 | * The rest should better be atomic, we can have disk failures | |
5998 | * noticed in interrupt contexts ... | |
5999 | */ | |
6000 | ||
1da177e4 LT |
6001 | rdev->raid_disk = -1; |
6002 | ||
850b2b42 | 6003 | md_update_sb(mddev, 1); |
1da177e4 | 6004 | |
293467aa GR |
6005 | if (mddev_is_clustered(mddev)) |
6006 | md_cluster_ops->metadata_update_finish(mddev); | |
1da177e4 LT |
6007 | /* |
6008 | * Kick recovery, maybe this spare has to be added to the | |
6009 | * array immediately. | |
6010 | */ | |
6011 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
6012 | md_wakeup_thread(mddev->thread); | |
d7603b7e | 6013 | md_new_event(mddev); |
1da177e4 LT |
6014 | return 0; |
6015 | ||
293467aa GR |
6016 | abort_clustered: |
6017 | if (mddev_is_clustered(mddev)) | |
6018 | md_cluster_ops->metadata_update_cancel(mddev); | |
1da177e4 LT |
6019 | abort_export: |
6020 | export_rdev(rdev); | |
6021 | return err; | |
6022 | } | |
6023 | ||
fd01b88c | 6024 | static int set_bitmap_file(struct mddev *mddev, int fd) |
32a7627c | 6025 | { |
035328c2 | 6026 | int err = 0; |
32a7627c | 6027 | |
36fa3063 | 6028 | if (mddev->pers) { |
d66b1b39 | 6029 | if (!mddev->pers->quiesce || !mddev->thread) |
36fa3063 N |
6030 | return -EBUSY; |
6031 | if (mddev->recovery || mddev->sync_thread) | |
6032 | return -EBUSY; | |
6033 | /* we should be able to change the bitmap.. */ | |
6034 | } | |
32a7627c | 6035 | |
36fa3063 | 6036 | if (fd >= 0) { |
035328c2 | 6037 | struct inode *inode; |
1e594bb2 N |
6038 | struct file *f; |
6039 | ||
6040 | if (mddev->bitmap || mddev->bitmap_info.file) | |
36fa3063 | 6041 | return -EEXIST; /* cannot add when bitmap is present */ |
1e594bb2 | 6042 | f = fget(fd); |
32a7627c | 6043 | |
1e594bb2 | 6044 | if (f == NULL) { |
36fa3063 N |
6045 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
6046 | mdname(mddev)); | |
6047 | return -EBADF; | |
6048 | } | |
6049 | ||
1e594bb2 | 6050 | inode = f->f_mapping->host; |
035328c2 N |
6051 | if (!S_ISREG(inode->i_mode)) { |
6052 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", | |
6053 | mdname(mddev)); | |
6054 | err = -EBADF; | |
1e594bb2 | 6055 | } else if (!(f->f_mode & FMODE_WRITE)) { |
035328c2 N |
6056 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
6057 | mdname(mddev)); | |
6058 | err = -EBADF; | |
6059 | } else if (atomic_read(&inode->i_writecount) != 1) { | |
36fa3063 N |
6060 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
6061 | mdname(mddev)); | |
035328c2 N |
6062 | err = -EBUSY; |
6063 | } | |
6064 | if (err) { | |
1e594bb2 | 6065 | fput(f); |
36fa3063 N |
6066 | return err; |
6067 | } | |
1e594bb2 | 6068 | mddev->bitmap_info.file = f; |
c3d9714e | 6069 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
36fa3063 N |
6070 | } else if (mddev->bitmap == NULL) |
6071 | return -ENOENT; /* cannot remove what isn't there */ | |
6072 | err = 0; | |
6073 | if (mddev->pers) { | |
6074 | mddev->pers->quiesce(mddev, 1); | |
69e51b44 | 6075 | if (fd >= 0) { |
f9209a32 GR |
6076 | struct bitmap *bitmap; |
6077 | ||
6078 | bitmap = bitmap_create(mddev, -1); | |
6079 | if (!IS_ERR(bitmap)) { | |
6080 | mddev->bitmap = bitmap; | |
69e51b44 | 6081 | err = bitmap_load(mddev); |
f9209a32 | 6082 | } |
69e51b44 | 6083 | } |
d7375ab3 | 6084 | if (fd < 0 || err) { |
36fa3063 | 6085 | bitmap_destroy(mddev); |
d7375ab3 N |
6086 | fd = -1; /* make sure to put the file */ |
6087 | } | |
36fa3063 | 6088 | mddev->pers->quiesce(mddev, 0); |
d7375ab3 N |
6089 | } |
6090 | if (fd < 0) { | |
4af1a041 N |
6091 | struct file *f = mddev->bitmap_info.file; |
6092 | if (f) { | |
6093 | spin_lock(&mddev->lock); | |
6094 | mddev->bitmap_info.file = NULL; | |
6095 | spin_unlock(&mddev->lock); | |
6096 | fput(f); | |
6097 | } | |
36fa3063 N |
6098 | } |
6099 | ||
32a7627c N |
6100 | return err; |
6101 | } | |
6102 | ||
1da177e4 LT |
6103 | /* |
6104 | * set_array_info is used two different ways | |
6105 | * The original usage is when creating a new array. | |
6106 | * In this usage, raid_disks is > 0 and it together with | |
6107 | * level, size, not_persistent,layout,chunksize determine the | |
6108 | * shape of the array. | |
6109 | * This will always create an array with a type-0.90.0 superblock. | |
6110 | * The newer usage is when assembling an array. | |
6111 | * In this case raid_disks will be 0, and the major_version field is | |
6112 | * use to determine which style super-blocks are to be found on the devices. | |
6113 | * The minor and patch _version numbers are also kept incase the | |
6114 | * super_block handler wishes to interpret them. | |
6115 | */ | |
f72ffdd6 | 6116 | static int set_array_info(struct mddev *mddev, mdu_array_info_t *info) |
1da177e4 LT |
6117 | { |
6118 | ||
6119 | if (info->raid_disks == 0) { | |
6120 | /* just setting version number for superblock loading */ | |
6121 | if (info->major_version < 0 || | |
50511da3 | 6122 | info->major_version >= ARRAY_SIZE(super_types) || |
1da177e4 LT |
6123 | super_types[info->major_version].name == NULL) { |
6124 | /* maybe try to auto-load a module? */ | |
f72ffdd6 | 6125 | printk(KERN_INFO |
1da177e4 LT |
6126 | "md: superblock version %d not known\n", |
6127 | info->major_version); | |
6128 | return -EINVAL; | |
6129 | } | |
6130 | mddev->major_version = info->major_version; | |
6131 | mddev->minor_version = info->minor_version; | |
6132 | mddev->patch_version = info->patch_version; | |
3f9d7b0d | 6133 | mddev->persistent = !info->not_persistent; |
cbd19983 N |
6134 | /* ensure mddev_put doesn't delete this now that there |
6135 | * is some minimal configuration. | |
6136 | */ | |
6137 | mddev->ctime = get_seconds(); | |
1da177e4 LT |
6138 | return 0; |
6139 | } | |
6140 | mddev->major_version = MD_MAJOR_VERSION; | |
6141 | mddev->minor_version = MD_MINOR_VERSION; | |
6142 | mddev->patch_version = MD_PATCHLEVEL_VERSION; | |
6143 | mddev->ctime = get_seconds(); | |
6144 | ||
6145 | mddev->level = info->level; | |
17115e03 | 6146 | mddev->clevel[0] = 0; |
58c0fed4 | 6147 | mddev->dev_sectors = 2 * (sector_t)info->size; |
1da177e4 LT |
6148 | mddev->raid_disks = info->raid_disks; |
6149 | /* don't set md_minor, it is determined by which /dev/md* was | |
6150 | * openned | |
6151 | */ | |
6152 | if (info->state & (1<<MD_SB_CLEAN)) | |
6153 | mddev->recovery_cp = MaxSector; | |
6154 | else | |
6155 | mddev->recovery_cp = 0; | |
6156 | mddev->persistent = ! info->not_persistent; | |
e691063a | 6157 | mddev->external = 0; |
1da177e4 LT |
6158 | |
6159 | mddev->layout = info->layout; | |
9d8f0363 | 6160 | mddev->chunk_sectors = info->chunk_size >> 9; |
1da177e4 LT |
6161 | |
6162 | mddev->max_disks = MD_SB_DISKS; | |
6163 | ||
e691063a N |
6164 | if (mddev->persistent) |
6165 | mddev->flags = 0; | |
850b2b42 | 6166 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
1da177e4 | 6167 | |
c3d9714e | 6168 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
6409bb05 | 6169 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
c3d9714e | 6170 | mddev->bitmap_info.offset = 0; |
b2a2703c | 6171 | |
f6705578 N |
6172 | mddev->reshape_position = MaxSector; |
6173 | ||
1da177e4 LT |
6174 | /* |
6175 | * Generate a 128 bit UUID | |
6176 | */ | |
6177 | get_random_bytes(mddev->uuid, 16); | |
6178 | ||
f6705578 | 6179 | mddev->new_level = mddev->level; |
664e7c41 | 6180 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
6181 | mddev->new_layout = mddev->layout; |
6182 | mddev->delta_disks = 0; | |
2c810cdd | 6183 | mddev->reshape_backwards = 0; |
f6705578 | 6184 | |
1da177e4 LT |
6185 | return 0; |
6186 | } | |
6187 | ||
fd01b88c | 6188 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
1f403624 | 6189 | { |
b522adcd DW |
6190 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
6191 | ||
6192 | if (mddev->external_size) | |
6193 | return; | |
6194 | ||
1f403624 DW |
6195 | mddev->array_sectors = array_sectors; |
6196 | } | |
6197 | EXPORT_SYMBOL(md_set_array_sectors); | |
6198 | ||
fd01b88c | 6199 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
a35b0d69 | 6200 | { |
3cb03002 | 6201 | struct md_rdev *rdev; |
a35b0d69 | 6202 | int rv; |
d71f9f88 | 6203 | int fit = (num_sectors == 0); |
a35b0d69 N |
6204 | |
6205 | if (mddev->pers->resize == NULL) | |
6206 | return -EINVAL; | |
d71f9f88 AN |
6207 | /* The "num_sectors" is the number of sectors of each device that |
6208 | * is used. This can only make sense for arrays with redundancy. | |
6209 | * linear and raid0 always use whatever space is available. We can only | |
6210 | * consider changing this number if no resync or reconstruction is | |
6211 | * happening, and if the new size is acceptable. It must fit before the | |
0f420358 | 6212 | * sb_start or, if that is <data_offset, it must fit before the size |
d71f9f88 AN |
6213 | * of each device. If num_sectors is zero, we find the largest size |
6214 | * that fits. | |
a35b0d69 | 6215 | */ |
f851b60d N |
6216 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
6217 | mddev->sync_thread) | |
a35b0d69 | 6218 | return -EBUSY; |
bd8839e0 N |
6219 | if (mddev->ro) |
6220 | return -EROFS; | |
a4a6125a | 6221 | |
dafb20fa | 6222 | rdev_for_each(rdev, mddev) { |
dd8ac336 | 6223 | sector_t avail = rdev->sectors; |
01ab5662 | 6224 | |
d71f9f88 AN |
6225 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
6226 | num_sectors = avail; | |
6227 | if (avail < num_sectors) | |
a35b0d69 N |
6228 | return -ENOSPC; |
6229 | } | |
d71f9f88 | 6230 | rv = mddev->pers->resize(mddev, num_sectors); |
449aad3e N |
6231 | if (!rv) |
6232 | revalidate_disk(mddev->gendisk); | |
a35b0d69 N |
6233 | return rv; |
6234 | } | |
6235 | ||
fd01b88c | 6236 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
da943b99 N |
6237 | { |
6238 | int rv; | |
c6563a8c | 6239 | struct md_rdev *rdev; |
da943b99 | 6240 | /* change the number of raid disks */ |
63c70c4f | 6241 | if (mddev->pers->check_reshape == NULL) |
da943b99 | 6242 | return -EINVAL; |
bd8839e0 N |
6243 | if (mddev->ro) |
6244 | return -EROFS; | |
da943b99 | 6245 | if (raid_disks <= 0 || |
233fca36 | 6246 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
da943b99 | 6247 | return -EINVAL; |
f851b60d N |
6248 | if (mddev->sync_thread || |
6249 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || | |
6250 | mddev->reshape_position != MaxSector) | |
da943b99 | 6251 | return -EBUSY; |
c6563a8c N |
6252 | |
6253 | rdev_for_each(rdev, mddev) { | |
6254 | if (mddev->raid_disks < raid_disks && | |
6255 | rdev->data_offset < rdev->new_data_offset) | |
6256 | return -EINVAL; | |
6257 | if (mddev->raid_disks > raid_disks && | |
6258 | rdev->data_offset > rdev->new_data_offset) | |
6259 | return -EINVAL; | |
6260 | } | |
6261 | ||
63c70c4f | 6262 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
2c810cdd N |
6263 | if (mddev->delta_disks < 0) |
6264 | mddev->reshape_backwards = 1; | |
6265 | else if (mddev->delta_disks > 0) | |
6266 | mddev->reshape_backwards = 0; | |
63c70c4f N |
6267 | |
6268 | rv = mddev->pers->check_reshape(mddev); | |
2c810cdd | 6269 | if (rv < 0) { |
de171cb9 | 6270 | mddev->delta_disks = 0; |
2c810cdd N |
6271 | mddev->reshape_backwards = 0; |
6272 | } | |
da943b99 N |
6273 | return rv; |
6274 | } | |
6275 | ||
1da177e4 LT |
6276 | /* |
6277 | * update_array_info is used to change the configuration of an | |
6278 | * on-line array. | |
6279 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size | |
6280 | * fields in the info are checked against the array. | |
6281 | * Any differences that cannot be handled will cause an error. | |
6282 | * Normally, only one change can be managed at a time. | |
6283 | */ | |
fd01b88c | 6284 | static int update_array_info(struct mddev *mddev, mdu_array_info_t *info) |
1da177e4 LT |
6285 | { |
6286 | int rv = 0; | |
6287 | int cnt = 0; | |
36fa3063 N |
6288 | int state = 0; |
6289 | ||
6290 | /* calculate expected state,ignoring low bits */ | |
c3d9714e | 6291 | if (mddev->bitmap && mddev->bitmap_info.offset) |
36fa3063 | 6292 | state |= (1 << MD_SB_BITMAP_PRESENT); |
1da177e4 LT |
6293 | |
6294 | if (mddev->major_version != info->major_version || | |
6295 | mddev->minor_version != info->minor_version || | |
6296 | /* mddev->patch_version != info->patch_version || */ | |
6297 | mddev->ctime != info->ctime || | |
6298 | mddev->level != info->level || | |
6299 | /* mddev->layout != info->layout || */ | |
6300 | !mddev->persistent != info->not_persistent|| | |
9d8f0363 | 6301 | mddev->chunk_sectors != info->chunk_size >> 9 || |
36fa3063 N |
6302 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
6303 | ((state^info->state) & 0xfffffe00) | |
6304 | ) | |
1da177e4 LT |
6305 | return -EINVAL; |
6306 | /* Check there is only one change */ | |
58c0fed4 AN |
6307 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
6308 | cnt++; | |
6309 | if (mddev->raid_disks != info->raid_disks) | |
6310 | cnt++; | |
6311 | if (mddev->layout != info->layout) | |
6312 | cnt++; | |
6313 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) | |
6314 | cnt++; | |
6315 | if (cnt == 0) | |
6316 | return 0; | |
6317 | if (cnt > 1) | |
6318 | return -EINVAL; | |
1da177e4 LT |
6319 | |
6320 | if (mddev->layout != info->layout) { | |
6321 | /* Change layout | |
6322 | * we don't need to do anything at the md level, the | |
6323 | * personality will take care of it all. | |
6324 | */ | |
50ac168a | 6325 | if (mddev->pers->check_reshape == NULL) |
1da177e4 | 6326 | return -EINVAL; |
597a711b N |
6327 | else { |
6328 | mddev->new_layout = info->layout; | |
50ac168a | 6329 | rv = mddev->pers->check_reshape(mddev); |
597a711b N |
6330 | if (rv) |
6331 | mddev->new_layout = mddev->layout; | |
6332 | return rv; | |
6333 | } | |
1da177e4 | 6334 | } |
293467aa GR |
6335 | if (mddev_is_clustered(mddev)) |
6336 | md_cluster_ops->metadata_update_start(mddev); | |
58c0fed4 | 6337 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
d71f9f88 | 6338 | rv = update_size(mddev, (sector_t)info->size * 2); |
a35b0d69 | 6339 | |
da943b99 N |
6340 | if (mddev->raid_disks != info->raid_disks) |
6341 | rv = update_raid_disks(mddev, info->raid_disks); | |
6342 | ||
36fa3063 | 6343 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
293467aa GR |
6344 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
6345 | rv = -EINVAL; | |
6346 | goto err; | |
6347 | } | |
6348 | if (mddev->recovery || mddev->sync_thread) { | |
6349 | rv = -EBUSY; | |
6350 | goto err; | |
6351 | } | |
36fa3063 | 6352 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
f9209a32 | 6353 | struct bitmap *bitmap; |
36fa3063 | 6354 | /* add the bitmap */ |
293467aa GR |
6355 | if (mddev->bitmap) { |
6356 | rv = -EEXIST; | |
6357 | goto err; | |
6358 | } | |
6359 | if (mddev->bitmap_info.default_offset == 0) { | |
6360 | rv = -EINVAL; | |
6361 | goto err; | |
6362 | } | |
c3d9714e N |
6363 | mddev->bitmap_info.offset = |
6364 | mddev->bitmap_info.default_offset; | |
6409bb05 N |
6365 | mddev->bitmap_info.space = |
6366 | mddev->bitmap_info.default_space; | |
36fa3063 | 6367 | mddev->pers->quiesce(mddev, 1); |
f9209a32 GR |
6368 | bitmap = bitmap_create(mddev, -1); |
6369 | if (!IS_ERR(bitmap)) { | |
6370 | mddev->bitmap = bitmap; | |
69e51b44 | 6371 | rv = bitmap_load(mddev); |
f9209a32 | 6372 | } |
36fa3063 N |
6373 | if (rv) |
6374 | bitmap_destroy(mddev); | |
6375 | mddev->pers->quiesce(mddev, 0); | |
6376 | } else { | |
6377 | /* remove the bitmap */ | |
293467aa GR |
6378 | if (!mddev->bitmap) { |
6379 | rv = -ENOENT; | |
6380 | goto err; | |
6381 | } | |
6382 | if (mddev->bitmap->storage.file) { | |
6383 | rv = -EINVAL; | |
6384 | goto err; | |
6385 | } | |
36fa3063 N |
6386 | mddev->pers->quiesce(mddev, 1); |
6387 | bitmap_destroy(mddev); | |
6388 | mddev->pers->quiesce(mddev, 0); | |
c3d9714e | 6389 | mddev->bitmap_info.offset = 0; |
36fa3063 N |
6390 | } |
6391 | } | |
850b2b42 | 6392 | md_update_sb(mddev, 1); |
293467aa GR |
6393 | if (mddev_is_clustered(mddev)) |
6394 | md_cluster_ops->metadata_update_finish(mddev); | |
6395 | return rv; | |
6396 | err: | |
6397 | if (mddev_is_clustered(mddev)) | |
6398 | md_cluster_ops->metadata_update_cancel(mddev); | |
1da177e4 LT |
6399 | return rv; |
6400 | } | |
6401 | ||
fd01b88c | 6402 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
1da177e4 | 6403 | { |
3cb03002 | 6404 | struct md_rdev *rdev; |
1ca69c4b | 6405 | int err = 0; |
1da177e4 LT |
6406 | |
6407 | if (mddev->pers == NULL) | |
6408 | return -ENODEV; | |
6409 | ||
1ca69c4b N |
6410 | rcu_read_lock(); |
6411 | rdev = find_rdev_rcu(mddev, dev); | |
1da177e4 | 6412 | if (!rdev) |
1ca69c4b N |
6413 | err = -ENODEV; |
6414 | else { | |
6415 | md_error(mddev, rdev); | |
6416 | if (!test_bit(Faulty, &rdev->flags)) | |
6417 | err = -EBUSY; | |
6418 | } | |
6419 | rcu_read_unlock(); | |
6420 | return err; | |
1da177e4 LT |
6421 | } |
6422 | ||
2f9618ce AN |
6423 | /* |
6424 | * We have a problem here : there is no easy way to give a CHS | |
6425 | * virtual geometry. We currently pretend that we have a 2 heads | |
6426 | * 4 sectors (with a BIG number of cylinders...). This drives | |
6427 | * dosfs just mad... ;-) | |
6428 | */ | |
a885c8c4 CH |
6429 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
6430 | { | |
fd01b88c | 6431 | struct mddev *mddev = bdev->bd_disk->private_data; |
a885c8c4 CH |
6432 | |
6433 | geo->heads = 2; | |
6434 | geo->sectors = 4; | |
49ce6cea | 6435 | geo->cylinders = mddev->array_sectors / 8; |
a885c8c4 CH |
6436 | return 0; |
6437 | } | |
6438 | ||
cb335f88 NS |
6439 | static inline bool md_ioctl_valid(unsigned int cmd) |
6440 | { | |
6441 | switch (cmd) { | |
6442 | case ADD_NEW_DISK: | |
6443 | case BLKROSET: | |
6444 | case GET_ARRAY_INFO: | |
6445 | case GET_BITMAP_FILE: | |
6446 | case GET_DISK_INFO: | |
6447 | case HOT_ADD_DISK: | |
6448 | case HOT_REMOVE_DISK: | |
cb335f88 NS |
6449 | case RAID_AUTORUN: |
6450 | case RAID_VERSION: | |
6451 | case RESTART_ARRAY_RW: | |
6452 | case RUN_ARRAY: | |
6453 | case SET_ARRAY_INFO: | |
6454 | case SET_BITMAP_FILE: | |
6455 | case SET_DISK_FAULTY: | |
6456 | case STOP_ARRAY: | |
6457 | case STOP_ARRAY_RO: | |
6458 | return true; | |
6459 | default: | |
6460 | return false; | |
6461 | } | |
6462 | } | |
6463 | ||
a39907fa | 6464 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
1da177e4 LT |
6465 | unsigned int cmd, unsigned long arg) |
6466 | { | |
6467 | int err = 0; | |
6468 | void __user *argp = (void __user *)arg; | |
fd01b88c | 6469 | struct mddev *mddev = NULL; |
e2218350 | 6470 | int ro; |
1da177e4 | 6471 | |
cb335f88 NS |
6472 | if (!md_ioctl_valid(cmd)) |
6473 | return -ENOTTY; | |
6474 | ||
506c9e44 N |
6475 | switch (cmd) { |
6476 | case RAID_VERSION: | |
6477 | case GET_ARRAY_INFO: | |
6478 | case GET_DISK_INFO: | |
6479 | break; | |
6480 | default: | |
6481 | if (!capable(CAP_SYS_ADMIN)) | |
6482 | return -EACCES; | |
6483 | } | |
1da177e4 LT |
6484 | |
6485 | /* | |
6486 | * Commands dealing with the RAID driver but not any | |
6487 | * particular array: | |
6488 | */ | |
c02c0aeb N |
6489 | switch (cmd) { |
6490 | case RAID_VERSION: | |
6491 | err = get_version(argp); | |
3adc28d8 | 6492 | goto out; |
1da177e4 | 6493 | |
1da177e4 | 6494 | #ifndef MODULE |
c02c0aeb N |
6495 | case RAID_AUTORUN: |
6496 | err = 0; | |
6497 | autostart_arrays(arg); | |
3adc28d8 | 6498 | goto out; |
1da177e4 | 6499 | #endif |
c02c0aeb | 6500 | default:; |
1da177e4 LT |
6501 | } |
6502 | ||
6503 | /* | |
6504 | * Commands creating/starting a new array: | |
6505 | */ | |
6506 | ||
a39907fa | 6507 | mddev = bdev->bd_disk->private_data; |
1da177e4 LT |
6508 | |
6509 | if (!mddev) { | |
6510 | BUG(); | |
3adc28d8 | 6511 | goto out; |
1da177e4 LT |
6512 | } |
6513 | ||
1ca69c4b N |
6514 | /* Some actions do not requires the mutex */ |
6515 | switch (cmd) { | |
6516 | case GET_ARRAY_INFO: | |
6517 | if (!mddev->raid_disks && !mddev->external) | |
6518 | err = -ENODEV; | |
6519 | else | |
6520 | err = get_array_info(mddev, argp); | |
3adc28d8 | 6521 | goto out; |
1ca69c4b N |
6522 | |
6523 | case GET_DISK_INFO: | |
6524 | if (!mddev->raid_disks && !mddev->external) | |
6525 | err = -ENODEV; | |
6526 | else | |
6527 | err = get_disk_info(mddev, argp); | |
3adc28d8 | 6528 | goto out; |
1ca69c4b N |
6529 | |
6530 | case SET_DISK_FAULTY: | |
6531 | err = set_disk_faulty(mddev, new_decode_dev(arg)); | |
3adc28d8 | 6532 | goto out; |
4af1a041 N |
6533 | |
6534 | case GET_BITMAP_FILE: | |
6535 | err = get_bitmap_file(mddev, argp); | |
6536 | goto out; | |
6537 | ||
1ca69c4b N |
6538 | } |
6539 | ||
a7a3f08d N |
6540 | if (cmd == ADD_NEW_DISK) |
6541 | /* need to ensure md_delayed_delete() has completed */ | |
6542 | flush_workqueue(md_misc_wq); | |
6543 | ||
90f5f7ad HR |
6544 | if (cmd == HOT_REMOVE_DISK) |
6545 | /* need to ensure recovery thread has run */ | |
6546 | wait_event_interruptible_timeout(mddev->sb_wait, | |
6547 | !test_bit(MD_RECOVERY_NEEDED, | |
6548 | &mddev->flags), | |
6549 | msecs_to_jiffies(5000)); | |
260fa034 N |
6550 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
6551 | /* Need to flush page cache, and ensure no-one else opens | |
6552 | * and writes | |
6553 | */ | |
6554 | mutex_lock(&mddev->open_mutex); | |
9ba3b7f5 | 6555 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
260fa034 N |
6556 | mutex_unlock(&mddev->open_mutex); |
6557 | err = -EBUSY; | |
3adc28d8 | 6558 | goto out; |
260fa034 N |
6559 | } |
6560 | set_bit(MD_STILL_CLOSED, &mddev->flags); | |
6561 | mutex_unlock(&mddev->open_mutex); | |
6562 | sync_blockdev(bdev); | |
6563 | } | |
1da177e4 LT |
6564 | err = mddev_lock(mddev); |
6565 | if (err) { | |
f72ffdd6 | 6566 | printk(KERN_INFO |
1da177e4 LT |
6567 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
6568 | err, cmd); | |
3adc28d8 | 6569 | goto out; |
1da177e4 LT |
6570 | } |
6571 | ||
c02c0aeb N |
6572 | if (cmd == SET_ARRAY_INFO) { |
6573 | mdu_array_info_t info; | |
6574 | if (!arg) | |
6575 | memset(&info, 0, sizeof(info)); | |
6576 | else if (copy_from_user(&info, argp, sizeof(info))) { | |
6577 | err = -EFAULT; | |
3adc28d8 | 6578 | goto unlock; |
c02c0aeb N |
6579 | } |
6580 | if (mddev->pers) { | |
6581 | err = update_array_info(mddev, &info); | |
6582 | if (err) { | |
6583 | printk(KERN_WARNING "md: couldn't update" | |
6584 | " array info. %d\n", err); | |
3adc28d8 | 6585 | goto unlock; |
1da177e4 | 6586 | } |
3adc28d8 | 6587 | goto unlock; |
c02c0aeb N |
6588 | } |
6589 | if (!list_empty(&mddev->disks)) { | |
6590 | printk(KERN_WARNING | |
6591 | "md: array %s already has disks!\n", | |
6592 | mdname(mddev)); | |
6593 | err = -EBUSY; | |
3adc28d8 | 6594 | goto unlock; |
c02c0aeb N |
6595 | } |
6596 | if (mddev->raid_disks) { | |
6597 | printk(KERN_WARNING | |
6598 | "md: array %s already initialised!\n", | |
6599 | mdname(mddev)); | |
6600 | err = -EBUSY; | |
3adc28d8 | 6601 | goto unlock; |
c02c0aeb N |
6602 | } |
6603 | err = set_array_info(mddev, &info); | |
6604 | if (err) { | |
6605 | printk(KERN_WARNING "md: couldn't set" | |
6606 | " array info. %d\n", err); | |
3adc28d8 | 6607 | goto unlock; |
c02c0aeb | 6608 | } |
3adc28d8 | 6609 | goto unlock; |
1da177e4 LT |
6610 | } |
6611 | ||
6612 | /* | |
6613 | * Commands querying/configuring an existing array: | |
6614 | */ | |
32a7627c | 6615 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
3f9d7b0d | 6616 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
a17184a9 N |
6617 | if ((!mddev->raid_disks && !mddev->external) |
6618 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY | |
6619 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE | |
6620 | && cmd != GET_BITMAP_FILE) { | |
1da177e4 | 6621 | err = -ENODEV; |
3adc28d8 | 6622 | goto unlock; |
1da177e4 LT |
6623 | } |
6624 | ||
6625 | /* | |
6626 | * Commands even a read-only array can execute: | |
6627 | */ | |
c02c0aeb | 6628 | switch (cmd) { |
c02c0aeb N |
6629 | case RESTART_ARRAY_RW: |
6630 | err = restart_array(mddev); | |
3adc28d8 | 6631 | goto unlock; |
1da177e4 | 6632 | |
c02c0aeb N |
6633 | case STOP_ARRAY: |
6634 | err = do_md_stop(mddev, 0, bdev); | |
3adc28d8 | 6635 | goto unlock; |
1da177e4 | 6636 | |
c02c0aeb N |
6637 | case STOP_ARRAY_RO: |
6638 | err = md_set_readonly(mddev, bdev); | |
3adc28d8 | 6639 | goto unlock; |
1da177e4 | 6640 | |
3ea8929d N |
6641 | case HOT_REMOVE_DISK: |
6642 | err = hot_remove_disk(mddev, new_decode_dev(arg)); | |
3adc28d8 | 6643 | goto unlock; |
3ea8929d | 6644 | |
7ceb17e8 N |
6645 | case ADD_NEW_DISK: |
6646 | /* We can support ADD_NEW_DISK on read-only arrays | |
6647 | * on if we are re-adding a preexisting device. | |
6648 | * So require mddev->pers and MD_DISK_SYNC. | |
6649 | */ | |
6650 | if (mddev->pers) { | |
6651 | mdu_disk_info_t info; | |
6652 | if (copy_from_user(&info, argp, sizeof(info))) | |
6653 | err = -EFAULT; | |
6654 | else if (!(info.state & (1<<MD_DISK_SYNC))) | |
6655 | /* Need to clear read-only for this */ | |
6656 | break; | |
6657 | else | |
6658 | err = add_new_disk(mddev, &info); | |
3adc28d8 | 6659 | goto unlock; |
7ceb17e8 N |
6660 | } |
6661 | break; | |
6662 | ||
c02c0aeb N |
6663 | case BLKROSET: |
6664 | if (get_user(ro, (int __user *)(arg))) { | |
6665 | err = -EFAULT; | |
3adc28d8 | 6666 | goto unlock; |
c02c0aeb N |
6667 | } |
6668 | err = -EINVAL; | |
e2218350 | 6669 | |
c02c0aeb N |
6670 | /* if the bdev is going readonly the value of mddev->ro |
6671 | * does not matter, no writes are coming | |
6672 | */ | |
6673 | if (ro) | |
3adc28d8 | 6674 | goto unlock; |
e2218350 | 6675 | |
c02c0aeb N |
6676 | /* are we are already prepared for writes? */ |
6677 | if (mddev->ro != 1) | |
3adc28d8 | 6678 | goto unlock; |
e2218350 | 6679 | |
c02c0aeb N |
6680 | /* transitioning to readauto need only happen for |
6681 | * arrays that call md_write_start | |
6682 | */ | |
6683 | if (mddev->pers) { | |
6684 | err = restart_array(mddev); | |
6685 | if (err == 0) { | |
6686 | mddev->ro = 2; | |
6687 | set_disk_ro(mddev->gendisk, 0); | |
e2218350 | 6688 | } |
c02c0aeb | 6689 | } |
3adc28d8 | 6690 | goto unlock; |
1da177e4 LT |
6691 | } |
6692 | ||
6693 | /* | |
6694 | * The remaining ioctls are changing the state of the | |
f91de92e | 6695 | * superblock, so we do not allow them on read-only arrays. |
1da177e4 | 6696 | */ |
326eb17d | 6697 | if (mddev->ro && mddev->pers) { |
f91de92e N |
6698 | if (mddev->ro == 2) { |
6699 | mddev->ro = 0; | |
00bcb4ac | 6700 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0fd62b86 | 6701 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
f3378b48 N |
6702 | /* mddev_unlock will wake thread */ |
6703 | /* If a device failed while we were read-only, we | |
6704 | * need to make sure the metadata is updated now. | |
6705 | */ | |
6706 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { | |
6707 | mddev_unlock(mddev); | |
6708 | wait_event(mddev->sb_wait, | |
6709 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && | |
6710 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); | |
29f097c4 | 6711 | mddev_lock_nointr(mddev); |
f3378b48 | 6712 | } |
f91de92e N |
6713 | } else { |
6714 | err = -EROFS; | |
3adc28d8 | 6715 | goto unlock; |
f91de92e | 6716 | } |
1da177e4 LT |
6717 | } |
6718 | ||
c02c0aeb N |
6719 | switch (cmd) { |
6720 | case ADD_NEW_DISK: | |
1da177e4 | 6721 | { |
c02c0aeb N |
6722 | mdu_disk_info_t info; |
6723 | if (copy_from_user(&info, argp, sizeof(info))) | |
6724 | err = -EFAULT; | |
6725 | else | |
6726 | err = add_new_disk(mddev, &info); | |
3adc28d8 | 6727 | goto unlock; |
c02c0aeb | 6728 | } |
1da177e4 | 6729 | |
c02c0aeb N |
6730 | case HOT_ADD_DISK: |
6731 | err = hot_add_disk(mddev, new_decode_dev(arg)); | |
3adc28d8 | 6732 | goto unlock; |
1da177e4 | 6733 | |
c02c0aeb N |
6734 | case RUN_ARRAY: |
6735 | err = do_md_run(mddev); | |
3adc28d8 | 6736 | goto unlock; |
1da177e4 | 6737 | |
c02c0aeb N |
6738 | case SET_BITMAP_FILE: |
6739 | err = set_bitmap_file(mddev, (int)arg); | |
3adc28d8 | 6740 | goto unlock; |
32a7627c | 6741 | |
c02c0aeb N |
6742 | default: |
6743 | err = -EINVAL; | |
3adc28d8 | 6744 | goto unlock; |
1da177e4 LT |
6745 | } |
6746 | ||
3adc28d8 | 6747 | unlock: |
d3374825 N |
6748 | if (mddev->hold_active == UNTIL_IOCTL && |
6749 | err != -EINVAL) | |
6750 | mddev->hold_active = 0; | |
1da177e4 | 6751 | mddev_unlock(mddev); |
3adc28d8 | 6752 | out: |
1da177e4 LT |
6753 | return err; |
6754 | } | |
aa98aa31 AB |
6755 | #ifdef CONFIG_COMPAT |
6756 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
6757 | unsigned int cmd, unsigned long arg) | |
6758 | { | |
6759 | switch (cmd) { | |
6760 | case HOT_REMOVE_DISK: | |
6761 | case HOT_ADD_DISK: | |
6762 | case SET_DISK_FAULTY: | |
6763 | case SET_BITMAP_FILE: | |
6764 | /* These take in integer arg, do not convert */ | |
6765 | break; | |
6766 | default: | |
6767 | arg = (unsigned long)compat_ptr(arg); | |
6768 | break; | |
6769 | } | |
6770 | ||
6771 | return md_ioctl(bdev, mode, cmd, arg); | |
6772 | } | |
6773 | #endif /* CONFIG_COMPAT */ | |
1da177e4 | 6774 | |
a39907fa | 6775 | static int md_open(struct block_device *bdev, fmode_t mode) |
1da177e4 LT |
6776 | { |
6777 | /* | |
6778 | * Succeed if we can lock the mddev, which confirms that | |
6779 | * it isn't being stopped right now. | |
6780 | */ | |
fd01b88c | 6781 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
1da177e4 LT |
6782 | int err; |
6783 | ||
0c098220 YL |
6784 | if (!mddev) |
6785 | return -ENODEV; | |
6786 | ||
d3374825 N |
6787 | if (mddev->gendisk != bdev->bd_disk) { |
6788 | /* we are racing with mddev_put which is discarding this | |
6789 | * bd_disk. | |
6790 | */ | |
6791 | mddev_put(mddev); | |
6792 | /* Wait until bdev->bd_disk is definitely gone */ | |
e804ac78 | 6793 | flush_workqueue(md_misc_wq); |
d3374825 N |
6794 | /* Then retry the open from the top */ |
6795 | return -ERESTARTSYS; | |
6796 | } | |
6797 | BUG_ON(mddev != bdev->bd_disk->private_data); | |
6798 | ||
c8c00a69 | 6799 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
1da177e4 LT |
6800 | goto out; |
6801 | ||
6802 | err = 0; | |
f2ea68cf | 6803 | atomic_inc(&mddev->openers); |
260fa034 | 6804 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
c8c00a69 | 6805 | mutex_unlock(&mddev->open_mutex); |
1da177e4 | 6806 | |
f0b4f7e2 | 6807 | check_disk_change(bdev); |
1da177e4 LT |
6808 | out: |
6809 | return err; | |
6810 | } | |
6811 | ||
db2a144b | 6812 | static void md_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 6813 | { |
f72ffdd6 | 6814 | struct mddev *mddev = disk->private_data; |
1da177e4 | 6815 | |
52e5f9d1 | 6816 | BUG_ON(!mddev); |
f2ea68cf | 6817 | atomic_dec(&mddev->openers); |
1da177e4 | 6818 | mddev_put(mddev); |
1da177e4 | 6819 | } |
f0b4f7e2 N |
6820 | |
6821 | static int md_media_changed(struct gendisk *disk) | |
6822 | { | |
fd01b88c | 6823 | struct mddev *mddev = disk->private_data; |
f0b4f7e2 N |
6824 | |
6825 | return mddev->changed; | |
6826 | } | |
6827 | ||
6828 | static int md_revalidate(struct gendisk *disk) | |
6829 | { | |
fd01b88c | 6830 | struct mddev *mddev = disk->private_data; |
f0b4f7e2 N |
6831 | |
6832 | mddev->changed = 0; | |
6833 | return 0; | |
6834 | } | |
83d5cde4 | 6835 | static const struct block_device_operations md_fops = |
1da177e4 LT |
6836 | { |
6837 | .owner = THIS_MODULE, | |
a39907fa AV |
6838 | .open = md_open, |
6839 | .release = md_release, | |
b492b852 | 6840 | .ioctl = md_ioctl, |
aa98aa31 AB |
6841 | #ifdef CONFIG_COMPAT |
6842 | .compat_ioctl = md_compat_ioctl, | |
6843 | #endif | |
a885c8c4 | 6844 | .getgeo = md_getgeo, |
f0b4f7e2 N |
6845 | .media_changed = md_media_changed, |
6846 | .revalidate_disk= md_revalidate, | |
1da177e4 LT |
6847 | }; |
6848 | ||
f72ffdd6 | 6849 | static int md_thread(void *arg) |
1da177e4 | 6850 | { |
2b8bf345 | 6851 | struct md_thread *thread = arg; |
1da177e4 | 6852 | |
1da177e4 LT |
6853 | /* |
6854 | * md_thread is a 'system-thread', it's priority should be very | |
6855 | * high. We avoid resource deadlocks individually in each | |
6856 | * raid personality. (RAID5 does preallocation) We also use RR and | |
6857 | * the very same RT priority as kswapd, thus we will never get | |
6858 | * into a priority inversion deadlock. | |
6859 | * | |
6860 | * we definitely have to have equal or higher priority than | |
6861 | * bdflush, otherwise bdflush will deadlock if there are too | |
6862 | * many dirty RAID5 blocks. | |
6863 | */ | |
1da177e4 | 6864 | |
6985c43f | 6865 | allow_signal(SIGKILL); |
a6fb0934 | 6866 | while (!kthread_should_stop()) { |
1da177e4 | 6867 | |
93588e22 N |
6868 | /* We need to wait INTERRUPTIBLE so that |
6869 | * we don't add to the load-average. | |
6870 | * That means we need to be sure no signals are | |
6871 | * pending | |
6872 | */ | |
6873 | if (signal_pending(current)) | |
6874 | flush_signals(current); | |
6875 | ||
6876 | wait_event_interruptible_timeout | |
6877 | (thread->wqueue, | |
6878 | test_bit(THREAD_WAKEUP, &thread->flags) | |
6879 | || kthread_should_stop(), | |
6880 | thread->timeout); | |
1da177e4 | 6881 | |
6c987910 N |
6882 | clear_bit(THREAD_WAKEUP, &thread->flags); |
6883 | if (!kthread_should_stop()) | |
4ed8731d | 6884 | thread->run(thread); |
1da177e4 | 6885 | } |
a6fb0934 | 6886 | |
1da177e4 LT |
6887 | return 0; |
6888 | } | |
6889 | ||
2b8bf345 | 6890 | void md_wakeup_thread(struct md_thread *thread) |
1da177e4 LT |
6891 | { |
6892 | if (thread) { | |
36a4e1fe | 6893 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
1da177e4 LT |
6894 | set_bit(THREAD_WAKEUP, &thread->flags); |
6895 | wake_up(&thread->wqueue); | |
6896 | } | |
6897 | } | |
6c144d31 | 6898 | EXPORT_SYMBOL(md_wakeup_thread); |
1da177e4 | 6899 | |
4ed8731d SL |
6900 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
6901 | struct mddev *mddev, const char *name) | |
1da177e4 | 6902 | { |
2b8bf345 | 6903 | struct md_thread *thread; |
1da177e4 | 6904 | |
2b8bf345 | 6905 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
1da177e4 LT |
6906 | if (!thread) |
6907 | return NULL; | |
6908 | ||
1da177e4 LT |
6909 | init_waitqueue_head(&thread->wqueue); |
6910 | ||
1da177e4 LT |
6911 | thread->run = run; |
6912 | thread->mddev = mddev; | |
32a7627c | 6913 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
0da3c619 N |
6914 | thread->tsk = kthread_run(md_thread, thread, |
6915 | "%s_%s", | |
6916 | mdname(thread->mddev), | |
0232605d | 6917 | name); |
a6fb0934 | 6918 | if (IS_ERR(thread->tsk)) { |
1da177e4 LT |
6919 | kfree(thread); |
6920 | return NULL; | |
6921 | } | |
1da177e4 LT |
6922 | return thread; |
6923 | } | |
6c144d31 | 6924 | EXPORT_SYMBOL(md_register_thread); |
1da177e4 | 6925 | |
2b8bf345 | 6926 | void md_unregister_thread(struct md_thread **threadp) |
1da177e4 | 6927 | { |
2b8bf345 | 6928 | struct md_thread *thread = *threadp; |
e0cf8f04 N |
6929 | if (!thread) |
6930 | return; | |
36a4e1fe | 6931 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
01f96c0a N |
6932 | /* Locking ensures that mddev_unlock does not wake_up a |
6933 | * non-existent thread | |
6934 | */ | |
6935 | spin_lock(&pers_lock); | |
6936 | *threadp = NULL; | |
6937 | spin_unlock(&pers_lock); | |
a6fb0934 N |
6938 | |
6939 | kthread_stop(thread->tsk); | |
1da177e4 LT |
6940 | kfree(thread); |
6941 | } | |
6c144d31 | 6942 | EXPORT_SYMBOL(md_unregister_thread); |
1da177e4 | 6943 | |
fd01b88c | 6944 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 6945 | { |
b2d444d7 | 6946 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
1da177e4 | 6947 | return; |
6bfe0b49 | 6948 | |
de393cde | 6949 | if (!mddev->pers || !mddev->pers->error_handler) |
1da177e4 LT |
6950 | return; |
6951 | mddev->pers->error_handler(mddev,rdev); | |
72a23c21 NB |
6952 | if (mddev->degraded) |
6953 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
00bcb4ac | 6954 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
1da177e4 LT |
6955 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
6956 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
6957 | md_wakeup_thread(mddev->thread); | |
768a418d | 6958 | if (mddev->event_work.func) |
e804ac78 | 6959 | queue_work(md_misc_wq, &mddev->event_work); |
c331eb04 | 6960 | md_new_event_inintr(mddev); |
1da177e4 | 6961 | } |
6c144d31 | 6962 | EXPORT_SYMBOL(md_error); |
1da177e4 LT |
6963 | |
6964 | /* seq_file implementation /proc/mdstat */ | |
6965 | ||
6966 | static void status_unused(struct seq_file *seq) | |
6967 | { | |
6968 | int i = 0; | |
3cb03002 | 6969 | struct md_rdev *rdev; |
1da177e4 LT |
6970 | |
6971 | seq_printf(seq, "unused devices: "); | |
6972 | ||
159ec1fc | 6973 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
1da177e4 LT |
6974 | char b[BDEVNAME_SIZE]; |
6975 | i++; | |
6976 | seq_printf(seq, "%s ", | |
6977 | bdevname(rdev->bdev,b)); | |
6978 | } | |
6979 | if (!i) | |
6980 | seq_printf(seq, "<none>"); | |
6981 | ||
6982 | seq_printf(seq, "\n"); | |
6983 | } | |
6984 | ||
f72ffdd6 | 6985 | static void status_resync(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 6986 | { |
dd71cf6b N |
6987 | sector_t max_sectors, resync, res; |
6988 | unsigned long dt, db; | |
6989 | sector_t rt; | |
4588b42e N |
6990 | int scale; |
6991 | unsigned int per_milli; | |
1da177e4 | 6992 | |
72f36d59 N |
6993 | if (mddev->curr_resync <= 3) |
6994 | resync = 0; | |
6995 | else | |
6996 | resync = mddev->curr_resync | |
6997 | - atomic_read(&mddev->recovery_active); | |
1da177e4 | 6998 | |
c804cdec N |
6999 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
7000 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
dd71cf6b | 7001 | max_sectors = mddev->resync_max_sectors; |
1da177e4 | 7002 | else |
dd71cf6b | 7003 | max_sectors = mddev->dev_sectors; |
1da177e4 | 7004 | |
403df478 | 7005 | WARN_ON(max_sectors == 0); |
4588b42e | 7006 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
dd71cf6b | 7007 | * in a sector_t, and (max_sectors>>scale) will fit in a |
4588b42e N |
7008 | * u32, as those are the requirements for sector_div. |
7009 | * Thus 'scale' must be at least 10 | |
7010 | */ | |
7011 | scale = 10; | |
7012 | if (sizeof(sector_t) > sizeof(unsigned long)) { | |
dd71cf6b | 7013 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
4588b42e N |
7014 | scale++; |
7015 | } | |
7016 | res = (resync>>scale)*1000; | |
dd71cf6b | 7017 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
4588b42e N |
7018 | |
7019 | per_milli = res; | |
1da177e4 | 7020 | { |
4588b42e | 7021 | int i, x = per_milli/50, y = 20-x; |
1da177e4 LT |
7022 | seq_printf(seq, "["); |
7023 | for (i = 0; i < x; i++) | |
7024 | seq_printf(seq, "="); | |
7025 | seq_printf(seq, ">"); | |
7026 | for (i = 0; i < y; i++) | |
7027 | seq_printf(seq, "."); | |
7028 | seq_printf(seq, "] "); | |
7029 | } | |
4588b42e | 7030 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
ccfcc3c1 N |
7031 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
7032 | "reshape" : | |
61df9d91 N |
7033 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
7034 | "check" : | |
7035 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? | |
7036 | "resync" : "recovery"))), | |
7037 | per_milli/10, per_milli % 10, | |
dd71cf6b N |
7038 | (unsigned long long) resync/2, |
7039 | (unsigned long long) max_sectors/2); | |
1da177e4 LT |
7040 | |
7041 | /* | |
1da177e4 LT |
7042 | * dt: time from mark until now |
7043 | * db: blocks written from mark until now | |
7044 | * rt: remaining time | |
dd71cf6b N |
7045 | * |
7046 | * rt is a sector_t, so could be 32bit or 64bit. | |
7047 | * So we divide before multiply in case it is 32bit and close | |
7048 | * to the limit. | |
25985edc | 7049 | * We scale the divisor (db) by 32 to avoid losing precision |
dd71cf6b N |
7050 | * near the end of resync when the number of remaining sectors |
7051 | * is close to 'db'. | |
7052 | * We then divide rt by 32 after multiplying by db to compensate. | |
7053 | * The '+1' avoids division by zero if db is very small. | |
1da177e4 LT |
7054 | */ |
7055 | dt = ((jiffies - mddev->resync_mark) / HZ); | |
7056 | if (!dt) dt++; | |
ff4e8d9a N |
7057 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
7058 | - mddev->resync_mark_cnt; | |
1da177e4 | 7059 | |
dd71cf6b N |
7060 | rt = max_sectors - resync; /* number of remaining sectors */ |
7061 | sector_div(rt, db/32+1); | |
7062 | rt *= dt; | |
7063 | rt >>= 5; | |
7064 | ||
7065 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, | |
7066 | ((unsigned long)rt % 60)/6); | |
1da177e4 | 7067 | |
ff4e8d9a | 7068 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
1da177e4 LT |
7069 | } |
7070 | ||
7071 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) | |
7072 | { | |
7073 | struct list_head *tmp; | |
7074 | loff_t l = *pos; | |
fd01b88c | 7075 | struct mddev *mddev; |
1da177e4 LT |
7076 | |
7077 | if (l >= 0x10000) | |
7078 | return NULL; | |
7079 | if (!l--) | |
7080 | /* header */ | |
7081 | return (void*)1; | |
7082 | ||
7083 | spin_lock(&all_mddevs_lock); | |
7084 | list_for_each(tmp,&all_mddevs) | |
7085 | if (!l--) { | |
fd01b88c | 7086 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
1da177e4 LT |
7087 | mddev_get(mddev); |
7088 | spin_unlock(&all_mddevs_lock); | |
7089 | return mddev; | |
7090 | } | |
7091 | spin_unlock(&all_mddevs_lock); | |
7092 | if (!l--) | |
7093 | return (void*)2;/* tail */ | |
7094 | return NULL; | |
7095 | } | |
7096 | ||
7097 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
7098 | { | |
7099 | struct list_head *tmp; | |
fd01b88c | 7100 | struct mddev *next_mddev, *mddev = v; |
f72ffdd6 | 7101 | |
1da177e4 LT |
7102 | ++*pos; |
7103 | if (v == (void*)2) | |
7104 | return NULL; | |
7105 | ||
7106 | spin_lock(&all_mddevs_lock); | |
7107 | if (v == (void*)1) | |
7108 | tmp = all_mddevs.next; | |
7109 | else | |
7110 | tmp = mddev->all_mddevs.next; | |
7111 | if (tmp != &all_mddevs) | |
fd01b88c | 7112 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
1da177e4 LT |
7113 | else { |
7114 | next_mddev = (void*)2; | |
7115 | *pos = 0x10000; | |
f72ffdd6 | 7116 | } |
1da177e4 LT |
7117 | spin_unlock(&all_mddevs_lock); |
7118 | ||
7119 | if (v != (void*)1) | |
7120 | mddev_put(mddev); | |
7121 | return next_mddev; | |
7122 | ||
7123 | } | |
7124 | ||
7125 | static void md_seq_stop(struct seq_file *seq, void *v) | |
7126 | { | |
fd01b88c | 7127 | struct mddev *mddev = v; |
1da177e4 LT |
7128 | |
7129 | if (mddev && v != (void*)1 && v != (void*)2) | |
7130 | mddev_put(mddev); | |
7131 | } | |
7132 | ||
7133 | static int md_seq_show(struct seq_file *seq, void *v) | |
7134 | { | |
fd01b88c | 7135 | struct mddev *mddev = v; |
dd8ac336 | 7136 | sector_t sectors; |
3cb03002 | 7137 | struct md_rdev *rdev; |
1da177e4 LT |
7138 | |
7139 | if (v == (void*)1) { | |
84fc4b56 | 7140 | struct md_personality *pers; |
1da177e4 LT |
7141 | seq_printf(seq, "Personalities : "); |
7142 | spin_lock(&pers_lock); | |
2604b703 N |
7143 | list_for_each_entry(pers, &pers_list, list) |
7144 | seq_printf(seq, "[%s] ", pers->name); | |
1da177e4 LT |
7145 | |
7146 | spin_unlock(&pers_lock); | |
7147 | seq_printf(seq, "\n"); | |
f1514638 | 7148 | seq->poll_event = atomic_read(&md_event_count); |
1da177e4 LT |
7149 | return 0; |
7150 | } | |
7151 | if (v == (void*)2) { | |
7152 | status_unused(seq); | |
7153 | return 0; | |
7154 | } | |
7155 | ||
36d091f4 | 7156 | spin_lock(&mddev->lock); |
1da177e4 LT |
7157 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
7158 | seq_printf(seq, "%s : %sactive", mdname(mddev), | |
7159 | mddev->pers ? "" : "in"); | |
7160 | if (mddev->pers) { | |
f91de92e | 7161 | if (mddev->ro==1) |
1da177e4 | 7162 | seq_printf(seq, " (read-only)"); |
f91de92e | 7163 | if (mddev->ro==2) |
52720ae7 | 7164 | seq_printf(seq, " (auto-read-only)"); |
1da177e4 LT |
7165 | seq_printf(seq, " %s", mddev->pers->name); |
7166 | } | |
7167 | ||
dd8ac336 | 7168 | sectors = 0; |
f97fcad3 N |
7169 | rcu_read_lock(); |
7170 | rdev_for_each_rcu(rdev, mddev) { | |
1da177e4 LT |
7171 | char b[BDEVNAME_SIZE]; |
7172 | seq_printf(seq, " %s[%d]", | |
7173 | bdevname(rdev->bdev,b), rdev->desc_nr); | |
8ddf9efe N |
7174 | if (test_bit(WriteMostly, &rdev->flags)) |
7175 | seq_printf(seq, "(W)"); | |
b2d444d7 | 7176 | if (test_bit(Faulty, &rdev->flags)) { |
1da177e4 LT |
7177 | seq_printf(seq, "(F)"); |
7178 | continue; | |
2d78f8c4 N |
7179 | } |
7180 | if (rdev->raid_disk < 0) | |
b325a32e | 7181 | seq_printf(seq, "(S)"); /* spare */ |
2d78f8c4 N |
7182 | if (test_bit(Replacement, &rdev->flags)) |
7183 | seq_printf(seq, "(R)"); | |
dd8ac336 | 7184 | sectors += rdev->sectors; |
1da177e4 | 7185 | } |
f97fcad3 | 7186 | rcu_read_unlock(); |
1da177e4 LT |
7187 | |
7188 | if (!list_empty(&mddev->disks)) { | |
7189 | if (mddev->pers) | |
7190 | seq_printf(seq, "\n %llu blocks", | |
f233ea5c AN |
7191 | (unsigned long long) |
7192 | mddev->array_sectors / 2); | |
1da177e4 LT |
7193 | else |
7194 | seq_printf(seq, "\n %llu blocks", | |
dd8ac336 | 7195 | (unsigned long long)sectors / 2); |
1da177e4 | 7196 | } |
1cd6bf19 N |
7197 | if (mddev->persistent) { |
7198 | if (mddev->major_version != 0 || | |
7199 | mddev->minor_version != 90) { | |
7200 | seq_printf(seq," super %d.%d", | |
7201 | mddev->major_version, | |
7202 | mddev->minor_version); | |
7203 | } | |
e691063a N |
7204 | } else if (mddev->external) |
7205 | seq_printf(seq, " super external:%s", | |
7206 | mddev->metadata_type); | |
7207 | else | |
1cd6bf19 | 7208 | seq_printf(seq, " super non-persistent"); |
1da177e4 LT |
7209 | |
7210 | if (mddev->pers) { | |
d710e138 | 7211 | mddev->pers->status(seq, mddev); |
f72ffdd6 | 7212 | seq_printf(seq, "\n "); |
8e1b39d6 N |
7213 | if (mddev->pers->sync_request) { |
7214 | if (mddev->curr_resync > 2) { | |
d710e138 | 7215 | status_resync(seq, mddev); |
8e1b39d6 | 7216 | seq_printf(seq, "\n "); |
72f36d59 | 7217 | } else if (mddev->curr_resync >= 1) |
8e1b39d6 N |
7218 | seq_printf(seq, "\tresync=DELAYED\n "); |
7219 | else if (mddev->recovery_cp < MaxSector) | |
7220 | seq_printf(seq, "\tresync=PENDING\n "); | |
7221 | } | |
32a7627c N |
7222 | } else |
7223 | seq_printf(seq, "\n "); | |
7224 | ||
57148964 | 7225 | bitmap_status(seq, mddev->bitmap); |
1da177e4 LT |
7226 | |
7227 | seq_printf(seq, "\n"); | |
7228 | } | |
36d091f4 | 7229 | spin_unlock(&mddev->lock); |
f72ffdd6 | 7230 | |
1da177e4 LT |
7231 | return 0; |
7232 | } | |
7233 | ||
110518bc | 7234 | static const struct seq_operations md_seq_ops = { |
1da177e4 LT |
7235 | .start = md_seq_start, |
7236 | .next = md_seq_next, | |
7237 | .stop = md_seq_stop, | |
7238 | .show = md_seq_show, | |
7239 | }; | |
7240 | ||
7241 | static int md_seq_open(struct inode *inode, struct file *file) | |
7242 | { | |
f1514638 | 7243 | struct seq_file *seq; |
1da177e4 LT |
7244 | int error; |
7245 | ||
7246 | error = seq_open(file, &md_seq_ops); | |
d7603b7e | 7247 | if (error) |
f1514638 KS |
7248 | return error; |
7249 | ||
7250 | seq = file->private_data; | |
7251 | seq->poll_event = atomic_read(&md_event_count); | |
1da177e4 LT |
7252 | return error; |
7253 | } | |
7254 | ||
e2f23b60 | 7255 | static int md_unloading; |
d7603b7e N |
7256 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
7257 | { | |
f1514638 | 7258 | struct seq_file *seq = filp->private_data; |
d7603b7e N |
7259 | int mask; |
7260 | ||
e2f23b60 | 7261 | if (md_unloading) |
7d7e64f2 | 7262 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
d7603b7e N |
7263 | poll_wait(filp, &md_event_waiters, wait); |
7264 | ||
7265 | /* always allow read */ | |
7266 | mask = POLLIN | POLLRDNORM; | |
7267 | ||
f1514638 | 7268 | if (seq->poll_event != atomic_read(&md_event_count)) |
d7603b7e N |
7269 | mask |= POLLERR | POLLPRI; |
7270 | return mask; | |
7271 | } | |
7272 | ||
fa027c2a | 7273 | static const struct file_operations md_seq_fops = { |
e24650c2 | 7274 | .owner = THIS_MODULE, |
1da177e4 LT |
7275 | .open = md_seq_open, |
7276 | .read = seq_read, | |
7277 | .llseek = seq_lseek, | |
c3f94b40 | 7278 | .release = seq_release_private, |
d7603b7e | 7279 | .poll = mdstat_poll, |
1da177e4 LT |
7280 | }; |
7281 | ||
84fc4b56 | 7282 | int register_md_personality(struct md_personality *p) |
1da177e4 | 7283 | { |
50bd3774 CY |
7284 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
7285 | p->name, p->level); | |
1da177e4 | 7286 | spin_lock(&pers_lock); |
2604b703 | 7287 | list_add_tail(&p->list, &pers_list); |
1da177e4 LT |
7288 | spin_unlock(&pers_lock); |
7289 | return 0; | |
7290 | } | |
6c144d31 | 7291 | EXPORT_SYMBOL(register_md_personality); |
1da177e4 | 7292 | |
84fc4b56 | 7293 | int unregister_md_personality(struct md_personality *p) |
1da177e4 | 7294 | { |
2604b703 | 7295 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
1da177e4 | 7296 | spin_lock(&pers_lock); |
2604b703 | 7297 | list_del_init(&p->list); |
1da177e4 LT |
7298 | spin_unlock(&pers_lock); |
7299 | return 0; | |
7300 | } | |
6c144d31 | 7301 | EXPORT_SYMBOL(unregister_md_personality); |
1da177e4 | 7302 | |
edb39c9d GR |
7303 | int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module) |
7304 | { | |
7305 | if (md_cluster_ops != NULL) | |
7306 | return -EALREADY; | |
7307 | spin_lock(&pers_lock); | |
7308 | md_cluster_ops = ops; | |
7309 | md_cluster_mod = module; | |
7310 | spin_unlock(&pers_lock); | |
7311 | return 0; | |
7312 | } | |
7313 | EXPORT_SYMBOL(register_md_cluster_operations); | |
7314 | ||
7315 | int unregister_md_cluster_operations(void) | |
7316 | { | |
7317 | spin_lock(&pers_lock); | |
7318 | md_cluster_ops = NULL; | |
7319 | spin_unlock(&pers_lock); | |
7320 | return 0; | |
7321 | } | |
7322 | EXPORT_SYMBOL(unregister_md_cluster_operations); | |
7323 | ||
7324 | int md_setup_cluster(struct mddev *mddev, int nodes) | |
7325 | { | |
7326 | int err; | |
7327 | ||
7328 | err = request_module("md-cluster"); | |
7329 | if (err) { | |
7330 | pr_err("md-cluster module not found.\n"); | |
7331 | return err; | |
7332 | } | |
7333 | ||
7334 | spin_lock(&pers_lock); | |
7335 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { | |
7336 | spin_unlock(&pers_lock); | |
7337 | return -ENOENT; | |
7338 | } | |
7339 | spin_unlock(&pers_lock); | |
7340 | ||
cf921cc1 | 7341 | return md_cluster_ops->join(mddev, nodes); |
edb39c9d GR |
7342 | } |
7343 | ||
7344 | void md_cluster_stop(struct mddev *mddev) | |
7345 | { | |
c4ce867f GR |
7346 | if (!md_cluster_ops) |
7347 | return; | |
edb39c9d GR |
7348 | md_cluster_ops->leave(mddev); |
7349 | module_put(md_cluster_mod); | |
7350 | } | |
7351 | ||
fd01b88c | 7352 | static int is_mddev_idle(struct mddev *mddev, int init) |
1da177e4 | 7353 | { |
f72ffdd6 | 7354 | struct md_rdev *rdev; |
1da177e4 | 7355 | int idle; |
eea1bf38 | 7356 | int curr_events; |
1da177e4 LT |
7357 | |
7358 | idle = 1; | |
4b80991c N |
7359 | rcu_read_lock(); |
7360 | rdev_for_each_rcu(rdev, mddev) { | |
1da177e4 | 7361 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
eea1bf38 N |
7362 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
7363 | (int)part_stat_read(&disk->part0, sectors[1]) - | |
7364 | atomic_read(&disk->sync_io); | |
713f6ab1 N |
7365 | /* sync IO will cause sync_io to increase before the disk_stats |
7366 | * as sync_io is counted when a request starts, and | |
7367 | * disk_stats is counted when it completes. | |
7368 | * So resync activity will cause curr_events to be smaller than | |
7369 | * when there was no such activity. | |
7370 | * non-sync IO will cause disk_stat to increase without | |
7371 | * increasing sync_io so curr_events will (eventually) | |
7372 | * be larger than it was before. Once it becomes | |
7373 | * substantially larger, the test below will cause | |
7374 | * the array to appear non-idle, and resync will slow | |
7375 | * down. | |
7376 | * If there is a lot of outstanding resync activity when | |
7377 | * we set last_event to curr_events, then all that activity | |
7378 | * completing might cause the array to appear non-idle | |
7379 | * and resync will be slowed down even though there might | |
7380 | * not have been non-resync activity. This will only | |
7381 | * happen once though. 'last_events' will soon reflect | |
7382 | * the state where there is little or no outstanding | |
7383 | * resync requests, and further resync activity will | |
7384 | * always make curr_events less than last_events. | |
c0e48521 | 7385 | * |
1da177e4 | 7386 | */ |
eea1bf38 | 7387 | if (init || curr_events - rdev->last_events > 64) { |
1da177e4 LT |
7388 | rdev->last_events = curr_events; |
7389 | idle = 0; | |
7390 | } | |
7391 | } | |
4b80991c | 7392 | rcu_read_unlock(); |
1da177e4 LT |
7393 | return idle; |
7394 | } | |
7395 | ||
fd01b88c | 7396 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
1da177e4 LT |
7397 | { |
7398 | /* another "blocks" (512byte) blocks have been synced */ | |
7399 | atomic_sub(blocks, &mddev->recovery_active); | |
7400 | wake_up(&mddev->recovery_wait); | |
7401 | if (!ok) { | |
dfc70645 | 7402 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
0a19caab | 7403 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
1da177e4 LT |
7404 | md_wakeup_thread(mddev->thread); |
7405 | // stop recovery, signal do_sync .... | |
7406 | } | |
7407 | } | |
6c144d31 | 7408 | EXPORT_SYMBOL(md_done_sync); |
1da177e4 | 7409 | |
06d91a5f N |
7410 | /* md_write_start(mddev, bi) |
7411 | * If we need to update some array metadata (e.g. 'active' flag | |
3d310eb7 N |
7412 | * in superblock) before writing, schedule a superblock update |
7413 | * and wait for it to complete. | |
06d91a5f | 7414 | */ |
fd01b88c | 7415 | void md_write_start(struct mddev *mddev, struct bio *bi) |
1da177e4 | 7416 | { |
0fd62b86 | 7417 | int did_change = 0; |
06d91a5f | 7418 | if (bio_data_dir(bi) != WRITE) |
3d310eb7 | 7419 | return; |
06d91a5f | 7420 | |
f91de92e N |
7421 | BUG_ON(mddev->ro == 1); |
7422 | if (mddev->ro == 2) { | |
7423 | /* need to switch to read/write */ | |
7424 | mddev->ro = 0; | |
7425 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
7426 | md_wakeup_thread(mddev->thread); | |
25156198 | 7427 | md_wakeup_thread(mddev->sync_thread); |
0fd62b86 | 7428 | did_change = 1; |
f91de92e | 7429 | } |
06d91a5f | 7430 | atomic_inc(&mddev->writes_pending); |
31a59e34 N |
7431 | if (mddev->safemode == 1) |
7432 | mddev->safemode = 0; | |
06d91a5f | 7433 | if (mddev->in_sync) { |
85572d7c | 7434 | spin_lock(&mddev->lock); |
3d310eb7 N |
7435 | if (mddev->in_sync) { |
7436 | mddev->in_sync = 0; | |
850b2b42 | 7437 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
070dc6dd | 7438 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
3d310eb7 | 7439 | md_wakeup_thread(mddev->thread); |
0fd62b86 | 7440 | did_change = 1; |
3d310eb7 | 7441 | } |
85572d7c | 7442 | spin_unlock(&mddev->lock); |
06d91a5f | 7443 | } |
0fd62b86 | 7444 | if (did_change) |
00bcb4ac | 7445 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
09a44cc1 | 7446 | wait_event(mddev->sb_wait, |
09a44cc1 | 7447 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
1da177e4 | 7448 | } |
6c144d31 | 7449 | EXPORT_SYMBOL(md_write_start); |
1da177e4 | 7450 | |
fd01b88c | 7451 | void md_write_end(struct mddev *mddev) |
1da177e4 LT |
7452 | { |
7453 | if (atomic_dec_and_test(&mddev->writes_pending)) { | |
7454 | if (mddev->safemode == 2) | |
7455 | md_wakeup_thread(mddev->thread); | |
16f17b39 | 7456 | else if (mddev->safemode_delay) |
1da177e4 LT |
7457 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
7458 | } | |
7459 | } | |
6c144d31 | 7460 | EXPORT_SYMBOL(md_write_end); |
1da177e4 | 7461 | |
2a2275d6 N |
7462 | /* md_allow_write(mddev) |
7463 | * Calling this ensures that the array is marked 'active' so that writes | |
7464 | * may proceed without blocking. It is important to call this before | |
7465 | * attempting a GFP_KERNEL allocation while holding the mddev lock. | |
7466 | * Must be called with mddev_lock held. | |
b5470dc5 DW |
7467 | * |
7468 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock | |
7469 | * is dropped, so return -EAGAIN after notifying userspace. | |
2a2275d6 | 7470 | */ |
fd01b88c | 7471 | int md_allow_write(struct mddev *mddev) |
2a2275d6 N |
7472 | { |
7473 | if (!mddev->pers) | |
b5470dc5 | 7474 | return 0; |
2a2275d6 | 7475 | if (mddev->ro) |
b5470dc5 | 7476 | return 0; |
1a0fd497 | 7477 | if (!mddev->pers->sync_request) |
b5470dc5 | 7478 | return 0; |
2a2275d6 | 7479 | |
85572d7c | 7480 | spin_lock(&mddev->lock); |
2a2275d6 N |
7481 | if (mddev->in_sync) { |
7482 | mddev->in_sync = 0; | |
7483 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); | |
070dc6dd | 7484 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
2a2275d6 N |
7485 | if (mddev->safemode_delay && |
7486 | mddev->safemode == 0) | |
7487 | mddev->safemode = 1; | |
85572d7c | 7488 | spin_unlock(&mddev->lock); |
293467aa GR |
7489 | if (mddev_is_clustered(mddev)) |
7490 | md_cluster_ops->metadata_update_start(mddev); | |
2a2275d6 | 7491 | md_update_sb(mddev, 0); |
293467aa GR |
7492 | if (mddev_is_clustered(mddev)) |
7493 | md_cluster_ops->metadata_update_finish(mddev); | |
00bcb4ac | 7494 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
2a2275d6 | 7495 | } else |
85572d7c | 7496 | spin_unlock(&mddev->lock); |
b5470dc5 | 7497 | |
070dc6dd | 7498 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
b5470dc5 DW |
7499 | return -EAGAIN; |
7500 | else | |
7501 | return 0; | |
2a2275d6 N |
7502 | } |
7503 | EXPORT_SYMBOL_GPL(md_allow_write); | |
7504 | ||
1da177e4 LT |
7505 | #define SYNC_MARKS 10 |
7506 | #define SYNC_MARK_STEP (3*HZ) | |
54f89341 | 7507 | #define UPDATE_FREQUENCY (5*60*HZ) |
4ed8731d | 7508 | void md_do_sync(struct md_thread *thread) |
1da177e4 | 7509 | { |
4ed8731d | 7510 | struct mddev *mddev = thread->mddev; |
fd01b88c | 7511 | struct mddev *mddev2; |
1da177e4 LT |
7512 | unsigned int currspeed = 0, |
7513 | window; | |
ac7e50a3 | 7514 | sector_t max_sectors,j, io_sectors, recovery_done; |
1da177e4 | 7515 | unsigned long mark[SYNC_MARKS]; |
54f89341 | 7516 | unsigned long update_time; |
1da177e4 LT |
7517 | sector_t mark_cnt[SYNC_MARKS]; |
7518 | int last_mark,m; | |
7519 | struct list_head *tmp; | |
7520 | sector_t last_check; | |
57afd89f | 7521 | int skipped = 0; |
3cb03002 | 7522 | struct md_rdev *rdev; |
c4a39551 | 7523 | char *desc, *action = NULL; |
7c2c57c9 | 7524 | struct blk_plug plug; |
1da177e4 LT |
7525 | |
7526 | /* just incase thread restarts... */ | |
7527 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) | |
7528 | return; | |
3991b31e N |
7529 | if (mddev->ro) {/* never try to sync a read-only array */ |
7530 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
5fd6c1dc | 7531 | return; |
3991b31e | 7532 | } |
1da177e4 | 7533 | |
61df9d91 | 7534 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
c4a39551 | 7535 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
61df9d91 | 7536 | desc = "data-check"; |
c4a39551 JB |
7537 | action = "check"; |
7538 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { | |
61df9d91 | 7539 | desc = "requested-resync"; |
c4a39551 JB |
7540 | action = "repair"; |
7541 | } else | |
61df9d91 N |
7542 | desc = "resync"; |
7543 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
7544 | desc = "reshape"; | |
7545 | else | |
7546 | desc = "recovery"; | |
7547 | ||
c4a39551 JB |
7548 | mddev->last_sync_action = action ?: desc; |
7549 | ||
1da177e4 LT |
7550 | /* we overload curr_resync somewhat here. |
7551 | * 0 == not engaged in resync at all | |
7552 | * 2 == checking that there is no conflict with another sync | |
7553 | * 1 == like 2, but have yielded to allow conflicting resync to | |
7554 | * commense | |
7555 | * other == active in resync - this many blocks | |
7556 | * | |
7557 | * Before starting a resync we must have set curr_resync to | |
7558 | * 2, and then checked that every "conflicting" array has curr_resync | |
7559 | * less than ours. When we find one that is the same or higher | |
7560 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync | |
7561 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). | |
7562 | * This will mean we have to start checking from the beginning again. | |
7563 | * | |
7564 | */ | |
7565 | ||
7566 | do { | |
7567 | mddev->curr_resync = 2; | |
7568 | ||
7569 | try_again: | |
404e4b43 | 7570 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
1da177e4 | 7571 | goto skip; |
29ac4aa3 | 7572 | for_each_mddev(mddev2, tmp) { |
1da177e4 LT |
7573 | if (mddev2 == mddev) |
7574 | continue; | |
90b08710 BS |
7575 | if (!mddev->parallel_resync |
7576 | && mddev2->curr_resync | |
7577 | && match_mddev_units(mddev, mddev2)) { | |
1da177e4 LT |
7578 | DEFINE_WAIT(wq); |
7579 | if (mddev < mddev2 && mddev->curr_resync == 2) { | |
7580 | /* arbitrarily yield */ | |
7581 | mddev->curr_resync = 1; | |
7582 | wake_up(&resync_wait); | |
7583 | } | |
7584 | if (mddev > mddev2 && mddev->curr_resync == 1) | |
7585 | /* no need to wait here, we can wait the next | |
7586 | * time 'round when curr_resync == 2 | |
7587 | */ | |
7588 | continue; | |
9744197c N |
7589 | /* We need to wait 'interruptible' so as not to |
7590 | * contribute to the load average, and not to | |
7591 | * be caught by 'softlockup' | |
7592 | */ | |
7593 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); | |
c91abf5a | 7594 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
8712e553 | 7595 | mddev2->curr_resync >= mddev->curr_resync) { |
61df9d91 N |
7596 | printk(KERN_INFO "md: delaying %s of %s" |
7597 | " until %s has finished (they" | |
1da177e4 | 7598 | " share one or more physical units)\n", |
61df9d91 | 7599 | desc, mdname(mddev), mdname(mddev2)); |
1da177e4 | 7600 | mddev_put(mddev2); |
9744197c N |
7601 | if (signal_pending(current)) |
7602 | flush_signals(current); | |
1da177e4 LT |
7603 | schedule(); |
7604 | finish_wait(&resync_wait, &wq); | |
7605 | goto try_again; | |
7606 | } | |
7607 | finish_wait(&resync_wait, &wq); | |
7608 | } | |
7609 | } | |
7610 | } while (mddev->curr_resync < 2); | |
7611 | ||
5fd6c1dc | 7612 | j = 0; |
9d88883e | 7613 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
1da177e4 | 7614 | /* resync follows the size requested by the personality, |
57afd89f | 7615 | * which defaults to physical size, but can be virtual size |
1da177e4 LT |
7616 | */ |
7617 | max_sectors = mddev->resync_max_sectors; | |
7f7583d4 | 7618 | atomic64_set(&mddev->resync_mismatches, 0); |
5fd6c1dc | 7619 | /* we don't use the checkpoint if there's a bitmap */ |
5e96ee65 NB |
7620 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
7621 | j = mddev->resync_min; | |
7622 | else if (!mddev->bitmap) | |
5fd6c1dc | 7623 | j = mddev->recovery_cp; |
5e96ee65 | 7624 | |
ccfcc3c1 | 7625 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
c804cdec | 7626 | max_sectors = mddev->resync_max_sectors; |
5fd6c1dc | 7627 | else { |
1da177e4 | 7628 | /* recovery follows the physical size of devices */ |
58c0fed4 | 7629 | max_sectors = mddev->dev_sectors; |
5fd6c1dc | 7630 | j = MaxSector; |
4e59ca7d | 7631 | rcu_read_lock(); |
dafb20fa | 7632 | rdev_for_each_rcu(rdev, mddev) |
5fd6c1dc N |
7633 | if (rdev->raid_disk >= 0 && |
7634 | !test_bit(Faulty, &rdev->flags) && | |
7635 | !test_bit(In_sync, &rdev->flags) && | |
7636 | rdev->recovery_offset < j) | |
7637 | j = rdev->recovery_offset; | |
4e59ca7d | 7638 | rcu_read_unlock(); |
133d4527 N |
7639 | |
7640 | /* If there is a bitmap, we need to make sure all | |
7641 | * writes that started before we added a spare | |
7642 | * complete before we start doing a recovery. | |
7643 | * Otherwise the write might complete and (via | |
7644 | * bitmap_endwrite) set a bit in the bitmap after the | |
7645 | * recovery has checked that bit and skipped that | |
7646 | * region. | |
7647 | */ | |
7648 | if (mddev->bitmap) { | |
7649 | mddev->pers->quiesce(mddev, 1); | |
7650 | mddev->pers->quiesce(mddev, 0); | |
7651 | } | |
5fd6c1dc | 7652 | } |
1da177e4 | 7653 | |
61df9d91 N |
7654 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
7655 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" | |
7656 | " %d KB/sec/disk.\n", speed_min(mddev)); | |
338cec32 | 7657 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
61df9d91 N |
7658 | "(but not more than %d KB/sec) for %s.\n", |
7659 | speed_max(mddev), desc); | |
1da177e4 | 7660 | |
eea1bf38 | 7661 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
5fd6c1dc | 7662 | |
57afd89f | 7663 | io_sectors = 0; |
1da177e4 LT |
7664 | for (m = 0; m < SYNC_MARKS; m++) { |
7665 | mark[m] = jiffies; | |
57afd89f | 7666 | mark_cnt[m] = io_sectors; |
1da177e4 LT |
7667 | } |
7668 | last_mark = 0; | |
7669 | mddev->resync_mark = mark[last_mark]; | |
7670 | mddev->resync_mark_cnt = mark_cnt[last_mark]; | |
7671 | ||
7672 | /* | |
7673 | * Tune reconstruction: | |
7674 | */ | |
7675 | window = 32*(PAGE_SIZE/512); | |
ac42450c JB |
7676 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
7677 | window/2, (unsigned long long)max_sectors/2); | |
1da177e4 LT |
7678 | |
7679 | atomic_set(&mddev->recovery_active, 0); | |
1da177e4 LT |
7680 | last_check = 0; |
7681 | ||
7682 | if (j>2) { | |
c91abf5a | 7683 | printk(KERN_INFO |
61df9d91 N |
7684 | "md: resuming %s of %s from checkpoint.\n", |
7685 | desc, mdname(mddev)); | |
1da177e4 | 7686 | mddev->curr_resync = j; |
72f36d59 N |
7687 | } else |
7688 | mddev->curr_resync = 3; /* no longer delayed */ | |
75d3da43 | 7689 | mddev->curr_resync_completed = j; |
72f36d59 N |
7690 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
7691 | md_new_event(mddev); | |
54f89341 | 7692 | update_time = jiffies; |
1da177e4 | 7693 | |
96ae923a | 7694 | if (mddev_is_clustered(mddev)) |
965400eb | 7695 | md_cluster_ops->resync_start(mddev, j, max_sectors); |
96ae923a | 7696 | |
7c2c57c9 | 7697 | blk_start_plug(&plug); |
1da177e4 | 7698 | while (j < max_sectors) { |
57afd89f | 7699 | sector_t sectors; |
1da177e4 | 7700 | |
57afd89f | 7701 | skipped = 0; |
97e4f42d | 7702 | |
7a91ee1f N |
7703 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
7704 | ((mddev->curr_resync > mddev->curr_resync_completed && | |
7705 | (mddev->curr_resync - mddev->curr_resync_completed) | |
7706 | > (max_sectors >> 4)) || | |
54f89341 | 7707 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
7a91ee1f N |
7708 | (j - mddev->curr_resync_completed)*2 |
7709 | >= mddev->resync_max - mddev->curr_resync_completed | |
7710 | )) { | |
97e4f42d | 7711 | /* time to update curr_resync_completed */ |
97e4f42d N |
7712 | wait_event(mddev->recovery_wait, |
7713 | atomic_read(&mddev->recovery_active) == 0); | |
75d3da43 | 7714 | mddev->curr_resync_completed = j; |
35d78c66 | 7715 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
7716 | j > mddev->recovery_cp) | |
7717 | mddev->recovery_cp = j; | |
54f89341 | 7718 | update_time = jiffies; |
070dc6dd | 7719 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
acb180b0 | 7720 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
97e4f42d | 7721 | } |
acb180b0 | 7722 | |
c91abf5a N |
7723 | while (j >= mddev->resync_max && |
7724 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
e62e58a5 N |
7725 | /* As this condition is controlled by user-space, |
7726 | * we can block indefinitely, so use '_interruptible' | |
7727 | * to avoid triggering warnings. | |
7728 | */ | |
7729 | flush_signals(current); /* just in case */ | |
7730 | wait_event_interruptible(mddev->recovery_wait, | |
7731 | mddev->resync_max > j | |
c91abf5a N |
7732 | || test_bit(MD_RECOVERY_INTR, |
7733 | &mddev->recovery)); | |
e62e58a5 | 7734 | } |
acb180b0 | 7735 | |
c91abf5a N |
7736 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
7737 | break; | |
acb180b0 | 7738 | |
57afd89f | 7739 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
c6207277 | 7740 | currspeed < speed_min(mddev)); |
57afd89f | 7741 | if (sectors == 0) { |
dfc70645 | 7742 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
c91abf5a | 7743 | break; |
1da177e4 | 7744 | } |
57afd89f N |
7745 | |
7746 | if (!skipped) { /* actual IO requested */ | |
7747 | io_sectors += sectors; | |
7748 | atomic_add(sectors, &mddev->recovery_active); | |
7749 | } | |
7750 | ||
e875ecea N |
7751 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
7752 | break; | |
7753 | ||
1da177e4 | 7754 | j += sectors; |
72f36d59 N |
7755 | if (j > 2) |
7756 | mddev->curr_resync = j; | |
96ae923a GR |
7757 | if (mddev_is_clustered(mddev)) |
7758 | md_cluster_ops->resync_info_update(mddev, j, max_sectors); | |
ff4e8d9a | 7759 | mddev->curr_mark_cnt = io_sectors; |
d7603b7e | 7760 | if (last_check == 0) |
e875ecea | 7761 | /* this is the earliest that rebuild will be |
d7603b7e N |
7762 | * visible in /proc/mdstat |
7763 | */ | |
7764 | md_new_event(mddev); | |
57afd89f N |
7765 | |
7766 | if (last_check + window > io_sectors || j == max_sectors) | |
1da177e4 LT |
7767 | continue; |
7768 | ||
57afd89f | 7769 | last_check = io_sectors; |
1da177e4 LT |
7770 | repeat: |
7771 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { | |
7772 | /* step marks */ | |
7773 | int next = (last_mark+1) % SYNC_MARKS; | |
7774 | ||
7775 | mddev->resync_mark = mark[next]; | |
7776 | mddev->resync_mark_cnt = mark_cnt[next]; | |
7777 | mark[next] = jiffies; | |
57afd89f | 7778 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
1da177e4 LT |
7779 | last_mark = next; |
7780 | } | |
7781 | ||
c91abf5a N |
7782 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
7783 | break; | |
1da177e4 LT |
7784 | |
7785 | /* | |
7786 | * this loop exits only if either when we are slower than | |
7787 | * the 'hard' speed limit, or the system was IO-idle for | |
7788 | * a jiffy. | |
7789 | * the system might be non-idle CPU-wise, but we only care | |
7790 | * about not overloading the IO subsystem. (things like an | |
7791 | * e2fsck being done on the RAID array should execute fast) | |
7792 | */ | |
1da177e4 LT |
7793 | cond_resched(); |
7794 | ||
ac7e50a3 XN |
7795 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
7796 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 | |
57afd89f | 7797 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
1da177e4 | 7798 | |
88202a0c N |
7799 | if (currspeed > speed_min(mddev)) { |
7800 | if ((currspeed > speed_max(mddev)) || | |
eea1bf38 | 7801 | !is_mddev_idle(mddev, 0)) { |
c0e48521 | 7802 | msleep(500); |
1da177e4 LT |
7803 | goto repeat; |
7804 | } | |
7805 | } | |
7806 | } | |
c91abf5a N |
7807 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
7808 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) | |
7809 | ? "interrupted" : "done"); | |
1da177e4 LT |
7810 | /* |
7811 | * this also signals 'finished resyncing' to md_stop | |
7812 | */ | |
7c2c57c9 | 7813 | blk_finish_plug(&plug); |
1da177e4 LT |
7814 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
7815 | ||
7816 | /* tell personality that we are finished */ | |
57afd89f | 7817 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
1da177e4 | 7818 | |
96ae923a | 7819 | if (mddev_is_clustered(mddev)) |
965400eb | 7820 | md_cluster_ops->resync_finish(mddev); |
96ae923a | 7821 | |
dfc70645 | 7822 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
5fd6c1dc N |
7823 | mddev->curr_resync > 2) { |
7824 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | |
7825 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
7826 | if (mddev->curr_resync >= mddev->recovery_cp) { | |
7827 | printk(KERN_INFO | |
61df9d91 N |
7828 | "md: checkpointing %s of %s.\n", |
7829 | desc, mdname(mddev)); | |
0a19caab | 7830 | if (test_bit(MD_RECOVERY_ERROR, |
7831 | &mddev->recovery)) | |
7832 | mddev->recovery_cp = | |
7833 | mddev->curr_resync_completed; | |
7834 | else | |
7835 | mddev->recovery_cp = | |
7836 | mddev->curr_resync; | |
5fd6c1dc N |
7837 | } |
7838 | } else | |
7839 | mddev->recovery_cp = MaxSector; | |
7840 | } else { | |
7841 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) | |
7842 | mddev->curr_resync = MaxSector; | |
4e59ca7d | 7843 | rcu_read_lock(); |
dafb20fa | 7844 | rdev_for_each_rcu(rdev, mddev) |
5fd6c1dc | 7845 | if (rdev->raid_disk >= 0 && |
70fffd0b | 7846 | mddev->delta_disks >= 0 && |
5fd6c1dc N |
7847 | !test_bit(Faulty, &rdev->flags) && |
7848 | !test_bit(In_sync, &rdev->flags) && | |
7849 | rdev->recovery_offset < mddev->curr_resync) | |
7850 | rdev->recovery_offset = mddev->curr_resync; | |
4e59ca7d | 7851 | rcu_read_unlock(); |
5fd6c1dc | 7852 | } |
1da177e4 | 7853 | } |
db91ff55 | 7854 | skip: |
17571284 | 7855 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
1da177e4 | 7856 | |
23da422b | 7857 | spin_lock(&mddev->lock); |
c07b70ad N |
7858 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
7859 | /* We completed so min/max setting can be forgotten if used. */ | |
7860 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) | |
7861 | mddev->resync_min = 0; | |
7862 | mddev->resync_max = MaxSector; | |
7863 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) | |
7864 | mddev->resync_min = mddev->curr_resync_completed; | |
1da177e4 | 7865 | mddev->curr_resync = 0; |
23da422b N |
7866 | spin_unlock(&mddev->lock); |
7867 | ||
1da177e4 LT |
7868 | wake_up(&resync_wait); |
7869 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); | |
7870 | md_wakeup_thread(mddev->thread); | |
c6207277 | 7871 | return; |
1da177e4 | 7872 | } |
29269553 | 7873 | EXPORT_SYMBOL_GPL(md_do_sync); |
1da177e4 | 7874 | |
746d3207 N |
7875 | static int remove_and_add_spares(struct mddev *mddev, |
7876 | struct md_rdev *this) | |
b4c4c7b8 | 7877 | { |
3cb03002 | 7878 | struct md_rdev *rdev; |
b4c4c7b8 | 7879 | int spares = 0; |
f2a371c5 | 7880 | int removed = 0; |
b4c4c7b8 | 7881 | |
dafb20fa | 7882 | rdev_for_each(rdev, mddev) |
746d3207 N |
7883 | if ((this == NULL || rdev == this) && |
7884 | rdev->raid_disk >= 0 && | |
6bfe0b49 | 7885 | !test_bit(Blocked, &rdev->flags) && |
b4c4c7b8 N |
7886 | (test_bit(Faulty, &rdev->flags) || |
7887 | ! test_bit(In_sync, &rdev->flags)) && | |
7888 | atomic_read(&rdev->nr_pending)==0) { | |
7889 | if (mddev->pers->hot_remove_disk( | |
b8321b68 | 7890 | mddev, rdev) == 0) { |
36fad858 | 7891 | sysfs_unlink_rdev(mddev, rdev); |
b4c4c7b8 | 7892 | rdev->raid_disk = -1; |
f2a371c5 | 7893 | removed++; |
b4c4c7b8 N |
7894 | } |
7895 | } | |
90584fc9 JB |
7896 | if (removed && mddev->kobj.sd) |
7897 | sysfs_notify(&mddev->kobj, NULL, "degraded"); | |
b4c4c7b8 | 7898 | |
746d3207 N |
7899 | if (this) |
7900 | goto no_add; | |
7901 | ||
dafb20fa | 7902 | rdev_for_each(rdev, mddev) { |
7bfec5f3 N |
7903 | if (rdev->raid_disk >= 0 && |
7904 | !test_bit(In_sync, &rdev->flags) && | |
7905 | !test_bit(Faulty, &rdev->flags)) | |
7906 | spares++; | |
7ceb17e8 N |
7907 | if (rdev->raid_disk >= 0) |
7908 | continue; | |
7909 | if (test_bit(Faulty, &rdev->flags)) | |
7910 | continue; | |
7911 | if (mddev->ro && | |
8313b8e5 N |
7912 | ! (rdev->saved_raid_disk >= 0 && |
7913 | !test_bit(Bitmap_sync, &rdev->flags))) | |
7ceb17e8 N |
7914 | continue; |
7915 | ||
7eb41885 N |
7916 | if (rdev->saved_raid_disk < 0) |
7917 | rdev->recovery_offset = 0; | |
7ceb17e8 N |
7918 | if (mddev->pers-> |
7919 | hot_add_disk(mddev, rdev) == 0) { | |
7920 | if (sysfs_link_rdev(mddev, rdev)) | |
7921 | /* failure here is OK */; | |
7922 | spares++; | |
7923 | md_new_event(mddev); | |
7924 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
dfc70645 | 7925 | } |
b4c4c7b8 | 7926 | } |
746d3207 | 7927 | no_add: |
6dafab6b N |
7928 | if (removed) |
7929 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
b4c4c7b8 N |
7930 | return spares; |
7931 | } | |
7ebc0be7 | 7932 | |
ac05f256 N |
7933 | static void md_start_sync(struct work_struct *ws) |
7934 | { | |
7935 | struct mddev *mddev = container_of(ws, struct mddev, del_work); | |
7936 | ||
7937 | mddev->sync_thread = md_register_thread(md_do_sync, | |
7938 | mddev, | |
7939 | "resync"); | |
7940 | if (!mddev->sync_thread) { | |
7941 | printk(KERN_ERR "%s: could not start resync" | |
7942 | " thread...\n", | |
7943 | mdname(mddev)); | |
7944 | /* leave the spares where they are, it shouldn't hurt */ | |
7945 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
7946 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
7947 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); | |
7948 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
7949 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
f851b60d | 7950 | wake_up(&resync_wait); |
ac05f256 N |
7951 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
7952 | &mddev->recovery)) | |
7953 | if (mddev->sysfs_action) | |
7954 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
7955 | } else | |
7956 | md_wakeup_thread(mddev->sync_thread); | |
7957 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
7958 | md_new_event(mddev); | |
7959 | } | |
7960 | ||
1da177e4 LT |
7961 | /* |
7962 | * This routine is regularly called by all per-raid-array threads to | |
7963 | * deal with generic issues like resync and super-block update. | |
7964 | * Raid personalities that don't have a thread (linear/raid0) do not | |
7965 | * need this as they never do any recovery or update the superblock. | |
7966 | * | |
7967 | * It does not do any resync itself, but rather "forks" off other threads | |
7968 | * to do that as needed. | |
7969 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in | |
7970 | * "->recovery" and create a thread at ->sync_thread. | |
dfc70645 | 7971 | * When the thread finishes it sets MD_RECOVERY_DONE |
1da177e4 LT |
7972 | * and wakeups up this thread which will reap the thread and finish up. |
7973 | * This thread also removes any faulty devices (with nr_pending == 0). | |
7974 | * | |
7975 | * The overall approach is: | |
7976 | * 1/ if the superblock needs updating, update it. | |
7977 | * 2/ If a recovery thread is running, don't do anything else. | |
7978 | * 3/ If recovery has finished, clean up, possibly marking spares active. | |
7979 | * 4/ If there are any faulty devices, remove them. | |
7980 | * 5/ If array is degraded, try to add spares devices | |
7981 | * 6/ If array has spares or is not in-sync, start a resync thread. | |
7982 | */ | |
fd01b88c | 7983 | void md_check_recovery(struct mddev *mddev) |
1da177e4 | 7984 | { |
68866e42 JB |
7985 | if (mddev->suspended) |
7986 | return; | |
7987 | ||
5f40402d | 7988 | if (mddev->bitmap) |
aa5cbd10 | 7989 | bitmap_daemon_work(mddev); |
1da177e4 | 7990 | |
fca4d848 | 7991 | if (signal_pending(current)) { |
31a59e34 | 7992 | if (mddev->pers->sync_request && !mddev->external) { |
fca4d848 N |
7993 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
7994 | mdname(mddev)); | |
7995 | mddev->safemode = 2; | |
7996 | } | |
7997 | flush_signals(current); | |
7998 | } | |
7999 | ||
c89a8eee N |
8000 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
8001 | return; | |
1da177e4 | 8002 | if ( ! ( |
142d44c3 | 8003 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
1da177e4 | 8004 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
fca4d848 | 8005 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
31a59e34 | 8006 | (mddev->external == 0 && mddev->safemode == 1) || |
fca4d848 N |
8007 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
8008 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) | |
1da177e4 LT |
8009 | )) |
8010 | return; | |
fca4d848 | 8011 | |
df5b89b3 | 8012 | if (mddev_trylock(mddev)) { |
b4c4c7b8 | 8013 | int spares = 0; |
fca4d848 | 8014 | |
c89a8eee | 8015 | if (mddev->ro) { |
7ceb17e8 N |
8016 | /* On a read-only array we can: |
8017 | * - remove failed devices | |
8018 | * - add already-in_sync devices if the array itself | |
8019 | * is in-sync. | |
8020 | * As we only add devices that are already in-sync, | |
8021 | * we can activate the spares immediately. | |
c89a8eee | 8022 | */ |
7ceb17e8 | 8023 | remove_and_add_spares(mddev, NULL); |
8313b8e5 N |
8024 | /* There is no thread, but we need to call |
8025 | * ->spare_active and clear saved_raid_disk | |
8026 | */ | |
2ac295a5 | 8027 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
8313b8e5 N |
8028 | md_reap_sync_thread(mddev); |
8029 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
c89a8eee N |
8030 | goto unlock; |
8031 | } | |
8032 | ||
31a59e34 | 8033 | if (!mddev->external) { |
0fd62b86 | 8034 | int did_change = 0; |
85572d7c | 8035 | spin_lock(&mddev->lock); |
31a59e34 N |
8036 | if (mddev->safemode && |
8037 | !atomic_read(&mddev->writes_pending) && | |
8038 | !mddev->in_sync && | |
8039 | mddev->recovery_cp == MaxSector) { | |
8040 | mddev->in_sync = 1; | |
0fd62b86 | 8041 | did_change = 1; |
070dc6dd | 8042 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
31a59e34 N |
8043 | } |
8044 | if (mddev->safemode == 1) | |
8045 | mddev->safemode = 0; | |
85572d7c | 8046 | spin_unlock(&mddev->lock); |
0fd62b86 | 8047 | if (did_change) |
00bcb4ac | 8048 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
fca4d848 | 8049 | } |
fca4d848 | 8050 | |
293467aa GR |
8051 | if (mddev->flags & MD_UPDATE_SB_FLAGS) { |
8052 | if (mddev_is_clustered(mddev)) | |
8053 | md_cluster_ops->metadata_update_start(mddev); | |
850b2b42 | 8054 | md_update_sb(mddev, 0); |
293467aa GR |
8055 | if (mddev_is_clustered(mddev)) |
8056 | md_cluster_ops->metadata_update_finish(mddev); | |
8057 | } | |
06d91a5f | 8058 | |
1da177e4 LT |
8059 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
8060 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { | |
8061 | /* resync/recovery still happening */ | |
8062 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
8063 | goto unlock; | |
8064 | } | |
8065 | if (mddev->sync_thread) { | |
a91d5ac0 | 8066 | md_reap_sync_thread(mddev); |
1da177e4 LT |
8067 | goto unlock; |
8068 | } | |
72a23c21 NB |
8069 | /* Set RUNNING before clearing NEEDED to avoid |
8070 | * any transients in the value of "sync_action". | |
8071 | */ | |
72f36d59 | 8072 | mddev->curr_resync_completed = 0; |
23da422b | 8073 | spin_lock(&mddev->lock); |
72a23c21 | 8074 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
23da422b | 8075 | spin_unlock(&mddev->lock); |
24dd469d N |
8076 | /* Clear some bits that don't mean anything, but |
8077 | * might be left set | |
8078 | */ | |
24dd469d N |
8079 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
8080 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); | |
1da177e4 | 8081 | |
ed209584 N |
8082 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
8083 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) | |
ac05f256 | 8084 | goto not_running; |
1da177e4 LT |
8085 | /* no recovery is running. |
8086 | * remove any failed drives, then | |
8087 | * add spares if possible. | |
72f36d59 | 8088 | * Spares are also removed and re-added, to allow |
1da177e4 LT |
8089 | * the personality to fail the re-add. |
8090 | */ | |
1da177e4 | 8091 | |
b4c4c7b8 | 8092 | if (mddev->reshape_position != MaxSector) { |
50ac168a N |
8093 | if (mddev->pers->check_reshape == NULL || |
8094 | mddev->pers->check_reshape(mddev) != 0) | |
b4c4c7b8 | 8095 | /* Cannot proceed */ |
ac05f256 | 8096 | goto not_running; |
b4c4c7b8 | 8097 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
72a23c21 | 8098 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
746d3207 | 8099 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
24dd469d N |
8100 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
8101 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
56ac36d7 | 8102 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
72a23c21 | 8103 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
24dd469d N |
8104 | } else if (mddev->recovery_cp < MaxSector) { |
8105 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
72a23c21 | 8106 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
24dd469d N |
8107 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
8108 | /* nothing to be done ... */ | |
ac05f256 | 8109 | goto not_running; |
24dd469d | 8110 | |
1da177e4 | 8111 | if (mddev->pers->sync_request) { |
ef99bf48 | 8112 | if (spares) { |
a654b9d8 N |
8113 | /* We are adding a device or devices to an array |
8114 | * which has the bitmap stored on all devices. | |
8115 | * So make sure all bitmap pages get written | |
8116 | */ | |
8117 | bitmap_write_all(mddev->bitmap); | |
8118 | } | |
ac05f256 N |
8119 | INIT_WORK(&mddev->del_work, md_start_sync); |
8120 | queue_work(md_misc_wq, &mddev->del_work); | |
8121 | goto unlock; | |
1da177e4 | 8122 | } |
ac05f256 | 8123 | not_running: |
72a23c21 NB |
8124 | if (!mddev->sync_thread) { |
8125 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
f851b60d | 8126 | wake_up(&resync_wait); |
72a23c21 NB |
8127 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
8128 | &mddev->recovery)) | |
0c3573f1 | 8129 | if (mddev->sysfs_action) |
00bcb4ac | 8130 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
72a23c21 | 8131 | } |
ac05f256 N |
8132 | unlock: |
8133 | wake_up(&mddev->sb_wait); | |
1da177e4 LT |
8134 | mddev_unlock(mddev); |
8135 | } | |
8136 | } | |
6c144d31 | 8137 | EXPORT_SYMBOL(md_check_recovery); |
1da177e4 | 8138 | |
a91d5ac0 JB |
8139 | void md_reap_sync_thread(struct mddev *mddev) |
8140 | { | |
8141 | struct md_rdev *rdev; | |
8142 | ||
8143 | /* resync has finished, collect result */ | |
8144 | md_unregister_thread(&mddev->sync_thread); | |
8145 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && | |
8146 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { | |
8147 | /* success...*/ | |
8148 | /* activate any spares */ | |
8149 | if (mddev->pers->spare_active(mddev)) { | |
8150 | sysfs_notify(&mddev->kobj, NULL, | |
8151 | "degraded"); | |
8152 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
8153 | } | |
8154 | } | |
293467aa GR |
8155 | if (mddev_is_clustered(mddev)) |
8156 | md_cluster_ops->metadata_update_start(mddev); | |
a91d5ac0 JB |
8157 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
8158 | mddev->pers->finish_reshape) | |
8159 | mddev->pers->finish_reshape(mddev); | |
8160 | ||
8161 | /* If array is no-longer degraded, then any saved_raid_disk | |
f466722c | 8162 | * information must be scrapped. |
a91d5ac0 | 8163 | */ |
f466722c N |
8164 | if (!mddev->degraded) |
8165 | rdev_for_each(rdev, mddev) | |
a91d5ac0 JB |
8166 | rdev->saved_raid_disk = -1; |
8167 | ||
8168 | md_update_sb(mddev, 1); | |
293467aa GR |
8169 | if (mddev_is_clustered(mddev)) |
8170 | md_cluster_ops->metadata_update_finish(mddev); | |
a91d5ac0 JB |
8171 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
8172 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
8173 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
8174 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); | |
8175 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
f851b60d | 8176 | wake_up(&resync_wait); |
a91d5ac0 JB |
8177 | /* flag recovery needed just to double check */ |
8178 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
8179 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
8180 | md_new_event(mddev); | |
8181 | if (mddev->event_work.func) | |
8182 | queue_work(md_misc_wq, &mddev->event_work); | |
8183 | } | |
6c144d31 | 8184 | EXPORT_SYMBOL(md_reap_sync_thread); |
a91d5ac0 | 8185 | |
fd01b88c | 8186 | void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev) |
6bfe0b49 | 8187 | { |
00bcb4ac | 8188 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
6bfe0b49 | 8189 | wait_event_timeout(rdev->blocked_wait, |
de393cde N |
8190 | !test_bit(Blocked, &rdev->flags) && |
8191 | !test_bit(BlockedBadBlocks, &rdev->flags), | |
6bfe0b49 DW |
8192 | msecs_to_jiffies(5000)); |
8193 | rdev_dec_pending(rdev, mddev); | |
8194 | } | |
8195 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); | |
8196 | ||
c6563a8c N |
8197 | void md_finish_reshape(struct mddev *mddev) |
8198 | { | |
8199 | /* called be personality module when reshape completes. */ | |
8200 | struct md_rdev *rdev; | |
8201 | ||
8202 | rdev_for_each(rdev, mddev) { | |
8203 | if (rdev->data_offset > rdev->new_data_offset) | |
8204 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; | |
8205 | else | |
8206 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; | |
8207 | rdev->data_offset = rdev->new_data_offset; | |
8208 | } | |
8209 | } | |
8210 | EXPORT_SYMBOL(md_finish_reshape); | |
2230dfe4 N |
8211 | |
8212 | /* Bad block management. | |
8213 | * We can record which blocks on each device are 'bad' and so just | |
8214 | * fail those blocks, or that stripe, rather than the whole device. | |
8215 | * Entries in the bad-block table are 64bits wide. This comprises: | |
8216 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 | |
8217 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) | |
8218 | * A 'shift' can be set so that larger blocks are tracked and | |
8219 | * consequently larger devices can be covered. | |
8220 | * 'Acknowledged' flag - 1 bit. - the most significant bit. | |
8221 | * | |
8222 | * Locking of the bad-block table uses a seqlock so md_is_badblock | |
8223 | * might need to retry if it is very unlucky. | |
8224 | * We will sometimes want to check for bad blocks in a bi_end_io function, | |
8225 | * so we use the write_seqlock_irq variant. | |
8226 | * | |
8227 | * When looking for a bad block we specify a range and want to | |
8228 | * know if any block in the range is bad. So we binary-search | |
8229 | * to the last range that starts at-or-before the given endpoint, | |
8230 | * (or "before the sector after the target range") | |
8231 | * then see if it ends after the given start. | |
8232 | * We return | |
8233 | * 0 if there are no known bad blocks in the range | |
8234 | * 1 if there are known bad block which are all acknowledged | |
8235 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. | |
8236 | * plus the start/length of the first bad section we overlap. | |
8237 | */ | |
8238 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, | |
8239 | sector_t *first_bad, int *bad_sectors) | |
8240 | { | |
8241 | int hi; | |
ab05613a | 8242 | int lo; |
2230dfe4 | 8243 | u64 *p = bb->page; |
ab05613a | 8244 | int rv; |
2230dfe4 N |
8245 | sector_t target = s + sectors; |
8246 | unsigned seq; | |
8247 | ||
8248 | if (bb->shift > 0) { | |
8249 | /* round the start down, and the end up */ | |
8250 | s >>= bb->shift; | |
8251 | target += (1<<bb->shift) - 1; | |
8252 | target >>= bb->shift; | |
8253 | sectors = target - s; | |
8254 | } | |
8255 | /* 'target' is now the first block after the bad range */ | |
8256 | ||
8257 | retry: | |
8258 | seq = read_seqbegin(&bb->lock); | |
ab05613a | 8259 | lo = 0; |
8260 | rv = 0; | |
2230dfe4 N |
8261 | hi = bb->count; |
8262 | ||
8263 | /* Binary search between lo and hi for 'target' | |
8264 | * i.e. for the last range that starts before 'target' | |
8265 | */ | |
8266 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' | |
8267 | * are known not to be the last range before target. | |
8268 | * VARIANT: hi-lo is the number of possible | |
8269 | * ranges, and decreases until it reaches 1 | |
8270 | */ | |
8271 | while (hi - lo > 1) { | |
8272 | int mid = (lo + hi) / 2; | |
8273 | sector_t a = BB_OFFSET(p[mid]); | |
8274 | if (a < target) | |
8275 | /* This could still be the one, earlier ranges | |
8276 | * could not. */ | |
8277 | lo = mid; | |
8278 | else | |
8279 | /* This and later ranges are definitely out. */ | |
8280 | hi = mid; | |
8281 | } | |
8282 | /* 'lo' might be the last that started before target, but 'hi' isn't */ | |
8283 | if (hi > lo) { | |
8284 | /* need to check all range that end after 's' to see if | |
8285 | * any are unacknowledged. | |
8286 | */ | |
8287 | while (lo >= 0 && | |
8288 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { | |
8289 | if (BB_OFFSET(p[lo]) < target) { | |
8290 | /* starts before the end, and finishes after | |
8291 | * the start, so they must overlap | |
8292 | */ | |
8293 | if (rv != -1 && BB_ACK(p[lo])) | |
8294 | rv = 1; | |
8295 | else | |
8296 | rv = -1; | |
8297 | *first_bad = BB_OFFSET(p[lo]); | |
8298 | *bad_sectors = BB_LEN(p[lo]); | |
8299 | } | |
8300 | lo--; | |
8301 | } | |
8302 | } | |
8303 | ||
8304 | if (read_seqretry(&bb->lock, seq)) | |
8305 | goto retry; | |
8306 | ||
8307 | return rv; | |
8308 | } | |
8309 | EXPORT_SYMBOL_GPL(md_is_badblock); | |
8310 | ||
8311 | /* | |
8312 | * Add a range of bad blocks to the table. | |
8313 | * This might extend the table, or might contract it | |
8314 | * if two adjacent ranges can be merged. | |
8315 | * We binary-search to find the 'insertion' point, then | |
8316 | * decide how best to handle it. | |
8317 | */ | |
8318 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, | |
8319 | int acknowledged) | |
8320 | { | |
8321 | u64 *p; | |
8322 | int lo, hi; | |
8323 | int rv = 1; | |
905b0297 | 8324 | unsigned long flags; |
2230dfe4 N |
8325 | |
8326 | if (bb->shift < 0) | |
8327 | /* badblocks are disabled */ | |
8328 | return 0; | |
8329 | ||
8330 | if (bb->shift) { | |
8331 | /* round the start down, and the end up */ | |
8332 | sector_t next = s + sectors; | |
8333 | s >>= bb->shift; | |
8334 | next += (1<<bb->shift) - 1; | |
8335 | next >>= bb->shift; | |
8336 | sectors = next - s; | |
8337 | } | |
8338 | ||
905b0297 | 8339 | write_seqlock_irqsave(&bb->lock, flags); |
2230dfe4 N |
8340 | |
8341 | p = bb->page; | |
8342 | lo = 0; | |
8343 | hi = bb->count; | |
8344 | /* Find the last range that starts at-or-before 's' */ | |
8345 | while (hi - lo > 1) { | |
8346 | int mid = (lo + hi) / 2; | |
8347 | sector_t a = BB_OFFSET(p[mid]); | |
8348 | if (a <= s) | |
8349 | lo = mid; | |
8350 | else | |
8351 | hi = mid; | |
8352 | } | |
8353 | if (hi > lo && BB_OFFSET(p[lo]) > s) | |
8354 | hi = lo; | |
8355 | ||
8356 | if (hi > lo) { | |
8357 | /* we found a range that might merge with the start | |
8358 | * of our new range | |
8359 | */ | |
8360 | sector_t a = BB_OFFSET(p[lo]); | |
8361 | sector_t e = a + BB_LEN(p[lo]); | |
8362 | int ack = BB_ACK(p[lo]); | |
8363 | if (e >= s) { | |
8364 | /* Yes, we can merge with a previous range */ | |
8365 | if (s == a && s + sectors >= e) | |
8366 | /* new range covers old */ | |
8367 | ack = acknowledged; | |
8368 | else | |
8369 | ack = ack && acknowledged; | |
8370 | ||
8371 | if (e < s + sectors) | |
8372 | e = s + sectors; | |
8373 | if (e - a <= BB_MAX_LEN) { | |
8374 | p[lo] = BB_MAKE(a, e-a, ack); | |
8375 | s = e; | |
8376 | } else { | |
8377 | /* does not all fit in one range, | |
8378 | * make p[lo] maximal | |
8379 | */ | |
8380 | if (BB_LEN(p[lo]) != BB_MAX_LEN) | |
8381 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); | |
8382 | s = a + BB_MAX_LEN; | |
8383 | } | |
8384 | sectors = e - s; | |
8385 | } | |
8386 | } | |
8387 | if (sectors && hi < bb->count) { | |
8388 | /* 'hi' points to the first range that starts after 's'. | |
8389 | * Maybe we can merge with the start of that range */ | |
8390 | sector_t a = BB_OFFSET(p[hi]); | |
8391 | sector_t e = a + BB_LEN(p[hi]); | |
8392 | int ack = BB_ACK(p[hi]); | |
8393 | if (a <= s + sectors) { | |
8394 | /* merging is possible */ | |
8395 | if (e <= s + sectors) { | |
8396 | /* full overlap */ | |
8397 | e = s + sectors; | |
8398 | ack = acknowledged; | |
8399 | } else | |
8400 | ack = ack && acknowledged; | |
8401 | ||
8402 | a = s; | |
8403 | if (e - a <= BB_MAX_LEN) { | |
8404 | p[hi] = BB_MAKE(a, e-a, ack); | |
8405 | s = e; | |
8406 | } else { | |
8407 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); | |
8408 | s = a + BB_MAX_LEN; | |
8409 | } | |
8410 | sectors = e - s; | |
8411 | lo = hi; | |
8412 | hi++; | |
8413 | } | |
8414 | } | |
8415 | if (sectors == 0 && hi < bb->count) { | |
8416 | /* we might be able to combine lo and hi */ | |
8417 | /* Note: 's' is at the end of 'lo' */ | |
8418 | sector_t a = BB_OFFSET(p[hi]); | |
8419 | int lolen = BB_LEN(p[lo]); | |
8420 | int hilen = BB_LEN(p[hi]); | |
8421 | int newlen = lolen + hilen - (s - a); | |
8422 | if (s >= a && newlen < BB_MAX_LEN) { | |
8423 | /* yes, we can combine them */ | |
8424 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); | |
8425 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); | |
8426 | memmove(p + hi, p + hi + 1, | |
8427 | (bb->count - hi - 1) * 8); | |
8428 | bb->count--; | |
8429 | } | |
8430 | } | |
8431 | while (sectors) { | |
8432 | /* didn't merge (it all). | |
8433 | * Need to add a range just before 'hi' */ | |
8434 | if (bb->count >= MD_MAX_BADBLOCKS) { | |
8435 | /* No room for more */ | |
8436 | rv = 0; | |
8437 | break; | |
8438 | } else { | |
8439 | int this_sectors = sectors; | |
8440 | memmove(p + hi + 1, p + hi, | |
8441 | (bb->count - hi) * 8); | |
8442 | bb->count++; | |
8443 | ||
8444 | if (this_sectors > BB_MAX_LEN) | |
8445 | this_sectors = BB_MAX_LEN; | |
8446 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); | |
8447 | sectors -= this_sectors; | |
8448 | s += this_sectors; | |
8449 | } | |
8450 | } | |
8451 | ||
8452 | bb->changed = 1; | |
de393cde N |
8453 | if (!acknowledged) |
8454 | bb->unacked_exist = 1; | |
905b0297 | 8455 | write_sequnlock_irqrestore(&bb->lock, flags); |
2230dfe4 N |
8456 | |
8457 | return rv; | |
8458 | } | |
8459 | ||
3cb03002 | 8460 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
c6563a8c | 8461 | int is_new) |
2230dfe4 | 8462 | { |
c6563a8c N |
8463 | int rv; |
8464 | if (is_new) | |
8465 | s += rdev->new_data_offset; | |
8466 | else | |
8467 | s += rdev->data_offset; | |
8468 | rv = md_set_badblocks(&rdev->badblocks, | |
8469 | s, sectors, 0); | |
2230dfe4 N |
8470 | if (rv) { |
8471 | /* Make sure they get written out promptly */ | |
8bd2f0a0 | 8472 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
2230dfe4 N |
8473 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
8474 | md_wakeup_thread(rdev->mddev->thread); | |
8475 | } | |
8476 | return rv; | |
8477 | } | |
8478 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); | |
8479 | ||
8480 | /* | |
8481 | * Remove a range of bad blocks from the table. | |
8482 | * This may involve extending the table if we spilt a region, | |
8483 | * but it must not fail. So if the table becomes full, we just | |
8484 | * drop the remove request. | |
8485 | */ | |
8486 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) | |
8487 | { | |
8488 | u64 *p; | |
8489 | int lo, hi; | |
8490 | sector_t target = s + sectors; | |
8491 | int rv = 0; | |
8492 | ||
8493 | if (bb->shift > 0) { | |
8494 | /* When clearing we round the start up and the end down. | |
8495 | * This should not matter as the shift should align with | |
8496 | * the block size and no rounding should ever be needed. | |
8497 | * However it is better the think a block is bad when it | |
8498 | * isn't than to think a block is not bad when it is. | |
8499 | */ | |
8500 | s += (1<<bb->shift) - 1; | |
8501 | s >>= bb->shift; | |
8502 | target >>= bb->shift; | |
8503 | sectors = target - s; | |
8504 | } | |
8505 | ||
8506 | write_seqlock_irq(&bb->lock); | |
8507 | ||
8508 | p = bb->page; | |
8509 | lo = 0; | |
8510 | hi = bb->count; | |
8511 | /* Find the last range that starts before 'target' */ | |
8512 | while (hi - lo > 1) { | |
8513 | int mid = (lo + hi) / 2; | |
8514 | sector_t a = BB_OFFSET(p[mid]); | |
8515 | if (a < target) | |
8516 | lo = mid; | |
8517 | else | |
8518 | hi = mid; | |
8519 | } | |
8520 | if (hi > lo) { | |
8521 | /* p[lo] is the last range that could overlap the | |
8522 | * current range. Earlier ranges could also overlap, | |
8523 | * but only this one can overlap the end of the range. | |
8524 | */ | |
8525 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { | |
8526 | /* Partial overlap, leave the tail of this range */ | |
8527 | int ack = BB_ACK(p[lo]); | |
8528 | sector_t a = BB_OFFSET(p[lo]); | |
8529 | sector_t end = a + BB_LEN(p[lo]); | |
8530 | ||
8531 | if (a < s) { | |
8532 | /* we need to split this range */ | |
8533 | if (bb->count >= MD_MAX_BADBLOCKS) { | |
8b32bf5e | 8534 | rv = -ENOSPC; |
2230dfe4 N |
8535 | goto out; |
8536 | } | |
8537 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); | |
8538 | bb->count++; | |
8539 | p[lo] = BB_MAKE(a, s-a, ack); | |
8540 | lo++; | |
8541 | } | |
8542 | p[lo] = BB_MAKE(target, end - target, ack); | |
8543 | /* there is no longer an overlap */ | |
8544 | hi = lo; | |
8545 | lo--; | |
8546 | } | |
8547 | while (lo >= 0 && | |
8548 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { | |
8549 | /* This range does overlap */ | |
8550 | if (BB_OFFSET(p[lo]) < s) { | |
8551 | /* Keep the early parts of this range. */ | |
8552 | int ack = BB_ACK(p[lo]); | |
8553 | sector_t start = BB_OFFSET(p[lo]); | |
8554 | p[lo] = BB_MAKE(start, s - start, ack); | |
8555 | /* now low doesn't overlap, so.. */ | |
8556 | break; | |
8557 | } | |
8558 | lo--; | |
8559 | } | |
8560 | /* 'lo' is strictly before, 'hi' is strictly after, | |
8561 | * anything between needs to be discarded | |
8562 | */ | |
8563 | if (hi - lo > 1) { | |
8564 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); | |
8565 | bb->count -= (hi - lo - 1); | |
8566 | } | |
8567 | } | |
8568 | ||
8569 | bb->changed = 1; | |
8570 | out: | |
8571 | write_sequnlock_irq(&bb->lock); | |
8572 | return rv; | |
8573 | } | |
8574 | ||
c6563a8c N |
8575 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
8576 | int is_new) | |
2230dfe4 | 8577 | { |
c6563a8c N |
8578 | if (is_new) |
8579 | s += rdev->new_data_offset; | |
8580 | else | |
8581 | s += rdev->data_offset; | |
2230dfe4 | 8582 | return md_clear_badblocks(&rdev->badblocks, |
c6563a8c | 8583 | s, sectors); |
2230dfe4 N |
8584 | } |
8585 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); | |
8586 | ||
8587 | /* | |
8588 | * Acknowledge all bad blocks in a list. | |
8589 | * This only succeeds if ->changed is clear. It is used by | |
8590 | * in-kernel metadata updates | |
8591 | */ | |
8592 | void md_ack_all_badblocks(struct badblocks *bb) | |
8593 | { | |
8594 | if (bb->page == NULL || bb->changed) | |
8595 | /* no point even trying */ | |
8596 | return; | |
8597 | write_seqlock_irq(&bb->lock); | |
8598 | ||
ecb178bb | 8599 | if (bb->changed == 0 && bb->unacked_exist) { |
2230dfe4 N |
8600 | u64 *p = bb->page; |
8601 | int i; | |
8602 | for (i = 0; i < bb->count ; i++) { | |
8603 | if (!BB_ACK(p[i])) { | |
8604 | sector_t start = BB_OFFSET(p[i]); | |
8605 | int len = BB_LEN(p[i]); | |
8606 | p[i] = BB_MAKE(start, len, 1); | |
8607 | } | |
8608 | } | |
de393cde | 8609 | bb->unacked_exist = 0; |
2230dfe4 N |
8610 | } |
8611 | write_sequnlock_irq(&bb->lock); | |
8612 | } | |
8613 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); | |
8614 | ||
16c791a5 N |
8615 | /* sysfs access to bad-blocks list. |
8616 | * We present two files. | |
8617 | * 'bad-blocks' lists sector numbers and lengths of ranges that | |
8618 | * are recorded as bad. The list is truncated to fit within | |
8619 | * the one-page limit of sysfs. | |
8620 | * Writing "sector length" to this file adds an acknowledged | |
8621 | * bad block list. | |
8622 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet | |
8623 | * been acknowledged. Writing to this file adds bad blocks | |
8624 | * without acknowledging them. This is largely for testing. | |
8625 | */ | |
8626 | ||
8627 | static ssize_t | |
8628 | badblocks_show(struct badblocks *bb, char *page, int unack) | |
8629 | { | |
8630 | size_t len; | |
8631 | int i; | |
8632 | u64 *p = bb->page; | |
8633 | unsigned seq; | |
8634 | ||
8635 | if (bb->shift < 0) | |
8636 | return 0; | |
8637 | ||
8638 | retry: | |
8639 | seq = read_seqbegin(&bb->lock); | |
8640 | ||
8641 | len = 0; | |
8642 | i = 0; | |
8643 | ||
8644 | while (len < PAGE_SIZE && i < bb->count) { | |
8645 | sector_t s = BB_OFFSET(p[i]); | |
8646 | unsigned int length = BB_LEN(p[i]); | |
8647 | int ack = BB_ACK(p[i]); | |
8648 | i++; | |
8649 | ||
8650 | if (unack && ack) | |
8651 | continue; | |
8652 | ||
8653 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", | |
8654 | (unsigned long long)s << bb->shift, | |
8655 | length << bb->shift); | |
8656 | } | |
de393cde N |
8657 | if (unack && len == 0) |
8658 | bb->unacked_exist = 0; | |
16c791a5 N |
8659 | |
8660 | if (read_seqretry(&bb->lock, seq)) | |
8661 | goto retry; | |
8662 | ||
8663 | return len; | |
8664 | } | |
8665 | ||
8666 | #define DO_DEBUG 1 | |
8667 | ||
8668 | static ssize_t | |
8669 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) | |
8670 | { | |
8671 | unsigned long long sector; | |
8672 | int length; | |
8673 | char newline; | |
8674 | #ifdef DO_DEBUG | |
8675 | /* Allow clearing via sysfs *only* for testing/debugging. | |
8676 | * Normally only a successful write may clear a badblock | |
8677 | */ | |
8678 | int clear = 0; | |
8679 | if (page[0] == '-') { | |
8680 | clear = 1; | |
8681 | page++; | |
8682 | } | |
8683 | #endif /* DO_DEBUG */ | |
8684 | ||
8685 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { | |
8686 | case 3: | |
8687 | if (newline != '\n') | |
8688 | return -EINVAL; | |
8689 | case 2: | |
8690 | if (length <= 0) | |
8691 | return -EINVAL; | |
8692 | break; | |
8693 | default: | |
8694 | return -EINVAL; | |
8695 | } | |
8696 | ||
8697 | #ifdef DO_DEBUG | |
8698 | if (clear) { | |
8699 | md_clear_badblocks(bb, sector, length); | |
8700 | return len; | |
8701 | } | |
8702 | #endif /* DO_DEBUG */ | |
8703 | if (md_set_badblocks(bb, sector, length, !unack)) | |
8704 | return len; | |
8705 | else | |
8706 | return -ENOSPC; | |
8707 | } | |
8708 | ||
75c96f85 AB |
8709 | static int md_notify_reboot(struct notifier_block *this, |
8710 | unsigned long code, void *x) | |
1da177e4 LT |
8711 | { |
8712 | struct list_head *tmp; | |
fd01b88c | 8713 | struct mddev *mddev; |
2dba6a91 | 8714 | int need_delay = 0; |
1da177e4 | 8715 | |
c744a65c N |
8716 | for_each_mddev(mddev, tmp) { |
8717 | if (mddev_trylock(mddev)) { | |
30b8aa91 N |
8718 | if (mddev->pers) |
8719 | __md_stop_writes(mddev); | |
0f62fb22 N |
8720 | if (mddev->persistent) |
8721 | mddev->safemode = 2; | |
c744a65c | 8722 | mddev_unlock(mddev); |
2dba6a91 | 8723 | } |
c744a65c | 8724 | need_delay = 1; |
1da177e4 | 8725 | } |
c744a65c N |
8726 | /* |
8727 | * certain more exotic SCSI devices are known to be | |
8728 | * volatile wrt too early system reboots. While the | |
8729 | * right place to handle this issue is the given | |
8730 | * driver, we do want to have a safe RAID driver ... | |
8731 | */ | |
8732 | if (need_delay) | |
8733 | mdelay(1000*1); | |
8734 | ||
1da177e4 LT |
8735 | return NOTIFY_DONE; |
8736 | } | |
8737 | ||
75c96f85 | 8738 | static struct notifier_block md_notifier = { |
1da177e4 LT |
8739 | .notifier_call = md_notify_reboot, |
8740 | .next = NULL, | |
8741 | .priority = INT_MAX, /* before any real devices */ | |
8742 | }; | |
8743 | ||
8744 | static void md_geninit(void) | |
8745 | { | |
36a4e1fe | 8746 | pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
1da177e4 | 8747 | |
c7705f34 | 8748 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
1da177e4 LT |
8749 | } |
8750 | ||
75c96f85 | 8751 | static int __init md_init(void) |
1da177e4 | 8752 | { |
e804ac78 TH |
8753 | int ret = -ENOMEM; |
8754 | ||
ada609ee | 8755 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
e804ac78 TH |
8756 | if (!md_wq) |
8757 | goto err_wq; | |
8758 | ||
8759 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); | |
8760 | if (!md_misc_wq) | |
8761 | goto err_misc_wq; | |
8762 | ||
8763 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) | |
8764 | goto err_md; | |
8765 | ||
8766 | if ((ret = register_blkdev(0, "mdp")) < 0) | |
8767 | goto err_mdp; | |
8768 | mdp_major = ret; | |
8769 | ||
af5628f0 | 8770 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
e8703fe1 N |
8771 | md_probe, NULL, NULL); |
8772 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, | |
1da177e4 LT |
8773 | md_probe, NULL, NULL); |
8774 | ||
1da177e4 | 8775 | register_reboot_notifier(&md_notifier); |
0b4d4147 | 8776 | raid_table_header = register_sysctl_table(raid_root_table); |
1da177e4 LT |
8777 | |
8778 | md_geninit(); | |
d710e138 | 8779 | return 0; |
1da177e4 | 8780 | |
e804ac78 TH |
8781 | err_mdp: |
8782 | unregister_blkdev(MD_MAJOR, "md"); | |
8783 | err_md: | |
8784 | destroy_workqueue(md_misc_wq); | |
8785 | err_misc_wq: | |
8786 | destroy_workqueue(md_wq); | |
8787 | err_wq: | |
8788 | return ret; | |
8789 | } | |
1da177e4 | 8790 | |
1d7e3e96 GR |
8791 | void md_reload_sb(struct mddev *mddev) |
8792 | { | |
8793 | struct md_rdev *rdev, *tmp; | |
8794 | ||
8795 | rdev_for_each_safe(rdev, tmp, mddev) { | |
8796 | rdev->sb_loaded = 0; | |
8797 | ClearPageUptodate(rdev->sb_page); | |
8798 | } | |
8799 | mddev->raid_disks = 0; | |
8800 | analyze_sbs(mddev); | |
8801 | rdev_for_each_safe(rdev, tmp, mddev) { | |
8802 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); | |
8803 | /* since we don't write to faulty devices, we figure out if the | |
8804 | * disk is faulty by comparing events | |
8805 | */ | |
8806 | if (mddev->events > sb->events) | |
8807 | set_bit(Faulty, &rdev->flags); | |
8808 | } | |
8809 | ||
8810 | } | |
8811 | EXPORT_SYMBOL(md_reload_sb); | |
8812 | ||
1da177e4 LT |
8813 | #ifndef MODULE |
8814 | ||
8815 | /* | |
8816 | * Searches all registered partitions for autorun RAID arrays | |
8817 | * at boot time. | |
8818 | */ | |
4d936ec1 ME |
8819 | |
8820 | static LIST_HEAD(all_detected_devices); | |
8821 | struct detected_devices_node { | |
8822 | struct list_head list; | |
8823 | dev_t dev; | |
8824 | }; | |
1da177e4 LT |
8825 | |
8826 | void md_autodetect_dev(dev_t dev) | |
8827 | { | |
4d936ec1 ME |
8828 | struct detected_devices_node *node_detected_dev; |
8829 | ||
8830 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); | |
8831 | if (node_detected_dev) { | |
8832 | node_detected_dev->dev = dev; | |
8833 | list_add_tail(&node_detected_dev->list, &all_detected_devices); | |
8834 | } else { | |
8835 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" | |
8836 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); | |
8837 | } | |
1da177e4 LT |
8838 | } |
8839 | ||
1da177e4 LT |
8840 | static void autostart_arrays(int part) |
8841 | { | |
3cb03002 | 8842 | struct md_rdev *rdev; |
4d936ec1 ME |
8843 | struct detected_devices_node *node_detected_dev; |
8844 | dev_t dev; | |
8845 | int i_scanned, i_passed; | |
1da177e4 | 8846 | |
4d936ec1 ME |
8847 | i_scanned = 0; |
8848 | i_passed = 0; | |
1da177e4 | 8849 | |
4d936ec1 | 8850 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
1da177e4 | 8851 | |
4d936ec1 ME |
8852 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
8853 | i_scanned++; | |
8854 | node_detected_dev = list_entry(all_detected_devices.next, | |
8855 | struct detected_devices_node, list); | |
8856 | list_del(&node_detected_dev->list); | |
8857 | dev = node_detected_dev->dev; | |
8858 | kfree(node_detected_dev); | |
df968c4e | 8859 | rdev = md_import_device(dev,0, 90); |
1da177e4 LT |
8860 | if (IS_ERR(rdev)) |
8861 | continue; | |
8862 | ||
403df478 | 8863 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 | 8864 | continue; |
403df478 | 8865 | |
d0fae18f | 8866 | set_bit(AutoDetected, &rdev->flags); |
1da177e4 | 8867 | list_add(&rdev->same_set, &pending_raid_disks); |
4d936ec1 | 8868 | i_passed++; |
1da177e4 | 8869 | } |
4d936ec1 ME |
8870 | |
8871 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", | |
8872 | i_scanned, i_passed); | |
1da177e4 LT |
8873 | |
8874 | autorun_devices(part); | |
8875 | } | |
8876 | ||
fdee8ae4 | 8877 | #endif /* !MODULE */ |
1da177e4 LT |
8878 | |
8879 | static __exit void md_exit(void) | |
8880 | { | |
fd01b88c | 8881 | struct mddev *mddev; |
1da177e4 | 8882 | struct list_head *tmp; |
e2f23b60 | 8883 | int delay = 1; |
8ab5e4c1 | 8884 | |
af5628f0 | 8885 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
e8703fe1 | 8886 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
1da177e4 | 8887 | |
3dbd8c2e | 8888 | unregister_blkdev(MD_MAJOR,"md"); |
1da177e4 LT |
8889 | unregister_blkdev(mdp_major, "mdp"); |
8890 | unregister_reboot_notifier(&md_notifier); | |
8891 | unregister_sysctl_table(raid_table_header); | |
e2f23b60 N |
8892 | |
8893 | /* We cannot unload the modules while some process is | |
8894 | * waiting for us in select() or poll() - wake them up | |
8895 | */ | |
8896 | md_unloading = 1; | |
8897 | while (waitqueue_active(&md_event_waiters)) { | |
8898 | /* not safe to leave yet */ | |
8899 | wake_up(&md_event_waiters); | |
8900 | msleep(delay); | |
8901 | delay += delay; | |
8902 | } | |
1da177e4 | 8903 | remove_proc_entry("mdstat", NULL); |
e2f23b60 | 8904 | |
29ac4aa3 | 8905 | for_each_mddev(mddev, tmp) { |
1da177e4 | 8906 | export_array(mddev); |
d3374825 | 8907 | mddev->hold_active = 0; |
1da177e4 | 8908 | } |
e804ac78 TH |
8909 | destroy_workqueue(md_misc_wq); |
8910 | destroy_workqueue(md_wq); | |
1da177e4 LT |
8911 | } |
8912 | ||
685784aa | 8913 | subsys_initcall(md_init); |
1da177e4 LT |
8914 | module_exit(md_exit) |
8915 | ||
f91de92e N |
8916 | static int get_ro(char *buffer, struct kernel_param *kp) |
8917 | { | |
8918 | return sprintf(buffer, "%d", start_readonly); | |
8919 | } | |
8920 | static int set_ro(const char *val, struct kernel_param *kp) | |
8921 | { | |
8922 | char *e; | |
8923 | int num = simple_strtoul(val, &e, 10); | |
8924 | if (*val && (*e == '\0' || *e == '\n')) { | |
8925 | start_readonly = num; | |
4dbcdc75 | 8926 | return 0; |
f91de92e N |
8927 | } |
8928 | return -EINVAL; | |
8929 | } | |
8930 | ||
80ca3a44 N |
8931 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
8932 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); | |
efeb53c0 | 8933 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
f91de92e | 8934 | |
1da177e4 | 8935 | MODULE_LICENSE("GPL"); |
0efb9e61 | 8936 | MODULE_DESCRIPTION("MD RAID framework"); |
aa1595e9 | 8937 | MODULE_ALIAS("md"); |
72008652 | 8938 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |