2 md.h : kernel internal structure of the Linux MD driver
3 Copyright (C) 1996-98 Ingo Molnar, Gadi Oxman
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 You should have received a copy of the GNU General Public License
11 (for example /usr/src/linux/COPYING); if not, write to the Free
12 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include <linux/blkdev.h>
19 #include <linux/kobject.h>
20 #include <linux/list.h>
22 #include <linux/mutex.h>
23 #include <linux/timer.h>
24 #include <linux/wait.h>
25 #include <linux/workqueue.h>
27 #define MaxSector (~(sector_t)0)
29 /* Bad block numbers are stored sorted in a single page.
30 * 64bits is used for each block or extent.
31 * 54 bits are sector number, 9 bits are extent size,
32 * 1 bit is an 'acknowledged' flag.
34 #define MD_MAX_BADBLOCKS (PAGE_SIZE/8)
37 * MD's 'extended' device
40 struct list_head same_set
; /* RAID devices within the same set */
42 sector_t sectors
; /* Device size (in 512bytes sectors) */
43 struct mddev
*mddev
; /* RAID array if running */
44 int last_events
; /* IO event timestamp */
47 * If meta_bdev is non-NULL, it means that a separate device is
48 * being used to store the metadata (superblock/bitmap) which
49 * would otherwise be contained on the same device as the data (bdev).
51 struct block_device
*meta_bdev
;
52 struct block_device
*bdev
; /* block device handle */
54 struct page
*sb_page
, *bb_page
;
57 sector_t data_offset
; /* start of data in array */
58 sector_t sb_start
; /* offset of the super block (in 512byte sectors) */
59 int sb_size
; /* bytes in the superblock */
60 int preferred_minor
; /* autorun support */
64 /* A device can be in one of three states based on two flags:
65 * Not working: faulty==1 in_sync==0
66 * Fully working: faulty==0 in_sync==1
69 * faulty==0 in_sync==0
71 * It can never have faulty==1, in_sync==1
72 * This reduces the burden of testing multiple flags in many cases
76 #define Faulty 1 /* device is known to have a fault */
77 #define In_sync 2 /* device is in_sync with rest of array */
78 #define WriteMostly 4 /* Avoid reading if at all possible */
79 #define AutoDetected 7 /* added by auto-detect */
80 #define Blocked 8 /* An error occurred but has not yet
81 * been acknowledged by the metadata
82 * handler, so don't allow writes
83 * until it is cleared */
84 #define WriteErrorSeen 9 /* A write error has been seen on this
87 #define FaultRecorded 10 /* Intermediate state for clearing
88 * Blocked. The Fault is/will-be
89 * recorded in the metadata, but that
90 * metadata hasn't been stored safely
93 #define BlockedBadBlocks 11 /* A writer is blocked because they
94 * found an unacknowledged bad-block.
95 * This can safely be cleared at any
96 * time, and the writer will re-check.
97 * It may be set at any time, and at
98 * worst the writer will timeout and
99 * re-check. So setting it as
100 * accurately as possible is good, but
101 * not absolutely critical.
103 wait_queue_head_t blocked_wait
;
105 int desc_nr
; /* descriptor index in the superblock */
106 int raid_disk
; /* role of device in array */
107 int new_raid_disk
; /* role that the device will have in
108 * the array after a level-change completes.
110 int saved_raid_disk
; /* role that device used to have in the
111 * array and could again if we did a partial
112 * resync from the bitmap
114 sector_t recovery_offset
;/* If this device has been partially
115 * recovered, this is where we were
119 atomic_t nr_pending
; /* number of pending requests.
120 * only maintained for arrays that
121 * support hot removal
123 atomic_t read_errors
; /* number of consecutive read errors that
124 * we have tried to ignore.
126 struct timespec last_read_error
; /* monotonic time since our
129 atomic_t corrected_errors
; /* number of corrected read errors,
130 * for reporting to userspace and storing
133 struct work_struct del_work
; /* used for delayed sysfs removal */
135 struct sysfs_dirent
*sysfs_state
; /* handle for 'state'
139 int count
; /* count of bad blocks */
140 int unacked_exist
; /* there probably are unacknowledged
141 * bad blocks. This is only cleared
142 * when a read discovers none
144 int shift
; /* shift from sectors to block size
145 * a -ve shift means badblocks are
147 u64
*page
; /* badblock list */
152 sector_t size
; /* in sectors */
156 #define BB_LEN_MASK (0x00000000000001FFULL)
157 #define BB_OFFSET_MASK (0x7FFFFFFFFFFFFE00ULL)
158 #define BB_ACK_MASK (0x8000000000000000ULL)
159 #define BB_MAX_LEN 512
160 #define BB_OFFSET(x) (((x) & BB_OFFSET_MASK) >> 9)
161 #define BB_LEN(x) (((x) & BB_LEN_MASK) + 1)
162 #define BB_ACK(x) (!!((x) & BB_ACK_MASK))
163 #define BB_MAKE(a, l, ack) (((a)<<9) | ((l)-1) | ((u64)(!!(ack)) << 63))
165 extern int md_is_badblock(struct badblocks
*bb
, sector_t s
, int sectors
,
166 sector_t
*first_bad
, int *bad_sectors
);
167 static inline int is_badblock(struct md_rdev
*rdev
, sector_t s
, int sectors
,
168 sector_t
*first_bad
, int *bad_sectors
)
170 if (unlikely(rdev
->badblocks
.count
)) {
171 int rv
= md_is_badblock(&rdev
->badblocks
, rdev
->data_offset
+ s
,
173 first_bad
, bad_sectors
);
175 *first_bad
-= rdev
->data_offset
;
180 extern int rdev_set_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
,
182 extern int rdev_clear_badblocks(struct md_rdev
*rdev
, sector_t s
, int sectors
);
183 extern void md_ack_all_badblocks(struct badblocks
*bb
);
187 struct md_personality
*pers
;
190 struct list_head disks
;
192 #define MD_CHANGE_DEVS 0 /* Some device status has changed */
193 #define MD_CHANGE_CLEAN 1 /* transition to or from 'clean' */
194 #define MD_CHANGE_PENDING 2 /* switch from 'clean' to 'active' in progress */
195 #define MD_ARRAY_FIRST_USE 3 /* First use of array, needs initialization */
200 int sysfs_active
; /* set when sysfs deletes
201 * are happening, so run/
202 * takeover/stop are not safe
204 int ready
; /* See when safe to pass
205 * IO requests down */
206 struct gendisk
*gendisk
;
210 #define UNTIL_IOCTL 1
213 /* Superblock information */
218 int external
; /* metadata is
219 * managed externally */
220 char metadata_type
[17]; /* externally set*/
227 sector_t dev_sectors
; /* used size of
228 * component devices */
229 sector_t array_sectors
; /* exported array size */
230 int external_size
; /* size managed
233 /* If the last 'event' was simply a clean->dirty transition, and
234 * we didn't write it to the spares, then it is safe and simple
235 * to just decrement the event count on a dirty->clean transition.
236 * So we record that possibility here.
238 int can_decrease_events
;
242 /* If the array is being reshaped, we need to record the
243 * new shape and an indication of where we are up to.
244 * This is written to the superblock.
245 * If reshape_position is MaxSector, then no reshape is happening (yet).
247 sector_t reshape_position
;
248 int delta_disks
, new_level
, new_layout
;
249 int new_chunk_sectors
;
251 atomic_t plug_cnt
; /* If device is expecting
254 struct md_thread
*thread
; /* management thread */
255 struct md_thread
*sync_thread
; /* doing resync or reconstruct */
256 sector_t curr_resync
; /* last block scheduled */
257 /* As resync requests can complete out of order, we cannot easily track
258 * how much resync has been completed. So we occasionally pause until
259 * everything completes, then set curr_resync_completed to curr_resync.
260 * As such it may be well behind the real resync mark, but it is a value
263 sector_t curr_resync_completed
;
264 unsigned long resync_mark
; /* a recent timestamp */
265 sector_t resync_mark_cnt
;/* blocks written at resync_mark */
266 sector_t curr_mark_cnt
; /* blocks scheduled now */
268 sector_t resync_max_sectors
; /* may be set by personality */
270 sector_t resync_mismatches
; /* count of sectors where
271 * parity/replica mismatch found
274 /* allow user-space to request suspension of IO to regions of the array */
277 /* if zero, use the system-wide default */
281 /* resync even though the same disks are shared among md-devices */
284 int ok_start_degraded
;
285 /* recovery/resync flags
286 * NEEDED: we might need to start a resync/recover
287 * RUNNING: a thread is running, or about to be started
288 * SYNC: actually doing a resync, not a recovery
289 * RECOVER: doing recovery, or need to try it.
290 * INTR: resync needs to be aborted for some reason
291 * DONE: thread is done and is waiting to be reaped
292 * REQUEST: user-space has requested a sync (used with SYNC)
293 * CHECK: user-space request for check-only, no repair
294 * RESHAPE: A reshape is happening
296 * If neither SYNC or RESHAPE are set, then it is a recovery.
298 #define MD_RECOVERY_RUNNING 0
299 #define MD_RECOVERY_SYNC 1
300 #define MD_RECOVERY_RECOVER 2
301 #define MD_RECOVERY_INTR 3
302 #define MD_RECOVERY_DONE 4
303 #define MD_RECOVERY_NEEDED 5
304 #define MD_RECOVERY_REQUESTED 6
305 #define MD_RECOVERY_CHECK 7
306 #define MD_RECOVERY_RESHAPE 8
307 #define MD_RECOVERY_FROZEN 9
309 unsigned long recovery
;
310 /* If a RAID personality determines that recovery (of a particular
311 * device) will fail due to a read error on the source device, it
312 * takes a copy of this number and does not attempt recovery again
313 * until this number changes.
315 int recovery_disabled
;
317 int in_sync
; /* know to not need resync */
318 /* 'open_mutex' avoids races between 'md_open' and 'do_md_stop', so
319 * that we are never stopping an array while it is open.
320 * 'reconfig_mutex' protects all other reconfiguration.
321 * These locks are separate due to conflicting interactions
322 * with bdev->bd_mutex.
324 * reconfig_mutex -> bd_mutex : e.g. do_md_run -> revalidate_disk
325 * bd_mutex -> open_mutex: e.g. __blkdev_get -> md_open
327 struct mutex open_mutex
;
328 struct mutex reconfig_mutex
;
329 atomic_t active
; /* general refcount */
330 atomic_t openers
; /* number of active opens */
332 int changed
; /* True if we might need to
333 * reread partition info */
334 int degraded
; /* whether md should consider
338 atomic_t recovery_active
; /* blocks scheduled, but not written */
339 wait_queue_head_t recovery_wait
;
340 sector_t recovery_cp
;
341 sector_t resync_min
; /* user requested sync
343 sector_t resync_max
; /* resync should pause
344 * when it gets here */
346 struct sysfs_dirent
*sysfs_state
; /* handle for 'array_state'
349 struct sysfs_dirent
*sysfs_action
; /* handle for 'sync_action' */
351 struct work_struct del_work
; /* used for delayed sysfs removal */
353 spinlock_t write_lock
;
354 wait_queue_head_t sb_wait
; /* for waiting on superblock updates */
355 atomic_t pending_writes
; /* number of active superblock writes */
357 unsigned int safemode
; /* if set, update "clean" superblock
358 * when no writes pending.
360 unsigned int safemode_delay
;
361 struct timer_list safemode_timer
;
362 atomic_t writes_pending
;
363 struct request_queue
*queue
; /* for plugging ... */
365 struct bitmap
*bitmap
; /* the bitmap for the device */
367 struct file
*file
; /* the bitmap file */
368 loff_t offset
; /* offset from superblock of
369 * start of bitmap. May be
370 * negative, but not '0'
371 * For external metadata, offset
372 * from start of device.
374 loff_t default_offset
; /* this is the offset to use when
375 * hot-adding a bitmap. It should
376 * eventually be settable by sysfs.
379 unsigned long chunksize
;
380 unsigned long daemon_sleep
; /* how many jiffies between updates? */
381 unsigned long max_write_behind
; /* write-behind mode */
385 atomic_t max_corr_read_errors
; /* max read retries */
386 struct list_head all_mddevs
;
388 struct attribute_group
*to_remove
;
390 struct bio_set
*bio_set
;
392 /* Generic flush handling.
393 * The last to finish preflush schedules a worker to submit
394 * the rest of the request (without the REQ_FLUSH flag).
396 struct bio
*flush_bio
;
397 atomic_t flush_pending
;
398 struct work_struct flush_work
;
399 struct work_struct event_work
; /* used by dm to report failure event */
400 void (*sync_super
)(struct mddev
*mddev
, struct md_rdev
*rdev
);
404 static inline void rdev_dec_pending(struct md_rdev
*rdev
, struct mddev
*mddev
)
406 int faulty
= test_bit(Faulty
, &rdev
->flags
);
407 if (atomic_dec_and_test(&rdev
->nr_pending
) && faulty
)
408 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
411 static inline void md_sync_acct(struct block_device
*bdev
, unsigned long nr_sectors
)
413 atomic_add(nr_sectors
, &bdev
->bd_contains
->bd_disk
->sync_io
);
416 struct md_personality
420 struct list_head list
;
421 struct module
*owner
;
422 void (*make_request
)(struct mddev
*mddev
, struct bio
*bio
);
423 int (*run
)(struct mddev
*mddev
);
424 int (*stop
)(struct mddev
*mddev
);
425 void (*status
)(struct seq_file
*seq
, struct mddev
*mddev
);
426 /* error_handler must set ->faulty and clear ->in_sync
427 * if appropriate, and should abort recovery if needed
429 void (*error_handler
)(struct mddev
*mddev
, struct md_rdev
*rdev
);
430 int (*hot_add_disk
) (struct mddev
*mddev
, struct md_rdev
*rdev
);
431 int (*hot_remove_disk
) (struct mddev
*mddev
, int number
);
432 int (*spare_active
) (struct mddev
*mddev
);
433 sector_t (*sync_request
)(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
);
434 int (*resize
) (struct mddev
*mddev
, sector_t sectors
);
435 sector_t (*size
) (struct mddev
*mddev
, sector_t sectors
, int raid_disks
);
436 int (*check_reshape
) (struct mddev
*mddev
);
437 int (*start_reshape
) (struct mddev
*mddev
);
438 void (*finish_reshape
) (struct mddev
*mddev
);
439 /* quiesce moves between quiescence states
441 * 1 - no new requests allowed
444 void (*quiesce
) (struct mddev
*mddev
, int state
);
445 /* takeover is used to transition an array from one
446 * personality to another. The new personality must be able
447 * to handle the data in the current layout.
448 * e.g. 2drive raid1 -> 2drive raid5
449 * ndrive raid5 -> degraded n+1drive raid6 with special layout
450 * If the takeover succeeds, a new 'private' structure is returned.
451 * This needs to be installed and then ->run used to activate the
454 void *(*takeover
) (struct mddev
*mddev
);
458 struct md_sysfs_entry
{
459 struct attribute attr
;
460 ssize_t (*show
)(struct mddev
*, char *);
461 ssize_t (*store
)(struct mddev
*, const char *, size_t);
463 extern struct attribute_group md_bitmap_group
;
465 static inline struct sysfs_dirent
*sysfs_get_dirent_safe(struct sysfs_dirent
*sd
, char *name
)
468 return sysfs_get_dirent(sd
, NULL
, name
);
471 static inline void sysfs_notify_dirent_safe(struct sysfs_dirent
*sd
)
474 sysfs_notify_dirent(sd
);
477 static inline char * mdname (struct mddev
* mddev
)
479 return mddev
->gendisk
? mddev
->gendisk
->disk_name
: "mdX";
482 static inline int sysfs_link_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
485 sprintf(nm
, "rd%d", rdev
->raid_disk
);
486 return sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
489 static inline void sysfs_unlink_rdev(struct mddev
*mddev
, struct md_rdev
*rdev
)
492 sprintf(nm
, "rd%d", rdev
->raid_disk
);
493 sysfs_remove_link(&mddev
->kobj
, nm
);
497 * iterates through some rdev ringlist. It's safe to remove the
498 * current 'rdev'. Dont touch 'tmp' though.
500 #define rdev_for_each_list(rdev, tmp, head) \
501 list_for_each_entry_safe(rdev, tmp, head, same_set)
504 * iterates through the 'same array disks' ringlist
506 #define rdev_for_each(rdev, tmp, mddev) \
507 list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set)
509 #define rdev_for_each_rcu(rdev, mddev) \
510 list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set)
513 void (*run
) (struct mddev
*mddev
);
515 wait_queue_head_t wqueue
;
517 struct task_struct
*tsk
;
518 unsigned long timeout
;
521 #define THREAD_WAKEUP 0
523 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
525 wait_queue_t __wait; \
526 init_waitqueue_entry(&__wait, current); \
528 add_wait_queue(&wq, &__wait); \
530 set_current_state(TASK_UNINTERRUPTIBLE); \
533 spin_unlock_irq(&lock); \
536 spin_lock_irq(&lock); \
538 current->state = TASK_RUNNING; \
539 remove_wait_queue(&wq, &__wait); \
542 #define wait_event_lock_irq(wq, condition, lock, cmd) \
546 __wait_event_lock_irq(wq, condition, lock, cmd); \
549 static inline void safe_put_page(struct page
*p
)
554 extern int register_md_personality(struct md_personality
*p
);
555 extern int unregister_md_personality(struct md_personality
*p
);
556 extern struct md_thread
*md_register_thread(
557 void (*run
)(struct mddev
*mddev
),
560 extern void md_unregister_thread(struct md_thread
**threadp
);
561 extern void md_wakeup_thread(struct md_thread
*thread
);
562 extern void md_check_recovery(struct mddev
*mddev
);
563 extern void md_write_start(struct mddev
*mddev
, struct bio
*bi
);
564 extern void md_write_end(struct mddev
*mddev
);
565 extern void md_done_sync(struct mddev
*mddev
, int blocks
, int ok
);
566 extern void md_error(struct mddev
*mddev
, struct md_rdev
*rdev
);
568 extern int mddev_congested(struct mddev
*mddev
, int bits
);
569 extern void md_flush_request(struct mddev
*mddev
, struct bio
*bio
);
570 extern void md_super_write(struct mddev
*mddev
, struct md_rdev
*rdev
,
571 sector_t sector
, int size
, struct page
*page
);
572 extern void md_super_wait(struct mddev
*mddev
);
573 extern int sync_page_io(struct md_rdev
*rdev
, sector_t sector
, int size
,
574 struct page
*page
, int rw
, bool metadata_op
);
575 extern void md_do_sync(struct mddev
*mddev
);
576 extern void md_new_event(struct mddev
*mddev
);
577 extern int md_allow_write(struct mddev
*mddev
);
578 extern void md_wait_for_blocked_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
);
579 extern void md_set_array_sectors(struct mddev
*mddev
, sector_t array_sectors
);
580 extern int md_check_no_bitmap(struct mddev
*mddev
);
581 extern int md_integrity_register(struct mddev
*mddev
);
582 extern void md_integrity_add_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
);
583 extern int strict_strtoul_scaled(const char *cp
, unsigned long *res
, int scale
);
584 extern void restore_bitmap_write_access(struct file
*file
);
586 extern void mddev_init(struct mddev
*mddev
);
587 extern int md_run(struct mddev
*mddev
);
588 extern void md_stop(struct mddev
*mddev
);
589 extern void md_stop_writes(struct mddev
*mddev
);
590 extern int md_rdev_init(struct md_rdev
*rdev
);
592 extern void mddev_suspend(struct mddev
*mddev
);
593 extern void mddev_resume(struct mddev
*mddev
);
594 extern struct bio
*bio_clone_mddev(struct bio
*bio
, gfp_t gfp_mask
,
595 struct mddev
*mddev
);
596 extern struct bio
*bio_alloc_mddev(gfp_t gfp_mask
, int nr_iovecs
,
597 struct mddev
*mddev
);
598 extern int mddev_check_plugged(struct mddev
*mddev
);
599 extern void md_trim_bio(struct bio
*bio
, int offset
, int size
);
600 #endif /* _MD_MD_H */