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, &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(&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, &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 = ((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(&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, &buf);
166 ext2fs_free_block_bitmap(fs->block_map);
167 sprintf(buf, "block bitmap for %s", fs->device_name);
168 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
171 block_bitmap = fs->block_map->bitmap;
174 ext2fs_free_inode_bitmap(fs->inode_map);
175 sprintf(buf, "inode bitmap for %s", fs->device_name);
176 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
179 inode_bitmap = fs->inode_map->bitmap;
181 ext2fs_free_mem(&buf);
183 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
185 blk = (fs->image_header->offset_inodemap /
187 retval = io_channel_read_blk(fs->image_io, blk,
188 -(inode_nbytes * fs->group_desc_count),
194 blk = (fs->image_header->offset_blockmap /
196 retval = io_channel_read_blk(fs->image_io, blk,
197 -(block_nbytes * fs->group_desc_count),
205 for (i = 0; i < fs->group_desc_count; i++) {
207 blk = fs->group_desc[i].bg_block_bitmap;
209 retval = io_channel_read_blk(fs->io, blk,
210 -block_nbytes, block_bitmap);
212 retval = EXT2_ET_BLOCK_BITMAP_READ;
215 #ifdef EXT2_BIG_ENDIAN_BITMAPS
216 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
217 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
218 ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
221 memset(block_bitmap, 0, block_nbytes);
222 block_bitmap += block_nbytes;
225 blk = fs->group_desc[i].bg_inode_bitmap;
227 retval = io_channel_read_blk(fs->io, blk,
228 -inode_nbytes, inode_bitmap);
230 retval = EXT2_ET_INODE_BITMAP_READ;
233 #ifdef EXT2_BIG_ENDIAN_BITMAPS
234 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
235 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
236 ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
239 memset(inode_bitmap, 0, inode_nbytes);
240 inode_bitmap += inode_nbytes;
247 ext2fs_free_mem(&fs->block_map);
250 ext2fs_free_mem(&fs->inode_map);
252 ext2fs_free_mem(&buf);
256 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
258 return read_bitmaps(fs, 1, 0);
261 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
263 return read_bitmaps(fs, 0, 1);
266 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
269 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
271 if (fs->inode_map && fs->block_map)
274 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
277 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
281 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
283 if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
284 retval = ext2fs_write_block_bitmap(fs);
288 if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
289 retval = ext2fs_write_inode_bitmap(fs);