blob: 2514e5fcda7f79638390c3e60e781f13745a31a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md_k.h : kernel internal structure of the Linux MD driver
3 Copyright (C) 1996-98 Ingo Molnar, Gadi Oxman
4
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)
8 any later version.
9
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.
13*/
14
15#ifndef _MD_K_H
16#define _MD_K_H
17
NeilBrown06d91a52005-06-21 17:17:12 -070018/* and dm-bio-list.h is not under include/linux because.... ??? */
19#include "../../../drivers/md/dm-bio-list.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define MD_RESERVED 0UL
22#define LINEAR 1UL
23#define RAID0 2UL
24#define RAID1 3UL
25#define RAID5 4UL
26#define TRANSLUCENT 5UL
27#define HSM 6UL
28#define MULTIPATH 7UL
29#define RAID6 8UL
30#define RAID10 9UL
31#define FAULTY 10UL
32#define MAX_PERSONALITY 11UL
33
34#define LEVEL_MULTIPATH (-4)
35#define LEVEL_LINEAR (-1)
36#define LEVEL_FAULTY (-5)
37
38#define MaxSector (~(sector_t)0)
39#define MD_THREAD_NAME_MAX 14
40
41static inline int pers_to_level (int pers)
42{
43 switch (pers) {
44 case FAULTY: return LEVEL_FAULTY;
45 case MULTIPATH: return LEVEL_MULTIPATH;
46 case HSM: return -3;
47 case TRANSLUCENT: return -2;
48 case LINEAR: return LEVEL_LINEAR;
49 case RAID0: return 0;
50 case RAID1: return 1;
51 case RAID5: return 5;
52 case RAID6: return 6;
53 case RAID10: return 10;
54 }
55 BUG();
56 return MD_RESERVED;
57}
58
59static inline int level_to_pers (int level)
60{
61 switch (level) {
62 case LEVEL_FAULTY: return FAULTY;
63 case LEVEL_MULTIPATH: return MULTIPATH;
64 case -3: return HSM;
65 case -2: return TRANSLUCENT;
66 case LEVEL_LINEAR: return LINEAR;
67 case 0: return RAID0;
68 case 1: return RAID1;
69 case 4:
70 case 5: return RAID5;
71 case 6: return RAID6;
72 case 10: return RAID10;
73 }
74 return MD_RESERVED;
75}
76
77typedef struct mddev_s mddev_t;
78typedef struct mdk_rdev_s mdk_rdev_t;
79
80#define MAX_MD_DEVS 256 /* Max number of md dev */
81
82/*
83 * options passed in raidrun:
84 */
85
86#define MAX_CHUNK_SIZE (4096*1024)
87
88/*
89 * default readahead
90 */
91
92static inline int disk_faulty(mdp_disk_t * d)
93{
94 return d->state & (1 << MD_DISK_FAULTY);
95}
96
97static inline int disk_active(mdp_disk_t * d)
98{
99 return d->state & (1 << MD_DISK_ACTIVE);
100}
101
102static inline int disk_sync(mdp_disk_t * d)
103{
104 return d->state & (1 << MD_DISK_SYNC);
105}
106
107static inline int disk_spare(mdp_disk_t * d)
108{
109 return !disk_sync(d) && !disk_active(d) && !disk_faulty(d);
110}
111
112static inline int disk_removed(mdp_disk_t * d)
113{
114 return d->state & (1 << MD_DISK_REMOVED);
115}
116
117static inline void mark_disk_faulty(mdp_disk_t * d)
118{
119 d->state |= (1 << MD_DISK_FAULTY);
120}
121
122static inline void mark_disk_active(mdp_disk_t * d)
123{
124 d->state |= (1 << MD_DISK_ACTIVE);
125}
126
127static inline void mark_disk_sync(mdp_disk_t * d)
128{
129 d->state |= (1 << MD_DISK_SYNC);
130}
131
132static inline void mark_disk_spare(mdp_disk_t * d)
133{
134 d->state = 0;
135}
136
137static inline void mark_disk_removed(mdp_disk_t * d)
138{
139 d->state = (1 << MD_DISK_FAULTY) | (1 << MD_DISK_REMOVED);
140}
141
142static inline void mark_disk_inactive(mdp_disk_t * d)
143{
144 d->state &= ~(1 << MD_DISK_ACTIVE);
145}
146
147static inline void mark_disk_nonsync(mdp_disk_t * d)
148{
149 d->state &= ~(1 << MD_DISK_SYNC);
150}
151
152/*
153 * MD's 'extended' device
154 */
155struct mdk_rdev_s
156{
157 struct list_head same_set; /* RAID devices within the same set */
158
159 sector_t size; /* Device size (in blocks) */
160 mddev_t *mddev; /* RAID array if running */
161 unsigned long last_events; /* IO event timestamp */
162
163 struct block_device *bdev; /* block device handle */
164
165 struct page *sb_page;
166 int sb_loaded;
167 sector_t data_offset; /* start of data in array */
168 sector_t sb_offset;
169 int preferred_minor; /* autorun support */
170
171 /* A device can be in one of three states based on two flags:
172 * Not working: faulty==1 in_sync==0
173 * Fully working: faulty==0 in_sync==1
174 * Working, but not
175 * in sync with array
176 * faulty==0 in_sync==0
177 *
178 * It can never have faulty==1, in_sync==1
179 * This reduces the burden of testing multiple flags in many cases
180 */
181 int faulty; /* if faulty do not issue IO requests */
182 int in_sync; /* device is a full member of the array */
183
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700184 unsigned long flags; /* Should include faulty and in_sync here. */
185#define WriteMostly 4 /* Avoid reading if at all possible */
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int desc_nr; /* descriptor index in the superblock */
188 int raid_disk; /* role of device in array */
NeilBrown41158c72005-06-21 17:17:25 -0700189 int saved_raid_disk; /* role that device used to have in the
190 * array and could again if we did a partial
191 * resync from the bitmap
192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 atomic_t nr_pending; /* number of pending requests.
195 * only maintained for arrays that
196 * support hot removal
197 */
198};
199
200typedef struct mdk_personality_s mdk_personality_t;
201
202struct mddev_s
203{
204 void *private;
205 mdk_personality_t *pers;
206 dev_t unit;
207 int md_minor;
208 struct list_head disks;
209 int sb_dirty;
210 int ro;
211
212 struct gendisk *gendisk;
213
214 /* Superblock information */
215 int major_version,
216 minor_version,
217 patch_version;
218 int persistent;
219 int chunk_size;
220 time_t ctime, utime;
221 int level, layout;
222 int raid_disks;
223 int max_disks;
224 sector_t size; /* used size of component devices */
225 sector_t array_size; /* exported array size */
226 __u64 events;
227
228 char uuid[16];
229
230 struct mdk_thread_s *thread; /* management thread */
231 struct mdk_thread_s *sync_thread; /* doing resync or reconstruct */
232 sector_t curr_resync; /* blocks scheduled */
233 unsigned long resync_mark; /* a recent timestamp */
234 sector_t resync_mark_cnt;/* blocks written at resync_mark */
235
236 sector_t resync_max_sectors; /* may be set by personality */
237 /* recovery/resync flags
238 * NEEDED: we might need to start a resync/recover
239 * RUNNING: a thread is running, or about to be started
240 * SYNC: actually doing a resync, not a recovery
241 * ERR: and IO error was detected - abort the resync/recovery
242 * INTR: someone requested a (clean) early abort.
243 * DONE: thread is done and is waiting to be reaped
244 */
245#define MD_RECOVERY_RUNNING 0
246#define MD_RECOVERY_SYNC 1
247#define MD_RECOVERY_ERR 2
248#define MD_RECOVERY_INTR 3
249#define MD_RECOVERY_DONE 4
250#define MD_RECOVERY_NEEDED 5
251 unsigned long recovery;
252
253 int in_sync; /* know to not need resync */
254 struct semaphore reconfig_sem;
255 atomic_t active;
256
257 int changed; /* true if we might need to reread partition info */
258 int degraded; /* whether md should consider
259 * adding a spare
260 */
261
262 atomic_t recovery_active; /* blocks scheduled, but not written */
263 wait_queue_head_t recovery_wait;
264 sector_t recovery_cp;
NeilBrown06d91a52005-06-21 17:17:12 -0700265
266 spinlock_t write_lock;
NeilBrown3d310eb2005-06-21 17:17:26 -0700267 wait_queue_head_t sb_wait; /* for waiting on superblock updates */
NeilBrown7bfa19f2005-06-21 17:17:28 -0700268 atomic_t pending_writes; /* number of active superblock writes */
NeilBrown06d91a52005-06-21 17:17:12 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned int safemode; /* if set, update "clean" superblock
271 * when no writes pending.
272 */
273 unsigned int safemode_delay;
274 struct timer_list safemode_timer;
275 atomic_t writes_pending;
276 request_queue_t *queue; /* for plugging ... */
277
NeilBrown4b6d2872005-09-09 16:23:47 -0700278 atomic_t write_behind; /* outstanding async IO */
279 unsigned int max_write_behind; /* 0 = sync */
280
NeilBrown32a76272005-06-21 17:17:14 -0700281 struct bitmap *bitmap; /* the bitmap for the device */
282 struct file *bitmap_file; /* the bitmap file */
NeilBrowna654b9d82005-06-21 17:17:27 -0700283 long bitmap_offset; /* offset from superblock of
284 * start of bitmap. May be
285 * negative, but not '0'
286 */
NeilBrown36fa3062005-09-09 16:23:45 -0700287 long default_bitmap_offset; /* this is the offset to use when
288 * hot-adding a bitmap. It should
289 * eventually be settable by sysfs.
290 */
NeilBrown32a76272005-06-21 17:17:14 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct list_head all_mddevs;
293};
294
295
296static inline void rdev_dec_pending(mdk_rdev_t *rdev, mddev_t *mddev)
297{
298 int faulty = rdev->faulty;
299 if (atomic_dec_and_test(&rdev->nr_pending) && faulty)
300 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
301}
302
303static inline void md_sync_acct(struct block_device *bdev, unsigned long nr_sectors)
304{
305 atomic_add(nr_sectors, &bdev->bd_contains->bd_disk->sync_io);
306}
307
308struct mdk_personality_s
309{
310 char *name;
311 struct module *owner;
312 int (*make_request)(request_queue_t *q, struct bio *bio);
313 int (*run)(mddev_t *mddev);
314 int (*stop)(mddev_t *mddev);
315 void (*status)(struct seq_file *seq, mddev_t *mddev);
316 /* error_handler must set ->faulty and clear ->in_sync
317 * if appropriate, and should abort recovery if needed
318 */
319 void (*error_handler)(mddev_t *mddev, mdk_rdev_t *rdev);
320 int (*hot_add_disk) (mddev_t *mddev, mdk_rdev_t *rdev);
321 int (*hot_remove_disk) (mddev_t *mddev, int number);
322 int (*spare_active) (mddev_t *mddev);
NeilBrown57afd892005-06-21 17:17:13 -0700323 sector_t (*sync_request)(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int (*resize) (mddev_t *mddev, sector_t sectors);
325 int (*reshape) (mddev_t *mddev, int raid_disks);
326 int (*reconfig) (mddev_t *mddev, int layout, int chunk_size);
NeilBrown36fa3062005-09-09 16:23:45 -0700327 /* quiesce moves between quiescence states
328 * 0 - fully active
329 * 1 - no new requests allowed
330 * others - reserved
331 */
332 void (*quiesce) (mddev_t *mddev, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
335
336static inline char * mdname (mddev_t * mddev)
337{
338 return mddev->gendisk ? mddev->gendisk->disk_name : "mdX";
339}
340
341extern mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr);
342
343/*
344 * iterates through some rdev ringlist. It's safe to remove the
345 * current 'rdev'. Dont touch 'tmp' though.
346 */
347#define ITERATE_RDEV_GENERIC(head,rdev,tmp) \
348 \
349 for ((tmp) = (head).next; \
350 (rdev) = (list_entry((tmp), mdk_rdev_t, same_set)), \
351 (tmp) = (tmp)->next, (tmp)->prev != &(head) \
352 ; )
353/*
354 * iterates through the 'same array disks' ringlist
355 */
356#define ITERATE_RDEV(mddev,rdev,tmp) \
357 ITERATE_RDEV_GENERIC((mddev)->disks,rdev,tmp)
358
359/*
360 * Iterates through 'pending RAID disks'
361 */
362#define ITERATE_RDEV_PENDING(rdev,tmp) \
363 ITERATE_RDEV_GENERIC(pending_raid_disks,rdev,tmp)
364
365typedef struct mdk_thread_s {
366 void (*run) (mddev_t *mddev);
367 mddev_t *mddev;
368 wait_queue_head_t wqueue;
369 unsigned long flags;
370 struct completion *event;
371 struct task_struct *tsk;
NeilBrown32a76272005-06-21 17:17:14 -0700372 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 const char *name;
374} mdk_thread_t;
375
376#define THREAD_WAKEUP 0
377
378#define __wait_event_lock_irq(wq, condition, lock, cmd) \
379do { \
380 wait_queue_t __wait; \
381 init_waitqueue_entry(&__wait, current); \
382 \
383 add_wait_queue(&wq, &__wait); \
384 for (;;) { \
385 set_current_state(TASK_UNINTERRUPTIBLE); \
386 if (condition) \
387 break; \
388 spin_unlock_irq(&lock); \
389 cmd; \
390 schedule(); \
391 spin_lock_irq(&lock); \
392 } \
393 current->state = TASK_RUNNING; \
394 remove_wait_queue(&wq, &__wait); \
395} while (0)
396
397#define wait_event_lock_irq(wq, condition, lock, cmd) \
398do { \
399 if (condition) \
400 break; \
401 __wait_event_lock_irq(wq, condition, lock, cmd); \
402} while (0)
403
404#endif
405