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 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_CHANGED;
92 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
93 ext2fs_free_mem((void **) &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, (void **) &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 = (int) ((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_CHANGED;
145 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
146 ext2fs_free_mem((void **) &bitmap_block);
150 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
153 char *block_bitmap = 0, *inode_bitmap = 0;
156 int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
157 int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8;
160 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
162 fs->write_bitmaps = ext2fs_write_bitmaps;
164 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, (void **) &buf);
169 ext2fs_free_block_bitmap(fs->block_map);
170 sprintf(buf, "block bitmap for %s", fs->device_name);
171 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
174 block_bitmap = fs->block_map->bitmap;
178 ext2fs_free_inode_bitmap(fs->inode_map);
179 sprintf(buf, "inode bitmap for %s", fs->device_name);
180 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
183 inode_bitmap = fs->inode_map->bitmap;
185 ext2fs_free_mem((void **) &buf);
187 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
189 blk = (fs->image_header->offset_inodemap /
191 retval = io_channel_read_blk(fs->io, blk,
192 -(inode_nbytes * fs->group_desc_count),
198 blk = (fs->image_header->offset_blockmap /
200 retval = io_channel_read_blk(fs->io, blk,
201 -(block_nbytes * fs->group_desc_count),
209 for (i = 0; i < fs->group_desc_count; i++) {
211 blk = fs->group_desc[i].bg_block_bitmap;
213 retval = io_channel_read_blk(fs->io, blk,
214 -block_nbytes, block_bitmap);
216 retval = EXT2_ET_BLOCK_BITMAP_READ;
219 #ifdef EXT2_BIG_ENDIAN_BITMAPS
220 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
221 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
222 ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
225 memset(block_bitmap, 0, block_nbytes);
226 block_bitmap += block_nbytes;
229 blk = fs->group_desc[i].bg_inode_bitmap;
231 retval = io_channel_read_blk(fs->io, blk,
232 -inode_nbytes, inode_bitmap);
234 retval = EXT2_ET_INODE_BITMAP_READ;
237 #ifdef EXT2_BIG_ENDIAN_BITMAPS
238 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
239 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
240 ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
243 memset(inode_bitmap, 0, inode_nbytes);
244 inode_bitmap += inode_nbytes;
251 ext2fs_free_mem((void **) &fs->block_map);
255 ext2fs_free_mem((void **) &fs->inode_map);
259 ext2fs_free_mem((void **) &buf);
263 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
265 return read_bitmaps(fs, 1, 0);
268 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
270 return read_bitmaps(fs, 0, 1);
273 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
276 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
278 if (fs->inode_map && fs->block_map)
281 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
284 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
288 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
290 if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
291 retval = ext2fs_write_block_bitmap(fs);
295 if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
296 retval = ext2fs_write_inode_bitmap(fs);