• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * rw_bitmaps.c --- routines to read and write the  inode and block bitmaps.
3  *
4  * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11 
12 #include "config.h"
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 #include "e2image.h"
30 
write_bitmaps(ext2_filsys fs,int do_inode,int do_block)31 static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)
32 {
33 	dgrp_t 		i;
34 	unsigned int	j;
35 	int		block_nbytes, inode_nbytes;
36 	unsigned int	nbits;
37 	errcode_t	retval;
38 	char		*block_buf = NULL, *inode_buf = NULL;
39 	int		csum_flag;
40 	blk64_t		blk;
41 	blk64_t		blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
42 	ext2_ino_t	ino_itr = 1;
43 
44 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
45 
46 	if (!(fs->flags & EXT2_FLAG_RW))
47 		return EXT2_ET_RO_FILSYS;
48 
49 	csum_flag = ext2fs_has_group_desc_csum(fs);
50 
51 	inode_nbytes = block_nbytes = 0;
52 	if (do_block) {
53 		block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
54 		retval = io_channel_alloc_buf(fs->io, 0, &block_buf);
55 		if (retval)
56 			goto errout;
57 		memset(block_buf, 0xff, fs->blocksize);
58 	}
59 	if (do_inode) {
60 		inode_nbytes = (size_t)
61 			((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
62 		retval = io_channel_alloc_buf(fs->io, 0, &inode_buf);
63 		if (retval)
64 			goto errout;
65 		memset(inode_buf, 0xff, fs->blocksize);
66 	}
67 
68 	for (i = 0; i < fs->group_desc_count; i++) {
69 		if (!do_block)
70 			goto skip_block_bitmap;
71 
72 		if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)
73 		    )
74 			goto skip_this_block_bitmap;
75 
76 		retval = ext2fs_get_block_bitmap_range2(fs->block_map,
77 				blk_itr, block_nbytes << 3, block_buf);
78 		if (retval)
79 			goto errout;
80 
81 		if (i == fs->group_desc_count - 1) {
82 			/* Force bitmap padding for the last group */
83 			nbits = EXT2FS_NUM_B2C(fs,
84 				((ext2fs_blocks_count(fs->super)
85 				  - (__u64) fs->super->s_first_data_block)
86 				 % (__u64) EXT2_BLOCKS_PER_GROUP(fs->super)));
87 			if (nbits)
88 				for (j = nbits; j < fs->blocksize * 8; j++)
89 					ext2fs_set_bit(j, block_buf);
90 		}
91 
92 		retval = ext2fs_block_bitmap_csum_set(fs, i, block_buf,
93 						      block_nbytes);
94 		if (retval)
95 			return retval;
96 		ext2fs_group_desc_csum_set(fs, i);
97 		fs->flags |= EXT2_FLAG_DIRTY;
98 
99 		blk = ext2fs_block_bitmap_loc(fs, i);
100 		if (blk) {
101 			retval = io_channel_write_blk64(fs->io, blk, 1,
102 							block_buf);
103 			if (retval) {
104 				retval = EXT2_ET_BLOCK_BITMAP_WRITE;
105 				goto errout;
106 			}
107 		}
108 	skip_this_block_bitmap:
109 		blk_itr += block_nbytes << 3;
110 	skip_block_bitmap:
111 
112 		if (!do_inode)
113 			continue;
114 
115 		if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)
116 		    )
117 			goto skip_this_inode_bitmap;
118 
119 		retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
120 				ino_itr, inode_nbytes << 3, inode_buf);
121 		if (retval)
122 			goto errout;
123 
124 		retval = ext2fs_inode_bitmap_csum_set(fs, i, inode_buf,
125 						      inode_nbytes);
126 		if (retval)
127 			goto errout;
128 		ext2fs_group_desc_csum_set(fs, i);
129 		fs->flags |= EXT2_FLAG_DIRTY;
130 
131 		blk = ext2fs_inode_bitmap_loc(fs, i);
132 		if (blk) {
133 			retval = io_channel_write_blk64(fs->io, blk, 1,
134 						      inode_buf);
135 			if (retval) {
136 				retval = EXT2_ET_INODE_BITMAP_WRITE;
137 				goto errout;
138 			}
139 		}
140 	skip_this_inode_bitmap:
141 		ino_itr += inode_nbytes << 3;
142 
143 	}
144 	if (do_block) {
145 		fs->flags &= ~EXT2_FLAG_BB_DIRTY;
146 		ext2fs_free_mem(&block_buf);
147 	}
148 	if (do_inode) {
149 		fs->flags &= ~EXT2_FLAG_IB_DIRTY;
150 		ext2fs_free_mem(&inode_buf);
151 	}
152 	return 0;
153 errout:
154 	if (inode_buf)
155 		ext2fs_free_mem(&inode_buf);
156 	if (block_buf)
157 		ext2fs_free_mem(&block_buf);
158 	return retval;
159 }
160 
mark_uninit_bg_group_blocks(ext2_filsys fs)161 static errcode_t mark_uninit_bg_group_blocks(ext2_filsys fs)
162 {
163 	dgrp_t			i;
164 	blk64_t			blk;
165 	ext2fs_block_bitmap	bmap = fs->block_map;
166 
167 	for (i = 0; i < fs->group_desc_count; i++) {
168 		if (!ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT))
169 			continue;
170 
171 		ext2fs_reserve_super_and_bgd(fs, i, bmap);
172 
173 		/*
174 		 * Mark the blocks used for the inode table
175 		 */
176 		blk = ext2fs_inode_table_loc(fs, i);
177 		if (blk)
178 			ext2fs_mark_block_bitmap_range2(bmap, blk,
179 						fs->inode_blocks_per_group);
180 
181 		/*
182 		 * Mark block used for the block bitmap
183 		 */
184 		blk = ext2fs_block_bitmap_loc(fs, i);
185 		if (blk)
186 			ext2fs_mark_block_bitmap2(bmap, blk);
187 
188 		/*
189 		 * Mark block used for the inode bitmap
190 		 */
191 		blk = ext2fs_inode_bitmap_loc(fs, i);
192 		if (blk)
193 			ext2fs_mark_block_bitmap2(bmap, blk);
194 	}
195 	return 0;
196 }
197 
bitmap_tail_verify(unsigned char * bitmap,int first,int last)198 static int bitmap_tail_verify(unsigned char *bitmap, int first, int last)
199 {
200 	int i;
201 
202 	for (i = first; i <= last; i++)
203 		if (bitmap[i] != 0xff)
204 			return 0;
205 	return 1;
206 }
207 
read_bitmaps(ext2_filsys fs,int do_inode,int do_block)208 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
209 {
210 	dgrp_t i;
211 	char *block_bitmap = 0, *inode_bitmap = 0;
212 	char *buf;
213 	errcode_t retval;
214 	int block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
215 	int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
216 	int tail_flags = 0;
217 	int csum_flag;
218 	unsigned int	cnt;
219 	blk64_t	blk;
220 	blk64_t	blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
221 	blk64_t   blk_cnt;
222 	ext2_ino_t ino_itr = 1;
223 	ext2_ino_t ino_cnt;
224 
225 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
226 
227 	if ((block_nbytes > (int) fs->blocksize) ||
228 	    (inode_nbytes > (int) fs->blocksize))
229 		return EXT2_ET_CORRUPT_SUPERBLOCK;
230 
231 	fs->write_bitmaps = ext2fs_write_bitmaps;
232 
233 	csum_flag = ext2fs_has_group_desc_csum(fs);
234 
235 	retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
236 	if (retval)
237 		return retval;
238 	if (do_block) {
239 		if (fs->block_map)
240 			ext2fs_free_block_bitmap(fs->block_map);
241 		strcpy(buf, "block bitmap for ");
242 		strcat(buf, fs->device_name);
243 		retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
244 		if (retval)
245 			goto cleanup;
246 		retval = io_channel_alloc_buf(fs->io, 0, &block_bitmap);
247 		if (retval)
248 			goto cleanup;
249 	} else
250 		block_nbytes = 0;
251 	if (do_inode) {
252 		if (fs->inode_map)
253 			ext2fs_free_inode_bitmap(fs->inode_map);
254 		strcpy(buf, "inode bitmap for ");
255 		strcat(buf, fs->device_name);
256 		retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
257 		if (retval)
258 			goto cleanup;
259 		retval = io_channel_alloc_buf(fs->io, 0, &inode_bitmap);
260 		if (retval)
261 			goto cleanup;
262 	} else
263 		inode_nbytes = 0;
264 	ext2fs_free_mem(&buf);
265 
266 	if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
267 		blk = (ext2fs_le32_to_cpu(fs->image_header->offset_inodemap) / fs->blocksize);
268 		ino_cnt = fs->super->s_inodes_count;
269 		while (inode_bitmap && ino_cnt > 0) {
270 			retval = io_channel_read_blk64(fs->image_io, blk++,
271 						     1, inode_bitmap);
272 			if (retval)
273 				goto cleanup;
274 			cnt = fs->blocksize << 3;
275 			if (cnt > ino_cnt)
276 				cnt = ino_cnt;
277 			retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
278 					       ino_itr, cnt, inode_bitmap);
279 			if (retval)
280 				goto cleanup;
281 			ino_itr += cnt;
282 			ino_cnt -= cnt;
283 		}
284 		blk = (ext2fs_le32_to_cpu(fs->image_header->offset_blockmap) /
285 		       fs->blocksize);
286 		blk_cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super,
287 						  fs->group_desc_count);
288 		while (block_bitmap && blk_cnt > 0) {
289 			retval = io_channel_read_blk64(fs->image_io, blk++,
290 						     1, block_bitmap);
291 			if (retval)
292 				goto cleanup;
293 			cnt = fs->blocksize << 3;
294 			if (cnt > blk_cnt)
295 				cnt = blk_cnt;
296 			retval = ext2fs_set_block_bitmap_range2(fs->block_map,
297 				       blk_itr, cnt, block_bitmap);
298 			if (retval)
299 				goto cleanup;
300 			blk_itr += cnt;
301 			blk_cnt -= cnt;
302 		}
303 		goto success_cleanup;
304 	}
305 
306 	for (i = 0; i < fs->group_desc_count; i++) {
307 		if (block_bitmap) {
308 			blk = ext2fs_block_bitmap_loc(fs, i);
309 			if ((csum_flag &&
310 			     ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
311 			     ext2fs_group_desc_csum_verify(fs, i)) ||
312 			    (blk >= ext2fs_blocks_count(fs->super)))
313 				blk = 0;
314 			if (blk) {
315 				retval = io_channel_read_blk64(fs->io, blk,
316 							       1, block_bitmap);
317 				if (retval) {
318 					retval = EXT2_ET_BLOCK_BITMAP_READ;
319 					goto cleanup;
320 				}
321 				/* verify block bitmap checksum */
322 				if (!(fs->flags &
323 				      EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
324 				    !ext2fs_block_bitmap_csum_verify(fs, i,
325 						block_bitmap, block_nbytes)) {
326 					retval =
327 					EXT2_ET_BLOCK_BITMAP_CSUM_INVALID;
328 					goto cleanup;
329 				}
330 				if (!bitmap_tail_verify((unsigned char *) block_bitmap,
331 							block_nbytes, fs->blocksize - 1))
332 					tail_flags |= EXT2_FLAG_BBITMAP_TAIL_PROBLEM;
333 			} else
334 				memset(block_bitmap, 0, block_nbytes);
335 			cnt = block_nbytes << 3;
336 			retval = ext2fs_set_block_bitmap_range2(fs->block_map,
337 					       blk_itr, cnt, block_bitmap);
338 			if (retval)
339 				goto cleanup;
340 			blk_itr += block_nbytes << 3;
341 		}
342 		if (inode_bitmap) {
343 			blk = ext2fs_inode_bitmap_loc(fs, i);
344 			if ((csum_flag &&
345 			     ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) &&
346 			     ext2fs_group_desc_csum_verify(fs, i)) ||
347 			    (blk >= ext2fs_blocks_count(fs->super)))
348 				blk = 0;
349 			if (blk) {
350 				retval = io_channel_read_blk64(fs->io, blk,
351 							       1, inode_bitmap);
352 				if (retval) {
353 					retval = EXT2_ET_INODE_BITMAP_READ;
354 					goto cleanup;
355 				}
356 
357 				/* verify inode bitmap checksum */
358 				if (!(fs->flags &
359 				      EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
360 				    !ext2fs_inode_bitmap_csum_verify(fs, i,
361 						inode_bitmap, inode_nbytes)) {
362 					retval =
363 					EXT2_ET_INODE_BITMAP_CSUM_INVALID;
364 					goto cleanup;
365 				}
366 				if (!bitmap_tail_verify((unsigned char *) inode_bitmap,
367 							inode_nbytes, fs->blocksize - 1))
368 					tail_flags |= EXT2_FLAG_IBITMAP_TAIL_PROBLEM;
369 			} else
370 				memset(inode_bitmap, 0, inode_nbytes);
371 			cnt = inode_nbytes << 3;
372 			retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
373 					       ino_itr, cnt, inode_bitmap);
374 			if (retval)
375 				goto cleanup;
376 			ino_itr += inode_nbytes << 3;
377 		}
378 	}
379 
380 	/* Mark group blocks for any BLOCK_UNINIT groups */
381 	if (do_block) {
382 		retval = mark_uninit_bg_group_blocks(fs);
383 		if (retval)
384 			goto cleanup;
385 	}
386 
387 success_cleanup:
388 	if (inode_bitmap) {
389 		ext2fs_free_mem(&inode_bitmap);
390 		fs->flags &= ~EXT2_FLAG_IBITMAP_TAIL_PROBLEM;
391 	}
392 	if (block_bitmap) {
393 		ext2fs_free_mem(&block_bitmap);
394 		fs->flags &= ~EXT2_FLAG_BBITMAP_TAIL_PROBLEM;
395 	}
396 	fs->flags |= tail_flags;
397 	return 0;
398 
399 cleanup:
400 	if (do_block) {
401 		ext2fs_free_block_bitmap(fs->block_map);
402 		fs->block_map = 0;
403 	}
404 	if (do_inode) {
405 		ext2fs_free_inode_bitmap(fs->inode_map);
406 		fs->inode_map = 0;
407 	}
408 	if (inode_bitmap)
409 		ext2fs_free_mem(&inode_bitmap);
410 	if (block_bitmap)
411 		ext2fs_free_mem(&block_bitmap);
412 	if (buf)
413 		ext2fs_free_mem(&buf);
414 	return retval;
415 }
416 
ext2fs_read_inode_bitmap(ext2_filsys fs)417 errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
418 {
419 	return read_bitmaps(fs, 1, 0);
420 }
421 
ext2fs_read_block_bitmap(ext2_filsys fs)422 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
423 {
424 	return read_bitmaps(fs, 0, 1);
425 }
426 
ext2fs_write_inode_bitmap(ext2_filsys fs)427 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
428 {
429 	return write_bitmaps(fs, 1, 0);
430 }
431 
ext2fs_write_block_bitmap(ext2_filsys fs)432 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
433 {
434 	return write_bitmaps(fs, 0, 1);
435 }
436 
ext2fs_read_bitmaps(ext2_filsys fs)437 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
438 {
439 	if (fs->inode_map && fs->block_map)
440 		return 0;
441 
442 	return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
443 }
444 
ext2fs_write_bitmaps(ext2_filsys fs)445 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
446 {
447 	int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
448 	int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
449 
450 	if (!do_inode && !do_block)
451 		return 0;
452 
453 	return write_bitmaps(fs, do_inode, do_block);
454 }
455