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 #ifdef HAVE_SYS_STAT_H
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
30 #if defined(__powerpc__) && defined(EXT2FS_ENABLE_SWAPFS)
32 * On the PowerPC, the big-endian variant of the ext2 filesystem
33 * has its bitmaps stored as 32-bit words with bit 0 as the LSB
34 * of each word. Thus a bitmap with only bit 0 set would be, as
35 * a string of bytes, 00 00 00 01 00 ...
36 * To cope with this, we byte-reverse each word of a bitmap if
37 * we have a big-endian filesystem, that is, if we are *not*
38 * byte-swapping other word-sized numbers.
40 #define EXT2_BIG_ENDIAN_BITMAPS
43 #ifdef EXT2_BIG_ENDIAN_BITMAPS
44 static void ext2fs_swap_bitmap(ext2_filsys fs, char *bitmap, int nbytes)
46 __u32 *p = (__u32 *) bitmap;
49 for (n = nbytes / sizeof(__u32); n > 0; --n, ++p)
50 *p = ext2fs_swab32(*p);
54 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
59 char * inode_bitmap = fs->inode_map->bitmap;
60 char * bitmap_block = NULL;
63 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
65 if (!(fs->flags & EXT2_FLAG_RW))
66 return EXT2_ET_RO_FILSYS;
69 nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
71 retval = ext2fs_get_mem(fs->blocksize, (void **) &bitmap_block);
74 memset(bitmap_block, 0xff, fs->blocksize);
75 for (i = 0; i < fs->group_desc_count; i++) {
76 memcpy(bitmap_block, inode_bitmap, nbytes);
77 blk = fs->group_desc[i].bg_inode_bitmap;
79 #ifdef EXT2_BIG_ENDIAN_BITMAPS
80 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
81 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
82 ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
84 retval = io_channel_write_blk(fs->io, blk, 1,
87 return EXT2_ET_INODE_BITMAP_WRITE;
89 inode_bitmap += nbytes;
91 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
92 ext2fs_free_mem((void **) &bitmap_block);
96 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
103 char * block_bitmap = fs->block_map->bitmap;
104 char * bitmap_block = NULL;
107 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
109 if (!(fs->flags & EXT2_FLAG_RW))
110 return EXT2_ET_RO_FILSYS;
113 nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
114 retval = ext2fs_get_mem(fs->blocksize, (void **) &bitmap_block);
117 memset(bitmap_block, 0xff, fs->blocksize);
118 for (i = 0; i < fs->group_desc_count; i++) {
119 memcpy(bitmap_block, block_bitmap, nbytes);
120 if (i == fs->group_desc_count - 1) {
121 /* Force bitmap padding for the last group */
122 nbits = (int) ((fs->super->s_blocks_count
123 - fs->super->s_first_data_block)
124 % EXT2_BLOCKS_PER_GROUP(fs->super));
126 for (j = nbits; j < fs->blocksize * 8; j++)
127 ext2fs_set_bit(j, bitmap_block);
129 blk = fs->group_desc[i].bg_block_bitmap;
131 #ifdef EXT2_BIG_ENDIAN_BITMAPS
132 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
133 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
134 ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
136 retval = io_channel_write_blk(fs->io, blk, 1,
139 return EXT2_ET_BLOCK_BITMAP_WRITE;
141 block_bitmap += nbytes;
143 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
144 ext2fs_free_mem((void **) &bitmap_block);
148 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
151 char *block_bitmap = 0, *inode_bitmap = 0;
154 int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
155 int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8;
158 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
160 fs->write_bitmaps = ext2fs_write_bitmaps;
162 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, (void **) &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;
176 ext2fs_free_inode_bitmap(fs->inode_map);
177 sprintf(buf, "inode bitmap for %s", fs->device_name);
178 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
181 inode_bitmap = fs->inode_map->bitmap;
183 ext2fs_free_mem((void **) &buf);
185 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
187 blk = (fs->image_header->offset_inodemap /
189 retval = io_channel_read_blk(fs->io, blk,
190 -(inode_nbytes * fs->group_desc_count),
196 blk = (fs->image_header->offset_blockmap /
198 retval = io_channel_read_blk(fs->io, blk,
199 -(block_nbytes * fs->group_desc_count),
207 for (i = 0; i < fs->group_desc_count; i++) {
209 blk = fs->group_desc[i].bg_block_bitmap;
211 retval = io_channel_read_blk(fs->io, blk,
212 -block_nbytes, block_bitmap);
214 retval = EXT2_ET_BLOCK_BITMAP_READ;
217 #ifdef EXT2_BIG_ENDIAN_BITMAPS
218 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
219 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
220 ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
223 memset(block_bitmap, 0, block_nbytes);
224 block_bitmap += block_nbytes;
227 blk = fs->group_desc[i].bg_inode_bitmap;
229 retval = io_channel_read_blk(fs->io, blk,
230 -inode_nbytes, inode_bitmap);
232 retval = EXT2_ET_INODE_BITMAP_READ;
235 #ifdef EXT2_BIG_ENDIAN_BITMAPS
236 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
237 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
238 ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
241 memset(inode_bitmap, 0, inode_nbytes);
242 inode_bitmap += inode_nbytes;
249 ext2fs_free_mem((void **) &fs->block_map);
253 ext2fs_free_mem((void **) &fs->inode_map);
257 ext2fs_free_mem((void **) &buf);
261 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
263 return read_bitmaps(fs, 1, 0);
266 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
268 return read_bitmaps(fs, 0, 1);
271 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
274 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
276 if (fs->inode_map && fs->block_map)
279 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
282 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
286 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
288 if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
289 retval = ext2fs_write_block_bitmap(fs);
293 if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
294 retval = ext2fs_write_inode_bitmap(fs);