]>
Commit | Line | Data |
---|---|---|
c82f047c NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2004 Paul Clements, SteelEye Technology, Inc. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | ||
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include "mdadm.h" | |
c82f047c NB |
24 | |
25 | #define min(a,b) (((a) < (b)) ? (a) : (b)) | |
26 | ||
27 | inline void sb_le_to_cpu(bitmap_super_t *sb) | |
28 | { | |
29 | sb->magic = __le32_to_cpu(sb->magic); | |
30 | sb->version = __le32_to_cpu(sb->version); | |
31 | /* uuid gets no translation */ | |
32 | sb->events = __le64_to_cpu(sb->events); | |
33 | sb->events_cleared = __le64_to_cpu(sb->events_cleared); | |
34 | sb->state = __le32_to_cpu(sb->state); | |
35 | sb->chunksize = __le32_to_cpu(sb->chunksize); | |
36 | sb->daemon_sleep = __le32_to_cpu(sb->daemon_sleep); | |
37 | sb->sync_size = __le64_to_cpu(sb->sync_size); | |
38 | } | |
39 | ||
40 | inline void sb_cpu_to_le(bitmap_super_t *sb) | |
41 | { | |
42 | sb_le_to_cpu(sb); /* these are really the same thing */ | |
43 | } | |
44 | ||
45 | mapping_t bitmap_states[] = { | |
46 | { "OK", 0 }, | |
47 | { "Out of date", 2 }, | |
48 | { NULL, -1 } | |
49 | }; | |
50 | ||
51 | const char *bitmap_state(int state_num) | |
52 | { | |
53 | char *state = map_num(bitmap_states, state_num); | |
54 | return state ? state : "Unknown"; | |
55 | } | |
56 | ||
57 | const char *human_chunksize(unsigned long bytes) | |
58 | { | |
59 | static char buf[16]; | |
60 | char *suffixes[] = { "B", "KB", "MB", "GB", "TB", NULL }; | |
61 | int i = 0; | |
62 | ||
63 | while (bytes >> 10) { | |
64 | bytes >>= 10; | |
65 | i++; | |
66 | } | |
67 | ||
8f23b0b3 | 68 | snprintf(buf, sizeof(buf), "%lu %s", bytes, suffixes[i]); |
c82f047c NB |
69 | |
70 | return buf; | |
71 | } | |
72 | ||
73 | typedef struct bitmap_info_s { | |
74 | bitmap_super_t sb; | |
75 | unsigned long long total_bits; | |
76 | unsigned long long dirty_bits; | |
77 | } bitmap_info_t; | |
78 | ||
79 | /* count the dirty bits in the first num_bits of byte */ | |
80 | inline int count_dirty_bits_byte(char byte, int num_bits) | |
81 | { | |
82 | int num = 0; | |
83 | ||
84 | switch (num_bits) { /* fall through... */ | |
85 | case 8: if (byte & 128) num++; | |
86 | case 7: if (byte & 64) num++; | |
87 | case 6: if (byte & 32) num++; | |
88 | case 5: if (byte & 16) num++; | |
89 | case 4: if (byte & 8) num++; | |
90 | case 3: if (byte & 4) num++; | |
91 | case 2: if (byte & 2) num++; | |
92 | case 1: if (byte & 1) num++; | |
93 | default: break; | |
94 | } | |
95 | ||
96 | return num; | |
97 | } | |
98 | ||
99 | int count_dirty_bits(char *buf, int num_bits) | |
100 | { | |
101 | int i, num = 0; | |
102 | ||
103 | for (i=0; i < num_bits / 8; i++) | |
104 | num += count_dirty_bits_byte(buf[i], 8); | |
105 | ||
106 | if (num_bits % 8) /* not an even byte boundary */ | |
107 | num += count_dirty_bits_byte(buf[i], num_bits % 8); | |
108 | ||
109 | return num; | |
110 | } | |
111 | ||
112 | /* calculate the size of the bitmap given the array size and bitmap chunksize */ | |
113 | unsigned long long bitmap_bits(unsigned long long array_size, | |
114 | unsigned long chunksize) | |
115 | { | |
116 | return (array_size * 512 + chunksize - 1) / chunksize; | |
117 | } | |
118 | ||
119 | bitmap_info_t *bitmap_fd_read(int fd, int brief) | |
120 | { | |
121 | unsigned long long total_bits = 0, read_bits = 0, dirty_bits = 0; | |
122 | bitmap_info_t *info; | |
123 | char buf[512]; | |
124 | int n; | |
125 | ||
126 | info = malloc(sizeof(*info)); | |
127 | if (info == NULL) { | |
838acbc2 NB |
128 | #if __GNUC__ < 3 |
129 | fprintf(stderr, Name ": failed to allocate %d bytes\n", | |
130 | (int)sizeof(*info)); | |
131 | #else | |
f9c25f1d | 132 | fprintf(stderr, Name ": failed to allocate %zd bytes\n", |
c82f047c | 133 | sizeof(*info)); |
838acbc2 | 134 | #endif |
c82f047c NB |
135 | return NULL; |
136 | } | |
137 | ||
138 | if (read(fd, &info->sb, sizeof(info->sb)) != sizeof(info->sb)) { | |
139 | fprintf(stderr, Name ": failed to read superblock of bitmap " | |
140 | "file: %s\n", strerror(errno)); | |
141 | free(info); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | sb_le_to_cpu(&info->sb); /* convert superblock to CPU byte ordering */ | |
146 | ||
147 | if (brief || info->sb.sync_size == 0) | |
148 | goto out; | |
149 | ||
150 | /* read the rest of the file counting total bits and dirty bits -- | |
151 | * we stop when either: | |
152 | * 1) we hit EOF, in which case we assume the rest of the bits (if any) | |
153 | * are dirty | |
154 | * 2) we've read the full bitmap, in which case we ignore any trailing | |
155 | * data in the file | |
156 | */ | |
157 | total_bits = bitmap_bits(info->sb.sync_size, info->sb.chunksize); | |
158 | ||
8431b2b2 | 159 | while ((n = read(fd, buf, sizeof(buf))) > 0) { |
c82f047c NB |
160 | unsigned long long remaining = total_bits - read_bits; |
161 | ||
8431b2b2 NB |
162 | if (remaining > sizeof(buf) * 8) /* we want the full buffer */ |
163 | remaining = sizeof(buf) * 8; | |
c82f047c NB |
164 | if (remaining > n * 8) /* the file is truncated */ |
165 | remaining = n * 8; | |
166 | dirty_bits += count_dirty_bits(buf, remaining); | |
167 | ||
168 | read_bits += remaining; | |
169 | if (read_bits >= total_bits) /* we've got what we want */ | |
170 | break; | |
171 | } | |
172 | ||
173 | if (read_bits < total_bits) { /* file truncated... */ | |
174 | fprintf(stderr, Name ": WARNING: bitmap file is not large " | |
f9c25f1d NB |
175 | "enough for array size %llu!\n\n", |
176 | (unsigned long long)info->sb.sync_size); | |
c82f047c NB |
177 | total_bits = read_bits; |
178 | } | |
179 | out: | |
180 | info->total_bits = total_bits; | |
181 | info->dirty_bits = dirty_bits; | |
182 | return info; | |
183 | } | |
184 | ||
34163fc7 | 185 | bitmap_info_t *bitmap_file_read(char *filename, int brief, struct supertype **stp) |
c82f047c NB |
186 | { |
187 | int fd; | |
188 | bitmap_info_t *info; | |
55935d51 | 189 | struct stat stb; |
34163fc7 | 190 | struct supertype *st = *stp; |
c82f047c NB |
191 | |
192 | fd = open(filename, O_RDONLY); | |
193 | if (fd < 0) { | |
194 | fprintf(stderr, Name ": failed to open bitmap file %s: %s\n", | |
195 | filename, strerror(errno)); | |
196 | return NULL; | |
197 | } | |
55935d51 NB |
198 | fstat(fd, &stb); |
199 | if ((S_IFMT & stb.st_mode) == S_IFBLK) { | |
200 | /* block device, so we are probably after an internal bitmap */ | |
201 | if (!st) st = guess_super(fd); | |
202 | if (!st) { | |
203 | /* just look at device... */ | |
204 | lseek(fd, 0, 0); | |
205 | } else { | |
f6d75de8 | 206 | st->ss->locate_bitmap(st, fd, NULL); |
55935d51 | 207 | } |
34163fc7 NB |
208 | ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */ |
209 | *stp = st; | |
55935d51 | 210 | } |
c82f047c NB |
211 | |
212 | info = bitmap_fd_read(fd, brief); | |
213 | close(fd); | |
214 | return info; | |
215 | } | |
216 | ||
34163fc7 NB |
217 | __u32 swapl(__u32 l) |
218 | { | |
219 | char *c = (char*)&l; | |
220 | char t= c[0]; | |
221 | c[0] = c[3]; | |
222 | c[3] = t; | |
223 | ||
224 | t = c[1]; | |
225 | c[1] = c[2]; | |
226 | c[2] = t; | |
227 | return l; | |
228 | } | |
55935d51 | 229 | int ExamineBitmap(char *filename, int brief, struct supertype *st) |
c82f047c NB |
230 | { |
231 | /* | |
232 | * Read the bitmap file and display its contents | |
233 | */ | |
234 | ||
235 | bitmap_super_t *sb; | |
236 | bitmap_info_t *info; | |
237 | int rv = 1; | |
dfd4d8ee | 238 | char buf[64]; |
c82f047c | 239 | |
34163fc7 | 240 | info = bitmap_file_read(filename, brief, &st); |
c82f047c NB |
241 | if (!info) |
242 | return rv; | |
243 | ||
244 | sb = &info->sb; | |
245 | printf(" Filename : %s\n", filename); | |
246 | printf(" Magic : %08x\n", sb->magic); | |
247 | if (sb->magic != BITMAP_MAGIC) { | |
248 | fprintf(stderr, Name ": invalid bitmap magic 0x%x, the bitmap file appears to be corrupted\n", sb->magic); | |
249 | } | |
250 | printf(" Version : %d\n", sb->version); | |
dcec9ee5 NB |
251 | if (sb->version < BITMAP_MAJOR_LO || |
252 | sb->version > BITMAP_MAJOR_HI) { | |
c82f047c NB |
253 | fprintf(stderr, Name ": unknown bitmap version %d, either the bitmap file is corrupted or you need to upgrade your tools\n", sb->version); |
254 | goto free_info; | |
255 | } | |
256 | ||
257 | rv = 0; | |
34163fc7 NB |
258 | if (st && st->ss->swapuuid) { |
259 | printf(" UUID : %08x.%08x.%08x.%08x\n", | |
260 | swapl(*(__u32 *)(sb->uuid+0)), | |
261 | swapl(*(__u32 *)(sb->uuid+4)), | |
262 | swapl(*(__u32 *)(sb->uuid+8)), | |
263 | swapl(*(__u32 *)(sb->uuid+12))); | |
264 | } else { | |
c82f047c NB |
265 | printf(" UUID : %08x.%08x.%08x.%08x\n", |
266 | *(__u32 *)(sb->uuid+0), | |
267 | *(__u32 *)(sb->uuid+4), | |
268 | *(__u32 *)(sb->uuid+8), | |
269 | *(__u32 *)(sb->uuid+12)); | |
34163fc7 | 270 | } |
f9c25f1d NB |
271 | printf(" Events : %llu\n", (unsigned long long)sb->events); |
272 | printf(" Events Cleared : %llu\n", (unsigned long long)sb->events_cleared); | |
c82f047c NB |
273 | printf(" State : %s\n", bitmap_state(sb->state)); |
274 | printf(" Chunksize : %s\n", human_chunksize(sb->chunksize)); | |
275 | printf(" Daemon : %ds flush period\n", sb->daemon_sleep); | |
dfd4d8ee NB |
276 | if (sb->write_behind) |
277 | sprintf(buf, "Allow write behind, max %d", sb->write_behind); | |
278 | else | |
279 | sprintf(buf, "Normal"); | |
280 | printf(" Write Mode : %s\n", buf); | |
f9c25f1d | 281 | printf(" Sync Size : %llu%s\n", (unsigned long long)sb->sync_size/2, |
f277ce36 | 282 | human_size(sb->sync_size * 512)); |
c82f047c NB |
283 | if (brief) |
284 | goto free_info; | |
285 | printf(" Bitmap : %llu bits (chunks), %llu dirty (%2.1f%%)\n", | |
286 | info->total_bits, info->dirty_bits, | |
287 | 100.0 * info->dirty_bits / (info->total_bits + 1)); | |
288 | free_info: | |
289 | free(info); | |
290 | return rv; | |
291 | } | |
292 | ||
293 | int CreateBitmap(char *filename, int force, char uuid[16], | |
1bf4e2d9 NB |
294 | unsigned long chunksize, unsigned long daemon_sleep, |
295 | unsigned long write_behind, | |
dcec9ee5 NB |
296 | unsigned long long array_size /* sectors */, |
297 | int major) | |
c82f047c NB |
298 | { |
299 | /* | |
300 | * Create a bitmap file with a superblock and (optionally) a full bitmap | |
301 | */ | |
302 | ||
303 | FILE *fp; | |
304 | int rv = 1; | |
305 | char block[512]; | |
306 | bitmap_super_t sb; | |
307 | long long bytes, filesize; | |
308 | ||
309 | if (!force && access(filename, F_OK) == 0) { | |
310 | fprintf(stderr, Name ": bitmap file %s already exists, use --force to overwrite\n", filename); | |
311 | return rv; | |
312 | } | |
313 | ||
314 | fp = fopen(filename, "w"); | |
315 | if (fp == NULL) { | |
316 | fprintf(stderr, Name ": failed to open bitmap file %s: %s\n", | |
317 | filename, strerror(errno)); | |
318 | return rv; | |
319 | } | |
320 | ||
321 | memset(&sb, 0, sizeof(sb)); | |
322 | sb.magic = BITMAP_MAGIC; | |
dcec9ee5 | 323 | sb.version = major; |
c82f047c NB |
324 | if (uuid != NULL) |
325 | memcpy(sb.uuid, uuid, 16); | |
326 | sb.chunksize = chunksize; | |
327 | sb.daemon_sleep = daemon_sleep; | |
dfd4d8ee | 328 | sb.write_behind = write_behind; |
c82f047c NB |
329 | sb.sync_size = array_size; |
330 | ||
331 | sb_cpu_to_le(&sb); /* convert to on-disk byte ordering */ | |
332 | ||
333 | if (fwrite(&sb, sizeof(sb), 1, fp) != 1) { | |
334 | fprintf(stderr, Name ": failed to write superblock to bitmap file %s: %s\n", filename, strerror(errno)); | |
335 | goto out; | |
336 | } | |
337 | ||
338 | /* calculate the size of the bitmap and write it to disk */ | |
339 | bytes = (bitmap_bits(array_size, chunksize) + 7) / 8; | |
340 | if (!bytes) { | |
341 | rv = 0; | |
342 | goto out; | |
343 | } | |
344 | ||
345 | filesize = bytes + sizeof(sb); | |
346 | ||
347 | memset(block, 0xff, sizeof(block)); | |
348 | ||
349 | while (bytes > 0) { | |
350 | if (fwrite(block, sizeof(block), 1, fp) != 1) { | |
351 | fprintf(stderr, Name ": failed to write bitmap file %s: %s\n", filename, strerror(errno)); | |
352 | goto out; | |
353 | } | |
354 | bytes -= sizeof(block); | |
355 | } | |
356 | ||
357 | rv = 0; | |
358 | /* make the file be the right size (well, to the nearest byte) */ | |
1e0d770c NB |
359 | if (ftruncate(fileno(fp), filesize)) |
360 | perror("ftrunace"); | |
c82f047c NB |
361 | out: |
362 | fclose(fp); | |
363 | if (rv) | |
364 | unlink(filename); /* possibly corrupted, better get rid of it */ | |
365 | return rv; | |
366 | } |