blob: 712a4a9b1d64ba0d540c3f6005a3aedeba5b32be [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger1fd98e02005-05-09 22:10:42 +00002/*
3 * bitmaps.c --- routines to read, write, and manipulate the inode and
4 * block bitmaps.
5 *
6 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
12 */
13
14#include <stdio.h>
15#include <string.h>
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19#include <fcntl.h>
20#include <time.h>
21#if HAVE_SYS_STAT_H
22#include <sys/stat.h>
23#endif
24#if HAVE_SYS_TYPES_H
25#include <sys/types.h>
26#endif
27
28#include "ext2_fs.h"
29#include "ext2fs.h"
30
31static errcode_t make_bitmap(__u32 start, __u32 end, __u32 real_end,
32 const char *descr, char *init_map,
33 ext2fs_generic_bitmap *ret)
34{
35 ext2fs_generic_bitmap bitmap;
36 errcode_t retval;
37 size_t size;
38
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000039 retval = ext2fs_get_mem(sizeof(struct ext2fs_struct_generic_bitmap),
Mike Frysinger1fd98e02005-05-09 22:10:42 +000040 &bitmap);
41 if (retval)
42 return retval;
43
44 bitmap->magic = EXT2_ET_MAGIC_GENERIC_BITMAP;
45 bitmap->fs = NULL;
46 bitmap->start = start;
47 bitmap->end = end;
48 bitmap->real_end = real_end;
49 bitmap->base_error_code = EXT2_ET_BAD_GENERIC_MARK;
50 if (descr) {
51 retval = ext2fs_get_mem(strlen(descr)+1, &bitmap->description);
52 if (retval) {
53 ext2fs_free_mem(&bitmap);
54 return retval;
55 }
56 strcpy(bitmap->description, descr);
57 } else
58 bitmap->description = 0;
59
60 size = (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1);
61 retval = ext2fs_get_mem(size, &bitmap->bitmap);
62 if (retval) {
63 ext2fs_free_mem(&bitmap->description);
64 ext2fs_free_mem(&bitmap);
65 return retval;
66 }
67
68 if (init_map)
69 memcpy(bitmap->bitmap, init_map, size);
70 else
71 memset(bitmap->bitmap, 0, size);
72 *ret = bitmap;
73 return 0;
74}
75
76errcode_t ext2fs_allocate_generic_bitmap(__u32 start,
77 __u32 end,
78 __u32 real_end,
79 const char *descr,
80 ext2fs_generic_bitmap *ret)
81{
82 return make_bitmap(start, end, real_end, descr, 0, ret);
83}
84
85errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
86 ext2fs_generic_bitmap *dest)
87{
88 errcode_t retval;
89 ext2fs_generic_bitmap new_map;
90
91 retval = make_bitmap(src->start, src->end, src->real_end,
92 src->description, src->bitmap, &new_map);
93 if (retval)
94 return retval;
95 new_map->magic = src->magic;
96 new_map->fs = src->fs;
97 new_map->base_error_code = src->base_error_code;
98 *dest = new_map;
99 return 0;
100}
101
102void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)
103{
104 __u32 i, j;
105
106 for (i=map->end+1, j = i - map->start; i <= map->real_end; i++, j++)
107 ext2fs_set_bit(j, map->bitmap);
108
109 return;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000110}
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000111
112errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
113 const char *descr,
114 ext2fs_inode_bitmap *ret)
115{
116 ext2fs_inode_bitmap bitmap;
117 errcode_t retval;
118 __u32 start, end, real_end;
119
120 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
121
122 fs->write_bitmaps = ext2fs_write_bitmaps;
123
124 start = 1;
125 end = fs->super->s_inodes_count;
126 real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
127
128 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
129 descr, &bitmap);
130 if (retval)
131 return retval;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000132
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000133 bitmap->magic = EXT2_ET_MAGIC_INODE_BITMAP;
134 bitmap->fs = fs;
135 bitmap->base_error_code = EXT2_ET_BAD_INODE_MARK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000136
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000137 *ret = bitmap;
138 return 0;
139}
140
141errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
142 const char *descr,
143 ext2fs_block_bitmap *ret)
144{
145 ext2fs_block_bitmap bitmap;
146 errcode_t retval;
147 __u32 start, end, real_end;
148
149 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
150
151 fs->write_bitmaps = ext2fs_write_bitmaps;
152
153 start = fs->super->s_first_data_block;
154 end = fs->super->s_blocks_count-1;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000155 real_end = (EXT2_BLOCKS_PER_GROUP(fs->super)
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000156 * fs->group_desc_count)-1 + start;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000157
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000158 retval = ext2fs_allocate_generic_bitmap(start, end, real_end,
159 descr, &bitmap);
160 if (retval)
161 return retval;
162
163 bitmap->magic = EXT2_ET_MAGIC_BLOCK_BITMAP;
164 bitmap->fs = fs;
165 bitmap->base_error_code = EXT2_ET_BAD_BLOCK_MARK;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000166
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000167 *ret = bitmap;
168 return 0;
169}
170
171errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
172 ext2_ino_t end, ext2_ino_t *oend)
173{
174 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_INODE_BITMAP);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000175
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000176 if (end > bitmap->real_end)
177 return EXT2_ET_FUDGE_INODE_BITMAP_END;
178 if (oend)
179 *oend = bitmap->end;
180 bitmap->end = end;
181 return 0;
182}
183
184errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
185 blk_t end, blk_t *oend)
186{
187 EXT2_CHECK_MAGIC(bitmap, EXT2_ET_MAGIC_BLOCK_BITMAP);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000188
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000189 if (end > bitmap->real_end)
190 return EXT2_ET_FUDGE_BLOCK_BITMAP_END;
191 if (oend)
192 *oend = bitmap->end;
193 bitmap->end = end;
194 return 0;
195}
196
197void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
198{
199 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_INODE_BITMAP))
200 return;
201
202 memset(bitmap->bitmap, 0,
203 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
204}
205
206void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
207{
208 if (!bitmap || (bitmap->magic != EXT2_ET_MAGIC_BLOCK_BITMAP))
209 return;
210
211 memset(bitmap->bitmap, 0,
212 (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1));
213}