]>
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" |
1da177e4 | 56 | |
1da177e4 | 57 | #ifndef MODULE |
d710e138 | 58 | static void autostart_arrays(int part); |
1da177e4 LT |
59 | #endif |
60 | ||
01f96c0a N |
61 | /* pers_list is a list of registered personalities protected |
62 | * by pers_lock. | |
63 | * pers_lock does extra service to protect accesses to | |
64 | * mddev->thread when the mutex cannot be held. | |
65 | */ | |
2604b703 | 66 | static LIST_HEAD(pers_list); |
1da177e4 LT |
67 | static DEFINE_SPINLOCK(pers_lock); |
68 | ||
5e56341d AB |
69 | static void md_print_devices(void); |
70 | ||
90b08710 | 71 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
e804ac78 TH |
72 | static struct workqueue_struct *md_wq; |
73 | static struct workqueue_struct *md_misc_wq; | |
90b08710 | 74 | |
746d3207 N |
75 | static int remove_and_add_spares(struct mddev *mddev, |
76 | struct md_rdev *this); | |
77 | ||
5e56341d AB |
78 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
79 | ||
1e50915f RB |
80 | /* |
81 | * Default number of read corrections we'll attempt on an rdev | |
82 | * before ejecting it from the array. We divide the read error | |
83 | * count by 2 for every hour elapsed between read errors. | |
84 | */ | |
85 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 | |
1da177e4 LT |
86 | /* |
87 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' | |
88 | * is 1000 KB/sec, so the extra system load does not show up that much. | |
89 | * Increase it if you want to have more _guaranteed_ speed. Note that | |
338cec32 | 90 | * the RAID driver will use the maximum available bandwidth if the IO |
1da177e4 LT |
91 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
92 | * speed limit - in case reconstruction slows down your system despite | |
93 | * idle IO detection. | |
94 | * | |
95 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. | |
88202a0c | 96 | * or /sys/block/mdX/md/sync_speed_{min,max} |
1da177e4 LT |
97 | */ |
98 | ||
99 | static int sysctl_speed_limit_min = 1000; | |
100 | static int sysctl_speed_limit_max = 200000; | |
fd01b88c | 101 | static inline int speed_min(struct mddev *mddev) |
88202a0c N |
102 | { |
103 | return mddev->sync_speed_min ? | |
104 | mddev->sync_speed_min : sysctl_speed_limit_min; | |
105 | } | |
106 | ||
fd01b88c | 107 | static inline int speed_max(struct mddev *mddev) |
88202a0c N |
108 | { |
109 | return mddev->sync_speed_max ? | |
110 | mddev->sync_speed_max : sysctl_speed_limit_max; | |
111 | } | |
1da177e4 LT |
112 | |
113 | static struct ctl_table_header *raid_table_header; | |
114 | ||
82592c38 | 115 | static struct ctl_table raid_table[] = { |
1da177e4 | 116 | { |
1da177e4 LT |
117 | .procname = "speed_limit_min", |
118 | .data = &sysctl_speed_limit_min, | |
119 | .maxlen = sizeof(int), | |
80ca3a44 | 120 | .mode = S_IRUGO|S_IWUSR, |
6d456111 | 121 | .proc_handler = proc_dointvec, |
1da177e4 LT |
122 | }, |
123 | { | |
1da177e4 LT |
124 | .procname = "speed_limit_max", |
125 | .data = &sysctl_speed_limit_max, | |
126 | .maxlen = sizeof(int), | |
80ca3a44 | 127 | .mode = S_IRUGO|S_IWUSR, |
6d456111 | 128 | .proc_handler = proc_dointvec, |
1da177e4 | 129 | }, |
894d2491 | 130 | { } |
1da177e4 LT |
131 | }; |
132 | ||
82592c38 | 133 | static struct ctl_table raid_dir_table[] = { |
1da177e4 | 134 | { |
1da177e4 LT |
135 | .procname = "raid", |
136 | .maxlen = 0, | |
80ca3a44 | 137 | .mode = S_IRUGO|S_IXUGO, |
1da177e4 LT |
138 | .child = raid_table, |
139 | }, | |
894d2491 | 140 | { } |
1da177e4 LT |
141 | }; |
142 | ||
82592c38 | 143 | static struct ctl_table raid_root_table[] = { |
1da177e4 | 144 | { |
1da177e4 LT |
145 | .procname = "dev", |
146 | .maxlen = 0, | |
147 | .mode = 0555, | |
148 | .child = raid_dir_table, | |
149 | }, | |
894d2491 | 150 | { } |
1da177e4 LT |
151 | }; |
152 | ||
83d5cde4 | 153 | static const struct block_device_operations md_fops; |
1da177e4 | 154 | |
f91de92e N |
155 | static int start_readonly; |
156 | ||
a167f663 N |
157 | /* bio_clone_mddev |
158 | * like bio_clone, but with a local bio set | |
159 | */ | |
160 | ||
a167f663 | 161 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
fd01b88c | 162 | struct mddev *mddev) |
a167f663 N |
163 | { |
164 | struct bio *b; | |
a167f663 N |
165 | |
166 | if (!mddev || !mddev->bio_set) | |
167 | return bio_alloc(gfp_mask, nr_iovecs); | |
168 | ||
395c72a7 | 169 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
a167f663 N |
170 | if (!b) |
171 | return NULL; | |
a167f663 N |
172 | return b; |
173 | } | |
174 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); | |
175 | ||
176 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, | |
fd01b88c | 177 | struct mddev *mddev) |
a167f663 | 178 | { |
a167f663 N |
179 | if (!mddev || !mddev->bio_set) |
180 | return bio_clone(bio, gfp_mask); | |
181 | ||
bf800ef1 | 182 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
a167f663 N |
183 | } |
184 | EXPORT_SYMBOL_GPL(bio_clone_mddev); | |
185 | ||
d7603b7e N |
186 | /* |
187 | * We have a system wide 'event count' that is incremented | |
188 | * on any 'interesting' event, and readers of /proc/mdstat | |
189 | * can use 'poll' or 'select' to find out when the event | |
190 | * count increases. | |
191 | * | |
192 | * Events are: | |
193 | * start array, stop array, error, add device, remove device, | |
194 | * start build, activate spare | |
195 | */ | |
2989ddbd | 196 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
d7603b7e | 197 | static atomic_t md_event_count; |
fd01b88c | 198 | void md_new_event(struct mddev *mddev) |
d7603b7e N |
199 | { |
200 | atomic_inc(&md_event_count); | |
201 | wake_up(&md_event_waiters); | |
202 | } | |
29269553 | 203 | EXPORT_SYMBOL_GPL(md_new_event); |
d7603b7e | 204 | |
c331eb04 N |
205 | /* Alternate version that can be called from interrupts |
206 | * when calling sysfs_notify isn't needed. | |
207 | */ | |
fd01b88c | 208 | static void md_new_event_inintr(struct mddev *mddev) |
c331eb04 N |
209 | { |
210 | atomic_inc(&md_event_count); | |
211 | wake_up(&md_event_waiters); | |
212 | } | |
213 | ||
1da177e4 LT |
214 | /* |
215 | * Enables to iterate over all existing md arrays | |
216 | * all_mddevs_lock protects this list. | |
217 | */ | |
218 | static LIST_HEAD(all_mddevs); | |
219 | static DEFINE_SPINLOCK(all_mddevs_lock); | |
220 | ||
1da177e4 LT |
221 | /* |
222 | * iterates through all used mddevs in the system. | |
223 | * We take care to grab the all_mddevs_lock whenever navigating | |
224 | * the list, and to always hold a refcount when unlocked. | |
225 | * Any code which breaks out of this loop while own | |
226 | * a reference to the current mddev and must mddev_put it. | |
227 | */ | |
fd01b88c | 228 | #define for_each_mddev(_mddev,_tmp) \ |
1da177e4 | 229 | \ |
f72ffdd6 | 230 | for (({ spin_lock(&all_mddevs_lock); \ |
fd01b88c N |
231 | _tmp = all_mddevs.next; \ |
232 | _mddev = NULL;}); \ | |
233 | ({ if (_tmp != &all_mddevs) \ | |
234 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ | |
1da177e4 | 235 | spin_unlock(&all_mddevs_lock); \ |
fd01b88c N |
236 | if (_mddev) mddev_put(_mddev); \ |
237 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ | |
238 | _tmp != &all_mddevs;}); \ | |
1da177e4 | 239 | ({ spin_lock(&all_mddevs_lock); \ |
fd01b88c | 240 | _tmp = _tmp->next;}) \ |
1da177e4 LT |
241 | ) |
242 | ||
409c57f3 N |
243 | /* Rather than calling directly into the personality make_request function, |
244 | * IO requests come here first so that we can check if the device is | |
245 | * being suspended pending a reconfiguration. | |
246 | * We hold a refcount over the call to ->make_request. By the time that | |
247 | * call has finished, the bio has been linked into some internal structure | |
248 | * and so is visible to ->quiesce(), so we don't need the refcount any more. | |
249 | */ | |
5a7bbad2 | 250 | static void md_make_request(struct request_queue *q, struct bio *bio) |
1da177e4 | 251 | { |
49077326 | 252 | const int rw = bio_data_dir(bio); |
fd01b88c | 253 | struct mddev *mddev = q->queuedata; |
49077326 | 254 | int cpu; |
e91ece55 | 255 | unsigned int sectors; |
49077326 | 256 | |
0ca69886 N |
257 | if (mddev == NULL || mddev->pers == NULL |
258 | || !mddev->ready) { | |
409c57f3 | 259 | bio_io_error(bio); |
5a7bbad2 | 260 | return; |
409c57f3 | 261 | } |
bbfa57c0 SR |
262 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
263 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); | |
264 | return; | |
265 | } | |
0ca69886 | 266 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
409c57f3 | 267 | rcu_read_lock(); |
e9c7469b | 268 | if (mddev->suspended) { |
409c57f3 N |
269 | DEFINE_WAIT(__wait); |
270 | for (;;) { | |
271 | prepare_to_wait(&mddev->sb_wait, &__wait, | |
272 | TASK_UNINTERRUPTIBLE); | |
e9c7469b | 273 | if (!mddev->suspended) |
409c57f3 N |
274 | break; |
275 | rcu_read_unlock(); | |
276 | schedule(); | |
277 | rcu_read_lock(); | |
278 | } | |
279 | finish_wait(&mddev->sb_wait, &__wait); | |
280 | } | |
281 | atomic_inc(&mddev->active_io); | |
282 | rcu_read_unlock(); | |
49077326 | 283 | |
e91ece55 CM |
284 | /* |
285 | * save the sectors now since our bio can | |
286 | * go away inside make_request | |
287 | */ | |
288 | sectors = bio_sectors(bio); | |
5a7bbad2 | 289 | mddev->pers->make_request(mddev, bio); |
49077326 N |
290 | |
291 | cpu = part_stat_lock(); | |
292 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); | |
e91ece55 | 293 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
49077326 N |
294 | part_stat_unlock(); |
295 | ||
409c57f3 N |
296 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
297 | wake_up(&mddev->sb_wait); | |
409c57f3 N |
298 | } |
299 | ||
9e35b99c N |
300 | /* mddev_suspend makes sure no new requests are submitted |
301 | * to the device, and that any requests that have been submitted | |
302 | * are completely handled. | |
303 | * Once ->stop is called and completes, the module will be completely | |
304 | * unused. | |
305 | */ | |
fd01b88c | 306 | void mddev_suspend(struct mddev *mddev) |
409c57f3 N |
307 | { |
308 | BUG_ON(mddev->suspended); | |
309 | mddev->suspended = 1; | |
310 | synchronize_rcu(); | |
311 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); | |
312 | mddev->pers->quiesce(mddev, 1); | |
0d9f4f13 JB |
313 | |
314 | del_timer_sync(&mddev->safemode_timer); | |
409c57f3 | 315 | } |
390ee602 | 316 | EXPORT_SYMBOL_GPL(mddev_suspend); |
409c57f3 | 317 | |
fd01b88c | 318 | void mddev_resume(struct mddev *mddev) |
409c57f3 N |
319 | { |
320 | mddev->suspended = 0; | |
321 | wake_up(&mddev->sb_wait); | |
322 | mddev->pers->quiesce(mddev, 0); | |
0fd018af | 323 | |
47525e59 | 324 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
0fd018af JB |
325 | md_wakeup_thread(mddev->thread); |
326 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ | |
1da177e4 | 327 | } |
390ee602 | 328 | EXPORT_SYMBOL_GPL(mddev_resume); |
1da177e4 | 329 | |
fd01b88c | 330 | int mddev_congested(struct mddev *mddev, int bits) |
3fa841d7 N |
331 | { |
332 | return mddev->suspended; | |
333 | } | |
334 | EXPORT_SYMBOL(mddev_congested); | |
335 | ||
a2826aa9 | 336 | /* |
e9c7469b | 337 | * Generic flush handling for md |
a2826aa9 N |
338 | */ |
339 | ||
e9c7469b | 340 | static void md_end_flush(struct bio *bio, int err) |
a2826aa9 | 341 | { |
3cb03002 | 342 | struct md_rdev *rdev = bio->bi_private; |
fd01b88c | 343 | struct mddev *mddev = rdev->mddev; |
a2826aa9 N |
344 | |
345 | rdev_dec_pending(rdev, mddev); | |
346 | ||
347 | if (atomic_dec_and_test(&mddev->flush_pending)) { | |
e9c7469b | 348 | /* The pre-request flush has finished */ |
e804ac78 | 349 | queue_work(md_wq, &mddev->flush_work); |
a2826aa9 N |
350 | } |
351 | bio_put(bio); | |
352 | } | |
353 | ||
a7a07e69 N |
354 | static void md_submit_flush_data(struct work_struct *ws); |
355 | ||
a035fc3e | 356 | static void submit_flushes(struct work_struct *ws) |
a2826aa9 | 357 | { |
fd01b88c | 358 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
3cb03002 | 359 | struct md_rdev *rdev; |
a2826aa9 | 360 | |
a7a07e69 N |
361 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
362 | atomic_set(&mddev->flush_pending, 1); | |
a2826aa9 | 363 | rcu_read_lock(); |
dafb20fa | 364 | rdev_for_each_rcu(rdev, mddev) |
a2826aa9 N |
365 | if (rdev->raid_disk >= 0 && |
366 | !test_bit(Faulty, &rdev->flags)) { | |
367 | /* Take two references, one is dropped | |
368 | * when request finishes, one after | |
369 | * we reclaim rcu_read_lock | |
370 | */ | |
371 | struct bio *bi; | |
372 | atomic_inc(&rdev->nr_pending); | |
373 | atomic_inc(&rdev->nr_pending); | |
374 | rcu_read_unlock(); | |
b5e1b8ce | 375 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
e9c7469b | 376 | bi->bi_end_io = md_end_flush; |
a2826aa9 N |
377 | bi->bi_private = rdev; |
378 | bi->bi_bdev = rdev->bdev; | |
379 | atomic_inc(&mddev->flush_pending); | |
e9c7469b | 380 | submit_bio(WRITE_FLUSH, bi); |
a2826aa9 N |
381 | rcu_read_lock(); |
382 | rdev_dec_pending(rdev, mddev); | |
383 | } | |
384 | rcu_read_unlock(); | |
a7a07e69 N |
385 | if (atomic_dec_and_test(&mddev->flush_pending)) |
386 | queue_work(md_wq, &mddev->flush_work); | |
a2826aa9 N |
387 | } |
388 | ||
e9c7469b | 389 | static void md_submit_flush_data(struct work_struct *ws) |
a2826aa9 | 390 | { |
fd01b88c | 391 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
e9c7469b | 392 | struct bio *bio = mddev->flush_bio; |
a2826aa9 | 393 | |
4f024f37 | 394 | if (bio->bi_iter.bi_size == 0) |
a2826aa9 N |
395 | /* an empty barrier - all done */ |
396 | bio_endio(bio, 0); | |
397 | else { | |
e9c7469b | 398 | bio->bi_rw &= ~REQ_FLUSH; |
5a7bbad2 | 399 | mddev->pers->make_request(mddev, bio); |
a2826aa9 | 400 | } |
2b74e12e N |
401 | |
402 | mddev->flush_bio = NULL; | |
403 | wake_up(&mddev->sb_wait); | |
a2826aa9 N |
404 | } |
405 | ||
fd01b88c | 406 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
a2826aa9 N |
407 | { |
408 | spin_lock_irq(&mddev->write_lock); | |
409 | wait_event_lock_irq(mddev->sb_wait, | |
e9c7469b | 410 | !mddev->flush_bio, |
eed8c02e | 411 | mddev->write_lock); |
e9c7469b | 412 | mddev->flush_bio = bio; |
a2826aa9 N |
413 | spin_unlock_irq(&mddev->write_lock); |
414 | ||
a035fc3e N |
415 | INIT_WORK(&mddev->flush_work, submit_flushes); |
416 | queue_work(md_wq, &mddev->flush_work); | |
a2826aa9 | 417 | } |
e9c7469b | 418 | EXPORT_SYMBOL(md_flush_request); |
409c57f3 | 419 | |
74018dc3 | 420 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
97658cdd | 421 | { |
9cbb1750 N |
422 | struct mddev *mddev = cb->data; |
423 | md_wakeup_thread(mddev->thread); | |
424 | kfree(cb); | |
97658cdd | 425 | } |
9cbb1750 | 426 | EXPORT_SYMBOL(md_unplug); |
2ac87401 | 427 | |
fd01b88c | 428 | static inline struct mddev *mddev_get(struct mddev *mddev) |
1da177e4 LT |
429 | { |
430 | atomic_inc(&mddev->active); | |
431 | return mddev; | |
432 | } | |
433 | ||
5fd3a17e | 434 | static void mddev_delayed_delete(struct work_struct *ws); |
d3374825 | 435 | |
fd01b88c | 436 | static void mddev_put(struct mddev *mddev) |
1da177e4 | 437 | { |
a167f663 N |
438 | struct bio_set *bs = NULL; |
439 | ||
1da177e4 LT |
440 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
441 | return; | |
d3374825 | 442 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
cbd19983 N |
443 | mddev->ctime == 0 && !mddev->hold_active) { |
444 | /* Array is not configured at all, and not held active, | |
445 | * so destroy it */ | |
af8a2434 | 446 | list_del_init(&mddev->all_mddevs); |
a167f663 N |
447 | bs = mddev->bio_set; |
448 | mddev->bio_set = NULL; | |
d3374825 | 449 | if (mddev->gendisk) { |
e804ac78 TH |
450 | /* We did a probe so need to clean up. Call |
451 | * queue_work inside the spinlock so that | |
452 | * flush_workqueue() after mddev_find will | |
453 | * succeed in waiting for the work to be done. | |
d3374825 N |
454 | */ |
455 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); | |
e804ac78 | 456 | queue_work(md_misc_wq, &mddev->del_work); |
d3374825 N |
457 | } else |
458 | kfree(mddev); | |
459 | } | |
460 | spin_unlock(&all_mddevs_lock); | |
a167f663 N |
461 | if (bs) |
462 | bioset_free(bs); | |
1da177e4 LT |
463 | } |
464 | ||
fd01b88c | 465 | void mddev_init(struct mddev *mddev) |
fafd7fb0 N |
466 | { |
467 | mutex_init(&mddev->open_mutex); | |
468 | mutex_init(&mddev->reconfig_mutex); | |
469 | mutex_init(&mddev->bitmap_info.mutex); | |
470 | INIT_LIST_HEAD(&mddev->disks); | |
471 | INIT_LIST_HEAD(&mddev->all_mddevs); | |
472 | init_timer(&mddev->safemode_timer); | |
473 | atomic_set(&mddev->active, 1); | |
474 | atomic_set(&mddev->openers, 0); | |
475 | atomic_set(&mddev->active_io, 0); | |
476 | spin_lock_init(&mddev->write_lock); | |
477 | atomic_set(&mddev->flush_pending, 0); | |
478 | init_waitqueue_head(&mddev->sb_wait); | |
479 | init_waitqueue_head(&mddev->recovery_wait); | |
480 | mddev->reshape_position = MaxSector; | |
2c810cdd | 481 | mddev->reshape_backwards = 0; |
c4a39551 | 482 | mddev->last_sync_action = "none"; |
fafd7fb0 N |
483 | mddev->resync_min = 0; |
484 | mddev->resync_max = MaxSector; | |
485 | mddev->level = LEVEL_NONE; | |
486 | } | |
390ee602 | 487 | EXPORT_SYMBOL_GPL(mddev_init); |
fafd7fb0 | 488 | |
f72ffdd6 | 489 | static struct mddev *mddev_find(dev_t unit) |
1da177e4 | 490 | { |
fd01b88c | 491 | struct mddev *mddev, *new = NULL; |
1da177e4 | 492 | |
8f5f02c4 N |
493 | if (unit && MAJOR(unit) != MD_MAJOR) |
494 | unit &= ~((1<<MdpMinorShift)-1); | |
495 | ||
1da177e4 LT |
496 | retry: |
497 | spin_lock(&all_mddevs_lock); | |
efeb53c0 N |
498 | |
499 | if (unit) { | |
500 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) | |
501 | if (mddev->unit == unit) { | |
502 | mddev_get(mddev); | |
503 | spin_unlock(&all_mddevs_lock); | |
504 | kfree(new); | |
505 | return mddev; | |
506 | } | |
507 | ||
508 | if (new) { | |
509 | list_add(&new->all_mddevs, &all_mddevs); | |
1da177e4 | 510 | spin_unlock(&all_mddevs_lock); |
efeb53c0 N |
511 | new->hold_active = UNTIL_IOCTL; |
512 | return new; | |
1da177e4 | 513 | } |
efeb53c0 N |
514 | } else if (new) { |
515 | /* find an unused unit number */ | |
516 | static int next_minor = 512; | |
517 | int start = next_minor; | |
518 | int is_free = 0; | |
519 | int dev = 0; | |
520 | while (!is_free) { | |
521 | dev = MKDEV(MD_MAJOR, next_minor); | |
522 | next_minor++; | |
523 | if (next_minor > MINORMASK) | |
524 | next_minor = 0; | |
525 | if (next_minor == start) { | |
526 | /* Oh dear, all in use. */ | |
527 | spin_unlock(&all_mddevs_lock); | |
528 | kfree(new); | |
529 | return NULL; | |
530 | } | |
f72ffdd6 | 531 | |
efeb53c0 N |
532 | is_free = 1; |
533 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) | |
534 | if (mddev->unit == dev) { | |
535 | is_free = 0; | |
536 | break; | |
537 | } | |
538 | } | |
539 | new->unit = dev; | |
540 | new->md_minor = MINOR(dev); | |
541 | new->hold_active = UNTIL_STOP; | |
1da177e4 LT |
542 | list_add(&new->all_mddevs, &all_mddevs); |
543 | spin_unlock(&all_mddevs_lock); | |
544 | return new; | |
545 | } | |
546 | spin_unlock(&all_mddevs_lock); | |
547 | ||
9ffae0cf | 548 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
1da177e4 LT |
549 | if (!new) |
550 | return NULL; | |
551 | ||
1da177e4 LT |
552 | new->unit = unit; |
553 | if (MAJOR(unit) == MD_MAJOR) | |
554 | new->md_minor = MINOR(unit); | |
555 | else | |
556 | new->md_minor = MINOR(unit) >> MdpMinorShift; | |
557 | ||
fafd7fb0 | 558 | mddev_init(new); |
1da177e4 | 559 | |
1da177e4 LT |
560 | goto retry; |
561 | } | |
562 | ||
f72ffdd6 | 563 | static inline int __must_check mddev_lock(struct mddev *mddev) |
1da177e4 | 564 | { |
df5b89b3 | 565 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
1da177e4 LT |
566 | } |
567 | ||
29f097c4 N |
568 | /* Sometimes we need to take the lock in a situation where |
569 | * failure due to interrupts is not acceptable. | |
570 | */ | |
f72ffdd6 | 571 | static inline void mddev_lock_nointr(struct mddev *mddev) |
29f097c4 N |
572 | { |
573 | mutex_lock(&mddev->reconfig_mutex); | |
574 | } | |
575 | ||
fd01b88c | 576 | static inline int mddev_is_locked(struct mddev *mddev) |
b522adcd DW |
577 | { |
578 | return mutex_is_locked(&mddev->reconfig_mutex); | |
579 | } | |
580 | ||
f72ffdd6 | 581 | static inline int mddev_trylock(struct mddev *mddev) |
1da177e4 | 582 | { |
df5b89b3 | 583 | return mutex_trylock(&mddev->reconfig_mutex); |
1da177e4 LT |
584 | } |
585 | ||
b6eb127d N |
586 | static struct attribute_group md_redundancy_group; |
587 | ||
f72ffdd6 | 588 | static void mddev_unlock(struct mddev *mddev) |
1da177e4 | 589 | { |
a64c876f | 590 | if (mddev->to_remove) { |
b6eb127d N |
591 | /* These cannot be removed under reconfig_mutex as |
592 | * an access to the files will try to take reconfig_mutex | |
593 | * while holding the file unremovable, which leads to | |
594 | * a deadlock. | |
bb4f1e9d N |
595 | * So hold set sysfs_active while the remove in happeing, |
596 | * and anything else which might set ->to_remove or my | |
597 | * otherwise change the sysfs namespace will fail with | |
598 | * -EBUSY if sysfs_active is still set. | |
599 | * We set sysfs_active under reconfig_mutex and elsewhere | |
600 | * test it under the same mutex to ensure its correct value | |
601 | * is seen. | |
b6eb127d | 602 | */ |
a64c876f N |
603 | struct attribute_group *to_remove = mddev->to_remove; |
604 | mddev->to_remove = NULL; | |
bb4f1e9d | 605 | mddev->sysfs_active = 1; |
b6eb127d N |
606 | mutex_unlock(&mddev->reconfig_mutex); |
607 | ||
00bcb4ac N |
608 | if (mddev->kobj.sd) { |
609 | if (to_remove != &md_redundancy_group) | |
610 | sysfs_remove_group(&mddev->kobj, to_remove); | |
611 | if (mddev->pers == NULL || | |
612 | mddev->pers->sync_request == NULL) { | |
613 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); | |
614 | if (mddev->sysfs_action) | |
615 | sysfs_put(mddev->sysfs_action); | |
616 | mddev->sysfs_action = NULL; | |
617 | } | |
a64c876f | 618 | } |
bb4f1e9d | 619 | mddev->sysfs_active = 0; |
b6eb127d N |
620 | } else |
621 | mutex_unlock(&mddev->reconfig_mutex); | |
1da177e4 | 622 | |
751e67ca CD |
623 | /* As we've dropped the mutex we need a spinlock to |
624 | * make sure the thread doesn't disappear | |
01f96c0a N |
625 | */ |
626 | spin_lock(&pers_lock); | |
005eca5e | 627 | md_wakeup_thread(mddev->thread); |
01f96c0a | 628 | spin_unlock(&pers_lock); |
1da177e4 LT |
629 | } |
630 | ||
1ca69c4b N |
631 | static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr) |
632 | { | |
633 | struct md_rdev *rdev; | |
634 | ||
635 | rdev_for_each_rcu(rdev, mddev) | |
636 | if (rdev->desc_nr == nr) | |
637 | return rdev; | |
638 | ||
639 | return NULL; | |
640 | } | |
641 | ||
642 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) | |
1da177e4 | 643 | { |
3cb03002 | 644 | struct md_rdev *rdev; |
1da177e4 | 645 | |
dafb20fa | 646 | rdev_for_each(rdev, mddev) |
1da177e4 LT |
647 | if (rdev->bdev->bd_dev == dev) |
648 | return rdev; | |
159ec1fc | 649 | |
1da177e4 LT |
650 | return NULL; |
651 | } | |
652 | ||
1ca69c4b N |
653 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
654 | { | |
655 | struct md_rdev *rdev; | |
656 | ||
657 | rdev_for_each_rcu(rdev, mddev) | |
658 | if (rdev->bdev->bd_dev == dev) | |
659 | return rdev; | |
660 | ||
661 | return NULL; | |
662 | } | |
663 | ||
84fc4b56 | 664 | static struct md_personality *find_pers(int level, char *clevel) |
2604b703 | 665 | { |
84fc4b56 | 666 | struct md_personality *pers; |
d9d166c2 N |
667 | list_for_each_entry(pers, &pers_list, list) { |
668 | if (level != LEVEL_NONE && pers->level == level) | |
2604b703 | 669 | return pers; |
d9d166c2 N |
670 | if (strcmp(pers->name, clevel)==0) |
671 | return pers; | |
672 | } | |
2604b703 N |
673 | return NULL; |
674 | } | |
675 | ||
b73df2d3 | 676 | /* return the offset of the super block in 512byte sectors */ |
3cb03002 | 677 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
1da177e4 | 678 | { |
57b2caa3 | 679 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
b73df2d3 | 680 | return MD_NEW_SIZE_SECTORS(num_sectors); |
1da177e4 LT |
681 | } |
682 | ||
f72ffdd6 | 683 | static int alloc_disk_sb(struct md_rdev *rdev) |
1da177e4 LT |
684 | { |
685 | if (rdev->sb_page) | |
686 | MD_BUG(); | |
687 | ||
688 | rdev->sb_page = alloc_page(GFP_KERNEL); | |
689 | if (!rdev->sb_page) { | |
690 | printk(KERN_ALERT "md: out of memory.\n"); | |
ebc24337 | 691 | return -ENOMEM; |
1da177e4 LT |
692 | } |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
545c8795 | 697 | void md_rdev_clear(struct md_rdev *rdev) |
1da177e4 LT |
698 | { |
699 | if (rdev->sb_page) { | |
2d1f3b5d | 700 | put_page(rdev->sb_page); |
1da177e4 LT |
701 | rdev->sb_loaded = 0; |
702 | rdev->sb_page = NULL; | |
0f420358 | 703 | rdev->sb_start = 0; |
dd8ac336 | 704 | rdev->sectors = 0; |
1da177e4 | 705 | } |
2699b672 N |
706 | if (rdev->bb_page) { |
707 | put_page(rdev->bb_page); | |
708 | rdev->bb_page = NULL; | |
709 | } | |
4fa2f327 N |
710 | kfree(rdev->badblocks.page); |
711 | rdev->badblocks.page = NULL; | |
1da177e4 | 712 | } |
545c8795 | 713 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
1da177e4 | 714 | |
6712ecf8 | 715 | static void super_written(struct bio *bio, int error) |
7bfa19f2 | 716 | { |
3cb03002 | 717 | struct md_rdev *rdev = bio->bi_private; |
fd01b88c | 718 | struct mddev *mddev = rdev->mddev; |
7bfa19f2 | 719 | |
3a0f5bbb N |
720 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
721 | printk("md: super_written gets error=%d, uptodate=%d\n", | |
722 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); | |
723 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); | |
a9701a30 | 724 | md_error(mddev, rdev); |
3a0f5bbb | 725 | } |
7bfa19f2 | 726 | |
a9701a30 N |
727 | if (atomic_dec_and_test(&mddev->pending_writes)) |
728 | wake_up(&mddev->sb_wait); | |
f8b58edf | 729 | bio_put(bio); |
7bfa19f2 N |
730 | } |
731 | ||
fd01b88c | 732 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
7bfa19f2 N |
733 | sector_t sector, int size, struct page *page) |
734 | { | |
735 | /* write first size bytes of page to sector of rdev | |
736 | * Increment mddev->pending_writes before returning | |
737 | * and decrement it on completion, waking up sb_wait | |
738 | * if zero is reached. | |
739 | * If an error occurred, call md_error | |
740 | */ | |
a167f663 | 741 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
7bfa19f2 | 742 | |
a6ff7e08 | 743 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
4f024f37 | 744 | bio->bi_iter.bi_sector = sector; |
7bfa19f2 N |
745 | bio_add_page(bio, page, size, 0); |
746 | bio->bi_private = rdev; | |
747 | bio->bi_end_io = super_written; | |
a9701a30 | 748 | |
7bfa19f2 | 749 | atomic_inc(&mddev->pending_writes); |
a5bf4df0 | 750 | submit_bio(WRITE_FLUSH_FUA, bio); |
a9701a30 N |
751 | } |
752 | ||
fd01b88c | 753 | void md_super_wait(struct mddev *mddev) |
a9701a30 | 754 | { |
e9c7469b | 755 | /* wait for all superblock writes that were scheduled to complete */ |
1967cd56 | 756 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
7bfa19f2 N |
757 | } |
758 | ||
3cb03002 | 759 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
ccebd4c4 | 760 | struct page *page, int rw, bool metadata_op) |
1da177e4 | 761 | { |
a167f663 | 762 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
1da177e4 LT |
763 | int ret; |
764 | ||
a6ff7e08 JB |
765 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
766 | rdev->meta_bdev : rdev->bdev; | |
ccebd4c4 | 767 | if (metadata_op) |
4f024f37 | 768 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
1fdd6fc9 N |
769 | else if (rdev->mddev->reshape_position != MaxSector && |
770 | (rdev->mddev->reshape_backwards == | |
771 | (sector >= rdev->mddev->reshape_position))) | |
4f024f37 | 772 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
ccebd4c4 | 773 | else |
4f024f37 | 774 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
1da177e4 | 775 | bio_add_page(bio, page, size, 0); |
c170bbb4 | 776 | submit_bio_wait(rw, bio); |
1da177e4 LT |
777 | |
778 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
779 | bio_put(bio); | |
780 | return ret; | |
781 | } | |
a8745db2 | 782 | EXPORT_SYMBOL_GPL(sync_page_io); |
1da177e4 | 783 | |
f72ffdd6 | 784 | static int read_disk_sb(struct md_rdev *rdev, int size) |
1da177e4 LT |
785 | { |
786 | char b[BDEVNAME_SIZE]; | |
787 | if (!rdev->sb_page) { | |
788 | MD_BUG(); | |
789 | return -EINVAL; | |
790 | } | |
791 | if (rdev->sb_loaded) | |
792 | return 0; | |
793 | ||
ccebd4c4 | 794 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
1da177e4 LT |
795 | goto fail; |
796 | rdev->sb_loaded = 1; | |
797 | return 0; | |
798 | ||
799 | fail: | |
800 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", | |
801 | bdevname(rdev->bdev,b)); | |
802 | return -EINVAL; | |
803 | } | |
804 | ||
805 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) | |
806 | { | |
f72ffdd6 | 807 | return sb1->set_uuid0 == sb2->set_uuid0 && |
05710466 AN |
808 | sb1->set_uuid1 == sb2->set_uuid1 && |
809 | sb1->set_uuid2 == sb2->set_uuid2 && | |
810 | sb1->set_uuid3 == sb2->set_uuid3; | |
1da177e4 LT |
811 | } |
812 | ||
1da177e4 LT |
813 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
814 | { | |
815 | int ret; | |
816 | mdp_super_t *tmp1, *tmp2; | |
817 | ||
818 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); | |
819 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); | |
820 | ||
821 | if (!tmp1 || !tmp2) { | |
822 | ret = 0; | |
35020f1a | 823 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
1da177e4 LT |
824 | goto abort; |
825 | } | |
826 | ||
827 | *tmp1 = *sb1; | |
828 | *tmp2 = *sb2; | |
829 | ||
830 | /* | |
831 | * nr_disks is not constant | |
832 | */ | |
833 | tmp1->nr_disks = 0; | |
834 | tmp2->nr_disks = 0; | |
835 | ||
ce0c8e05 | 836 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
1da177e4 | 837 | abort: |
990a8baf JJ |
838 | kfree(tmp1); |
839 | kfree(tmp2); | |
1da177e4 LT |
840 | return ret; |
841 | } | |
842 | ||
4d167f09 N |
843 | static u32 md_csum_fold(u32 csum) |
844 | { | |
845 | csum = (csum & 0xffff) + (csum >> 16); | |
846 | return (csum & 0xffff) + (csum >> 16); | |
847 | } | |
848 | ||
f72ffdd6 | 849 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
1da177e4 | 850 | { |
4d167f09 N |
851 | u64 newcsum = 0; |
852 | u32 *sb32 = (u32*)sb; | |
853 | int i; | |
1da177e4 LT |
854 | unsigned int disk_csum, csum; |
855 | ||
856 | disk_csum = sb->sb_csum; | |
857 | sb->sb_csum = 0; | |
4d167f09 N |
858 | |
859 | for (i = 0; i < MD_SB_BYTES/4 ; i++) | |
860 | newcsum += sb32[i]; | |
861 | csum = (newcsum & 0xffffffff) + (newcsum>>32); | |
862 | ||
4d167f09 N |
863 | #ifdef CONFIG_ALPHA |
864 | /* This used to use csum_partial, which was wrong for several | |
865 | * reasons including that different results are returned on | |
866 | * different architectures. It isn't critical that we get exactly | |
867 | * the same return value as before (we always csum_fold before | |
868 | * testing, and that removes any differences). However as we | |
869 | * know that csum_partial always returned a 16bit value on | |
870 | * alphas, do a fold to maximise conformity to previous behaviour. | |
871 | */ | |
872 | sb->sb_csum = md_csum_fold(disk_csum); | |
873 | #else | |
1da177e4 | 874 | sb->sb_csum = disk_csum; |
4d167f09 | 875 | #endif |
1da177e4 LT |
876 | return csum; |
877 | } | |
878 | ||
1da177e4 LT |
879 | /* |
880 | * Handle superblock details. | |
881 | * We want to be able to handle multiple superblock formats | |
882 | * so we have a common interface to them all, and an array of | |
883 | * different handlers. | |
884 | * We rely on user-space to write the initial superblock, and support | |
885 | * reading and updating of superblocks. | |
886 | * Interface methods are: | |
3cb03002 | 887 | * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
888 | * loads and validates a superblock on dev. |
889 | * if refdev != NULL, compare superblocks on both devices | |
890 | * Return: | |
891 | * 0 - dev has a superblock that is compatible with refdev | |
892 | * 1 - dev has a superblock that is compatible and newer than refdev | |
893 | * so dev should be used as the refdev in future | |
894 | * -EINVAL superblock incompatible or invalid | |
895 | * -othererror e.g. -EIO | |
896 | * | |
fd01b88c | 897 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
1da177e4 LT |
898 | * Verify that dev is acceptable into mddev. |
899 | * The first time, mddev->raid_disks will be 0, and data from | |
900 | * dev should be merged in. Subsequent calls check that dev | |
901 | * is new enough. Return 0 or -EINVAL | |
902 | * | |
fd01b88c | 903 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
1da177e4 LT |
904 | * Update the superblock for rdev with data in mddev |
905 | * This does not write to disc. | |
906 | * | |
907 | */ | |
908 | ||
909 | struct super_type { | |
0cd17fec CW |
910 | char *name; |
911 | struct module *owner; | |
c6563a8c N |
912 | int (*load_super)(struct md_rdev *rdev, |
913 | struct md_rdev *refdev, | |
0cd17fec | 914 | int minor_version); |
c6563a8c N |
915 | int (*validate_super)(struct mddev *mddev, |
916 | struct md_rdev *rdev); | |
917 | void (*sync_super)(struct mddev *mddev, | |
918 | struct md_rdev *rdev); | |
3cb03002 | 919 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
15f4a5fd | 920 | sector_t num_sectors); |
c6563a8c N |
921 | int (*allow_new_offset)(struct md_rdev *rdev, |
922 | unsigned long long new_offset); | |
1da177e4 LT |
923 | }; |
924 | ||
0894cc30 AN |
925 | /* |
926 | * Check that the given mddev has no bitmap. | |
927 | * | |
928 | * This function is called from the run method of all personalities that do not | |
929 | * support bitmaps. It prints an error message and returns non-zero if mddev | |
930 | * has a bitmap. Otherwise, it returns 0. | |
931 | * | |
932 | */ | |
fd01b88c | 933 | int md_check_no_bitmap(struct mddev *mddev) |
0894cc30 | 934 | { |
c3d9714e | 935 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
0894cc30 AN |
936 | return 0; |
937 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", | |
938 | mdname(mddev), mddev->pers->name); | |
939 | return 1; | |
940 | } | |
941 | EXPORT_SYMBOL(md_check_no_bitmap); | |
942 | ||
1da177e4 | 943 | /* |
f72ffdd6 | 944 | * load_super for 0.90.0 |
1da177e4 | 945 | */ |
3cb03002 | 946 | static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
947 | { |
948 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
949 | mdp_super_t *sb; | |
950 | int ret; | |
1da177e4 LT |
951 | |
952 | /* | |
0f420358 | 953 | * Calculate the position of the superblock (512byte sectors), |
1da177e4 LT |
954 | * it's at the end of the disk. |
955 | * | |
956 | * It also happens to be a multiple of 4Kb. | |
957 | */ | |
57b2caa3 | 958 | rdev->sb_start = calc_dev_sboffset(rdev); |
1da177e4 | 959 | |
0002b271 | 960 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
1da177e4 LT |
961 | if (ret) return ret; |
962 | ||
963 | ret = -EINVAL; | |
964 | ||
965 | bdevname(rdev->bdev, b); | |
65a06f06 | 966 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
967 | |
968 | if (sb->md_magic != MD_SB_MAGIC) { | |
969 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", | |
970 | b); | |
971 | goto abort; | |
972 | } | |
973 | ||
974 | if (sb->major_version != 0 || | |
f6705578 N |
975 | sb->minor_version < 90 || |
976 | sb->minor_version > 91) { | |
1da177e4 LT |
977 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
978 | sb->major_version, sb->minor_version, | |
979 | b); | |
980 | goto abort; | |
981 | } | |
982 | ||
983 | if (sb->raid_disks <= 0) | |
984 | goto abort; | |
985 | ||
4d167f09 | 986 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
1da177e4 LT |
987 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
988 | b); | |
989 | goto abort; | |
990 | } | |
991 | ||
992 | rdev->preferred_minor = sb->md_minor; | |
993 | rdev->data_offset = 0; | |
c6563a8c | 994 | rdev->new_data_offset = 0; |
0002b271 | 995 | rdev->sb_size = MD_SB_BYTES; |
9f2f3830 | 996 | rdev->badblocks.shift = -1; |
1da177e4 LT |
997 | |
998 | if (sb->level == LEVEL_MULTIPATH) | |
999 | rdev->desc_nr = -1; | |
1000 | else | |
1001 | rdev->desc_nr = sb->this_disk.number; | |
1002 | ||
9a7b2b0f | 1003 | if (!refdev) { |
1da177e4 | 1004 | ret = 1; |
9a7b2b0f | 1005 | } else { |
1da177e4 | 1006 | __u64 ev1, ev2; |
65a06f06 | 1007 | mdp_super_t *refsb = page_address(refdev->sb_page); |
1da177e4 LT |
1008 | if (!uuid_equal(refsb, sb)) { |
1009 | printk(KERN_WARNING "md: %s has different UUID to %s\n", | |
1010 | b, bdevname(refdev->bdev,b2)); | |
1011 | goto abort; | |
1012 | } | |
1013 | if (!sb_equal(refsb, sb)) { | |
1014 | printk(KERN_WARNING "md: %s has same UUID" | |
1015 | " but different superblock to %s\n", | |
1016 | b, bdevname(refdev->bdev, b2)); | |
1017 | goto abort; | |
1018 | } | |
1019 | ev1 = md_event(sb); | |
1020 | ev2 = md_event(refsb); | |
1021 | if (ev1 > ev2) | |
1022 | ret = 1; | |
f72ffdd6 | 1023 | else |
1da177e4 LT |
1024 | ret = 0; |
1025 | } | |
8190e754 | 1026 | rdev->sectors = rdev->sb_start; |
667a5313 N |
1027 | /* Limit to 4TB as metadata cannot record more than that. |
1028 | * (not needed for Linear and RAID0 as metadata doesn't | |
1029 | * record this size) | |
1030 | */ | |
1031 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) | |
27a7b260 | 1032 | rdev->sectors = (2ULL << 32) - 2; |
1da177e4 | 1033 | |
27a7b260 | 1034 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
2bf071bf N |
1035 | /* "this cannot possibly happen" ... */ |
1036 | ret = -EINVAL; | |
1037 | ||
1da177e4 LT |
1038 | abort: |
1039 | return ret; | |
1040 | } | |
1041 | ||
1042 | /* | |
1043 | * validate_super for 0.90.0 | |
1044 | */ | |
fd01b88c | 1045 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1046 | { |
1047 | mdp_disk_t *desc; | |
65a06f06 | 1048 | mdp_super_t *sb = page_address(rdev->sb_page); |
07d84d10 | 1049 | __u64 ev1 = md_event(sb); |
1da177e4 | 1050 | |
41158c7e | 1051 | rdev->raid_disk = -1; |
c5d79adb N |
1052 | clear_bit(Faulty, &rdev->flags); |
1053 | clear_bit(In_sync, &rdev->flags); | |
8313b8e5 | 1054 | clear_bit(Bitmap_sync, &rdev->flags); |
c5d79adb | 1055 | clear_bit(WriteMostly, &rdev->flags); |
c5d79adb | 1056 | |
1da177e4 LT |
1057 | if (mddev->raid_disks == 0) { |
1058 | mddev->major_version = 0; | |
1059 | mddev->minor_version = sb->minor_version; | |
1060 | mddev->patch_version = sb->patch_version; | |
e691063a | 1061 | mddev->external = 0; |
9d8f0363 | 1062 | mddev->chunk_sectors = sb->chunk_size >> 9; |
1da177e4 LT |
1063 | mddev->ctime = sb->ctime; |
1064 | mddev->utime = sb->utime; | |
1065 | mddev->level = sb->level; | |
d9d166c2 | 1066 | mddev->clevel[0] = 0; |
1da177e4 LT |
1067 | mddev->layout = sb->layout; |
1068 | mddev->raid_disks = sb->raid_disks; | |
27a7b260 | 1069 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
07d84d10 | 1070 | mddev->events = ev1; |
c3d9714e | 1071 | mddev->bitmap_info.offset = 0; |
6409bb05 N |
1072 | mddev->bitmap_info.space = 0; |
1073 | /* bitmap can use 60 K after the 4K superblocks */ | |
c3d9714e | 1074 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
6409bb05 | 1075 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
2c810cdd | 1076 | mddev->reshape_backwards = 0; |
1da177e4 | 1077 | |
f6705578 N |
1078 | if (mddev->minor_version >= 91) { |
1079 | mddev->reshape_position = sb->reshape_position; | |
1080 | mddev->delta_disks = sb->delta_disks; | |
1081 | mddev->new_level = sb->new_level; | |
1082 | mddev->new_layout = sb->new_layout; | |
664e7c41 | 1083 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
2c810cdd N |
1084 | if (mddev->delta_disks < 0) |
1085 | mddev->reshape_backwards = 1; | |
f6705578 N |
1086 | } else { |
1087 | mddev->reshape_position = MaxSector; | |
1088 | mddev->delta_disks = 0; | |
1089 | mddev->new_level = mddev->level; | |
1090 | mddev->new_layout = mddev->layout; | |
664e7c41 | 1091 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
1092 | } |
1093 | ||
1da177e4 LT |
1094 | if (sb->state & (1<<MD_SB_CLEAN)) |
1095 | mddev->recovery_cp = MaxSector; | |
1096 | else { | |
f72ffdd6 | 1097 | if (sb->events_hi == sb->cp_events_hi && |
1da177e4 LT |
1098 | sb->events_lo == sb->cp_events_lo) { |
1099 | mddev->recovery_cp = sb->recovery_cp; | |
1100 | } else | |
1101 | mddev->recovery_cp = 0; | |
1102 | } | |
1103 | ||
1104 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); | |
1105 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); | |
1106 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); | |
1107 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); | |
1108 | ||
1109 | mddev->max_disks = MD_SB_DISKS; | |
a654b9d8 N |
1110 | |
1111 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && | |
6409bb05 | 1112 | mddev->bitmap_info.file == NULL) { |
c3d9714e N |
1113 | mddev->bitmap_info.offset = |
1114 | mddev->bitmap_info.default_offset; | |
6409bb05 | 1115 | mddev->bitmap_info.space = |
c9ad020f | 1116 | mddev->bitmap_info.default_space; |
6409bb05 | 1117 | } |
a654b9d8 | 1118 | |
41158c7e | 1119 | } else if (mddev->pers == NULL) { |
be6800a7 N |
1120 | /* Insist on good event counter while assembling, except |
1121 | * for spares (which don't need an event count) */ | |
1da177e4 | 1122 | ++ev1; |
be6800a7 N |
1123 | if (sb->disks[rdev->desc_nr].state & ( |
1124 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) | |
f72ffdd6 | 1125 | if (ev1 < mddev->events) |
be6800a7 | 1126 | return -EINVAL; |
41158c7e N |
1127 | } else if (mddev->bitmap) { |
1128 | /* if adding to array with a bitmap, then we can accept an | |
1129 | * older device ... but not too old. | |
1130 | */ | |
41158c7e N |
1131 | if (ev1 < mddev->bitmap->events_cleared) |
1132 | return 0; | |
8313b8e5 N |
1133 | if (ev1 < mddev->events) |
1134 | set_bit(Bitmap_sync, &rdev->flags); | |
07d84d10 N |
1135 | } else { |
1136 | if (ev1 < mddev->events) | |
1137 | /* just a hot-add of a new device, leave raid_disk at -1 */ | |
1138 | return 0; | |
1139 | } | |
41158c7e | 1140 | |
1da177e4 | 1141 | if (mddev->level != LEVEL_MULTIPATH) { |
1da177e4 LT |
1142 | desc = sb->disks + rdev->desc_nr; |
1143 | ||
1144 | if (desc->state & (1<<MD_DISK_FAULTY)) | |
b2d444d7 | 1145 | set_bit(Faulty, &rdev->flags); |
7c7546cc N |
1146 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
1147 | desc->raid_disk < mddev->raid_disks */) { | |
b2d444d7 | 1148 | set_bit(In_sync, &rdev->flags); |
1da177e4 | 1149 | rdev->raid_disk = desc->raid_disk; |
f466722c | 1150 | rdev->saved_raid_disk = desc->raid_disk; |
0261cd9f N |
1151 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
1152 | /* active but not in sync implies recovery up to | |
1153 | * reshape position. We don't know exactly where | |
1154 | * that is, so set to zero for now */ | |
1155 | if (mddev->minor_version >= 91) { | |
1156 | rdev->recovery_offset = 0; | |
1157 | rdev->raid_disk = desc->raid_disk; | |
1158 | } | |
1da177e4 | 1159 | } |
8ddf9efe N |
1160 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
1161 | set_bit(WriteMostly, &rdev->flags); | |
41158c7e | 1162 | } else /* MULTIPATH are always insync */ |
b2d444d7 | 1163 | set_bit(In_sync, &rdev->flags); |
1da177e4 LT |
1164 | return 0; |
1165 | } | |
1166 | ||
1167 | /* | |
1168 | * sync_super for 0.90.0 | |
1169 | */ | |
fd01b88c | 1170 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1171 | { |
1172 | mdp_super_t *sb; | |
3cb03002 | 1173 | struct md_rdev *rdev2; |
1da177e4 | 1174 | int next_spare = mddev->raid_disks; |
19133a42 | 1175 | |
1da177e4 LT |
1176 | /* make rdev->sb match mddev data.. |
1177 | * | |
1178 | * 1/ zero out disks | |
1179 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); | |
1180 | * 3/ any empty disks < next_spare become removed | |
1181 | * | |
1182 | * disks[0] gets initialised to REMOVED because | |
1183 | * we cannot be sure from other fields if it has | |
1184 | * been initialised or not. | |
1185 | */ | |
1186 | int i; | |
1187 | int active=0, working=0,failed=0,spare=0,nr_disks=0; | |
1188 | ||
61181565 N |
1189 | rdev->sb_size = MD_SB_BYTES; |
1190 | ||
65a06f06 | 1191 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1192 | |
1193 | memset(sb, 0, sizeof(*sb)); | |
1194 | ||
1195 | sb->md_magic = MD_SB_MAGIC; | |
1196 | sb->major_version = mddev->major_version; | |
1da177e4 LT |
1197 | sb->patch_version = mddev->patch_version; |
1198 | sb->gvalid_words = 0; /* ignored */ | |
1199 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); | |
1200 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); | |
1201 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); | |
1202 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); | |
1203 | ||
1204 | sb->ctime = mddev->ctime; | |
1205 | sb->level = mddev->level; | |
58c0fed4 | 1206 | sb->size = mddev->dev_sectors / 2; |
1da177e4 LT |
1207 | sb->raid_disks = mddev->raid_disks; |
1208 | sb->md_minor = mddev->md_minor; | |
e691063a | 1209 | sb->not_persistent = 0; |
1da177e4 LT |
1210 | sb->utime = mddev->utime; |
1211 | sb->state = 0; | |
1212 | sb->events_hi = (mddev->events>>32); | |
1213 | sb->events_lo = (u32)mddev->events; | |
1214 | ||
f6705578 N |
1215 | if (mddev->reshape_position == MaxSector) |
1216 | sb->minor_version = 90; | |
1217 | else { | |
1218 | sb->minor_version = 91; | |
1219 | sb->reshape_position = mddev->reshape_position; | |
1220 | sb->new_level = mddev->new_level; | |
1221 | sb->delta_disks = mddev->delta_disks; | |
1222 | sb->new_layout = mddev->new_layout; | |
664e7c41 | 1223 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
f6705578 N |
1224 | } |
1225 | mddev->minor_version = sb->minor_version; | |
1da177e4 LT |
1226 | if (mddev->in_sync) |
1227 | { | |
1228 | sb->recovery_cp = mddev->recovery_cp; | |
1229 | sb->cp_events_hi = (mddev->events>>32); | |
1230 | sb->cp_events_lo = (u32)mddev->events; | |
1231 | if (mddev->recovery_cp == MaxSector) | |
1232 | sb->state = (1<< MD_SB_CLEAN); | |
1233 | } else | |
1234 | sb->recovery_cp = 0; | |
1235 | ||
1236 | sb->layout = mddev->layout; | |
9d8f0363 | 1237 | sb->chunk_size = mddev->chunk_sectors << 9; |
1da177e4 | 1238 | |
c3d9714e | 1239 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
a654b9d8 N |
1240 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
1241 | ||
1da177e4 | 1242 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
dafb20fa | 1243 | rdev_for_each(rdev2, mddev) { |
1da177e4 | 1244 | mdp_disk_t *d; |
86e6ffdd | 1245 | int desc_nr; |
0261cd9f N |
1246 | int is_active = test_bit(In_sync, &rdev2->flags); |
1247 | ||
1248 | if (rdev2->raid_disk >= 0 && | |
1249 | sb->minor_version >= 91) | |
1250 | /* we have nowhere to store the recovery_offset, | |
1251 | * but if it is not below the reshape_position, | |
1252 | * we can piggy-back on that. | |
1253 | */ | |
1254 | is_active = 1; | |
1255 | if (rdev2->raid_disk < 0 || | |
1256 | test_bit(Faulty, &rdev2->flags)) | |
1257 | is_active = 0; | |
1258 | if (is_active) | |
86e6ffdd | 1259 | desc_nr = rdev2->raid_disk; |
1da177e4 | 1260 | else |
86e6ffdd | 1261 | desc_nr = next_spare++; |
19133a42 | 1262 | rdev2->desc_nr = desc_nr; |
1da177e4 LT |
1263 | d = &sb->disks[rdev2->desc_nr]; |
1264 | nr_disks++; | |
1265 | d->number = rdev2->desc_nr; | |
1266 | d->major = MAJOR(rdev2->bdev->bd_dev); | |
1267 | d->minor = MINOR(rdev2->bdev->bd_dev); | |
0261cd9f | 1268 | if (is_active) |
1da177e4 LT |
1269 | d->raid_disk = rdev2->raid_disk; |
1270 | else | |
1271 | d->raid_disk = rdev2->desc_nr; /* compatibility */ | |
1be7892f | 1272 | if (test_bit(Faulty, &rdev2->flags)) |
1da177e4 | 1273 | d->state = (1<<MD_DISK_FAULTY); |
0261cd9f | 1274 | else if (is_active) { |
1da177e4 | 1275 | d->state = (1<<MD_DISK_ACTIVE); |
0261cd9f N |
1276 | if (test_bit(In_sync, &rdev2->flags)) |
1277 | d->state |= (1<<MD_DISK_SYNC); | |
1da177e4 LT |
1278 | active++; |
1279 | working++; | |
1280 | } else { | |
1281 | d->state = 0; | |
1282 | spare++; | |
1283 | working++; | |
1284 | } | |
8ddf9efe N |
1285 | if (test_bit(WriteMostly, &rdev2->flags)) |
1286 | d->state |= (1<<MD_DISK_WRITEMOSTLY); | |
1da177e4 | 1287 | } |
1da177e4 LT |
1288 | /* now set the "removed" and "faulty" bits on any missing devices */ |
1289 | for (i=0 ; i < mddev->raid_disks ; i++) { | |
1290 | mdp_disk_t *d = &sb->disks[i]; | |
1291 | if (d->state == 0 && d->number == 0) { | |
1292 | d->number = i; | |
1293 | d->raid_disk = i; | |
1294 | d->state = (1<<MD_DISK_REMOVED); | |
1295 | d->state |= (1<<MD_DISK_FAULTY); | |
1296 | failed++; | |
1297 | } | |
1298 | } | |
1299 | sb->nr_disks = nr_disks; | |
1300 | sb->active_disks = active; | |
1301 | sb->working_disks = working; | |
1302 | sb->failed_disks = failed; | |
1303 | sb->spare_disks = spare; | |
1304 | ||
1305 | sb->this_disk = sb->disks[rdev->desc_nr]; | |
1306 | sb->sb_csum = calc_sb_csum(sb); | |
1307 | } | |
1308 | ||
0cd17fec CW |
1309 | /* |
1310 | * rdev_size_change for 0.90.0 | |
1311 | */ | |
1312 | static unsigned long long | |
3cb03002 | 1313 | super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
0cd17fec | 1314 | { |
58c0fed4 | 1315 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
0cd17fec | 1316 | return 0; /* component must fit device */ |
c3d9714e | 1317 | if (rdev->mddev->bitmap_info.offset) |
0cd17fec | 1318 | return 0; /* can't move bitmap */ |
57b2caa3 | 1319 | rdev->sb_start = calc_dev_sboffset(rdev); |
15f4a5fd AN |
1320 | if (!num_sectors || num_sectors > rdev->sb_start) |
1321 | num_sectors = rdev->sb_start; | |
27a7b260 N |
1322 | /* Limit to 4TB as metadata cannot record more than that. |
1323 | * 4TB == 2^32 KB, or 2*2^32 sectors. | |
1324 | */ | |
667a5313 | 1325 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
27a7b260 | 1326 | num_sectors = (2ULL << 32) - 2; |
0f420358 | 1327 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
0cd17fec CW |
1328 | rdev->sb_page); |
1329 | md_super_wait(rdev->mddev); | |
c26a44ed | 1330 | return num_sectors; |
0cd17fec CW |
1331 | } |
1332 | ||
c6563a8c N |
1333 | static int |
1334 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) | |
1335 | { | |
1336 | /* non-zero offset changes not possible with v0.90 */ | |
1337 | return new_offset == 0; | |
1338 | } | |
0cd17fec | 1339 | |
1da177e4 LT |
1340 | /* |
1341 | * version 1 superblock | |
1342 | */ | |
1343 | ||
f72ffdd6 | 1344 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
1da177e4 | 1345 | { |
1c05b4bc N |
1346 | __le32 disk_csum; |
1347 | u32 csum; | |
1da177e4 LT |
1348 | unsigned long long newcsum; |
1349 | int size = 256 + le32_to_cpu(sb->max_dev)*2; | |
1c05b4bc | 1350 | __le32 *isuper = (__le32*)sb; |
1da177e4 LT |
1351 | |
1352 | disk_csum = sb->sb_csum; | |
1353 | sb->sb_csum = 0; | |
1354 | newcsum = 0; | |
1f3c9907 | 1355 | for (; size >= 4; size -= 4) |
1da177e4 LT |
1356 | newcsum += le32_to_cpu(*isuper++); |
1357 | ||
1358 | if (size == 2) | |
1c05b4bc | 1359 | newcsum += le16_to_cpu(*(__le16*) isuper); |
1da177e4 LT |
1360 | |
1361 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); | |
1362 | sb->sb_csum = disk_csum; | |
1363 | return cpu_to_le32(csum); | |
1364 | } | |
1365 | ||
2699b672 N |
1366 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
1367 | int acknowledged); | |
3cb03002 | 1368 | static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
1da177e4 LT |
1369 | { |
1370 | struct mdp_superblock_1 *sb; | |
1371 | int ret; | |
0f420358 | 1372 | sector_t sb_start; |
c6563a8c | 1373 | sector_t sectors; |
1da177e4 | 1374 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
0002b271 | 1375 | int bmask; |
1da177e4 LT |
1376 | |
1377 | /* | |
0f420358 | 1378 | * Calculate the position of the superblock in 512byte sectors. |
1da177e4 LT |
1379 | * It is always aligned to a 4K boundary and |
1380 | * depeding on minor_version, it can be: | |
1381 | * 0: At least 8K, but less than 12K, from end of device | |
1382 | * 1: At start of device | |
1383 | * 2: 4K from start of device. | |
1384 | */ | |
1385 | switch(minor_version) { | |
1386 | case 0: | |
77304d2a | 1387 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
0f420358 AN |
1388 | sb_start -= 8*2; |
1389 | sb_start &= ~(sector_t)(4*2-1); | |
1da177e4 LT |
1390 | break; |
1391 | case 1: | |
0f420358 | 1392 | sb_start = 0; |
1da177e4 LT |
1393 | break; |
1394 | case 2: | |
0f420358 | 1395 | sb_start = 8; |
1da177e4 LT |
1396 | break; |
1397 | default: | |
1398 | return -EINVAL; | |
1399 | } | |
0f420358 | 1400 | rdev->sb_start = sb_start; |
1da177e4 | 1401 | |
0002b271 N |
1402 | /* superblock is rarely larger than 1K, but it can be larger, |
1403 | * and it is safe to read 4k, so we do that | |
1404 | */ | |
1405 | ret = read_disk_sb(rdev, 4096); | |
1da177e4 LT |
1406 | if (ret) return ret; |
1407 | ||
65a06f06 | 1408 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1409 | |
1410 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || | |
1411 | sb->major_version != cpu_to_le32(1) || | |
1412 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || | |
0f420358 | 1413 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
71c0805c | 1414 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
1da177e4 LT |
1415 | return -EINVAL; |
1416 | ||
1417 | if (calc_sb_1_csum(sb) != sb->sb_csum) { | |
1418 | printk("md: invalid superblock checksum on %s\n", | |
1419 | bdevname(rdev->bdev,b)); | |
1420 | return -EINVAL; | |
1421 | } | |
1422 | if (le64_to_cpu(sb->data_size) < 10) { | |
1423 | printk("md: data_size too small on %s\n", | |
1424 | bdevname(rdev->bdev,b)); | |
1425 | return -EINVAL; | |
1426 | } | |
c6563a8c N |
1427 | if (sb->pad0 || |
1428 | sb->pad3[0] || | |
1429 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) | |
1430 | /* Some padding is non-zero, might be a new feature */ | |
1431 | return -EINVAL; | |
e11e93fa | 1432 | |
1da177e4 LT |
1433 | rdev->preferred_minor = 0xffff; |
1434 | rdev->data_offset = le64_to_cpu(sb->data_offset); | |
c6563a8c N |
1435 | rdev->new_data_offset = rdev->data_offset; |
1436 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && | |
1437 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) | |
1438 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); | |
4dbcdc75 | 1439 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
1da177e4 | 1440 | |
0002b271 | 1441 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
e1defc4f | 1442 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
0002b271 | 1443 | if (rdev->sb_size & bmask) |
a1801f85 N |
1444 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
1445 | ||
1446 | if (minor_version | |
0f420358 | 1447 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
a1801f85 | 1448 | return -EINVAL; |
c6563a8c N |
1449 | if (minor_version |
1450 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) | |
1451 | return -EINVAL; | |
0002b271 | 1452 | |
31b65a0d N |
1453 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
1454 | rdev->desc_nr = -1; | |
1455 | else | |
1456 | rdev->desc_nr = le32_to_cpu(sb->dev_number); | |
1457 | ||
2699b672 N |
1458 | if (!rdev->bb_page) { |
1459 | rdev->bb_page = alloc_page(GFP_KERNEL); | |
1460 | if (!rdev->bb_page) | |
1461 | return -ENOMEM; | |
1462 | } | |
1463 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && | |
1464 | rdev->badblocks.count == 0) { | |
1465 | /* need to load the bad block list. | |
1466 | * Currently we limit it to one page. | |
1467 | */ | |
1468 | s32 offset; | |
1469 | sector_t bb_sector; | |
1470 | u64 *bbp; | |
1471 | int i; | |
1472 | int sectors = le16_to_cpu(sb->bblog_size); | |
1473 | if (sectors > (PAGE_SIZE / 512)) | |
1474 | return -EINVAL; | |
1475 | offset = le32_to_cpu(sb->bblog_offset); | |
1476 | if (offset == 0) | |
1477 | return -EINVAL; | |
1478 | bb_sector = (long long)offset; | |
1479 | if (!sync_page_io(rdev, bb_sector, sectors << 9, | |
1480 | rdev->bb_page, READ, true)) | |
1481 | return -EIO; | |
1482 | bbp = (u64 *)page_address(rdev->bb_page); | |
1483 | rdev->badblocks.shift = sb->bblog_shift; | |
1484 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { | |
1485 | u64 bb = le64_to_cpu(*bbp); | |
1486 | int count = bb & (0x3ff); | |
1487 | u64 sector = bb >> 10; | |
1488 | sector <<= sb->bblog_shift; | |
1489 | count <<= sb->bblog_shift; | |
1490 | if (bb + 1 == 0) | |
1491 | break; | |
1492 | if (md_set_badblocks(&rdev->badblocks, | |
1493 | sector, count, 1) == 0) | |
1494 | return -EINVAL; | |
1495 | } | |
486adf72 N |
1496 | } else if (sb->bblog_offset != 0) |
1497 | rdev->badblocks.shift = 0; | |
2699b672 | 1498 | |
9a7b2b0f | 1499 | if (!refdev) { |
8ed75463 | 1500 | ret = 1; |
9a7b2b0f | 1501 | } else { |
1da177e4 | 1502 | __u64 ev1, ev2; |
65a06f06 | 1503 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
1da177e4 LT |
1504 | |
1505 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || | |
1506 | sb->level != refsb->level || | |
1507 | sb->layout != refsb->layout || | |
1508 | sb->chunksize != refsb->chunksize) { | |
1509 | printk(KERN_WARNING "md: %s has strangely different" | |
1510 | " superblock to %s\n", | |
1511 | bdevname(rdev->bdev,b), | |
1512 | bdevname(refdev->bdev,b2)); | |
1513 | return -EINVAL; | |
1514 | } | |
1515 | ev1 = le64_to_cpu(sb->events); | |
1516 | ev2 = le64_to_cpu(refsb->events); | |
1517 | ||
1518 | if (ev1 > ev2) | |
8ed75463 N |
1519 | ret = 1; |
1520 | else | |
1521 | ret = 0; | |
1da177e4 | 1522 | } |
c6563a8c N |
1523 | if (minor_version) { |
1524 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); | |
1525 | sectors -= rdev->data_offset; | |
1526 | } else | |
1527 | sectors = rdev->sb_start; | |
1528 | if (sectors < le64_to_cpu(sb->data_size)) | |
1da177e4 | 1529 | return -EINVAL; |
dd8ac336 | 1530 | rdev->sectors = le64_to_cpu(sb->data_size); |
8ed75463 | 1531 | return ret; |
1da177e4 LT |
1532 | } |
1533 | ||
fd01b88c | 1534 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1535 | { |
65a06f06 | 1536 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
07d84d10 | 1537 | __u64 ev1 = le64_to_cpu(sb->events); |
1da177e4 | 1538 | |
41158c7e | 1539 | rdev->raid_disk = -1; |
c5d79adb N |
1540 | clear_bit(Faulty, &rdev->flags); |
1541 | clear_bit(In_sync, &rdev->flags); | |
8313b8e5 | 1542 | clear_bit(Bitmap_sync, &rdev->flags); |
c5d79adb | 1543 | clear_bit(WriteMostly, &rdev->flags); |
c5d79adb | 1544 | |
1da177e4 LT |
1545 | if (mddev->raid_disks == 0) { |
1546 | mddev->major_version = 1; | |
1547 | mddev->patch_version = 0; | |
e691063a | 1548 | mddev->external = 0; |
9d8f0363 | 1549 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
1da177e4 LT |
1550 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
1551 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); | |
1552 | mddev->level = le32_to_cpu(sb->level); | |
d9d166c2 | 1553 | mddev->clevel[0] = 0; |
1da177e4 LT |
1554 | mddev->layout = le32_to_cpu(sb->layout); |
1555 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); | |
58c0fed4 | 1556 | mddev->dev_sectors = le64_to_cpu(sb->size); |
07d84d10 | 1557 | mddev->events = ev1; |
c3d9714e | 1558 | mddev->bitmap_info.offset = 0; |
6409bb05 N |
1559 | mddev->bitmap_info.space = 0; |
1560 | /* Default location for bitmap is 1K after superblock | |
1561 | * using 3K - total of 4K | |
1562 | */ | |
c3d9714e | 1563 | mddev->bitmap_info.default_offset = 1024 >> 9; |
6409bb05 | 1564 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
2c810cdd N |
1565 | mddev->reshape_backwards = 0; |
1566 | ||
1da177e4 LT |
1567 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
1568 | memcpy(mddev->uuid, sb->set_uuid, 16); | |
1569 | ||
1570 | mddev->max_disks = (4096-256)/2; | |
a654b9d8 | 1571 | |
71c0805c | 1572 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
6409bb05 | 1573 | mddev->bitmap_info.file == NULL) { |
c3d9714e N |
1574 | mddev->bitmap_info.offset = |
1575 | (__s32)le32_to_cpu(sb->bitmap_offset); | |
6409bb05 N |
1576 | /* Metadata doesn't record how much space is available. |
1577 | * For 1.0, we assume we can use up to the superblock | |
1578 | * if before, else to 4K beyond superblock. | |
1579 | * For others, assume no change is possible. | |
1580 | */ | |
1581 | if (mddev->minor_version > 0) | |
1582 | mddev->bitmap_info.space = 0; | |
1583 | else if (mddev->bitmap_info.offset > 0) | |
1584 | mddev->bitmap_info.space = | |
1585 | 8 - mddev->bitmap_info.offset; | |
1586 | else | |
1587 | mddev->bitmap_info.space = | |
1588 | -mddev->bitmap_info.offset; | |
1589 | } | |
e11e93fa | 1590 | |
f6705578 N |
1591 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
1592 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); | |
1593 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); | |
1594 | mddev->new_level = le32_to_cpu(sb->new_level); | |
1595 | mddev->new_layout = le32_to_cpu(sb->new_layout); | |
664e7c41 | 1596 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
2c810cdd N |
1597 | if (mddev->delta_disks < 0 || |
1598 | (mddev->delta_disks == 0 && | |
1599 | (le32_to_cpu(sb->feature_map) | |
1600 | & MD_FEATURE_RESHAPE_BACKWARDS))) | |
1601 | mddev->reshape_backwards = 1; | |
f6705578 N |
1602 | } else { |
1603 | mddev->reshape_position = MaxSector; | |
1604 | mddev->delta_disks = 0; | |
1605 | mddev->new_level = mddev->level; | |
1606 | mddev->new_layout = mddev->layout; | |
664e7c41 | 1607 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
1608 | } |
1609 | ||
41158c7e | 1610 | } else if (mddev->pers == NULL) { |
be6800a7 N |
1611 | /* Insist of good event counter while assembling, except for |
1612 | * spares (which don't need an event count) */ | |
1da177e4 | 1613 | ++ev1; |
be6800a7 N |
1614 | if (rdev->desc_nr >= 0 && |
1615 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && | |
1616 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) | |
1617 | if (ev1 < mddev->events) | |
1618 | return -EINVAL; | |
41158c7e N |
1619 | } else if (mddev->bitmap) { |
1620 | /* If adding to array with a bitmap, then we can accept an | |
1621 | * older device, but not too old. | |
1622 | */ | |
41158c7e N |
1623 | if (ev1 < mddev->bitmap->events_cleared) |
1624 | return 0; | |
8313b8e5 N |
1625 | if (ev1 < mddev->events) |
1626 | set_bit(Bitmap_sync, &rdev->flags); | |
07d84d10 N |
1627 | } else { |
1628 | if (ev1 < mddev->events) | |
1629 | /* just a hot-add of a new device, leave raid_disk at -1 */ | |
1630 | return 0; | |
1631 | } | |
1da177e4 LT |
1632 | if (mddev->level != LEVEL_MULTIPATH) { |
1633 | int role; | |
3673f305 N |
1634 | if (rdev->desc_nr < 0 || |
1635 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { | |
1636 | role = 0xffff; | |
1637 | rdev->desc_nr = -1; | |
1638 | } else | |
1639 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); | |
1da177e4 LT |
1640 | switch(role) { |
1641 | case 0xffff: /* spare */ | |
1da177e4 LT |
1642 | break; |
1643 | case 0xfffe: /* faulty */ | |
b2d444d7 | 1644 | set_bit(Faulty, &rdev->flags); |
1da177e4 LT |
1645 | break; |
1646 | default: | |
f466722c | 1647 | rdev->saved_raid_disk = role; |
5fd6c1dc | 1648 | if ((le32_to_cpu(sb->feature_map) & |
f466722c | 1649 | MD_FEATURE_RECOVERY_OFFSET)) { |
5fd6c1dc | 1650 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
f466722c N |
1651 | if (!(le32_to_cpu(sb->feature_map) & |
1652 | MD_FEATURE_RECOVERY_BITMAP)) | |
1653 | rdev->saved_raid_disk = -1; | |
1654 | } else | |
5fd6c1dc | 1655 | set_bit(In_sync, &rdev->flags); |
1da177e4 LT |
1656 | rdev->raid_disk = role; |
1657 | break; | |
1658 | } | |
8ddf9efe N |
1659 | if (sb->devflags & WriteMostly1) |
1660 | set_bit(WriteMostly, &rdev->flags); | |
2d78f8c4 N |
1661 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
1662 | set_bit(Replacement, &rdev->flags); | |
41158c7e | 1663 | } else /* MULTIPATH are always insync */ |
b2d444d7 | 1664 | set_bit(In_sync, &rdev->flags); |
41158c7e | 1665 | |
1da177e4 LT |
1666 | return 0; |
1667 | } | |
1668 | ||
fd01b88c | 1669 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1670 | { |
1671 | struct mdp_superblock_1 *sb; | |
3cb03002 | 1672 | struct md_rdev *rdev2; |
1da177e4 LT |
1673 | int max_dev, i; |
1674 | /* make rdev->sb match mddev and rdev data. */ | |
1675 | ||
65a06f06 | 1676 | sb = page_address(rdev->sb_page); |
1da177e4 LT |
1677 | |
1678 | sb->feature_map = 0; | |
1679 | sb->pad0 = 0; | |
5fd6c1dc | 1680 | sb->recovery_offset = cpu_to_le64(0); |
1da177e4 LT |
1681 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
1682 | ||
1683 | sb->utime = cpu_to_le64((__u64)mddev->utime); | |
1684 | sb->events = cpu_to_le64(mddev->events); | |
1685 | if (mddev->in_sync) | |
1686 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); | |
1687 | else | |
1688 | sb->resync_offset = cpu_to_le64(0); | |
1689 | ||
1c05b4bc | 1690 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
4dbcdc75 | 1691 | |
f0ca340c | 1692 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
58c0fed4 | 1693 | sb->size = cpu_to_le64(mddev->dev_sectors); |
9d8f0363 | 1694 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
62e1e389 N |
1695 | sb->level = cpu_to_le32(mddev->level); |
1696 | sb->layout = cpu_to_le32(mddev->layout); | |
f0ca340c | 1697 | |
aeb9b211 N |
1698 | if (test_bit(WriteMostly, &rdev->flags)) |
1699 | sb->devflags |= WriteMostly1; | |
1700 | else | |
1701 | sb->devflags &= ~WriteMostly1; | |
c6563a8c N |
1702 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
1703 | sb->data_size = cpu_to_le64(rdev->sectors); | |
aeb9b211 | 1704 | |
c3d9714e N |
1705 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
1706 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); | |
71c0805c | 1707 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
a654b9d8 | 1708 | } |
5fd6c1dc N |
1709 | |
1710 | if (rdev->raid_disk >= 0 && | |
97e4f42d | 1711 | !test_bit(In_sync, &rdev->flags)) { |
93be75ff N |
1712 | sb->feature_map |= |
1713 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); | |
1714 | sb->recovery_offset = | |
1715 | cpu_to_le64(rdev->recovery_offset); | |
f466722c N |
1716 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
1717 | sb->feature_map |= | |
1718 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); | |
5fd6c1dc | 1719 | } |
2d78f8c4 N |
1720 | if (test_bit(Replacement, &rdev->flags)) |
1721 | sb->feature_map |= | |
1722 | cpu_to_le32(MD_FEATURE_REPLACEMENT); | |
5fd6c1dc | 1723 | |
f6705578 N |
1724 | if (mddev->reshape_position != MaxSector) { |
1725 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); | |
1726 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); | |
1727 | sb->new_layout = cpu_to_le32(mddev->new_layout); | |
1728 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); | |
1729 | sb->new_level = cpu_to_le32(mddev->new_level); | |
664e7c41 | 1730 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
2c810cdd N |
1731 | if (mddev->delta_disks == 0 && |
1732 | mddev->reshape_backwards) | |
1733 | sb->feature_map | |
1734 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); | |
c6563a8c N |
1735 | if (rdev->new_data_offset != rdev->data_offset) { |
1736 | sb->feature_map | |
1737 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); | |
1738 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset | |
1739 | - rdev->data_offset)); | |
1740 | } | |
f6705578 | 1741 | } |
a654b9d8 | 1742 | |
2699b672 N |
1743 | if (rdev->badblocks.count == 0) |
1744 | /* Nothing to do for bad blocks*/ ; | |
1745 | else if (sb->bblog_offset == 0) | |
1746 | /* Cannot record bad blocks on this device */ | |
1747 | md_error(mddev, rdev); | |
1748 | else { | |
1749 | struct badblocks *bb = &rdev->badblocks; | |
1750 | u64 *bbp = (u64 *)page_address(rdev->bb_page); | |
1751 | u64 *p = bb->page; | |
1752 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); | |
1753 | if (bb->changed) { | |
1754 | unsigned seq; | |
1755 | ||
1756 | retry: | |
1757 | seq = read_seqbegin(&bb->lock); | |
1758 | ||
1759 | memset(bbp, 0xff, PAGE_SIZE); | |
1760 | ||
1761 | for (i = 0 ; i < bb->count ; i++) { | |
35f9ac2d | 1762 | u64 internal_bb = p[i]; |
2699b672 N |
1763 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
1764 | | BB_LEN(internal_bb)); | |
35f9ac2d | 1765 | bbp[i] = cpu_to_le64(store_bb); |
2699b672 | 1766 | } |
d0962936 | 1767 | bb->changed = 0; |
2699b672 N |
1768 | if (read_seqretry(&bb->lock, seq)) |
1769 | goto retry; | |
1770 | ||
1771 | bb->sector = (rdev->sb_start + | |
1772 | (int)le32_to_cpu(sb->bblog_offset)); | |
1773 | bb->size = le16_to_cpu(sb->bblog_size); | |
2699b672 N |
1774 | } |
1775 | } | |
1776 | ||
1da177e4 | 1777 | max_dev = 0; |
dafb20fa | 1778 | rdev_for_each(rdev2, mddev) |
1da177e4 LT |
1779 | if (rdev2->desc_nr+1 > max_dev) |
1780 | max_dev = rdev2->desc_nr+1; | |
a778b73f | 1781 | |
70471daf N |
1782 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
1783 | int bmask; | |
a778b73f | 1784 | sb->max_dev = cpu_to_le32(max_dev); |
70471daf N |
1785 | rdev->sb_size = max_dev * 2 + 256; |
1786 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; | |
1787 | if (rdev->sb_size & bmask) | |
1788 | rdev->sb_size = (rdev->sb_size | bmask) + 1; | |
ddcf3522 N |
1789 | } else |
1790 | max_dev = le32_to_cpu(sb->max_dev); | |
1791 | ||
1da177e4 LT |
1792 | for (i=0; i<max_dev;i++) |
1793 | sb->dev_roles[i] = cpu_to_le16(0xfffe); | |
f72ffdd6 | 1794 | |
dafb20fa | 1795 | rdev_for_each(rdev2, mddev) { |
1da177e4 | 1796 | i = rdev2->desc_nr; |
b2d444d7 | 1797 | if (test_bit(Faulty, &rdev2->flags)) |
1da177e4 | 1798 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
b2d444d7 | 1799 | else if (test_bit(In_sync, &rdev2->flags)) |
1da177e4 | 1800 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
93be75ff | 1801 | else if (rdev2->raid_disk >= 0) |
5fd6c1dc | 1802 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
1da177e4 LT |
1803 | else |
1804 | sb->dev_roles[i] = cpu_to_le16(0xffff); | |
1805 | } | |
1806 | ||
1da177e4 LT |
1807 | sb->sb_csum = calc_sb_1_csum(sb); |
1808 | } | |
1809 | ||
0cd17fec | 1810 | static unsigned long long |
3cb03002 | 1811 | super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
0cd17fec CW |
1812 | { |
1813 | struct mdp_superblock_1 *sb; | |
15f4a5fd | 1814 | sector_t max_sectors; |
58c0fed4 | 1815 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
0cd17fec | 1816 | return 0; /* component must fit device */ |
c6563a8c N |
1817 | if (rdev->data_offset != rdev->new_data_offset) |
1818 | return 0; /* too confusing */ | |
0f420358 | 1819 | if (rdev->sb_start < rdev->data_offset) { |
0cd17fec | 1820 | /* minor versions 1 and 2; superblock before data */ |
77304d2a | 1821 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
15f4a5fd AN |
1822 | max_sectors -= rdev->data_offset; |
1823 | if (!num_sectors || num_sectors > max_sectors) | |
1824 | num_sectors = max_sectors; | |
c3d9714e | 1825 | } else if (rdev->mddev->bitmap_info.offset) { |
0cd17fec CW |
1826 | /* minor version 0 with bitmap we can't move */ |
1827 | return 0; | |
1828 | } else { | |
1829 | /* minor version 0; superblock after data */ | |
0f420358 | 1830 | sector_t sb_start; |
77304d2a | 1831 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
0f420358 | 1832 | sb_start &= ~(sector_t)(4*2 - 1); |
dd8ac336 | 1833 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
15f4a5fd AN |
1834 | if (!num_sectors || num_sectors > max_sectors) |
1835 | num_sectors = max_sectors; | |
0f420358 | 1836 | rdev->sb_start = sb_start; |
0cd17fec | 1837 | } |
65a06f06 | 1838 | sb = page_address(rdev->sb_page); |
15f4a5fd | 1839 | sb->data_size = cpu_to_le64(num_sectors); |
0f420358 | 1840 | sb->super_offset = rdev->sb_start; |
0cd17fec | 1841 | sb->sb_csum = calc_sb_1_csum(sb); |
0f420358 | 1842 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
0cd17fec CW |
1843 | rdev->sb_page); |
1844 | md_super_wait(rdev->mddev); | |
c26a44ed | 1845 | return num_sectors; |
c6563a8c N |
1846 | |
1847 | } | |
1848 | ||
1849 | static int | |
1850 | super_1_allow_new_offset(struct md_rdev *rdev, | |
1851 | unsigned long long new_offset) | |
1852 | { | |
1853 | /* All necessary checks on new >= old have been done */ | |
1854 | struct bitmap *bitmap; | |
1855 | if (new_offset >= rdev->data_offset) | |
1856 | return 1; | |
1857 | ||
1858 | /* with 1.0 metadata, there is no metadata to tread on | |
1859 | * so we can always move back */ | |
1860 | if (rdev->mddev->minor_version == 0) | |
1861 | return 1; | |
1862 | ||
1863 | /* otherwise we must be sure not to step on | |
1864 | * any metadata, so stay: | |
1865 | * 36K beyond start of superblock | |
1866 | * beyond end of badblocks | |
1867 | * beyond write-intent bitmap | |
1868 | */ | |
1869 | if (rdev->sb_start + (32+4)*2 > new_offset) | |
1870 | return 0; | |
1871 | bitmap = rdev->mddev->bitmap; | |
1872 | if (bitmap && !rdev->mddev->bitmap_info.file && | |
1873 | rdev->sb_start + rdev->mddev->bitmap_info.offset + | |
1ec885cd | 1874 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
c6563a8c N |
1875 | return 0; |
1876 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) | |
1877 | return 0; | |
1878 | ||
1879 | return 1; | |
0cd17fec | 1880 | } |
1da177e4 | 1881 | |
75c96f85 | 1882 | static struct super_type super_types[] = { |
1da177e4 LT |
1883 | [0] = { |
1884 | .name = "0.90.0", | |
1885 | .owner = THIS_MODULE, | |
0cd17fec CW |
1886 | .load_super = super_90_load, |
1887 | .validate_super = super_90_validate, | |
1888 | .sync_super = super_90_sync, | |
1889 | .rdev_size_change = super_90_rdev_size_change, | |
c6563a8c | 1890 | .allow_new_offset = super_90_allow_new_offset, |
1da177e4 LT |
1891 | }, |
1892 | [1] = { | |
1893 | .name = "md-1", | |
1894 | .owner = THIS_MODULE, | |
0cd17fec CW |
1895 | .load_super = super_1_load, |
1896 | .validate_super = super_1_validate, | |
1897 | .sync_super = super_1_sync, | |
1898 | .rdev_size_change = super_1_rdev_size_change, | |
c6563a8c | 1899 | .allow_new_offset = super_1_allow_new_offset, |
1da177e4 LT |
1900 | }, |
1901 | }; | |
1da177e4 | 1902 | |
fd01b88c | 1903 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
076f968b JB |
1904 | { |
1905 | if (mddev->sync_super) { | |
1906 | mddev->sync_super(mddev, rdev); | |
1907 | return; | |
1908 | } | |
1909 | ||
1910 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); | |
1911 | ||
1912 | super_types[mddev->major_version].sync_super(mddev, rdev); | |
1913 | } | |
1914 | ||
fd01b88c | 1915 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
1da177e4 | 1916 | { |
3cb03002 | 1917 | struct md_rdev *rdev, *rdev2; |
1da177e4 | 1918 | |
4b80991c N |
1919 | rcu_read_lock(); |
1920 | rdev_for_each_rcu(rdev, mddev1) | |
1921 | rdev_for_each_rcu(rdev2, mddev2) | |
7dd5e7c3 | 1922 | if (rdev->bdev->bd_contains == |
4b80991c N |
1923 | rdev2->bdev->bd_contains) { |
1924 | rcu_read_unlock(); | |
7dd5e7c3 | 1925 | return 1; |
4b80991c N |
1926 | } |
1927 | rcu_read_unlock(); | |
1da177e4 LT |
1928 | return 0; |
1929 | } | |
1930 | ||
1931 | static LIST_HEAD(pending_raid_disks); | |
1932 | ||
ac5e7113 AN |
1933 | /* |
1934 | * Try to register data integrity profile for an mddev | |
1935 | * | |
1936 | * This is called when an array is started and after a disk has been kicked | |
1937 | * from the array. It only succeeds if all working and active component devices | |
1938 | * are integrity capable with matching profiles. | |
1939 | */ | |
fd01b88c | 1940 | int md_integrity_register(struct mddev *mddev) |
ac5e7113 | 1941 | { |
3cb03002 | 1942 | struct md_rdev *rdev, *reference = NULL; |
ac5e7113 AN |
1943 | |
1944 | if (list_empty(&mddev->disks)) | |
1945 | return 0; /* nothing to do */ | |
629acb6a JB |
1946 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
1947 | return 0; /* shouldn't register, or already is */ | |
dafb20fa | 1948 | rdev_for_each(rdev, mddev) { |
ac5e7113 AN |
1949 | /* skip spares and non-functional disks */ |
1950 | if (test_bit(Faulty, &rdev->flags)) | |
1951 | continue; | |
1952 | if (rdev->raid_disk < 0) | |
1953 | continue; | |
ac5e7113 AN |
1954 | if (!reference) { |
1955 | /* Use the first rdev as the reference */ | |
1956 | reference = rdev; | |
1957 | continue; | |
1958 | } | |
1959 | /* does this rdev's profile match the reference profile? */ | |
1960 | if (blk_integrity_compare(reference->bdev->bd_disk, | |
1961 | rdev->bdev->bd_disk) < 0) | |
1962 | return -EINVAL; | |
1963 | } | |
89078d57 MP |
1964 | if (!reference || !bdev_get_integrity(reference->bdev)) |
1965 | return 0; | |
ac5e7113 AN |
1966 | /* |
1967 | * All component devices are integrity capable and have matching | |
1968 | * profiles, register the common profile for the md device. | |
1969 | */ | |
1970 | if (blk_integrity_register(mddev->gendisk, | |
1971 | bdev_get_integrity(reference->bdev)) != 0) { | |
1972 | printk(KERN_ERR "md: failed to register integrity for %s\n", | |
1973 | mdname(mddev)); | |
1974 | return -EINVAL; | |
1975 | } | |
a91a2785 MP |
1976 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
1977 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { | |
1978 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", | |
1979 | mdname(mddev)); | |
1980 | return -EINVAL; | |
1981 | } | |
ac5e7113 AN |
1982 | return 0; |
1983 | } | |
1984 | EXPORT_SYMBOL(md_integrity_register); | |
1985 | ||
1986 | /* Disable data integrity if non-capable/non-matching disk is being added */ | |
fd01b88c | 1987 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
3f9d99c1 | 1988 | { |
2863b9eb JB |
1989 | struct blk_integrity *bi_rdev; |
1990 | struct blk_integrity *bi_mddev; | |
1991 | ||
1992 | if (!mddev->gendisk) | |
1993 | return; | |
1994 | ||
1995 | bi_rdev = bdev_get_integrity(rdev->bdev); | |
1996 | bi_mddev = blk_get_integrity(mddev->gendisk); | |
3f9d99c1 | 1997 | |
ac5e7113 | 1998 | if (!bi_mddev) /* nothing to do */ |
3f9d99c1 | 1999 | return; |
ac5e7113 | 2000 | if (rdev->raid_disk < 0) /* skip spares */ |
3f9d99c1 | 2001 | return; |
ac5e7113 AN |
2002 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
2003 | rdev->bdev->bd_disk) >= 0) | |
2004 | return; | |
2005 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); | |
2006 | blk_integrity_unregister(mddev->gendisk); | |
3f9d99c1 | 2007 | } |
ac5e7113 | 2008 | EXPORT_SYMBOL(md_integrity_add_rdev); |
3f9d99c1 | 2009 | |
f72ffdd6 | 2010 | static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev) |
1da177e4 | 2011 | { |
7dd5e7c3 | 2012 | char b[BDEVNAME_SIZE]; |
f637b9f9 | 2013 | struct kobject *ko; |
1edf80d3 | 2014 | char *s; |
5e55e2f5 | 2015 | int err; |
1da177e4 LT |
2016 | |
2017 | if (rdev->mddev) { | |
2018 | MD_BUG(); | |
2019 | return -EINVAL; | |
2020 | } | |
11e2ede0 DW |
2021 | |
2022 | /* prevent duplicates */ | |
2023 | if (find_rdev(mddev, rdev->bdev->bd_dev)) | |
2024 | return -EEXIST; | |
2025 | ||
dd8ac336 AN |
2026 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
2027 | if (rdev->sectors && (mddev->dev_sectors == 0 || | |
2028 | rdev->sectors < mddev->dev_sectors)) { | |
a778b73f N |
2029 | if (mddev->pers) { |
2030 | /* Cannot change size, so fail | |
2031 | * If mddev->level <= 0, then we don't care | |
2032 | * about aligning sizes (e.g. linear) | |
2033 | */ | |
2034 | if (mddev->level > 0) | |
2035 | return -ENOSPC; | |
2036 | } else | |
dd8ac336 | 2037 | mddev->dev_sectors = rdev->sectors; |
2bf071bf | 2038 | } |
1da177e4 LT |
2039 | |
2040 | /* Verify rdev->desc_nr is unique. | |
2041 | * If it is -1, assign a free number, else | |
2042 | * check number is not in use | |
2043 | */ | |
4878e9eb | 2044 | rcu_read_lock(); |
1da177e4 LT |
2045 | if (rdev->desc_nr < 0) { |
2046 | int choice = 0; | |
4878e9eb N |
2047 | if (mddev->pers) |
2048 | choice = mddev->raid_disks; | |
2049 | while (find_rdev_nr_rcu(mddev, choice)) | |
1da177e4 LT |
2050 | choice++; |
2051 | rdev->desc_nr = choice; | |
2052 | } else { | |
4878e9eb N |
2053 | if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
2054 | rcu_read_unlock(); | |
1da177e4 | 2055 | return -EBUSY; |
4878e9eb | 2056 | } |
1da177e4 | 2057 | } |
4878e9eb | 2058 | rcu_read_unlock(); |
de01dfad N |
2059 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
2060 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", | |
2061 | mdname(mddev), mddev->max_disks); | |
2062 | return -EBUSY; | |
2063 | } | |
19133a42 | 2064 | bdevname(rdev->bdev,b); |
649316b2 | 2065 | while ( (s=strchr(b, '/')) != NULL) |
1edf80d3 | 2066 | *s = '!'; |
649316b2 | 2067 | |
1da177e4 | 2068 | rdev->mddev = mddev; |
19133a42 | 2069 | printk(KERN_INFO "md: bind<%s>\n", b); |
86e6ffdd | 2070 | |
b2d6db58 | 2071 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
5e55e2f5 | 2072 | goto fail; |
86e6ffdd | 2073 | |
0762b8bd | 2074 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
00bcb4ac N |
2075 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
2076 | /* failure here is OK */; | |
2077 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); | |
3c0ee63a | 2078 | |
4b80991c | 2079 | list_add_rcu(&rdev->same_set, &mddev->disks); |
e09b457b | 2080 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
4044ba58 N |
2081 | |
2082 | /* May as well allow recovery to be retried once */ | |
5389042f | 2083 | mddev->recovery_disabled++; |
3f9d99c1 | 2084 | |
1da177e4 | 2085 | return 0; |
5e55e2f5 N |
2086 | |
2087 | fail: | |
2088 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", | |
2089 | b, mdname(mddev)); | |
2090 | return err; | |
1da177e4 LT |
2091 | } |
2092 | ||
177a99b2 | 2093 | static void md_delayed_delete(struct work_struct *ws) |
5792a285 | 2094 | { |
3cb03002 | 2095 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
5792a285 | 2096 | kobject_del(&rdev->kobj); |
177a99b2 | 2097 | kobject_put(&rdev->kobj); |
5792a285 N |
2098 | } |
2099 | ||
f72ffdd6 | 2100 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
1da177e4 LT |
2101 | { |
2102 | char b[BDEVNAME_SIZE]; | |
2103 | if (!rdev->mddev) { | |
2104 | MD_BUG(); | |
2105 | return; | |
2106 | } | |
49731baa | 2107 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
4b80991c | 2108 | list_del_rcu(&rdev->same_set); |
1da177e4 LT |
2109 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
2110 | rdev->mddev = NULL; | |
86e6ffdd | 2111 | sysfs_remove_link(&rdev->kobj, "block"); |
3c0ee63a N |
2112 | sysfs_put(rdev->sysfs_state); |
2113 | rdev->sysfs_state = NULL; | |
2230dfe4 | 2114 | rdev->badblocks.count = 0; |
5792a285 | 2115 | /* We need to delay this, otherwise we can deadlock when |
4b80991c N |
2116 | * writing to 'remove' to "dev/state". We also need |
2117 | * to delay it due to rcu usage. | |
5792a285 | 2118 | */ |
4b80991c | 2119 | synchronize_rcu(); |
177a99b2 N |
2120 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
2121 | kobject_get(&rdev->kobj); | |
e804ac78 | 2122 | queue_work(md_misc_wq, &rdev->del_work); |
1da177e4 LT |
2123 | } |
2124 | ||
2125 | /* | |
2126 | * prevent the device from being mounted, repartitioned or | |
2127 | * otherwise reused by a RAID array (or any other kernel | |
2128 | * subsystem), by bd_claiming the device. | |
2129 | */ | |
3cb03002 | 2130 | static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared) |
1da177e4 LT |
2131 | { |
2132 | int err = 0; | |
2133 | struct block_device *bdev; | |
2134 | char b[BDEVNAME_SIZE]; | |
2135 | ||
d4d77629 | 2136 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
3cb03002 | 2137 | shared ? (struct md_rdev *)lock_rdev : rdev); |
1da177e4 LT |
2138 | if (IS_ERR(bdev)) { |
2139 | printk(KERN_ERR "md: could not open %s.\n", | |
2140 | __bdevname(dev, b)); | |
2141 | return PTR_ERR(bdev); | |
2142 | } | |
1da177e4 LT |
2143 | rdev->bdev = bdev; |
2144 | return err; | |
2145 | } | |
2146 | ||
3cb03002 | 2147 | static void unlock_rdev(struct md_rdev *rdev) |
1da177e4 LT |
2148 | { |
2149 | struct block_device *bdev = rdev->bdev; | |
2150 | rdev->bdev = NULL; | |
2151 | if (!bdev) | |
2152 | MD_BUG(); | |
e525fd89 | 2153 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
1da177e4 LT |
2154 | } |
2155 | ||
2156 | void md_autodetect_dev(dev_t dev); | |
2157 | ||
f72ffdd6 | 2158 | static void export_rdev(struct md_rdev *rdev) |
1da177e4 LT |
2159 | { |
2160 | char b[BDEVNAME_SIZE]; | |
2161 | printk(KERN_INFO "md: export_rdev(%s)\n", | |
2162 | bdevname(rdev->bdev,b)); | |
2163 | if (rdev->mddev) | |
2164 | MD_BUG(); | |
545c8795 | 2165 | md_rdev_clear(rdev); |
1da177e4 | 2166 | #ifndef MODULE |
d0fae18f N |
2167 | if (test_bit(AutoDetected, &rdev->flags)) |
2168 | md_autodetect_dev(rdev->bdev->bd_dev); | |
1da177e4 LT |
2169 | #endif |
2170 | unlock_rdev(rdev); | |
86e6ffdd | 2171 | kobject_put(&rdev->kobj); |
1da177e4 LT |
2172 | } |
2173 | ||
f72ffdd6 | 2174 | static void kick_rdev_from_array(struct md_rdev *rdev) |
1da177e4 LT |
2175 | { |
2176 | unbind_rdev_from_array(rdev); | |
2177 | export_rdev(rdev); | |
2178 | } | |
2179 | ||
fd01b88c | 2180 | static void export_array(struct mddev *mddev) |
1da177e4 | 2181 | { |
0638bb0e | 2182 | struct md_rdev *rdev; |
1da177e4 | 2183 | |
0638bb0e N |
2184 | while (!list_empty(&mddev->disks)) { |
2185 | rdev = list_first_entry(&mddev->disks, struct md_rdev, | |
2186 | same_set); | |
1da177e4 LT |
2187 | kick_rdev_from_array(rdev); |
2188 | } | |
1da177e4 LT |
2189 | mddev->raid_disks = 0; |
2190 | mddev->major_version = 0; | |
2191 | } | |
2192 | ||
2193 | static void print_desc(mdp_disk_t *desc) | |
2194 | { | |
2195 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, | |
2196 | desc->major,desc->minor,desc->raid_disk,desc->state); | |
2197 | } | |
2198 | ||
cd2ac932 | 2199 | static void print_sb_90(mdp_super_t *sb) |
1da177e4 LT |
2200 | { |
2201 | int i; | |
2202 | ||
f72ffdd6 | 2203 | printk(KERN_INFO |
1da177e4 LT |
2204 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
2205 | sb->major_version, sb->minor_version, sb->patch_version, | |
2206 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, | |
2207 | sb->ctime); | |
2208 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", | |
2209 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, | |
2210 | sb->md_minor, sb->layout, sb->chunk_size); | |
2211 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" | |
2212 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", | |
2213 | sb->utime, sb->state, sb->active_disks, sb->working_disks, | |
2214 | sb->failed_disks, sb->spare_disks, | |
2215 | sb->sb_csum, (unsigned long)sb->events_lo); | |
2216 | ||
2217 | printk(KERN_INFO); | |
2218 | for (i = 0; i < MD_SB_DISKS; i++) { | |
2219 | mdp_disk_t *desc; | |
2220 | ||
2221 | desc = sb->disks + i; | |
2222 | if (desc->number || desc->major || desc->minor || | |
2223 | desc->raid_disk || (desc->state && (desc->state != 4))) { | |
2224 | printk(" D %2d: ", i); | |
2225 | print_desc(desc); | |
2226 | } | |
2227 | } | |
2228 | printk(KERN_INFO "md: THIS: "); | |
2229 | print_desc(&sb->this_disk); | |
cd2ac932 | 2230 | } |
1da177e4 | 2231 | |
cd2ac932 CR |
2232 | static void print_sb_1(struct mdp_superblock_1 *sb) |
2233 | { | |
2234 | __u8 *uuid; | |
2235 | ||
2236 | uuid = sb->set_uuid; | |
ad361c98 | 2237 | printk(KERN_INFO |
7b75c2f8 | 2238 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
ad361c98 | 2239 | "md: Name: \"%s\" CT:%llu\n", |
cd2ac932 CR |
2240 | le32_to_cpu(sb->major_version), |
2241 | le32_to_cpu(sb->feature_map), | |
7b75c2f8 | 2242 | uuid, |
cd2ac932 CR |
2243 | sb->set_name, |
2244 | (unsigned long long)le64_to_cpu(sb->ctime) | |
2245 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); | |
2246 | ||
2247 | uuid = sb->device_uuid; | |
ad361c98 JP |
2248 | printk(KERN_INFO |
2249 | "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" | |
cd2ac932 | 2250 | " RO:%llu\n" |
7b75c2f8 | 2251 | "md: Dev:%08x UUID: %pU\n" |
ad361c98 JP |
2252 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
2253 | "md: (MaxDev:%u) \n", | |
cd2ac932 CR |
2254 | le32_to_cpu(sb->level), |
2255 | (unsigned long long)le64_to_cpu(sb->size), | |
2256 | le32_to_cpu(sb->raid_disks), | |
2257 | le32_to_cpu(sb->layout), | |
2258 | le32_to_cpu(sb->chunksize), | |
2259 | (unsigned long long)le64_to_cpu(sb->data_offset), | |
2260 | (unsigned long long)le64_to_cpu(sb->data_size), | |
2261 | (unsigned long long)le64_to_cpu(sb->super_offset), | |
2262 | (unsigned long long)le64_to_cpu(sb->recovery_offset), | |
2263 | le32_to_cpu(sb->dev_number), | |
7b75c2f8 | 2264 | uuid, |
cd2ac932 CR |
2265 | sb->devflags, |
2266 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, | |
2267 | (unsigned long long)le64_to_cpu(sb->events), | |
2268 | (unsigned long long)le64_to_cpu(sb->resync_offset), | |
2269 | le32_to_cpu(sb->sb_csum), | |
2270 | le32_to_cpu(sb->max_dev) | |
2271 | ); | |
1da177e4 LT |
2272 | } |
2273 | ||
3cb03002 | 2274 | static void print_rdev(struct md_rdev *rdev, int major_version) |
1da177e4 LT |
2275 | { |
2276 | char b[BDEVNAME_SIZE]; | |
dd8ac336 | 2277 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
f72ffdd6 N |
2278 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
2279 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), | |
2280 | rdev->desc_nr); | |
1da177e4 | 2281 | if (rdev->sb_loaded) { |
cd2ac932 CR |
2282 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
2283 | switch (major_version) { | |
2284 | case 0: | |
65a06f06 | 2285 | print_sb_90(page_address(rdev->sb_page)); |
cd2ac932 CR |
2286 | break; |
2287 | case 1: | |
65a06f06 | 2288 | print_sb_1(page_address(rdev->sb_page)); |
cd2ac932 CR |
2289 | break; |
2290 | } | |
1da177e4 LT |
2291 | } else |
2292 | printk(KERN_INFO "md: no rdev superblock!\n"); | |
2293 | } | |
2294 | ||
5e56341d | 2295 | static void md_print_devices(void) |
1da177e4 | 2296 | { |
159ec1fc | 2297 | struct list_head *tmp; |
3cb03002 | 2298 | struct md_rdev *rdev; |
fd01b88c | 2299 | struct mddev *mddev; |
1da177e4 LT |
2300 | char b[BDEVNAME_SIZE]; |
2301 | ||
2302 | printk("\n"); | |
2303 | printk("md: **********************************\n"); | |
2304 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); | |
2305 | printk("md: **********************************\n"); | |
29ac4aa3 | 2306 | for_each_mddev(mddev, tmp) { |
1da177e4 | 2307 | |
32a7627c N |
2308 | if (mddev->bitmap) |
2309 | bitmap_print_sb(mddev->bitmap); | |
2310 | else | |
2311 | printk("%s: ", mdname(mddev)); | |
dafb20fa | 2312 | rdev_for_each(rdev, mddev) |
1da177e4 LT |
2313 | printk("<%s>", bdevname(rdev->bdev,b)); |
2314 | printk("\n"); | |
2315 | ||
dafb20fa | 2316 | rdev_for_each(rdev, mddev) |
cd2ac932 | 2317 | print_rdev(rdev, mddev->major_version); |
1da177e4 LT |
2318 | } |
2319 | printk("md: **********************************\n"); | |
2320 | printk("\n"); | |
2321 | } | |
2322 | ||
f72ffdd6 | 2323 | static void sync_sbs(struct mddev *mddev, int nospares) |
1da177e4 | 2324 | { |
42543769 N |
2325 | /* Update each superblock (in-memory image), but |
2326 | * if we are allowed to, skip spares which already | |
2327 | * have the right event counter, or have one earlier | |
2328 | * (which would mean they aren't being marked as dirty | |
2329 | * with the rest of the array) | |
2330 | */ | |
3cb03002 | 2331 | struct md_rdev *rdev; |
dafb20fa | 2332 | rdev_for_each(rdev, mddev) { |
42543769 N |
2333 | if (rdev->sb_events == mddev->events || |
2334 | (nospares && | |
2335 | rdev->raid_disk < 0 && | |
42543769 N |
2336 | rdev->sb_events+1 == mddev->events)) { |
2337 | /* Don't update this superblock */ | |
2338 | rdev->sb_loaded = 2; | |
2339 | } else { | |
076f968b | 2340 | sync_super(mddev, rdev); |
42543769 N |
2341 | rdev->sb_loaded = 1; |
2342 | } | |
1da177e4 LT |
2343 | } |
2344 | } | |
2345 | ||
f72ffdd6 | 2346 | static void md_update_sb(struct mddev *mddev, int force_change) |
1da177e4 | 2347 | { |
3cb03002 | 2348 | struct md_rdev *rdev; |
06d91a5f | 2349 | int sync_req; |
42543769 | 2350 | int nospares = 0; |
2699b672 | 2351 | int any_badblocks_changed = 0; |
1da177e4 | 2352 | |
d87f064f N |
2353 | if (mddev->ro) { |
2354 | if (force_change) | |
2355 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
2356 | return; | |
2357 | } | |
1da177e4 | 2358 | repeat: |
3a3a5ddb | 2359 | /* First make sure individual recovery_offsets are correct */ |
dafb20fa | 2360 | rdev_for_each(rdev, mddev) { |
3a3a5ddb N |
2361 | if (rdev->raid_disk >= 0 && |
2362 | mddev->delta_disks >= 0 && | |
2363 | !test_bit(In_sync, &rdev->flags) && | |
2364 | mddev->curr_resync_completed > rdev->recovery_offset) | |
2365 | rdev->recovery_offset = mddev->curr_resync_completed; | |
2366 | ||
f72ffdd6 | 2367 | } |
bd52b746 | 2368 | if (!mddev->persistent) { |
070dc6dd | 2369 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
3a3a5ddb | 2370 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
de393cde | 2371 | if (!mddev->external) { |
d97a41dc | 2372 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
dafb20fa | 2373 | rdev_for_each(rdev, mddev) { |
de393cde | 2374 | if (rdev->badblocks.changed) { |
d0962936 | 2375 | rdev->badblocks.changed = 0; |
de393cde N |
2376 | md_ack_all_badblocks(&rdev->badblocks); |
2377 | md_error(mddev, rdev); | |
2378 | } | |
2379 | clear_bit(Blocked, &rdev->flags); | |
2380 | clear_bit(BlockedBadBlocks, &rdev->flags); | |
2381 | wake_up(&rdev->blocked_wait); | |
2382 | } | |
2383 | } | |
3a3a5ddb N |
2384 | wake_up(&mddev->sb_wait); |
2385 | return; | |
2386 | } | |
2387 | ||
a9701a30 | 2388 | spin_lock_irq(&mddev->write_lock); |
84692195 | 2389 | |
3a3a5ddb N |
2390 | mddev->utime = get_seconds(); |
2391 | ||
850b2b42 N |
2392 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
2393 | force_change = 1; | |
2394 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) | |
2395 | /* just a clean<-> dirty transition, possibly leave spares alone, | |
2396 | * though if events isn't the right even/odd, we will have to do | |
2397 | * spares after all | |
2398 | */ | |
2399 | nospares = 1; | |
2400 | if (force_change) | |
2401 | nospares = 0; | |
2402 | if (mddev->degraded) | |
84692195 N |
2403 | /* If the array is degraded, then skipping spares is both |
2404 | * dangerous and fairly pointless. | |
2405 | * Dangerous because a device that was removed from the array | |
2406 | * might have a event_count that still looks up-to-date, | |
2407 | * so it can be re-added without a resync. | |
2408 | * Pointless because if there are any spares to skip, | |
2409 | * then a recovery will happen and soon that array won't | |
2410 | * be degraded any more and the spare can go back to sleep then. | |
2411 | */ | |
850b2b42 | 2412 | nospares = 0; |
84692195 | 2413 | |
06d91a5f | 2414 | sync_req = mddev->in_sync; |
42543769 N |
2415 | |
2416 | /* If this is just a dirty<->clean transition, and the array is clean | |
2417 | * and 'events' is odd, we can roll back to the previous clean state */ | |
850b2b42 | 2418 | if (nospares |
42543769 | 2419 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
a8707c08 N |
2420 | && mddev->can_decrease_events |
2421 | && mddev->events != 1) { | |
42543769 | 2422 | mddev->events--; |
a8707c08 N |
2423 | mddev->can_decrease_events = 0; |
2424 | } else { | |
42543769 N |
2425 | /* otherwise we have to go forward and ... */ |
2426 | mddev->events ++; | |
a8707c08 | 2427 | mddev->can_decrease_events = nospares; |
42543769 | 2428 | } |
1da177e4 LT |
2429 | |
2430 | if (!mddev->events) { | |
2431 | /* | |
2432 | * oops, this 64-bit counter should never wrap. | |
2433 | * Either we are in around ~1 trillion A.C., assuming | |
2434 | * 1 reboot per second, or we have a bug: | |
2435 | */ | |
2436 | MD_BUG(); | |
2437 | mddev->events --; | |
2438 | } | |
2699b672 | 2439 | |
dafb20fa | 2440 | rdev_for_each(rdev, mddev) { |
2699b672 N |
2441 | if (rdev->badblocks.changed) |
2442 | any_badblocks_changed++; | |
de393cde N |
2443 | if (test_bit(Faulty, &rdev->flags)) |
2444 | set_bit(FaultRecorded, &rdev->flags); | |
2445 | } | |
2699b672 | 2446 | |
e691063a | 2447 | sync_sbs(mddev, nospares); |
a9701a30 | 2448 | spin_unlock_irq(&mddev->write_lock); |
1da177e4 | 2449 | |
36a4e1fe N |
2450 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
2451 | mdname(mddev), mddev->in_sync); | |
1da177e4 | 2452 | |
4ad13663 | 2453 | bitmap_update_sb(mddev->bitmap); |
dafb20fa | 2454 | rdev_for_each(rdev, mddev) { |
1da177e4 | 2455 | char b[BDEVNAME_SIZE]; |
36a4e1fe | 2456 | |
42543769 N |
2457 | if (rdev->sb_loaded != 1) |
2458 | continue; /* no noise on spare devices */ | |
1da177e4 | 2459 | |
f466722c | 2460 | if (!test_bit(Faulty, &rdev->flags)) { |
7bfa19f2 | 2461 | md_super_write(mddev,rdev, |
0f420358 | 2462 | rdev->sb_start, rdev->sb_size, |
7bfa19f2 | 2463 | rdev->sb_page); |
36a4e1fe N |
2464 | pr_debug("md: (write) %s's sb offset: %llu\n", |
2465 | bdevname(rdev->bdev, b), | |
2466 | (unsigned long long)rdev->sb_start); | |
42543769 | 2467 | rdev->sb_events = mddev->events; |
2699b672 N |
2468 | if (rdev->badblocks.size) { |
2469 | md_super_write(mddev, rdev, | |
2470 | rdev->badblocks.sector, | |
2471 | rdev->badblocks.size << 9, | |
2472 | rdev->bb_page); | |
2473 | rdev->badblocks.size = 0; | |
2474 | } | |
7bfa19f2 | 2475 | |
f466722c | 2476 | } else |
36a4e1fe N |
2477 | pr_debug("md: %s (skipping faulty)\n", |
2478 | bdevname(rdev->bdev, b)); | |
d70ed2e4 | 2479 | |
7bfa19f2 | 2480 | if (mddev->level == LEVEL_MULTIPATH) |
1da177e4 LT |
2481 | /* only need to write one superblock... */ |
2482 | break; | |
2483 | } | |
a9701a30 | 2484 | md_super_wait(mddev); |
850b2b42 | 2485 | /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */ |
7bfa19f2 | 2486 | |
a9701a30 | 2487 | spin_lock_irq(&mddev->write_lock); |
850b2b42 N |
2488 | if (mddev->in_sync != sync_req || |
2489 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { | |
06d91a5f | 2490 | /* have to write it out again */ |
a9701a30 | 2491 | spin_unlock_irq(&mddev->write_lock); |
06d91a5f N |
2492 | goto repeat; |
2493 | } | |
850b2b42 | 2494 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
a9701a30 | 2495 | spin_unlock_irq(&mddev->write_lock); |
3d310eb7 | 2496 | wake_up(&mddev->sb_wait); |
acb180b0 N |
2497 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
2498 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); | |
06d91a5f | 2499 | |
dafb20fa | 2500 | rdev_for_each(rdev, mddev) { |
de393cde N |
2501 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
2502 | clear_bit(Blocked, &rdev->flags); | |
2503 | ||
2504 | if (any_badblocks_changed) | |
2699b672 | 2505 | md_ack_all_badblocks(&rdev->badblocks); |
de393cde N |
2506 | clear_bit(BlockedBadBlocks, &rdev->flags); |
2507 | wake_up(&rdev->blocked_wait); | |
2508 | } | |
1da177e4 LT |
2509 | } |
2510 | ||
7f6ce769 | 2511 | /* words written to sysfs files may, or may not, be \n terminated. |
bce74dac N |
2512 | * We want to accept with case. For this we use cmd_match. |
2513 | */ | |
2514 | static int cmd_match(const char *cmd, const char *str) | |
2515 | { | |
2516 | /* See if cmd, written into a sysfs file, matches | |
2517 | * str. They must either be the same, or cmd can | |
2518 | * have a trailing newline | |
2519 | */ | |
2520 | while (*cmd && *str && *cmd == *str) { | |
2521 | cmd++; | |
2522 | str++; | |
2523 | } | |
2524 | if (*cmd == '\n') | |
2525 | cmd++; | |
2526 | if (*str || *cmd) | |
2527 | return 0; | |
2528 | return 1; | |
2529 | } | |
2530 | ||
86e6ffdd N |
2531 | struct rdev_sysfs_entry { |
2532 | struct attribute attr; | |
3cb03002 N |
2533 | ssize_t (*show)(struct md_rdev *, char *); |
2534 | ssize_t (*store)(struct md_rdev *, const char *, size_t); | |
86e6ffdd N |
2535 | }; |
2536 | ||
2537 | static ssize_t | |
3cb03002 | 2538 | state_show(struct md_rdev *rdev, char *page) |
86e6ffdd N |
2539 | { |
2540 | char *sep = ""; | |
20a49ff6 | 2541 | size_t len = 0; |
86e6ffdd | 2542 | |
de393cde N |
2543 | if (test_bit(Faulty, &rdev->flags) || |
2544 | rdev->badblocks.unacked_exist) { | |
86e6ffdd N |
2545 | len+= sprintf(page+len, "%sfaulty",sep); |
2546 | sep = ","; | |
2547 | } | |
b2d444d7 | 2548 | if (test_bit(In_sync, &rdev->flags)) { |
86e6ffdd N |
2549 | len += sprintf(page+len, "%sin_sync",sep); |
2550 | sep = ","; | |
2551 | } | |
f655675b N |
2552 | if (test_bit(WriteMostly, &rdev->flags)) { |
2553 | len += sprintf(page+len, "%swrite_mostly",sep); | |
2554 | sep = ","; | |
2555 | } | |
de393cde | 2556 | if (test_bit(Blocked, &rdev->flags) || |
52c64152 N |
2557 | (rdev->badblocks.unacked_exist |
2558 | && !test_bit(Faulty, &rdev->flags))) { | |
6bfe0b49 DW |
2559 | len += sprintf(page+len, "%sblocked", sep); |
2560 | sep = ","; | |
2561 | } | |
b2d444d7 N |
2562 | if (!test_bit(Faulty, &rdev->flags) && |
2563 | !test_bit(In_sync, &rdev->flags)) { | |
86e6ffdd N |
2564 | len += sprintf(page+len, "%sspare", sep); |
2565 | sep = ","; | |
2566 | } | |
d7a9d443 N |
2567 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
2568 | len += sprintf(page+len, "%swrite_error", sep); | |
2569 | sep = ","; | |
2570 | } | |
2d78f8c4 N |
2571 | if (test_bit(WantReplacement, &rdev->flags)) { |
2572 | len += sprintf(page+len, "%swant_replacement", sep); | |
2573 | sep = ","; | |
2574 | } | |
2575 | if (test_bit(Replacement, &rdev->flags)) { | |
2576 | len += sprintf(page+len, "%sreplacement", sep); | |
2577 | sep = ","; | |
2578 | } | |
2579 | ||
86e6ffdd N |
2580 | return len+sprintf(page+len, "\n"); |
2581 | } | |
2582 | ||
45dc2de1 | 2583 | static ssize_t |
3cb03002 | 2584 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
45dc2de1 N |
2585 | { |
2586 | /* can write | |
de393cde | 2587 | * faulty - simulates an error |
45dc2de1 | 2588 | * remove - disconnects the device |
f655675b N |
2589 | * writemostly - sets write_mostly |
2590 | * -writemostly - clears write_mostly | |
de393cde N |
2591 | * blocked - sets the Blocked flags |
2592 | * -blocked - clears the Blocked and possibly simulates an error | |
6d56e278 | 2593 | * insync - sets Insync providing device isn't active |
f466722c N |
2594 | * -insync - clear Insync for a device with a slot assigned, |
2595 | * so that it gets rebuilt based on bitmap | |
d7a9d443 N |
2596 | * write_error - sets WriteErrorSeen |
2597 | * -write_error - clears WriteErrorSeen | |
45dc2de1 N |
2598 | */ |
2599 | int err = -EINVAL; | |
2600 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { | |
2601 | md_error(rdev->mddev, rdev); | |
5ef56c8f N |
2602 | if (test_bit(Faulty, &rdev->flags)) |
2603 | err = 0; | |
2604 | else | |
2605 | err = -EBUSY; | |
45dc2de1 N |
2606 | } else if (cmd_match(buf, "remove")) { |
2607 | if (rdev->raid_disk >= 0) | |
2608 | err = -EBUSY; | |
2609 | else { | |
fd01b88c | 2610 | struct mddev *mddev = rdev->mddev; |
45dc2de1 | 2611 | kick_rdev_from_array(rdev); |
3f9d7b0d N |
2612 | if (mddev->pers) |
2613 | md_update_sb(mddev, 1); | |
45dc2de1 N |
2614 | md_new_event(mddev); |
2615 | err = 0; | |
2616 | } | |
f655675b N |
2617 | } else if (cmd_match(buf, "writemostly")) { |
2618 | set_bit(WriteMostly, &rdev->flags); | |
2619 | err = 0; | |
2620 | } else if (cmd_match(buf, "-writemostly")) { | |
2621 | clear_bit(WriteMostly, &rdev->flags); | |
6bfe0b49 DW |
2622 | err = 0; |
2623 | } else if (cmd_match(buf, "blocked")) { | |
2624 | set_bit(Blocked, &rdev->flags); | |
2625 | err = 0; | |
2626 | } else if (cmd_match(buf, "-blocked")) { | |
de393cde | 2627 | if (!test_bit(Faulty, &rdev->flags) && |
7da64a0a | 2628 | rdev->badblocks.unacked_exist) { |
de393cde N |
2629 | /* metadata handler doesn't understand badblocks, |
2630 | * so we need to fail the device | |
2631 | */ | |
2632 | md_error(rdev->mddev, rdev); | |
2633 | } | |
6bfe0b49 | 2634 | clear_bit(Blocked, &rdev->flags); |
de393cde | 2635 | clear_bit(BlockedBadBlocks, &rdev->flags); |
6bfe0b49 DW |
2636 | wake_up(&rdev->blocked_wait); |
2637 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); | |
2638 | md_wakeup_thread(rdev->mddev->thread); | |
2639 | ||
6d56e278 N |
2640 | err = 0; |
2641 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { | |
2642 | set_bit(In_sync, &rdev->flags); | |
f655675b | 2643 | err = 0; |
f466722c | 2644 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) { |
e1960f8c N |
2645 | if (rdev->mddev->pers == NULL) { |
2646 | clear_bit(In_sync, &rdev->flags); | |
2647 | rdev->saved_raid_disk = rdev->raid_disk; | |
2648 | rdev->raid_disk = -1; | |
2649 | err = 0; | |
2650 | } | |
d7a9d443 N |
2651 | } else if (cmd_match(buf, "write_error")) { |
2652 | set_bit(WriteErrorSeen, &rdev->flags); | |
2653 | err = 0; | |
2654 | } else if (cmd_match(buf, "-write_error")) { | |
2655 | clear_bit(WriteErrorSeen, &rdev->flags); | |
2656 | err = 0; | |
2d78f8c4 N |
2657 | } else if (cmd_match(buf, "want_replacement")) { |
2658 | /* Any non-spare device that is not a replacement can | |
2659 | * become want_replacement at any time, but we then need to | |
2660 | * check if recovery is needed. | |
2661 | */ | |
2662 | if (rdev->raid_disk >= 0 && | |
2663 | !test_bit(Replacement, &rdev->flags)) | |
2664 | set_bit(WantReplacement, &rdev->flags); | |
2665 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); | |
2666 | md_wakeup_thread(rdev->mddev->thread); | |
2667 | err = 0; | |
2668 | } else if (cmd_match(buf, "-want_replacement")) { | |
2669 | /* Clearing 'want_replacement' is always allowed. | |
2670 | * Once replacements starts it is too late though. | |
2671 | */ | |
2672 | err = 0; | |
2673 | clear_bit(WantReplacement, &rdev->flags); | |
2674 | } else if (cmd_match(buf, "replacement")) { | |
2675 | /* Can only set a device as a replacement when array has not | |
2676 | * yet been started. Once running, replacement is automatic | |
2677 | * from spares, or by assigning 'slot'. | |
2678 | */ | |
2679 | if (rdev->mddev->pers) | |
2680 | err = -EBUSY; | |
2681 | else { | |
2682 | set_bit(Replacement, &rdev->flags); | |
2683 | err = 0; | |
2684 | } | |
2685 | } else if (cmd_match(buf, "-replacement")) { | |
2686 | /* Similarly, can only clear Replacement before start */ | |
2687 | if (rdev->mddev->pers) | |
2688 | err = -EBUSY; | |
2689 | else { | |
2690 | clear_bit(Replacement, &rdev->flags); | |
2691 | err = 0; | |
2692 | } | |
45dc2de1 | 2693 | } |
00bcb4ac N |
2694 | if (!err) |
2695 | sysfs_notify_dirent_safe(rdev->sysfs_state); | |
45dc2de1 N |
2696 | return err ? err : len; |
2697 | } | |
80ca3a44 N |
2698 | static struct rdev_sysfs_entry rdev_state = |
2699 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); | |
86e6ffdd | 2700 | |
4dbcdc75 | 2701 | static ssize_t |
3cb03002 | 2702 | errors_show(struct md_rdev *rdev, char *page) |
4dbcdc75 N |
2703 | { |
2704 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); | |
2705 | } | |
2706 | ||
2707 | static ssize_t | |
3cb03002 | 2708 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
4dbcdc75 N |
2709 | { |
2710 | char *e; | |
2711 | unsigned long n = simple_strtoul(buf, &e, 10); | |
2712 | if (*buf && (*e == 0 || *e == '\n')) { | |
2713 | atomic_set(&rdev->corrected_errors, n); | |
2714 | return len; | |
2715 | } | |
2716 | return -EINVAL; | |
2717 | } | |
2718 | static struct rdev_sysfs_entry rdev_errors = | |
80ca3a44 | 2719 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
4dbcdc75 | 2720 | |
014236d2 | 2721 | static ssize_t |
3cb03002 | 2722 | slot_show(struct md_rdev *rdev, char *page) |
014236d2 N |
2723 | { |
2724 | if (rdev->raid_disk < 0) | |
2725 | return sprintf(page, "none\n"); | |
2726 | else | |
2727 | return sprintf(page, "%d\n", rdev->raid_disk); | |
2728 | } | |
2729 | ||
2730 | static ssize_t | |
3cb03002 | 2731 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
014236d2 N |
2732 | { |
2733 | char *e; | |
c303da6d | 2734 | int err; |
014236d2 N |
2735 | int slot = simple_strtoul(buf, &e, 10); |
2736 | if (strncmp(buf, "none", 4)==0) | |
2737 | slot = -1; | |
2738 | else if (e==buf || (*e && *e!= '\n')) | |
2739 | return -EINVAL; | |
6c2fce2e | 2740 | if (rdev->mddev->pers && slot == -1) { |
c303da6d N |
2741 | /* Setting 'slot' on an active array requires also |
2742 | * updating the 'rd%d' link, and communicating | |
2743 | * with the personality with ->hot_*_disk. | |
2744 | * For now we only support removing | |
2745 | * failed/spare devices. This normally happens automatically, | |
2746 | * but not when the metadata is externally managed. | |
2747 | */ | |
c303da6d N |
2748 | if (rdev->raid_disk == -1) |
2749 | return -EEXIST; | |
2750 | /* personality does all needed checks */ | |
01393f3d | 2751 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
c303da6d | 2752 | return -EINVAL; |
746d3207 N |
2753 | clear_bit(Blocked, &rdev->flags); |
2754 | remove_and_add_spares(rdev->mddev, rdev); | |
2755 | if (rdev->raid_disk >= 0) | |
2756 | return -EBUSY; | |
c303da6d N |
2757 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
2758 | md_wakeup_thread(rdev->mddev->thread); | |
6c2fce2e | 2759 | } else if (rdev->mddev->pers) { |
6c2fce2e | 2760 | /* Activating a spare .. or possibly reactivating |
6d56e278 | 2761 | * if we ever get bitmaps working here. |
6c2fce2e NB |
2762 | */ |
2763 | ||
2764 | if (rdev->raid_disk != -1) | |
2765 | return -EBUSY; | |
2766 | ||
c6751b2b N |
2767 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
2768 | return -EBUSY; | |
2769 | ||
6c2fce2e NB |
2770 | if (rdev->mddev->pers->hot_add_disk == NULL) |
2771 | return -EINVAL; | |
2772 | ||
ba1b41b6 N |
2773 | if (slot >= rdev->mddev->raid_disks && |
2774 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) | |
2775 | return -ENOSPC; | |
2776 | ||
6c2fce2e NB |
2777 | rdev->raid_disk = slot; |
2778 | if (test_bit(In_sync, &rdev->flags)) | |
2779 | rdev->saved_raid_disk = slot; | |
2780 | else | |
2781 | rdev->saved_raid_disk = -1; | |
d30519fc | 2782 | clear_bit(In_sync, &rdev->flags); |
8313b8e5 | 2783 | clear_bit(Bitmap_sync, &rdev->flags); |
6c2fce2e NB |
2784 | err = rdev->mddev->pers-> |
2785 | hot_add_disk(rdev->mddev, rdev); | |
199050ea | 2786 | if (err) { |
6c2fce2e | 2787 | rdev->raid_disk = -1; |
6c2fce2e | 2788 | return err; |
52664732 | 2789 | } else |
00bcb4ac | 2790 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
36fad858 | 2791 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
00bcb4ac | 2792 | /* failure here is OK */; |
6c2fce2e | 2793 | /* don't wakeup anyone, leave that to userspace. */ |
c303da6d | 2794 | } else { |
ba1b41b6 N |
2795 | if (slot >= rdev->mddev->raid_disks && |
2796 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) | |
c303da6d N |
2797 | return -ENOSPC; |
2798 | rdev->raid_disk = slot; | |
2799 | /* assume it is working */ | |
c5d79adb N |
2800 | clear_bit(Faulty, &rdev->flags); |
2801 | clear_bit(WriteMostly, &rdev->flags); | |
c303da6d | 2802 | set_bit(In_sync, &rdev->flags); |
00bcb4ac | 2803 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
c303da6d | 2804 | } |
014236d2 N |
2805 | return len; |
2806 | } | |
2807 | ||
014236d2 | 2808 | static struct rdev_sysfs_entry rdev_slot = |
80ca3a44 | 2809 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
014236d2 | 2810 | |
93c8cad0 | 2811 | static ssize_t |
3cb03002 | 2812 | offset_show(struct md_rdev *rdev, char *page) |
93c8cad0 | 2813 | { |
6961ece4 | 2814 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
93c8cad0 N |
2815 | } |
2816 | ||
2817 | static ssize_t | |
3cb03002 | 2818 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
93c8cad0 | 2819 | { |
c6563a8c | 2820 | unsigned long long offset; |
b29bebd6 | 2821 | if (kstrtoull(buf, 10, &offset) < 0) |
93c8cad0 | 2822 | return -EINVAL; |
8ed0a521 | 2823 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
93c8cad0 | 2824 | return -EBUSY; |
dd8ac336 | 2825 | if (rdev->sectors && rdev->mddev->external) |
c5d79adb N |
2826 | /* Must set offset before size, so overlap checks |
2827 | * can be sane */ | |
2828 | return -EBUSY; | |
93c8cad0 | 2829 | rdev->data_offset = offset; |
25f7fd47 | 2830 | rdev->new_data_offset = offset; |
93c8cad0 N |
2831 | return len; |
2832 | } | |
2833 | ||
2834 | static struct rdev_sysfs_entry rdev_offset = | |
80ca3a44 | 2835 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
93c8cad0 | 2836 | |
c6563a8c N |
2837 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
2838 | { | |
2839 | return sprintf(page, "%llu\n", | |
2840 | (unsigned long long)rdev->new_data_offset); | |
2841 | } | |
2842 | ||
2843 | static ssize_t new_offset_store(struct md_rdev *rdev, | |
2844 | const char *buf, size_t len) | |
2845 | { | |
2846 | unsigned long long new_offset; | |
2847 | struct mddev *mddev = rdev->mddev; | |
2848 | ||
b29bebd6 | 2849 | if (kstrtoull(buf, 10, &new_offset) < 0) |
c6563a8c N |
2850 | return -EINVAL; |
2851 | ||
2852 | if (mddev->sync_thread) | |
2853 | return -EBUSY; | |
2854 | if (new_offset == rdev->data_offset) | |
2855 | /* reset is always permitted */ | |
2856 | ; | |
2857 | else if (new_offset > rdev->data_offset) { | |
2858 | /* must not push array size beyond rdev_sectors */ | |
2859 | if (new_offset - rdev->data_offset | |
2860 | + mddev->dev_sectors > rdev->sectors) | |
2861 | return -E2BIG; | |
2862 | } | |
2863 | /* Metadata worries about other space details. */ | |
2864 | ||
2865 | /* decreasing the offset is inconsistent with a backwards | |
2866 | * reshape. | |
2867 | */ | |
2868 | if (new_offset < rdev->data_offset && | |
2869 | mddev->reshape_backwards) | |
2870 | return -EINVAL; | |
2871 | /* Increasing offset is inconsistent with forwards | |
2872 | * reshape. reshape_direction should be set to | |
2873 | * 'backwards' first. | |
2874 | */ | |
2875 | if (new_offset > rdev->data_offset && | |
2876 | !mddev->reshape_backwards) | |
2877 | return -EINVAL; | |
2878 | ||
2879 | if (mddev->pers && mddev->persistent && | |
2880 | !super_types[mddev->major_version] | |
2881 | .allow_new_offset(rdev, new_offset)) | |
2882 | return -E2BIG; | |
2883 | rdev->new_data_offset = new_offset; | |
2884 | if (new_offset > rdev->data_offset) | |
2885 | mddev->reshape_backwards = 1; | |
2886 | else if (new_offset < rdev->data_offset) | |
2887 | mddev->reshape_backwards = 0; | |
2888 | ||
2889 | return len; | |
2890 | } | |
2891 | static struct rdev_sysfs_entry rdev_new_offset = | |
2892 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); | |
2893 | ||
83303b61 | 2894 | static ssize_t |
3cb03002 | 2895 | rdev_size_show(struct md_rdev *rdev, char *page) |
83303b61 | 2896 | { |
dd8ac336 | 2897 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
83303b61 N |
2898 | } |
2899 | ||
c5d79adb N |
2900 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
2901 | { | |
2902 | /* check if two start/length pairs overlap */ | |
2903 | if (s1+l1 <= s2) | |
2904 | return 0; | |
2905 | if (s2+l2 <= s1) | |
2906 | return 0; | |
2907 | return 1; | |
2908 | } | |
2909 | ||
b522adcd DW |
2910 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
2911 | { | |
2912 | unsigned long long blocks; | |
2913 | sector_t new; | |
2914 | ||
b29bebd6 | 2915 | if (kstrtoull(buf, 10, &blocks) < 0) |
b522adcd DW |
2916 | return -EINVAL; |
2917 | ||
2918 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) | |
2919 | return -EINVAL; /* sector conversion overflow */ | |
2920 | ||
2921 | new = blocks * 2; | |
2922 | if (new != blocks * 2) | |
2923 | return -EINVAL; /* unsigned long long to sector_t overflow */ | |
2924 | ||
2925 | *sectors = new; | |
2926 | return 0; | |
2927 | } | |
2928 | ||
83303b61 | 2929 | static ssize_t |
3cb03002 | 2930 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
83303b61 | 2931 | { |
fd01b88c | 2932 | struct mddev *my_mddev = rdev->mddev; |
dd8ac336 | 2933 | sector_t oldsectors = rdev->sectors; |
b522adcd | 2934 | sector_t sectors; |
27c529bb | 2935 | |
b522adcd | 2936 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
d7027458 | 2937 | return -EINVAL; |
c6563a8c N |
2938 | if (rdev->data_offset != rdev->new_data_offset) |
2939 | return -EINVAL; /* too confusing */ | |
0cd17fec | 2940 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
d7027458 | 2941 | if (my_mddev->persistent) { |
dd8ac336 AN |
2942 | sectors = super_types[my_mddev->major_version]. |
2943 | rdev_size_change(rdev, sectors); | |
2944 | if (!sectors) | |
0cd17fec | 2945 | return -EBUSY; |
dd8ac336 | 2946 | } else if (!sectors) |
77304d2a | 2947 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
dd8ac336 | 2948 | rdev->data_offset; |
a6468539 N |
2949 | if (!my_mddev->pers->resize) |
2950 | /* Cannot change size for RAID0 or Linear etc */ | |
2951 | return -EINVAL; | |
0cd17fec | 2952 | } |
dd8ac336 | 2953 | if (sectors < my_mddev->dev_sectors) |
7d3c6f87 | 2954 | return -EINVAL; /* component must fit device */ |
0cd17fec | 2955 | |
dd8ac336 AN |
2956 | rdev->sectors = sectors; |
2957 | if (sectors > oldsectors && my_mddev->external) { | |
8b1afc3d N |
2958 | /* Need to check that all other rdevs with the same |
2959 | * ->bdev do not overlap. 'rcu' is sufficient to walk | |
2960 | * the rdev lists safely. | |
2961 | * This check does not provide a hard guarantee, it | |
2962 | * just helps avoid dangerous mistakes. | |
c5d79adb | 2963 | */ |
fd01b88c | 2964 | struct mddev *mddev; |
c5d79adb | 2965 | int overlap = 0; |
159ec1fc | 2966 | struct list_head *tmp; |
c5d79adb | 2967 | |
8b1afc3d | 2968 | rcu_read_lock(); |
29ac4aa3 | 2969 | for_each_mddev(mddev, tmp) { |
3cb03002 | 2970 | struct md_rdev *rdev2; |
c5d79adb | 2971 | |
dafb20fa | 2972 | rdev_for_each(rdev2, mddev) |
f21e9ff7 N |
2973 | if (rdev->bdev == rdev2->bdev && |
2974 | rdev != rdev2 && | |
2975 | overlaps(rdev->data_offset, rdev->sectors, | |
2976 | rdev2->data_offset, | |
2977 | rdev2->sectors)) { | |
c5d79adb N |
2978 | overlap = 1; |
2979 | break; | |
2980 | } | |
c5d79adb N |
2981 | if (overlap) { |
2982 | mddev_put(mddev); | |
2983 | break; | |
2984 | } | |
2985 | } | |
8b1afc3d | 2986 | rcu_read_unlock(); |
c5d79adb N |
2987 | if (overlap) { |
2988 | /* Someone else could have slipped in a size | |
2989 | * change here, but doing so is just silly. | |
dd8ac336 | 2990 | * We put oldsectors back because we *know* it is |
c5d79adb N |
2991 | * safe, and trust userspace not to race with |
2992 | * itself | |
2993 | */ | |
dd8ac336 | 2994 | rdev->sectors = oldsectors; |
c5d79adb N |
2995 | return -EBUSY; |
2996 | } | |
2997 | } | |
83303b61 N |
2998 | return len; |
2999 | } | |
3000 | ||
3001 | static struct rdev_sysfs_entry rdev_size = | |
80ca3a44 | 3002 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
83303b61 | 3003 | |
3cb03002 | 3004 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
06e3c817 DW |
3005 | { |
3006 | unsigned long long recovery_start = rdev->recovery_offset; | |
3007 | ||
3008 | if (test_bit(In_sync, &rdev->flags) || | |
3009 | recovery_start == MaxSector) | |
3010 | return sprintf(page, "none\n"); | |
3011 | ||
3012 | return sprintf(page, "%llu\n", recovery_start); | |
3013 | } | |
3014 | ||
3cb03002 | 3015 | static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len) |
06e3c817 DW |
3016 | { |
3017 | unsigned long long recovery_start; | |
3018 | ||
3019 | if (cmd_match(buf, "none")) | |
3020 | recovery_start = MaxSector; | |
b29bebd6 | 3021 | else if (kstrtoull(buf, 10, &recovery_start)) |
06e3c817 DW |
3022 | return -EINVAL; |
3023 | ||
3024 | if (rdev->mddev->pers && | |
3025 | rdev->raid_disk >= 0) | |
3026 | return -EBUSY; | |
3027 | ||
3028 | rdev->recovery_offset = recovery_start; | |
3029 | if (recovery_start == MaxSector) | |
3030 | set_bit(In_sync, &rdev->flags); | |
3031 | else | |
3032 | clear_bit(In_sync, &rdev->flags); | |
3033 | return len; | |
3034 | } | |
3035 | ||
3036 | static struct rdev_sysfs_entry rdev_recovery_start = | |
3037 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); | |
3038 | ||
16c791a5 N |
3039 | static ssize_t |
3040 | badblocks_show(struct badblocks *bb, char *page, int unack); | |
3041 | static ssize_t | |
3042 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); | |
3043 | ||
3cb03002 | 3044 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
16c791a5 N |
3045 | { |
3046 | return badblocks_show(&rdev->badblocks, page, 0); | |
3047 | } | |
3cb03002 | 3048 | static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len) |
16c791a5 | 3049 | { |
de393cde N |
3050 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
3051 | /* Maybe that ack was all we needed */ | |
3052 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) | |
3053 | wake_up(&rdev->blocked_wait); | |
3054 | return rv; | |
16c791a5 N |
3055 | } |
3056 | static struct rdev_sysfs_entry rdev_bad_blocks = | |
3057 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); | |
3058 | ||
3cb03002 | 3059 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
16c791a5 N |
3060 | { |
3061 | return badblocks_show(&rdev->badblocks, page, 1); | |
3062 | } | |
3cb03002 | 3063 | static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len) |
16c791a5 N |
3064 | { |
3065 | return badblocks_store(&rdev->badblocks, page, len, 1); | |
3066 | } | |
3067 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = | |
3068 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); | |
3069 | ||
86e6ffdd N |
3070 | static struct attribute *rdev_default_attrs[] = { |
3071 | &rdev_state.attr, | |
4dbcdc75 | 3072 | &rdev_errors.attr, |
014236d2 | 3073 | &rdev_slot.attr, |
93c8cad0 | 3074 | &rdev_offset.attr, |
c6563a8c | 3075 | &rdev_new_offset.attr, |
83303b61 | 3076 | &rdev_size.attr, |
06e3c817 | 3077 | &rdev_recovery_start.attr, |
16c791a5 N |
3078 | &rdev_bad_blocks.attr, |
3079 | &rdev_unack_bad_blocks.attr, | |
86e6ffdd N |
3080 | NULL, |
3081 | }; | |
3082 | static ssize_t | |
3083 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
3084 | { | |
3085 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); | |
3cb03002 | 3086 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
fd01b88c | 3087 | struct mddev *mddev = rdev->mddev; |
27c529bb | 3088 | ssize_t rv; |
86e6ffdd N |
3089 | |
3090 | if (!entry->show) | |
3091 | return -EIO; | |
27c529bb N |
3092 | |
3093 | rv = mddev ? mddev_lock(mddev) : -EBUSY; | |
3094 | if (!rv) { | |
3095 | if (rdev->mddev == NULL) | |
3096 | rv = -EBUSY; | |
3097 | else | |
3098 | rv = entry->show(rdev, page); | |
3099 | mddev_unlock(mddev); | |
3100 | } | |
3101 | return rv; | |
86e6ffdd N |
3102 | } |
3103 | ||
3104 | static ssize_t | |
3105 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, | |
3106 | const char *page, size_t length) | |
3107 | { | |
3108 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); | |
3cb03002 | 3109 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
27c529bb | 3110 | ssize_t rv; |
fd01b88c | 3111 | struct mddev *mddev = rdev->mddev; |
86e6ffdd N |
3112 | |
3113 | if (!entry->store) | |
3114 | return -EIO; | |
67463acb N |
3115 | if (!capable(CAP_SYS_ADMIN)) |
3116 | return -EACCES; | |
27c529bb | 3117 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
ca388059 | 3118 | if (!rv) { |
27c529bb N |
3119 | if (rdev->mddev == NULL) |
3120 | rv = -EBUSY; | |
3121 | else | |
3122 | rv = entry->store(rdev, page, length); | |
6a51830e | 3123 | mddev_unlock(mddev); |
ca388059 N |
3124 | } |
3125 | return rv; | |
86e6ffdd N |
3126 | } |
3127 | ||
3128 | static void rdev_free(struct kobject *ko) | |
3129 | { | |
3cb03002 | 3130 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
86e6ffdd N |
3131 | kfree(rdev); |
3132 | } | |
52cf25d0 | 3133 | static const struct sysfs_ops rdev_sysfs_ops = { |
86e6ffdd N |
3134 | .show = rdev_attr_show, |
3135 | .store = rdev_attr_store, | |
3136 | }; | |
3137 | static struct kobj_type rdev_ktype = { | |
3138 | .release = rdev_free, | |
3139 | .sysfs_ops = &rdev_sysfs_ops, | |
3140 | .default_attrs = rdev_default_attrs, | |
3141 | }; | |
3142 | ||
3cb03002 | 3143 | int md_rdev_init(struct md_rdev *rdev) |
e8bb9a83 N |
3144 | { |
3145 | rdev->desc_nr = -1; | |
3146 | rdev->saved_raid_disk = -1; | |
3147 | rdev->raid_disk = -1; | |
3148 | rdev->flags = 0; | |
3149 | rdev->data_offset = 0; | |
c6563a8c | 3150 | rdev->new_data_offset = 0; |
e8bb9a83 N |
3151 | rdev->sb_events = 0; |
3152 | rdev->last_read_error.tv_sec = 0; | |
3153 | rdev->last_read_error.tv_nsec = 0; | |
2699b672 N |
3154 | rdev->sb_loaded = 0; |
3155 | rdev->bb_page = NULL; | |
e8bb9a83 N |
3156 | atomic_set(&rdev->nr_pending, 0); |
3157 | atomic_set(&rdev->read_errors, 0); | |
3158 | atomic_set(&rdev->corrected_errors, 0); | |
3159 | ||
3160 | INIT_LIST_HEAD(&rdev->same_set); | |
3161 | init_waitqueue_head(&rdev->blocked_wait); | |
2230dfe4 N |
3162 | |
3163 | /* Add space to store bad block list. | |
3164 | * This reserves the space even on arrays where it cannot | |
3165 | * be used - I wonder if that matters | |
3166 | */ | |
3167 | rdev->badblocks.count = 0; | |
486adf72 | 3168 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
2230dfe4 N |
3169 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
3170 | seqlock_init(&rdev->badblocks.lock); | |
3171 | if (rdev->badblocks.page == NULL) | |
3172 | return -ENOMEM; | |
3173 | ||
3174 | return 0; | |
e8bb9a83 N |
3175 | } |
3176 | EXPORT_SYMBOL_GPL(md_rdev_init); | |
1da177e4 LT |
3177 | /* |
3178 | * Import a device. If 'super_format' >= 0, then sanity check the superblock | |
3179 | * | |
3180 | * mark the device faulty if: | |
3181 | * | |
3182 | * - the device is nonexistent (zero size) | |
3183 | * - the device has no valid superblock | |
3184 | * | |
3185 | * a faulty rdev _never_ has rdev->sb set. | |
3186 | */ | |
3cb03002 | 3187 | static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor) |
1da177e4 LT |
3188 | { |
3189 | char b[BDEVNAME_SIZE]; | |
3190 | int err; | |
3cb03002 | 3191 | struct md_rdev *rdev; |
1da177e4 LT |
3192 | sector_t size; |
3193 | ||
9ffae0cf | 3194 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
1da177e4 LT |
3195 | if (!rdev) { |
3196 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); | |
3197 | return ERR_PTR(-ENOMEM); | |
3198 | } | |
1da177e4 | 3199 | |
2230dfe4 N |
3200 | err = md_rdev_init(rdev); |
3201 | if (err) | |
3202 | goto abort_free; | |
3203 | err = alloc_disk_sb(rdev); | |
3204 | if (err) | |
1da177e4 LT |
3205 | goto abort_free; |
3206 | ||
c5d79adb | 3207 | err = lock_rdev(rdev, newdev, super_format == -2); |
1da177e4 LT |
3208 | if (err) |
3209 | goto abort_free; | |
3210 | ||
f9cb074b | 3211 | kobject_init(&rdev->kobj, &rdev_ktype); |
86e6ffdd | 3212 | |
77304d2a | 3213 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
1da177e4 | 3214 | if (!size) { |
f72ffdd6 | 3215 | printk(KERN_WARNING |
1da177e4 LT |
3216 | "md: %s has zero or unknown size, marking faulty!\n", |
3217 | bdevname(rdev->bdev,b)); | |
3218 | err = -EINVAL; | |
3219 | goto abort_free; | |
3220 | } | |
3221 | ||
3222 | if (super_format >= 0) { | |
3223 | err = super_types[super_format]. | |
3224 | load_super(rdev, NULL, super_minor); | |
3225 | if (err == -EINVAL) { | |
df968c4e N |
3226 | printk(KERN_WARNING |
3227 | "md: %s does not have a valid v%d.%d " | |
3228 | "superblock, not importing!\n", | |
3229 | bdevname(rdev->bdev,b), | |
3230 | super_format, super_minor); | |
1da177e4 LT |
3231 | goto abort_free; |
3232 | } | |
3233 | if (err < 0) { | |
f72ffdd6 | 3234 | printk(KERN_WARNING |
1da177e4 LT |
3235 | "md: could not read %s's sb, not importing!\n", |
3236 | bdevname(rdev->bdev,b)); | |
3237 | goto abort_free; | |
3238 | } | |
3239 | } | |
6bfe0b49 | 3240 | |
1da177e4 LT |
3241 | return rdev; |
3242 | ||
3243 | abort_free: | |
2699b672 N |
3244 | if (rdev->bdev) |
3245 | unlock_rdev(rdev); | |
545c8795 | 3246 | md_rdev_clear(rdev); |
1da177e4 LT |
3247 | kfree(rdev); |
3248 | return ERR_PTR(err); | |
3249 | } | |
3250 | ||
3251 | /* | |
3252 | * Check a full RAID array for plausibility | |
3253 | */ | |
3254 | ||
f72ffdd6 | 3255 | static void analyze_sbs(struct mddev *mddev) |
1da177e4 LT |
3256 | { |
3257 | int i; | |
3cb03002 | 3258 | struct md_rdev *rdev, *freshest, *tmp; |
1da177e4 LT |
3259 | char b[BDEVNAME_SIZE]; |
3260 | ||
3261 | freshest = NULL; | |
dafb20fa | 3262 | rdev_for_each_safe(rdev, tmp, mddev) |
1da177e4 LT |
3263 | switch (super_types[mddev->major_version]. |
3264 | load_super(rdev, freshest, mddev->minor_version)) { | |
3265 | case 1: | |
3266 | freshest = rdev; | |
3267 | break; | |
3268 | case 0: | |
3269 | break; | |
3270 | default: | |
3271 | printk( KERN_ERR \ | |
3272 | "md: fatal superblock inconsistency in %s" | |
f72ffdd6 | 3273 | " -- removing from array\n", |
1da177e4 LT |
3274 | bdevname(rdev->bdev,b)); |
3275 | kick_rdev_from_array(rdev); | |
3276 | } | |
3277 | ||
1da177e4 LT |
3278 | super_types[mddev->major_version]. |
3279 | validate_super(mddev, freshest); | |
3280 | ||
3281 | i = 0; | |
dafb20fa | 3282 | rdev_for_each_safe(rdev, tmp, mddev) { |
233fca36 N |
3283 | if (mddev->max_disks && |
3284 | (rdev->desc_nr >= mddev->max_disks || | |
3285 | i > mddev->max_disks)) { | |
de01dfad N |
3286 | printk(KERN_WARNING |
3287 | "md: %s: %s: only %d devices permitted\n", | |
3288 | mdname(mddev), bdevname(rdev->bdev, b), | |
3289 | mddev->max_disks); | |
3290 | kick_rdev_from_array(rdev); | |
3291 | continue; | |
3292 | } | |
1da177e4 LT |
3293 | if (rdev != freshest) |
3294 | if (super_types[mddev->major_version]. | |
3295 | validate_super(mddev, rdev)) { | |
3296 | printk(KERN_WARNING "md: kicking non-fresh %s" | |
3297 | " from array!\n", | |
3298 | bdevname(rdev->bdev,b)); | |
3299 | kick_rdev_from_array(rdev); | |
3300 | continue; | |
3301 | } | |
3302 | if (mddev->level == LEVEL_MULTIPATH) { | |
3303 | rdev->desc_nr = i++; | |
3304 | rdev->raid_disk = rdev->desc_nr; | |
b2d444d7 | 3305 | set_bit(In_sync, &rdev->flags); |
5e5e3e78 | 3306 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
a778b73f N |
3307 | rdev->raid_disk = -1; |
3308 | clear_bit(In_sync, &rdev->flags); | |
1da177e4 LT |
3309 | } |
3310 | } | |
1da177e4 LT |
3311 | } |
3312 | ||
72e02075 N |
3313 | /* Read a fixed-point number. |
3314 | * Numbers in sysfs attributes should be in "standard" units where | |
3315 | * possible, so time should be in seconds. | |
f72ffdd6 | 3316 | * However we internally use a a much smaller unit such as |
72e02075 N |
3317 | * milliseconds or jiffies. |
3318 | * This function takes a decimal number with a possible fractional | |
3319 | * component, and produces an integer which is the result of | |
3320 | * multiplying that number by 10^'scale'. | |
3321 | * all without any floating-point arithmetic. | |
3322 | */ | |
3323 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) | |
3324 | { | |
3325 | unsigned long result = 0; | |
3326 | long decimals = -1; | |
3327 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { | |
3328 | if (*cp == '.') | |
3329 | decimals = 0; | |
3330 | else if (decimals < scale) { | |
3331 | unsigned int value; | |
3332 | value = *cp - '0'; | |
3333 | result = result * 10 + value; | |
3334 | if (decimals >= 0) | |
3335 | decimals++; | |
3336 | } | |
3337 | cp++; | |
3338 | } | |
3339 | if (*cp == '\n') | |
3340 | cp++; | |
3341 | if (*cp) | |
3342 | return -EINVAL; | |
3343 | if (decimals < 0) | |
3344 | decimals = 0; | |
3345 | while (decimals < scale) { | |
3346 | result *= 10; | |
3347 | decimals ++; | |
3348 | } | |
3349 | *res = result; | |
3350 | return 0; | |
3351 | } | |
3352 | ||
19052c0e N |
3353 | static void md_safemode_timeout(unsigned long data); |
3354 | ||
16f17b39 | 3355 | static ssize_t |
fd01b88c | 3356 | safe_delay_show(struct mddev *mddev, char *page) |
16f17b39 N |
3357 | { |
3358 | int msec = (mddev->safemode_delay*1000)/HZ; | |
3359 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); | |
3360 | } | |
3361 | static ssize_t | |
fd01b88c | 3362 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
16f17b39 | 3363 | { |
16f17b39 | 3364 | unsigned long msec; |
97ce0a7f | 3365 | |
72e02075 | 3366 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
16f17b39 | 3367 | return -EINVAL; |
16f17b39 N |
3368 | if (msec == 0) |
3369 | mddev->safemode_delay = 0; | |
3370 | else { | |
19052c0e | 3371 | unsigned long old_delay = mddev->safemode_delay; |
16f17b39 N |
3372 | mddev->safemode_delay = (msec*HZ)/1000; |
3373 | if (mddev->safemode_delay == 0) | |
3374 | mddev->safemode_delay = 1; | |
275c51c4 | 3375 | if (mddev->safemode_delay < old_delay || old_delay == 0) |
19052c0e | 3376 | md_safemode_timeout((unsigned long)mddev); |
16f17b39 N |
3377 | } |
3378 | return len; | |
3379 | } | |
3380 | static struct md_sysfs_entry md_safe_delay = | |
80ca3a44 | 3381 | __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store); |
16f17b39 | 3382 | |
eae1701f | 3383 | static ssize_t |
fd01b88c | 3384 | level_show(struct mddev *mddev, char *page) |
eae1701f | 3385 | { |
84fc4b56 | 3386 | struct md_personality *p = mddev->pers; |
d9d166c2 | 3387 | if (p) |
eae1701f | 3388 | return sprintf(page, "%s\n", p->name); |
d9d166c2 N |
3389 | else if (mddev->clevel[0]) |
3390 | return sprintf(page, "%s\n", mddev->clevel); | |
3391 | else if (mddev->level != LEVEL_NONE) | |
3392 | return sprintf(page, "%d\n", mddev->level); | |
3393 | else | |
3394 | return 0; | |
eae1701f N |
3395 | } |
3396 | ||
d9d166c2 | 3397 | static ssize_t |
fd01b88c | 3398 | level_store(struct mddev *mddev, const char *buf, size_t len) |
d9d166c2 | 3399 | { |
f2859af6 | 3400 | char clevel[16]; |
20a49ff6 | 3401 | ssize_t rv = len; |
84fc4b56 | 3402 | struct md_personality *pers; |
f2859af6 | 3403 | long level; |
245f46c2 | 3404 | void *priv; |
3cb03002 | 3405 | struct md_rdev *rdev; |
245f46c2 N |
3406 | |
3407 | if (mddev->pers == NULL) { | |
3408 | if (len == 0) | |
3409 | return 0; | |
3410 | if (len >= sizeof(mddev->clevel)) | |
3411 | return -ENOSPC; | |
3412 | strncpy(mddev->clevel, buf, len); | |
3413 | if (mddev->clevel[len-1] == '\n') | |
3414 | len--; | |
3415 | mddev->clevel[len] = 0; | |
3416 | mddev->level = LEVEL_NONE; | |
3417 | return rv; | |
3418 | } | |
bd8839e0 N |
3419 | if (mddev->ro) |
3420 | return -EROFS; | |
245f46c2 N |
3421 | |
3422 | /* request to change the personality. Need to ensure: | |
3423 | * - array is not engaged in resync/recovery/reshape | |
3424 | * - old personality can be suspended | |
3425 | * - new personality will access other array. | |
3426 | */ | |
3427 | ||
bb4f1e9d N |
3428 | if (mddev->sync_thread || |
3429 | mddev->reshape_position != MaxSector || | |
3430 | mddev->sysfs_active) | |
d9d166c2 | 3431 | return -EBUSY; |
245f46c2 N |
3432 | |
3433 | if (!mddev->pers->quiesce) { | |
3434 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", | |
3435 | mdname(mddev), mddev->pers->name); | |
3436 | return -EINVAL; | |
3437 | } | |
3438 | ||
3439 | /* Now find the new personality */ | |
f2859af6 | 3440 | if (len == 0 || len >= sizeof(clevel)) |
245f46c2 | 3441 | return -EINVAL; |
f2859af6 DW |
3442 | strncpy(clevel, buf, len); |
3443 | if (clevel[len-1] == '\n') | |
d9d166c2 | 3444 | len--; |
f2859af6 | 3445 | clevel[len] = 0; |
b29bebd6 | 3446 | if (kstrtol(clevel, 10, &level)) |
f2859af6 | 3447 | level = LEVEL_NONE; |
245f46c2 | 3448 | |
f2859af6 DW |
3449 | if (request_module("md-%s", clevel) != 0) |
3450 | request_module("md-level-%s", clevel); | |
245f46c2 | 3451 | spin_lock(&pers_lock); |
f2859af6 | 3452 | pers = find_pers(level, clevel); |
245f46c2 N |
3453 | if (!pers || !try_module_get(pers->owner)) { |
3454 | spin_unlock(&pers_lock); | |
f2859af6 | 3455 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
245f46c2 N |
3456 | return -EINVAL; |
3457 | } | |
3458 | spin_unlock(&pers_lock); | |
3459 | ||
3460 | if (pers == mddev->pers) { | |
3461 | /* Nothing to do! */ | |
3462 | module_put(pers->owner); | |
3463 | return rv; | |
3464 | } | |
3465 | if (!pers->takeover) { | |
3466 | module_put(pers->owner); | |
3467 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", | |
f2859af6 | 3468 | mdname(mddev), clevel); |
245f46c2 N |
3469 | return -EINVAL; |
3470 | } | |
3471 | ||
dafb20fa | 3472 | rdev_for_each(rdev, mddev) |
e93f68a1 N |
3473 | rdev->new_raid_disk = rdev->raid_disk; |
3474 | ||
245f46c2 N |
3475 | /* ->takeover must set new_* and/or delta_disks |
3476 | * if it succeeds, and may set them when it fails. | |
3477 | */ | |
3478 | priv = pers->takeover(mddev); | |
3479 | if (IS_ERR(priv)) { | |
3480 | mddev->new_level = mddev->level; | |
3481 | mddev->new_layout = mddev->layout; | |
664e7c41 | 3482 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
245f46c2 N |
3483 | mddev->raid_disks -= mddev->delta_disks; |
3484 | mddev->delta_disks = 0; | |
2c810cdd | 3485 | mddev->reshape_backwards = 0; |
245f46c2 N |
3486 | module_put(pers->owner); |
3487 | printk(KERN_WARNING "md: %s: %s would not accept array\n", | |
f2859af6 | 3488 | mdname(mddev), clevel); |
245f46c2 N |
3489 | return PTR_ERR(priv); |
3490 | } | |
3491 | ||
3492 | /* Looks like we have a winner */ | |
3493 | mddev_suspend(mddev); | |
3494 | mddev->pers->stop(mddev); | |
f72ffdd6 | 3495 | |
a64c876f N |
3496 | if (mddev->pers->sync_request == NULL && |
3497 | pers->sync_request != NULL) { | |
3498 | /* need to add the md_redundancy_group */ | |
3499 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) | |
3500 | printk(KERN_WARNING | |
3501 | "md: cannot register extra attributes for %s\n", | |
3502 | mdname(mddev)); | |
388975cc | 3503 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
f72ffdd6 | 3504 | } |
a64c876f N |
3505 | if (mddev->pers->sync_request != NULL && |
3506 | pers->sync_request == NULL) { | |
3507 | /* need to remove the md_redundancy_group */ | |
3508 | if (mddev->to_remove == NULL) | |
3509 | mddev->to_remove = &md_redundancy_group; | |
3510 | } | |
3511 | ||
54071b38 TM |
3512 | if (mddev->pers->sync_request == NULL && |
3513 | mddev->external) { | |
3514 | /* We are converting from a no-redundancy array | |
3515 | * to a redundancy array and metadata is managed | |
3516 | * externally so we need to be sure that writes | |
3517 | * won't block due to a need to transition | |
3518 | * clean->dirty | |
3519 | * until external management is started. | |
3520 | */ | |
3521 | mddev->in_sync = 0; | |
3522 | mddev->safemode_delay = 0; | |
3523 | mddev->safemode = 0; | |
3524 | } | |
3525 | ||
dafb20fa | 3526 | rdev_for_each(rdev, mddev) { |
e93f68a1 N |
3527 | if (rdev->raid_disk < 0) |
3528 | continue; | |
bf2cb0da | 3529 | if (rdev->new_raid_disk >= mddev->raid_disks) |
e93f68a1 N |
3530 | rdev->new_raid_disk = -1; |
3531 | if (rdev->new_raid_disk == rdev->raid_disk) | |
3532 | continue; | |
36fad858 | 3533 | sysfs_unlink_rdev(mddev, rdev); |
e93f68a1 | 3534 | } |
dafb20fa | 3535 | rdev_for_each(rdev, mddev) { |
e93f68a1 N |
3536 | if (rdev->raid_disk < 0) |
3537 | continue; | |
3538 | if (rdev->new_raid_disk == rdev->raid_disk) | |
3539 | continue; | |
3540 | rdev->raid_disk = rdev->new_raid_disk; | |
3541 | if (rdev->raid_disk < 0) | |
3a981b03 | 3542 | clear_bit(In_sync, &rdev->flags); |
e93f68a1 | 3543 | else { |
36fad858 NK |
3544 | if (sysfs_link_rdev(mddev, rdev)) |
3545 | printk(KERN_WARNING "md: cannot register rd%d" | |
3546 | " for %s after level change\n", | |
3547 | rdev->raid_disk, mdname(mddev)); | |
3a981b03 | 3548 | } |
e93f68a1 N |
3549 | } |
3550 | ||
3551 | module_put(mddev->pers->owner); | |
245f46c2 N |
3552 | mddev->pers = pers; |
3553 | mddev->private = priv; | |
3554 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); | |
3555 | mddev->level = mddev->new_level; | |
3556 | mddev->layout = mddev->new_layout; | |
664e7c41 | 3557 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
245f46c2 | 3558 | mddev->delta_disks = 0; |
2c810cdd | 3559 | mddev->reshape_backwards = 0; |
fee68723 | 3560 | mddev->degraded = 0; |
9af204cf TM |
3561 | if (mddev->pers->sync_request == NULL) { |
3562 | /* this is now an array without redundancy, so | |
3563 | * it must always be in_sync | |
3564 | */ | |
3565 | mddev->in_sync = 1; | |
3566 | del_timer_sync(&mddev->safemode_timer); | |
3567 | } | |
02e5f5c0 | 3568 | blk_set_stacking_limits(&mddev->queue->limits); |
245f46c2 | 3569 | pers->run(mddev); |
245f46c2 | 3570 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
47525e59 | 3571 | mddev_resume(mddev); |
830778a1 N |
3572 | if (!mddev->thread) |
3573 | md_update_sb(mddev, 1); | |
5cac7861 | 3574 | sysfs_notify(&mddev->kobj, NULL, "level"); |
bb7f8d22 | 3575 | md_new_event(mddev); |
d9d166c2 N |
3576 | return rv; |
3577 | } | |
3578 | ||
3579 | static struct md_sysfs_entry md_level = | |
80ca3a44 | 3580 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
eae1701f | 3581 | |
d4dbd025 | 3582 | static ssize_t |
fd01b88c | 3583 | layout_show(struct mddev *mddev, char *page) |
d4dbd025 N |
3584 | { |
3585 | /* just a number, not meaningful for all levels */ | |
08a02ecd N |
3586 | if (mddev->reshape_position != MaxSector && |
3587 | mddev->layout != mddev->new_layout) | |
3588 | return sprintf(page, "%d (%d)\n", | |
3589 | mddev->new_layout, mddev->layout); | |
d4dbd025 N |
3590 | return sprintf(page, "%d\n", mddev->layout); |
3591 | } | |
3592 | ||
3593 | static ssize_t | |
fd01b88c | 3594 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
d4dbd025 N |
3595 | { |
3596 | char *e; | |
3597 | unsigned long n = simple_strtoul(buf, &e, 10); | |
d4dbd025 N |
3598 | |
3599 | if (!*buf || (*e && *e != '\n')) | |
3600 | return -EINVAL; | |
3601 | ||
b3546035 N |
3602 | if (mddev->pers) { |
3603 | int err; | |
50ac168a | 3604 | if (mddev->pers->check_reshape == NULL) |
b3546035 | 3605 | return -EBUSY; |
bd8839e0 N |
3606 | if (mddev->ro) |
3607 | return -EROFS; | |
597a711b | 3608 | mddev->new_layout = n; |
50ac168a | 3609 | err = mddev->pers->check_reshape(mddev); |
597a711b N |
3610 | if (err) { |
3611 | mddev->new_layout = mddev->layout; | |
b3546035 | 3612 | return err; |
597a711b | 3613 | } |
b3546035 | 3614 | } else { |
08a02ecd | 3615 | mddev->new_layout = n; |
b3546035 N |
3616 | if (mddev->reshape_position == MaxSector) |
3617 | mddev->layout = n; | |
3618 | } | |
d4dbd025 N |
3619 | return len; |
3620 | } | |
3621 | static struct md_sysfs_entry md_layout = | |
80ca3a44 | 3622 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
d4dbd025 | 3623 | |
eae1701f | 3624 | static ssize_t |
fd01b88c | 3625 | raid_disks_show(struct mddev *mddev, char *page) |
eae1701f | 3626 | { |
bb636547 N |
3627 | if (mddev->raid_disks == 0) |
3628 | return 0; | |
08a02ecd N |
3629 | if (mddev->reshape_position != MaxSector && |
3630 | mddev->delta_disks != 0) | |
3631 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, | |
3632 | mddev->raid_disks - mddev->delta_disks); | |
eae1701f N |
3633 | return sprintf(page, "%d\n", mddev->raid_disks); |
3634 | } | |
3635 | ||
fd01b88c | 3636 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
da943b99 N |
3637 | |
3638 | static ssize_t | |
fd01b88c | 3639 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
da943b99 | 3640 | { |
da943b99 N |
3641 | char *e; |
3642 | int rv = 0; | |
3643 | unsigned long n = simple_strtoul(buf, &e, 10); | |
3644 | ||
3645 | if (!*buf || (*e && *e != '\n')) | |
3646 | return -EINVAL; | |
3647 | ||
3648 | if (mddev->pers) | |
3649 | rv = update_raid_disks(mddev, n); | |
08a02ecd | 3650 | else if (mddev->reshape_position != MaxSector) { |
c6563a8c | 3651 | struct md_rdev *rdev; |
08a02ecd | 3652 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
c6563a8c N |
3653 | |
3654 | rdev_for_each(rdev, mddev) { | |
3655 | if (olddisks < n && | |
3656 | rdev->data_offset < rdev->new_data_offset) | |
3657 | return -EINVAL; | |
3658 | if (olddisks > n && | |
3659 | rdev->data_offset > rdev->new_data_offset) | |
3660 | return -EINVAL; | |
3661 | } | |
08a02ecd N |
3662 | mddev->delta_disks = n - olddisks; |
3663 | mddev->raid_disks = n; | |
2c810cdd | 3664 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
08a02ecd | 3665 | } else |
da943b99 N |
3666 | mddev->raid_disks = n; |
3667 | return rv ? rv : len; | |
3668 | } | |
3669 | static struct md_sysfs_entry md_raid_disks = | |
80ca3a44 | 3670 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
eae1701f | 3671 | |
3b34380a | 3672 | static ssize_t |
fd01b88c | 3673 | chunk_size_show(struct mddev *mddev, char *page) |
3b34380a | 3674 | { |
08a02ecd | 3675 | if (mddev->reshape_position != MaxSector && |
664e7c41 AN |
3676 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
3677 | return sprintf(page, "%d (%d)\n", | |
3678 | mddev->new_chunk_sectors << 9, | |
9d8f0363 AN |
3679 | mddev->chunk_sectors << 9); |
3680 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); | |
3b34380a N |
3681 | } |
3682 | ||
3683 | static ssize_t | |
fd01b88c | 3684 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
3b34380a | 3685 | { |
3b34380a N |
3686 | char *e; |
3687 | unsigned long n = simple_strtoul(buf, &e, 10); | |
3688 | ||
3b34380a N |
3689 | if (!*buf || (*e && *e != '\n')) |
3690 | return -EINVAL; | |
3691 | ||
b3546035 N |
3692 | if (mddev->pers) { |
3693 | int err; | |
50ac168a | 3694 | if (mddev->pers->check_reshape == NULL) |
b3546035 | 3695 | return -EBUSY; |
bd8839e0 N |
3696 | if (mddev->ro) |
3697 | return -EROFS; | |
597a711b | 3698 | mddev->new_chunk_sectors = n >> 9; |
50ac168a | 3699 | err = mddev->pers->check_reshape(mddev); |
597a711b N |
3700 | if (err) { |
3701 | mddev->new_chunk_sectors = mddev->chunk_sectors; | |
b3546035 | 3702 | return err; |
597a711b | 3703 | } |
b3546035 | 3704 | } else { |
664e7c41 | 3705 | mddev->new_chunk_sectors = n >> 9; |
b3546035 | 3706 | if (mddev->reshape_position == MaxSector) |
9d8f0363 | 3707 | mddev->chunk_sectors = n >> 9; |
b3546035 | 3708 | } |
3b34380a N |
3709 | return len; |
3710 | } | |
3711 | static struct md_sysfs_entry md_chunk_size = | |
80ca3a44 | 3712 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
3b34380a | 3713 | |
a94213b1 | 3714 | static ssize_t |
fd01b88c | 3715 | resync_start_show(struct mddev *mddev, char *page) |
a94213b1 | 3716 | { |
d1a7c503 N |
3717 | if (mddev->recovery_cp == MaxSector) |
3718 | return sprintf(page, "none\n"); | |
a94213b1 N |
3719 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
3720 | } | |
3721 | ||
3722 | static ssize_t | |
fd01b88c | 3723 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
a94213b1 | 3724 | { |
a94213b1 N |
3725 | char *e; |
3726 | unsigned long long n = simple_strtoull(buf, &e, 10); | |
3727 | ||
b098636c | 3728 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
a94213b1 | 3729 | return -EBUSY; |
06e3c817 DW |
3730 | if (cmd_match(buf, "none")) |
3731 | n = MaxSector; | |
3732 | else if (!*buf || (*e && *e != '\n')) | |
a94213b1 N |
3733 | return -EINVAL; |
3734 | ||
3735 | mddev->recovery_cp = n; | |
db07d85e N |
3736 | if (mddev->pers) |
3737 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); | |
a94213b1 N |
3738 | return len; |
3739 | } | |
3740 | static struct md_sysfs_entry md_resync_start = | |
80ca3a44 | 3741 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
a94213b1 | 3742 | |
9e653b63 N |
3743 | /* |
3744 | * The array state can be: | |
3745 | * | |
3746 | * clear | |
3747 | * No devices, no size, no level | |
3748 | * Equivalent to STOP_ARRAY ioctl | |
3749 | * inactive | |
3750 | * May have some settings, but array is not active | |
3751 | * all IO results in error | |
3752 | * When written, doesn't tear down array, but just stops it | |
3753 | * suspended (not supported yet) | |
3754 | * All IO requests will block. The array can be reconfigured. | |
910d8cb3 | 3755 | * Writing this, if accepted, will block until array is quiescent |
9e653b63 N |
3756 | * readonly |
3757 | * no resync can happen. no superblocks get written. | |
3758 | * write requests fail | |
3759 | * read-auto | |
3760 | * like readonly, but behaves like 'clean' on a write request. | |
3761 | * | |
3762 | * clean - no pending writes, but otherwise active. | |
3763 | * When written to inactive array, starts without resync | |
3764 | * If a write request arrives then | |
3765 | * if metadata is known, mark 'dirty' and switch to 'active'. | |
3766 | * if not known, block and switch to write-pending | |
3767 | * If written to an active array that has pending writes, then fails. | |
3768 | * active | |
3769 | * fully active: IO and resync can be happening. | |
3770 | * When written to inactive array, starts with resync | |
3771 | * | |
3772 | * write-pending | |
3773 | * clean, but writes are blocked waiting for 'active' to be written. | |
3774 | * | |
3775 | * active-idle | |
3776 | * like active, but no writes have been seen for a while (100msec). | |
3777 | * | |
3778 | */ | |
3779 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, | |
3780 | write_pending, active_idle, bad_word}; | |
05381954 | 3781 | static char *array_states[] = { |
9e653b63 N |
3782 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
3783 | "write-pending", "active-idle", NULL }; | |
3784 | ||
3785 | static int match_word(const char *word, char **list) | |
3786 | { | |
3787 | int n; | |
3788 | for (n=0; list[n]; n++) | |
3789 | if (cmd_match(word, list[n])) | |
3790 | break; | |
3791 | return n; | |
3792 | } | |
3793 | ||
3794 | static ssize_t | |
fd01b88c | 3795 | array_state_show(struct mddev *mddev, char *page) |
9e653b63 N |
3796 | { |
3797 | enum array_state st = inactive; | |
3798 | ||
3799 | if (mddev->pers) | |
3800 | switch(mddev->ro) { | |
3801 | case 1: | |
3802 | st = readonly; | |
3803 | break; | |
3804 | case 2: | |
3805 | st = read_auto; | |
3806 | break; | |
3807 | case 0: | |
3808 | if (mddev->in_sync) | |
3809 | st = clean; | |
070dc6dd | 3810 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
e691063a | 3811 | st = write_pending; |
9e653b63 N |
3812 | else if (mddev->safemode) |
3813 | st = active_idle; | |
3814 | else | |
3815 | st = active; | |
3816 | } | |
3817 | else { | |
3818 | if (list_empty(&mddev->disks) && | |
3819 | mddev->raid_disks == 0 && | |
58c0fed4 | 3820 | mddev->dev_sectors == 0) |
9e653b63 N |
3821 | st = clear; |
3822 | else | |
3823 | st = inactive; | |
3824 | } | |
3825 | return sprintf(page, "%s\n", array_states[st]); | |
3826 | } | |
3827 | ||
f72ffdd6 N |
3828 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
3829 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); | |
3830 | static int do_md_run(struct mddev *mddev); | |
fd01b88c | 3831 | static int restart_array(struct mddev *mddev); |
9e653b63 N |
3832 | |
3833 | static ssize_t | |
fd01b88c | 3834 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
9e653b63 N |
3835 | { |
3836 | int err = -EINVAL; | |
3837 | enum array_state st = match_word(buf, array_states); | |
3838 | switch(st) { | |
3839 | case bad_word: | |
3840 | break; | |
3841 | case clear: | |
3842 | /* stopping an active array */ | |
a05b7ea0 | 3843 | err = do_md_stop(mddev, 0, NULL); |
9e653b63 N |
3844 | break; |
3845 | case inactive: | |
3846 | /* stopping an active array */ | |
90cf195d | 3847 | if (mddev->pers) |
a05b7ea0 | 3848 | err = do_md_stop(mddev, 2, NULL); |
90cf195d | 3849 | else |
e691063a | 3850 | err = 0; /* already inactive */ |
9e653b63 N |
3851 | break; |
3852 | case suspended: | |
3853 | break; /* not supported yet */ | |
3854 | case readonly: | |
3855 | if (mddev->pers) | |
a05b7ea0 | 3856 | err = md_set_readonly(mddev, NULL); |
9e653b63 N |
3857 | else { |
3858 | mddev->ro = 1; | |
648b629e | 3859 | set_disk_ro(mddev->gendisk, 1); |
9e653b63 N |
3860 | err = do_md_run(mddev); |
3861 | } | |
3862 | break; | |
3863 | case read_auto: | |
9e653b63 | 3864 | if (mddev->pers) { |
80268ee9 | 3865 | if (mddev->ro == 0) |
a05b7ea0 | 3866 | err = md_set_readonly(mddev, NULL); |
80268ee9 | 3867 | else if (mddev->ro == 1) |
648b629e N |
3868 | err = restart_array(mddev); |
3869 | if (err == 0) { | |
3870 | mddev->ro = 2; | |
3871 | set_disk_ro(mddev->gendisk, 0); | |
3872 | } | |
9e653b63 N |
3873 | } else { |
3874 | mddev->ro = 2; | |
3875 | err = do_md_run(mddev); | |
3876 | } | |
3877 | break; | |
3878 | case clean: | |
3879 | if (mddev->pers) { | |
3880 | restart_array(mddev); | |
3881 | spin_lock_irq(&mddev->write_lock); | |
3882 | if (atomic_read(&mddev->writes_pending) == 0) { | |
e691063a N |
3883 | if (mddev->in_sync == 0) { |
3884 | mddev->in_sync = 1; | |
31a59e34 N |
3885 | if (mddev->safemode == 1) |
3886 | mddev->safemode = 0; | |
070dc6dd | 3887 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
e691063a N |
3888 | } |
3889 | err = 0; | |
3890 | } else | |
3891 | err = -EBUSY; | |
9e653b63 | 3892 | spin_unlock_irq(&mddev->write_lock); |
5bf29597 N |
3893 | } else |
3894 | err = -EINVAL; | |
9e653b63 N |
3895 | break; |
3896 | case active: | |
3897 | if (mddev->pers) { | |
3898 | restart_array(mddev); | |
070dc6dd | 3899 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
9e653b63 N |
3900 | wake_up(&mddev->sb_wait); |
3901 | err = 0; | |
3902 | } else { | |
3903 | mddev->ro = 0; | |
648b629e | 3904 | set_disk_ro(mddev->gendisk, 0); |
9e653b63 N |
3905 | err = do_md_run(mddev); |
3906 | } | |
3907 | break; | |
3908 | case write_pending: | |
3909 | case active_idle: | |
3910 | /* these cannot be set */ | |
3911 | break; | |
3912 | } | |
3913 | if (err) | |
3914 | return err; | |
0fd62b86 | 3915 | else { |
1d23f178 N |
3916 | if (mddev->hold_active == UNTIL_IOCTL) |
3917 | mddev->hold_active = 0; | |
00bcb4ac | 3918 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
9e653b63 | 3919 | return len; |
0fd62b86 | 3920 | } |
9e653b63 | 3921 | } |
80ca3a44 N |
3922 | static struct md_sysfs_entry md_array_state = |
3923 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); | |
9e653b63 | 3924 | |
1e50915f | 3925 | static ssize_t |
fd01b88c | 3926 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
1e50915f RB |
3927 | return sprintf(page, "%d\n", |
3928 | atomic_read(&mddev->max_corr_read_errors)); | |
3929 | } | |
3930 | ||
3931 | static ssize_t | |
fd01b88c | 3932 | max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len) |
1e50915f RB |
3933 | { |
3934 | char *e; | |
3935 | unsigned long n = simple_strtoul(buf, &e, 10); | |
3936 | ||
3937 | if (*buf && (*e == 0 || *e == '\n')) { | |
3938 | atomic_set(&mddev->max_corr_read_errors, n); | |
3939 | return len; | |
3940 | } | |
3941 | return -EINVAL; | |
3942 | } | |
3943 | ||
3944 | static struct md_sysfs_entry max_corr_read_errors = | |
3945 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, | |
3946 | max_corrected_read_errors_store); | |
3947 | ||
6d7ff738 | 3948 | static ssize_t |
fd01b88c | 3949 | null_show(struct mddev *mddev, char *page) |
6d7ff738 N |
3950 | { |
3951 | return -EINVAL; | |
3952 | } | |
3953 | ||
3954 | static ssize_t | |
fd01b88c | 3955 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
6d7ff738 N |
3956 | { |
3957 | /* buf must be %d:%d\n? giving major and minor numbers */ | |
3958 | /* The new device is added to the array. | |
3959 | * If the array has a persistent superblock, we read the | |
3960 | * superblock to initialise info and check validity. | |
3961 | * Otherwise, only checking done is that in bind_rdev_to_array, | |
3962 | * which mainly checks size. | |
3963 | */ | |
3964 | char *e; | |
3965 | int major = simple_strtoul(buf, &e, 10); | |
3966 | int minor; | |
3967 | dev_t dev; | |
3cb03002 | 3968 | struct md_rdev *rdev; |
6d7ff738 N |
3969 | int err; |
3970 | ||
3971 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') | |
3972 | return -EINVAL; | |
3973 | minor = simple_strtoul(e+1, &e, 10); | |
3974 | if (*e && *e != '\n') | |
3975 | return -EINVAL; | |
3976 | dev = MKDEV(major, minor); | |
3977 | if (major != MAJOR(dev) || | |
3978 | minor != MINOR(dev)) | |
3979 | return -EOVERFLOW; | |
3980 | ||
6d7ff738 N |
3981 | if (mddev->persistent) { |
3982 | rdev = md_import_device(dev, mddev->major_version, | |
3983 | mddev->minor_version); | |
3984 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { | |
3cb03002 N |
3985 | struct md_rdev *rdev0 |
3986 | = list_entry(mddev->disks.next, | |
3987 | struct md_rdev, same_set); | |
6d7ff738 N |
3988 | err = super_types[mddev->major_version] |
3989 | .load_super(rdev, rdev0, mddev->minor_version); | |
3990 | if (err < 0) | |
3991 | goto out; | |
3992 | } | |
c5d79adb N |
3993 | } else if (mddev->external) |
3994 | rdev = md_import_device(dev, -2, -1); | |
3995 | else | |
6d7ff738 N |
3996 | rdev = md_import_device(dev, -1, -1); |
3997 | ||
3998 | if (IS_ERR(rdev)) | |
3999 | return PTR_ERR(rdev); | |
4000 | err = bind_rdev_to_array(rdev, mddev); | |
4001 | out: | |
4002 | if (err) | |
4003 | export_rdev(rdev); | |
4004 | return err ? err : len; | |
4005 | } | |
4006 | ||
4007 | static struct md_sysfs_entry md_new_device = | |
80ca3a44 | 4008 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
3b34380a | 4009 | |
9b1d1dac | 4010 | static ssize_t |
fd01b88c | 4011 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
9b1d1dac PC |
4012 | { |
4013 | char *end; | |
4014 | unsigned long chunk, end_chunk; | |
4015 | ||
4016 | if (!mddev->bitmap) | |
4017 | goto out; | |
4018 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ | |
4019 | while (*buf) { | |
4020 | chunk = end_chunk = simple_strtoul(buf, &end, 0); | |
4021 | if (buf == end) break; | |
4022 | if (*end == '-') { /* range */ | |
4023 | buf = end + 1; | |
4024 | end_chunk = simple_strtoul(buf, &end, 0); | |
4025 | if (buf == end) break; | |
4026 | } | |
4027 | if (*end && !isspace(*end)) break; | |
4028 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); | |
e7d2860b | 4029 | buf = skip_spaces(end); |
9b1d1dac PC |
4030 | } |
4031 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ | |
4032 | out: | |
4033 | return len; | |
4034 | } | |
4035 | ||
4036 | static struct md_sysfs_entry md_bitmap = | |
4037 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); | |
4038 | ||
a35b0d69 | 4039 | static ssize_t |
fd01b88c | 4040 | size_show(struct mddev *mddev, char *page) |
a35b0d69 | 4041 | { |
58c0fed4 AN |
4042 | return sprintf(page, "%llu\n", |
4043 | (unsigned long long)mddev->dev_sectors / 2); | |
a35b0d69 N |
4044 | } |
4045 | ||
fd01b88c | 4046 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
a35b0d69 N |
4047 | |
4048 | static ssize_t | |
fd01b88c | 4049 | size_store(struct mddev *mddev, const char *buf, size_t len) |
a35b0d69 N |
4050 | { |
4051 | /* If array is inactive, we can reduce the component size, but | |
4052 | * not increase it (except from 0). | |
4053 | * If array is active, we can try an on-line resize | |
4054 | */ | |
b522adcd DW |
4055 | sector_t sectors; |
4056 | int err = strict_blocks_to_sectors(buf, §ors); | |
a35b0d69 | 4057 | |
58c0fed4 AN |
4058 | if (err < 0) |
4059 | return err; | |
a35b0d69 | 4060 | if (mddev->pers) { |
58c0fed4 | 4061 | err = update_size(mddev, sectors); |
850b2b42 | 4062 | md_update_sb(mddev, 1); |
a35b0d69 | 4063 | } else { |
58c0fed4 AN |
4064 | if (mddev->dev_sectors == 0 || |
4065 | mddev->dev_sectors > sectors) | |
4066 | mddev->dev_sectors = sectors; | |
a35b0d69 N |
4067 | else |
4068 | err = -ENOSPC; | |
4069 | } | |
4070 | return err ? err : len; | |
4071 | } | |
4072 | ||
4073 | static struct md_sysfs_entry md_size = | |
80ca3a44 | 4074 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
a35b0d69 | 4075 | |
83f0d77a | 4076 | /* Metadata version. |
e691063a N |
4077 | * This is one of |
4078 | * 'none' for arrays with no metadata (good luck...) | |
4079 | * 'external' for arrays with externally managed metadata, | |
8bb93aac N |
4080 | * or N.M for internally known formats |
4081 | */ | |
4082 | static ssize_t | |
fd01b88c | 4083 | metadata_show(struct mddev *mddev, char *page) |
8bb93aac N |
4084 | { |
4085 | if (mddev->persistent) | |
4086 | return sprintf(page, "%d.%d\n", | |
4087 | mddev->major_version, mddev->minor_version); | |
e691063a N |
4088 | else if (mddev->external) |
4089 | return sprintf(page, "external:%s\n", mddev->metadata_type); | |
8bb93aac N |
4090 | else |
4091 | return sprintf(page, "none\n"); | |
4092 | } | |
4093 | ||
4094 | static ssize_t | |
fd01b88c | 4095 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
8bb93aac N |
4096 | { |
4097 | int major, minor; | |
4098 | char *e; | |
ea43ddd8 N |
4099 | /* Changing the details of 'external' metadata is |
4100 | * always permitted. Otherwise there must be | |
4101 | * no devices attached to the array. | |
4102 | */ | |
4103 | if (mddev->external && strncmp(buf, "external:", 9) == 0) | |
4104 | ; | |
4105 | else if (!list_empty(&mddev->disks)) | |
8bb93aac N |
4106 | return -EBUSY; |
4107 | ||
4108 | if (cmd_match(buf, "none")) { | |
4109 | mddev->persistent = 0; | |
e691063a N |
4110 | mddev->external = 0; |
4111 | mddev->major_version = 0; | |
4112 | mddev->minor_version = 90; | |
4113 | return len; | |
4114 | } | |
4115 | if (strncmp(buf, "external:", 9) == 0) { | |
20a49ff6 | 4116 | size_t namelen = len-9; |
e691063a N |
4117 | if (namelen >= sizeof(mddev->metadata_type)) |
4118 | namelen = sizeof(mddev->metadata_type)-1; | |
4119 | strncpy(mddev->metadata_type, buf+9, namelen); | |
4120 | mddev->metadata_type[namelen] = 0; | |
4121 | if (namelen && mddev->metadata_type[namelen-1] == '\n') | |
4122 | mddev->metadata_type[--namelen] = 0; | |
4123 | mddev->persistent = 0; | |
4124 | mddev->external = 1; | |
8bb93aac N |
4125 | mddev->major_version = 0; |
4126 | mddev->minor_version = 90; | |
4127 | return len; | |
4128 | } | |
4129 | major = simple_strtoul(buf, &e, 10); | |
4130 | if (e==buf || *e != '.') | |
4131 | return -EINVAL; | |
4132 | buf = e+1; | |
4133 | minor = simple_strtoul(buf, &e, 10); | |
3f9d7b0d | 4134 | if (e==buf || (*e && *e != '\n') ) |
8bb93aac | 4135 | return -EINVAL; |
50511da3 | 4136 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
8bb93aac N |
4137 | return -ENOENT; |
4138 | mddev->major_version = major; | |
4139 | mddev->minor_version = minor; | |
4140 | mddev->persistent = 1; | |
e691063a | 4141 | mddev->external = 0; |
8bb93aac N |
4142 | return len; |
4143 | } | |
4144 | ||
4145 | static struct md_sysfs_entry md_metadata = | |
80ca3a44 | 4146 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
8bb93aac | 4147 | |
24dd469d | 4148 | static ssize_t |
fd01b88c | 4149 | action_show(struct mddev *mddev, char *page) |
24dd469d | 4150 | { |
7eec314d | 4151 | char *type = "idle"; |
b6a9ce68 N |
4152 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
4153 | type = "frozen"; | |
4154 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || | |
2b12ab6d | 4155 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
ccfcc3c1 N |
4156 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
4157 | type = "reshape"; | |
4158 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | |
24dd469d N |
4159 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
4160 | type = "resync"; | |
4161 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) | |
4162 | type = "check"; | |
4163 | else | |
4164 | type = "repair"; | |
72a23c21 | 4165 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
24dd469d N |
4166 | type = "recover"; |
4167 | } | |
4168 | return sprintf(page, "%s\n", type); | |
4169 | } | |
4170 | ||
4171 | static ssize_t | |
fd01b88c | 4172 | action_store(struct mddev *mddev, const char *page, size_t len) |
24dd469d | 4173 | { |
7eec314d N |
4174 | if (!mddev->pers || !mddev->pers->sync_request) |
4175 | return -EINVAL; | |
4176 | ||
b6a9ce68 N |
4177 | if (cmd_match(page, "frozen")) |
4178 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
4179 | else | |
4180 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
4181 | ||
4182 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { | |
7eec314d N |
4183 | if (mddev->sync_thread) { |
4184 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
a91d5ac0 | 4185 | md_reap_sync_thread(mddev); |
7eec314d | 4186 | } |
03c902e1 N |
4187 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
4188 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) | |
24dd469d | 4189 | return -EBUSY; |
72a23c21 NB |
4190 | else if (cmd_match(page, "resync")) |
4191 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
4192 | else if (cmd_match(page, "recover")) { | |
4193 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
7eec314d | 4194 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
72a23c21 | 4195 | } else if (cmd_match(page, "reshape")) { |
16484bf5 N |
4196 | int err; |
4197 | if (mddev->pers->start_reshape == NULL) | |
4198 | return -EINVAL; | |
4199 | err = mddev->pers->start_reshape(mddev); | |
4200 | if (err) | |
4201 | return err; | |
a99ac971 | 4202 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
16484bf5 | 4203 | } else { |
bce74dac | 4204 | if (cmd_match(page, "check")) |
7eec314d | 4205 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
2adc7d47 | 4206 | else if (!cmd_match(page, "repair")) |
7eec314d N |
4207 | return -EINVAL; |
4208 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); | |
4209 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
7eec314d | 4210 | } |
48c26ddc N |
4211 | if (mddev->ro == 2) { |
4212 | /* A write to sync_action is enough to justify | |
4213 | * canceling read-auto mode | |
4214 | */ | |
4215 | mddev->ro = 0; | |
4216 | md_wakeup_thread(mddev->sync_thread); | |
4217 | } | |
03c902e1 | 4218 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
24dd469d | 4219 | md_wakeup_thread(mddev->thread); |
00bcb4ac | 4220 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
24dd469d N |
4221 | return len; |
4222 | } | |
4223 | ||
c4a39551 JB |
4224 | static struct md_sysfs_entry md_scan_mode = |
4225 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); | |
4226 | ||
4227 | static ssize_t | |
4228 | last_sync_action_show(struct mddev *mddev, char *page) | |
4229 | { | |
4230 | return sprintf(page, "%s\n", mddev->last_sync_action); | |
4231 | } | |
4232 | ||
4233 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); | |
4234 | ||
9d88883e | 4235 | static ssize_t |
fd01b88c | 4236 | mismatch_cnt_show(struct mddev *mddev, char *page) |
9d88883e N |
4237 | { |
4238 | return sprintf(page, "%llu\n", | |
7f7583d4 JM |
4239 | (unsigned long long) |
4240 | atomic64_read(&mddev->resync_mismatches)); | |
9d88883e N |
4241 | } |
4242 | ||
80ca3a44 | 4243 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
9d88883e | 4244 | |
88202a0c | 4245 | static ssize_t |
fd01b88c | 4246 | sync_min_show(struct mddev *mddev, char *page) |
88202a0c N |
4247 | { |
4248 | return sprintf(page, "%d (%s)\n", speed_min(mddev), | |
4249 | mddev->sync_speed_min ? "local": "system"); | |
4250 | } | |
4251 | ||
4252 | static ssize_t | |
fd01b88c | 4253 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
88202a0c N |
4254 | { |
4255 | int min; | |
4256 | char *e; | |
4257 | if (strncmp(buf, "system", 6)==0) { | |
4258 | mddev->sync_speed_min = 0; | |
4259 | return len; | |
4260 | } | |
4261 | min = simple_strtoul(buf, &e, 10); | |
4262 | if (buf == e || (*e && *e != '\n') || min <= 0) | |
4263 | return -EINVAL; | |
4264 | mddev->sync_speed_min = min; | |
4265 | return len; | |
4266 | } | |
4267 | ||
4268 | static struct md_sysfs_entry md_sync_min = | |
4269 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); | |
4270 | ||
4271 | static ssize_t | |
fd01b88c | 4272 | sync_max_show(struct mddev *mddev, char *page) |
88202a0c N |
4273 | { |
4274 | return sprintf(page, "%d (%s)\n", speed_max(mddev), | |
4275 | mddev->sync_speed_max ? "local": "system"); | |
4276 | } | |
4277 | ||
4278 | static ssize_t | |
fd01b88c | 4279 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
88202a0c N |
4280 | { |
4281 | int max; | |
4282 | char *e; | |
4283 | if (strncmp(buf, "system", 6)==0) { | |
4284 | mddev->sync_speed_max = 0; | |
4285 | return len; | |
4286 | } | |
4287 | max = simple_strtoul(buf, &e, 10); | |
4288 | if (buf == e || (*e && *e != '\n') || max <= 0) | |
4289 | return -EINVAL; | |
4290 | mddev->sync_speed_max = max; | |
4291 | return len; | |
4292 | } | |
4293 | ||
4294 | static struct md_sysfs_entry md_sync_max = | |
4295 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); | |
4296 | ||
d7f3d291 | 4297 | static ssize_t |
fd01b88c | 4298 | degraded_show(struct mddev *mddev, char *page) |
d7f3d291 IP |
4299 | { |
4300 | return sprintf(page, "%d\n", mddev->degraded); | |
4301 | } | |
4302 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); | |
88202a0c | 4303 | |
90b08710 | 4304 | static ssize_t |
fd01b88c | 4305 | sync_force_parallel_show(struct mddev *mddev, char *page) |
90b08710 BS |
4306 | { |
4307 | return sprintf(page, "%d\n", mddev->parallel_resync); | |
4308 | } | |
4309 | ||
4310 | static ssize_t | |
fd01b88c | 4311 | sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len) |
90b08710 BS |
4312 | { |
4313 | long n; | |
4314 | ||
b29bebd6 | 4315 | if (kstrtol(buf, 10, &n)) |
90b08710 BS |
4316 | return -EINVAL; |
4317 | ||
4318 | if (n != 0 && n != 1) | |
4319 | return -EINVAL; | |
4320 | ||
4321 | mddev->parallel_resync = n; | |
4322 | ||
4323 | if (mddev->sync_thread) | |
4324 | wake_up(&resync_wait); | |
4325 | ||
4326 | return len; | |
4327 | } | |
4328 | ||
4329 | /* force parallel resync, even with shared block devices */ | |
4330 | static struct md_sysfs_entry md_sync_force_parallel = | |
4331 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, | |
4332 | sync_force_parallel_show, sync_force_parallel_store); | |
4333 | ||
88202a0c | 4334 | static ssize_t |
fd01b88c | 4335 | sync_speed_show(struct mddev *mddev, char *page) |
88202a0c N |
4336 | { |
4337 | unsigned long resync, dt, db; | |
d1a7c503 N |
4338 | if (mddev->curr_resync == 0) |
4339 | return sprintf(page, "none\n"); | |
9687a60c AN |
4340 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
4341 | dt = (jiffies - mddev->resync_mark) / HZ; | |
88202a0c | 4342 | if (!dt) dt++; |
9687a60c AN |
4343 | db = resync - mddev->resync_mark_cnt; |
4344 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ | |
88202a0c N |
4345 | } |
4346 | ||
80ca3a44 | 4347 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
88202a0c N |
4348 | |
4349 | static ssize_t | |
fd01b88c | 4350 | sync_completed_show(struct mddev *mddev, char *page) |
88202a0c | 4351 | { |
13ae864b | 4352 | unsigned long long max_sectors, resync; |
88202a0c | 4353 | |
acb180b0 N |
4354 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
4355 | return sprintf(page, "none\n"); | |
4356 | ||
72f36d59 N |
4357 | if (mddev->curr_resync == 1 || |
4358 | mddev->curr_resync == 2) | |
4359 | return sprintf(page, "delayed\n"); | |
4360 | ||
c804cdec N |
4361 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
4362 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
58c0fed4 | 4363 | max_sectors = mddev->resync_max_sectors; |
88202a0c | 4364 | else |
58c0fed4 | 4365 | max_sectors = mddev->dev_sectors; |
88202a0c | 4366 | |
acb180b0 | 4367 | resync = mddev->curr_resync_completed; |
13ae864b | 4368 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
88202a0c N |
4369 | } |
4370 | ||
80ca3a44 | 4371 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
88202a0c | 4372 | |
5e96ee65 | 4373 | static ssize_t |
fd01b88c | 4374 | min_sync_show(struct mddev *mddev, char *page) |
5e96ee65 NB |
4375 | { |
4376 | return sprintf(page, "%llu\n", | |
4377 | (unsigned long long)mddev->resync_min); | |
4378 | } | |
4379 | static ssize_t | |
fd01b88c | 4380 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
5e96ee65 NB |
4381 | { |
4382 | unsigned long long min; | |
b29bebd6 | 4383 | if (kstrtoull(buf, 10, &min)) |
5e96ee65 NB |
4384 | return -EINVAL; |
4385 | if (min > mddev->resync_max) | |
4386 | return -EINVAL; | |
4387 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) | |
4388 | return -EBUSY; | |
4389 | ||
4390 | /* Must be a multiple of chunk_size */ | |
9d8f0363 | 4391 | if (mddev->chunk_sectors) { |
2ac06c33 | 4392 | sector_t temp = min; |
9d8f0363 | 4393 | if (sector_div(temp, mddev->chunk_sectors)) |
5e96ee65 NB |
4394 | return -EINVAL; |
4395 | } | |
4396 | mddev->resync_min = min; | |
4397 | ||
4398 | return len; | |
4399 | } | |
4400 | ||
4401 | static struct md_sysfs_entry md_min_sync = | |
4402 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); | |
4403 | ||
c6207277 | 4404 | static ssize_t |
fd01b88c | 4405 | max_sync_show(struct mddev *mddev, char *page) |
c6207277 N |
4406 | { |
4407 | if (mddev->resync_max == MaxSector) | |
4408 | return sprintf(page, "max\n"); | |
4409 | else | |
4410 | return sprintf(page, "%llu\n", | |
4411 | (unsigned long long)mddev->resync_max); | |
4412 | } | |
4413 | static ssize_t | |
fd01b88c | 4414 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
c6207277 N |
4415 | { |
4416 | if (strncmp(buf, "max", 3) == 0) | |
4417 | mddev->resync_max = MaxSector; | |
4418 | else { | |
5e96ee65 | 4419 | unsigned long long max; |
b29bebd6 | 4420 | if (kstrtoull(buf, 10, &max)) |
5e96ee65 NB |
4421 | return -EINVAL; |
4422 | if (max < mddev->resync_min) | |
c6207277 N |
4423 | return -EINVAL; |
4424 | if (max < mddev->resync_max && | |
4d484a4a | 4425 | mddev->ro == 0 && |
c6207277 N |
4426 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
4427 | return -EBUSY; | |
4428 | ||
4429 | /* Must be a multiple of chunk_size */ | |
9d8f0363 | 4430 | if (mddev->chunk_sectors) { |
2ac06c33 | 4431 | sector_t temp = max; |
9d8f0363 | 4432 | if (sector_div(temp, mddev->chunk_sectors)) |
c6207277 N |
4433 | return -EINVAL; |
4434 | } | |
4435 | mddev->resync_max = max; | |
4436 | } | |
4437 | wake_up(&mddev->recovery_wait); | |
4438 | return len; | |
4439 | } | |
4440 | ||
4441 | static struct md_sysfs_entry md_max_sync = | |
4442 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); | |
4443 | ||
e464eafd | 4444 | static ssize_t |
fd01b88c | 4445 | suspend_lo_show(struct mddev *mddev, char *page) |
e464eafd N |
4446 | { |
4447 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); | |
4448 | } | |
4449 | ||
4450 | static ssize_t | |
fd01b88c | 4451 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
e464eafd N |
4452 | { |
4453 | char *e; | |
4454 | unsigned long long new = simple_strtoull(buf, &e, 10); | |
23ddff37 | 4455 | unsigned long long old = mddev->suspend_lo; |
e464eafd | 4456 | |
f72ffdd6 | 4457 | if (mddev->pers == NULL || |
b8d966ef | 4458 | mddev->pers->quiesce == NULL) |
e464eafd N |
4459 | return -EINVAL; |
4460 | if (buf == e || (*e && *e != '\n')) | |
4461 | return -EINVAL; | |
23ddff37 N |
4462 | |
4463 | mddev->suspend_lo = new; | |
4464 | if (new >= old) | |
4465 | /* Shrinking suspended region */ | |
e464eafd | 4466 | mddev->pers->quiesce(mddev, 2); |
23ddff37 N |
4467 | else { |
4468 | /* Expanding suspended region - need to wait */ | |
4469 | mddev->pers->quiesce(mddev, 1); | |
4470 | mddev->pers->quiesce(mddev, 0); | |
4471 | } | |
4472 | return len; | |
e464eafd N |
4473 | } |
4474 | static struct md_sysfs_entry md_suspend_lo = | |
4475 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); | |
4476 | ||
e464eafd | 4477 | static ssize_t |
fd01b88c | 4478 | suspend_hi_show(struct mddev *mddev, char *page) |
e464eafd N |
4479 | { |
4480 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); | |
4481 | } | |
4482 | ||
4483 | static ssize_t | |
fd01b88c | 4484 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
e464eafd N |
4485 | { |
4486 | char *e; | |
4487 | unsigned long long new = simple_strtoull(buf, &e, 10); | |
23ddff37 | 4488 | unsigned long long old = mddev->suspend_hi; |
e464eafd | 4489 | |
b8d966ef N |
4490 | if (mddev->pers == NULL || |
4491 | mddev->pers->quiesce == NULL) | |
e464eafd N |
4492 | return -EINVAL; |
4493 | if (buf == e || (*e && *e != '\n')) | |
4494 | return -EINVAL; | |
23ddff37 N |
4495 | |
4496 | mddev->suspend_hi = new; | |
4497 | if (new <= old) | |
4498 | /* Shrinking suspended region */ | |
4499 | mddev->pers->quiesce(mddev, 2); | |
4500 | else { | |
4501 | /* Expanding suspended region - need to wait */ | |
e464eafd N |
4502 | mddev->pers->quiesce(mddev, 1); |
4503 | mddev->pers->quiesce(mddev, 0); | |
23ddff37 N |
4504 | } |
4505 | return len; | |
e464eafd N |
4506 | } |
4507 | static struct md_sysfs_entry md_suspend_hi = | |
4508 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); | |
4509 | ||
08a02ecd | 4510 | static ssize_t |
fd01b88c | 4511 | reshape_position_show(struct mddev *mddev, char *page) |
08a02ecd N |
4512 | { |
4513 | if (mddev->reshape_position != MaxSector) | |
4514 | return sprintf(page, "%llu\n", | |
4515 | (unsigned long long)mddev->reshape_position); | |
4516 | strcpy(page, "none\n"); | |
4517 | return 5; | |
4518 | } | |
4519 | ||
4520 | static ssize_t | |
fd01b88c | 4521 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
08a02ecd | 4522 | { |
c6563a8c | 4523 | struct md_rdev *rdev; |
08a02ecd N |
4524 | char *e; |
4525 | unsigned long long new = simple_strtoull(buf, &e, 10); | |
4526 | if (mddev->pers) | |
4527 | return -EBUSY; | |
4528 | if (buf == e || (*e && *e != '\n')) | |
4529 | return -EINVAL; | |
4530 | mddev->reshape_position = new; | |
4531 | mddev->delta_disks = 0; | |
2c810cdd | 4532 | mddev->reshape_backwards = 0; |
08a02ecd N |
4533 | mddev->new_level = mddev->level; |
4534 | mddev->new_layout = mddev->layout; | |
664e7c41 | 4535 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
c6563a8c N |
4536 | rdev_for_each(rdev, mddev) |
4537 | rdev->new_data_offset = rdev->data_offset; | |
08a02ecd N |
4538 | return len; |
4539 | } | |
4540 | ||
4541 | static struct md_sysfs_entry md_reshape_position = | |
4542 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, | |
4543 | reshape_position_store); | |
4544 | ||
2c810cdd N |
4545 | static ssize_t |
4546 | reshape_direction_show(struct mddev *mddev, char *page) | |
4547 | { | |
4548 | return sprintf(page, "%s\n", | |
4549 | mddev->reshape_backwards ? "backwards" : "forwards"); | |
4550 | } | |
4551 | ||
4552 | static ssize_t | |
4553 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) | |
4554 | { | |
4555 | int backwards = 0; | |
4556 | if (cmd_match(buf, "forwards")) | |
4557 | backwards = 0; | |
4558 | else if (cmd_match(buf, "backwards")) | |
4559 | backwards = 1; | |
4560 | else | |
4561 | return -EINVAL; | |
4562 | if (mddev->reshape_backwards == backwards) | |
4563 | return len; | |
4564 | ||
4565 | /* check if we are allowed to change */ | |
4566 | if (mddev->delta_disks) | |
4567 | return -EBUSY; | |
4568 | ||
4569 | if (mddev->persistent && | |
4570 | mddev->major_version == 0) | |
4571 | return -EINVAL; | |
4572 | ||
4573 | mddev->reshape_backwards = backwards; | |
4574 | return len; | |
4575 | } | |
4576 | ||
4577 | static struct md_sysfs_entry md_reshape_direction = | |
4578 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, | |
4579 | reshape_direction_store); | |
4580 | ||
b522adcd | 4581 | static ssize_t |
fd01b88c | 4582 | array_size_show(struct mddev *mddev, char *page) |
b522adcd DW |
4583 | { |
4584 | if (mddev->external_size) | |
4585 | return sprintf(page, "%llu\n", | |
4586 | (unsigned long long)mddev->array_sectors/2); | |
4587 | else | |
4588 | return sprintf(page, "default\n"); | |
4589 | } | |
4590 | ||
4591 | static ssize_t | |
fd01b88c | 4592 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
b522adcd DW |
4593 | { |
4594 | sector_t sectors; | |
4595 | ||
4596 | if (strncmp(buf, "default", 7) == 0) { | |
4597 | if (mddev->pers) | |
4598 | sectors = mddev->pers->size(mddev, 0, 0); | |
4599 | else | |
4600 | sectors = mddev->array_sectors; | |
4601 | ||
4602 | mddev->external_size = 0; | |
4603 | } else { | |
4604 | if (strict_blocks_to_sectors(buf, §ors) < 0) | |
4605 | return -EINVAL; | |
4606 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) | |
2b69c839 | 4607 | return -E2BIG; |
b522adcd DW |
4608 | |
4609 | mddev->external_size = 1; | |
4610 | } | |
4611 | ||
4612 | mddev->array_sectors = sectors; | |
cbe6ef1d N |
4613 | if (mddev->pers) { |
4614 | set_capacity(mddev->gendisk, mddev->array_sectors); | |
449aad3e | 4615 | revalidate_disk(mddev->gendisk); |
cbe6ef1d | 4616 | } |
b522adcd DW |
4617 | return len; |
4618 | } | |
4619 | ||
4620 | static struct md_sysfs_entry md_array_size = | |
4621 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, | |
4622 | array_size_store); | |
e464eafd | 4623 | |
eae1701f N |
4624 | static struct attribute *md_default_attrs[] = { |
4625 | &md_level.attr, | |
d4dbd025 | 4626 | &md_layout.attr, |
eae1701f | 4627 | &md_raid_disks.attr, |
3b34380a | 4628 | &md_chunk_size.attr, |
a35b0d69 | 4629 | &md_size.attr, |
a94213b1 | 4630 | &md_resync_start.attr, |
8bb93aac | 4631 | &md_metadata.attr, |
6d7ff738 | 4632 | &md_new_device.attr, |
16f17b39 | 4633 | &md_safe_delay.attr, |
9e653b63 | 4634 | &md_array_state.attr, |
08a02ecd | 4635 | &md_reshape_position.attr, |
2c810cdd | 4636 | &md_reshape_direction.attr, |
b522adcd | 4637 | &md_array_size.attr, |
1e50915f | 4638 | &max_corr_read_errors.attr, |
411036fa N |
4639 | NULL, |
4640 | }; | |
4641 | ||
4642 | static struct attribute *md_redundancy_attrs[] = { | |
24dd469d | 4643 | &md_scan_mode.attr, |
c4a39551 | 4644 | &md_last_scan_mode.attr, |
9d88883e | 4645 | &md_mismatches.attr, |
88202a0c N |
4646 | &md_sync_min.attr, |
4647 | &md_sync_max.attr, | |
4648 | &md_sync_speed.attr, | |
90b08710 | 4649 | &md_sync_force_parallel.attr, |
88202a0c | 4650 | &md_sync_completed.attr, |
5e96ee65 | 4651 | &md_min_sync.attr, |
c6207277 | 4652 | &md_max_sync.attr, |
e464eafd N |
4653 | &md_suspend_lo.attr, |
4654 | &md_suspend_hi.attr, | |
9b1d1dac | 4655 | &md_bitmap.attr, |
d7f3d291 | 4656 | &md_degraded.attr, |
eae1701f N |
4657 | NULL, |
4658 | }; | |
411036fa N |
4659 | static struct attribute_group md_redundancy_group = { |
4660 | .name = NULL, | |
4661 | .attrs = md_redundancy_attrs, | |
4662 | }; | |
4663 | ||
eae1701f N |
4664 | static ssize_t |
4665 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
4666 | { | |
4667 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); | |
fd01b88c | 4668 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
96de1e66 | 4669 | ssize_t rv; |
eae1701f N |
4670 | |
4671 | if (!entry->show) | |
4672 | return -EIO; | |
af8a2434 N |
4673 | spin_lock(&all_mddevs_lock); |
4674 | if (list_empty(&mddev->all_mddevs)) { | |
4675 | spin_unlock(&all_mddevs_lock); | |
4676 | return -EBUSY; | |
4677 | } | |
4678 | mddev_get(mddev); | |
4679 | spin_unlock(&all_mddevs_lock); | |
4680 | ||
5dc5cf7d IM |
4681 | rv = mddev_lock(mddev); |
4682 | if (!rv) { | |
4683 | rv = entry->show(mddev, page); | |
4684 | mddev_unlock(mddev); | |
4685 | } | |
af8a2434 | 4686 | mddev_put(mddev); |
96de1e66 | 4687 | return rv; |
eae1701f N |
4688 | } |
4689 | ||
4690 | static ssize_t | |
4691 | md_attr_store(struct kobject *kobj, struct attribute *attr, | |
4692 | const char *page, size_t length) | |
4693 | { | |
4694 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); | |
fd01b88c | 4695 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
96de1e66 | 4696 | ssize_t rv; |
eae1701f N |
4697 | |
4698 | if (!entry->store) | |
4699 | return -EIO; | |
67463acb N |
4700 | if (!capable(CAP_SYS_ADMIN)) |
4701 | return -EACCES; | |
af8a2434 N |
4702 | spin_lock(&all_mddevs_lock); |
4703 | if (list_empty(&mddev->all_mddevs)) { | |
4704 | spin_unlock(&all_mddevs_lock); | |
4705 | return -EBUSY; | |
4706 | } | |
4707 | mddev_get(mddev); | |
4708 | spin_unlock(&all_mddevs_lock); | |
a7a3f08d N |
4709 | if (entry->store == new_dev_store) |
4710 | flush_workqueue(md_misc_wq); | |
5dc5cf7d IM |
4711 | rv = mddev_lock(mddev); |
4712 | if (!rv) { | |
4713 | rv = entry->store(mddev, page, length); | |
4714 | mddev_unlock(mddev); | |
4715 | } | |
af8a2434 | 4716 | mddev_put(mddev); |
96de1e66 | 4717 | return rv; |
eae1701f N |
4718 | } |
4719 | ||
4720 | static void md_free(struct kobject *ko) | |
4721 | { | |
fd01b88c | 4722 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
a21d1504 N |
4723 | |
4724 | if (mddev->sysfs_state) | |
4725 | sysfs_put(mddev->sysfs_state); | |
4726 | ||
4727 | if (mddev->gendisk) { | |
4728 | del_gendisk(mddev->gendisk); | |
4729 | put_disk(mddev->gendisk); | |
4730 | } | |
4731 | if (mddev->queue) | |
4732 | blk_cleanup_queue(mddev->queue); | |
4733 | ||
eae1701f N |
4734 | kfree(mddev); |
4735 | } | |
4736 | ||
52cf25d0 | 4737 | static const struct sysfs_ops md_sysfs_ops = { |
eae1701f N |
4738 | .show = md_attr_show, |
4739 | .store = md_attr_store, | |
4740 | }; | |
4741 | static struct kobj_type md_ktype = { | |
4742 | .release = md_free, | |
4743 | .sysfs_ops = &md_sysfs_ops, | |
4744 | .default_attrs = md_default_attrs, | |
4745 | }; | |
4746 | ||
1da177e4 LT |
4747 | int mdp_major = 0; |
4748 | ||
5fd3a17e DW |
4749 | static void mddev_delayed_delete(struct work_struct *ws) |
4750 | { | |
fd01b88c | 4751 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
5fd3a17e | 4752 | |
43a70507 | 4753 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
5fd3a17e DW |
4754 | kobject_del(&mddev->kobj); |
4755 | kobject_put(&mddev->kobj); | |
4756 | } | |
4757 | ||
efeb53c0 | 4758 | static int md_alloc(dev_t dev, char *name) |
1da177e4 | 4759 | { |
48c9c27b | 4760 | static DEFINE_MUTEX(disks_mutex); |
fd01b88c | 4761 | struct mddev *mddev = mddev_find(dev); |
1da177e4 | 4762 | struct gendisk *disk; |
efeb53c0 N |
4763 | int partitioned; |
4764 | int shift; | |
4765 | int unit; | |
3830c62f | 4766 | int error; |
1da177e4 LT |
4767 | |
4768 | if (!mddev) | |
efeb53c0 N |
4769 | return -ENODEV; |
4770 | ||
4771 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); | |
4772 | shift = partitioned ? MdpMinorShift : 0; | |
4773 | unit = MINOR(mddev->unit) >> shift; | |
1da177e4 | 4774 | |
e804ac78 TH |
4775 | /* wait for any previous instance of this device to be |
4776 | * completely removed (mddev_delayed_delete). | |
d3374825 | 4777 | */ |
e804ac78 | 4778 | flush_workqueue(md_misc_wq); |
d3374825 | 4779 | |
48c9c27b | 4780 | mutex_lock(&disks_mutex); |
0909dc44 N |
4781 | error = -EEXIST; |
4782 | if (mddev->gendisk) | |
4783 | goto abort; | |
efeb53c0 N |
4784 | |
4785 | if (name) { | |
4786 | /* Need to ensure that 'name' is not a duplicate. | |
4787 | */ | |
fd01b88c | 4788 | struct mddev *mddev2; |
efeb53c0 N |
4789 | spin_lock(&all_mddevs_lock); |
4790 | ||
4791 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) | |
4792 | if (mddev2->gendisk && | |
4793 | strcmp(mddev2->gendisk->disk_name, name) == 0) { | |
4794 | spin_unlock(&all_mddevs_lock); | |
0909dc44 | 4795 | goto abort; |
efeb53c0 N |
4796 | } |
4797 | spin_unlock(&all_mddevs_lock); | |
1da177e4 | 4798 | } |
8b765398 | 4799 | |
0909dc44 | 4800 | error = -ENOMEM; |
8b765398 | 4801 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
0909dc44 N |
4802 | if (!mddev->queue) |
4803 | goto abort; | |
409c57f3 N |
4804 | mddev->queue->queuedata = mddev; |
4805 | ||
409c57f3 | 4806 | blk_queue_make_request(mddev->queue, md_make_request); |
b1bd055d | 4807 | blk_set_stacking_limits(&mddev->queue->limits); |
8b765398 | 4808 | |
1da177e4 LT |
4809 | disk = alloc_disk(1 << shift); |
4810 | if (!disk) { | |
8b765398 N |
4811 | blk_cleanup_queue(mddev->queue); |
4812 | mddev->queue = NULL; | |
0909dc44 | 4813 | goto abort; |
1da177e4 | 4814 | } |
efeb53c0 | 4815 | disk->major = MAJOR(mddev->unit); |
1da177e4 | 4816 | disk->first_minor = unit << shift; |
efeb53c0 N |
4817 | if (name) |
4818 | strcpy(disk->disk_name, name); | |
4819 | else if (partitioned) | |
1da177e4 | 4820 | sprintf(disk->disk_name, "md_d%d", unit); |
ce7b0f46 | 4821 | else |
1da177e4 | 4822 | sprintf(disk->disk_name, "md%d", unit); |
1da177e4 LT |
4823 | disk->fops = &md_fops; |
4824 | disk->private_data = mddev; | |
4825 | disk->queue = mddev->queue; | |
b0140891 | 4826 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
92850bbd | 4827 | /* Allow extended partitions. This makes the |
d3374825 | 4828 | * 'mdp' device redundant, but we can't really |
92850bbd N |
4829 | * remove it now. |
4830 | */ | |
4831 | disk->flags |= GENHD_FL_EXT_DEVT; | |
1da177e4 | 4832 | mddev->gendisk = disk; |
b0140891 N |
4833 | /* As soon as we call add_disk(), another thread could get |
4834 | * through to md_open, so make sure it doesn't get too far | |
4835 | */ | |
4836 | mutex_lock(&mddev->open_mutex); | |
4837 | add_disk(disk); | |
4838 | ||
ed9e1982 TH |
4839 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
4840 | &disk_to_dev(disk)->kobj, "%s", "md"); | |
0909dc44 N |
4841 | if (error) { |
4842 | /* This isn't possible, but as kobject_init_and_add is marked | |
4843 | * __must_check, we must do something with the result | |
4844 | */ | |
5e55e2f5 N |
4845 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
4846 | disk->disk_name); | |
0909dc44 N |
4847 | error = 0; |
4848 | } | |
00bcb4ac N |
4849 | if (mddev->kobj.sd && |
4850 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) | |
43a70507 | 4851 | printk(KERN_DEBUG "pointless warning\n"); |
b0140891 | 4852 | mutex_unlock(&mddev->open_mutex); |
0909dc44 N |
4853 | abort: |
4854 | mutex_unlock(&disks_mutex); | |
00bcb4ac | 4855 | if (!error && mddev->kobj.sd) { |
3830c62f | 4856 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
00bcb4ac | 4857 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
b62b7590 | 4858 | } |
d3374825 | 4859 | mddev_put(mddev); |
0909dc44 | 4860 | return error; |
efeb53c0 N |
4861 | } |
4862 | ||
4863 | static struct kobject *md_probe(dev_t dev, int *part, void *data) | |
4864 | { | |
4865 | md_alloc(dev, NULL); | |
1da177e4 LT |
4866 | return NULL; |
4867 | } | |
4868 | ||
efeb53c0 N |
4869 | static int add_named_array(const char *val, struct kernel_param *kp) |
4870 | { | |
4871 | /* val must be "md_*" where * is not all digits. | |
4872 | * We allocate an array with a large free minor number, and | |
4873 | * set the name to val. val must not already be an active name. | |
4874 | */ | |
4875 | int len = strlen(val); | |
4876 | char buf[DISK_NAME_LEN]; | |
4877 | ||
4878 | while (len && val[len-1] == '\n') | |
4879 | len--; | |
4880 | if (len >= DISK_NAME_LEN) | |
4881 | return -E2BIG; | |
4882 | strlcpy(buf, val, len+1); | |
4883 | if (strncmp(buf, "md_", 3) != 0) | |
4884 | return -EINVAL; | |
4885 | return md_alloc(0, buf); | |
4886 | } | |
4887 | ||
1da177e4 LT |
4888 | static void md_safemode_timeout(unsigned long data) |
4889 | { | |
fd01b88c | 4890 | struct mddev *mddev = (struct mddev *) data; |
1da177e4 | 4891 | |
0fd62b86 NB |
4892 | if (!atomic_read(&mddev->writes_pending)) { |
4893 | mddev->safemode = 1; | |
4894 | if (mddev->external) | |
00bcb4ac | 4895 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0fd62b86 | 4896 | } |
1da177e4 LT |
4897 | md_wakeup_thread(mddev->thread); |
4898 | } | |
4899 | ||
6ff8d8ec | 4900 | static int start_dirty_degraded; |
1da177e4 | 4901 | |
fd01b88c | 4902 | int md_run(struct mddev *mddev) |
1da177e4 | 4903 | { |
2604b703 | 4904 | int err; |
3cb03002 | 4905 | struct md_rdev *rdev; |
84fc4b56 | 4906 | struct md_personality *pers; |
1da177e4 | 4907 | |
a757e64c N |
4908 | if (list_empty(&mddev->disks)) |
4909 | /* cannot run an array with no devices.. */ | |
1da177e4 | 4910 | return -EINVAL; |
1da177e4 LT |
4911 | |
4912 | if (mddev->pers) | |
4913 | return -EBUSY; | |
bb4f1e9d N |
4914 | /* Cannot run until previous stop completes properly */ |
4915 | if (mddev->sysfs_active) | |
4916 | return -EBUSY; | |
b6eb127d | 4917 | |
1da177e4 LT |
4918 | /* |
4919 | * Analyze all RAID superblock(s) | |
4920 | */ | |
1ec4a939 N |
4921 | if (!mddev->raid_disks) { |
4922 | if (!mddev->persistent) | |
4923 | return -EINVAL; | |
a757e64c | 4924 | analyze_sbs(mddev); |
1ec4a939 | 4925 | } |
1da177e4 | 4926 | |
d9d166c2 N |
4927 | if (mddev->level != LEVEL_NONE) |
4928 | request_module("md-level-%d", mddev->level); | |
4929 | else if (mddev->clevel[0]) | |
4930 | request_module("md-%s", mddev->clevel); | |
1da177e4 LT |
4931 | |
4932 | /* | |
4933 | * Drop all container device buffers, from now on | |
4934 | * the only valid external interface is through the md | |
4935 | * device. | |
1da177e4 | 4936 | */ |
dafb20fa | 4937 | rdev_for_each(rdev, mddev) { |
b2d444d7 | 4938 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 LT |
4939 | continue; |
4940 | sync_blockdev(rdev->bdev); | |
f98393a6 | 4941 | invalidate_bdev(rdev->bdev); |
f0d76d70 N |
4942 | |
4943 | /* perform some consistency tests on the device. | |
4944 | * We don't want the data to overlap the metadata, | |
58c0fed4 | 4945 | * Internal Bitmap issues have been handled elsewhere. |
f0d76d70 | 4946 | */ |
a6ff7e08 JB |
4947 | if (rdev->meta_bdev) { |
4948 | /* Nothing to check */; | |
4949 | } else if (rdev->data_offset < rdev->sb_start) { | |
58c0fed4 AN |
4950 | if (mddev->dev_sectors && |
4951 | rdev->data_offset + mddev->dev_sectors | |
0f420358 | 4952 | > rdev->sb_start) { |
f0d76d70 N |
4953 | printk("md: %s: data overlaps metadata\n", |
4954 | mdname(mddev)); | |
4955 | return -EINVAL; | |
4956 | } | |
4957 | } else { | |
0f420358 | 4958 | if (rdev->sb_start + rdev->sb_size/512 |
f0d76d70 N |
4959 | > rdev->data_offset) { |
4960 | printk("md: %s: metadata overlaps data\n", | |
4961 | mdname(mddev)); | |
4962 | return -EINVAL; | |
4963 | } | |
4964 | } | |
00bcb4ac | 4965 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
1da177e4 LT |
4966 | } |
4967 | ||
a167f663 | 4968 | if (mddev->bio_set == NULL) |
395c72a7 | 4969 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
a167f663 | 4970 | |
1da177e4 | 4971 | spin_lock(&pers_lock); |
d9d166c2 | 4972 | pers = find_pers(mddev->level, mddev->clevel); |
2604b703 | 4973 | if (!pers || !try_module_get(pers->owner)) { |
1da177e4 | 4974 | spin_unlock(&pers_lock); |
d9d166c2 N |
4975 | if (mddev->level != LEVEL_NONE) |
4976 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", | |
4977 | mddev->level); | |
4978 | else | |
4979 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", | |
4980 | mddev->clevel); | |
1da177e4 LT |
4981 | return -EINVAL; |
4982 | } | |
2604b703 | 4983 | mddev->pers = pers; |
1da177e4 | 4984 | spin_unlock(&pers_lock); |
34817e8c N |
4985 | if (mddev->level != pers->level) { |
4986 | mddev->level = pers->level; | |
4987 | mddev->new_level = pers->level; | |
4988 | } | |
d9d166c2 | 4989 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
1da177e4 | 4990 | |
f6705578 | 4991 | if (mddev->reshape_position != MaxSector && |
63c70c4f | 4992 | pers->start_reshape == NULL) { |
f6705578 N |
4993 | /* This personality cannot handle reshaping... */ |
4994 | mddev->pers = NULL; | |
4995 | module_put(pers->owner); | |
4996 | return -EINVAL; | |
4997 | } | |
4998 | ||
7dd5e7c3 N |
4999 | if (pers->sync_request) { |
5000 | /* Warn if this is a potentially silly | |
5001 | * configuration. | |
5002 | */ | |
5003 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
3cb03002 | 5004 | struct md_rdev *rdev2; |
7dd5e7c3 | 5005 | int warned = 0; |
159ec1fc | 5006 | |
dafb20fa N |
5007 | rdev_for_each(rdev, mddev) |
5008 | rdev_for_each(rdev2, mddev) { | |
7dd5e7c3 N |
5009 | if (rdev < rdev2 && |
5010 | rdev->bdev->bd_contains == | |
5011 | rdev2->bdev->bd_contains) { | |
5012 | printk(KERN_WARNING | |
5013 | "%s: WARNING: %s appears to be" | |
5014 | " on the same physical disk as" | |
5015 | " %s.\n", | |
5016 | mdname(mddev), | |
5017 | bdevname(rdev->bdev,b), | |
5018 | bdevname(rdev2->bdev,b2)); | |
5019 | warned = 1; | |
5020 | } | |
5021 | } | |
159ec1fc | 5022 | |
7dd5e7c3 N |
5023 | if (warned) |
5024 | printk(KERN_WARNING | |
5025 | "True protection against single-disk" | |
5026 | " failure might be compromised.\n"); | |
5027 | } | |
5028 | ||
657390d2 | 5029 | mddev->recovery = 0; |
58c0fed4 AN |
5030 | /* may be over-ridden by personality */ |
5031 | mddev->resync_max_sectors = mddev->dev_sectors; | |
5032 | ||
6ff8d8ec | 5033 | mddev->ok_start_degraded = start_dirty_degraded; |
1da177e4 | 5034 | |
0f9552b5 | 5035 | if (start_readonly && mddev->ro == 0) |
f91de92e N |
5036 | mddev->ro = 2; /* read-only, but switch on first write */ |
5037 | ||
b15c2e57 | 5038 | err = mddev->pers->run(mddev); |
13e53df3 AN |
5039 | if (err) |
5040 | printk(KERN_ERR "md: pers->run() failed ...\n"); | |
b522adcd DW |
5041 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
5042 | WARN_ONCE(!mddev->external_size, "%s: default size too small," | |
5043 | " but 'external_size' not in effect?\n", __func__); | |
5044 | printk(KERN_ERR | |
5045 | "md: invalid array_size %llu > default size %llu\n", | |
5046 | (unsigned long long)mddev->array_sectors / 2, | |
5047 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); | |
5048 | err = -EINVAL; | |
5049 | mddev->pers->stop(mddev); | |
5050 | } | |
ef99bf48 N |
5051 | if (err == 0 && mddev->pers->sync_request && |
5052 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { | |
b15c2e57 N |
5053 | err = bitmap_create(mddev); |
5054 | if (err) { | |
5055 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", | |
5056 | mdname(mddev), err); | |
5057 | mddev->pers->stop(mddev); | |
5058 | } | |
5059 | } | |
1da177e4 | 5060 | if (err) { |
1da177e4 LT |
5061 | module_put(mddev->pers->owner); |
5062 | mddev->pers = NULL; | |
32a7627c N |
5063 | bitmap_destroy(mddev); |
5064 | return err; | |
1da177e4 | 5065 | } |
5e55e2f5 | 5066 | if (mddev->pers->sync_request) { |
00bcb4ac N |
5067 | if (mddev->kobj.sd && |
5068 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) | |
5e55e2f5 N |
5069 | printk(KERN_WARNING |
5070 | "md: cannot register extra attributes for %s\n", | |
5071 | mdname(mddev)); | |
00bcb4ac | 5072 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
5e55e2f5 | 5073 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
fd9d49ca N |
5074 | mddev->ro = 0; |
5075 | ||
f72ffdd6 | 5076 | atomic_set(&mddev->writes_pending,0); |
1e50915f RB |
5077 | atomic_set(&mddev->max_corr_read_errors, |
5078 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); | |
1da177e4 LT |
5079 | mddev->safemode = 0; |
5080 | mddev->safemode_timer.function = md_safemode_timeout; | |
5081 | mddev->safemode_timer.data = (unsigned long) mddev; | |
16f17b39 | 5082 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
1da177e4 | 5083 | mddev->in_sync = 1; |
0ca69886 N |
5084 | smp_wmb(); |
5085 | mddev->ready = 1; | |
dafb20fa | 5086 | rdev_for_each(rdev, mddev) |
36fad858 NK |
5087 | if (rdev->raid_disk >= 0) |
5088 | if (sysfs_link_rdev(mddev, rdev)) | |
00bcb4ac | 5089 | /* failure here is OK */; |
f72ffdd6 | 5090 | |
1da177e4 | 5091 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
f72ffdd6 | 5092 | |
7a0a5355 | 5093 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
850b2b42 | 5094 | md_update_sb(mddev, 0); |
1da177e4 | 5095 | |
d7603b7e | 5096 | md_new_event(mddev); |
00bcb4ac N |
5097 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
5098 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
a99ac971 | 5099 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
1da177e4 LT |
5100 | return 0; |
5101 | } | |
390ee602 | 5102 | EXPORT_SYMBOL_GPL(md_run); |
1da177e4 | 5103 | |
fd01b88c | 5104 | static int do_md_run(struct mddev *mddev) |
fe60b014 N |
5105 | { |
5106 | int err; | |
5107 | ||
5108 | err = md_run(mddev); | |
5109 | if (err) | |
5110 | goto out; | |
69e51b44 N |
5111 | err = bitmap_load(mddev); |
5112 | if (err) { | |
5113 | bitmap_destroy(mddev); | |
5114 | goto out; | |
5115 | } | |
0fd018af JB |
5116 | |
5117 | md_wakeup_thread(mddev->thread); | |
5118 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ | |
5119 | ||
fe60b014 N |
5120 | set_capacity(mddev->gendisk, mddev->array_sectors); |
5121 | revalidate_disk(mddev->gendisk); | |
f0b4f7e2 | 5122 | mddev->changed = 1; |
fe60b014 N |
5123 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
5124 | out: | |
5125 | return err; | |
5126 | } | |
5127 | ||
fd01b88c | 5128 | static int restart_array(struct mddev *mddev) |
1da177e4 LT |
5129 | { |
5130 | struct gendisk *disk = mddev->gendisk; | |
1da177e4 | 5131 | |
80fab1d7 | 5132 | /* Complain if it has no devices */ |
1da177e4 | 5133 | if (list_empty(&mddev->disks)) |
80fab1d7 AN |
5134 | return -ENXIO; |
5135 | if (!mddev->pers) | |
5136 | return -EINVAL; | |
5137 | if (!mddev->ro) | |
5138 | return -EBUSY; | |
5139 | mddev->safemode = 0; | |
5140 | mddev->ro = 0; | |
5141 | set_disk_ro(disk, 0); | |
5142 | printk(KERN_INFO "md: %s switched to read-write mode.\n", | |
5143 | mdname(mddev)); | |
5144 | /* Kick recovery or resync if necessary */ | |
5145 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
5146 | md_wakeup_thread(mddev->thread); | |
5147 | md_wakeup_thread(mddev->sync_thread); | |
00bcb4ac | 5148 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
80fab1d7 | 5149 | return 0; |
1da177e4 LT |
5150 | } |
5151 | ||
fd01b88c | 5152 | static void md_clean(struct mddev *mddev) |
6177b472 N |
5153 | { |
5154 | mddev->array_sectors = 0; | |
5155 | mddev->external_size = 0; | |
5156 | mddev->dev_sectors = 0; | |
5157 | mddev->raid_disks = 0; | |
5158 | mddev->recovery_cp = 0; | |
5159 | mddev->resync_min = 0; | |
5160 | mddev->resync_max = MaxSector; | |
5161 | mddev->reshape_position = MaxSector; | |
5162 | mddev->external = 0; | |
5163 | mddev->persistent = 0; | |
5164 | mddev->level = LEVEL_NONE; | |
5165 | mddev->clevel[0] = 0; | |
5166 | mddev->flags = 0; | |
5167 | mddev->ro = 0; | |
5168 | mddev->metadata_type[0] = 0; | |
5169 | mddev->chunk_sectors = 0; | |
5170 | mddev->ctime = mddev->utime = 0; | |
5171 | mddev->layout = 0; | |
5172 | mddev->max_disks = 0; | |
5173 | mddev->events = 0; | |
a8707c08 | 5174 | mddev->can_decrease_events = 0; |
6177b472 | 5175 | mddev->delta_disks = 0; |
2c810cdd | 5176 | mddev->reshape_backwards = 0; |
6177b472 N |
5177 | mddev->new_level = LEVEL_NONE; |
5178 | mddev->new_layout = 0; | |
5179 | mddev->new_chunk_sectors = 0; | |
5180 | mddev->curr_resync = 0; | |
7f7583d4 | 5181 | atomic64_set(&mddev->resync_mismatches, 0); |
6177b472 N |
5182 | mddev->suspend_lo = mddev->suspend_hi = 0; |
5183 | mddev->sync_speed_min = mddev->sync_speed_max = 0; | |
5184 | mddev->recovery = 0; | |
5185 | mddev->in_sync = 0; | |
f0b4f7e2 | 5186 | mddev->changed = 0; |
6177b472 | 5187 | mddev->degraded = 0; |
6177b472 | 5188 | mddev->safemode = 0; |
050b6615 | 5189 | mddev->merge_check_needed = 0; |
6177b472 N |
5190 | mddev->bitmap_info.offset = 0; |
5191 | mddev->bitmap_info.default_offset = 0; | |
6409bb05 | 5192 | mddev->bitmap_info.default_space = 0; |
6177b472 N |
5193 | mddev->bitmap_info.chunksize = 0; |
5194 | mddev->bitmap_info.daemon_sleep = 0; | |
5195 | mddev->bitmap_info.max_write_behind = 0; | |
5196 | } | |
5197 | ||
fd01b88c | 5198 | static void __md_stop_writes(struct mddev *mddev) |
a047e125 | 5199 | { |
6b6204ee | 5200 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
a047e125 | 5201 | if (mddev->sync_thread) { |
a047e125 | 5202 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
a91d5ac0 | 5203 | md_reap_sync_thread(mddev); |
a047e125 N |
5204 | } |
5205 | ||
5206 | del_timer_sync(&mddev->safemode_timer); | |
5207 | ||
5208 | bitmap_flush(mddev); | |
5209 | md_super_wait(mddev); | |
5210 | ||
b6d428c6 | 5211 | if (mddev->ro == 0 && |
7a0a5355 | 5212 | (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
a047e125 N |
5213 | /* mark array as shutdown cleanly */ |
5214 | mddev->in_sync = 1; | |
5215 | md_update_sb(mddev, 1); | |
5216 | } | |
5217 | } | |
defad61a | 5218 | |
fd01b88c | 5219 | void md_stop_writes(struct mddev *mddev) |
defad61a | 5220 | { |
29f097c4 | 5221 | mddev_lock_nointr(mddev); |
defad61a N |
5222 | __md_stop_writes(mddev); |
5223 | mddev_unlock(mddev); | |
5224 | } | |
390ee602 | 5225 | EXPORT_SYMBOL_GPL(md_stop_writes); |
a047e125 | 5226 | |
5eff3c43 | 5227 | static void __md_stop(struct mddev *mddev) |
6177b472 | 5228 | { |
0ca69886 | 5229 | mddev->ready = 0; |
6177b472 N |
5230 | mddev->pers->stop(mddev); |
5231 | if (mddev->pers->sync_request && mddev->to_remove == NULL) | |
5232 | mddev->to_remove = &md_redundancy_group; | |
5233 | module_put(mddev->pers->owner); | |
5234 | mddev->pers = NULL; | |
cca9cf90 | 5235 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
6177b472 | 5236 | } |
5eff3c43 N |
5237 | |
5238 | void md_stop(struct mddev *mddev) | |
5239 | { | |
5240 | /* stop the array and free an attached data structures. | |
5241 | * This is called from dm-raid | |
5242 | */ | |
5243 | __md_stop(mddev); | |
5244 | bitmap_destroy(mddev); | |
5245 | if (mddev->bio_set) | |
5246 | bioset_free(mddev->bio_set); | |
5247 | } | |
5248 | ||
390ee602 | 5249 | EXPORT_SYMBOL_GPL(md_stop); |
6177b472 | 5250 | |
a05b7ea0 | 5251 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
a4bd82d0 N |
5252 | { |
5253 | int err = 0; | |
30b8feb7 N |
5254 | int did_freeze = 0; |
5255 | ||
5256 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { | |
5257 | did_freeze = 1; | |
5258 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
5259 | md_wakeup_thread(mddev->thread); | |
5260 | } | |
5261 | if (mddev->sync_thread) { | |
5262 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
5263 | /* Thread might be blocked waiting for metadata update | |
5264 | * which will now never happen */ | |
5265 | wake_up_process(mddev->sync_thread->tsk); | |
5266 | } | |
5267 | mddev_unlock(mddev); | |
5268 | wait_event(resync_wait, mddev->sync_thread == NULL); | |
5269 | mddev_lock_nointr(mddev); | |
5270 | ||
a4bd82d0 | 5271 | mutex_lock(&mddev->open_mutex); |
9ba3b7f5 | 5272 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
30b8feb7 N |
5273 | mddev->sync_thread || |
5274 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { | |
a4bd82d0 | 5275 | printk("md: %s still in use.\n",mdname(mddev)); |
30b8feb7 N |
5276 | if (did_freeze) { |
5277 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
5278 | md_wakeup_thread(mddev->thread); | |
5279 | } | |
a4bd82d0 N |
5280 | err = -EBUSY; |
5281 | goto out; | |
5282 | } | |
5283 | if (mddev->pers) { | |
defad61a | 5284 | __md_stop_writes(mddev); |
a4bd82d0 N |
5285 | |
5286 | err = -ENXIO; | |
5287 | if (mddev->ro==1) | |
5288 | goto out; | |
5289 | mddev->ro = 1; | |
5290 | set_disk_ro(mddev->gendisk, 1); | |
5291 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
00bcb4ac | 5292 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
30b8feb7 | 5293 | err = 0; |
a4bd82d0 N |
5294 | } |
5295 | out: | |
5296 | mutex_unlock(&mddev->open_mutex); | |
5297 | return err; | |
5298 | } | |
5299 | ||
9e653b63 N |
5300 | /* mode: |
5301 | * 0 - completely stop and dis-assemble array | |
9e653b63 N |
5302 | * 2 - stop but do not disassemble array |
5303 | */ | |
f72ffdd6 | 5304 | static int do_md_stop(struct mddev *mddev, int mode, |
a05b7ea0 | 5305 | struct block_device *bdev) |
1da177e4 | 5306 | { |
1da177e4 | 5307 | struct gendisk *disk = mddev->gendisk; |
3cb03002 | 5308 | struct md_rdev *rdev; |
30b8feb7 N |
5309 | int did_freeze = 0; |
5310 | ||
5311 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { | |
5312 | did_freeze = 1; | |
5313 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
5314 | md_wakeup_thread(mddev->thread); | |
5315 | } | |
5316 | if (mddev->sync_thread) { | |
5317 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
5318 | /* Thread might be blocked waiting for metadata update | |
5319 | * which will now never happen */ | |
5320 | wake_up_process(mddev->sync_thread->tsk); | |
5321 | } | |
5322 | mddev_unlock(mddev); | |
5323 | wait_event(resync_wait, mddev->sync_thread == NULL); | |
5324 | mddev_lock_nointr(mddev); | |
1da177e4 | 5325 | |
c8c00a69 | 5326 | mutex_lock(&mddev->open_mutex); |
9ba3b7f5 | 5327 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
30b8feb7 N |
5328 | mddev->sysfs_active || |
5329 | mddev->sync_thread || | |
5330 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { | |
df5b20cf | 5331 | printk("md: %s still in use.\n",mdname(mddev)); |
6e17b027 | 5332 | mutex_unlock(&mddev->open_mutex); |
30b8feb7 N |
5333 | if (did_freeze) { |
5334 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); | |
5335 | md_wakeup_thread(mddev->thread); | |
5336 | } | |
260fa034 N |
5337 | return -EBUSY; |
5338 | } | |
6e17b027 | 5339 | if (mddev->pers) { |
a4bd82d0 N |
5340 | if (mddev->ro) |
5341 | set_disk_ro(disk, 0); | |
409c57f3 | 5342 | |
defad61a | 5343 | __md_stop_writes(mddev); |
5eff3c43 | 5344 | __md_stop(mddev); |
a4bd82d0 | 5345 | mddev->queue->merge_bvec_fn = NULL; |
a4bd82d0 | 5346 | mddev->queue->backing_dev_info.congested_fn = NULL; |
6177b472 | 5347 | |
a4bd82d0 | 5348 | /* tell userspace to handle 'inactive' */ |
00bcb4ac | 5349 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0d4ca600 | 5350 | |
dafb20fa | 5351 | rdev_for_each(rdev, mddev) |
36fad858 NK |
5352 | if (rdev->raid_disk >= 0) |
5353 | sysfs_unlink_rdev(mddev, rdev); | |
c4647292 | 5354 | |
a4bd82d0 | 5355 | set_capacity(disk, 0); |
6e17b027 | 5356 | mutex_unlock(&mddev->open_mutex); |
f0b4f7e2 | 5357 | mddev->changed = 1; |
a4bd82d0 | 5358 | revalidate_disk(disk); |
0d4ca600 | 5359 | |
a4bd82d0 N |
5360 | if (mddev->ro) |
5361 | mddev->ro = 0; | |
6e17b027 N |
5362 | } else |
5363 | mutex_unlock(&mddev->open_mutex); | |
1da177e4 LT |
5364 | /* |
5365 | * Free resources if final stop | |
5366 | */ | |
9e653b63 | 5367 | if (mode == 0) { |
1da177e4 LT |
5368 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
5369 | ||
978f946b | 5370 | bitmap_destroy(mddev); |
c3d9714e | 5371 | if (mddev->bitmap_info.file) { |
c3d9714e N |
5372 | fput(mddev->bitmap_info.file); |
5373 | mddev->bitmap_info.file = NULL; | |
978f946b | 5374 | } |
c3d9714e | 5375 | mddev->bitmap_info.offset = 0; |
978f946b | 5376 | |
1da177e4 LT |
5377 | export_array(mddev); |
5378 | ||
6177b472 | 5379 | md_clean(mddev); |
934d9c23 | 5380 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
efeb53c0 N |
5381 | if (mddev->hold_active == UNTIL_STOP) |
5382 | mddev->hold_active = 0; | |
a4bd82d0 | 5383 | } |
3f9d99c1 | 5384 | blk_integrity_unregister(disk); |
d7603b7e | 5385 | md_new_event(mddev); |
00bcb4ac | 5386 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
6e17b027 | 5387 | return 0; |
1da177e4 LT |
5388 | } |
5389 | ||
fdee8ae4 | 5390 | #ifndef MODULE |
fd01b88c | 5391 | static void autorun_array(struct mddev *mddev) |
1da177e4 | 5392 | { |
3cb03002 | 5393 | struct md_rdev *rdev; |
1da177e4 LT |
5394 | int err; |
5395 | ||
a757e64c | 5396 | if (list_empty(&mddev->disks)) |
1da177e4 | 5397 | return; |
1da177e4 LT |
5398 | |
5399 | printk(KERN_INFO "md: running: "); | |
5400 | ||
dafb20fa | 5401 | rdev_for_each(rdev, mddev) { |
1da177e4 LT |
5402 | char b[BDEVNAME_SIZE]; |
5403 | printk("<%s>", bdevname(rdev->bdev,b)); | |
5404 | } | |
5405 | printk("\n"); | |
5406 | ||
d710e138 | 5407 | err = do_md_run(mddev); |
1da177e4 LT |
5408 | if (err) { |
5409 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); | |
a05b7ea0 | 5410 | do_md_stop(mddev, 0, NULL); |
1da177e4 LT |
5411 | } |
5412 | } | |
5413 | ||
5414 | /* | |
5415 | * lets try to run arrays based on all disks that have arrived | |
5416 | * until now. (those are in pending_raid_disks) | |
5417 | * | |
5418 | * the method: pick the first pending disk, collect all disks with | |
5419 | * the same UUID, remove all from the pending list and put them into | |
5420 | * the 'same_array' list. Then order this list based on superblock | |
5421 | * update time (freshest comes first), kick out 'old' disks and | |
5422 | * compare superblocks. If everything's fine then run it. | |
5423 | * | |
5424 | * If "unit" is allocated, then bump its reference count | |
5425 | */ | |
5426 | static void autorun_devices(int part) | |
5427 | { | |
3cb03002 | 5428 | struct md_rdev *rdev0, *rdev, *tmp; |
fd01b88c | 5429 | struct mddev *mddev; |
1da177e4 LT |
5430 | char b[BDEVNAME_SIZE]; |
5431 | ||
5432 | printk(KERN_INFO "md: autorun ...\n"); | |
5433 | while (!list_empty(&pending_raid_disks)) { | |
e8703fe1 | 5434 | int unit; |
1da177e4 | 5435 | dev_t dev; |
ad01c9e3 | 5436 | LIST_HEAD(candidates); |
1da177e4 | 5437 | rdev0 = list_entry(pending_raid_disks.next, |
3cb03002 | 5438 | struct md_rdev, same_set); |
1da177e4 LT |
5439 | |
5440 | printk(KERN_INFO "md: considering %s ...\n", | |
5441 | bdevname(rdev0->bdev,b)); | |
5442 | INIT_LIST_HEAD(&candidates); | |
159ec1fc | 5443 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
1da177e4 LT |
5444 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
5445 | printk(KERN_INFO "md: adding %s ...\n", | |
5446 | bdevname(rdev->bdev,b)); | |
5447 | list_move(&rdev->same_set, &candidates); | |
5448 | } | |
5449 | /* | |
5450 | * now we have a set of devices, with all of them having | |
5451 | * mostly sane superblocks. It's time to allocate the | |
5452 | * mddev. | |
5453 | */ | |
e8703fe1 N |
5454 | if (part) { |
5455 | dev = MKDEV(mdp_major, | |
5456 | rdev0->preferred_minor << MdpMinorShift); | |
5457 | unit = MINOR(dev) >> MdpMinorShift; | |
5458 | } else { | |
5459 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); | |
5460 | unit = MINOR(dev); | |
5461 | } | |
5462 | if (rdev0->preferred_minor != unit) { | |
1da177e4 LT |
5463 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
5464 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); | |
5465 | break; | |
5466 | } | |
1da177e4 LT |
5467 | |
5468 | md_probe(dev, NULL, NULL); | |
5469 | mddev = mddev_find(dev); | |
9bbbca3a NB |
5470 | if (!mddev || !mddev->gendisk) { |
5471 | if (mddev) | |
5472 | mddev_put(mddev); | |
5473 | printk(KERN_ERR | |
1da177e4 LT |
5474 | "md: cannot allocate memory for md drive.\n"); |
5475 | break; | |
5476 | } | |
f72ffdd6 | 5477 | if (mddev_lock(mddev)) |
1da177e4 LT |
5478 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
5479 | mdname(mddev)); | |
5480 | else if (mddev->raid_disks || mddev->major_version | |
5481 | || !list_empty(&mddev->disks)) { | |
f72ffdd6 | 5482 | printk(KERN_WARNING |
1da177e4 LT |
5483 | "md: %s already running, cannot run %s\n", |
5484 | mdname(mddev), bdevname(rdev0->bdev,b)); | |
5485 | mddev_unlock(mddev); | |
5486 | } else { | |
5487 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); | |
1ec4a939 | 5488 | mddev->persistent = 1; |
159ec1fc | 5489 | rdev_for_each_list(rdev, tmp, &candidates) { |
1da177e4 LT |
5490 | list_del_init(&rdev->same_set); |
5491 | if (bind_rdev_to_array(rdev, mddev)) | |
5492 | export_rdev(rdev); | |
5493 | } | |
5494 | autorun_array(mddev); | |
5495 | mddev_unlock(mddev); | |
5496 | } | |
5497 | /* on success, candidates will be empty, on error | |
5498 | * it won't... | |
5499 | */ | |
159ec1fc | 5500 | rdev_for_each_list(rdev, tmp, &candidates) { |
4b80991c | 5501 | list_del_init(&rdev->same_set); |
1da177e4 | 5502 | export_rdev(rdev); |
4b80991c | 5503 | } |
1da177e4 LT |
5504 | mddev_put(mddev); |
5505 | } | |
5506 | printk(KERN_INFO "md: ... autorun DONE.\n"); | |
5507 | } | |
fdee8ae4 | 5508 | #endif /* !MODULE */ |
1da177e4 | 5509 | |
f72ffdd6 | 5510 | static int get_version(void __user *arg) |
1da177e4 LT |
5511 | { |
5512 | mdu_version_t ver; | |
5513 | ||
5514 | ver.major = MD_MAJOR_VERSION; | |
5515 | ver.minor = MD_MINOR_VERSION; | |
5516 | ver.patchlevel = MD_PATCHLEVEL_VERSION; | |
5517 | ||
5518 | if (copy_to_user(arg, &ver, sizeof(ver))) | |
5519 | return -EFAULT; | |
5520 | ||
5521 | return 0; | |
5522 | } | |
5523 | ||
f72ffdd6 | 5524 | static int get_array_info(struct mddev *mddev, void __user *arg) |
1da177e4 LT |
5525 | { |
5526 | mdu_array_info_t info; | |
a9f326eb | 5527 | int nr,working,insync,failed,spare; |
3cb03002 | 5528 | struct md_rdev *rdev; |
1da177e4 | 5529 | |
1ca69c4b N |
5530 | nr = working = insync = failed = spare = 0; |
5531 | rcu_read_lock(); | |
5532 | rdev_for_each_rcu(rdev, mddev) { | |
1da177e4 | 5533 | nr++; |
b2d444d7 | 5534 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 LT |
5535 | failed++; |
5536 | else { | |
5537 | working++; | |
b2d444d7 | 5538 | if (test_bit(In_sync, &rdev->flags)) |
f72ffdd6 | 5539 | insync++; |
1da177e4 LT |
5540 | else |
5541 | spare++; | |
5542 | } | |
5543 | } | |
1ca69c4b | 5544 | rcu_read_unlock(); |
1da177e4 LT |
5545 | |
5546 | info.major_version = mddev->major_version; | |
5547 | info.minor_version = mddev->minor_version; | |
5548 | info.patch_version = MD_PATCHLEVEL_VERSION; | |
5549 | info.ctime = mddev->ctime; | |
5550 | info.level = mddev->level; | |
58c0fed4 AN |
5551 | info.size = mddev->dev_sectors / 2; |
5552 | if (info.size != mddev->dev_sectors / 2) /* overflow */ | |
284ae7ca | 5553 | info.size = -1; |
1da177e4 LT |
5554 | info.nr_disks = nr; |
5555 | info.raid_disks = mddev->raid_disks; | |
5556 | info.md_minor = mddev->md_minor; | |
5557 | info.not_persistent= !mddev->persistent; | |
5558 | ||
5559 | info.utime = mddev->utime; | |
5560 | info.state = 0; | |
5561 | if (mddev->in_sync) | |
5562 | info.state = (1<<MD_SB_CLEAN); | |
c3d9714e | 5563 | if (mddev->bitmap && mddev->bitmap_info.offset) |
9bd35920 | 5564 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
a9f326eb | 5565 | info.active_disks = insync; |
1da177e4 LT |
5566 | info.working_disks = working; |
5567 | info.failed_disks = failed; | |
5568 | info.spare_disks = spare; | |
5569 | ||
5570 | info.layout = mddev->layout; | |
9d8f0363 | 5571 | info.chunk_size = mddev->chunk_sectors << 9; |
1da177e4 LT |
5572 | |
5573 | if (copy_to_user(arg, &info, sizeof(info))) | |
5574 | return -EFAULT; | |
5575 | ||
5576 | return 0; | |
5577 | } | |
5578 | ||
f72ffdd6 | 5579 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
32a7627c N |
5580 | { |
5581 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ | |
5582 | char *ptr, *buf = NULL; | |
5583 | int err = -ENOMEM; | |
5584 | ||
60559da4 | 5585 | file = kmalloc(sizeof(*file), GFP_NOIO); |
2a2275d6 | 5586 | |
32a7627c N |
5587 | if (!file) |
5588 | goto out; | |
5589 | ||
5590 | /* bitmap disabled, zero the first byte and copy out */ | |
1ec885cd | 5591 | if (!mddev->bitmap || !mddev->bitmap->storage.file) { |
32a7627c N |
5592 | file->pathname[0] = '\0'; |
5593 | goto copy_out; | |
5594 | } | |
5595 | ||
5596 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); | |
5597 | if (!buf) | |
5598 | goto out; | |
5599 | ||
1ec885cd N |
5600 | ptr = d_path(&mddev->bitmap->storage.file->f_path, |
5601 | buf, sizeof(file->pathname)); | |
6bcfd601 | 5602 | if (IS_ERR(ptr)) |
32a7627c N |
5603 | goto out; |
5604 | ||
5605 | strcpy(file->pathname, ptr); | |
5606 | ||
5607 | copy_out: | |
5608 | err = 0; | |
5609 | if (copy_to_user(arg, file, sizeof(*file))) | |
5610 | err = -EFAULT; | |
5611 | out: | |
5612 | kfree(buf); | |
5613 | kfree(file); | |
5614 | return err; | |
5615 | } | |
5616 | ||
f72ffdd6 | 5617 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
1da177e4 LT |
5618 | { |
5619 | mdu_disk_info_t info; | |
3cb03002 | 5620 | struct md_rdev *rdev; |
1da177e4 LT |
5621 | |
5622 | if (copy_from_user(&info, arg, sizeof(info))) | |
5623 | return -EFAULT; | |
5624 | ||
1ca69c4b N |
5625 | rcu_read_lock(); |
5626 | rdev = find_rdev_nr_rcu(mddev, info.number); | |
1da177e4 LT |
5627 | if (rdev) { |
5628 | info.major = MAJOR(rdev->bdev->bd_dev); | |
5629 | info.minor = MINOR(rdev->bdev->bd_dev); | |
5630 | info.raid_disk = rdev->raid_disk; | |
5631 | info.state = 0; | |
b2d444d7 | 5632 | if (test_bit(Faulty, &rdev->flags)) |
1da177e4 | 5633 | info.state |= (1<<MD_DISK_FAULTY); |
b2d444d7 | 5634 | else if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 LT |
5635 | info.state |= (1<<MD_DISK_ACTIVE); |
5636 | info.state |= (1<<MD_DISK_SYNC); | |
5637 | } | |
8ddf9efe N |
5638 | if (test_bit(WriteMostly, &rdev->flags)) |
5639 | info.state |= (1<<MD_DISK_WRITEMOSTLY); | |
1da177e4 LT |
5640 | } else { |
5641 | info.major = info.minor = 0; | |
5642 | info.raid_disk = -1; | |
5643 | info.state = (1<<MD_DISK_REMOVED); | |
5644 | } | |
1ca69c4b | 5645 | rcu_read_unlock(); |
1da177e4 LT |
5646 | |
5647 | if (copy_to_user(arg, &info, sizeof(info))) | |
5648 | return -EFAULT; | |
5649 | ||
5650 | return 0; | |
5651 | } | |
5652 | ||
f72ffdd6 | 5653 | static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info) |
1da177e4 LT |
5654 | { |
5655 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; | |
3cb03002 | 5656 | struct md_rdev *rdev; |
1da177e4 LT |
5657 | dev_t dev = MKDEV(info->major,info->minor); |
5658 | ||
5659 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) | |
5660 | return -EOVERFLOW; | |
5661 | ||
5662 | if (!mddev->raid_disks) { | |
5663 | int err; | |
5664 | /* expecting a device which has a superblock */ | |
5665 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); | |
5666 | if (IS_ERR(rdev)) { | |
f72ffdd6 | 5667 | printk(KERN_WARNING |
1da177e4 LT |
5668 | "md: md_import_device returned %ld\n", |
5669 | PTR_ERR(rdev)); | |
5670 | return PTR_ERR(rdev); | |
5671 | } | |
5672 | if (!list_empty(&mddev->disks)) { | |
3cb03002 N |
5673 | struct md_rdev *rdev0 |
5674 | = list_entry(mddev->disks.next, | |
5675 | struct md_rdev, same_set); | |
a9f326eb | 5676 | err = super_types[mddev->major_version] |
1da177e4 LT |
5677 | .load_super(rdev, rdev0, mddev->minor_version); |
5678 | if (err < 0) { | |
f72ffdd6 | 5679 | printk(KERN_WARNING |
1da177e4 | 5680 | "md: %s has different UUID to %s\n", |
f72ffdd6 | 5681 | bdevname(rdev->bdev,b), |
1da177e4 LT |
5682 | bdevname(rdev0->bdev,b2)); |
5683 | export_rdev(rdev); | |
5684 | return -EINVAL; | |
5685 | } | |
5686 | } | |
5687 | err = bind_rdev_to_array(rdev, mddev); | |
5688 | if (err) | |
5689 | export_rdev(rdev); | |
5690 | return err; | |
5691 | } | |
5692 | ||
5693 | /* | |
5694 | * add_new_disk can be used once the array is assembled | |
5695 | * to add "hot spares". They must already have a superblock | |
5696 | * written | |
5697 | */ | |
5698 | if (mddev->pers) { | |
5699 | int err; | |
5700 | if (!mddev->pers->hot_add_disk) { | |
f72ffdd6 | 5701 | printk(KERN_WARNING |
1da177e4 LT |
5702 | "%s: personality does not support diskops!\n", |
5703 | mdname(mddev)); | |
5704 | return -EINVAL; | |
5705 | } | |
7b1e35f6 N |
5706 | if (mddev->persistent) |
5707 | rdev = md_import_device(dev, mddev->major_version, | |
5708 | mddev->minor_version); | |
5709 | else | |
5710 | rdev = md_import_device(dev, -1, -1); | |
1da177e4 | 5711 | if (IS_ERR(rdev)) { |
f72ffdd6 | 5712 | printk(KERN_WARNING |
1da177e4 LT |
5713 | "md: md_import_device returned %ld\n", |
5714 | PTR_ERR(rdev)); | |
5715 | return PTR_ERR(rdev); | |
5716 | } | |
1a855a06 | 5717 | /* set saved_raid_disk if appropriate */ |
41158c7e N |
5718 | if (!mddev->persistent) { |
5719 | if (info->state & (1<<MD_DISK_SYNC) && | |
bf572541 | 5720 | info->raid_disk < mddev->raid_disks) { |
41158c7e | 5721 | rdev->raid_disk = info->raid_disk; |
bf572541 | 5722 | set_bit(In_sync, &rdev->flags); |
8313b8e5 | 5723 | clear_bit(Bitmap_sync, &rdev->flags); |
bf572541 | 5724 | } else |
41158c7e | 5725 | rdev->raid_disk = -1; |
f466722c | 5726 | rdev->saved_raid_disk = rdev->raid_disk; |
41158c7e N |
5727 | } else |
5728 | super_types[mddev->major_version]. | |
5729 | validate_super(mddev, rdev); | |
bedd86b7 | 5730 | if ((info->state & (1<<MD_DISK_SYNC)) && |
f4563091 | 5731 | rdev->raid_disk != info->raid_disk) { |
bedd86b7 N |
5732 | /* This was a hot-add request, but events doesn't |
5733 | * match, so reject it. | |
5734 | */ | |
5735 | export_rdev(rdev); | |
5736 | return -EINVAL; | |
5737 | } | |
5738 | ||
b2d444d7 | 5739 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
8ddf9efe N |
5740 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
5741 | set_bit(WriteMostly, &rdev->flags); | |
575a80fa N |
5742 | else |
5743 | clear_bit(WriteMostly, &rdev->flags); | |
8ddf9efe | 5744 | |
1da177e4 LT |
5745 | rdev->raid_disk = -1; |
5746 | err = bind_rdev_to_array(rdev, mddev); | |
7c7546cc N |
5747 | if (!err && !mddev->pers->hot_remove_disk) { |
5748 | /* If there is hot_add_disk but no hot_remove_disk | |
5749 | * then added disks for geometry changes, | |
5750 | * and should be added immediately. | |
5751 | */ | |
5752 | super_types[mddev->major_version]. | |
5753 | validate_super(mddev, rdev); | |
5754 | err = mddev->pers->hot_add_disk(mddev, rdev); | |
5755 | if (err) | |
5756 | unbind_rdev_from_array(rdev); | |
5757 | } | |
1da177e4 LT |
5758 | if (err) |
5759 | export_rdev(rdev); | |
52664732 | 5760 | else |
00bcb4ac | 5761 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
c361777f | 5762 | |
7ceb17e8 | 5763 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
72a23c21 NB |
5764 | if (mddev->degraded) |
5765 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
c361777f | 5766 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
9864c005 | 5767 | if (!err) |
5768 | md_new_event(mddev); | |
005eca5e | 5769 | md_wakeup_thread(mddev->thread); |
1da177e4 LT |
5770 | return err; |
5771 | } | |
5772 | ||
5773 | /* otherwise, add_new_disk is only allowed | |
5774 | * for major_version==0 superblocks | |
5775 | */ | |
5776 | if (mddev->major_version != 0) { | |
5777 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", | |
5778 | mdname(mddev)); | |
5779 | return -EINVAL; | |
5780 | } | |
5781 | ||
5782 | if (!(info->state & (1<<MD_DISK_FAULTY))) { | |
5783 | int err; | |
d710e138 | 5784 | rdev = md_import_device(dev, -1, 0); |
1da177e4 | 5785 | if (IS_ERR(rdev)) { |
f72ffdd6 | 5786 | printk(KERN_WARNING |
1da177e4 LT |
5787 | "md: error, md_import_device() returned %ld\n", |
5788 | PTR_ERR(rdev)); | |
5789 | return PTR_ERR(rdev); | |
5790 | } | |
5791 | rdev->desc_nr = info->number; | |
5792 | if (info->raid_disk < mddev->raid_disks) | |
5793 | rdev->raid_disk = info->raid_disk; | |
5794 | else | |
5795 | rdev->raid_disk = -1; | |
5796 | ||
1da177e4 | 5797 | if (rdev->raid_disk < mddev->raid_disks) |
b2d444d7 N |
5798 | if (info->state & (1<<MD_DISK_SYNC)) |
5799 | set_bit(In_sync, &rdev->flags); | |
1da177e4 | 5800 | |
8ddf9efe N |
5801 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
5802 | set_bit(WriteMostly, &rdev->flags); | |
5803 | ||
1da177e4 LT |
5804 | if (!mddev->persistent) { |
5805 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); | |
77304d2a MS |
5806 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
5807 | } else | |
57b2caa3 | 5808 | rdev->sb_start = calc_dev_sboffset(rdev); |
8190e754 | 5809 | rdev->sectors = rdev->sb_start; |
1da177e4 | 5810 | |
2bf071bf N |
5811 | err = bind_rdev_to_array(rdev, mddev); |
5812 | if (err) { | |
5813 | export_rdev(rdev); | |
5814 | return err; | |
5815 | } | |
1da177e4 LT |
5816 | } |
5817 | ||
5818 | return 0; | |
5819 | } | |
5820 | ||
f72ffdd6 | 5821 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
1da177e4 LT |
5822 | { |
5823 | char b[BDEVNAME_SIZE]; | |
3cb03002 | 5824 | struct md_rdev *rdev; |
1da177e4 | 5825 | |
1da177e4 LT |
5826 | rdev = find_rdev(mddev, dev); |
5827 | if (!rdev) | |
5828 | return -ENXIO; | |
5829 | ||
3ea8929d N |
5830 | clear_bit(Blocked, &rdev->flags); |
5831 | remove_and_add_spares(mddev, rdev); | |
5832 | ||
1da177e4 LT |
5833 | if (rdev->raid_disk >= 0) |
5834 | goto busy; | |
5835 | ||
5836 | kick_rdev_from_array(rdev); | |
850b2b42 | 5837 | md_update_sb(mddev, 1); |
d7603b7e | 5838 | md_new_event(mddev); |
1da177e4 LT |
5839 | |
5840 | return 0; | |
5841 | busy: | |
fdefa4d8 | 5842 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
1da177e4 LT |
5843 | bdevname(rdev->bdev,b), mdname(mddev)); |
5844 | return -EBUSY; | |
5845 | } | |
5846 | ||
f72ffdd6 | 5847 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
1da177e4 LT |
5848 | { |
5849 | char b[BDEVNAME_SIZE]; | |
5850 | int err; | |
3cb03002 | 5851 | struct md_rdev *rdev; |
1da177e4 LT |
5852 | |
5853 | if (!mddev->pers) | |
5854 | return -ENODEV; | |
5855 | ||
5856 | if (mddev->major_version != 0) { | |
5857 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" | |
5858 | " version-0 superblocks.\n", | |
5859 | mdname(mddev)); | |
5860 | return -EINVAL; | |
5861 | } | |
5862 | if (!mddev->pers->hot_add_disk) { | |
f72ffdd6 | 5863 | printk(KERN_WARNING |
1da177e4 LT |
5864 | "%s: personality does not support diskops!\n", |
5865 | mdname(mddev)); | |
5866 | return -EINVAL; | |
5867 | } | |
5868 | ||
d710e138 | 5869 | rdev = md_import_device(dev, -1, 0); |
1da177e4 | 5870 | if (IS_ERR(rdev)) { |
f72ffdd6 | 5871 | printk(KERN_WARNING |
1da177e4 LT |
5872 | "md: error, md_import_device() returned %ld\n", |
5873 | PTR_ERR(rdev)); | |
5874 | return -EINVAL; | |
5875 | } | |
5876 | ||
5877 | if (mddev->persistent) | |
57b2caa3 | 5878 | rdev->sb_start = calc_dev_sboffset(rdev); |
1da177e4 | 5879 | else |
77304d2a | 5880 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
1da177e4 | 5881 | |
8190e754 | 5882 | rdev->sectors = rdev->sb_start; |
1da177e4 | 5883 | |
b2d444d7 | 5884 | if (test_bit(Faulty, &rdev->flags)) { |
f72ffdd6 | 5885 | printk(KERN_WARNING |
1da177e4 LT |
5886 | "md: can not hot-add faulty %s disk to %s!\n", |
5887 | bdevname(rdev->bdev,b), mdname(mddev)); | |
5888 | err = -EINVAL; | |
5889 | goto abort_export; | |
5890 | } | |
b2d444d7 | 5891 | clear_bit(In_sync, &rdev->flags); |
1da177e4 | 5892 | rdev->desc_nr = -1; |
5842730d | 5893 | rdev->saved_raid_disk = -1; |
2bf071bf N |
5894 | err = bind_rdev_to_array(rdev, mddev); |
5895 | if (err) | |
5896 | goto abort_export; | |
1da177e4 LT |
5897 | |
5898 | /* | |
5899 | * The rest should better be atomic, we can have disk failures | |
5900 | * noticed in interrupt contexts ... | |
5901 | */ | |
5902 | ||
1da177e4 LT |
5903 | rdev->raid_disk = -1; |
5904 | ||
850b2b42 | 5905 | md_update_sb(mddev, 1); |
1da177e4 LT |
5906 | |
5907 | /* | |
5908 | * Kick recovery, maybe this spare has to be added to the | |
5909 | * array immediately. | |
5910 | */ | |
5911 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
5912 | md_wakeup_thread(mddev->thread); | |
d7603b7e | 5913 | md_new_event(mddev); |
1da177e4 LT |
5914 | return 0; |
5915 | ||
1da177e4 LT |
5916 | abort_export: |
5917 | export_rdev(rdev); | |
5918 | return err; | |
5919 | } | |
5920 | ||
fd01b88c | 5921 | static int set_bitmap_file(struct mddev *mddev, int fd) |
32a7627c | 5922 | { |
035328c2 | 5923 | int err = 0; |
32a7627c | 5924 | |
36fa3063 | 5925 | if (mddev->pers) { |
d66b1b39 | 5926 | if (!mddev->pers->quiesce || !mddev->thread) |
36fa3063 N |
5927 | return -EBUSY; |
5928 | if (mddev->recovery || mddev->sync_thread) | |
5929 | return -EBUSY; | |
5930 | /* we should be able to change the bitmap.. */ | |
5931 | } | |
32a7627c | 5932 | |
36fa3063 | 5933 | if (fd >= 0) { |
035328c2 | 5934 | struct inode *inode; |
36fa3063 N |
5935 | if (mddev->bitmap) |
5936 | return -EEXIST; /* cannot add when bitmap is present */ | |
c3d9714e | 5937 | mddev->bitmap_info.file = fget(fd); |
32a7627c | 5938 | |
c3d9714e | 5939 | if (mddev->bitmap_info.file == NULL) { |
36fa3063 N |
5940 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
5941 | mdname(mddev)); | |
5942 | return -EBADF; | |
5943 | } | |
5944 | ||
035328c2 N |
5945 | inode = mddev->bitmap_info.file->f_mapping->host; |
5946 | if (!S_ISREG(inode->i_mode)) { | |
5947 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", | |
5948 | mdname(mddev)); | |
5949 | err = -EBADF; | |
5950 | } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) { | |
5951 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", | |
5952 | mdname(mddev)); | |
5953 | err = -EBADF; | |
5954 | } else if (atomic_read(&inode->i_writecount) != 1) { | |
36fa3063 N |
5955 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
5956 | mdname(mddev)); | |
035328c2 N |
5957 | err = -EBUSY; |
5958 | } | |
5959 | if (err) { | |
c3d9714e N |
5960 | fput(mddev->bitmap_info.file); |
5961 | mddev->bitmap_info.file = NULL; | |
36fa3063 N |
5962 | return err; |
5963 | } | |
c3d9714e | 5964 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
36fa3063 N |
5965 | } else if (mddev->bitmap == NULL) |
5966 | return -ENOENT; /* cannot remove what isn't there */ | |
5967 | err = 0; | |
5968 | if (mddev->pers) { | |
5969 | mddev->pers->quiesce(mddev, 1); | |
69e51b44 | 5970 | if (fd >= 0) { |
36fa3063 | 5971 | err = bitmap_create(mddev); |
69e51b44 N |
5972 | if (!err) |
5973 | err = bitmap_load(mddev); | |
5974 | } | |
d7375ab3 | 5975 | if (fd < 0 || err) { |
36fa3063 | 5976 | bitmap_destroy(mddev); |
d7375ab3 N |
5977 | fd = -1; /* make sure to put the file */ |
5978 | } | |
36fa3063 | 5979 | mddev->pers->quiesce(mddev, 0); |
d7375ab3 N |
5980 | } |
5981 | if (fd < 0) { | |
035328c2 | 5982 | if (mddev->bitmap_info.file) |
c3d9714e | 5983 | fput(mddev->bitmap_info.file); |
c3d9714e | 5984 | mddev->bitmap_info.file = NULL; |
36fa3063 N |
5985 | } |
5986 | ||
32a7627c N |
5987 | return err; |
5988 | } | |
5989 | ||
1da177e4 LT |
5990 | /* |
5991 | * set_array_info is used two different ways | |
5992 | * The original usage is when creating a new array. | |
5993 | * In this usage, raid_disks is > 0 and it together with | |
5994 | * level, size, not_persistent,layout,chunksize determine the | |
5995 | * shape of the array. | |
5996 | * This will always create an array with a type-0.90.0 superblock. | |
5997 | * The newer usage is when assembling an array. | |
5998 | * In this case raid_disks will be 0, and the major_version field is | |
5999 | * use to determine which style super-blocks are to be found on the devices. | |
6000 | * The minor and patch _version numbers are also kept incase the | |
6001 | * super_block handler wishes to interpret them. | |
6002 | */ | |
f72ffdd6 | 6003 | static int set_array_info(struct mddev *mddev, mdu_array_info_t *info) |
1da177e4 LT |
6004 | { |
6005 | ||
6006 | if (info->raid_disks == 0) { | |
6007 | /* just setting version number for superblock loading */ | |
6008 | if (info->major_version < 0 || | |
50511da3 | 6009 | info->major_version >= ARRAY_SIZE(super_types) || |
1da177e4 LT |
6010 | super_types[info->major_version].name == NULL) { |
6011 | /* maybe try to auto-load a module? */ | |
f72ffdd6 | 6012 | printk(KERN_INFO |
1da177e4 LT |
6013 | "md: superblock version %d not known\n", |
6014 | info->major_version); | |
6015 | return -EINVAL; | |
6016 | } | |
6017 | mddev->major_version = info->major_version; | |
6018 | mddev->minor_version = info->minor_version; | |
6019 | mddev->patch_version = info->patch_version; | |
3f9d7b0d | 6020 | mddev->persistent = !info->not_persistent; |
cbd19983 N |
6021 | /* ensure mddev_put doesn't delete this now that there |
6022 | * is some minimal configuration. | |
6023 | */ | |
6024 | mddev->ctime = get_seconds(); | |
1da177e4 LT |
6025 | return 0; |
6026 | } | |
6027 | mddev->major_version = MD_MAJOR_VERSION; | |
6028 | mddev->minor_version = MD_MINOR_VERSION; | |
6029 | mddev->patch_version = MD_PATCHLEVEL_VERSION; | |
6030 | mddev->ctime = get_seconds(); | |
6031 | ||
6032 | mddev->level = info->level; | |
17115e03 | 6033 | mddev->clevel[0] = 0; |
58c0fed4 | 6034 | mddev->dev_sectors = 2 * (sector_t)info->size; |
1da177e4 LT |
6035 | mddev->raid_disks = info->raid_disks; |
6036 | /* don't set md_minor, it is determined by which /dev/md* was | |
6037 | * openned | |
6038 | */ | |
6039 | if (info->state & (1<<MD_SB_CLEAN)) | |
6040 | mddev->recovery_cp = MaxSector; | |
6041 | else | |
6042 | mddev->recovery_cp = 0; | |
6043 | mddev->persistent = ! info->not_persistent; | |
e691063a | 6044 | mddev->external = 0; |
1da177e4 LT |
6045 | |
6046 | mddev->layout = info->layout; | |
9d8f0363 | 6047 | mddev->chunk_sectors = info->chunk_size >> 9; |
1da177e4 LT |
6048 | |
6049 | mddev->max_disks = MD_SB_DISKS; | |
6050 | ||
e691063a N |
6051 | if (mddev->persistent) |
6052 | mddev->flags = 0; | |
850b2b42 | 6053 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
1da177e4 | 6054 | |
c3d9714e | 6055 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
6409bb05 | 6056 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
c3d9714e | 6057 | mddev->bitmap_info.offset = 0; |
b2a2703c | 6058 | |
f6705578 N |
6059 | mddev->reshape_position = MaxSector; |
6060 | ||
1da177e4 LT |
6061 | /* |
6062 | * Generate a 128 bit UUID | |
6063 | */ | |
6064 | get_random_bytes(mddev->uuid, 16); | |
6065 | ||
f6705578 | 6066 | mddev->new_level = mddev->level; |
664e7c41 | 6067 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
f6705578 N |
6068 | mddev->new_layout = mddev->layout; |
6069 | mddev->delta_disks = 0; | |
2c810cdd | 6070 | mddev->reshape_backwards = 0; |
f6705578 | 6071 | |
1da177e4 LT |
6072 | return 0; |
6073 | } | |
6074 | ||
fd01b88c | 6075 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
1f403624 | 6076 | { |
b522adcd DW |
6077 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
6078 | ||
6079 | if (mddev->external_size) | |
6080 | return; | |
6081 | ||
1f403624 DW |
6082 | mddev->array_sectors = array_sectors; |
6083 | } | |
6084 | EXPORT_SYMBOL(md_set_array_sectors); | |
6085 | ||
fd01b88c | 6086 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
a35b0d69 | 6087 | { |
3cb03002 | 6088 | struct md_rdev *rdev; |
a35b0d69 | 6089 | int rv; |
d71f9f88 | 6090 | int fit = (num_sectors == 0); |
a35b0d69 N |
6091 | |
6092 | if (mddev->pers->resize == NULL) | |
6093 | return -EINVAL; | |
d71f9f88 AN |
6094 | /* The "num_sectors" is the number of sectors of each device that |
6095 | * is used. This can only make sense for arrays with redundancy. | |
6096 | * linear and raid0 always use whatever space is available. We can only | |
6097 | * consider changing this number if no resync or reconstruction is | |
6098 | * happening, and if the new size is acceptable. It must fit before the | |
0f420358 | 6099 | * sb_start or, if that is <data_offset, it must fit before the size |
d71f9f88 AN |
6100 | * of each device. If num_sectors is zero, we find the largest size |
6101 | * that fits. | |
a35b0d69 N |
6102 | */ |
6103 | if (mddev->sync_thread) | |
6104 | return -EBUSY; | |
bd8839e0 N |
6105 | if (mddev->ro) |
6106 | return -EROFS; | |
a4a6125a | 6107 | |
dafb20fa | 6108 | rdev_for_each(rdev, mddev) { |
dd8ac336 | 6109 | sector_t avail = rdev->sectors; |
01ab5662 | 6110 | |
d71f9f88 AN |
6111 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
6112 | num_sectors = avail; | |
6113 | if (avail < num_sectors) | |
a35b0d69 N |
6114 | return -ENOSPC; |
6115 | } | |
d71f9f88 | 6116 | rv = mddev->pers->resize(mddev, num_sectors); |
449aad3e N |
6117 | if (!rv) |
6118 | revalidate_disk(mddev->gendisk); | |
a35b0d69 N |
6119 | return rv; |
6120 | } | |
6121 | ||
fd01b88c | 6122 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
da943b99 N |
6123 | { |
6124 | int rv; | |
c6563a8c | 6125 | struct md_rdev *rdev; |
da943b99 | 6126 | /* change the number of raid disks */ |
63c70c4f | 6127 | if (mddev->pers->check_reshape == NULL) |
da943b99 | 6128 | return -EINVAL; |
bd8839e0 N |
6129 | if (mddev->ro) |
6130 | return -EROFS; | |
da943b99 | 6131 | if (raid_disks <= 0 || |
233fca36 | 6132 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
da943b99 | 6133 | return -EINVAL; |
63c70c4f | 6134 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
da943b99 | 6135 | return -EBUSY; |
c6563a8c N |
6136 | |
6137 | rdev_for_each(rdev, mddev) { | |
6138 | if (mddev->raid_disks < raid_disks && | |
6139 | rdev->data_offset < rdev->new_data_offset) | |
6140 | return -EINVAL; | |
6141 | if (mddev->raid_disks > raid_disks && | |
6142 | rdev->data_offset > rdev->new_data_offset) | |
6143 | return -EINVAL; | |
6144 | } | |
6145 | ||
63c70c4f | 6146 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
2c810cdd N |
6147 | if (mddev->delta_disks < 0) |
6148 | mddev->reshape_backwards = 1; | |
6149 | else if (mddev->delta_disks > 0) | |
6150 | mddev->reshape_backwards = 0; | |
63c70c4f N |
6151 | |
6152 | rv = mddev->pers->check_reshape(mddev); | |
2c810cdd | 6153 | if (rv < 0) { |
de171cb9 | 6154 | mddev->delta_disks = 0; |
2c810cdd N |
6155 | mddev->reshape_backwards = 0; |
6156 | } | |
da943b99 N |
6157 | return rv; |
6158 | } | |
6159 | ||
1da177e4 LT |
6160 | /* |
6161 | * update_array_info is used to change the configuration of an | |
6162 | * on-line array. | |
6163 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size | |
6164 | * fields in the info are checked against the array. | |
6165 | * Any differences that cannot be handled will cause an error. | |
6166 | * Normally, only one change can be managed at a time. | |
6167 | */ | |
fd01b88c | 6168 | static int update_array_info(struct mddev *mddev, mdu_array_info_t *info) |
1da177e4 LT |
6169 | { |
6170 | int rv = 0; | |
6171 | int cnt = 0; | |
36fa3063 N |
6172 | int state = 0; |
6173 | ||
6174 | /* calculate expected state,ignoring low bits */ | |
c3d9714e | 6175 | if (mddev->bitmap && mddev->bitmap_info.offset) |
36fa3063 | 6176 | state |= (1 << MD_SB_BITMAP_PRESENT); |
1da177e4 LT |
6177 | |
6178 | if (mddev->major_version != info->major_version || | |
6179 | mddev->minor_version != info->minor_version || | |
6180 | /* mddev->patch_version != info->patch_version || */ | |
6181 | mddev->ctime != info->ctime || | |
6182 | mddev->level != info->level || | |
6183 | /* mddev->layout != info->layout || */ | |
6184 | !mddev->persistent != info->not_persistent|| | |
9d8f0363 | 6185 | mddev->chunk_sectors != info->chunk_size >> 9 || |
36fa3063 N |
6186 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
6187 | ((state^info->state) & 0xfffffe00) | |
6188 | ) | |
1da177e4 LT |
6189 | return -EINVAL; |
6190 | /* Check there is only one change */ | |
58c0fed4 AN |
6191 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
6192 | cnt++; | |
6193 | if (mddev->raid_disks != info->raid_disks) | |
6194 | cnt++; | |
6195 | if (mddev->layout != info->layout) | |
6196 | cnt++; | |
6197 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) | |
6198 | cnt++; | |
6199 | if (cnt == 0) | |
6200 | return 0; | |
6201 | if (cnt > 1) | |
6202 | return -EINVAL; | |
1da177e4 LT |
6203 | |
6204 | if (mddev->layout != info->layout) { | |
6205 | /* Change layout | |
6206 | * we don't need to do anything at the md level, the | |
6207 | * personality will take care of it all. | |
6208 | */ | |
50ac168a | 6209 | if (mddev->pers->check_reshape == NULL) |
1da177e4 | 6210 | return -EINVAL; |
597a711b N |
6211 | else { |
6212 | mddev->new_layout = info->layout; | |
50ac168a | 6213 | rv = mddev->pers->check_reshape(mddev); |
597a711b N |
6214 | if (rv) |
6215 | mddev->new_layout = mddev->layout; | |
6216 | return rv; | |
6217 | } | |
1da177e4 | 6218 | } |
58c0fed4 | 6219 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
d71f9f88 | 6220 | rv = update_size(mddev, (sector_t)info->size * 2); |
a35b0d69 | 6221 | |
da943b99 N |
6222 | if (mddev->raid_disks != info->raid_disks) |
6223 | rv = update_raid_disks(mddev, info->raid_disks); | |
6224 | ||
36fa3063 | 6225 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
d66b1b39 | 6226 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) |
36fa3063 N |
6227 | return -EINVAL; |
6228 | if (mddev->recovery || mddev->sync_thread) | |
6229 | return -EBUSY; | |
6230 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { | |
6231 | /* add the bitmap */ | |
6232 | if (mddev->bitmap) | |
6233 | return -EEXIST; | |
c3d9714e | 6234 | if (mddev->bitmap_info.default_offset == 0) |
36fa3063 | 6235 | return -EINVAL; |
c3d9714e N |
6236 | mddev->bitmap_info.offset = |
6237 | mddev->bitmap_info.default_offset; | |
6409bb05 N |
6238 | mddev->bitmap_info.space = |
6239 | mddev->bitmap_info.default_space; | |
36fa3063 N |
6240 | mddev->pers->quiesce(mddev, 1); |
6241 | rv = bitmap_create(mddev); | |
69e51b44 N |
6242 | if (!rv) |
6243 | rv = bitmap_load(mddev); | |
36fa3063 N |
6244 | if (rv) |
6245 | bitmap_destroy(mddev); | |
6246 | mddev->pers->quiesce(mddev, 0); | |
6247 | } else { | |
6248 | /* remove the bitmap */ | |
6249 | if (!mddev->bitmap) | |
6250 | return -ENOENT; | |
1ec885cd | 6251 | if (mddev->bitmap->storage.file) |
36fa3063 N |
6252 | return -EINVAL; |
6253 | mddev->pers->quiesce(mddev, 1); | |
6254 | bitmap_destroy(mddev); | |
6255 | mddev->pers->quiesce(mddev, 0); | |
c3d9714e | 6256 | mddev->bitmap_info.offset = 0; |
36fa3063 N |
6257 | } |
6258 | } | |
850b2b42 | 6259 | md_update_sb(mddev, 1); |
1da177e4 LT |
6260 | return rv; |
6261 | } | |
6262 | ||
fd01b88c | 6263 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
1da177e4 | 6264 | { |
3cb03002 | 6265 | struct md_rdev *rdev; |
1ca69c4b | 6266 | int err = 0; |
1da177e4 LT |
6267 | |
6268 | if (mddev->pers == NULL) | |
6269 | return -ENODEV; | |
6270 | ||
1ca69c4b N |
6271 | rcu_read_lock(); |
6272 | rdev = find_rdev_rcu(mddev, dev); | |
1da177e4 | 6273 | if (!rdev) |
1ca69c4b N |
6274 | err = -ENODEV; |
6275 | else { | |
6276 | md_error(mddev, rdev); | |
6277 | if (!test_bit(Faulty, &rdev->flags)) | |
6278 | err = -EBUSY; | |
6279 | } | |
6280 | rcu_read_unlock(); | |
6281 | return err; | |
1da177e4 LT |
6282 | } |
6283 | ||
2f9618ce AN |
6284 | /* |
6285 | * We have a problem here : there is no easy way to give a CHS | |
6286 | * virtual geometry. We currently pretend that we have a 2 heads | |
6287 | * 4 sectors (with a BIG number of cylinders...). This drives | |
6288 | * dosfs just mad... ;-) | |
6289 | */ | |
a885c8c4 CH |
6290 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
6291 | { | |
fd01b88c | 6292 | struct mddev *mddev = bdev->bd_disk->private_data; |
a885c8c4 CH |
6293 | |
6294 | geo->heads = 2; | |
6295 | geo->sectors = 4; | |
49ce6cea | 6296 | geo->cylinders = mddev->array_sectors / 8; |
a885c8c4 CH |
6297 | return 0; |
6298 | } | |
6299 | ||
cb335f88 NS |
6300 | static inline bool md_ioctl_valid(unsigned int cmd) |
6301 | { | |
6302 | switch (cmd) { | |
6303 | case ADD_NEW_DISK: | |
6304 | case BLKROSET: | |
6305 | case GET_ARRAY_INFO: | |
6306 | case GET_BITMAP_FILE: | |
6307 | case GET_DISK_INFO: | |
6308 | case HOT_ADD_DISK: | |
6309 | case HOT_REMOVE_DISK: | |
6310 | case PRINT_RAID_DEBUG: | |
6311 | case RAID_AUTORUN: | |
6312 | case RAID_VERSION: | |
6313 | case RESTART_ARRAY_RW: | |
6314 | case RUN_ARRAY: | |
6315 | case SET_ARRAY_INFO: | |
6316 | case SET_BITMAP_FILE: | |
6317 | case SET_DISK_FAULTY: | |
6318 | case STOP_ARRAY: | |
6319 | case STOP_ARRAY_RO: | |
6320 | return true; | |
6321 | default: | |
6322 | return false; | |
6323 | } | |
6324 | } | |
6325 | ||
a39907fa | 6326 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
1da177e4 LT |
6327 | unsigned int cmd, unsigned long arg) |
6328 | { | |
6329 | int err = 0; | |
6330 | void __user *argp = (void __user *)arg; | |
fd01b88c | 6331 | struct mddev *mddev = NULL; |
e2218350 | 6332 | int ro; |
1da177e4 | 6333 | |
cb335f88 NS |
6334 | if (!md_ioctl_valid(cmd)) |
6335 | return -ENOTTY; | |
6336 | ||
506c9e44 N |
6337 | switch (cmd) { |
6338 | case RAID_VERSION: | |
6339 | case GET_ARRAY_INFO: | |
6340 | case GET_DISK_INFO: | |
6341 | break; | |
6342 | default: | |
6343 | if (!capable(CAP_SYS_ADMIN)) | |
6344 | return -EACCES; | |
6345 | } | |
1da177e4 LT |
6346 | |
6347 | /* | |
6348 | * Commands dealing with the RAID driver but not any | |
6349 | * particular array: | |
6350 | */ | |
c02c0aeb N |
6351 | switch (cmd) { |
6352 | case RAID_VERSION: | |
6353 | err = get_version(argp); | |
6354 | goto done; | |
1da177e4 | 6355 | |
c02c0aeb N |
6356 | case PRINT_RAID_DEBUG: |
6357 | err = 0; | |
6358 | md_print_devices(); | |
6359 | goto done; | |
1da177e4 LT |
6360 | |
6361 | #ifndef MODULE | |
c02c0aeb N |
6362 | case RAID_AUTORUN: |
6363 | err = 0; | |
6364 | autostart_arrays(arg); | |
6365 | goto done; | |
1da177e4 | 6366 | #endif |
c02c0aeb | 6367 | default:; |
1da177e4 LT |
6368 | } |
6369 | ||
6370 | /* | |
6371 | * Commands creating/starting a new array: | |
6372 | */ | |
6373 | ||
a39907fa | 6374 | mddev = bdev->bd_disk->private_data; |
1da177e4 LT |
6375 | |
6376 | if (!mddev) { | |
6377 | BUG(); | |
6378 | goto abort; | |
6379 | } | |
6380 | ||
1ca69c4b N |
6381 | /* Some actions do not requires the mutex */ |
6382 | switch (cmd) { | |
6383 | case GET_ARRAY_INFO: | |
6384 | if (!mddev->raid_disks && !mddev->external) | |
6385 | err = -ENODEV; | |
6386 | else | |
6387 | err = get_array_info(mddev, argp); | |
6388 | goto abort; | |
6389 | ||
6390 | case GET_DISK_INFO: | |
6391 | if (!mddev->raid_disks && !mddev->external) | |
6392 | err = -ENODEV; | |
6393 | else | |
6394 | err = get_disk_info(mddev, argp); | |
6395 | goto abort; | |
6396 | ||
6397 | case SET_DISK_FAULTY: | |
6398 | err = set_disk_faulty(mddev, new_decode_dev(arg)); | |
6399 | goto abort; | |
6400 | } | |
6401 | ||
a7a3f08d N |
6402 | if (cmd == ADD_NEW_DISK) |
6403 | /* need to ensure md_delayed_delete() has completed */ | |
6404 | flush_workqueue(md_misc_wq); | |
6405 | ||
90f5f7ad HR |
6406 | if (cmd == HOT_REMOVE_DISK) |
6407 | /* need to ensure recovery thread has run */ | |
6408 | wait_event_interruptible_timeout(mddev->sb_wait, | |
6409 | !test_bit(MD_RECOVERY_NEEDED, | |
6410 | &mddev->flags), | |
6411 | msecs_to_jiffies(5000)); | |
260fa034 N |
6412 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
6413 | /* Need to flush page cache, and ensure no-one else opens | |
6414 | * and writes | |
6415 | */ | |
6416 | mutex_lock(&mddev->open_mutex); | |
9ba3b7f5 | 6417 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
260fa034 N |
6418 | mutex_unlock(&mddev->open_mutex); |
6419 | err = -EBUSY; | |
6420 | goto abort; | |
6421 | } | |
6422 | set_bit(MD_STILL_CLOSED, &mddev->flags); | |
6423 | mutex_unlock(&mddev->open_mutex); | |
6424 | sync_blockdev(bdev); | |
6425 | } | |
1da177e4 LT |
6426 | err = mddev_lock(mddev); |
6427 | if (err) { | |
f72ffdd6 | 6428 | printk(KERN_INFO |
1da177e4 LT |
6429 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
6430 | err, cmd); | |
6431 | goto abort; | |
6432 | } | |
6433 | ||
c02c0aeb N |
6434 | if (cmd == SET_ARRAY_INFO) { |
6435 | mdu_array_info_t info; | |
6436 | if (!arg) | |
6437 | memset(&info, 0, sizeof(info)); | |
6438 | else if (copy_from_user(&info, argp, sizeof(info))) { | |
6439 | err = -EFAULT; | |
6440 | goto abort_unlock; | |
6441 | } | |
6442 | if (mddev->pers) { | |
6443 | err = update_array_info(mddev, &info); | |
6444 | if (err) { | |
6445 | printk(KERN_WARNING "md: couldn't update" | |
6446 | " array info. %d\n", err); | |
6447 | goto abort_unlock; | |
1da177e4 LT |
6448 | } |
6449 | goto done_unlock; | |
c02c0aeb N |
6450 | } |
6451 | if (!list_empty(&mddev->disks)) { | |
6452 | printk(KERN_WARNING | |
6453 | "md: array %s already has disks!\n", | |
6454 | mdname(mddev)); | |
6455 | err = -EBUSY; | |
6456 | goto abort_unlock; | |
6457 | } | |
6458 | if (mddev->raid_disks) { | |
6459 | printk(KERN_WARNING | |
6460 | "md: array %s already initialised!\n", | |
6461 | mdname(mddev)); | |
6462 | err = -EBUSY; | |
6463 | goto abort_unlock; | |
6464 | } | |
6465 | err = set_array_info(mddev, &info); | |
6466 | if (err) { | |
6467 | printk(KERN_WARNING "md: couldn't set" | |
6468 | " array info. %d\n", err); | |
6469 | goto abort_unlock; | |
6470 | } | |
6471 | goto done_unlock; | |
1da177e4 LT |
6472 | } |
6473 | ||
6474 | /* | |
6475 | * Commands querying/configuring an existing array: | |
6476 | */ | |
32a7627c | 6477 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
3f9d7b0d | 6478 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
a17184a9 N |
6479 | if ((!mddev->raid_disks && !mddev->external) |
6480 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY | |
6481 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE | |
6482 | && cmd != GET_BITMAP_FILE) { | |
1da177e4 LT |
6483 | err = -ENODEV; |
6484 | goto abort_unlock; | |
6485 | } | |
6486 | ||
6487 | /* | |
6488 | * Commands even a read-only array can execute: | |
6489 | */ | |
c02c0aeb N |
6490 | switch (cmd) { |
6491 | case GET_BITMAP_FILE: | |
6492 | err = get_bitmap_file(mddev, argp); | |
6493 | goto done_unlock; | |
32a7627c | 6494 | |
c02c0aeb N |
6495 | case RESTART_ARRAY_RW: |
6496 | err = restart_array(mddev); | |
6497 | goto done_unlock; | |
1da177e4 | 6498 | |
c02c0aeb N |
6499 | case STOP_ARRAY: |
6500 | err = do_md_stop(mddev, 0, bdev); | |
6501 | goto done_unlock; | |
1da177e4 | 6502 | |
c02c0aeb N |
6503 | case STOP_ARRAY_RO: |
6504 | err = md_set_readonly(mddev, bdev); | |
6505 | goto done_unlock; | |
1da177e4 | 6506 | |
3ea8929d N |
6507 | case HOT_REMOVE_DISK: |
6508 | err = hot_remove_disk(mddev, new_decode_dev(arg)); | |
6509 | goto done_unlock; | |
6510 | ||
7ceb17e8 N |
6511 | case ADD_NEW_DISK: |
6512 | /* We can support ADD_NEW_DISK on read-only arrays | |
6513 | * on if we are re-adding a preexisting device. | |
6514 | * So require mddev->pers and MD_DISK_SYNC. | |
6515 | */ | |
6516 | if (mddev->pers) { | |
6517 | mdu_disk_info_t info; | |
6518 | if (copy_from_user(&info, argp, sizeof(info))) | |
6519 | err = -EFAULT; | |
6520 | else if (!(info.state & (1<<MD_DISK_SYNC))) | |
6521 | /* Need to clear read-only for this */ | |
6522 | break; | |
6523 | else | |
6524 | err = add_new_disk(mddev, &info); | |
6525 | goto done_unlock; | |
6526 | } | |
6527 | break; | |
6528 | ||
c02c0aeb N |
6529 | case BLKROSET: |
6530 | if (get_user(ro, (int __user *)(arg))) { | |
6531 | err = -EFAULT; | |
6532 | goto done_unlock; | |
6533 | } | |
6534 | err = -EINVAL; | |
e2218350 | 6535 | |
c02c0aeb N |
6536 | /* if the bdev is going readonly the value of mddev->ro |
6537 | * does not matter, no writes are coming | |
6538 | */ | |
6539 | if (ro) | |
6540 | goto done_unlock; | |
e2218350 | 6541 | |
c02c0aeb N |
6542 | /* are we are already prepared for writes? */ |
6543 | if (mddev->ro != 1) | |
6544 | goto done_unlock; | |
e2218350 | 6545 | |
c02c0aeb N |
6546 | /* transitioning to readauto need only happen for |
6547 | * arrays that call md_write_start | |
6548 | */ | |
6549 | if (mddev->pers) { | |
6550 | err = restart_array(mddev); | |
6551 | if (err == 0) { | |
6552 | mddev->ro = 2; | |
6553 | set_disk_ro(mddev->gendisk, 0); | |
e2218350 | 6554 | } |
c02c0aeb N |
6555 | } |
6556 | goto done_unlock; | |
1da177e4 LT |
6557 | } |
6558 | ||
6559 | /* | |
6560 | * The remaining ioctls are changing the state of the | |
f91de92e N |
6561 | * superblock, so we do not allow them on read-only arrays. |
6562 | * However non-MD ioctls (e.g. get-size) will still come through | |
6563 | * here and hit the 'default' below, so only disallow | |
6564 | * 'md' ioctls, and switch to rw mode if started auto-readonly. | |
1da177e4 | 6565 | */ |
bb57fc64 | 6566 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
f91de92e N |
6567 | if (mddev->ro == 2) { |
6568 | mddev->ro = 0; | |
00bcb4ac | 6569 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
0fd62b86 | 6570 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
f3378b48 N |
6571 | /* mddev_unlock will wake thread */ |
6572 | /* If a device failed while we were read-only, we | |
6573 | * need to make sure the metadata is updated now. | |
6574 | */ | |
6575 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { | |
6576 | mddev_unlock(mddev); | |
6577 | wait_event(mddev->sb_wait, | |
6578 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && | |
6579 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); | |
29f097c4 | 6580 | mddev_lock_nointr(mddev); |
f3378b48 | 6581 | } |
f91de92e N |
6582 | } else { |
6583 | err = -EROFS; | |
6584 | goto abort_unlock; | |
6585 | } | |
1da177e4 LT |
6586 | } |
6587 | ||
c02c0aeb N |
6588 | switch (cmd) { |
6589 | case ADD_NEW_DISK: | |
1da177e4 | 6590 | { |
c02c0aeb N |
6591 | mdu_disk_info_t info; |
6592 | if (copy_from_user(&info, argp, sizeof(info))) | |
6593 | err = -EFAULT; | |
6594 | else | |
6595 | err = add_new_disk(mddev, &info); | |
6596 | goto done_unlock; | |
6597 | } | |
1da177e4 | 6598 | |
c02c0aeb N |
6599 | case HOT_ADD_DISK: |
6600 | err = hot_add_disk(mddev, new_decode_dev(arg)); | |
6601 | goto done_unlock; | |
1da177e4 | 6602 | |
c02c0aeb N |
6603 | case RUN_ARRAY: |
6604 | err = do_md_run(mddev); | |
6605 | goto done_unlock; | |
1da177e4 | 6606 | |
c02c0aeb N |
6607 | case SET_BITMAP_FILE: |
6608 | err = set_bitmap_file(mddev, (int)arg); | |
6609 | goto done_unlock; | |
32a7627c | 6610 | |
c02c0aeb N |
6611 | default: |
6612 | err = -EINVAL; | |
6613 | goto abort_unlock; | |
1da177e4 LT |
6614 | } |
6615 | ||
6616 | done_unlock: | |
6617 | abort_unlock: | |
d3374825 N |
6618 | if (mddev->hold_active == UNTIL_IOCTL && |
6619 | err != -EINVAL) | |
6620 | mddev->hold_active = 0; | |
1da177e4 LT |
6621 | mddev_unlock(mddev); |
6622 | ||
6623 | return err; | |
6624 | done: | |
6625 | if (err) | |
6626 | MD_BUG(); | |
6627 | abort: | |
6628 | return err; | |
6629 | } | |
aa98aa31 AB |
6630 | #ifdef CONFIG_COMPAT |
6631 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
6632 | unsigned int cmd, unsigned long arg) | |
6633 | { | |
6634 | switch (cmd) { | |
6635 | case HOT_REMOVE_DISK: | |
6636 | case HOT_ADD_DISK: | |
6637 | case SET_DISK_FAULTY: | |
6638 | case SET_BITMAP_FILE: | |
6639 | /* These take in integer arg, do not convert */ | |
6640 | break; | |
6641 | default: | |
6642 | arg = (unsigned long)compat_ptr(arg); | |
6643 | break; | |
6644 | } | |
6645 | ||
6646 | return md_ioctl(bdev, mode, cmd, arg); | |
6647 | } | |
6648 | #endif /* CONFIG_COMPAT */ | |
1da177e4 | 6649 | |
a39907fa | 6650 | static int md_open(struct block_device *bdev, fmode_t mode) |
1da177e4 LT |
6651 | { |
6652 | /* | |
6653 | * Succeed if we can lock the mddev, which confirms that | |
6654 | * it isn't being stopped right now. | |
6655 | */ | |
fd01b88c | 6656 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
1da177e4 LT |
6657 | int err; |
6658 | ||
0c098220 YL |
6659 | if (!mddev) |
6660 | return -ENODEV; | |
6661 | ||
d3374825 N |
6662 | if (mddev->gendisk != bdev->bd_disk) { |
6663 | /* we are racing with mddev_put which is discarding this | |
6664 | * bd_disk. | |
6665 | */ | |
6666 | mddev_put(mddev); | |
6667 | /* Wait until bdev->bd_disk is definitely gone */ | |
e804ac78 | 6668 | flush_workqueue(md_misc_wq); |
d3374825 N |
6669 | /* Then retry the open from the top */ |
6670 | return -ERESTARTSYS; | |
6671 | } | |
6672 | BUG_ON(mddev != bdev->bd_disk->private_data); | |
6673 | ||
c8c00a69 | 6674 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
1da177e4 LT |
6675 | goto out; |
6676 | ||
6677 | err = 0; | |
f2ea68cf | 6678 | atomic_inc(&mddev->openers); |
260fa034 | 6679 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
c8c00a69 | 6680 | mutex_unlock(&mddev->open_mutex); |
1da177e4 | 6681 | |
f0b4f7e2 | 6682 | check_disk_change(bdev); |
1da177e4 LT |
6683 | out: |
6684 | return err; | |
6685 | } | |
6686 | ||
db2a144b | 6687 | static void md_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 6688 | { |
f72ffdd6 | 6689 | struct mddev *mddev = disk->private_data; |
1da177e4 | 6690 | |
52e5f9d1 | 6691 | BUG_ON(!mddev); |
f2ea68cf | 6692 | atomic_dec(&mddev->openers); |
1da177e4 | 6693 | mddev_put(mddev); |
1da177e4 | 6694 | } |
f0b4f7e2 N |
6695 | |
6696 | static int md_media_changed(struct gendisk *disk) | |
6697 | { | |
fd01b88c | 6698 | struct mddev *mddev = disk->private_data; |
f0b4f7e2 N |
6699 | |
6700 | return mddev->changed; | |
6701 | } | |
6702 | ||
6703 | static int md_revalidate(struct gendisk *disk) | |
6704 | { | |
fd01b88c | 6705 | struct mddev *mddev = disk->private_data; |
f0b4f7e2 N |
6706 | |
6707 | mddev->changed = 0; | |
6708 | return 0; | |
6709 | } | |
83d5cde4 | 6710 | static const struct block_device_operations md_fops = |
1da177e4 LT |
6711 | { |
6712 | .owner = THIS_MODULE, | |
a39907fa AV |
6713 | .open = md_open, |
6714 | .release = md_release, | |
b492b852 | 6715 | .ioctl = md_ioctl, |
aa98aa31 AB |
6716 | #ifdef CONFIG_COMPAT |
6717 | .compat_ioctl = md_compat_ioctl, | |
6718 | #endif | |
a885c8c4 | 6719 | .getgeo = md_getgeo, |
f0b4f7e2 N |
6720 | .media_changed = md_media_changed, |
6721 | .revalidate_disk= md_revalidate, | |
1da177e4 LT |
6722 | }; |
6723 | ||
f72ffdd6 | 6724 | static int md_thread(void *arg) |
1da177e4 | 6725 | { |
2b8bf345 | 6726 | struct md_thread *thread = arg; |
1da177e4 | 6727 | |
1da177e4 LT |
6728 | /* |
6729 | * md_thread is a 'system-thread', it's priority should be very | |
6730 | * high. We avoid resource deadlocks individually in each | |
6731 | * raid personality. (RAID5 does preallocation) We also use RR and | |
6732 | * the very same RT priority as kswapd, thus we will never get | |
6733 | * into a priority inversion deadlock. | |
6734 | * | |
6735 | * we definitely have to have equal or higher priority than | |
6736 | * bdflush, otherwise bdflush will deadlock if there are too | |
6737 | * many dirty RAID5 blocks. | |
6738 | */ | |
1da177e4 | 6739 | |
6985c43f | 6740 | allow_signal(SIGKILL); |
a6fb0934 | 6741 | while (!kthread_should_stop()) { |
1da177e4 | 6742 | |
93588e22 N |
6743 | /* We need to wait INTERRUPTIBLE so that |
6744 | * we don't add to the load-average. | |
6745 | * That means we need to be sure no signals are | |
6746 | * pending | |
6747 | */ | |
6748 | if (signal_pending(current)) | |
6749 | flush_signals(current); | |
6750 | ||
6751 | wait_event_interruptible_timeout | |
6752 | (thread->wqueue, | |
6753 | test_bit(THREAD_WAKEUP, &thread->flags) | |
6754 | || kthread_should_stop(), | |
6755 | thread->timeout); | |
1da177e4 | 6756 | |
6c987910 N |
6757 | clear_bit(THREAD_WAKEUP, &thread->flags); |
6758 | if (!kthread_should_stop()) | |
4ed8731d | 6759 | thread->run(thread); |
1da177e4 | 6760 | } |
a6fb0934 | 6761 | |
1da177e4 LT |
6762 | return 0; |
6763 | } | |
6764 | ||
2b8bf345 | 6765 | void md_wakeup_thread(struct md_thread *thread) |
1da177e4 LT |
6766 | { |
6767 | if (thread) { | |
36a4e1fe | 6768 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
1da177e4 LT |
6769 | set_bit(THREAD_WAKEUP, &thread->flags); |
6770 | wake_up(&thread->wqueue); | |
6771 | } | |
6772 | } | |
6773 | ||
4ed8731d SL |
6774 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
6775 | struct mddev *mddev, const char *name) | |
1da177e4 | 6776 | { |
2b8bf345 | 6777 | struct md_thread *thread; |
1da177e4 | 6778 | |
2b8bf345 | 6779 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
1da177e4 LT |
6780 | if (!thread) |
6781 | return NULL; | |
6782 | ||
1da177e4 LT |
6783 | init_waitqueue_head(&thread->wqueue); |
6784 | ||
1da177e4 LT |
6785 | thread->run = run; |
6786 | thread->mddev = mddev; | |
32a7627c | 6787 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
0da3c619 N |
6788 | thread->tsk = kthread_run(md_thread, thread, |
6789 | "%s_%s", | |
6790 | mdname(thread->mddev), | |
0232605d | 6791 | name); |
a6fb0934 | 6792 | if (IS_ERR(thread->tsk)) { |
1da177e4 LT |
6793 | kfree(thread); |
6794 | return NULL; | |
6795 | } | |
1da177e4 LT |
6796 | return thread; |
6797 | } | |
6798 | ||
2b8bf345 | 6799 | void md_unregister_thread(struct md_thread **threadp) |
1da177e4 | 6800 | { |
2b8bf345 | 6801 | struct md_thread *thread = *threadp; |
e0cf8f04 N |
6802 | if (!thread) |
6803 | return; | |
36a4e1fe | 6804 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
01f96c0a N |
6805 | /* Locking ensures that mddev_unlock does not wake_up a |
6806 | * non-existent thread | |
6807 | */ | |
6808 | spin_lock(&pers_lock); | |
6809 | *threadp = NULL; | |
6810 | spin_unlock(&pers_lock); | |
a6fb0934 N |
6811 | |
6812 | kthread_stop(thread->tsk); | |
1da177e4 LT |
6813 | kfree(thread); |
6814 | } | |
6815 | ||
fd01b88c | 6816 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
6817 | { |
6818 | if (!mddev) { | |
6819 | MD_BUG(); | |
6820 | return; | |
6821 | } | |
6822 | ||
b2d444d7 | 6823 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
1da177e4 | 6824 | return; |
6bfe0b49 | 6825 | |
de393cde | 6826 | if (!mddev->pers || !mddev->pers->error_handler) |
1da177e4 LT |
6827 | return; |
6828 | mddev->pers->error_handler(mddev,rdev); | |
72a23c21 NB |
6829 | if (mddev->degraded) |
6830 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); | |
00bcb4ac | 6831 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
1da177e4 LT |
6832 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
6833 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
6834 | md_wakeup_thread(mddev->thread); | |
768a418d | 6835 | if (mddev->event_work.func) |
e804ac78 | 6836 | queue_work(md_misc_wq, &mddev->event_work); |
c331eb04 | 6837 | md_new_event_inintr(mddev); |
1da177e4 LT |
6838 | } |
6839 | ||
6840 | /* seq_file implementation /proc/mdstat */ | |
6841 | ||
6842 | static void status_unused(struct seq_file *seq) | |
6843 | { | |
6844 | int i = 0; | |
3cb03002 | 6845 | struct md_rdev *rdev; |
1da177e4 LT |
6846 | |
6847 | seq_printf(seq, "unused devices: "); | |
6848 | ||
159ec1fc | 6849 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
1da177e4 LT |
6850 | char b[BDEVNAME_SIZE]; |
6851 | i++; | |
6852 | seq_printf(seq, "%s ", | |
6853 | bdevname(rdev->bdev,b)); | |
6854 | } | |
6855 | if (!i) | |
6856 | seq_printf(seq, "<none>"); | |
6857 | ||
6858 | seq_printf(seq, "\n"); | |
6859 | } | |
6860 | ||
f72ffdd6 | 6861 | static void status_resync(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 6862 | { |
dd71cf6b N |
6863 | sector_t max_sectors, resync, res; |
6864 | unsigned long dt, db; | |
6865 | sector_t rt; | |
4588b42e N |
6866 | int scale; |
6867 | unsigned int per_milli; | |
1da177e4 | 6868 | |
72f36d59 N |
6869 | if (mddev->curr_resync <= 3) |
6870 | resync = 0; | |
6871 | else | |
6872 | resync = mddev->curr_resync | |
6873 | - atomic_read(&mddev->recovery_active); | |
1da177e4 | 6874 | |
c804cdec N |
6875 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
6876 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
dd71cf6b | 6877 | max_sectors = mddev->resync_max_sectors; |
1da177e4 | 6878 | else |
dd71cf6b | 6879 | max_sectors = mddev->dev_sectors; |
1da177e4 LT |
6880 | |
6881 | /* | |
6882 | * Should not happen. | |
6883 | */ | |
dd71cf6b | 6884 | if (!max_sectors) { |
1da177e4 LT |
6885 | MD_BUG(); |
6886 | return; | |
6887 | } | |
4588b42e | 6888 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
dd71cf6b | 6889 | * in a sector_t, and (max_sectors>>scale) will fit in a |
4588b42e N |
6890 | * u32, as those are the requirements for sector_div. |
6891 | * Thus 'scale' must be at least 10 | |
6892 | */ | |
6893 | scale = 10; | |
6894 | if (sizeof(sector_t) > sizeof(unsigned long)) { | |
dd71cf6b | 6895 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
4588b42e N |
6896 | scale++; |
6897 | } | |
6898 | res = (resync>>scale)*1000; | |
dd71cf6b | 6899 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
4588b42e N |
6900 | |
6901 | per_milli = res; | |
1da177e4 | 6902 | { |
4588b42e | 6903 | int i, x = per_milli/50, y = 20-x; |
1da177e4 LT |
6904 | seq_printf(seq, "["); |
6905 | for (i = 0; i < x; i++) | |
6906 | seq_printf(seq, "="); | |
6907 | seq_printf(seq, ">"); | |
6908 | for (i = 0; i < y; i++) | |
6909 | seq_printf(seq, "."); | |
6910 | seq_printf(seq, "] "); | |
6911 | } | |
4588b42e | 6912 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
ccfcc3c1 N |
6913 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
6914 | "reshape" : | |
61df9d91 N |
6915 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
6916 | "check" : | |
6917 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? | |
6918 | "resync" : "recovery"))), | |
6919 | per_milli/10, per_milli % 10, | |
dd71cf6b N |
6920 | (unsigned long long) resync/2, |
6921 | (unsigned long long) max_sectors/2); | |
1da177e4 LT |
6922 | |
6923 | /* | |
1da177e4 LT |
6924 | * dt: time from mark until now |
6925 | * db: blocks written from mark until now | |
6926 | * rt: remaining time | |
dd71cf6b N |
6927 | * |
6928 | * rt is a sector_t, so could be 32bit or 64bit. | |
6929 | * So we divide before multiply in case it is 32bit and close | |
6930 | * to the limit. | |
25985edc | 6931 | * We scale the divisor (db) by 32 to avoid losing precision |
dd71cf6b N |
6932 | * near the end of resync when the number of remaining sectors |
6933 | * is close to 'db'. | |
6934 | * We then divide rt by 32 after multiplying by db to compensate. | |
6935 | * The '+1' avoids division by zero if db is very small. | |
1da177e4 LT |
6936 | */ |
6937 | dt = ((jiffies - mddev->resync_mark) / HZ); | |
6938 | if (!dt) dt++; | |
ff4e8d9a N |
6939 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
6940 | - mddev->resync_mark_cnt; | |
1da177e4 | 6941 | |
dd71cf6b N |
6942 | rt = max_sectors - resync; /* number of remaining sectors */ |
6943 | sector_div(rt, db/32+1); | |
6944 | rt *= dt; | |
6945 | rt >>= 5; | |
6946 | ||
6947 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, | |
6948 | ((unsigned long)rt % 60)/6); | |
1da177e4 | 6949 | |
ff4e8d9a | 6950 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
1da177e4 LT |
6951 | } |
6952 | ||
6953 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) | |
6954 | { | |
6955 | struct list_head *tmp; | |
6956 | loff_t l = *pos; | |
fd01b88c | 6957 | struct mddev *mddev; |
1da177e4 LT |
6958 | |
6959 | if (l >= 0x10000) | |
6960 | return NULL; | |
6961 | if (!l--) | |
6962 | /* header */ | |
6963 | return (void*)1; | |
6964 | ||
6965 | spin_lock(&all_mddevs_lock); | |
6966 | list_for_each(tmp,&all_mddevs) | |
6967 | if (!l--) { | |
fd01b88c | 6968 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
1da177e4 LT |
6969 | mddev_get(mddev); |
6970 | spin_unlock(&all_mddevs_lock); | |
6971 | return mddev; | |
6972 | } | |
6973 | spin_unlock(&all_mddevs_lock); | |
6974 | if (!l--) | |
6975 | return (void*)2;/* tail */ | |
6976 | return NULL; | |
6977 | } | |
6978 | ||
6979 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
6980 | { | |
6981 | struct list_head *tmp; | |
fd01b88c | 6982 | struct mddev *next_mddev, *mddev = v; |
f72ffdd6 | 6983 | |
1da177e4 LT |
6984 | ++*pos; |
6985 | if (v == (void*)2) | |
6986 | return NULL; | |
6987 | ||
6988 | spin_lock(&all_mddevs_lock); | |
6989 | if (v == (void*)1) | |
6990 | tmp = all_mddevs.next; | |
6991 | else | |
6992 | tmp = mddev->all_mddevs.next; | |
6993 | if (tmp != &all_mddevs) | |
fd01b88c | 6994 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
1da177e4 LT |
6995 | else { |
6996 | next_mddev = (void*)2; | |
6997 | *pos = 0x10000; | |
f72ffdd6 | 6998 | } |
1da177e4 LT |
6999 | spin_unlock(&all_mddevs_lock); |
7000 | ||
7001 | if (v != (void*)1) | |
7002 | mddev_put(mddev); | |
7003 | return next_mddev; | |
7004 | ||
7005 | } | |
7006 | ||
7007 | static void md_seq_stop(struct seq_file *seq, void *v) | |
7008 | { | |
fd01b88c | 7009 | struct mddev *mddev = v; |
1da177e4 LT |
7010 | |
7011 | if (mddev && v != (void*)1 && v != (void*)2) | |
7012 | mddev_put(mddev); | |
7013 | } | |
7014 | ||
7015 | static int md_seq_show(struct seq_file *seq, void *v) | |
7016 | { | |
fd01b88c | 7017 | struct mddev *mddev = v; |
dd8ac336 | 7018 | sector_t sectors; |
3cb03002 | 7019 | struct md_rdev *rdev; |
1da177e4 LT |
7020 | |
7021 | if (v == (void*)1) { | |
84fc4b56 | 7022 | struct md_personality *pers; |
1da177e4 LT |
7023 | seq_printf(seq, "Personalities : "); |
7024 | spin_lock(&pers_lock); | |
2604b703 N |
7025 | list_for_each_entry(pers, &pers_list, list) |
7026 | seq_printf(seq, "[%s] ", pers->name); | |
1da177e4 LT |
7027 | |
7028 | spin_unlock(&pers_lock); | |
7029 | seq_printf(seq, "\n"); | |
f1514638 | 7030 | seq->poll_event = atomic_read(&md_event_count); |
1da177e4 LT |
7031 | return 0; |
7032 | } | |
7033 | if (v == (void*)2) { | |
7034 | status_unused(seq); | |
7035 | return 0; | |
7036 | } | |
7037 | ||
5dc5cf7d | 7038 | if (mddev_lock(mddev) < 0) |
1da177e4 | 7039 | return -EINTR; |
5dc5cf7d | 7040 | |
1da177e4 LT |
7041 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
7042 | seq_printf(seq, "%s : %sactive", mdname(mddev), | |
7043 | mddev->pers ? "" : "in"); | |
7044 | if (mddev->pers) { | |
f91de92e | 7045 | if (mddev->ro==1) |
1da177e4 | 7046 | seq_printf(seq, " (read-only)"); |
f91de92e | 7047 | if (mddev->ro==2) |
52720ae7 | 7048 | seq_printf(seq, " (auto-read-only)"); |
1da177e4 LT |
7049 | seq_printf(seq, " %s", mddev->pers->name); |
7050 | } | |
7051 | ||
dd8ac336 | 7052 | sectors = 0; |
dafb20fa | 7053 | rdev_for_each(rdev, mddev) { |
1da177e4 LT |
7054 | char b[BDEVNAME_SIZE]; |
7055 | seq_printf(seq, " %s[%d]", | |
7056 | bdevname(rdev->bdev,b), rdev->desc_nr); | |
8ddf9efe N |
7057 | if (test_bit(WriteMostly, &rdev->flags)) |
7058 | seq_printf(seq, "(W)"); | |
b2d444d7 | 7059 | if (test_bit(Faulty, &rdev->flags)) { |
1da177e4 LT |
7060 | seq_printf(seq, "(F)"); |
7061 | continue; | |
2d78f8c4 N |
7062 | } |
7063 | if (rdev->raid_disk < 0) | |
b325a32e | 7064 | seq_printf(seq, "(S)"); /* spare */ |
2d78f8c4 N |
7065 | if (test_bit(Replacement, &rdev->flags)) |
7066 | seq_printf(seq, "(R)"); | |
dd8ac336 | 7067 | sectors += rdev->sectors; |
1da177e4 LT |
7068 | } |
7069 | ||
7070 | if (!list_empty(&mddev->disks)) { | |
7071 | if (mddev->pers) | |
7072 | seq_printf(seq, "\n %llu blocks", | |
f233ea5c AN |
7073 | (unsigned long long) |
7074 | mddev->array_sectors / 2); | |
1da177e4 LT |
7075 | else |
7076 | seq_printf(seq, "\n %llu blocks", | |
dd8ac336 | 7077 | (unsigned long long)sectors / 2); |
1da177e4 | 7078 | } |
1cd6bf19 N |
7079 | if (mddev->persistent) { |
7080 | if (mddev->major_version != 0 || | |
7081 | mddev->minor_version != 90) { | |
7082 | seq_printf(seq," super %d.%d", | |
7083 | mddev->major_version, | |
7084 | mddev->minor_version); | |
7085 | } | |
e691063a N |
7086 | } else if (mddev->external) |
7087 | seq_printf(seq, " super external:%s", | |
7088 | mddev->metadata_type); | |
7089 | else | |
1cd6bf19 | 7090 | seq_printf(seq, " super non-persistent"); |
1da177e4 LT |
7091 | |
7092 | if (mddev->pers) { | |
d710e138 | 7093 | mddev->pers->status(seq, mddev); |
f72ffdd6 | 7094 | seq_printf(seq, "\n "); |
8e1b39d6 N |
7095 | if (mddev->pers->sync_request) { |
7096 | if (mddev->curr_resync > 2) { | |
d710e138 | 7097 | status_resync(seq, mddev); |
8e1b39d6 | 7098 | seq_printf(seq, "\n "); |
72f36d59 | 7099 | } else if (mddev->curr_resync >= 1) |
8e1b39d6 N |
7100 | seq_printf(seq, "\tresync=DELAYED\n "); |
7101 | else if (mddev->recovery_cp < MaxSector) | |
7102 | seq_printf(seq, "\tresync=PENDING\n "); | |
7103 | } | |
32a7627c N |
7104 | } else |
7105 | seq_printf(seq, "\n "); | |
7106 | ||
57148964 | 7107 | bitmap_status(seq, mddev->bitmap); |
1da177e4 LT |
7108 | |
7109 | seq_printf(seq, "\n"); | |
7110 | } | |
7111 | mddev_unlock(mddev); | |
f72ffdd6 | 7112 | |
1da177e4 LT |
7113 | return 0; |
7114 | } | |
7115 | ||
110518bc | 7116 | static const struct seq_operations md_seq_ops = { |
1da177e4 LT |
7117 | .start = md_seq_start, |
7118 | .next = md_seq_next, | |
7119 | .stop = md_seq_stop, | |
7120 | .show = md_seq_show, | |
7121 | }; | |
7122 | ||
7123 | static int md_seq_open(struct inode *inode, struct file *file) | |
7124 | { | |
f1514638 | 7125 | struct seq_file *seq; |
1da177e4 LT |
7126 | int error; |
7127 | ||
7128 | error = seq_open(file, &md_seq_ops); | |
d7603b7e | 7129 | if (error) |
f1514638 KS |
7130 | return error; |
7131 | ||
7132 | seq = file->private_data; | |
7133 | seq->poll_event = atomic_read(&md_event_count); | |
1da177e4 LT |
7134 | return error; |
7135 | } | |
7136 | ||
e2f23b60 | 7137 | static int md_unloading; |
d7603b7e N |
7138 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
7139 | { | |
f1514638 | 7140 | struct seq_file *seq = filp->private_data; |
d7603b7e N |
7141 | int mask; |
7142 | ||
e2f23b60 N |
7143 | if (md_unloading) |
7144 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI;; | |
d7603b7e N |
7145 | poll_wait(filp, &md_event_waiters, wait); |
7146 | ||
7147 | /* always allow read */ | |
7148 | mask = POLLIN | POLLRDNORM; | |
7149 | ||
f1514638 | 7150 | if (seq->poll_event != atomic_read(&md_event_count)) |
d7603b7e N |
7151 | mask |= POLLERR | POLLPRI; |
7152 | return mask; | |
7153 | } | |
7154 | ||
fa027c2a | 7155 | static const struct file_operations md_seq_fops = { |
e24650c2 | 7156 | .owner = THIS_MODULE, |
1da177e4 LT |
7157 | .open = md_seq_open, |
7158 | .read = seq_read, | |
7159 | .llseek = seq_lseek, | |
c3f94b40 | 7160 | .release = seq_release_private, |
d7603b7e | 7161 | .poll = mdstat_poll, |
1da177e4 LT |
7162 | }; |
7163 | ||
84fc4b56 | 7164 | int register_md_personality(struct md_personality *p) |
1da177e4 | 7165 | { |
50bd3774 CY |
7166 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
7167 | p->name, p->level); | |
1da177e4 | 7168 | spin_lock(&pers_lock); |
2604b703 | 7169 | list_add_tail(&p->list, &pers_list); |
1da177e4 LT |
7170 | spin_unlock(&pers_lock); |
7171 | return 0; | |
7172 | } | |
7173 | ||
84fc4b56 | 7174 | int unregister_md_personality(struct md_personality *p) |
1da177e4 | 7175 | { |
2604b703 | 7176 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
1da177e4 | 7177 | spin_lock(&pers_lock); |
2604b703 | 7178 | list_del_init(&p->list); |
1da177e4 LT |
7179 | spin_unlock(&pers_lock); |
7180 | return 0; | |
7181 | } | |
7182 | ||
fd01b88c | 7183 | static int is_mddev_idle(struct mddev *mddev, int init) |
1da177e4 | 7184 | { |
f72ffdd6 | 7185 | struct md_rdev *rdev; |
1da177e4 | 7186 | int idle; |
eea1bf38 | 7187 | int curr_events; |
1da177e4 LT |
7188 | |
7189 | idle = 1; | |
4b80991c N |
7190 | rcu_read_lock(); |
7191 | rdev_for_each_rcu(rdev, mddev) { | |
1da177e4 | 7192 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
eea1bf38 N |
7193 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
7194 | (int)part_stat_read(&disk->part0, sectors[1]) - | |
7195 | atomic_read(&disk->sync_io); | |
713f6ab1 N |
7196 | /* sync IO will cause sync_io to increase before the disk_stats |
7197 | * as sync_io is counted when a request starts, and | |
7198 | * disk_stats is counted when it completes. | |
7199 | * So resync activity will cause curr_events to be smaller than | |
7200 | * when there was no such activity. | |
7201 | * non-sync IO will cause disk_stat to increase without | |
7202 | * increasing sync_io so curr_events will (eventually) | |
7203 | * be larger than it was before. Once it becomes | |
7204 | * substantially larger, the test below will cause | |
7205 | * the array to appear non-idle, and resync will slow | |
7206 | * down. | |
7207 | * If there is a lot of outstanding resync activity when | |
7208 | * we set last_event to curr_events, then all that activity | |
7209 | * completing might cause the array to appear non-idle | |
7210 | * and resync will be slowed down even though there might | |
7211 | * not have been non-resync activity. This will only | |
7212 | * happen once though. 'last_events' will soon reflect | |
7213 | * the state where there is little or no outstanding | |
7214 | * resync requests, and further resync activity will | |
7215 | * always make curr_events less than last_events. | |
c0e48521 | 7216 | * |
1da177e4 | 7217 | */ |
eea1bf38 | 7218 | if (init || curr_events - rdev->last_events > 64) { |
1da177e4 LT |
7219 | rdev->last_events = curr_events; |
7220 | idle = 0; | |
7221 | } | |
7222 | } | |
4b80991c | 7223 | rcu_read_unlock(); |
1da177e4 LT |
7224 | return idle; |
7225 | } | |
7226 | ||
fd01b88c | 7227 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
1da177e4 LT |
7228 | { |
7229 | /* another "blocks" (512byte) blocks have been synced */ | |
7230 | atomic_sub(blocks, &mddev->recovery_active); | |
7231 | wake_up(&mddev->recovery_wait); | |
7232 | if (!ok) { | |
dfc70645 | 7233 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
0a19caab | 7234 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
1da177e4 LT |
7235 | md_wakeup_thread(mddev->thread); |
7236 | // stop recovery, signal do_sync .... | |
7237 | } | |
7238 | } | |
7239 | ||
06d91a5f N |
7240 | /* md_write_start(mddev, bi) |
7241 | * If we need to update some array metadata (e.g. 'active' flag | |
3d310eb7 N |
7242 | * in superblock) before writing, schedule a superblock update |
7243 | * and wait for it to complete. | |
06d91a5f | 7244 | */ |
fd01b88c | 7245 | void md_write_start(struct mddev *mddev, struct bio *bi) |
1da177e4 | 7246 | { |
0fd62b86 | 7247 | int did_change = 0; |
06d91a5f | 7248 | if (bio_data_dir(bi) != WRITE) |
3d310eb7 | 7249 | return; |
06d91a5f | 7250 | |
f91de92e N |
7251 | BUG_ON(mddev->ro == 1); |
7252 | if (mddev->ro == 2) { | |
7253 | /* need to switch to read/write */ | |
7254 | mddev->ro = 0; | |
7255 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
7256 | md_wakeup_thread(mddev->thread); | |
25156198 | 7257 | md_wakeup_thread(mddev->sync_thread); |
0fd62b86 | 7258 | did_change = 1; |
f91de92e | 7259 | } |
06d91a5f | 7260 | atomic_inc(&mddev->writes_pending); |
31a59e34 N |
7261 | if (mddev->safemode == 1) |
7262 | mddev->safemode = 0; | |
06d91a5f | 7263 | if (mddev->in_sync) { |
a9701a30 | 7264 | spin_lock_irq(&mddev->write_lock); |
3d310eb7 N |
7265 | if (mddev->in_sync) { |
7266 | mddev->in_sync = 0; | |
850b2b42 | 7267 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
070dc6dd | 7268 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
3d310eb7 | 7269 | md_wakeup_thread(mddev->thread); |
0fd62b86 | 7270 | did_change = 1; |
3d310eb7 | 7271 | } |
a9701a30 | 7272 | spin_unlock_irq(&mddev->write_lock); |
06d91a5f | 7273 | } |
0fd62b86 | 7274 | if (did_change) |
00bcb4ac | 7275 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
09a44cc1 | 7276 | wait_event(mddev->sb_wait, |
09a44cc1 | 7277 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
1da177e4 LT |
7278 | } |
7279 | ||
fd01b88c | 7280 | void md_write_end(struct mddev *mddev) |
1da177e4 LT |
7281 | { |
7282 | if (atomic_dec_and_test(&mddev->writes_pending)) { | |
7283 | if (mddev->safemode == 2) | |
7284 | md_wakeup_thread(mddev->thread); | |
16f17b39 | 7285 | else if (mddev->safemode_delay) |
1da177e4 LT |
7286 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
7287 | } | |
7288 | } | |
7289 | ||
2a2275d6 N |
7290 | /* md_allow_write(mddev) |
7291 | * Calling this ensures that the array is marked 'active' so that writes | |
7292 | * may proceed without blocking. It is important to call this before | |
7293 | * attempting a GFP_KERNEL allocation while holding the mddev lock. | |
7294 | * Must be called with mddev_lock held. | |
b5470dc5 DW |
7295 | * |
7296 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock | |
7297 | * is dropped, so return -EAGAIN after notifying userspace. | |
2a2275d6 | 7298 | */ |
fd01b88c | 7299 | int md_allow_write(struct mddev *mddev) |
2a2275d6 N |
7300 | { |
7301 | if (!mddev->pers) | |
b5470dc5 | 7302 | return 0; |
2a2275d6 | 7303 | if (mddev->ro) |
b5470dc5 | 7304 | return 0; |
1a0fd497 | 7305 | if (!mddev->pers->sync_request) |
b5470dc5 | 7306 | return 0; |
2a2275d6 N |
7307 | |
7308 | spin_lock_irq(&mddev->write_lock); | |
7309 | if (mddev->in_sync) { | |
7310 | mddev->in_sync = 0; | |
7311 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); | |
070dc6dd | 7312 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
2a2275d6 N |
7313 | if (mddev->safemode_delay && |
7314 | mddev->safemode == 0) | |
7315 | mddev->safemode = 1; | |
7316 | spin_unlock_irq(&mddev->write_lock); | |
7317 | md_update_sb(mddev, 0); | |
00bcb4ac | 7318 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
2a2275d6 N |
7319 | } else |
7320 | spin_unlock_irq(&mddev->write_lock); | |
b5470dc5 | 7321 | |
070dc6dd | 7322 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
b5470dc5 DW |
7323 | return -EAGAIN; |
7324 | else | |
7325 | return 0; | |
2a2275d6 N |
7326 | } |
7327 | EXPORT_SYMBOL_GPL(md_allow_write); | |
7328 | ||
1da177e4 LT |
7329 | #define SYNC_MARKS 10 |
7330 | #define SYNC_MARK_STEP (3*HZ) | |
54f89341 | 7331 | #define UPDATE_FREQUENCY (5*60*HZ) |
4ed8731d | 7332 | void md_do_sync(struct md_thread *thread) |
1da177e4 | 7333 | { |
4ed8731d | 7334 | struct mddev *mddev = thread->mddev; |
fd01b88c | 7335 | struct mddev *mddev2; |
1da177e4 LT |
7336 | unsigned int currspeed = 0, |
7337 | window; | |
ac7e50a3 | 7338 | sector_t max_sectors,j, io_sectors, recovery_done; |
1da177e4 | 7339 | unsigned long mark[SYNC_MARKS]; |
54f89341 | 7340 | unsigned long update_time; |
1da177e4 LT |
7341 | sector_t mark_cnt[SYNC_MARKS]; |
7342 | int last_mark,m; | |
7343 | struct list_head *tmp; | |
7344 | sector_t last_check; | |
57afd89f | 7345 | int skipped = 0; |
3cb03002 | 7346 | struct md_rdev *rdev; |
c4a39551 | 7347 | char *desc, *action = NULL; |
7c2c57c9 | 7348 | struct blk_plug plug; |
1da177e4 LT |
7349 | |
7350 | /* just incase thread restarts... */ | |
7351 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) | |
7352 | return; | |
3991b31e N |
7353 | if (mddev->ro) {/* never try to sync a read-only array */ |
7354 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
5fd6c1dc | 7355 | return; |
3991b31e | 7356 | } |
1da177e4 | 7357 | |
61df9d91 | 7358 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
c4a39551 | 7359 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
61df9d91 | 7360 | desc = "data-check"; |
c4a39551 JB |
7361 | action = "check"; |
7362 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { | |
61df9d91 | 7363 | desc = "requested-resync"; |
c4a39551 JB |
7364 | action = "repair"; |
7365 | } else | |
61df9d91 N |
7366 | desc = "resync"; |
7367 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
7368 | desc = "reshape"; | |
7369 | else | |
7370 | desc = "recovery"; | |
7371 | ||
c4a39551 JB |
7372 | mddev->last_sync_action = action ?: desc; |
7373 | ||
1da177e4 LT |
7374 | /* we overload curr_resync somewhat here. |
7375 | * 0 == not engaged in resync at all | |
7376 | * 2 == checking that there is no conflict with another sync | |
7377 | * 1 == like 2, but have yielded to allow conflicting resync to | |
7378 | * commense | |
7379 | * other == active in resync - this many blocks | |
7380 | * | |
7381 | * Before starting a resync we must have set curr_resync to | |
7382 | * 2, and then checked that every "conflicting" array has curr_resync | |
7383 | * less than ours. When we find one that is the same or higher | |
7384 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync | |
7385 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). | |
7386 | * This will mean we have to start checking from the beginning again. | |
7387 | * | |
7388 | */ | |
7389 | ||
7390 | do { | |
7391 | mddev->curr_resync = 2; | |
7392 | ||
7393 | try_again: | |
404e4b43 | 7394 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
1da177e4 | 7395 | goto skip; |
29ac4aa3 | 7396 | for_each_mddev(mddev2, tmp) { |
1da177e4 LT |
7397 | if (mddev2 == mddev) |
7398 | continue; | |
90b08710 BS |
7399 | if (!mddev->parallel_resync |
7400 | && mddev2->curr_resync | |
7401 | && match_mddev_units(mddev, mddev2)) { | |
1da177e4 LT |
7402 | DEFINE_WAIT(wq); |
7403 | if (mddev < mddev2 && mddev->curr_resync == 2) { | |
7404 | /* arbitrarily yield */ | |
7405 | mddev->curr_resync = 1; | |
7406 | wake_up(&resync_wait); | |
7407 | } | |
7408 | if (mddev > mddev2 && mddev->curr_resync == 1) | |
7409 | /* no need to wait here, we can wait the next | |
7410 | * time 'round when curr_resync == 2 | |
7411 | */ | |
7412 | continue; | |
9744197c N |
7413 | /* We need to wait 'interruptible' so as not to |
7414 | * contribute to the load average, and not to | |
7415 | * be caught by 'softlockup' | |
7416 | */ | |
7417 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); | |
c91abf5a | 7418 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
8712e553 | 7419 | mddev2->curr_resync >= mddev->curr_resync) { |
61df9d91 N |
7420 | printk(KERN_INFO "md: delaying %s of %s" |
7421 | " until %s has finished (they" | |
1da177e4 | 7422 | " share one or more physical units)\n", |
61df9d91 | 7423 | desc, mdname(mddev), mdname(mddev2)); |
1da177e4 | 7424 | mddev_put(mddev2); |
9744197c N |
7425 | if (signal_pending(current)) |
7426 | flush_signals(current); | |
1da177e4 LT |
7427 | schedule(); |
7428 | finish_wait(&resync_wait, &wq); | |
7429 | goto try_again; | |
7430 | } | |
7431 | finish_wait(&resync_wait, &wq); | |
7432 | } | |
7433 | } | |
7434 | } while (mddev->curr_resync < 2); | |
7435 | ||
5fd6c1dc | 7436 | j = 0; |
9d88883e | 7437 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
1da177e4 | 7438 | /* resync follows the size requested by the personality, |
57afd89f | 7439 | * which defaults to physical size, but can be virtual size |
1da177e4 LT |
7440 | */ |
7441 | max_sectors = mddev->resync_max_sectors; | |
7f7583d4 | 7442 | atomic64_set(&mddev->resync_mismatches, 0); |
5fd6c1dc | 7443 | /* we don't use the checkpoint if there's a bitmap */ |
5e96ee65 NB |
7444 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
7445 | j = mddev->resync_min; | |
7446 | else if (!mddev->bitmap) | |
5fd6c1dc | 7447 | j = mddev->recovery_cp; |
5e96ee65 | 7448 | |
ccfcc3c1 | 7449 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
c804cdec | 7450 | max_sectors = mddev->resync_max_sectors; |
5fd6c1dc | 7451 | else { |
1da177e4 | 7452 | /* recovery follows the physical size of devices */ |
58c0fed4 | 7453 | max_sectors = mddev->dev_sectors; |
5fd6c1dc | 7454 | j = MaxSector; |
4e59ca7d | 7455 | rcu_read_lock(); |
dafb20fa | 7456 | rdev_for_each_rcu(rdev, mddev) |
5fd6c1dc N |
7457 | if (rdev->raid_disk >= 0 && |
7458 | !test_bit(Faulty, &rdev->flags) && | |
7459 | !test_bit(In_sync, &rdev->flags) && | |
7460 | rdev->recovery_offset < j) | |
7461 | j = rdev->recovery_offset; | |
4e59ca7d | 7462 | rcu_read_unlock(); |
133d4527 N |
7463 | |
7464 | /* If there is a bitmap, we need to make sure all | |
7465 | * writes that started before we added a spare | |
7466 | * complete before we start doing a recovery. | |
7467 | * Otherwise the write might complete and (via | |
7468 | * bitmap_endwrite) set a bit in the bitmap after the | |
7469 | * recovery has checked that bit and skipped that | |
7470 | * region. | |
7471 | */ | |
7472 | if (mddev->bitmap) { | |
7473 | mddev->pers->quiesce(mddev, 1); | |
7474 | mddev->pers->quiesce(mddev, 0); | |
7475 | } | |
5fd6c1dc | 7476 | } |
1da177e4 | 7477 | |
61df9d91 N |
7478 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
7479 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" | |
7480 | " %d KB/sec/disk.\n", speed_min(mddev)); | |
338cec32 | 7481 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
61df9d91 N |
7482 | "(but not more than %d KB/sec) for %s.\n", |
7483 | speed_max(mddev), desc); | |
1da177e4 | 7484 | |
eea1bf38 | 7485 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
5fd6c1dc | 7486 | |
57afd89f | 7487 | io_sectors = 0; |
1da177e4 LT |
7488 | for (m = 0; m < SYNC_MARKS; m++) { |
7489 | mark[m] = jiffies; | |
57afd89f | 7490 | mark_cnt[m] = io_sectors; |
1da177e4 LT |
7491 | } |
7492 | last_mark = 0; | |
7493 | mddev->resync_mark = mark[last_mark]; | |
7494 | mddev->resync_mark_cnt = mark_cnt[last_mark]; | |
7495 | ||
7496 | /* | |
7497 | * Tune reconstruction: | |
7498 | */ | |
7499 | window = 32*(PAGE_SIZE/512); | |
ac42450c JB |
7500 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
7501 | window/2, (unsigned long long)max_sectors/2); | |
1da177e4 LT |
7502 | |
7503 | atomic_set(&mddev->recovery_active, 0); | |
1da177e4 LT |
7504 | last_check = 0; |
7505 | ||
7506 | if (j>2) { | |
c91abf5a | 7507 | printk(KERN_INFO |
61df9d91 N |
7508 | "md: resuming %s of %s from checkpoint.\n", |
7509 | desc, mdname(mddev)); | |
1da177e4 | 7510 | mddev->curr_resync = j; |
72f36d59 N |
7511 | } else |
7512 | mddev->curr_resync = 3; /* no longer delayed */ | |
75d3da43 | 7513 | mddev->curr_resync_completed = j; |
72f36d59 N |
7514 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
7515 | md_new_event(mddev); | |
54f89341 | 7516 | update_time = jiffies; |
1da177e4 | 7517 | |
7c2c57c9 | 7518 | blk_start_plug(&plug); |
1da177e4 | 7519 | while (j < max_sectors) { |
57afd89f | 7520 | sector_t sectors; |
1da177e4 | 7521 | |
57afd89f | 7522 | skipped = 0; |
97e4f42d | 7523 | |
7a91ee1f N |
7524 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
7525 | ((mddev->curr_resync > mddev->curr_resync_completed && | |
7526 | (mddev->curr_resync - mddev->curr_resync_completed) | |
7527 | > (max_sectors >> 4)) || | |
54f89341 | 7528 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
7a91ee1f N |
7529 | (j - mddev->curr_resync_completed)*2 |
7530 | >= mddev->resync_max - mddev->curr_resync_completed | |
7531 | )) { | |
97e4f42d | 7532 | /* time to update curr_resync_completed */ |
97e4f42d N |
7533 | wait_event(mddev->recovery_wait, |
7534 | atomic_read(&mddev->recovery_active) == 0); | |
75d3da43 | 7535 | mddev->curr_resync_completed = j; |
35d78c66 | 7536 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
7537 | j > mddev->recovery_cp) | |
7538 | mddev->recovery_cp = j; | |
54f89341 | 7539 | update_time = jiffies; |
070dc6dd | 7540 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
acb180b0 | 7541 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
97e4f42d | 7542 | } |
acb180b0 | 7543 | |
c91abf5a N |
7544 | while (j >= mddev->resync_max && |
7545 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
e62e58a5 N |
7546 | /* As this condition is controlled by user-space, |
7547 | * we can block indefinitely, so use '_interruptible' | |
7548 | * to avoid triggering warnings. | |
7549 | */ | |
7550 | flush_signals(current); /* just in case */ | |
7551 | wait_event_interruptible(mddev->recovery_wait, | |
7552 | mddev->resync_max > j | |
c91abf5a N |
7553 | || test_bit(MD_RECOVERY_INTR, |
7554 | &mddev->recovery)); | |
e62e58a5 | 7555 | } |
acb180b0 | 7556 | |
c91abf5a N |
7557 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
7558 | break; | |
acb180b0 | 7559 | |
57afd89f | 7560 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
c6207277 | 7561 | currspeed < speed_min(mddev)); |
57afd89f | 7562 | if (sectors == 0) { |
dfc70645 | 7563 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
c91abf5a | 7564 | break; |
1da177e4 | 7565 | } |
57afd89f N |
7566 | |
7567 | if (!skipped) { /* actual IO requested */ | |
7568 | io_sectors += sectors; | |
7569 | atomic_add(sectors, &mddev->recovery_active); | |
7570 | } | |
7571 | ||
e875ecea N |
7572 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
7573 | break; | |
7574 | ||
1da177e4 | 7575 | j += sectors; |
72f36d59 N |
7576 | if (j > 2) |
7577 | mddev->curr_resync = j; | |
ff4e8d9a | 7578 | mddev->curr_mark_cnt = io_sectors; |
d7603b7e | 7579 | if (last_check == 0) |
e875ecea | 7580 | /* this is the earliest that rebuild will be |
d7603b7e N |
7581 | * visible in /proc/mdstat |
7582 | */ | |
7583 | md_new_event(mddev); | |
57afd89f N |
7584 | |
7585 | if (last_check + window > io_sectors || j == max_sectors) | |
1da177e4 LT |
7586 | continue; |
7587 | ||
57afd89f | 7588 | last_check = io_sectors; |
1da177e4 LT |
7589 | repeat: |
7590 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { | |
7591 | /* step marks */ | |
7592 | int next = (last_mark+1) % SYNC_MARKS; | |
7593 | ||
7594 | mddev->resync_mark = mark[next]; | |
7595 | mddev->resync_mark_cnt = mark_cnt[next]; | |
7596 | mark[next] = jiffies; | |
57afd89f | 7597 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
1da177e4 LT |
7598 | last_mark = next; |
7599 | } | |
7600 | ||
c91abf5a N |
7601 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
7602 | break; | |
1da177e4 LT |
7603 | |
7604 | /* | |
7605 | * this loop exits only if either when we are slower than | |
7606 | * the 'hard' speed limit, or the system was IO-idle for | |
7607 | * a jiffy. | |
7608 | * the system might be non-idle CPU-wise, but we only care | |
7609 | * about not overloading the IO subsystem. (things like an | |
7610 | * e2fsck being done on the RAID array should execute fast) | |
7611 | */ | |
1da177e4 LT |
7612 | cond_resched(); |
7613 | ||
ac7e50a3 XN |
7614 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
7615 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 | |
57afd89f | 7616 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
1da177e4 | 7617 | |
88202a0c N |
7618 | if (currspeed > speed_min(mddev)) { |
7619 | if ((currspeed > speed_max(mddev)) || | |
eea1bf38 | 7620 | !is_mddev_idle(mddev, 0)) { |
c0e48521 | 7621 | msleep(500); |
1da177e4 LT |
7622 | goto repeat; |
7623 | } | |
7624 | } | |
7625 | } | |
c91abf5a N |
7626 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
7627 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) | |
7628 | ? "interrupted" : "done"); | |
1da177e4 LT |
7629 | /* |
7630 | * this also signals 'finished resyncing' to md_stop | |
7631 | */ | |
7c2c57c9 | 7632 | blk_finish_plug(&plug); |
1da177e4 LT |
7633 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
7634 | ||
7635 | /* tell personality that we are finished */ | |
57afd89f | 7636 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
1da177e4 | 7637 | |
dfc70645 | 7638 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
5fd6c1dc N |
7639 | mddev->curr_resync > 2) { |
7640 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | |
7641 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
7642 | if (mddev->curr_resync >= mddev->recovery_cp) { | |
7643 | printk(KERN_INFO | |
61df9d91 N |
7644 | "md: checkpointing %s of %s.\n", |
7645 | desc, mdname(mddev)); | |
0a19caab | 7646 | if (test_bit(MD_RECOVERY_ERROR, |
7647 | &mddev->recovery)) | |
7648 | mddev->recovery_cp = | |
7649 | mddev->curr_resync_completed; | |
7650 | else | |
7651 | mddev->recovery_cp = | |
7652 | mddev->curr_resync; | |
5fd6c1dc N |
7653 | } |
7654 | } else | |
7655 | mddev->recovery_cp = MaxSector; | |
7656 | } else { | |
7657 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) | |
7658 | mddev->curr_resync = MaxSector; | |
4e59ca7d | 7659 | rcu_read_lock(); |
dafb20fa | 7660 | rdev_for_each_rcu(rdev, mddev) |
5fd6c1dc | 7661 | if (rdev->raid_disk >= 0 && |
70fffd0b | 7662 | mddev->delta_disks >= 0 && |
5fd6c1dc N |
7663 | !test_bit(Faulty, &rdev->flags) && |
7664 | !test_bit(In_sync, &rdev->flags) && | |
7665 | rdev->recovery_offset < mddev->curr_resync) | |
7666 | rdev->recovery_offset = mddev->curr_resync; | |
4e59ca7d | 7667 | rcu_read_unlock(); |
5fd6c1dc | 7668 | } |
1da177e4 | 7669 | } |
db91ff55 | 7670 | skip: |
17571284 | 7671 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
1da177e4 | 7672 | |
c07b70ad N |
7673 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
7674 | /* We completed so min/max setting can be forgotten if used. */ | |
7675 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) | |
7676 | mddev->resync_min = 0; | |
7677 | mddev->resync_max = MaxSector; | |
7678 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) | |
7679 | mddev->resync_min = mddev->curr_resync_completed; | |
1da177e4 LT |
7680 | mddev->curr_resync = 0; |
7681 | wake_up(&resync_wait); | |
7682 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); | |
7683 | md_wakeup_thread(mddev->thread); | |
c6207277 | 7684 | return; |
1da177e4 | 7685 | } |
29269553 | 7686 | EXPORT_SYMBOL_GPL(md_do_sync); |
1da177e4 | 7687 | |
746d3207 N |
7688 | static int remove_and_add_spares(struct mddev *mddev, |
7689 | struct md_rdev *this) | |
b4c4c7b8 | 7690 | { |
3cb03002 | 7691 | struct md_rdev *rdev; |
b4c4c7b8 | 7692 | int spares = 0; |
f2a371c5 | 7693 | int removed = 0; |
b4c4c7b8 | 7694 | |
dafb20fa | 7695 | rdev_for_each(rdev, mddev) |
746d3207 N |
7696 | if ((this == NULL || rdev == this) && |
7697 | rdev->raid_disk >= 0 && | |
6bfe0b49 | 7698 | !test_bit(Blocked, &rdev->flags) && |
b4c4c7b8 N |
7699 | (test_bit(Faulty, &rdev->flags) || |
7700 | ! test_bit(In_sync, &rdev->flags)) && | |
7701 | atomic_read(&rdev->nr_pending)==0) { | |
7702 | if (mddev->pers->hot_remove_disk( | |
b8321b68 | 7703 | mddev, rdev) == 0) { |
36fad858 | 7704 | sysfs_unlink_rdev(mddev, rdev); |
b4c4c7b8 | 7705 | rdev->raid_disk = -1; |
f2a371c5 | 7706 | removed++; |
b4c4c7b8 N |
7707 | } |
7708 | } | |
90584fc9 JB |
7709 | if (removed && mddev->kobj.sd) |
7710 | sysfs_notify(&mddev->kobj, NULL, "degraded"); | |
b4c4c7b8 | 7711 | |
746d3207 N |
7712 | if (this) |
7713 | goto no_add; | |
7714 | ||
dafb20fa | 7715 | rdev_for_each(rdev, mddev) { |
7bfec5f3 N |
7716 | if (rdev->raid_disk >= 0 && |
7717 | !test_bit(In_sync, &rdev->flags) && | |
7718 | !test_bit(Faulty, &rdev->flags)) | |
7719 | spares++; | |
7ceb17e8 N |
7720 | if (rdev->raid_disk >= 0) |
7721 | continue; | |
7722 | if (test_bit(Faulty, &rdev->flags)) | |
7723 | continue; | |
7724 | if (mddev->ro && | |
8313b8e5 N |
7725 | ! (rdev->saved_raid_disk >= 0 && |
7726 | !test_bit(Bitmap_sync, &rdev->flags))) | |
7ceb17e8 N |
7727 | continue; |
7728 | ||
7eb41885 N |
7729 | if (rdev->saved_raid_disk < 0) |
7730 | rdev->recovery_offset = 0; | |
7ceb17e8 N |
7731 | if (mddev->pers-> |
7732 | hot_add_disk(mddev, rdev) == 0) { | |
7733 | if (sysfs_link_rdev(mddev, rdev)) | |
7734 | /* failure here is OK */; | |
7735 | spares++; | |
7736 | md_new_event(mddev); | |
7737 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
dfc70645 | 7738 | } |
b4c4c7b8 | 7739 | } |
746d3207 | 7740 | no_add: |
6dafab6b N |
7741 | if (removed) |
7742 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
b4c4c7b8 N |
7743 | return spares; |
7744 | } | |
7ebc0be7 | 7745 | |
ac05f256 N |
7746 | static void md_start_sync(struct work_struct *ws) |
7747 | { | |
7748 | struct mddev *mddev = container_of(ws, struct mddev, del_work); | |
7749 | ||
7750 | mddev->sync_thread = md_register_thread(md_do_sync, | |
7751 | mddev, | |
7752 | "resync"); | |
7753 | if (!mddev->sync_thread) { | |
7754 | printk(KERN_ERR "%s: could not start resync" | |
7755 | " thread...\n", | |
7756 | mdname(mddev)); | |
7757 | /* leave the spares where they are, it shouldn't hurt */ | |
7758 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
7759 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
7760 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); | |
7761 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
7762 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
7763 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, | |
7764 | &mddev->recovery)) | |
7765 | if (mddev->sysfs_action) | |
7766 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
7767 | } else | |
7768 | md_wakeup_thread(mddev->sync_thread); | |
7769 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
7770 | md_new_event(mddev); | |
7771 | } | |
7772 | ||
1da177e4 LT |
7773 | /* |
7774 | * This routine is regularly called by all per-raid-array threads to | |
7775 | * deal with generic issues like resync and super-block update. | |
7776 | * Raid personalities that don't have a thread (linear/raid0) do not | |
7777 | * need this as they never do any recovery or update the superblock. | |
7778 | * | |
7779 | * It does not do any resync itself, but rather "forks" off other threads | |
7780 | * to do that as needed. | |
7781 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in | |
7782 | * "->recovery" and create a thread at ->sync_thread. | |
dfc70645 | 7783 | * When the thread finishes it sets MD_RECOVERY_DONE |
1da177e4 LT |
7784 | * and wakeups up this thread which will reap the thread and finish up. |
7785 | * This thread also removes any faulty devices (with nr_pending == 0). | |
7786 | * | |
7787 | * The overall approach is: | |
7788 | * 1/ if the superblock needs updating, update it. | |
7789 | * 2/ If a recovery thread is running, don't do anything else. | |
7790 | * 3/ If recovery has finished, clean up, possibly marking spares active. | |
7791 | * 4/ If there are any faulty devices, remove them. | |
7792 | * 5/ If array is degraded, try to add spares devices | |
7793 | * 6/ If array has spares or is not in-sync, start a resync thread. | |
7794 | */ | |
fd01b88c | 7795 | void md_check_recovery(struct mddev *mddev) |
1da177e4 | 7796 | { |
68866e42 JB |
7797 | if (mddev->suspended) |
7798 | return; | |
7799 | ||
5f40402d | 7800 | if (mddev->bitmap) |
aa5cbd10 | 7801 | bitmap_daemon_work(mddev); |
1da177e4 | 7802 | |
fca4d848 | 7803 | if (signal_pending(current)) { |
31a59e34 | 7804 | if (mddev->pers->sync_request && !mddev->external) { |
fca4d848 N |
7805 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
7806 | mdname(mddev)); | |
7807 | mddev->safemode = 2; | |
7808 | } | |
7809 | flush_signals(current); | |
7810 | } | |
7811 | ||
c89a8eee N |
7812 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
7813 | return; | |
1da177e4 | 7814 | if ( ! ( |
142d44c3 | 7815 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
1da177e4 | 7816 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
fca4d848 | 7817 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
31a59e34 | 7818 | (mddev->external == 0 && mddev->safemode == 1) || |
fca4d848 N |
7819 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
7820 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) | |
1da177e4 LT |
7821 | )) |
7822 | return; | |
fca4d848 | 7823 | |
df5b89b3 | 7824 | if (mddev_trylock(mddev)) { |
b4c4c7b8 | 7825 | int spares = 0; |
fca4d848 | 7826 | |
c89a8eee | 7827 | if (mddev->ro) { |
7ceb17e8 N |
7828 | /* On a read-only array we can: |
7829 | * - remove failed devices | |
7830 | * - add already-in_sync devices if the array itself | |
7831 | * is in-sync. | |
7832 | * As we only add devices that are already in-sync, | |
7833 | * we can activate the spares immediately. | |
c89a8eee | 7834 | */ |
7ceb17e8 | 7835 | remove_and_add_spares(mddev, NULL); |
8313b8e5 N |
7836 | /* There is no thread, but we need to call |
7837 | * ->spare_active and clear saved_raid_disk | |
7838 | */ | |
2ac295a5 | 7839 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
8313b8e5 N |
7840 | md_reap_sync_thread(mddev); |
7841 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
c89a8eee N |
7842 | goto unlock; |
7843 | } | |
7844 | ||
31a59e34 | 7845 | if (!mddev->external) { |
0fd62b86 | 7846 | int did_change = 0; |
31a59e34 N |
7847 | spin_lock_irq(&mddev->write_lock); |
7848 | if (mddev->safemode && | |
7849 | !atomic_read(&mddev->writes_pending) && | |
7850 | !mddev->in_sync && | |
7851 | mddev->recovery_cp == MaxSector) { | |
7852 | mddev->in_sync = 1; | |
0fd62b86 | 7853 | did_change = 1; |
070dc6dd | 7854 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
31a59e34 N |
7855 | } |
7856 | if (mddev->safemode == 1) | |
7857 | mddev->safemode = 0; | |
7858 | spin_unlock_irq(&mddev->write_lock); | |
0fd62b86 | 7859 | if (did_change) |
00bcb4ac | 7860 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
fca4d848 | 7861 | } |
fca4d848 | 7862 | |
7a0a5355 | 7863 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
850b2b42 | 7864 | md_update_sb(mddev, 0); |
06d91a5f | 7865 | |
1da177e4 LT |
7866 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
7867 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { | |
7868 | /* resync/recovery still happening */ | |
7869 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
7870 | goto unlock; | |
7871 | } | |
7872 | if (mddev->sync_thread) { | |
a91d5ac0 | 7873 | md_reap_sync_thread(mddev); |
1da177e4 LT |
7874 | goto unlock; |
7875 | } | |
72a23c21 NB |
7876 | /* Set RUNNING before clearing NEEDED to avoid |
7877 | * any transients in the value of "sync_action". | |
7878 | */ | |
72f36d59 | 7879 | mddev->curr_resync_completed = 0; |
72a23c21 | 7880 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
24dd469d N |
7881 | /* Clear some bits that don't mean anything, but |
7882 | * might be left set | |
7883 | */ | |
24dd469d N |
7884 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
7885 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); | |
1da177e4 | 7886 | |
ed209584 N |
7887 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
7888 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) | |
ac05f256 | 7889 | goto not_running; |
1da177e4 LT |
7890 | /* no recovery is running. |
7891 | * remove any failed drives, then | |
7892 | * add spares if possible. | |
72f36d59 | 7893 | * Spares are also removed and re-added, to allow |
1da177e4 LT |
7894 | * the personality to fail the re-add. |
7895 | */ | |
1da177e4 | 7896 | |
b4c4c7b8 | 7897 | if (mddev->reshape_position != MaxSector) { |
50ac168a N |
7898 | if (mddev->pers->check_reshape == NULL || |
7899 | mddev->pers->check_reshape(mddev) != 0) | |
b4c4c7b8 | 7900 | /* Cannot proceed */ |
ac05f256 | 7901 | goto not_running; |
b4c4c7b8 | 7902 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
72a23c21 | 7903 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
746d3207 | 7904 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
24dd469d N |
7905 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
7906 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
56ac36d7 | 7907 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
72a23c21 | 7908 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
24dd469d N |
7909 | } else if (mddev->recovery_cp < MaxSector) { |
7910 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
72a23c21 | 7911 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
24dd469d N |
7912 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
7913 | /* nothing to be done ... */ | |
ac05f256 | 7914 | goto not_running; |
24dd469d | 7915 | |
1da177e4 | 7916 | if (mddev->pers->sync_request) { |
ef99bf48 | 7917 | if (spares) { |
a654b9d8 N |
7918 | /* We are adding a device or devices to an array |
7919 | * which has the bitmap stored on all devices. | |
7920 | * So make sure all bitmap pages get written | |
7921 | */ | |
7922 | bitmap_write_all(mddev->bitmap); | |
7923 | } | |
ac05f256 N |
7924 | INIT_WORK(&mddev->del_work, md_start_sync); |
7925 | queue_work(md_misc_wq, &mddev->del_work); | |
7926 | goto unlock; | |
1da177e4 | 7927 | } |
ac05f256 | 7928 | not_running: |
72a23c21 NB |
7929 | if (!mddev->sync_thread) { |
7930 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
7931 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, | |
7932 | &mddev->recovery)) | |
0c3573f1 | 7933 | if (mddev->sysfs_action) |
00bcb4ac | 7934 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
72a23c21 | 7935 | } |
ac05f256 N |
7936 | unlock: |
7937 | wake_up(&mddev->sb_wait); | |
1da177e4 LT |
7938 | mddev_unlock(mddev); |
7939 | } | |
7940 | } | |
7941 | ||
a91d5ac0 JB |
7942 | void md_reap_sync_thread(struct mddev *mddev) |
7943 | { | |
7944 | struct md_rdev *rdev; | |
7945 | ||
7946 | /* resync has finished, collect result */ | |
7947 | md_unregister_thread(&mddev->sync_thread); | |
30b8feb7 | 7948 | wake_up(&resync_wait); |
a91d5ac0 JB |
7949 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
7950 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { | |
7951 | /* success...*/ | |
7952 | /* activate any spares */ | |
7953 | if (mddev->pers->spare_active(mddev)) { | |
7954 | sysfs_notify(&mddev->kobj, NULL, | |
7955 | "degraded"); | |
7956 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
7957 | } | |
7958 | } | |
7959 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && | |
7960 | mddev->pers->finish_reshape) | |
7961 | mddev->pers->finish_reshape(mddev); | |
7962 | ||
7963 | /* If array is no-longer degraded, then any saved_raid_disk | |
f466722c | 7964 | * information must be scrapped. |
a91d5ac0 | 7965 | */ |
f466722c N |
7966 | if (!mddev->degraded) |
7967 | rdev_for_each(rdev, mddev) | |
a91d5ac0 JB |
7968 | rdev->saved_raid_disk = -1; |
7969 | ||
7970 | md_update_sb(mddev, 1); | |
7971 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
7972 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
7973 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
7974 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); | |
7975 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
7976 | /* flag recovery needed just to double check */ | |
7977 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
7978 | sysfs_notify_dirent_safe(mddev->sysfs_action); | |
7979 | md_new_event(mddev); | |
7980 | if (mddev->event_work.func) | |
7981 | queue_work(md_misc_wq, &mddev->event_work); | |
7982 | } | |
7983 | ||
fd01b88c | 7984 | void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev) |
6bfe0b49 | 7985 | { |
00bcb4ac | 7986 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
6bfe0b49 | 7987 | wait_event_timeout(rdev->blocked_wait, |
de393cde N |
7988 | !test_bit(Blocked, &rdev->flags) && |
7989 | !test_bit(BlockedBadBlocks, &rdev->flags), | |
6bfe0b49 DW |
7990 | msecs_to_jiffies(5000)); |
7991 | rdev_dec_pending(rdev, mddev); | |
7992 | } | |
7993 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); | |
7994 | ||
c6563a8c N |
7995 | void md_finish_reshape(struct mddev *mddev) |
7996 | { | |
7997 | /* called be personality module when reshape completes. */ | |
7998 | struct md_rdev *rdev; | |
7999 | ||
8000 | rdev_for_each(rdev, mddev) { | |
8001 | if (rdev->data_offset > rdev->new_data_offset) | |
8002 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; | |
8003 | else | |
8004 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; | |
8005 | rdev->data_offset = rdev->new_data_offset; | |
8006 | } | |
8007 | } | |
8008 | EXPORT_SYMBOL(md_finish_reshape); | |
2230dfe4 N |
8009 | |
8010 | /* Bad block management. | |
8011 | * We can record which blocks on each device are 'bad' and so just | |
8012 | * fail those blocks, or that stripe, rather than the whole device. | |
8013 | * Entries in the bad-block table are 64bits wide. This comprises: | |
8014 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 | |
8015 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) | |
8016 | * A 'shift' can be set so that larger blocks are tracked and | |
8017 | * consequently larger devices can be covered. | |
8018 | * 'Acknowledged' flag - 1 bit. - the most significant bit. | |
8019 | * | |
8020 | * Locking of the bad-block table uses a seqlock so md_is_badblock | |
8021 | * might need to retry if it is very unlucky. | |
8022 | * We will sometimes want to check for bad blocks in a bi_end_io function, | |
8023 | * so we use the write_seqlock_irq variant. | |
8024 | * | |
8025 | * When looking for a bad block we specify a range and want to | |
8026 | * know if any block in the range is bad. So we binary-search | |
8027 | * to the last range that starts at-or-before the given endpoint, | |
8028 | * (or "before the sector after the target range") | |
8029 | * then see if it ends after the given start. | |
8030 | * We return | |
8031 | * 0 if there are no known bad blocks in the range | |
8032 | * 1 if there are known bad block which are all acknowledged | |
8033 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. | |
8034 | * plus the start/length of the first bad section we overlap. | |
8035 | */ | |
8036 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, | |
8037 | sector_t *first_bad, int *bad_sectors) | |
8038 | { | |
8039 | int hi; | |
ab05613a | 8040 | int lo; |
2230dfe4 | 8041 | u64 *p = bb->page; |
ab05613a | 8042 | int rv; |
2230dfe4 N |
8043 | sector_t target = s + sectors; |
8044 | unsigned seq; | |
8045 | ||
8046 | if (bb->shift > 0) { | |
8047 | /* round the start down, and the end up */ | |
8048 | s >>= bb->shift; | |
8049 | target += (1<<bb->shift) - 1; | |
8050 | target >>= bb->shift; | |
8051 | sectors = target - s; | |
8052 | } | |
8053 | /* 'target' is now the first block after the bad range */ | |
8054 | ||
8055 | retry: | |
8056 | seq = read_seqbegin(&bb->lock); | |
ab05613a | 8057 | lo = 0; |
8058 | rv = 0; | |
2230dfe4 N |
8059 | hi = bb->count; |
8060 | ||
8061 | /* Binary search between lo and hi for 'target' | |
8062 | * i.e. for the last range that starts before 'target' | |
8063 | */ | |
8064 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' | |
8065 | * are known not to be the last range before target. | |
8066 | * VARIANT: hi-lo is the number of possible | |
8067 | * ranges, and decreases until it reaches 1 | |
8068 | */ | |
8069 | while (hi - lo > 1) { | |
8070 | int mid = (lo + hi) / 2; | |
8071 | sector_t a = BB_OFFSET(p[mid]); | |
8072 | if (a < target) | |
8073 | /* This could still be the one, earlier ranges | |
8074 | * could not. */ | |
8075 | lo = mid; | |
8076 | else | |
8077 | /* This and later ranges are definitely out. */ | |
8078 | hi = mid; | |
8079 | } | |
8080 | /* 'lo' might be the last that started before target, but 'hi' isn't */ | |
8081 | if (hi > lo) { | |
8082 | /* need to check all range that end after 's' to see if | |
8083 | * any are unacknowledged. | |
8084 | */ | |
8085 | while (lo >= 0 && | |
8086 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { | |
8087 | if (BB_OFFSET(p[lo]) < target) { | |
8088 | /* starts before the end, and finishes after | |
8089 | * the start, so they must overlap | |
8090 | */ | |
8091 | if (rv != -1 && BB_ACK(p[lo])) | |
8092 | rv = 1; | |
8093 | else | |
8094 | rv = -1; | |
8095 | *first_bad = BB_OFFSET(p[lo]); | |
8096 | *bad_sectors = BB_LEN(p[lo]); | |
8097 | } | |
8098 | lo--; | |
8099 | } | |
8100 | } | |
8101 | ||
8102 | if (read_seqretry(&bb->lock, seq)) | |
8103 | goto retry; | |
8104 | ||
8105 | return rv; | |
8106 | } | |
8107 | EXPORT_SYMBOL_GPL(md_is_badblock); | |
8108 | ||
8109 | /* | |
8110 | * Add a range of bad blocks to the table. | |
8111 | * This might extend the table, or might contract it | |
8112 | * if two adjacent ranges can be merged. | |
8113 | * We binary-search to find the 'insertion' point, then | |
8114 | * decide how best to handle it. | |
8115 | */ | |
8116 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, | |
8117 | int acknowledged) | |
8118 | { | |
8119 | u64 *p; | |
8120 | int lo, hi; | |
8121 | int rv = 1; | |
905b0297 | 8122 | unsigned long flags; |
2230dfe4 N |
8123 | |
8124 | if (bb->shift < 0) | |
8125 | /* badblocks are disabled */ | |
8126 | return 0; | |
8127 | ||
8128 | if (bb->shift) { | |
8129 | /* round the start down, and the end up */ | |
8130 | sector_t next = s + sectors; | |
8131 | s >>= bb->shift; | |
8132 | next += (1<<bb->shift) - 1; | |
8133 | next >>= bb->shift; | |
8134 | sectors = next - s; | |
8135 | } | |
8136 | ||
905b0297 | 8137 | write_seqlock_irqsave(&bb->lock, flags); |
2230dfe4 N |
8138 | |
8139 | p = bb->page; | |
8140 | lo = 0; | |
8141 | hi = bb->count; | |
8142 | /* Find the last range that starts at-or-before 's' */ | |
8143 | while (hi - lo > 1) { | |
8144 | int mid = (lo + hi) / 2; | |
8145 | sector_t a = BB_OFFSET(p[mid]); | |
8146 | if (a <= s) | |
8147 | lo = mid; | |
8148 | else | |
8149 | hi = mid; | |
8150 | } | |
8151 | if (hi > lo && BB_OFFSET(p[lo]) > s) | |
8152 | hi = lo; | |
8153 | ||
8154 | if (hi > lo) { | |
8155 | /* we found a range that might merge with the start | |
8156 | * of our new range | |
8157 | */ | |
8158 | sector_t a = BB_OFFSET(p[lo]); | |
8159 | sector_t e = a + BB_LEN(p[lo]); | |
8160 | int ack = BB_ACK(p[lo]); | |
8161 | if (e >= s) { | |
8162 | /* Yes, we can merge with a previous range */ | |
8163 | if (s == a && s + sectors >= e) | |
8164 | /* new range covers old */ | |
8165 | ack = acknowledged; | |
8166 | else | |
8167 | ack = ack && acknowledged; | |
8168 | ||
8169 | if (e < s + sectors) | |
8170 | e = s + sectors; | |
8171 | if (e - a <= BB_MAX_LEN) { | |
8172 | p[lo] = BB_MAKE(a, e-a, ack); | |
8173 | s = e; | |
8174 | } else { | |
8175 | /* does not all fit in one range, | |
8176 | * make p[lo] maximal | |
8177 | */ | |
8178 | if (BB_LEN(p[lo]) != BB_MAX_LEN) | |
8179 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); | |
8180 | s = a + BB_MAX_LEN; | |
8181 | } | |
8182 | sectors = e - s; | |
8183 | } | |
8184 | } | |
8185 | if (sectors && hi < bb->count) { | |
8186 | /* 'hi' points to the first range that starts after 's'. | |
8187 | * Maybe we can merge with the start of that range */ | |
8188 | sector_t a = BB_OFFSET(p[hi]); | |
8189 | sector_t e = a + BB_LEN(p[hi]); | |
8190 | int ack = BB_ACK(p[hi]); | |
8191 | if (a <= s + sectors) { | |
8192 | /* merging is possible */ | |
8193 | if (e <= s + sectors) { | |
8194 | /* full overlap */ | |
8195 | e = s + sectors; | |
8196 | ack = acknowledged; | |
8197 | } else | |
8198 | ack = ack && acknowledged; | |
8199 | ||
8200 | a = s; | |
8201 | if (e - a <= BB_MAX_LEN) { | |
8202 | p[hi] = BB_MAKE(a, e-a, ack); | |
8203 | s = e; | |
8204 | } else { | |
8205 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); | |
8206 | s = a + BB_MAX_LEN; | |
8207 | } | |
8208 | sectors = e - s; | |
8209 | lo = hi; | |
8210 | hi++; | |
8211 | } | |
8212 | } | |
8213 | if (sectors == 0 && hi < bb->count) { | |
8214 | /* we might be able to combine lo and hi */ | |
8215 | /* Note: 's' is at the end of 'lo' */ | |
8216 | sector_t a = BB_OFFSET(p[hi]); | |
8217 | int lolen = BB_LEN(p[lo]); | |
8218 | int hilen = BB_LEN(p[hi]); | |
8219 | int newlen = lolen + hilen - (s - a); | |
8220 | if (s >= a && newlen < BB_MAX_LEN) { | |
8221 | /* yes, we can combine them */ | |
8222 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); | |
8223 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); | |
8224 | memmove(p + hi, p + hi + 1, | |
8225 | (bb->count - hi - 1) * 8); | |
8226 | bb->count--; | |
8227 | } | |
8228 | } | |
8229 | while (sectors) { | |
8230 | /* didn't merge (it all). | |
8231 | * Need to add a range just before 'hi' */ | |
8232 | if (bb->count >= MD_MAX_BADBLOCKS) { | |
8233 | /* No room for more */ | |
8234 | rv = 0; | |
8235 | break; | |
8236 | } else { | |
8237 | int this_sectors = sectors; | |
8238 | memmove(p + hi + 1, p + hi, | |
8239 | (bb->count - hi) * 8); | |
8240 | bb->count++; | |
8241 | ||
8242 | if (this_sectors > BB_MAX_LEN) | |
8243 | this_sectors = BB_MAX_LEN; | |
8244 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); | |
8245 | sectors -= this_sectors; | |
8246 | s += this_sectors; | |
8247 | } | |
8248 | } | |
8249 | ||
8250 | bb->changed = 1; | |
de393cde N |
8251 | if (!acknowledged) |
8252 | bb->unacked_exist = 1; | |
905b0297 | 8253 | write_sequnlock_irqrestore(&bb->lock, flags); |
2230dfe4 N |
8254 | |
8255 | return rv; | |
8256 | } | |
8257 | ||
3cb03002 | 8258 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
c6563a8c | 8259 | int is_new) |
2230dfe4 | 8260 | { |
c6563a8c N |
8261 | int rv; |
8262 | if (is_new) | |
8263 | s += rdev->new_data_offset; | |
8264 | else | |
8265 | s += rdev->data_offset; | |
8266 | rv = md_set_badblocks(&rdev->badblocks, | |
8267 | s, sectors, 0); | |
2230dfe4 N |
8268 | if (rv) { |
8269 | /* Make sure they get written out promptly */ | |
8bd2f0a0 | 8270 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
2230dfe4 N |
8271 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
8272 | md_wakeup_thread(rdev->mddev->thread); | |
8273 | } | |
8274 | return rv; | |
8275 | } | |
8276 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); | |
8277 | ||
8278 | /* | |
8279 | * Remove a range of bad blocks from the table. | |
8280 | * This may involve extending the table if we spilt a region, | |
8281 | * but it must not fail. So if the table becomes full, we just | |
8282 | * drop the remove request. | |
8283 | */ | |
8284 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) | |
8285 | { | |
8286 | u64 *p; | |
8287 | int lo, hi; | |
8288 | sector_t target = s + sectors; | |
8289 | int rv = 0; | |
8290 | ||
8291 | if (bb->shift > 0) { | |
8292 | /* When clearing we round the start up and the end down. | |
8293 | * This should not matter as the shift should align with | |
8294 | * the block size and no rounding should ever be needed. | |
8295 | * However it is better the think a block is bad when it | |
8296 | * isn't than to think a block is not bad when it is. | |
8297 | */ | |
8298 | s += (1<<bb->shift) - 1; | |
8299 | s >>= bb->shift; | |
8300 | target >>= bb->shift; | |
8301 | sectors = target - s; | |
8302 | } | |
8303 | ||
8304 | write_seqlock_irq(&bb->lock); | |
8305 | ||
8306 | p = bb->page; | |
8307 | lo = 0; | |
8308 | hi = bb->count; | |
8309 | /* Find the last range that starts before 'target' */ | |
8310 | while (hi - lo > 1) { | |
8311 | int mid = (lo + hi) / 2; | |
8312 | sector_t a = BB_OFFSET(p[mid]); | |
8313 | if (a < target) | |
8314 | lo = mid; | |
8315 | else | |
8316 | hi = mid; | |
8317 | } | |
8318 | if (hi > lo) { | |
8319 | /* p[lo] is the last range that could overlap the | |
8320 | * current range. Earlier ranges could also overlap, | |
8321 | * but only this one can overlap the end of the range. | |
8322 | */ | |
8323 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { | |
8324 | /* Partial overlap, leave the tail of this range */ | |
8325 | int ack = BB_ACK(p[lo]); | |
8326 | sector_t a = BB_OFFSET(p[lo]); | |
8327 | sector_t end = a + BB_LEN(p[lo]); | |
8328 | ||
8329 | if (a < s) { | |
8330 | /* we need to split this range */ | |
8331 | if (bb->count >= MD_MAX_BADBLOCKS) { | |
8b32bf5e | 8332 | rv = -ENOSPC; |
2230dfe4 N |
8333 | goto out; |
8334 | } | |
8335 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); | |
8336 | bb->count++; | |
8337 | p[lo] = BB_MAKE(a, s-a, ack); | |
8338 | lo++; | |
8339 | } | |
8340 | p[lo] = BB_MAKE(target, end - target, ack); | |
8341 | /* there is no longer an overlap */ | |
8342 | hi = lo; | |
8343 | lo--; | |
8344 | } | |
8345 | while (lo >= 0 && | |
8346 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { | |
8347 | /* This range does overlap */ | |
8348 | if (BB_OFFSET(p[lo]) < s) { | |
8349 | /* Keep the early parts of this range. */ | |
8350 | int ack = BB_ACK(p[lo]); | |
8351 | sector_t start = BB_OFFSET(p[lo]); | |
8352 | p[lo] = BB_MAKE(start, s - start, ack); | |
8353 | /* now low doesn't overlap, so.. */ | |
8354 | break; | |
8355 | } | |
8356 | lo--; | |
8357 | } | |
8358 | /* 'lo' is strictly before, 'hi' is strictly after, | |
8359 | * anything between needs to be discarded | |
8360 | */ | |
8361 | if (hi - lo > 1) { | |
8362 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); | |
8363 | bb->count -= (hi - lo - 1); | |
8364 | } | |
8365 | } | |
8366 | ||
8367 | bb->changed = 1; | |
8368 | out: | |
8369 | write_sequnlock_irq(&bb->lock); | |
8370 | return rv; | |
8371 | } | |
8372 | ||
c6563a8c N |
8373 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
8374 | int is_new) | |
2230dfe4 | 8375 | { |
c6563a8c N |
8376 | if (is_new) |
8377 | s += rdev->new_data_offset; | |
8378 | else | |
8379 | s += rdev->data_offset; | |
2230dfe4 | 8380 | return md_clear_badblocks(&rdev->badblocks, |
c6563a8c | 8381 | s, sectors); |
2230dfe4 N |
8382 | } |
8383 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); | |
8384 | ||
8385 | /* | |
8386 | * Acknowledge all bad blocks in a list. | |
8387 | * This only succeeds if ->changed is clear. It is used by | |
8388 | * in-kernel metadata updates | |
8389 | */ | |
8390 | void md_ack_all_badblocks(struct badblocks *bb) | |
8391 | { | |
8392 | if (bb->page == NULL || bb->changed) | |
8393 | /* no point even trying */ | |
8394 | return; | |
8395 | write_seqlock_irq(&bb->lock); | |
8396 | ||
ecb178bb | 8397 | if (bb->changed == 0 && bb->unacked_exist) { |
2230dfe4 N |
8398 | u64 *p = bb->page; |
8399 | int i; | |
8400 | for (i = 0; i < bb->count ; i++) { | |
8401 | if (!BB_ACK(p[i])) { | |
8402 | sector_t start = BB_OFFSET(p[i]); | |
8403 | int len = BB_LEN(p[i]); | |
8404 | p[i] = BB_MAKE(start, len, 1); | |
8405 | } | |
8406 | } | |
de393cde | 8407 | bb->unacked_exist = 0; |
2230dfe4 N |
8408 | } |
8409 | write_sequnlock_irq(&bb->lock); | |
8410 | } | |
8411 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); | |
8412 | ||
16c791a5 N |
8413 | /* sysfs access to bad-blocks list. |
8414 | * We present two files. | |
8415 | * 'bad-blocks' lists sector numbers and lengths of ranges that | |
8416 | * are recorded as bad. The list is truncated to fit within | |
8417 | * the one-page limit of sysfs. | |
8418 | * Writing "sector length" to this file adds an acknowledged | |
8419 | * bad block list. | |
8420 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet | |
8421 | * been acknowledged. Writing to this file adds bad blocks | |
8422 | * without acknowledging them. This is largely for testing. | |
8423 | */ | |
8424 | ||
8425 | static ssize_t | |
8426 | badblocks_show(struct badblocks *bb, char *page, int unack) | |
8427 | { | |
8428 | size_t len; | |
8429 | int i; | |
8430 | u64 *p = bb->page; | |
8431 | unsigned seq; | |
8432 | ||
8433 | if (bb->shift < 0) | |
8434 | return 0; | |
8435 | ||
8436 | retry: | |
8437 | seq = read_seqbegin(&bb->lock); | |
8438 | ||
8439 | len = 0; | |
8440 | i = 0; | |
8441 | ||
8442 | while (len < PAGE_SIZE && i < bb->count) { | |
8443 | sector_t s = BB_OFFSET(p[i]); | |
8444 | unsigned int length = BB_LEN(p[i]); | |
8445 | int ack = BB_ACK(p[i]); | |
8446 | i++; | |
8447 | ||
8448 | if (unack && ack) | |
8449 | continue; | |
8450 | ||
8451 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", | |
8452 | (unsigned long long)s << bb->shift, | |
8453 | length << bb->shift); | |
8454 | } | |
de393cde N |
8455 | if (unack && len == 0) |
8456 | bb->unacked_exist = 0; | |
16c791a5 N |
8457 | |
8458 | if (read_seqretry(&bb->lock, seq)) | |
8459 | goto retry; | |
8460 | ||
8461 | return len; | |
8462 | } | |
8463 | ||
8464 | #define DO_DEBUG 1 | |
8465 | ||
8466 | static ssize_t | |
8467 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) | |
8468 | { | |
8469 | unsigned long long sector; | |
8470 | int length; | |
8471 | char newline; | |
8472 | #ifdef DO_DEBUG | |
8473 | /* Allow clearing via sysfs *only* for testing/debugging. | |
8474 | * Normally only a successful write may clear a badblock | |
8475 | */ | |
8476 | int clear = 0; | |
8477 | if (page[0] == '-') { | |
8478 | clear = 1; | |
8479 | page++; | |
8480 | } | |
8481 | #endif /* DO_DEBUG */ | |
8482 | ||
8483 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { | |
8484 | case 3: | |
8485 | if (newline != '\n') | |
8486 | return -EINVAL; | |
8487 | case 2: | |
8488 | if (length <= 0) | |
8489 | return -EINVAL; | |
8490 | break; | |
8491 | default: | |
8492 | return -EINVAL; | |
8493 | } | |
8494 | ||
8495 | #ifdef DO_DEBUG | |
8496 | if (clear) { | |
8497 | md_clear_badblocks(bb, sector, length); | |
8498 | return len; | |
8499 | } | |
8500 | #endif /* DO_DEBUG */ | |
8501 | if (md_set_badblocks(bb, sector, length, !unack)) | |
8502 | return len; | |
8503 | else | |
8504 | return -ENOSPC; | |
8505 | } | |
8506 | ||
75c96f85 AB |
8507 | static int md_notify_reboot(struct notifier_block *this, |
8508 | unsigned long code, void *x) | |
1da177e4 LT |
8509 | { |
8510 | struct list_head *tmp; | |
fd01b88c | 8511 | struct mddev *mddev; |
2dba6a91 | 8512 | int need_delay = 0; |
1da177e4 | 8513 | |
c744a65c N |
8514 | for_each_mddev(mddev, tmp) { |
8515 | if (mddev_trylock(mddev)) { | |
30b8aa91 N |
8516 | if (mddev->pers) |
8517 | __md_stop_writes(mddev); | |
0f62fb22 N |
8518 | if (mddev->persistent) |
8519 | mddev->safemode = 2; | |
c744a65c | 8520 | mddev_unlock(mddev); |
2dba6a91 | 8521 | } |
c744a65c | 8522 | need_delay = 1; |
1da177e4 | 8523 | } |
c744a65c N |
8524 | /* |
8525 | * certain more exotic SCSI devices are known to be | |
8526 | * volatile wrt too early system reboots. While the | |
8527 | * right place to handle this issue is the given | |
8528 | * driver, we do want to have a safe RAID driver ... | |
8529 | */ | |
8530 | if (need_delay) | |
8531 | mdelay(1000*1); | |
8532 | ||
1da177e4 LT |
8533 | return NOTIFY_DONE; |
8534 | } | |
8535 | ||
75c96f85 | 8536 | static struct notifier_block md_notifier = { |
1da177e4 LT |
8537 | .notifier_call = md_notify_reboot, |
8538 | .next = NULL, | |
8539 | .priority = INT_MAX, /* before any real devices */ | |
8540 | }; | |
8541 | ||
8542 | static void md_geninit(void) | |
8543 | { | |
36a4e1fe | 8544 | pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
1da177e4 | 8545 | |
c7705f34 | 8546 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
1da177e4 LT |
8547 | } |
8548 | ||
75c96f85 | 8549 | static int __init md_init(void) |
1da177e4 | 8550 | { |
e804ac78 TH |
8551 | int ret = -ENOMEM; |
8552 | ||
ada609ee | 8553 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
e804ac78 TH |
8554 | if (!md_wq) |
8555 | goto err_wq; | |
8556 | ||
8557 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); | |
8558 | if (!md_misc_wq) | |
8559 | goto err_misc_wq; | |
8560 | ||
8561 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) | |
8562 | goto err_md; | |
8563 | ||
8564 | if ((ret = register_blkdev(0, "mdp")) < 0) | |
8565 | goto err_mdp; | |
8566 | mdp_major = ret; | |
8567 | ||
af5628f0 | 8568 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
e8703fe1 N |
8569 | md_probe, NULL, NULL); |
8570 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, | |
1da177e4 LT |
8571 | md_probe, NULL, NULL); |
8572 | ||
1da177e4 | 8573 | register_reboot_notifier(&md_notifier); |
0b4d4147 | 8574 | raid_table_header = register_sysctl_table(raid_root_table); |
1da177e4 LT |
8575 | |
8576 | md_geninit(); | |
d710e138 | 8577 | return 0; |
1da177e4 | 8578 | |
e804ac78 TH |
8579 | err_mdp: |
8580 | unregister_blkdev(MD_MAJOR, "md"); | |
8581 | err_md: | |
8582 | destroy_workqueue(md_misc_wq); | |
8583 | err_misc_wq: | |
8584 | destroy_workqueue(md_wq); | |
8585 | err_wq: | |
8586 | return ret; | |
8587 | } | |
1da177e4 LT |
8588 | |
8589 | #ifndef MODULE | |
8590 | ||
8591 | /* | |
8592 | * Searches all registered partitions for autorun RAID arrays | |
8593 | * at boot time. | |
8594 | */ | |
4d936ec1 ME |
8595 | |
8596 | static LIST_HEAD(all_detected_devices); | |
8597 | struct detected_devices_node { | |
8598 | struct list_head list; | |
8599 | dev_t dev; | |
8600 | }; | |
1da177e4 LT |
8601 | |
8602 | void md_autodetect_dev(dev_t dev) | |
8603 | { | |
4d936ec1 ME |
8604 | struct detected_devices_node *node_detected_dev; |
8605 | ||
8606 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); | |
8607 | if (node_detected_dev) { | |
8608 | node_detected_dev->dev = dev; | |
8609 | list_add_tail(&node_detected_dev->list, &all_detected_devices); | |
8610 | } else { | |
8611 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" | |
8612 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); | |
8613 | } | |
1da177e4 LT |
8614 | } |
8615 | ||
1da177e4 LT |
8616 | static void autostart_arrays(int part) |
8617 | { | |
3cb03002 | 8618 | struct md_rdev *rdev; |
4d936ec1 ME |
8619 | struct detected_devices_node *node_detected_dev; |
8620 | dev_t dev; | |
8621 | int i_scanned, i_passed; | |
1da177e4 | 8622 | |
4d936ec1 ME |
8623 | i_scanned = 0; |
8624 | i_passed = 0; | |
1da177e4 | 8625 | |
4d936ec1 | 8626 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
1da177e4 | 8627 | |
4d936ec1 ME |
8628 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
8629 | i_scanned++; | |
8630 | node_detected_dev = list_entry(all_detected_devices.next, | |
8631 | struct detected_devices_node, list); | |
8632 | list_del(&node_detected_dev->list); | |
8633 | dev = node_detected_dev->dev; | |
8634 | kfree(node_detected_dev); | |
df968c4e | 8635 | rdev = md_import_device(dev,0, 90); |
1da177e4 LT |
8636 | if (IS_ERR(rdev)) |
8637 | continue; | |
8638 | ||
b2d444d7 | 8639 | if (test_bit(Faulty, &rdev->flags)) { |
1da177e4 LT |
8640 | MD_BUG(); |
8641 | continue; | |
8642 | } | |
d0fae18f | 8643 | set_bit(AutoDetected, &rdev->flags); |
1da177e4 | 8644 | list_add(&rdev->same_set, &pending_raid_disks); |
4d936ec1 | 8645 | i_passed++; |
1da177e4 | 8646 | } |
4d936ec1 ME |
8647 | |
8648 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", | |
8649 | i_scanned, i_passed); | |
1da177e4 LT |
8650 | |
8651 | autorun_devices(part); | |
8652 | } | |
8653 | ||
fdee8ae4 | 8654 | #endif /* !MODULE */ |
1da177e4 LT |
8655 | |
8656 | static __exit void md_exit(void) | |
8657 | { | |
fd01b88c | 8658 | struct mddev *mddev; |
1da177e4 | 8659 | struct list_head *tmp; |
e2f23b60 | 8660 | int delay = 1; |
8ab5e4c1 | 8661 | |
af5628f0 | 8662 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
e8703fe1 | 8663 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
1da177e4 | 8664 | |
3dbd8c2e | 8665 | unregister_blkdev(MD_MAJOR,"md"); |
1da177e4 LT |
8666 | unregister_blkdev(mdp_major, "mdp"); |
8667 | unregister_reboot_notifier(&md_notifier); | |
8668 | unregister_sysctl_table(raid_table_header); | |
e2f23b60 N |
8669 | |
8670 | /* We cannot unload the modules while some process is | |
8671 | * waiting for us in select() or poll() - wake them up | |
8672 | */ | |
8673 | md_unloading = 1; | |
8674 | while (waitqueue_active(&md_event_waiters)) { | |
8675 | /* not safe to leave yet */ | |
8676 | wake_up(&md_event_waiters); | |
8677 | msleep(delay); | |
8678 | delay += delay; | |
8679 | } | |
1da177e4 | 8680 | remove_proc_entry("mdstat", NULL); |
e2f23b60 | 8681 | |
29ac4aa3 | 8682 | for_each_mddev(mddev, tmp) { |
1da177e4 | 8683 | export_array(mddev); |
d3374825 | 8684 | mddev->hold_active = 0; |
1da177e4 | 8685 | } |
e804ac78 TH |
8686 | destroy_workqueue(md_misc_wq); |
8687 | destroy_workqueue(md_wq); | |
1da177e4 LT |
8688 | } |
8689 | ||
685784aa | 8690 | subsys_initcall(md_init); |
1da177e4 LT |
8691 | module_exit(md_exit) |
8692 | ||
f91de92e N |
8693 | static int get_ro(char *buffer, struct kernel_param *kp) |
8694 | { | |
8695 | return sprintf(buffer, "%d", start_readonly); | |
8696 | } | |
8697 | static int set_ro(const char *val, struct kernel_param *kp) | |
8698 | { | |
8699 | char *e; | |
8700 | int num = simple_strtoul(val, &e, 10); | |
8701 | if (*val && (*e == '\0' || *e == '\n')) { | |
8702 | start_readonly = num; | |
4dbcdc75 | 8703 | return 0; |
f91de92e N |
8704 | } |
8705 | return -EINVAL; | |
8706 | } | |
8707 | ||
80ca3a44 N |
8708 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
8709 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); | |
6ff8d8ec | 8710 | |
efeb53c0 | 8711 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
f91de92e | 8712 | |
1da177e4 LT |
8713 | EXPORT_SYMBOL(register_md_personality); |
8714 | EXPORT_SYMBOL(unregister_md_personality); | |
8715 | EXPORT_SYMBOL(md_error); | |
8716 | EXPORT_SYMBOL(md_done_sync); | |
8717 | EXPORT_SYMBOL(md_write_start); | |
8718 | EXPORT_SYMBOL(md_write_end); | |
1da177e4 LT |
8719 | EXPORT_SYMBOL(md_register_thread); |
8720 | EXPORT_SYMBOL(md_unregister_thread); | |
8721 | EXPORT_SYMBOL(md_wakeup_thread); | |
1da177e4 | 8722 | EXPORT_SYMBOL(md_check_recovery); |
a91d5ac0 | 8723 | EXPORT_SYMBOL(md_reap_sync_thread); |
1da177e4 | 8724 | MODULE_LICENSE("GPL"); |
0efb9e61 | 8725 | MODULE_DESCRIPTION("MD RAID framework"); |
aa1595e9 | 8726 | MODULE_ALIAS("md"); |
72008652 | 8727 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |