blob: c41b5e65b14e41206e5fe549d6ac6a3fb99948e1 [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
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#include <unistd.h>
15#include <stdlib.h>
16#include <fcntl.h>
17#include <time.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20#if HAVE_ERRNO_H
21#include <errno.h>
22#endif
23
24#include <linux/ext2_fs.h>
25
26#include "ext2fs.h"
27
28errcode_t ext2fs_resize_generic_bitmap(__u32 new_end, __u32 new_real_end,
29 ext2fs_generic_bitmap bmap)
30{
31 size_t size, new_size;
32 char *new_bitmap;
Theodore Ts'o1e1da291997-06-09 14:51:29 +000033 __u32 bitno;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000034
35 if (!bmap)
36 return EINVAL;
37
38 EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_GENERIC_BITMAP);
Theodore Ts'o1e1da291997-06-09 14:51:29 +000039
40 /*
41 * If we're expanding the bitmap, make sure all of the new
42 * parts of the bitmap are zero.
43 */
44 if (new_end > bmap->end) {
45 bitno = bmap->real_end;
46 if (bitno > new_end)
47 bitno = new_end;
48 for (; bitno > bmap->end; bitno--)
49 ext2fs_clear_bit(bitno - bmap->start, bmap->bitmap);
50 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000051 if (new_real_end == bmap->real_end) {
52 bmap->end = new_end;
53 return 0;
54 }
55
56 size = ((bmap->real_end - bmap->start) / 8) + 1;
57 new_size = ((new_real_end - bmap->start) / 8) + 1;
58
59 new_bitmap = realloc(bmap->bitmap, new_size);
60 if (!new_bitmap)
61 return ENOMEM;
62 if (new_size > size)
63 memset(new_bitmap + size, 0, new_size - size);
64
65 bmap->bitmap = new_bitmap;
66 bmap->end = new_end;
67 bmap->real_end = new_real_end;
68 return 0;
69}
70
71errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
72 ext2fs_inode_bitmap bmap)
73{
74 errcode_t retval;
75
76 if (!bmap)
77 return EINVAL;
78
79 EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_INODE_BITMAP);
80
81 bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
82 retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
83 bmap);
84 bmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
85 return retval;
86}
87
88errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
89 ext2fs_block_bitmap bmap)
90{
91 errcode_t retval;
92
93 if (!bmap)
94 return EINVAL;
95
96 EXT2_CHECK_MAGIC(bmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
97
98 bmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
99 retval = ext2fs_resize_generic_bitmap(new_end, new_real_end,
100 bmap);
101 bmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
102 return retval;
103}
104