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 Public
19 #include <sys/types.h>
24 #include <linux/ext2_fs.h>
28 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
33 char * inode_bitmap = fs->inode_map->bitmap;
34 char * bitmap_block = NULL;
37 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
39 if (!(fs->flags & EXT2_FLAG_RW))
40 return EXT2_ET_RO_FILSYS;
43 nbytes = (EXT2_INODES_PER_GROUP(fs->super)+7) / 8;
45 bitmap_block = malloc(fs->blocksize);
48 memset(bitmap_block, 0xff, fs->blocksize);
49 for (i = 0; i < fs->group_desc_count; i++) {
50 memcpy(bitmap_block, inode_bitmap, nbytes);
51 blk = fs->group_desc[i].bg_inode_bitmap;
53 retval = io_channel_write_blk(fs->io, blk, 1,
56 return EXT2_ET_INODE_BITMAP_WRITE;
58 inode_bitmap += nbytes;
60 fs->flags |= EXT2_FLAG_CHANGED;
61 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
66 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
73 char * block_bitmap = fs->block_map->bitmap;
74 char * bitmap_block = NULL;
77 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
79 if (!(fs->flags & EXT2_FLAG_RW))
80 return EXT2_ET_RO_FILSYS;
83 nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
84 bitmap_block = malloc(fs->blocksize);
87 memset(bitmap_block, 0xff, fs->blocksize);
88 for (i = 0; i < fs->group_desc_count; i++) {
89 memcpy(bitmap_block, block_bitmap, nbytes);
90 if (i == fs->group_desc_count - 1) {
91 /* Force bitmap padding for the last group */
92 nbits = (fs->super->s_blocks_count
93 - fs->super->s_first_data_block)
94 % EXT2_BLOCKS_PER_GROUP(fs->super);
96 for (j = nbits; j < fs->blocksize * 8; j++)
97 ext2fs_set_bit(j, bitmap_block);
99 blk = fs->group_desc[i].bg_block_bitmap;
101 retval = io_channel_write_blk(fs->io, blk, 1,
104 return EXT2_ET_BLOCK_BITMAP_WRITE;
106 block_bitmap += nbytes;
108 fs->flags |= EXT2_FLAG_CHANGED;
109 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
114 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
117 char *block_bitmap = 0, *inode_bitmap = 0;
120 int block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
121 int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
124 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
126 fs->write_bitmaps = ext2fs_write_bitmaps;
128 buf = malloc(strlen(fs->device_name) + 80);
131 ext2fs_free_block_bitmap(fs->block_map);
132 sprintf(buf, "block bitmap for %s", fs->device_name);
133 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
136 block_bitmap = fs->block_map->bitmap;
140 ext2fs_free_inode_bitmap(fs->inode_map);
141 sprintf(buf, "inode bitmap for %s", fs->device_name);
142 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
145 inode_bitmap = fs->inode_map->bitmap;
149 for (i = 0; i < fs->group_desc_count; i++) {
151 blk = fs->group_desc[i].bg_block_bitmap;
153 retval = io_channel_read_blk(fs->io, blk,
154 -block_nbytes, block_bitmap);
156 retval = EXT2_ET_BLOCK_BITMAP_READ;
160 memset(block_bitmap, 0, block_nbytes);
161 block_bitmap += block_nbytes;
164 blk = fs->group_desc[i].bg_inode_bitmap;
166 retval = io_channel_read_blk(fs->io, blk,
167 -inode_nbytes, inode_bitmap);
169 retval = EXT2_ET_INODE_BITMAP_READ;
173 memset(inode_bitmap, 0, inode_nbytes);
174 inode_bitmap += inode_nbytes;
193 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
195 return read_bitmaps(fs, 1, 0);
198 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
200 return read_bitmaps(fs, 0, 1);
203 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
206 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
208 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
211 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
215 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
217 if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
218 retval = ext2fs_write_block_bitmap(fs);
222 if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
223 retval = ext2fs_write_inode_bitmap(fs);