1 /* vi: set sw=4 ts=4: */
3 * rw_bitmaps.c --- routines to read and write the inode and block bitmaps.
5 * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
20 #ifdef HAVE_SYS_STAT_H
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
31 #if defined(__powerpc__) && BB_BIG_ENDIAN
33 * On the PowerPC, the big-endian variant of the ext2 filesystem
34 * has its bitmaps stored as 32-bit words with bit 0 as the LSB
35 * of each word. Thus a bitmap with only bit 0 set would be, as
36 * a string of bytes, 00 00 00 01 00 ...
37 * To cope with this, we byte-reverse each word of a bitmap if
38 * we have a big-endian filesystem, that is, if we are *not*
39 * byte-swapping other word-sized numbers.
41 #define EXT2_BIG_ENDIAN_BITMAPS
44 #ifdef EXT2_BIG_ENDIAN_BITMAPS
45 static void ext2fs_swap_bitmap(ext2_filsys fs, char *bitmap, int nbytes)
47 __u32 *p = (__u32 *) bitmap;
50 for (n = nbytes / sizeof(__u32); n > 0; --n, ++p)
51 *p = ext2fs_swab32(*p);
55 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
60 char * inode_bitmap = fs->inode_map->bitmap;
61 char * bitmap_block = NULL;
64 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
66 if (!(fs->flags & EXT2_FLAG_RW))
67 return EXT2_ET_RO_FILSYS;
70 nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
72 retval = ext2fs_get_mem(fs->blocksize, &bitmap_block);
75 memset(bitmap_block, 0xff, fs->blocksize);
76 for (i = 0; i < fs->group_desc_count; i++) {
77 memcpy(bitmap_block, inode_bitmap, nbytes);
78 blk = fs->group_desc[i].bg_inode_bitmap;
80 #ifdef EXT2_BIG_ENDIAN_BITMAPS
81 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
82 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
83 ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
85 retval = io_channel_write_blk(fs->io, blk, 1,
88 return EXT2_ET_INODE_BITMAP_WRITE;
90 inode_bitmap += nbytes;
92 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
93 ext2fs_free_mem(&bitmap_block);
97 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
104 char * block_bitmap = fs->block_map->bitmap;
105 char * bitmap_block = NULL;
108 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
110 if (!(fs->flags & EXT2_FLAG_RW))
111 return EXT2_ET_RO_FILSYS;
114 nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
115 retval = ext2fs_get_mem(fs->blocksize, &bitmap_block);
118 memset(bitmap_block, 0xff, fs->blocksize);
119 for (i = 0; i < fs->group_desc_count; i++) {
120 memcpy(bitmap_block, block_bitmap, nbytes);
121 if (i == fs->group_desc_count - 1) {
122 /* Force bitmap padding for the last group */
123 nbits = ((fs->super->s_blocks_count
124 - fs->super->s_first_data_block)
125 % EXT2_BLOCKS_PER_GROUP(fs->super));
127 for (j = nbits; j < fs->blocksize * 8; j++)
128 ext2fs_set_bit(j, bitmap_block);
130 blk = fs->group_desc[i].bg_block_bitmap;
132 #ifdef EXT2_BIG_ENDIAN_BITMAPS
133 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
134 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
135 ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
137 retval = io_channel_write_blk(fs->io, blk, 1,
140 return EXT2_ET_BLOCK_BITMAP_WRITE;
142 block_bitmap += nbytes;
144 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
145 ext2fs_free_mem(&bitmap_block);
149 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
152 char *block_bitmap = 0, *inode_bitmap = 0;
155 int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
156 int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8;
159 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
161 fs->write_bitmaps = ext2fs_write_bitmaps;
163 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
167 ext2fs_free_block_bitmap(fs->block_map);
168 sprintf(buf, "block bitmap for %s", fs->device_name);
169 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
172 block_bitmap = fs->block_map->bitmap;
175 ext2fs_free_inode_bitmap(fs->inode_map);
176 sprintf(buf, "inode bitmap for %s", fs->device_name);
177 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
180 inode_bitmap = fs->inode_map->bitmap;
182 ext2fs_free_mem(&buf);
184 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
186 blk = (fs->image_header->offset_inodemap /
188 retval = io_channel_read_blk(fs->image_io, blk,
189 -(inode_nbytes * fs->group_desc_count),
195 blk = (fs->image_header->offset_blockmap /
197 retval = io_channel_read_blk(fs->image_io, blk,
198 -(block_nbytes * fs->group_desc_count),
206 for (i = 0; i < fs->group_desc_count; i++) {
208 blk = fs->group_desc[i].bg_block_bitmap;
210 retval = io_channel_read_blk(fs->io, blk,
211 -block_nbytes, block_bitmap);
213 retval = EXT2_ET_BLOCK_BITMAP_READ;
216 #ifdef EXT2_BIG_ENDIAN_BITMAPS
217 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
218 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
219 ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
222 memset(block_bitmap, 0, block_nbytes);
223 block_bitmap += block_nbytes;
226 blk = fs->group_desc[i].bg_inode_bitmap;
228 retval = io_channel_read_blk(fs->io, blk,
229 -inode_nbytes, inode_bitmap);
231 retval = EXT2_ET_INODE_BITMAP_READ;
234 #ifdef EXT2_BIG_ENDIAN_BITMAPS
235 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
236 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
237 ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
240 memset(inode_bitmap, 0, inode_nbytes);
241 inode_bitmap += inode_nbytes;
248 ext2fs_free_mem(&fs->block_map);
251 ext2fs_free_mem(&fs->inode_map);
253 ext2fs_free_mem(&buf);
257 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
259 return read_bitmaps(fs, 1, 0);
262 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
264 return read_bitmaps(fs, 0, 1);
267 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
270 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
272 if (fs->inode_map && fs->block_map)
275 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
278 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
282 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
284 if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
285 retval = ext2fs_write_block_bitmap(fs);
289 if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
290 retval = ext2fs_write_inode_bitmap(fs);