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