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/backing-dev.h>
20 #include <linux/kobject.h>
21 #include <linux/list.h>
23 #include <linux/mutex.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #include <linux/workqueue.h>
27 #include "md-cluster.h"
29 #define MaxSector (~(sector_t)0)
31 /* Bad block numbers are stored sorted in a single page.
32 * 64bits is used for each block or extent.
33 * 54 bits are sector number, 9 bits are extent size,
34 * 1 bit is an 'acknowledged' flag.
36 #define MD_MAX_BADBLOCKS (PAGE_SIZE/8)
39 * MD's 'extended' device
42 struct list_head same_set; /* RAID devices within the same set */
44 sector_t sectors; /* Device size (in 512bytes sectors) */
45 struct mddev *mddev; /* RAID array if running */
46 int last_events; /* IO event timestamp */
49 * If meta_bdev is non-NULL, it means that a separate device is
50 * being used to store the metadata (superblock/bitmap) which
51 * would otherwise be contained on the same device as the data (bdev).
53 struct block_device *meta_bdev;
54 struct block_device *bdev; /* block device handle */
56 struct page *sb_page, *bb_page;
59 sector_t data_offset; /* start of data in array */
60 sector_t new_data_offset;/* only relevant while reshaping */
61 sector_t sb_start; /* offset of the super block (in 512byte sectors) */
62 int sb_size; /* bytes in the superblock */
63 int preferred_minor; /* autorun support */
67 /* A device can be in one of three states based on two flags:
68 * Not working: faulty==1 in_sync==0
69 * Fully working: faulty==0 in_sync==1
72 * faulty==0 in_sync==0
74 * It can never have faulty==1, in_sync==1
75 * This reduces the burden of testing multiple flags in many cases
78 unsigned long flags; /* bit set of 'enum flag_bits' bits. */
79 wait_queue_head_t blocked_wait;
81 int desc_nr; /* descriptor index in the superblock */
82 int raid_disk; /* role of device in array */
83 int new_raid_disk; /* role that the device will have in
84 * the array after a level-change completes.
86 int saved_raid_disk; /* role that device used to have in the
87 * array and could again if we did a partial
88 * resync from the bitmap
90 sector_t recovery_offset;/* If this device has been partially
91 * recovered, this is where we were
95 atomic_t nr_pending; /* number of pending requests.
96 * only maintained for arrays that
99 atomic_t read_errors; /* number of consecutive read errors that
100 * we have tried to ignore.
102 struct timespec last_read_error; /* monotonic time since our
105 atomic_t corrected_errors; /* number of corrected read errors,
106 * for reporting to userspace and storing
109 struct work_struct del_work; /* used for delayed sysfs removal */
111 struct kernfs_node *sysfs_state; /* handle for 'state'
115 int count; /* count of bad blocks */
116 int unacked_exist; /* there probably are unacknowledged
117 * bad blocks. This is only cleared
118 * when a read discovers none
120 int shift; /* shift from sectors to block size
121 * a -ve shift means badblocks are
123 u64 *page; /* badblock list */
128 sector_t size; /* in sectors */
132 Faulty, /* device is known to have a fault */
133 In_sync, /* device is in_sync with rest of array */
134 Bitmap_sync, /* ..actually, not quite In_sync. Need a
135 * bitmap-based recovery to get fully in sync
137 WriteMostly, /* Avoid reading if at all possible */
138 AutoDetected, /* added by auto-detect */
139 Blocked, /* An error occurred but has not yet
140 * been acknowledged by the metadata
141 * handler, so don't allow writes
142 * until it is cleared */
143 WriteErrorSeen, /* A write error has been seen on this
146 FaultRecorded, /* Intermediate state for clearing
147 * Blocked. The Fault is/will-be
148 * recorded in the metadata, but that
149 * metadata hasn't been stored safely
152 BlockedBadBlocks, /* A writer is blocked because they
153 * found an unacknowledged bad-block.
154 * This can safely be cleared at any
155 * time, and the writer will re-check.
156 * It may be set at any time, and at
157 * worst the writer will timeout and
158 * re-check. So setting it as
159 * accurately as possible is good, but
160 * not absolutely critical.
162 WantReplacement, /* This device is a candidate to be
163 * hot-replaced, either because it has
164 * reported some faults, or because
165 * of explicit request.
167 Replacement, /* This device is a replacement for
168 * a want_replacement device with same
171 Candidate, /* For clustered environments only:
172 * This device is seen locally but not
173 * by the whole cluster
177 #define BB_LEN_MASK (0x00000000000001FFULL)
178 #define BB_OFFSET_MASK (0x7FFFFFFFFFFFFE00ULL)
179 #define BB_ACK_MASK (0x8000000000000000ULL)
180 #define BB_MAX_LEN 512
181 #define BB_OFFSET(x) (((x) & BB_OFFSET_MASK) >> 9)
182 #define BB_LEN(x) (((x) & BB_LEN_MASK) + 1)
183 #define BB_ACK(x) (!!((x) & BB_ACK_MASK))
184 #define BB_MAKE(a, l, ack) (((a)<<9) | ((l)-1) | ((u64)(!!(ack)) << 63))
186 extern int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
187 sector_t *first_bad, int *bad_sectors);
188 static inline int is_badblock(struct md_rdev *rdev, sector_t s, int sectors,
189 sector_t *first_bad, int *bad_sectors)
191 if (unlikely(rdev->badblocks.count)) {
192 int rv = md_is_badblock(&rdev->badblocks, rdev->data_offset + s,
194 first_bad, bad_sectors);
196 *first_bad -= rdev->data_offset;
201 extern int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
203 extern int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
205 extern void md_ack_all_badblocks(struct badblocks *bb);
207 struct md_cluster_info;
211 struct md_personality *pers;
214 struct list_head disks;
216 #define MD_CHANGE_DEVS 0 /* Some device status has changed */
217 #define MD_CHANGE_CLEAN 1 /* transition to or from 'clean' */
218 #define MD_CHANGE_PENDING 2 /* switch from 'clean' to 'active' in progress */
219 #define MD_UPDATE_SB_FLAGS (1 | 2 | 4) /* If these are set, md_update_sb needed */
220 #define MD_ARRAY_FIRST_USE 3 /* First use of array, needs initialization */
221 #define MD_STILL_CLOSED 4 /* If set, then array has not been opened since
222 * md_ioctl checked on it.
228 int sysfs_active; /* set when sysfs deletes
229 * are happening, so run/
230 * takeover/stop are not safe
232 int ready; /* See when safe to pass
233 * IO requests down */
234 struct gendisk *gendisk;
238 #define UNTIL_IOCTL 1
241 /* Superblock information */
246 int external; /* metadata is
247 * managed externally */
248 char metadata_type[17]; /* externally set*/
255 sector_t dev_sectors; /* used size of
256 * component devices */
257 sector_t array_sectors; /* exported array size */
258 int external_size; /* size managed
261 /* If the last 'event' was simply a clean->dirty transition, and
262 * we didn't write it to the spares, then it is safe and simple
263 * to just decrement the event count on a dirty->clean transition.
264 * So we record that possibility here.
266 int can_decrease_events;
270 /* If the array is being reshaped, we need to record the
271 * new shape and an indication of where we are up to.
272 * This is written to the superblock.
273 * If reshape_position is MaxSector, then no reshape is happening (yet).
275 sector_t reshape_position;
276 int delta_disks, new_level, new_layout;
277 int new_chunk_sectors;
278 int reshape_backwards;
280 struct md_thread *thread; /* management thread */
281 struct md_thread *sync_thread; /* doing resync or reconstruct */
283 /* 'last_sync_action' is initialized to "none". It is set when a
284 * sync operation (i.e "data-check", "requested-resync", "resync",
285 * "recovery", or "reshape") is started. It holds this value even
286 * when the sync thread is "frozen" (interrupted) or "idle" (stopped
287 * or finished). It is overwritten when a new sync operation is begun.
289 char *last_sync_action;
290 sector_t curr_resync; /* last block scheduled */
291 /* As resync requests can complete out of order, we cannot easily track
292 * how much resync has been completed. So we occasionally pause until
293 * everything completes, then set curr_resync_completed to curr_resync.
294 * As such it may be well behind the real resync mark, but it is a value
297 sector_t curr_resync_completed;
298 unsigned long resync_mark; /* a recent timestamp */
299 sector_t resync_mark_cnt;/* blocks written at resync_mark */
300 sector_t curr_mark_cnt; /* blocks scheduled now */
302 sector_t resync_max_sectors; /* may be set by personality */
304 atomic64_t resync_mismatches; /* count of sectors where
305 * parity/replica mismatch found
308 /* allow user-space to request suspension of IO to regions of the array */
311 /* if zero, use the system-wide default */
315 /* resync even though the same disks are shared among md-devices */
318 int ok_start_degraded;
319 /* recovery/resync flags
320 * NEEDED: we might need to start a resync/recover
321 * RUNNING: a thread is running, or about to be started
322 * SYNC: actually doing a resync, not a recovery
323 * RECOVER: doing recovery, or need to try it.
324 * INTR: resync needs to be aborted for some reason
325 * DONE: thread is done and is waiting to be reaped
326 * REQUEST: user-space has requested a sync (used with SYNC)
327 * CHECK: user-space request for check-only, no repair
328 * RESHAPE: A reshape is happening
329 * ERROR: sync-action interrupted because io-error
331 * If neither SYNC or RESHAPE are set, then it is a recovery.
333 #define MD_RECOVERY_RUNNING 0
334 #define MD_RECOVERY_SYNC 1
335 #define MD_RECOVERY_RECOVER 2
336 #define MD_RECOVERY_INTR 3
337 #define MD_RECOVERY_DONE 4
338 #define MD_RECOVERY_NEEDED 5
339 #define MD_RECOVERY_REQUESTED 6
340 #define MD_RECOVERY_CHECK 7
341 #define MD_RECOVERY_RESHAPE 8
342 #define MD_RECOVERY_FROZEN 9
343 #define MD_RECOVERY_ERROR 10
345 unsigned long recovery;
346 /* If a RAID personality determines that recovery (of a particular
347 * device) will fail due to a read error on the source device, it
348 * takes a copy of this number and does not attempt recovery again
349 * until this number changes.
351 int recovery_disabled;
353 int in_sync; /* know to not need resync */
354 /* 'open_mutex' avoids races between 'md_open' and 'do_md_stop', so
355 * that we are never stopping an array while it is open.
356 * 'reconfig_mutex' protects all other reconfiguration.
357 * These locks are separate due to conflicting interactions
358 * with bdev->bd_mutex.
360 * reconfig_mutex -> bd_mutex : e.g. do_md_run -> revalidate_disk
361 * bd_mutex -> open_mutex: e.g. __blkdev_get -> md_open
363 struct mutex open_mutex;
364 struct mutex reconfig_mutex;
365 atomic_t active; /* general refcount */
366 atomic_t openers; /* number of active opens */
368 int changed; /* True if we might need to
369 * reread partition info */
370 int degraded; /* whether md should consider
374 atomic_t recovery_active; /* blocks scheduled, but not written */
375 wait_queue_head_t recovery_wait;
376 sector_t recovery_cp;
377 sector_t resync_min; /* user requested sync
379 sector_t resync_max; /* resync should pause
380 * when it gets here */
382 struct kernfs_node *sysfs_state; /* handle for 'array_state'
385 struct kernfs_node *sysfs_action; /* handle for 'sync_action' */
387 struct work_struct del_work; /* used for delayed sysfs removal */
390 * flush_bio transition from NULL to !NULL
391 * rdev superblocks, events
392 * clearing MD_CHANGE_*
393 * in_sync - and related safemode and MD_CHANGE changes
394 * pers (also protected by reconfig_mutex and pending IO).
396 * clearing ->bitmap_info.file
397 * changing ->resync_{min,max}
398 * setting MD_RECOVERY_RUNNING (which interacts with resync_{min,max})
401 wait_queue_head_t sb_wait; /* for waiting on superblock updates */
402 atomic_t pending_writes; /* number of active superblock writes */
404 unsigned int safemode; /* if set, update "clean" superblock
405 * when no writes pending.
407 unsigned int safemode_delay;
408 struct timer_list safemode_timer;
409 atomic_t writes_pending;
410 struct request_queue *queue; /* for plugging ... */
412 struct bitmap *bitmap; /* the bitmap for the device */
414 struct file *file; /* the bitmap file */
415 loff_t offset; /* offset from superblock of
416 * start of bitmap. May be
417 * negative, but not '0'
418 * For external metadata, offset
419 * from start of device.
421 unsigned long space; /* space available at this offset */
422 loff_t default_offset; /* this is the offset to use when
423 * hot-adding a bitmap. It should
424 * eventually be settable by sysfs.
426 unsigned long default_space; /* space available at
429 unsigned long chunksize;
430 unsigned long daemon_sleep; /* how many jiffies between updates? */
431 unsigned long max_write_behind; /* write-behind mode */
433 int nodes; /* Maximum number of nodes in the cluster */
434 char cluster_name[64]; /* Name of the cluster */
437 atomic_t max_corr_read_errors; /* max read retries */
438 struct list_head all_mddevs;
440 struct attribute_group *to_remove;
442 struct bio_set *bio_set;
444 /* Generic flush handling.
445 * The last to finish preflush schedules a worker to submit
446 * the rest of the request (without the REQ_FLUSH flag).
448 struct bio *flush_bio;
449 atomic_t flush_pending;
450 struct work_struct flush_work;
451 struct work_struct event_work; /* used by dm to report failure event */
452 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
453 struct md_cluster_info *cluster_info;
456 static inline int __must_check mddev_lock(struct mddev *mddev)
458 return mutex_lock_interruptible(&mddev->reconfig_mutex);
461 /* Sometimes we need to take the lock in a situation where
462 * failure due to interrupts is not acceptable.
464 static inline void mddev_lock_nointr(struct mddev *mddev)
466 mutex_lock(&mddev->reconfig_mutex);
469 static inline int mddev_is_locked(struct mddev *mddev)
471 return mutex_is_locked(&mddev->reconfig_mutex);
474 static inline int mddev_trylock(struct mddev *mddev)
476 return mutex_trylock(&mddev->reconfig_mutex);
478 extern void mddev_unlock(struct mddev *mddev);
480 static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
482 atomic_add(nr_sectors, &bdev->bd_contains->bd_disk->sync_io);
485 struct md_personality
489 struct list_head list;
490 struct module *owner;
491 void (*make_request)(struct mddev *mddev, struct bio *bio);
492 int (*run)(struct mddev *mddev);
493 void (*free)(struct mddev *mddev, void *priv);
494 void (*status)(struct seq_file *seq, struct mddev *mddev);
495 /* error_handler must set ->faulty and clear ->in_sync
496 * if appropriate, and should abort recovery if needed
498 void (*error_handler)(struct mddev *mddev, struct md_rdev *rdev);
499 int (*hot_add_disk) (struct mddev *mddev, struct md_rdev *rdev);
500 int (*hot_remove_disk) (struct mddev *mddev, struct md_rdev *rdev);
501 int (*spare_active) (struct mddev *mddev);
502 sector_t (*sync_request)(struct mddev *mddev, sector_t sector_nr, int *skipped);
503 int (*resize) (struct mddev *mddev, sector_t sectors);
504 sector_t (*size) (struct mddev *mddev, sector_t sectors, int raid_disks);
505 int (*check_reshape) (struct mddev *mddev);
506 int (*start_reshape) (struct mddev *mddev);
507 void (*finish_reshape) (struct mddev *mddev);
508 /* quiesce moves between quiescence states
510 * 1 - no new requests allowed
513 void (*quiesce) (struct mddev *mddev, int state);
514 /* takeover is used to transition an array from one
515 * personality to another. The new personality must be able
516 * to handle the data in the current layout.
517 * e.g. 2drive raid1 -> 2drive raid5
518 * ndrive raid5 -> degraded n+1drive raid6 with special layout
519 * If the takeover succeeds, a new 'private' structure is returned.
520 * This needs to be installed and then ->run used to activate the
523 void *(*takeover) (struct mddev *mddev);
524 /* congested implements bdi.congested_fn().
525 * Will not be called while array is 'suspended' */
526 int (*congested)(struct mddev *mddev, int bits);
529 struct md_sysfs_entry {
530 struct attribute attr;
531 ssize_t (*show)(struct mddev *, char *);
532 ssize_t (*store)(struct mddev *, const char *, size_t);
534 extern struct attribute_group md_bitmap_group;
536 static inline struct kernfs_node *sysfs_get_dirent_safe(struct kernfs_node *sd, char *name)
539 return sysfs_get_dirent(sd, name);
542 static inline void sysfs_notify_dirent_safe(struct kernfs_node *sd)
545 sysfs_notify_dirent(sd);
548 static inline char * mdname (struct mddev * mddev)
550 return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";
553 static inline int sysfs_link_rdev(struct mddev *mddev, struct md_rdev *rdev)
556 if (!test_bit(Replacement, &rdev->flags) && mddev->kobj.sd) {
557 sprintf(nm, "rd%d", rdev->raid_disk);
558 return sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
563 static inline void sysfs_unlink_rdev(struct mddev *mddev, struct md_rdev *rdev)
566 if (!test_bit(Replacement, &rdev->flags) && mddev->kobj.sd) {
567 sprintf(nm, "rd%d", rdev->raid_disk);
568 sysfs_remove_link(&mddev->kobj, nm);
573 * iterates through some rdev ringlist. It's safe to remove the
574 * current 'rdev'. Dont touch 'tmp' though.
576 #define rdev_for_each_list(rdev, tmp, head) \
577 list_for_each_entry_safe(rdev, tmp, head, same_set)
580 * iterates through the 'same array disks' ringlist
582 #define rdev_for_each(rdev, mddev) \
583 list_for_each_entry(rdev, &((mddev)->disks), same_set)
585 #define rdev_for_each_safe(rdev, tmp, mddev) \
586 list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set)
588 #define rdev_for_each_rcu(rdev, mddev) \
589 list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set)
592 void (*run) (struct md_thread *thread);
594 wait_queue_head_t wqueue;
596 struct task_struct *tsk;
597 unsigned long timeout;
601 #define THREAD_WAKEUP 0
603 static inline void safe_put_page(struct page *p)
608 extern int register_md_personality(struct md_personality *p);
609 extern int unregister_md_personality(struct md_personality *p);
610 extern int register_md_cluster_operations(struct md_cluster_operations *ops,
611 struct module *module);
612 extern int unregister_md_cluster_operations(void);
613 extern int md_setup_cluster(struct mddev *mddev, int nodes);
614 extern void md_cluster_stop(struct mddev *mddev);
615 extern struct md_thread *md_register_thread(
616 void (*run)(struct md_thread *thread),
619 extern void md_unregister_thread(struct md_thread **threadp);
620 extern void md_wakeup_thread(struct md_thread *thread);
621 extern void md_check_recovery(struct mddev *mddev);
622 extern void md_reap_sync_thread(struct mddev *mddev);
623 extern void md_write_start(struct mddev *mddev, struct bio *bi);
624 extern void md_write_end(struct mddev *mddev);
625 extern void md_done_sync(struct mddev *mddev, int blocks, int ok);
626 extern void md_error(struct mddev *mddev, struct md_rdev *rdev);
627 extern void md_finish_reshape(struct mddev *mddev);
629 extern int mddev_congested(struct mddev *mddev, int bits);
630 extern void md_flush_request(struct mddev *mddev, struct bio *bio);
631 extern void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
632 sector_t sector, int size, struct page *page);
633 extern void md_super_wait(struct mddev *mddev);
634 extern int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
635 struct page *page, int rw, bool metadata_op);
636 extern void md_do_sync(struct md_thread *thread);
637 extern void md_new_event(struct mddev *mddev);
638 extern int md_allow_write(struct mddev *mddev);
639 extern void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev);
640 extern void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors);
641 extern int md_check_no_bitmap(struct mddev *mddev);
642 extern int md_integrity_register(struct mddev *mddev);
643 extern void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev);
644 extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale);
646 extern void mddev_init(struct mddev *mddev);
647 extern int md_run(struct mddev *mddev);
648 extern void md_stop(struct mddev *mddev);
649 extern void md_stop_writes(struct mddev *mddev);
650 extern int md_rdev_init(struct md_rdev *rdev);
651 extern void md_rdev_clear(struct md_rdev *rdev);
653 extern void mddev_suspend(struct mddev *mddev);
654 extern void mddev_resume(struct mddev *mddev);
655 extern struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
656 struct mddev *mddev);
657 extern struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
658 struct mddev *mddev);
660 extern void md_unplug(struct blk_plug_cb *cb, bool from_schedule);
661 extern void md_reload_sb(struct mddev *mddev, int raid_disk);
662 extern void md_update_sb(struct mddev *mddev, int force);
663 extern void md_kick_rdev_from_array(struct md_rdev * rdev);
664 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr);
665 static inline int mddev_check_plugged(struct mddev *mddev)
667 return !!blk_check_plugged(md_unplug, mddev,
668 sizeof(struct blk_plug_cb));
671 static inline void rdev_dec_pending(struct md_rdev *rdev, struct mddev *mddev)
673 int faulty = test_bit(Faulty, &rdev->flags);
674 if (atomic_dec_and_test(&rdev->nr_pending) && faulty) {
675 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
676 md_wakeup_thread(mddev->thread);
680 extern struct md_cluster_operations *md_cluster_ops;
681 static inline int mddev_is_clustered(struct mddev *mddev)
683 return mddev->cluster_info && mddev->bitmap_info.nodes > 1;
685 #endif /* _MD_MD_H */