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
21 #include <sys/types.h>
26 #include <linux/ext2_fs.h>
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 bitmap_block = malloc(fs->blocksize);
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;
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 bitmap_block = malloc(fs->blocksize);
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;
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 buf = malloc(strlen(fs->device_name) + 80);
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;
185 for (i = 0; i < fs->group_desc_count; i++) {
187 blk = fs->group_desc[i].bg_block_bitmap;
189 retval = io_channel_read_blk(fs->io, blk,
190 -block_nbytes, block_bitmap);
192 retval = EXT2_ET_BLOCK_BITMAP_READ;
195 #ifdef EXT2_BIG_ENDIAN_BITMAPS
196 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
197 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
198 ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
201 memset(block_bitmap, 0, block_nbytes);
202 block_bitmap += block_nbytes;
205 blk = fs->group_desc[i].bg_inode_bitmap;
207 retval = io_channel_read_blk(fs->io, blk,
208 -inode_nbytes, inode_bitmap);
210 retval = EXT2_ET_INODE_BITMAP_READ;
213 #ifdef EXT2_BIG_ENDIAN_BITMAPS
214 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
215 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
216 ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
219 memset(inode_bitmap, 0, inode_nbytes);
220 inode_bitmap += inode_nbytes;
239 errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
241 return read_bitmaps(fs, 1, 0);
244 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
246 return read_bitmaps(fs, 0, 1);
249 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
252 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
254 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
257 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
261 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
263 if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
264 retval = ext2fs_write_block_bitmap(fs);
268 if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
269 retval = ext2fs_write_inode_bitmap(fs);