Initial public busybox upstream commit
[busybox4maemo] / e2fsprogs / old_e2fsprogs / ext2fs / rs_bitmap.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * rs_bitmap.c --- routine for changing the size of a bitmap
4  *
5  * Copyright (C) 1996, 1997 Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  * %End-Header%
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #ifdef HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26
27 #include "ext2_fs.h"
28 #include "ext2fs.h"
29
30 errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end,
31                                        ext2fs_generic_bitmap bmap)
32 {
33         errcode_t       retval;
34         size_t          size, new_size;
35         __u32           bitno;
36
37         if (!bmap)
38                 return EXT2_ET_INVALID_ARGUMENT;
39
40         EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP);
41
42         /*
43          * If we're expanding the bitmap, make sure all of the new
44          * parts of the bitmap are zero.
45          */
46         if (new_end > bmap->end) {
47                 bitno = bmap->real_end;
48                 if (bitno > new_end)
49                         bitno = new_end;
50                 for (; bitno > bmap->end; bitno--)
51                         ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
52         }
53         if (new_real_end == bmap->real_end) {
54                 bmap->end = new_end;
55                 return 0;
56         }
57
58         size = ((bmap->real_end - bmap->start) / 8) + 1;
59         new_size = ((new_real_end - bmap->start) / 8) + 1;
60
61         if (size != new_size) {
62                 retval = ext2fs_resize_mem(size, new_size, &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