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 static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)
58 int block_nbytes, inode_nbytes;
61 char *block_bitmap, *inode_bitmap;
62 char *block_buf, *inode_buf;
66 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
68 if (!(fs->flags & EXT2_FLAG_RW))
69 return EXT2_ET_RO_FILSYS;
70 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
71 EXT2_FEATURE_COMPAT_LAZY_BG))
73 inode_nbytes = block_nbytes = 0;
74 block_bitmap = inode_bitmap = 0;
76 block_bitmap = fs->block_map->bitmap;
77 block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
78 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
81 memset(block_buf, 0xff, fs->blocksize);
84 inode_bitmap = fs->inode_map->bitmap;
85 inode_nbytes = (size_t)
86 ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
87 retval = ext2fs_get_mem(fs->blocksize, &inode_buf);
90 memset(inode_buf, 0xff, fs->blocksize);
93 for (i = 0; i < fs->group_desc_count; i++) {
94 if (!block_bitmap || !do_block)
95 goto skip_block_bitmap;
97 if (lazy_flag && fs->group_desc[i].bg_flags &
99 goto skip_this_block_bitmap;
101 memcpy(block_buf, block_bitmap, block_nbytes);
102 if (i == fs->group_desc_count - 1) {
103 /* Force bitmap padding for the last group */
104 nbits = ((fs->super->s_blocks_count
105 - fs->super->s_first_data_block)
106 % EXT2_BLOCKS_PER_GROUP(fs->super));
108 for (j = nbits; j < fs->blocksize * 8; j++)
109 ext2fs_set_bit(j, block_buf);
111 blk = fs->group_desc[i].bg_block_bitmap;
113 #ifdef EXT2_BIG_ENDIAN_BITMAPS
114 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
115 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
116 ext2fs_swap_bitmap(fs, block_buf,
119 retval = io_channel_write_blk(fs->io, blk, 1,
122 return EXT2_ET_BLOCK_BITMAP_WRITE;
124 skip_this_block_bitmap:
125 block_bitmap += block_nbytes;
128 if (!inode_bitmap || !do_inode)
131 if (lazy_flag && fs->group_desc[i].bg_flags &
132 EXT2_BG_INODE_UNINIT)
133 goto skip_this_inode_bitmap;
135 memcpy(inode_buf, inode_bitmap, inode_nbytes);
136 blk = fs->group_desc[i].bg_inode_bitmap;
138 #ifdef EXT2_BIG_ENDIAN_BITMAPS
139 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
140 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
141 ext2fs_swap_bitmap(fs, inode_buf,
144 retval = io_channel_write_blk(fs->io, blk, 1,
147 return EXT2_ET_INODE_BITMAP_WRITE;
149 skip_this_inode_bitmap:
150 inode_bitmap += inode_nbytes;
154 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
155 ext2fs_free_mem(&block_buf);
158 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
159 ext2fs_free_mem(&inode_buf);
164 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
167 char *block_bitmap = 0, *inode_bitmap = 0;
170 int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
171 int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8;
175 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
177 fs->write_bitmaps = ext2fs_write_bitmaps;
179 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
180 EXT2_FEATURE_COMPAT_LAZY_BG))
183 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
188 ext2fs_free_block_bitmap(fs->block_map);
189 sprintf(buf, "block bitmap for %s", fs->device_name);
190 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
193 block_bitmap = fs->block_map->bitmap;
197 ext2fs_free_inode_bitmap(fs->inode_map);
198 sprintf(buf, "inode bitmap for %s", fs->device_name);
199 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
202 inode_bitmap = fs->inode_map->bitmap;
204 ext2fs_free_mem(&buf);
206 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
208 blk = (fs->image_header->offset_inodemap /
210 retval = io_channel_read_blk(fs->image_io, blk,
211 -(inode_nbytes * fs->group_desc_count),
217 blk = (fs->image_header->offset_blockmap /
219 retval = io_channel_read_blk(fs->image_io, blk,
220 -(block_nbytes * fs->group_desc_count),
228 for (i = 0; i < fs->group_desc_count; i++) {
230 blk = fs->group_desc[i].bg_block_bitmap;
231 if (lazy_flag && fs->group_desc[i].bg_flags &
232 EXT2_BG_BLOCK_UNINIT)
235 retval = io_channel_read_blk(fs->io, blk,
236 -block_nbytes, block_bitmap);
238 retval = EXT2_ET_BLOCK_BITMAP_READ;
241 #ifdef EXT2_BIG_ENDIAN_BITMAPS
242 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
243 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
244 ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
247 memset(block_bitmap, 0xff, block_nbytes);
248 block_bitmap += block_nbytes;
251 blk = fs->group_desc[i].bg_inode_bitmap;
252 if (lazy_flag && fs->group_desc[i].bg_flags &
253 EXT2_BG_INODE_UNINIT)
256 retval = io_channel_read_blk(fs->io, blk,
257 -inode_nbytes, inode_bitmap);
259 retval = EXT2_ET_INODE_BITMAP_READ;
262 #ifdef EXT2_BIG_ENDIAN_BITMAPS
263 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
264 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
265 ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
268 memset(inode_bitmap, 0xff, inode_nbytes);
269 inode_bitmap += inode_nbytes;
276 ext2fs_free_mem(&fs->block_map);
280 ext2fs_free_mem(&fs->inode_map);
284 ext2fs_free_mem(&buf);
288 errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
290 return read_bitmaps(fs, 1, 0);
293 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
295 return read_bitmaps(fs, 0, 1);
298 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
300 return write_bitmaps(fs, 1, 0);
303 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
305 return write_bitmaps(fs, 0, 1);
308 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
310 if (fs->inode_map && fs->block_map)
313 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
316 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
318 int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
319 int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
321 if (!do_inode && !do_block)
324 return write_bitmaps(fs, do_inode, do_block);