Convert a chunk of usage.h to USE_ and SKIP_ (more to do there), and fix a
[oweals/busybox.git] / e2fsprogs / 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, &bmap->bitmap);
62                 if (retval)
63                         return retval;
64         }
65         if (new_size > size)
66                 memset(bmap->bitmap + size, 0, new_size - size);
67
68         bmap->end = new_end;
69         bmap->real_end = new_real_end;
70         return 0;
71 }
72
73 errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
74                                      ext2fs_inode_bitmap bmap)
75 {
76         errcode_t       retval;
77
78         if (!bmap)
79                 return EXT2_ET_INVALID_ARGUMENT;
80
81         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_INODE_BITMAP);
82
83         bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
84         retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
85                                               bmap);
86         bmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
87         return retval;
88 }
89
90 errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
91                                      ext2fs_block_bitmap bmap)
92 {
93         errcode_t       retval;
94
95         if (!bmap)
96                 return EXT2_ET_INVALID_ARGUMENT;
97
98         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
99
100         bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
101         retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
102                                               bmap);
103         bmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
104         return retval;
105 }
106