Whamcloud - gitweb
Fix gcc -Wall nits.
[tools/e2fsprogs.git] / lib / ext2fs / rs_bitmap.c
1 /*
2  * rs_bitmap.c --- routine for changing the size of a bitmap
3  *
4  * Copyright (C) 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <fcntl.h>
18 #include <time.h>
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #include "ext2_fs.h"
27 #include "ext2fs.h"
28
29 errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end,
30                                        ext2fs_generic_bitmap bmap)
31 {
32         errcode_t       retval;
33         size_t          size, new_size;
34         __u32           bitno;
35
36         if (!bmap)
37                 return EXT2_ET_INVALID_ARGUMENT;
38
39         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP);
40
41         /*
42          * If we're expanding the bitmap, make sure all of the new
43          * parts of the bitmap are zero.
44          */
45         if (new_end > bmap->end) {
46                 bitno = bmap->real_end;
47                 if (bitno > new_end)
48                         bitno = new_end;
49                 for (; bitno > bmap->end; bitno--)
50                         ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
51         }
52         if (new_real_end == bmap->real_end) {
53                 bmap->end = new_end;
54                 return 0;
55         }
56         
57         size = ((bmap->real_end - bmap->start) / 8) + 1;
58         new_size = ((new_real_end - bmap->start) / 8) + 1;
59
60         if (size != new_size) {
61                 retval = ext2fs_resize_mem(size, new_size,
62                                            (void **) &bmap->bitmap);
63                 if (retval)
64                         return retval;
65         }
66         if (new_size > size)
67                 memset(bmap->bitmap + size, 0, new_size - size);
68
69         bmap->end = new_end;
70         bmap->real_end = new_real_end;
71         return 0;
72 }
73
74 errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
75                                      ext2fs_inode_bitmap bmap)
76 {
77         errcode_t       retval;
78         
79         if (!bmap)
80                 return EXT2_ET_INVALID_ARGUMENT;
81
82         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_INODE_BITMAP);
83
84         bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
85         retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
86                                               bmap);
87         bmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
88         return retval;
89 }
90
91 errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
92                                      ext2fs_block_bitmap bmap)
93 {
94         errcode_t       retval;
95         
96         if (!bmap)
97                 return EXT2_ET_INVALID_ARGUMENT;
98
99         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
100
101         bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
102         retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
103                                               bmap);
104         bmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
105         return retval;
106 }
107