2 * rw_bitmaps.c --- routines to read and write the inode and block bitmaps.
4 * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
20 #ifdef HAVE_SYS_STAT_H
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
31 static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)
35 int block_nbytes, inode_nbytes;
38 char *block_buf = NULL, *inode_buf = NULL;
41 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
42 ext2_ino_t ino_itr = 1;
44 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
46 if (!(fs->flags & EXT2_FLAG_RW))
47 return EXT2_ET_RO_FILSYS;
49 csum_flag = ext2fs_has_group_desc_csum(fs);
51 inode_nbytes = block_nbytes = 0;
53 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
54 retval = io_channel_alloc_buf(fs->io, 0, &block_buf);
57 memset(block_buf, 0xff, fs->blocksize);
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);
65 memset(inode_buf, 0xff, fs->blocksize);
68 for (i = 0; i < fs->group_desc_count; i++) {
70 goto skip_block_bitmap;
72 if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)
74 goto skip_this_block_bitmap;
76 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
77 blk_itr, block_nbytes << 3, block_buf);
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)));
88 for (j = nbits; j < fs->blocksize * 8; j++)
89 ext2fs_set_bit(j, block_buf);
92 retval = ext2fs_block_bitmap_csum_set(fs, i, block_buf,
96 ext2fs_group_desc_csum_set(fs, i);
98 blk = ext2fs_block_bitmap_loc(fs, i);
100 retval = io_channel_write_blk64(fs->io, blk, 1,
103 retval = EXT2_ET_BLOCK_BITMAP_WRITE;
107 skip_this_block_bitmap:
108 blk_itr += block_nbytes << 3;
114 if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)
116 goto skip_this_inode_bitmap;
118 retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
119 ino_itr, inode_nbytes << 3, inode_buf);
123 retval = ext2fs_inode_bitmap_csum_set(fs, i, inode_buf,
127 ext2fs_group_desc_csum_set(fs, i);
129 blk = ext2fs_inode_bitmap_loc(fs, i);
131 retval = io_channel_write_blk64(fs->io, blk, 1,
134 retval = EXT2_ET_INODE_BITMAP_WRITE;
138 skip_this_inode_bitmap:
139 ino_itr += inode_nbytes << 3;
143 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
144 ext2fs_free_mem(&block_buf);
147 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
148 ext2fs_free_mem(&inode_buf);
153 ext2fs_free_mem(&inode_buf);
155 ext2fs_free_mem(&block_buf);
159 static errcode_t mark_uninit_bg_group_blocks(ext2_filsys fs)
163 ext2fs_block_bitmap bmap = fs->block_map;
165 for (i = 0; i < fs->group_desc_count; i++) {
166 if (!ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT))
169 ext2fs_reserve_super_and_bgd(fs, i, bmap);
172 * Mark the blocks used for the inode table
174 blk = ext2fs_inode_table_loc(fs, i);
176 ext2fs_mark_block_bitmap_range2(bmap, blk,
177 fs->inode_blocks_per_group);
180 * Mark block used for the block bitmap
182 blk = ext2fs_block_bitmap_loc(fs, i);
184 ext2fs_mark_block_bitmap2(bmap, blk);
187 * Mark block used for the inode bitmap
189 blk = ext2fs_inode_bitmap_loc(fs, i);
191 ext2fs_mark_block_bitmap2(bmap, blk);
196 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
199 char *block_bitmap = 0, *inode_bitmap = 0;
202 int block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
203 int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
207 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
209 ext2_ino_t ino_itr = 1;
212 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
214 if ((block_nbytes > (int) fs->blocksize) ||
215 (inode_nbytes > (int) fs->blocksize))
216 return EXT2_ET_CORRUPT_SUPERBLOCK;
218 fs->write_bitmaps = ext2fs_write_bitmaps;
220 csum_flag = ext2fs_has_group_desc_csum(fs);
222 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
227 ext2fs_free_block_bitmap(fs->block_map);
228 strcpy(buf, "block bitmap for ");
229 strcat(buf, fs->device_name);
230 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
233 retval = io_channel_alloc_buf(fs->io, 0, &block_bitmap);
240 ext2fs_free_inode_bitmap(fs->inode_map);
241 strcpy(buf, "inode bitmap for ");
242 strcat(buf, fs->device_name);
243 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
246 retval = io_channel_alloc_buf(fs->io, 0, &inode_bitmap);
251 ext2fs_free_mem(&buf);
253 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
254 blk = (fs->image_header->offset_inodemap / fs->blocksize);
255 ino_cnt = fs->super->s_inodes_count;
256 while (inode_nbytes > 0) {
257 retval = io_channel_read_blk64(fs->image_io, blk++,
261 cnt = fs->blocksize << 3;
264 retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
265 ino_itr, cnt, inode_bitmap);
268 ino_itr += fs->blocksize << 3;
269 ino_cnt -= fs->blocksize << 3;
270 inode_nbytes -= fs->blocksize;
272 blk = (fs->image_header->offset_blockmap /
274 blk_cnt = (blk64_t)EXT2_CLUSTERS_PER_GROUP(fs->super) *
275 fs->group_desc_count;
276 while (block_nbytes > 0) {
277 retval = io_channel_read_blk64(fs->image_io, blk++,
281 cnt = fs->blocksize << 3;
284 retval = ext2fs_set_block_bitmap_range2(fs->block_map,
285 blk_itr, cnt, block_bitmap);
288 blk_itr += fs->blocksize << 3;
289 blk_cnt -= fs->blocksize << 3;
290 block_nbytes -= fs->blocksize;
292 goto success_cleanup;
295 for (i = 0; i < fs->group_desc_count; i++) {
297 blk = ext2fs_block_bitmap_loc(fs, i);
299 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
300 ext2fs_group_desc_csum_verify(fs, i))
303 retval = io_channel_read_blk64(fs->io, blk,
306 retval = EXT2_ET_BLOCK_BITMAP_READ;
309 /* verify block bitmap checksum */
311 EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
312 !ext2fs_block_bitmap_csum_verify(fs, i,
313 block_bitmap, block_nbytes)) {
315 EXT2_ET_BLOCK_BITMAP_CSUM_INVALID;
319 memset(block_bitmap, 0, block_nbytes);
320 cnt = block_nbytes << 3;
321 retval = ext2fs_set_block_bitmap_range2(fs->block_map,
322 blk_itr, cnt, block_bitmap);
325 blk_itr += block_nbytes << 3;
328 blk = ext2fs_inode_bitmap_loc(fs, i);
330 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) &&
331 ext2fs_group_desc_csum_verify(fs, i))
334 retval = io_channel_read_blk64(fs->io, blk,
337 retval = EXT2_ET_INODE_BITMAP_READ;
341 /* verify inode bitmap checksum */
343 EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
344 !ext2fs_inode_bitmap_csum_verify(fs, i,
345 inode_bitmap, inode_nbytes)) {
347 EXT2_ET_INODE_BITMAP_CSUM_INVALID;
351 memset(inode_bitmap, 0, inode_nbytes);
352 cnt = inode_nbytes << 3;
353 retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
354 ino_itr, cnt, inode_bitmap);
357 ino_itr += inode_nbytes << 3;
361 /* Mark group blocks for any BLOCK_UNINIT groups */
363 retval = mark_uninit_bg_group_blocks(fs);
370 ext2fs_free_mem(&inode_bitmap);
372 ext2fs_free_mem(&block_bitmap);
377 ext2fs_free_mem(&fs->block_map);
381 ext2fs_free_mem(&fs->inode_map);
385 ext2fs_free_mem(&inode_bitmap);
387 ext2fs_free_mem(&block_bitmap);
389 ext2fs_free_mem(&buf);
393 errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
395 return read_bitmaps(fs, 1, 0);
398 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
400 return read_bitmaps(fs, 0, 1);
403 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
405 return write_bitmaps(fs, 1, 0);
408 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
410 return write_bitmaps(fs, 0, 1);
413 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
415 if (fs->inode_map && fs->block_map)
418 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
421 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
423 int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
424 int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
426 if (!do_inode && !do_block)
429 return write_bitmaps(fs, do_inode, do_block);