xref: /linux/drivers/md/raid1.h (revision c40f341f1e7fd4eddcfc5881d94cfa8669071ee6)
1ef740c37SChristoph Hellwig #ifndef _RAID1_H
2ef740c37SChristoph Hellwig #define _RAID1_H
3ef740c37SChristoph Hellwig 
40eaf822cSJonathan Brassow struct raid1_info {
53cb03002SNeilBrown 	struct md_rdev	*rdev;
6ef740c37SChristoph Hellwig 	sector_t	head_position;
7be4d3280SShaohua Li 
8be4d3280SShaohua Li 	/* When choose the best device for a read (read_balance())
9be4d3280SShaohua Li 	 * we try to keep sequential reads one the same device
10be4d3280SShaohua Li 	 */
11be4d3280SShaohua Li 	sector_t	next_seq_sect;
1212cee5a8SShaohua Li 	sector_t	seq_start;
13ef740c37SChristoph Hellwig };
14ef740c37SChristoph Hellwig 
15ef740c37SChristoph Hellwig /*
16ef740c37SChristoph Hellwig  * memory pools need a pointer to the mddev, so they can force an unplug
17ef740c37SChristoph Hellwig  * when memory is tight, and a count of the number of drives that the
18ef740c37SChristoph Hellwig  * pool was allocated for, so they know how much to allocate and free.
19ef740c37SChristoph Hellwig  * mddev->raid_disks cannot be used, as it can change while a pool is active
20ef740c37SChristoph Hellwig  * These two datums are stored in a kmalloced struct.
218f19ccb2SNeilBrown  * The 'raid_disks' here is twice the raid_disks in r1conf.
228f19ccb2SNeilBrown  * This allows space for each 'real' device can have a replacement in the
238f19ccb2SNeilBrown  * second half of the array.
24ef740c37SChristoph Hellwig  */
25ef740c37SChristoph Hellwig 
26ef740c37SChristoph Hellwig struct pool_info {
27fd01b88cSNeilBrown 	struct mddev *mddev;
28ef740c37SChristoph Hellwig 	int	raid_disks;
29ef740c37SChristoph Hellwig };
30ef740c37SChristoph Hellwig 
31e8096360SNeilBrown struct r1conf {
32fd01b88cSNeilBrown 	struct mddev		*mddev;
330eaf822cSJonathan Brassow 	struct raid1_info	*mirrors;	/* twice 'raid_disks' to
348f19ccb2SNeilBrown 						 * allow for replacements.
358f19ccb2SNeilBrown 						 */
36ef740c37SChristoph Hellwig 	int			raid_disks;
37ce550c20SNeilBrown 
38ce550c20SNeilBrown 	/* During resync, read_balancing is only allowed on the part
39ce550c20SNeilBrown 	 * of the array that has been resynced.  'next_resync' tells us
40ce550c20SNeilBrown 	 * where that is.
41ce550c20SNeilBrown 	 */
42ce550c20SNeilBrown 	sector_t		next_resync;
43ce550c20SNeilBrown 
4479ef3a8aSmajianpeng 	/* When raid1 starts resync, we divide array into four partitions
4579ef3a8aSmajianpeng 	 * |---------|--------------|---------------------|-------------|
4679ef3a8aSmajianpeng 	 *        next_resync   start_next_window       end_window
4779ef3a8aSmajianpeng 	 * start_next_window = next_resync + NEXT_NORMALIO_DISTANCE
4879ef3a8aSmajianpeng 	 * end_window = start_next_window + NEXT_NORMALIO_DISTANCE
4979ef3a8aSmajianpeng 	 * current_window_requests means the count of normalIO between
5079ef3a8aSmajianpeng 	 *   start_next_window and end_window.
5179ef3a8aSmajianpeng 	 * next_window_requests means the count of normalIO after end_window.
5279ef3a8aSmajianpeng 	 * */
5379ef3a8aSmajianpeng 	sector_t		start_next_window;
5479ef3a8aSmajianpeng 	int			current_window_requests;
5579ef3a8aSmajianpeng 	int			next_window_requests;
5679ef3a8aSmajianpeng 
57ef740c37SChristoph Hellwig 	spinlock_t		device_lock;
58ef740c37SChristoph Hellwig 
599f2c9d12SNeilBrown 	/* list of 'struct r1bio' that need to be processed by raid1d,
609f2c9d12SNeilBrown 	 * whether to retry a read, writeout a resync or recovery
619f2c9d12SNeilBrown 	 * block, or anything else.
62ce550c20SNeilBrown 	 */
63ef740c37SChristoph Hellwig 	struct list_head	retry_list;
6455ce74d4SNeilBrown 	/* A separate list of r1bio which just need raid_end_bio_io called.
6555ce74d4SNeilBrown 	 * This mustn't happen for writes which had any errors if the superblock
6655ce74d4SNeilBrown 	 * needs to be written.
6755ce74d4SNeilBrown 	 */
6855ce74d4SNeilBrown 	struct list_head	bio_end_io_list;
69ce550c20SNeilBrown 
70ce550c20SNeilBrown 	/* queue pending writes to be submitted on unplug */
71ef740c37SChristoph Hellwig 	struct bio_list		pending_bio_list;
7234db0cd6SNeilBrown 	int			pending_count;
73ef740c37SChristoph Hellwig 
74ce550c20SNeilBrown 	/* for use when syncing mirrors:
75ce550c20SNeilBrown 	 * We don't allow both normal IO and resync/recovery IO at
76ce550c20SNeilBrown 	 * the same time - resync/recovery can only happen when there
77ce550c20SNeilBrown 	 * is no other IO.  So when either is active, the other has to wait.
78ce550c20SNeilBrown 	 * See more details description in raid1.c near raise_barrier().
79ce550c20SNeilBrown 	 */
80ce550c20SNeilBrown 	wait_queue_head_t	wait_barrier;
81ef740c37SChristoph Hellwig 	spinlock_t		resync_lock;
82ef740c37SChristoph Hellwig 	int			nr_pending;
83ef740c37SChristoph Hellwig 	int			nr_waiting;
84ef740c37SChristoph Hellwig 	int			nr_queued;
85ef740c37SChristoph Hellwig 	int			barrier;
86b364e3d0Smajianpeng 	int			array_frozen;
87ce550c20SNeilBrown 
88ce550c20SNeilBrown 	/* Set to 1 if a full sync is needed, (fresh device added).
89ef740c37SChristoph Hellwig 	 * Cleared when a sync completes.
90ef740c37SChristoph Hellwig 	 */
91ce550c20SNeilBrown 	int			fullsync;
92ce550c20SNeilBrown 
93ce550c20SNeilBrown 	/* When the same as mddev->recovery_disabled we don't allow
94ce550c20SNeilBrown 	 * recovery to be attempted as we expect a read error.
955389042fSNeilBrown 	 */
96ce550c20SNeilBrown 	int			recovery_disabled;
97ef740c37SChristoph Hellwig 
98ce550c20SNeilBrown 	/* poolinfo contains information about the content of the
99ce550c20SNeilBrown 	 * mempools - it changes when the array grows or shrinks
100ce550c20SNeilBrown 	 */
101ef740c37SChristoph Hellwig 	struct pool_info	*poolinfo;
102ef740c37SChristoph Hellwig 	mempool_t		*r1bio_pool;
103ef740c37SChristoph Hellwig 	mempool_t		*r1buf_pool;
104709ae487SNeilBrown 
105ce550c20SNeilBrown 	/* temporary buffer to synchronous IO when attempting to repair
106ce550c20SNeilBrown 	 * a read error.
107ce550c20SNeilBrown 	 */
108ce550c20SNeilBrown 	struct page		*tmppage;
109ce550c20SNeilBrown 
110709ae487SNeilBrown 	/* When taking over an array from a different personality, we store
111709ae487SNeilBrown 	 * the new thread here until we fully activate the array.
112709ae487SNeilBrown 	 */
1132b8bf345SNeilBrown 	struct md_thread	*thread;
114*c40f341fSGoldwyn Rodrigues 
115*c40f341fSGoldwyn Rodrigues 	/* Keep track of cluster resync window to send to other
116*c40f341fSGoldwyn Rodrigues 	 * nodes.
117*c40f341fSGoldwyn Rodrigues 	 */
118*c40f341fSGoldwyn Rodrigues 	sector_t		cluster_sync_low;
119*c40f341fSGoldwyn Rodrigues 	sector_t		cluster_sync_high;
120*c40f341fSGoldwyn Rodrigues 
121ef740c37SChristoph Hellwig };
122ef740c37SChristoph Hellwig 
123ef740c37SChristoph Hellwig /*
124ef740c37SChristoph Hellwig  * this is our 'private' RAID1 bio.
125ef740c37SChristoph Hellwig  *
126ef740c37SChristoph Hellwig  * it contains information about what kind of IO operations were started
127ef740c37SChristoph Hellwig  * for this RAID1 operation, and about their status:
128ef740c37SChristoph Hellwig  */
129ef740c37SChristoph Hellwig 
1309f2c9d12SNeilBrown struct r1bio {
131ef740c37SChristoph Hellwig 	atomic_t		remaining; /* 'have we finished' count,
132ef740c37SChristoph Hellwig 					    * used from IRQ handlers
133ef740c37SChristoph Hellwig 					    */
134ef740c37SChristoph Hellwig 	atomic_t		behind_remaining; /* number of write-behind ios remaining
135ef740c37SChristoph Hellwig 						 * in this BehindIO request
136ef740c37SChristoph Hellwig 						 */
137ef740c37SChristoph Hellwig 	sector_t		sector;
13879ef3a8aSmajianpeng 	sector_t		start_next_window;
139ef740c37SChristoph Hellwig 	int			sectors;
140ef740c37SChristoph Hellwig 	unsigned long		state;
141fd01b88cSNeilBrown 	struct mddev		*mddev;
142ef740c37SChristoph Hellwig 	/*
143ef740c37SChristoph Hellwig 	 * original bio going to /dev/mdx
144ef740c37SChristoph Hellwig 	 */
145ef740c37SChristoph Hellwig 	struct bio		*master_bio;
146ef740c37SChristoph Hellwig 	/*
147ef740c37SChristoph Hellwig 	 * if the IO is in READ direction, then this is where we read
148ef740c37SChristoph Hellwig 	 */
149ef740c37SChristoph Hellwig 	int			read_disk;
150ef740c37SChristoph Hellwig 
151ef740c37SChristoph Hellwig 	struct list_head	retry_list;
152af6d7b76SNeilBrown 	/* Next two are only valid when R1BIO_BehindIO is set */
1532ca68f5eSNeilBrown 	struct bio_vec		*behind_bvecs;
154af6d7b76SNeilBrown 	int			behind_page_count;
155ef740c37SChristoph Hellwig 	/*
156ef740c37SChristoph Hellwig 	 * if the IO is in WRITE direction, then multiple bios are used.
157ef740c37SChristoph Hellwig 	 * We choose the number when they are allocated.
158ef740c37SChristoph Hellwig 	 */
159ef740c37SChristoph Hellwig 	struct bio		*bios[0];
160ef740c37SChristoph Hellwig 	/* DO NOT PUT ANY NEW FIELDS HERE - bios array is contiguously alloced*/
161ef740c37SChristoph Hellwig };
162ef740c37SChristoph Hellwig 
163ef740c37SChristoph Hellwig /* bits for r1bio.state */
164ef740c37SChristoph Hellwig #define	R1BIO_Uptodate	0
165ef740c37SChristoph Hellwig #define	R1BIO_IsSync	1
166ef740c37SChristoph Hellwig #define	R1BIO_Degraded	2
167ef740c37SChristoph Hellwig #define	R1BIO_BehindIO	3
168d2eb35acSNeilBrown /* Set ReadError on bios that experience a readerror so that
169d2eb35acSNeilBrown  * raid1d knows what to do with them.
170d2eb35acSNeilBrown  */
171d2eb35acSNeilBrown #define R1BIO_ReadError 4
172ef740c37SChristoph Hellwig /* For write-behind requests, we call bi_end_io when
173ef740c37SChristoph Hellwig  * the last non-write-behind device completes, providing
174ef740c37SChristoph Hellwig  * any write was successful.  Otherwise we call when
175ef740c37SChristoph Hellwig  * any write-behind write succeeds, otherwise we call
176ef740c37SChristoph Hellwig  * with failure when last write completes (and all failed).
177ef740c37SChristoph Hellwig  * Record that bi_end_io was called with this flag...
178ef740c37SChristoph Hellwig  */
179ef740c37SChristoph Hellwig #define	R1BIO_Returned 6
1804367af55SNeilBrown /* If a write for this request means we can clear some
1814367af55SNeilBrown  * known-bad-block records, we set this flag
1824367af55SNeilBrown  */
1834367af55SNeilBrown #define	R1BIO_MadeGood 7
184cd5ff9a1SNeilBrown #define	R1BIO_WriteError 8
185ef740c37SChristoph Hellwig #endif
186