xref: /linux/drivers/md/md-bitmap.h (revision 55ab7e14222e5f0b0fd9f7711ca391d2924b35e3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * bitmap.h: Copyright (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
4  *
5  * additions: Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
6  */
7 #ifndef BITMAP_H
8 #define BITMAP_H 1
9 
10 #define BITMAP_MAGIC 0x6d746962
11 
12 /*
13  * version 3 is host-endian order, this is deprecated and not used for new
14  * array
15  */
16 #define BITMAP_MAJOR_LO		3
17 #define BITMAP_MAJOR_HOSTENDIAN	3
18 /* version 4 is little-endian order, the default value */
19 #define BITMAP_MAJOR_HI		4
20 /* version 5 is only used for cluster */
21 #define BITMAP_MAJOR_CLUSTERED	5
22 /* version 6 is only used for lockless bitmap */
23 #define BITMAP_MAJOR_LOCKLESS	6
24 
25 /* use these for bitmap->flags and bitmap->sb->state bit-fields */
26 enum bitmap_state {
27 	BITMAP_STALE	   = 1, /* the bitmap file is out of date or had -EIO */
28 	BITMAP_WRITE_ERROR = 2, /* A write error has occurred */
29 	BITMAP_FIRST_USE   = 3, /* llbitmap is just created */
30 	BITMAP_CLEAN       = 4, /* llbitmap is created with assume_clean */
31 	BITMAP_DAEMON_BUSY = 5, /* llbitmap daemon is not finished after daemon_sleep */
32 	BITMAP_SHUTDOWN    = 6, /* llbitmap is being destroyed */
33 	BITMAP_HOSTENDIAN  =15,
34 };
35 
36 /* the superblock at the front of the bitmap file -- little endian */
37 typedef struct bitmap_super_s {
38 	__le32 magic;        /*  0  BITMAP_MAGIC */
39 	__le32 version;      /*  4  the bitmap major for now, could change... */
40 	__u8  uuid[16];      /*  8  128 bit uuid - must match md device uuid */
41 	__le64 events;       /* 24  event counter for the bitmap (1)*/
42 	__le64 events_cleared;/*32  event counter when last bit cleared (2) */
43 	__le64 sync_size;    /* 40  the size of the md device's sync range(3) */
44 	__le32 state;        /* 48  bitmap state information */
45 	__le32 chunksize;    /* 52  the bitmap chunk size in bytes */
46 	__le32 daemon_sleep; /* 56  seconds between disk flushes */
47 	__le32 write_behind; /* 60  number of outstanding write-behind writes */
48 	__le32 sectors_reserved; /* 64 number of 512-byte sectors that are
49 				  * reserved for the bitmap. */
50 	__le32 nodes;        /* 68 the maximum number of nodes in cluster. */
51 	__u8 cluster_name[64]; /* 72 cluster name to which this md belongs */
52 	__u8  pad[256 - 136]; /* set to zero */
53 } bitmap_super_t;
54 
55 /* notes:
56  * (1) This event counter is updated before the eventcounter in the md superblock
57  *    When a bitmap is loaded, it is only accepted if this event counter is equal
58  *    to, or one greater than, the event counter in the superblock.
59  * (2) This event counter is updated when the other one is *if*and*only*if* the
60  *    array is not degraded.  As bits are not cleared when the array is degraded,
61  *    this represents the last time that any bits were cleared.
62  *    If a device is being added that has an event count with this value or
63  *    higher, it is accepted as conforming to the bitmap.
64  * (3)This is the number of sectors represented by the bitmap, and is the range that
65  *    resync happens across.  For raid1 and raid5/6 it is the size of individual
66  *    devices.  For raid10 it is the size of the array.
67  */
68 
69 struct md_bitmap_stats {
70 	u64		events_cleared;
71 	int		behind_writes;
72 	bool		behind_wait;
73 
74 	unsigned long	missing_pages;
75 	unsigned long	file_pages;
76 	unsigned long	sync_size;
77 	unsigned long	pages;
78 	struct file	*file;
79 };
80 
81 typedef void (md_bitmap_fn)(struct mddev *mddev, sector_t offset,
82 			    unsigned long sectors);
83 
84 struct bitmap_operations {
85 	struct md_submodule_head head;
86 
87 	bool (*enabled)(void *data, bool flush);
88 	int (*create)(struct mddev *mddev);
89 	int (*resize)(struct mddev *mddev, sector_t blocks, int chunksize);
90 
91 	int (*load)(struct mddev *mddev);
92 	void (*destroy)(struct mddev *mddev);
93 	void (*flush)(struct mddev *mddev);
94 	void (*write_all)(struct mddev *mddev);
95 	void (*dirty_bits)(struct mddev *mddev, unsigned long s,
96 			   unsigned long e);
97 	/* Prepare a range for this bitmap implementation. */
98 	void (*prepare_range)(struct mddev *mddev,
99 			      sector_t *offset,
100 			      unsigned long *sectors,
101 			      bool discard);
102 	void (*reshape_finish)(struct mddev *mddev);
103 	int (*reshape_can_start)(struct mddev *mddev);
104 	void (*reshape_mark)(struct mddev *mddev, sector_t old_pos,
105 			     sector_t new_pos);
106 	void (*unplug)(struct mddev *mddev, bool sync);
107 	void (*daemon_work)(struct mddev *mddev);
108 
109 	void (*start_behind_write)(struct mddev *mddev);
110 	void (*end_behind_write)(struct mddev *mddev);
111 	void (*wait_behind_writes)(struct mddev *mddev);
112 
113 	md_bitmap_fn *start_write;
114 	md_bitmap_fn *end_write;
115 	md_bitmap_fn *start_discard;
116 	md_bitmap_fn *end_discard;
117 
118 	sector_t (*skip_sync_blocks)(struct mddev *mddev, sector_t offset);
119 	bool (*blocks_synced)(struct mddev *mddev, sector_t offset);
120 	bool (*start_sync)(struct mddev *mddev, sector_t offset,
121 			   sector_t *blocks, bool degraded);
122 	void (*end_sync)(struct mddev *mddev, sector_t offset, sector_t *blocks);
123 	void (*cond_end_sync)(struct mddev *mddev, sector_t sector, bool force);
124 	void (*close_sync)(struct mddev *mddev);
125 
126 	void (*update_sb)(void *data);
127 	int (*get_stats)(void *data, struct md_bitmap_stats *stats);
128 
129 	void (*sync_with_cluster)(struct mddev *mddev,
130 				  sector_t old_lo, sector_t old_hi,
131 				  sector_t new_lo, sector_t new_hi);
132 	void *(*get_from_slot)(struct mddev *mddev, int slot);
133 	int (*copy_from_slot)(struct mddev *mddev, int slot, sector_t *lo,
134 			      sector_t *hi, bool clear_bits);
135 	void (*set_pages)(void *data, unsigned long pages);
136 	void (*free)(void *data);
137 
138 	const struct attribute_group **groups;
139 };
140 
141 /* the bitmap API */
md_bitmap_registered(struct mddev * mddev)142 static inline bool md_bitmap_registered(struct mddev *mddev)
143 {
144 	return mddev->bitmap_ops != NULL;
145 }
146 
md_bitmap_enabled(struct mddev * mddev,bool flush)147 static inline bool md_bitmap_enabled(struct mddev *mddev, bool flush)
148 {
149 	/* bitmap_ops must be registered before creating bitmap. */
150 	if (!md_bitmap_registered(mddev))
151 		return false;
152 
153 	if (!mddev->bitmap)
154 		return false;
155 
156 	return mddev->bitmap_ops->enabled(mddev->bitmap, flush);
157 }
158 
md_bitmap_start_sync(struct mddev * mddev,sector_t offset,sector_t * blocks,bool degraded)159 static inline bool md_bitmap_start_sync(struct mddev *mddev, sector_t offset,
160 					sector_t *blocks, bool degraded)
161 {
162 	/* always resync if no bitmap */
163 	if (!md_bitmap_enabled(mddev, false)) {
164 		*blocks = 1024;
165 		return true;
166 	}
167 
168 	return mddev->bitmap_ops->start_sync(mddev, offset, blocks, degraded);
169 }
170 
md_bitmap_end_sync(struct mddev * mddev,sector_t offset,sector_t * blocks)171 static inline void md_bitmap_end_sync(struct mddev *mddev, sector_t offset,
172 				      sector_t *blocks)
173 {
174 	if (!md_bitmap_enabled(mddev, false)) {
175 		*blocks = 1024;
176 		return;
177 	}
178 
179 	mddev->bitmap_ops->end_sync(mddev, offset, blocks);
180 }
181 
182 #ifdef CONFIG_MD_BITMAP
183 int md_bitmap_init(void);
184 void md_bitmap_exit(void);
185 #else
md_bitmap_init(void)186 static inline int md_bitmap_init(void)
187 {
188 	return 0;
189 }
md_bitmap_exit(void)190 static inline void md_bitmap_exit(void)
191 {
192 }
193 #endif
194 
195 #ifdef CONFIG_MD_LLBITMAP
196 int md_llbitmap_init(void);
197 void md_llbitmap_exit(void);
198 #else
md_llbitmap_init(void)199 static inline int md_llbitmap_init(void)
200 {
201 	return 0;
202 }
md_llbitmap_exit(void)203 static inline void md_llbitmap_exit(void)
204 {
205 }
206 #endif
207 
208 #endif
209