]> git.neil.brown.name Git - mdadm.git/blob - bitmap.c
Merge branch 'fix-unlikely-potential-overflows' of https://github.com/sjvs/mdadm
[mdadm.git] / bitmap.c
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 "mdadm.h"
22
23 static inline void sb_le_to_cpu(bitmap_super_t *sb)
24 {
25         sb->magic = __le32_to_cpu(sb->magic);
26         sb->version = __le32_to_cpu(sb->version);
27         /* uuid gets no translation */
28         sb->events = __le64_to_cpu(sb->events);
29         sb->events_cleared = __le64_to_cpu(sb->events_cleared);
30         sb->state = __le32_to_cpu(sb->state);
31         sb->chunksize = __le32_to_cpu(sb->chunksize);
32         sb->daemon_sleep = __le32_to_cpu(sb->daemon_sleep);
33         sb->sync_size = __le64_to_cpu(sb->sync_size);
34         sb->write_behind = __le32_to_cpu(sb->write_behind);
35         sb->nodes = __le32_to_cpu(sb->nodes);
36         sb->sectors_reserved = __le32_to_cpu(sb->sectors_reserved);
37 }
38
39 static inline void sb_cpu_to_le(bitmap_super_t *sb)
40 {
41         sb_le_to_cpu(sb); /* these are really the same thing */
42 }
43
44 mapping_t bitmap_states[] = {
45         { "OK", 0 },
46         { "Out of date", 2 },
47         { NULL, -1 }
48 };
49
50 const char *bitmap_state(int state_num)
51 {
52         char *state = map_num(bitmap_states, state_num);
53         return state ? state : "Unknown";
54 }
55
56 const char *human_chunksize(unsigned long bytes)
57 {
58         static char buf[16];
59         char *suffixes[] = { "B", "KB", "MB", "GB", "TB", NULL };
60         int i = 0;
61
62         while (bytes >> 10) {
63                 bytes >>= 10;
64                 i++;
65         }
66
67         snprintf(buf, sizeof(buf), "%lu %s", bytes, suffixes[i]);
68
69         return buf;
70 }
71
72 typedef struct bitmap_info_s {
73         bitmap_super_t sb;
74         unsigned long long total_bits;
75         unsigned long long dirty_bits;
76 } bitmap_info_t;
77
78 /* count the dirty bits in the first num_bits of byte */
79 static inline int count_dirty_bits_byte(char byte, int num_bits)
80 {
81         int num = 0;
82
83         switch (num_bits) { /* fall through... */
84                 case 8: if (byte & 128) num++;
85                 case 7: if (byte &  64) num++;
86                 case 6: if (byte &  32) num++;
87                 case 5: if (byte &  16) num++;
88                 case 4: if (byte &   8) num++;
89                 case 3: if (byte &   4) num++;
90                 case 2: if (byte &   2) num++;
91                 case 1: if (byte &   1) num++;
92                 default: break;
93         }
94
95         return num;
96 }
97
98 int count_dirty_bits(char *buf, int num_bits)
99 {
100         int i, num = 0;
101
102         for (i = 0; i < num_bits / 8; i++)
103                 num += count_dirty_bits_byte(buf[i], 8);
104
105         if (num_bits % 8) /* not an even byte boundary */
106                 num += count_dirty_bits_byte(buf[i], num_bits % 8);
107
108         return num;
109 }
110
111 /* calculate the size of the bitmap given the array size and bitmap chunksize */
112 unsigned long long bitmap_bits(unsigned long long array_size,
113                                 unsigned long chunksize)
114 {
115         return (array_size * 512 + chunksize - 1) / chunksize;
116 }
117
118 unsigned long bitmap_sectors(struct bitmap_super_s *bsb)
119 {
120         unsigned long long bits = bitmap_bits(__le64_to_cpu(bsb->sync_size),
121                                               __le32_to_cpu(bsb->chunksize));
122         int bits_per_sector = 8*512;
123         return (bits + bits_per_sector - 1) / bits_per_sector;
124 }
125
126 bitmap_info_t *bitmap_fd_read(int fd, int brief)
127 {
128         /* Note: fd might be open O_DIRECT, so we must be
129          * careful to align reads properly
130          */
131         unsigned long long total_bits = 0, read_bits = 0, dirty_bits = 0;
132         bitmap_info_t *info;
133         void *buf;
134         unsigned int n, skip;
135
136         if (posix_memalign(&buf, 4096, 8192) != 0) {
137                 pr_err("failed to allocate 8192 bytes\n");
138                 return NULL;
139         }
140         n = read(fd, buf, 8192);
141
142         info = xmalloc(sizeof(*info));
143
144         if (n < sizeof(info->sb)) {
145                 pr_err("failed to read superblock of bitmap file: %s\n", strerror(errno));
146                 free(info);
147                 free(buf);
148                 return NULL;
149         }
150         memcpy(&info->sb, buf, sizeof(info->sb));
151         skip = sizeof(info->sb);
152
153         sb_le_to_cpu(&info->sb); /* convert superblock to CPU byte ordering */
154
155         if (brief || info->sb.sync_size == 0 || info->sb.chunksize == 0)
156                 goto out;
157
158         /* read the rest of the file counting total bits and dirty bits --
159          * we stop when either:
160          * 1) we hit EOF, in which case we assume the rest of the bits (if any)
161          *    are dirty
162          * 2) we've read the full bitmap, in which case we ignore any trailing
163          *    data in the file
164          */
165         total_bits = bitmap_bits(info->sb.sync_size, info->sb.chunksize);
166
167         while(read_bits < total_bits) {
168                 unsigned long long remaining = total_bits - read_bits;
169
170                 if (n == 0) {
171                         n = read(fd, buf, 8192);
172                         skip = 0;
173                         if (n <= 0)
174                                 break;
175                 }
176                 if (remaining > (n-skip) * 8) /* we want the full buffer */
177                         remaining = (n-skip) * 8;
178
179                 dirty_bits += count_dirty_bits(buf+skip, remaining);
180
181                 read_bits += remaining;
182                 n = 0;
183         }
184
185         if (read_bits < total_bits) { /* file truncated... */
186                 pr_err("WARNING: bitmap file is not large enough for array size %llu!\n\n",
187                         (unsigned long long)info->sb.sync_size);
188                 total_bits = read_bits;
189         }
190 out:
191         free(buf);
192         info->total_bits = total_bits;
193         info->dirty_bits = dirty_bits;
194         return info;
195 }
196
197 int bitmap_file_open(char *filename, struct supertype **stp)
198 {
199         int fd;
200         struct stat stb;
201         struct supertype *st = *stp;
202
203         if (stat(filename, &stb) < 0) {
204                 pr_err("failed to find file %s: %s\n",
205                         filename, strerror(errno));
206                 return -1;
207         }
208         if ((S_IFMT & stb.st_mode) == S_IFBLK) {
209                 fd = open(filename, O_RDONLY|O_DIRECT);
210                 if (fd < 0) {
211                         pr_err("failed to open bitmap file %s: %s\n",
212                                 filename, strerror(errno));
213                         return -1;
214                 }
215                 /* block device, so we are probably after an internal bitmap */
216                 if (!st) st = guess_super(fd);
217                 if (!st) {
218                         /* just look at device... */
219                         lseek(fd, 0, 0);
220                 } else if (!st->ss->locate_bitmap) {
221                         pr_err("No bitmap possible with %s metadata\n",
222                                 st->ss->name);
223                         return -1;
224                 } else {
225                         if (st->ss->locate_bitmap(st, fd)) {
226                                 pr_err("%s doesn't have bitmap\n", filename);
227                                 fd = -1;
228                         }
229                 }
230
231                 *stp = st;
232         } else {
233                 fd = open(filename, O_RDONLY|O_DIRECT);
234                 if (fd < 0) {
235                         pr_err("failed to open bitmap file %s: %s\n",
236                                 filename, strerror(errno));
237                         return -1;
238                 }
239         }
240
241         return fd;
242 }
243
244 __u32 swapl(__u32 l)
245 {
246         char *c = (char*)&l;
247         char t= c[0];
248         c[0] = c[3];
249         c[3] = t;
250
251         t = c[1];
252         c[1] = c[2];
253         c[2] = t;
254         return l;
255 }
256 int ExamineBitmap(char *filename, int brief, struct supertype *st)
257 {
258         /*
259          * Read the bitmap file and display its contents
260          */
261
262         bitmap_super_t *sb;
263         bitmap_info_t *info;
264         int rv = 1;
265         char buf[64];
266         int swap;
267         int fd, i;
268         __u32 uuid32[4];
269
270         fd = bitmap_file_open(filename, &st);
271         if (fd < 0)
272                 return rv;
273
274         info = bitmap_fd_read(fd, brief);
275         if (!info)
276                 return rv;
277         sb = &info->sb;
278         if (sb->magic != BITMAP_MAGIC && md_get_version(fd) > 0) {
279                 pr_err("This is an md array.  To view a bitmap you need to examine\n");
280                 pr_err("a member device, not the array.\n");
281                 pr_err("Reporting bitmap that would be used if this array were used\n");
282                 pr_err("as a member of some other array\n");
283         }
284         close(fd);
285         printf("        Filename : %s\n", filename);
286         printf("           Magic : %08x\n", sb->magic);
287         if (sb->magic != BITMAP_MAGIC) {
288                 pr_err("invalid bitmap magic 0x%x, the bitmap file appears\n",
289                        sb->magic);
290                 pr_err("to be corrupted or missing.\n");
291         }
292         printf("         Version : %d\n", sb->version);
293         if (sb->version < BITMAP_MAJOR_LO ||
294             sb->version > BITMAP_MAJOR_CLUSTERED) {
295                 pr_err("unknown bitmap version %d, either the bitmap file\n",
296                        sb->version);
297                 pr_err("is corrupted or you need to upgrade your tools\n");
298                 goto free_info;
299         }
300
301         rv = 0;
302         if (st)
303                 swap = st->ss->swapuuid;
304         else
305 #if __BYTE_ORDER == BIG_ENDIAN
306                 swap = 0;
307 #else
308                 swap = 1;
309 #endif
310         memcpy(uuid32, sb->uuid, 16);
311         if (swap)
312                 printf("            UUID : %08x:%08x:%08x:%08x\n",
313                        swapl(uuid32[0]),
314                        swapl(uuid32[1]),
315                        swapl(uuid32[2]),
316                        swapl(uuid32[3]));
317         else
318                 printf("            UUID : %08x:%08x:%08x:%08x\n",
319                        uuid32[0],
320                        uuid32[1],
321                        uuid32[2],
322                        uuid32[3]);
323
324         if (sb->nodes == 0) {
325                 printf("          Events : %llu\n", (unsigned long long)sb->events);
326                 printf("  Events Cleared : %llu\n", (unsigned long long)sb->events_cleared);
327                 printf("           State : %s\n", bitmap_state(sb->state));
328
329         }
330
331         printf("       Chunksize : %s\n", human_chunksize(sb->chunksize));
332         printf("          Daemon : %ds flush period\n", sb->daemon_sleep);
333         if (sb->write_behind)
334                 sprintf(buf, "Allow write behind, max %d", sb->write_behind);
335         else
336                 sprintf(buf, "Normal");
337         printf("      Write Mode : %s\n", buf);
338         printf("       Sync Size : %llu%s\n", (unsigned long long)sb->sync_size/2,
339                                         human_size(sb->sync_size * 512));
340
341         if (sb->nodes == 0) {
342                 if (brief)
343                         goto free_info;
344                 printf("          Bitmap : %llu bits (chunks), %llu dirty (%2.1f%%)\n",
345                        info->total_bits, info->dirty_bits,
346                        100.0 * info->dirty_bits / (info->total_bits?:1));
347         } else {
348                 printf("   Cluster nodes : %d\n", sb->nodes);
349                 printf("    Cluster name : %-64s\n", sb->cluster_name);
350                 for (i = 0; i < (int)sb->nodes; i++) {
351                         if (i) {
352                                 free(info);
353                                 info = bitmap_fd_read(fd, brief);
354                                 sb = &info->sb;
355                         }
356                         if (sb->magic != BITMAP_MAGIC)
357                                 pr_err("invalid bitmap magic 0x%x, the bitmap file appears to be corrupted\n", sb->magic);
358
359                         printf("       Node Slot : %d\n", i);
360                         printf("          Events : %llu\n",
361                                (unsigned long long)sb->events);
362                         printf("  Events Cleared : %llu\n",
363                                (unsigned long long)sb->events_cleared);
364                         printf("           State : %s\n", bitmap_state(sb->state));
365                         if (brief)
366                                 continue;
367                         printf("          Bitmap : %llu bits (chunks), %llu dirty (%2.1f%%)\n",
368                                info->total_bits, info->dirty_bits,
369                                100.0 * info->dirty_bits / (info->total_bits?:1));
370
371                 }
372         }
373
374 free_info:
375         free(info);
376         return rv;
377 }
378
379 int CreateBitmap(char *filename, int force, char uuid[16],
380                  unsigned long chunksize, unsigned long daemon_sleep,
381                  unsigned long write_behind,
382                  unsigned long long array_size /* sectors */,
383                  int major)
384 {
385         /*
386          * Create a bitmap file with a superblock and (optionally) a full bitmap
387          */
388
389         FILE *fp;
390         int rv = 1;
391         char block[512];
392         bitmap_super_t sb;
393         long long bytes, filesize;
394
395         if (!force && access(filename, F_OK) == 0) {
396                 pr_err("bitmap file %s already exists, use --force to overwrite\n", filename);
397                 return rv;
398         }
399
400         fp = fopen(filename, "w");
401         if (fp == NULL) {
402                 pr_err("failed to open bitmap file %s: %s\n",
403                         filename, strerror(errno));
404                 return rv;
405         }
406
407         if (chunksize == UnSet) {
408                 /* We don't want more than 2^21 chunks, as 2^11 fill up one
409                  * 4K page (2 bytes per chunk), and 2^10 address of those
410                  * fill up a 4K indexing page.  2^20 might be safer, especially
411                  * on 64bit hosts, so use that.
412                  */
413                 chunksize = DEFAULT_BITMAP_CHUNK;
414                 /* <<20 for 2^20 chunks, >>9 to convert bytes to sectors */
415                 while (array_size > ((unsigned long long)chunksize << (20-9)))
416                         chunksize <<= 1;
417         }
418
419         memset(&sb, 0, sizeof(sb));
420         sb.magic = BITMAP_MAGIC;
421         sb.version = major;
422         if (uuid != NULL)
423                 memcpy(sb.uuid, uuid, 16);
424         sb.chunksize = chunksize;
425         sb.daemon_sleep = daemon_sleep;
426         sb.write_behind = write_behind;
427         sb.sync_size = array_size;
428
429         sb_cpu_to_le(&sb); /* convert to on-disk byte ordering */
430
431         if (fwrite(&sb, sizeof(sb), 1, fp) != 1) {
432                 pr_err("failed to write superblock to bitmap file %s: %s\n", filename, strerror(errno));
433                 goto out;
434         }
435
436         /* calculate the size of the bitmap and write it to disk */
437         bytes = (bitmap_bits(array_size, chunksize) + 7) / 8;
438         if (!bytes) {
439                 rv = 0;
440                 goto out;
441         }
442
443         filesize = bytes + sizeof(sb);
444
445         memset(block, 0xff, sizeof(block));
446
447         while (bytes > 0) {
448                 if (fwrite(block, sizeof(block), 1, fp) != 1) {
449                         pr_err("failed to write bitmap file %s: %s\n", filename, strerror(errno));
450                         goto out;
451                 }
452                 bytes -= sizeof(block);
453         }
454
455         rv = 0;
456         fflush(fp);
457         /* make the file be the right size (well, to the nearest byte) */
458         if (ftruncate(fileno(fp), filesize))
459                 perror("ftrunace");
460 out:
461         fclose(fp);
462         if (rv)
463                 unlink(filename); /* possibly corrupted, better get rid of it */
464         return rv;
465 }
466
467 int bitmap_update_uuid(int fd, int *uuid, int swap)
468 {
469         struct bitmap_super_s bm;
470         if (lseek(fd, 0, 0) != 0)
471                 return 1;
472         if (read(fd, &bm, sizeof(bm)) != sizeof(bm))
473                 return 1;
474         if (bm.magic != __cpu_to_le32(BITMAP_MAGIC))
475                 return 1;
476         copy_uuid(bm.uuid, uuid, swap);
477         if (lseek(fd, 0, 0) != 0)
478                 return 2;
479         if (write(fd, &bm, sizeof(bm)) != sizeof(bm)) {
480                 lseek(fd, 0, 0);
481                 return 2;
482         }
483         lseek(fd, 0, 0);
484         return 0;
485 }