xref: /linux/drivers/md/md-bitmap.h (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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_HOSTENDIAN  =15,
33 };
34 
35 /* the superblock at the front of the bitmap file -- little endian */
36 typedef struct bitmap_super_s {
37 	__le32 magic;        /*  0  BITMAP_MAGIC */
38 	__le32 version;      /*  4  the bitmap major for now, could change... */
39 	__u8  uuid[16];      /*  8  128 bit uuid - must match md device uuid */
40 	__le64 events;       /* 24  event counter for the bitmap (1)*/
41 	__le64 events_cleared;/*32  event counter when last bit cleared (2) */
42 	__le64 sync_size;    /* 40  the size of the md device's sync range(3) */
43 	__le32 state;        /* 48  bitmap state information */
44 	__le32 chunksize;    /* 52  the bitmap chunk size in bytes */
45 	__le32 daemon_sleep; /* 56  seconds between disk flushes */
46 	__le32 write_behind; /* 60  number of outstanding write-behind writes */
47 	__le32 sectors_reserved; /* 64 number of 512-byte sectors that are
48 				  * reserved for the bitmap. */
49 	__le32 nodes;        /* 68 the maximum number of nodes in cluster. */
50 	__u8 cluster_name[64]; /* 72 cluster name to which this md belongs */
51 	__u8  pad[256 - 136]; /* set to zero */
52 } bitmap_super_t;
53 
54 /* notes:
55  * (1) This event counter is updated before the eventcounter in the md superblock
56  *    When a bitmap is loaded, it is only accepted if this event counter is equal
57  *    to, or one greater than, the event counter in the superblock.
58  * (2) This event counter is updated when the other one is *if*and*only*if* the
59  *    array is not degraded.  As bits are not cleared when the array is degraded,
60  *    this represents the last time that any bits were cleared.
61  *    If a device is being added that has an event count with this value or
62  *    higher, it is accepted as conforming to the bitmap.
63  * (3)This is the number of sectors represented by the bitmap, and is the range that
64  *    resync happens across.  For raid1 and raid5/6 it is the size of individual
65  *    devices.  For raid10 it is the size of the array.
66  */
67 
68 struct md_bitmap_stats {
69 	u64		events_cleared;
70 	int		behind_writes;
71 	bool		behind_wait;
72 
73 	unsigned long	missing_pages;
74 	unsigned long	file_pages;
75 	unsigned long	sync_size;
76 	unsigned long	pages;
77 	struct file	*file;
78 };
79 
80 typedef void (md_bitmap_fn)(struct mddev *mddev, sector_t offset,
81 			    unsigned long sectors);
82 
83 struct bitmap_operations {
84 	struct md_submodule_head head;
85 
86 	bool (*enabled)(void *data, bool flush);
87 	int (*create)(struct mddev *mddev);
88 	int (*resize)(struct mddev *mddev, sector_t blocks, int chunksize);
89 
90 	int (*load)(struct mddev *mddev);
91 	void (*destroy)(struct mddev *mddev);
92 	void (*flush)(struct mddev *mddev);
93 	void (*write_all)(struct mddev *mddev);
94 	void (*dirty_bits)(struct mddev *mddev, unsigned long s,
95 			   unsigned long e);
96 	void (*unplug)(struct mddev *mddev, bool sync);
97 	void (*daemon_work)(struct mddev *mddev);
98 
99 	void (*start_behind_write)(struct mddev *mddev);
100 	void (*end_behind_write)(struct mddev *mddev);
101 	void (*wait_behind_writes)(struct mddev *mddev);
102 
103 	md_bitmap_fn *start_write;
104 	md_bitmap_fn *end_write;
105 	md_bitmap_fn *start_discard;
106 	md_bitmap_fn *end_discard;
107 
108 	sector_t (*skip_sync_blocks)(struct mddev *mddev, sector_t offset);
109 	bool (*blocks_synced)(struct mddev *mddev, sector_t offset);
110 	bool (*start_sync)(struct mddev *mddev, sector_t offset,
111 			   sector_t *blocks, bool degraded);
112 	void (*end_sync)(struct mddev *mddev, sector_t offset, sector_t *blocks);
113 	void (*cond_end_sync)(struct mddev *mddev, sector_t sector, bool force);
114 	void (*close_sync)(struct mddev *mddev);
115 
116 	void (*update_sb)(void *data);
117 	int (*get_stats)(void *data, struct md_bitmap_stats *stats);
118 
119 	void (*sync_with_cluster)(struct mddev *mddev,
120 				  sector_t old_lo, sector_t old_hi,
121 				  sector_t new_lo, sector_t new_hi);
122 	void *(*get_from_slot)(struct mddev *mddev, int slot);
123 	int (*copy_from_slot)(struct mddev *mddev, int slot, sector_t *lo,
124 			      sector_t *hi, bool clear_bits);
125 	void (*set_pages)(void *data, unsigned long pages);
126 	void (*free)(void *data);
127 
128 	struct attribute_group *group;
129 };
130 
131 /* the bitmap API */
132 static inline bool md_bitmap_registered(struct mddev *mddev)
133 {
134 	return mddev->bitmap_ops != NULL;
135 }
136 
137 static inline bool md_bitmap_enabled(struct mddev *mddev, bool flush)
138 {
139 	/* bitmap_ops must be registered before creating bitmap. */
140 	if (!md_bitmap_registered(mddev))
141 		return false;
142 
143 	if (!mddev->bitmap)
144 		return false;
145 
146 	return mddev->bitmap_ops->enabled(mddev->bitmap, flush);
147 }
148 
149 static inline bool md_bitmap_start_sync(struct mddev *mddev, sector_t offset,
150 					sector_t *blocks, bool degraded)
151 {
152 	/* always resync if no bitmap */
153 	if (!md_bitmap_enabled(mddev, false)) {
154 		*blocks = 1024;
155 		return true;
156 	}
157 
158 	return mddev->bitmap_ops->start_sync(mddev, offset, blocks, degraded);
159 }
160 
161 static inline void md_bitmap_end_sync(struct mddev *mddev, sector_t offset,
162 				      sector_t *blocks)
163 {
164 	if (!md_bitmap_enabled(mddev, false)) {
165 		*blocks = 1024;
166 		return;
167 	}
168 
169 	mddev->bitmap_ops->end_sync(mddev, offset, blocks);
170 }
171 
172 #ifdef CONFIG_MD_BITMAP
173 int md_bitmap_init(void);
174 void md_bitmap_exit(void);
175 #else
176 static inline int md_bitmap_init(void)
177 {
178 	return 0;
179 }
180 static inline void md_bitmap_exit(void)
181 {
182 }
183 #endif
184 
185 #ifdef CONFIG_MD_LLBITMAP
186 int md_llbitmap_init(void);
187 void md_llbitmap_exit(void);
188 #else
189 static inline int md_llbitmap_init(void)
190 {
191 	return 0;
192 }
193 static inline void md_llbitmap_exit(void)
194 {
195 }
196 #endif
197 
198 #endif
199