/*1md_k.h : kernel internal structure of the Linux MD driver2Copyright (C) 1996-98 Ingo Molnar, Gadi Oxman34This program is free software; you can redistribute it and/or modify5it under the terms of the GNU General Public License as published by6the Free Software Foundation; either version 2, or (at your option)7any later version.89You should have received a copy of the GNU General Public License10(for example /usr/src/linux/COPYING); if not, write to the Free11Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.12*/1314#ifndef _MD_MD_H15#define _MD_MD_H1617#include <linux/blkdev.h>18#include <linux/kobject.h>19#include <linux/list.h>20#include <linux/mm.h>21#include <linux/mutex.h>22#include <linux/timer.h>23#include <linux/wait.h>24#include <linux/workqueue.h>2526#define MaxSector (~(sector_t)0)2728typedef struct mddev_s mddev_t;29typedef struct mdk_rdev_s mdk_rdev_t;3031/*32* MD's 'extended' device33*/34struct mdk_rdev_s35{36struct list_head same_set; /* RAID devices within the same set */3738sector_t sectors; /* Device size (in 512bytes sectors) */39mddev_t *mddev; /* RAID array if running */40int last_events; /* IO event timestamp */4142/*43* If meta_bdev is non-NULL, it means that a separate device is44* being used to store the metadata (superblock/bitmap) which45* would otherwise be contained on the same device as the data (bdev).46*/47struct block_device *meta_bdev;48struct block_device *bdev; /* block device handle */4950struct page *sb_page;51int sb_loaded;52__u64 sb_events;53sector_t data_offset; /* start of data in array */54sector_t sb_start; /* offset of the super block (in 512byte sectors) */55int sb_size; /* bytes in the superblock */56int preferred_minor; /* autorun support */5758struct kobject kobj;5960/* A device can be in one of three states based on two flags:61* Not working: faulty==1 in_sync==062* Fully working: faulty==0 in_sync==163* Working, but not64* in sync with array65* faulty==0 in_sync==066*67* It can never have faulty==1, in_sync==168* This reduces the burden of testing multiple flags in many cases69*/7071unsigned long flags;72#define Faulty 1 /* device is known to have a fault */73#define In_sync 2 /* device is in_sync with rest of array */74#define WriteMostly 4 /* Avoid reading if at all possible */75#define AutoDetected 7 /* added by auto-detect */76#define Blocked 8 /* An error occurred on an externally77* managed array, don't allow writes78* until it is cleared */79wait_queue_head_t blocked_wait;8081int desc_nr; /* descriptor index in the superblock */82int raid_disk; /* role of device in array */83int new_raid_disk; /* role that the device will have in84* the array after a level-change completes.85*/86int saved_raid_disk; /* role that device used to have in the87* array and could again if we did a partial88* resync from the bitmap89*/90sector_t recovery_offset;/* If this device has been partially91* recovered, this is where we were92* up to.93*/9495atomic_t nr_pending; /* number of pending requests.96* only maintained for arrays that97* support hot removal98*/99atomic_t read_errors; /* number of consecutive read errors that100* we have tried to ignore.101*/102struct timespec last_read_error; /* monotonic time since our103* last read error104*/105atomic_t corrected_errors; /* number of corrected read errors,106* for reporting to userspace and storing107* in superblock.108*/109struct work_struct del_work; /* used for delayed sysfs removal */110111struct sysfs_dirent *sysfs_state; /* handle for 'state'112* sysfs entry */113};114115struct mddev_s116{117void *private;118struct mdk_personality *pers;119dev_t unit;120int md_minor;121struct list_head disks;122unsigned long flags;123#define MD_CHANGE_DEVS 0 /* Some device status has changed */124#define MD_CHANGE_CLEAN 1 /* transition to or from 'clean' */125#define MD_CHANGE_PENDING 2 /* switch from 'clean' to 'active' in progress */126#define MD_ARRAY_FIRST_USE 3 /* First use of array, needs initialization */127128int suspended;129atomic_t active_io;130int ro;131int sysfs_active; /* set when sysfs deletes132* are happening, so run/133* takeover/stop are not safe134*/135int ready; /* See when safe to pass136* IO requests down */137struct gendisk *gendisk;138139struct kobject kobj;140int hold_active;141#define UNTIL_IOCTL 1142#define UNTIL_STOP 2143144/* Superblock information */145int major_version,146minor_version,147patch_version;148int persistent;149int external; /* metadata is150* managed externally */151char metadata_type[17]; /* externally set*/152int chunk_sectors;153time_t ctime, utime;154int level, layout;155char clevel[16];156int raid_disks;157int max_disks;158sector_t dev_sectors; /* used size of159* component devices */160sector_t array_sectors; /* exported array size */161int external_size; /* size managed162* externally */163__u64 events;164/* If the last 'event' was simply a clean->dirty transition, and165* we didn't write it to the spares, then it is safe and simple166* to just decrement the event count on a dirty->clean transition.167* So we record that possibility here.168*/169int can_decrease_events;170171char uuid[16];172173/* If the array is being reshaped, we need to record the174* new shape and an indication of where we are up to.175* This is written to the superblock.176* If reshape_position is MaxSector, then no reshape is happening (yet).177*/178sector_t reshape_position;179int delta_disks, new_level, new_layout;180int new_chunk_sectors;181182atomic_t plug_cnt; /* If device is expecting183* more bios soon.184*/185struct mdk_thread_s *thread; /* management thread */186struct mdk_thread_s *sync_thread; /* doing resync or reconstruct */187sector_t curr_resync; /* last block scheduled */188/* As resync requests can complete out of order, we cannot easily track189* how much resync has been completed. So we occasionally pause until190* everything completes, then set curr_resync_completed to curr_resync.191* As such it may be well behind the real resync mark, but it is a value192* we are certain of.193*/194sector_t curr_resync_completed;195unsigned long resync_mark; /* a recent timestamp */196sector_t resync_mark_cnt;/* blocks written at resync_mark */197sector_t curr_mark_cnt; /* blocks scheduled now */198199sector_t resync_max_sectors; /* may be set by personality */200201sector_t resync_mismatches; /* count of sectors where202* parity/replica mismatch found203*/204205/* allow user-space to request suspension of IO to regions of the array */206sector_t suspend_lo;207sector_t suspend_hi;208/* if zero, use the system-wide default */209int sync_speed_min;210int sync_speed_max;211212/* resync even though the same disks are shared among md-devices */213int parallel_resync;214215int ok_start_degraded;216/* recovery/resync flags217* NEEDED: we might need to start a resync/recover218* RUNNING: a thread is running, or about to be started219* SYNC: actually doing a resync, not a recovery220* RECOVER: doing recovery, or need to try it.221* INTR: resync needs to be aborted for some reason222* DONE: thread is done and is waiting to be reaped223* REQUEST: user-space has requested a sync (used with SYNC)224* CHECK: user-space request for check-only, no repair225* RESHAPE: A reshape is happening226*227* If neither SYNC or RESHAPE are set, then it is a recovery.228*/229#define MD_RECOVERY_RUNNING 0230#define MD_RECOVERY_SYNC 1231#define MD_RECOVERY_RECOVER 2232#define MD_RECOVERY_INTR 3233#define MD_RECOVERY_DONE 4234#define MD_RECOVERY_NEEDED 5235#define MD_RECOVERY_REQUESTED 6236#define MD_RECOVERY_CHECK 7237#define MD_RECOVERY_RESHAPE 8238#define MD_RECOVERY_FROZEN 9239240unsigned long recovery;241int recovery_disabled; /* if we detect that recovery242* will always fail, set this243* so we don't loop trying */244245int in_sync; /* know to not need resync */246/* 'open_mutex' avoids races between 'md_open' and 'do_md_stop', so247* that we are never stopping an array while it is open.248* 'reconfig_mutex' protects all other reconfiguration.249* These locks are separate due to conflicting interactions250* with bdev->bd_mutex.251* Lock ordering is:252* reconfig_mutex -> bd_mutex : e.g. do_md_run -> revalidate_disk253* bd_mutex -> open_mutex: e.g. __blkdev_get -> md_open254*/255struct mutex open_mutex;256struct mutex reconfig_mutex;257atomic_t active; /* general refcount */258atomic_t openers; /* number of active opens */259260int changed; /* True if we might need to261* reread partition info */262int degraded; /* whether md should consider263* adding a spare264*/265266atomic_t recovery_active; /* blocks scheduled, but not written */267wait_queue_head_t recovery_wait;268sector_t recovery_cp;269sector_t resync_min; /* user requested sync270* starts here */271sector_t resync_max; /* resync should pause272* when it gets here */273274struct sysfs_dirent *sysfs_state; /* handle for 'array_state'275* file in sysfs.276*/277struct sysfs_dirent *sysfs_action; /* handle for 'sync_action' */278279struct work_struct del_work; /* used for delayed sysfs removal */280281spinlock_t write_lock;282wait_queue_head_t sb_wait; /* for waiting on superblock updates */283atomic_t pending_writes; /* number of active superblock writes */284285unsigned int safemode; /* if set, update "clean" superblock286* when no writes pending.287*/288unsigned int safemode_delay;289struct timer_list safemode_timer;290atomic_t writes_pending;291struct request_queue *queue; /* for plugging ... */292293struct bitmap *bitmap; /* the bitmap for the device */294struct {295struct file *file; /* the bitmap file */296loff_t offset; /* offset from superblock of297* start of bitmap. May be298* negative, but not '0'299* For external metadata, offset300* from start of device.301*/302loff_t default_offset; /* this is the offset to use when303* hot-adding a bitmap. It should304* eventually be settable by sysfs.305*/306/* When md is serving under dm, it might use a307* dirty_log to store the bits.308*/309struct dm_dirty_log *log;310311struct mutex mutex;312unsigned long chunksize;313unsigned long daemon_sleep; /* how many jiffies between updates? */314unsigned long max_write_behind; /* write-behind mode */315int external;316} bitmap_info;317318atomic_t max_corr_read_errors; /* max read retries */319struct list_head all_mddevs;320321struct attribute_group *to_remove;322323struct bio_set *bio_set;324325/* Generic flush handling.326* The last to finish preflush schedules a worker to submit327* the rest of the request (without the REQ_FLUSH flag).328*/329struct bio *flush_bio;330atomic_t flush_pending;331struct work_struct flush_work;332struct work_struct event_work; /* used by dm to report failure event */333void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);334};335336337static inline void rdev_dec_pending(mdk_rdev_t *rdev, mddev_t *mddev)338{339int faulty = test_bit(Faulty, &rdev->flags);340if (atomic_dec_and_test(&rdev->nr_pending) && faulty)341set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);342}343344static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)345{346atomic_add(nr_sectors, &bdev->bd_contains->bd_disk->sync_io);347}348349struct mdk_personality350{351char *name;352int level;353struct list_head list;354struct module *owner;355int (*make_request)(mddev_t *mddev, struct bio *bio);356int (*run)(mddev_t *mddev);357int (*stop)(mddev_t *mddev);358void (*status)(struct seq_file *seq, mddev_t *mddev);359/* error_handler must set ->faulty and clear ->in_sync360* if appropriate, and should abort recovery if needed361*/362void (*error_handler)(mddev_t *mddev, mdk_rdev_t *rdev);363int (*hot_add_disk) (mddev_t *mddev, mdk_rdev_t *rdev);364int (*hot_remove_disk) (mddev_t *mddev, int number);365int (*spare_active) (mddev_t *mddev);366sector_t (*sync_request)(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster);367int (*resize) (mddev_t *mddev, sector_t sectors);368sector_t (*size) (mddev_t *mddev, sector_t sectors, int raid_disks);369int (*check_reshape) (mddev_t *mddev);370int (*start_reshape) (mddev_t *mddev);371void (*finish_reshape) (mddev_t *mddev);372/* quiesce moves between quiescence states373* 0 - fully active374* 1 - no new requests allowed375* others - reserved376*/377void (*quiesce) (mddev_t *mddev, int state);378/* takeover is used to transition an array from one379* personality to another. The new personality must be able380* to handle the data in the current layout.381* e.g. 2drive raid1 -> 2drive raid5382* ndrive raid5 -> degraded n+1drive raid6 with special layout383* If the takeover succeeds, a new 'private' structure is returned.384* This needs to be installed and then ->run used to activate the385* array.386*/387void *(*takeover) (mddev_t *mddev);388};389390391struct md_sysfs_entry {392struct attribute attr;393ssize_t (*show)(mddev_t *, char *);394ssize_t (*store)(mddev_t *, const char *, size_t);395};396extern struct attribute_group md_bitmap_group;397398static inline struct sysfs_dirent *sysfs_get_dirent_safe(struct sysfs_dirent *sd, char *name)399{400if (sd)401return sysfs_get_dirent(sd, NULL, name);402return sd;403}404static inline void sysfs_notify_dirent_safe(struct sysfs_dirent *sd)405{406if (sd)407sysfs_notify_dirent(sd);408}409410static inline char * mdname (mddev_t * mddev)411{412return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";413}414415/*416* iterates through some rdev ringlist. It's safe to remove the417* current 'rdev'. Dont touch 'tmp' though.418*/419#define rdev_for_each_list(rdev, tmp, head) \420list_for_each_entry_safe(rdev, tmp, head, same_set)421422/*423* iterates through the 'same array disks' ringlist424*/425#define rdev_for_each(rdev, tmp, mddev) \426list_for_each_entry_safe(rdev, tmp, &((mddev)->disks), same_set)427428#define rdev_for_each_rcu(rdev, mddev) \429list_for_each_entry_rcu(rdev, &((mddev)->disks), same_set)430431typedef struct mdk_thread_s {432void (*run) (mddev_t *mddev);433mddev_t *mddev;434wait_queue_head_t wqueue;435unsigned long flags;436struct task_struct *tsk;437unsigned long timeout;438} mdk_thread_t;439440#define THREAD_WAKEUP 0441442#define __wait_event_lock_irq(wq, condition, lock, cmd) \443do { \444wait_queue_t __wait; \445init_waitqueue_entry(&__wait, current); \446\447add_wait_queue(&wq, &__wait); \448for (;;) { \449set_current_state(TASK_UNINTERRUPTIBLE); \450if (condition) \451break; \452spin_unlock_irq(&lock); \453cmd; \454schedule(); \455spin_lock_irq(&lock); \456} \457current->state = TASK_RUNNING; \458remove_wait_queue(&wq, &__wait); \459} while (0)460461#define wait_event_lock_irq(wq, condition, lock, cmd) \462do { \463if (condition) \464break; \465__wait_event_lock_irq(wq, condition, lock, cmd); \466} while (0)467468static inline void safe_put_page(struct page *p)469{470if (p) put_page(p);471}472473extern int register_md_personality(struct mdk_personality *p);474extern int unregister_md_personality(struct mdk_personality *p);475extern mdk_thread_t * md_register_thread(void (*run) (mddev_t *mddev),476mddev_t *mddev, const char *name);477extern void md_unregister_thread(mdk_thread_t *thread);478extern void md_wakeup_thread(mdk_thread_t *thread);479extern void md_check_recovery(mddev_t *mddev);480extern void md_write_start(mddev_t *mddev, struct bio *bi);481extern void md_write_end(mddev_t *mddev);482extern void md_done_sync(mddev_t *mddev, int blocks, int ok);483extern void md_error(mddev_t *mddev, mdk_rdev_t *rdev);484485extern int mddev_congested(mddev_t *mddev, int bits);486extern void md_flush_request(mddev_t *mddev, struct bio *bio);487extern void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,488sector_t sector, int size, struct page *page);489extern void md_super_wait(mddev_t *mddev);490extern int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,491struct page *page, int rw, bool metadata_op);492extern void md_do_sync(mddev_t *mddev);493extern void md_new_event(mddev_t *mddev);494extern int md_allow_write(mddev_t *mddev);495extern void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev);496extern void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors);497extern int md_check_no_bitmap(mddev_t *mddev);498extern int md_integrity_register(mddev_t *mddev);499extern void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev);500extern int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale);501extern void restore_bitmap_write_access(struct file *file);502503extern void mddev_init(mddev_t *mddev);504extern int md_run(mddev_t *mddev);505extern void md_stop(mddev_t *mddev);506extern void md_stop_writes(mddev_t *mddev);507extern void md_rdev_init(mdk_rdev_t *rdev);508509extern void mddev_suspend(mddev_t *mddev);510extern void mddev_resume(mddev_t *mddev);511extern struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,512mddev_t *mddev);513extern struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,514mddev_t *mddev);515extern int mddev_check_plugged(mddev_t *mddev);516#endif /* _MD_MD_H */517518519