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
21 #include <sys/types.h>
26 #include <linux/ext2_fs.h>
30 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
35 char * inode_bitmap = fs->inode_map->bitmap;
36 char * bitmap_block = NULL;
39 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
41 if (!(fs->flags & EXT2_FLAG_RW))
42 return EXT2_ET_RO_FILSYS;
45 nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
47 bitmap_block = malloc(fs->blocksize);
50 memset(bitmap_block, 0xff, fs->blocksize);
51 for (i = 0; i < fs->group_desc_count; i++) {
52 memcpy(bitmap_block, inode_bitmap, nbytes);
53 blk = fs->group_desc[i].bg_inode_bitmap;
55 retval = io_channel_write_blk(fs->io, blk, 1,
58 return EXT2_ET_INODE_BITMAP_WRITE;
60 inode_bitmap += nbytes;
62 fs->flags |= EXT2_FLAG_CHANGED;
63 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
68 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
75 char * block_bitmap = fs->block_map->bitmap;
76 char * bitmap_block = NULL;
79 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
81 if (!(fs->flags & EXT2_FLAG_RW))
82 return EXT2_ET_RO_FILSYS;
85 nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
86 bitmap_block = malloc(fs->blocksize);
89 memset(bitmap_block, 0xff, fs->blocksize);
90 for (i = 0; i < fs->group_desc_count; i++) {
91 memcpy(bitmap_block, block_bitmap, nbytes);
92 if (i == fs->group_desc_count - 1) {
93 /* Force bitmap padding for the last group */
94 nbits = (int) ((fs->super->s_blocks_count
95 - fs->super->s_first_data_block)
96 % EXT2_BLOCKS_PER_GROUP(fs->super));
98 for (j = nbits; j < fs->blocksize * 8; j++)
99 ext2fs_set_bit(j, bitmap_block);
101 blk = fs->group_desc[i].bg_block_bitmap;
103 retval = io_channel_write_blk(fs->io, blk, 1,
106 return EXT2_ET_BLOCK_BITMAP_WRITE;
108 block_bitmap += nbytes;
110 fs->flags |= EXT2_FLAG_CHANGED;
111 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
116 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
119 char *block_bitmap = 0, *inode_bitmap = 0;
122 int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
123 int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8;
126 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
128 fs->write_bitmaps = ext2fs_write_bitmaps;
130 buf = malloc(strlen(fs->device_name) + 80);
133 ext2fs_free_block_bitmap(fs->block_map);
134 sprintf(buf, "block bitmap for %s", fs->device_name);
135 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
138 block_bitmap = fs->block_map->bitmap;
142 ext2fs_free_inode_bitmap(fs->inode_map);
143 sprintf(buf, "inode bitmap for %s", fs->device_name);
144 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
147 inode_bitmap = fs->inode_map->bitmap;
151 for (i = 0; i < fs->group_desc_count; i++) {
153 blk = fs->group_desc[i].bg_block_bitmap;
155 retval = io_channel_read_blk(fs->io, blk,
156 -block_nbytes, block_bitmap);
158 retval = EXT2_ET_BLOCK_BITMAP_READ;
162 memset(block_bitmap, 0, block_nbytes);
163 block_bitmap += block_nbytes;
166 blk = fs->group_desc[i].bg_inode_bitmap;
168 retval = io_channel_read_blk(fs->io, blk,
169 -inode_nbytes, inode_bitmap);
171 retval = EXT2_ET_INODE_BITMAP_READ;
175 memset(inode_bitmap, 0, inode_nbytes);
176 inode_bitmap += inode_nbytes;
195 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
197 return read_bitmaps(fs, 1, 0);
200 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
202 return read_bitmaps(fs, 0, 1);
205 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
208 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
210 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
213 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
217 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
219 if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
220 retval = ext2fs_write_block_bitmap(fs);
224 if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
225 retval = ext2fs_write_inode_bitmap(fs);