• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	fs->write_bitmaps = ext2fs_write_bitmaps;
62 
63 	start = 1;
64 	end = fs->super->s_inodes_count;
65 	real_end = (EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count);
66 
67 	/* Are we permitted to use new-style bitmaps? */
68 	if (fs->flags & EXT2_FLAG_64BITS)
69 		return (ext2fs_alloc_generic_bmap(fs,
70 				EXT2_ET_MAGIC_INODE_BITMAP64,
71 				fs->default_bitmap_type,
72 				start, end, real_end, descr, ret));
73 
74 	/* Otherwise, check to see if the file system is small enough
75 	 * to use old-style 32-bit bitmaps */
76 	if ((end > ~0U) || (real_end > ~0U))
77 		return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
78 
79 	return (ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_INODE_BITMAP, fs,
80 					 start, end, real_end,
81 					 descr, 0,
82 					 (ext2fs_generic_bitmap *) ret));
83 }
84 
ext2fs_allocate_block_bitmap(ext2_filsys fs,const char * descr,ext2fs_block_bitmap * ret)85 errcode_t ext2fs_allocate_block_bitmap(ext2_filsys fs,
86 				       const char *descr,
87 				       ext2fs_block_bitmap *ret)
88 {
89 	__u64		start, end, real_end;
90 
91 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
92 
93 	fs->write_bitmaps = ext2fs_write_bitmaps;
94 
95 	start = EXT2FS_B2C(fs, fs->super->s_first_data_block);
96 	end = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1);
97 	real_end = ((__u64) EXT2_CLUSTERS_PER_GROUP(fs->super)
98 		    * (__u64) fs->group_desc_count)-1 + start;
99 
100 	if (fs->flags & EXT2_FLAG_64BITS)
101 		return (ext2fs_alloc_generic_bmap(fs,
102 				EXT2_ET_MAGIC_BLOCK_BITMAP64,
103 				fs->default_bitmap_type,
104 				start, end, real_end, descr, ret));
105 
106 	if ((end > ~0U) || (real_end > ~0U))
107 		return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
108 
109 	return (ext2fs_make_generic_bitmap(EXT2_ET_MAGIC_BLOCK_BITMAP, fs,
110 					   start, end, real_end,
111 					   descr, 0,
112 					   (ext2fs_generic_bitmap *) ret));
113 }
114 
115 /*
116  * ext2fs_allocate_block_bitmap() really allocates a per-cluster
117  * bitmap for backwards compatibility.  This function allocates a
118  * block bitmap which is truly per-block, even if clusters/bigalloc
119  * are enabled.  mke2fs and e2fsck need this for tracking the
120  * allocation of the file system metadata blocks.
121  */
ext2fs_allocate_subcluster_bitmap(ext2_filsys fs,const char * descr,ext2fs_block_bitmap * ret)122 errcode_t ext2fs_allocate_subcluster_bitmap(ext2_filsys fs,
123 					    const char *descr,
124 					    ext2fs_block_bitmap *ret)
125 {
126 	__u64			start, end, real_end;
127 	ext2fs_generic_bitmap	bmap;
128 	ext2fs_generic_bitmap_64 bmap64;
129 	errcode_t		retval;
130 
131 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
132 
133 	fs->write_bitmaps = ext2fs_write_bitmaps;
134 
135 	if (!fs->cluster_ratio_bits)
136 		return ext2fs_allocate_block_bitmap(fs, descr, ret);
137 
138 	if ((fs->flags & EXT2_FLAG_64BITS) == 0)
139 		return EXT2_ET_CANT_USE_LEGACY_BITMAPS;
140 
141 	start = fs->super->s_first_data_block;
142 	end = ext2fs_blocks_count(fs->super)-1;
143 	real_end = ((__u64) EXT2_BLOCKS_PER_GROUP(fs->super)
144 		    * (__u64) fs->group_desc_count)-1 + start;
145 
146 	retval = ext2fs_alloc_generic_bmap(fs, EXT2_ET_MAGIC_BLOCK_BITMAP64,
147 					   fs->default_bitmap_type, start,
148 					   end, real_end, descr, &bmap);
149 	if (retval)
150 		return retval;
151 	bmap64 = (ext2fs_generic_bitmap_64) bmap;
152 	bmap64->cluster_bits = 0;
153 	*ret = bmap;
154 	return 0;
155 }
156 
ext2fs_get_bitmap_granularity(ext2fs_block_bitmap bitmap)157 int ext2fs_get_bitmap_granularity(ext2fs_block_bitmap bitmap)
158 {
159 	ext2fs_generic_bitmap_64 bmap = (ext2fs_generic_bitmap_64) bitmap;
160 
161 	if (!EXT2FS_IS_64_BITMAP(bmap))
162 		return 0;
163 
164 	return bmap->cluster_bits;
165 }
166 
ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,ext2_ino_t end,ext2_ino_t * oend)167 errcode_t ext2fs_fudge_inode_bitmap_end(ext2fs_inode_bitmap bitmap,
168 					ext2_ino_t end, ext2_ino_t *oend)
169 {
170 	__u64 tmp_oend;
171 	int retval;
172 
173 	retval = ext2fs_fudge_generic_bmap_end((ext2fs_generic_bitmap) bitmap,
174 					       EXT2_ET_FUDGE_INODE_BITMAP_END,
175 					       end, &tmp_oend);
176 	if (oend)
177 		*oend = tmp_oend;
178 	return retval;
179 }
180 
ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,blk_t end,blk_t * oend)181 errcode_t ext2fs_fudge_block_bitmap_end(ext2fs_block_bitmap bitmap,
182 					blk_t end, blk_t *oend)
183 {
184 	return (ext2fs_fudge_generic_bitmap_end(bitmap,
185 						EXT2_ET_MAGIC_BLOCK_BITMAP,
186 						EXT2_ET_FUDGE_BLOCK_BITMAP_END,
187 						end, oend));
188 }
189 
ext2fs_fudge_block_bitmap_end2(ext2fs_block_bitmap bitmap,blk64_t end,blk64_t * oend)190 errcode_t ext2fs_fudge_block_bitmap_end2(ext2fs_block_bitmap bitmap,
191 					 blk64_t end, blk64_t *oend)
192 {
193 	return (ext2fs_fudge_generic_bmap_end(bitmap,
194 					      EXT2_ET_FUDGE_BLOCK_BITMAP_END,
195 					      end, oend));
196 }
197 
ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)198 void ext2fs_clear_inode_bitmap(ext2fs_inode_bitmap bitmap)
199 {
200 	ext2fs_clear_generic_bmap(bitmap);
201 }
202 
ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)203 void ext2fs_clear_block_bitmap(ext2fs_block_bitmap bitmap)
204 {
205 	ext2fs_clear_generic_bmap(bitmap);
206 }
207 
ext2fs_resize_inode_bitmap(__u32 new_end,__u32 new_real_end,ext2fs_inode_bitmap bmap)208 errcode_t ext2fs_resize_inode_bitmap(__u32 new_end, __u32 new_real_end,
209 				     ext2fs_inode_bitmap bmap)
210 {
211 	return (ext2fs_resize_generic_bitmap(EXT2_ET_MAGIC_INODE_BITMAP,
212 					     new_end, new_real_end, bmap));
213 }
214 
ext2fs_resize_inode_bitmap2(__u64 new_end,__u64 new_real_end,ext2fs_inode_bitmap bmap)215 errcode_t ext2fs_resize_inode_bitmap2(__u64 new_end, __u64 new_real_end,
216 				      ext2fs_inode_bitmap bmap)
217 {
218 	return (ext2fs_resize_generic_bmap(bmap, new_end, new_real_end));
219 }
220 
ext2fs_resize_block_bitmap(__u32 new_end,__u32 new_real_end,ext2fs_block_bitmap bmap)221 errcode_t ext2fs_resize_block_bitmap(__u32 new_end, __u32 new_real_end,
222 				     ext2fs_block_bitmap bmap)
223 {
224 	return (ext2fs_resize_generic_bitmap(EXT2_ET_MAGIC_BLOCK_BITMAP,
225 					     new_end, new_real_end, bmap));
226 }
227 
ext2fs_resize_block_bitmap2(__u64 new_end,__u64 new_real_end,ext2fs_block_bitmap bmap)228 errcode_t ext2fs_resize_block_bitmap2(__u64 new_end, __u64 new_real_end,
229 				      ext2fs_block_bitmap bmap)
230 {
231 	return (ext2fs_resize_generic_bmap(bmap, new_end, new_real_end));
232 }
233 
ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,ext2fs_block_bitmap bm2)234 errcode_t ext2fs_compare_block_bitmap(ext2fs_block_bitmap bm1,
235 				      ext2fs_block_bitmap bm2)
236 {
237 	return (ext2fs_compare_generic_bmap(EXT2_ET_NEQ_BLOCK_BITMAP,
238 					    bm1, bm2));
239 }
240 
ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,ext2fs_inode_bitmap bm2)241 errcode_t ext2fs_compare_inode_bitmap(ext2fs_inode_bitmap bm1,
242 				      ext2fs_inode_bitmap bm2)
243 {
244 	return (ext2fs_compare_generic_bmap(EXT2_ET_NEQ_INODE_BITMAP,
245 					    bm1, bm2));
246 }
247 
ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,ext2_ino_t start,unsigned int num,void * in)248 errcode_t ext2fs_set_inode_bitmap_range(ext2fs_inode_bitmap bmap,
249 					ext2_ino_t start, unsigned int num,
250 					void *in)
251 {
252 	return (ext2fs_set_generic_bitmap_range(bmap,
253 						EXT2_ET_MAGIC_INODE_BITMAP,
254 						start, num, in));
255 }
256 
ext2fs_set_inode_bitmap_range2(ext2fs_inode_bitmap bmap,__u64 start,size_t num,void * in)257 errcode_t ext2fs_set_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
258 					 __u64 start, size_t num,
259 					 void *in)
260 {
261 	return (ext2fs_set_generic_bmap_range(bmap, start, num, in));
262 }
263 
ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,ext2_ino_t start,unsigned int num,void * out)264 errcode_t ext2fs_get_inode_bitmap_range(ext2fs_inode_bitmap bmap,
265 					ext2_ino_t start, unsigned int num,
266 					void *out)
267 {
268 	return (ext2fs_get_generic_bitmap_range(bmap,
269 						EXT2_ET_MAGIC_INODE_BITMAP,
270 						start, num, out));
271 }
272 
ext2fs_get_inode_bitmap_range2(ext2fs_inode_bitmap bmap,__u64 start,size_t num,void * out)273 errcode_t ext2fs_get_inode_bitmap_range2(ext2fs_inode_bitmap bmap,
274 					 __u64 start, size_t num,
275 					 void *out)
276 {
277 	return (ext2fs_get_generic_bmap_range(bmap, start, num, out));
278 }
279 
ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,blk_t start,unsigned int num,void * in)280 errcode_t ext2fs_set_block_bitmap_range(ext2fs_block_bitmap bmap,
281 					blk_t start, unsigned int num,
282 					void *in)
283 {
284 	return (ext2fs_set_generic_bitmap_range(bmap,
285 						EXT2_ET_MAGIC_BLOCK_BITMAP,
286 						start, num, in));
287 }
288 
ext2fs_set_block_bitmap_range2(ext2fs_block_bitmap bmap,blk64_t start,size_t num,void * in)289 errcode_t ext2fs_set_block_bitmap_range2(ext2fs_block_bitmap bmap,
290 					 blk64_t start, size_t num,
291 					 void *in)
292 {
293 	return (ext2fs_set_generic_bmap_range(bmap, start, num, in));
294 }
295 
ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,blk_t start,unsigned int num,void * out)296 errcode_t ext2fs_get_block_bitmap_range(ext2fs_block_bitmap bmap,
297 					blk_t start, unsigned int num,
298 					void *out)
299 {
300 	return (ext2fs_get_generic_bitmap_range(bmap,
301 						EXT2_ET_MAGIC_BLOCK_BITMAP,
302 						start, num, out));
303 }
304 
ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,blk64_t start,size_t num,void * out)305 errcode_t ext2fs_get_block_bitmap_range2(ext2fs_block_bitmap bmap,
306 					 blk64_t start, size_t num,
307 					 void *out)
308 {
309 	return (ext2fs_get_generic_bmap_range(bmap, start, num, out));
310 }
311