1 /*
2 * bitmaps.c --- routines to read, write, and manipulate the inode and
3 * block bitmaps.
4 *
5 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
10 * %End-Header%
11 */
12
13 #include "config.h"
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 #include "ext2fsP.h"
31 #include "bmap64.h"
32
ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)33 void ext2fs_free_inode_bitmap(ext2fs_inode_bitmap bitmap)
34 {
35 ext2fs_free_generic_bmap(bitmap);
36 }
37
ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)38 void ext2fs_free_block_bitmap(ext2fs_block_bitmap bitmap)
39 {
40 ext2fs_free_generic_bmap(bitmap);
41 }
42
ext2fs_copy_bitmap(ext2fs_generic_bitmap src,ext2fs_generic_bitmap * dest)43 errcode_t ext2fs_copy_bitmap(ext2fs_generic_bitmap src,
44 ext2fs_generic_bitmap *dest)
45 {
46 return (ext2fs_copy_generic_bmap(src, dest));
47 }
ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)48 void ext2fs_set_bitmap_padding(ext2fs_generic_bitmap map)
49 {
50 ext2fs_set_generic_bmap_padding(map);
51 }
52
ext2fs_allocate_inode_bitmap(ext2_filsys fs,const char * descr,ext2fs_inode_bitmap * ret)53 errcode_t ext2fs_allocate_inode_bitmap(ext2_filsys fs,
54 const char *descr,
55 ext2fs_inode_bitmap *ret)
56 {
57 __u64 start, end, real_end;
58
59 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
60
61 if (ext2fs_has_feature_journal_dev(fs->super))
62 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
63
64 fs->write_bitmaps = ext2fs_write_bitmaps;
65
66 start = 1;
67 end = fs->super->s_inodes_count;
68 real_end = (__u64)EXT2_INODES_PER_GROUP(fs->super) *
69 fs->group_desc_count;
70
71 /* Are we permitted to use new-style bitmaps? */
72 if (fs->flags & EXT2_FLAG_64BITS)
73 return (ext2fs_alloc_generic_bmap(fs,
74 EXT2_ET_MAGIC_INODE_BITMAP64,
75 fs->default_bitmap_type,
76 start, end, real_end, descr, ret));
77
78 /* Otherwise, check to see if the file system is small enough
79 * to use old-style 32-bit bitmaps */
80 if ((end > ~0U) || (real_end > ~0U))
81 return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
82
83 return (ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_INODE_BITMAP, fs,
84 start, end, real_end,
85 descr, 0,
86 (ext2fs_generic_bitmap *) ret));
87 }
88
ext2fs_allocate_block_bitmap(ext2_filsys fs,const char * descr,ext2fs_block_bitmap * ret)89 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
90 const char *descr,
91 ext2fs_block_bitmap *ret)
92 {
93 __u64 start, end, real_end;
94
95 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
96
97 if (ext2fs_has_feature_journal_dev(fs->super))
98 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
99
100 fs->write_bitmaps = ext2fs_write_bitmaps;
101
102 start = EXT2FS_B2C(fs, fs->super->s_first_data_block);
103 end = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1);
104 real_end = ((__u64) EXT2_CLUSTERS_PER_GROUP(fs->super)
105 * (__u64) fs->group_desc_count)-1 + start;
106
107 if (fs->flags & EXT2_FLAG_64BITS)
108 return (ext2fs_alloc_generic_bmap(fs,
109 EXT2_ET_MAGIC_BLOCK_BITMAP64,
110 fs->default_bitmap_type,
111 start, end, real_end, descr, ret));
112
113 if ((end > ~0U) || (real_end > ~0U))
114 return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
115
116 return (ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_BLOCK_BITMAP, fs,
117 start, end, real_end,
118 descr, 0,
119 (ext2fs_generic_bitmap *) ret));
120 }
121
122 /*
123 * ext2fs_allocate_block_bitmap() really allocates a per-cluster
124 * bitmap for backwards compatibility. This function allocates a
125 * block bitmap which is truly per-block, even if clusters/bigalloc
126 * are enabled. mke2fs and e2fsck need this for tracking the
127 * allocation of the file system metadata blocks.
128 */
ext2fs_allocate_subcluster_bitmap(ext2_filsys fs,const char * descr,ext2fs_block_bitmap * ret)129 errcode_t ext2fs_allocate_subcluster_bitmap(ext2_filsys fs,
130 const char *descr,
131 ext2fs_block_bitmap *ret)
132 {
133 __u64 start, end, real_end;
134 ext2fs_generic_bitmap bmap;
135 ext2fs_generic_bitmap_64 bmap64;
136 errcode_t retval;
137
138 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
139
140 if (ext2fs_has_feature_journal_dev(fs->super))
141 return EXT2_ET_EXTERNAL_JOURNAL_NOSUPP;
142
143 fs->write_bitmaps = ext2fs_write_bitmaps;
144
145 if (!fs->cluster_ratio_bits)
146 return ext2fs_allocate_block_bitmap(fs, descr, ret);
147
148 if ((fs->flags & EXT2_FLAG_64BITS) == 0)
149 return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
150
151 start = fs->super->s_first_data_block;
152 end = ext2fs_blocks_count(fs->super)-1;
153 real_end = ((__u64) EXT2_BLOCKS_PER_GROUP(fs->super)
154 * (__u64) fs->group_desc_count)-1 + start;
155
156 retval = ext2fs_alloc_generic_bmap(fs, EXT2_ET_MAGIC_BLOCK_BITMAP64,
157 fs->default_bitmap_type, start,
158 end, real_end, descr, &bmap);
159 if (retval)
160 return retval;
161 bmap64 = (ext2fs_generic_bitmap_64) bmap;
162 bmap64->cluster_bits = 0;
163 *ret = bmap;
164 return 0;
165 }
166
ext2fs_get_bitmap_granularity(ext2fs_block_bitmap bitmap)167 int ext2fs_get_bitmap_granularity(ext2fs_block_bitmap bitmap)
168 {
169 ext2fs_generic_bitmap_64 bmap = (ext2fs_generic_bitmap_64) bitmap;
170
171 if (!EXT2FS_IS_64_BITMAP(bmap))
172 return 0;
173
174 return bmap->cluster_bits;
175 }
176
ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,ext2_ino_t end,ext2_ino_t * oend)177 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
178 ext2_ino_t end, ext2_ino_t *oend)
179 {
180 __u64 tmp_oend;
181 int retval;
182
183 retval = ext2fs_fudge_generic_bmap_end((ext2fs_generic_bitmap) bitmap,
184 EXT2_ET_FUDGE_INODE_BITMAP_END,
185 end, &tmp_oend);
186 if (oend)
187 *oend = tmp_oend;
188 return retval;
189 }
190
ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,blk_t end,blk_t * oend)191 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
192 blk_t end, blk_t *oend)
193 {
194 return (ext2fs_fudge_generic_bitmap_end(bitmap,
195 EXT2_ET_MAGIC_BLOCK_BITMAP,
196 EXT2_ET_FUDGE_BLOCK_BITMAP_END,
197 end, oend));
198 }
199
ext2fs_fudge_block_bitmap_end2(ext2fs_block_bitmap bitmap,blk64_t end,blk64_t * oend)200 errcode_t ext2fs_fudge_block_bitmap_end2(ext2fs_block_bitmap bitmap,
201 blk64_t end, blk64_t *oend)
202 {
203 return (ext2fs_fudge_generic_bmap_end(bitmap,
204 EXT2_ET_FUDGE_BLOCK_BITMAP_END,
205 end, oend));
206 }
207
ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)208 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
209 {
210 ext2fs_clear_generic_bmap(bitmap);
211 }
212
ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)213 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
214 {
215 ext2fs_clear_generic_bmap(bitmap);
216 }
217
ext2fs_resize_inode_bitmap(__u32 new_end,__u32 new_real_end,ext2fs_inode_bitmap bmap)218 errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
219 ext2fs_inode_bitmap bmap)
220 {
221 return (ext2fs_resize_generic_bitmap(EXT2_ET_MAGIC_INODE_BITMAP,
222 new_end, new_real_end, bmap));
223 }
224
ext2fs_resize_inode_bitmap2(__u64 new_end,__u64 new_real_end,ext2fs_inode_bitmap bmap)225 errcode_t ext2fs_resize_inode_bitmap2(__u64 new_end, __u64 new_real_end,
226 ext2fs_inode_bitmap bmap)
227 {
228 return (ext2fs_resize_generic_bmap(bmap, new_end, new_real_end));
229 }
230
ext2fs_resize_block_bitmap(__u32 new_end,__u32 new_real_end,ext2fs_block_bitmap bmap)231 errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
232 ext2fs_block_bitmap bmap)
233 {
234 return (ext2fs_resize_generic_bitmap(EXT2_ET_MAGIC_BLOCK_BITMAP,
235 new_end, new_real_end, bmap));
236 }
237
ext2fs_resize_block_bitmap2(__u64 new_end,__u64 new_real_end,ext2fs_block_bitmap bmap)238 errcode_t ext2fs_resize_block_bitmap2(__u64 new_end, __u64 new_real_end,
239 ext2fs_block_bitmap bmap)
240 {
241 return (ext2fs_resize_generic_bmap(bmap, new_end, new_real_end));
242 }
243
ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,ext2fs_block_bitmap bm2)244 errcode_t ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,
245 ext2fs_block_bitmap bm2)
246 {
247 return (ext2fs_compare_generic_bmap(EXT2_ET_NEQ_BLOCK_BITMAP,
248 bm1, bm2));
249 }
250
ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,ext2fs_inode_bitmap bm2)251 errcode_t ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,
252 ext2fs_inode_bitmap bm2)
253 {
254 return (ext2fs_compare_generic_bmap(EXT2_ET_NEQ_INODE_BITMAP,
255 bm1, bm2));
256 }
257
ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,ext2_ino_t start,unsigned int num,void * in)258 errcode_t ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,
259 ext2_ino_t start, unsigned int num,
260 void *in)
261 {
262 return (ext2fs_set_generic_bitmap_range(bmap,
263 EXT2_ET_MAGIC_INODE_BITMAP,
264 start, num, in));
265 }
266
ext2fs_set_inode_bitmap_range2(ext2fs_inode_bitmap bmap,__u64 start,size_t num,void * in)267 errcode_t ext2fs_set_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
268 __u64 start, size_t num,
269 void *in)
270 {
271 return (ext2fs_set_generic_bmap_range(bmap, start, num, in));
272 }
273
ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,ext2_ino_t start,unsigned int num,void * out)274 errcode_t ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,
275 ext2_ino_t start, unsigned int num,
276 void *out)
277 {
278 return (ext2fs_get_generic_bitmap_range(bmap,
279 EXT2_ET_MAGIC_INODE_BITMAP,
280 start, num, out));
281 }
282
ext2fs_get_inode_bitmap_range2(ext2fs_inode_bitmap bmap,__u64 start,size_t num,void * out)283 errcode_t ext2fs_get_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
284 __u64 start, size_t num,
285 void *out)
286 {
287 return (ext2fs_get_generic_bmap_range(bmap, start, num, out));
288 }
289
ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,blk_t start,unsigned int num,void * in)290 errcode_t ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,
291 blk_t start, unsigned int num,
292 void *in)
293 {
294 return (ext2fs_set_generic_bitmap_range(bmap,
295 EXT2_ET_MAGIC_BLOCK_BITMAP,
296 start, num, in));
297 }
298
ext2fs_set_block_bitmap_range2(ext2fs_block_bitmap bmap,blk64_t start,size_t num,void * in)299 errcode_t ext2fs_set_block_bitmap_range2(ext2fs_block_bitmap bmap,
300 blk64_t start, size_t num,
301 void *in)
302 {
303 return (ext2fs_set_generic_bmap_range(bmap, start, num, in));
304 }
305
ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,blk_t start,unsigned int num,void * out)306 errcode_t ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,
307 blk_t start, unsigned int num,
308 void *out)
309 {
310 return (ext2fs_get_generic_bitmap_range(bmap,
311 EXT2_ET_MAGIC_BLOCK_BITMAP,
312 start, num, out));
313 }
314
ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,blk64_t start,size_t num,void * out)315 errcode_t ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,
316 blk64_t start, size_t num,
317 void *out)
318 {
319 return (ext2fs_get_generic_bmap_range(bmap, start, num, out));
320 }
321