Whamcloud - gitweb
ChangeLog, debugfs.8.in, debugfs.c:
[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 #if EXT2_FLAT_INCLUDES
27 #include "ext2_fs.h"
28 #else
29 #include <linux/ext2_fs.h>
30 #endif
31
32 #include "ext2fs.h"
33
34 errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end,
35                                        ext2fs_generic_bitmap bmap)
36 {
37         errcode_t       retval;
38         size_t          size, new_size;
39         __u32           bitno;
40
41         if (!bmap)
42                 return EXT2_ET_INVALID_ARGUMENT;
43
44         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP);
45
46         /*
47          * If we're expanding the bitmap, make sure all of the new
48          * parts of the bitmap are zero.
49          */
50         if (new_end > bmap->end) {
51                 bitno = bmap->real_end;
52                 if (bitno > new_end)
53                         bitno = new_end;
54                 for (; bitno > bmap->end; bitno--)
55                         ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
56         }
57         if (new_real_end == bmap->real_end) {
58                 bmap->end = new_end;
59                 return 0;
60         }
61         
62         size = ((bmap->real_end - bmap->start) / 8) + 1;
63         new_size = ((new_real_end - bmap->start) / 8) + 1;
64
65         if (size != new_size) {
66                 retval = ext2fs_resize_mem(size, new_size,
67                                            (void **) &bmap->bitmap);
68                 if (retval)
69                         return retval;
70         }
71         if (new_size > size)
72                 memset(bmap->bitmap + size, 0, new_size - size);
73
74         bmap->end = new_end;
75         bmap->real_end = new_real_end;
76         return 0;
77 }
78
79 errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
80                                      ext2fs_inode_bitmap bmap)
81 {
82         errcode_t       retval;
83         
84         if (!bmap)
85                 return EXT2_ET_INVALID_ARGUMENT;
86
87         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_INODE_BITMAP);
88
89         bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
90         retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
91                                               bmap);
92         bmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
93         return retval;
94 }
95
96 errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
97                                      ext2fs_block_bitmap bmap)
98 {
99         errcode_t       retval;
100         
101         if (!bmap)
102                 return EXT2_ET_INVALID_ARGUMENT;
103
104         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
105
106         bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
107         retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
108                                               bmap);
109         bmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
110         return retval;
111 }
112