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_buf, *inode_buf;
64 blk_t blk_itr = fs->super->s_first_data_block;
65 ext2_ino_t ino_itr = 1;
67 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
69 if (!(fs->flags & EXT2_FLAG_RW))
70 return EXT2_ET_RO_FILSYS;
71 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
72 EXT2_FEATURE_COMPAT_LAZY_BG))
74 inode_nbytes = block_nbytes = 0;
76 block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
77 retval = ext2fs_get_mem(fs->blocksize, &block_buf);
80 memset(block_buf, 0xff, fs->blocksize);
83 inode_nbytes = (size_t)
84 ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
85 retval = ext2fs_get_mem(fs->blocksize, &inode_buf);
88 memset(inode_buf, 0xff, fs->blocksize);
91 for (i = 0; i < fs->group_desc_count; i++) {
93 goto skip_block_bitmap;
95 if (lazy_flag && fs->group_desc[i].bg_flags &
97 goto skip_this_block_bitmap;
99 retval = ext2fs_get_block_bitmap_range(fs->block_map,
100 blk_itr, block_nbytes << 3, block_buf);
104 if (i == fs->group_desc_count - 1) {
105 /* Force bitmap padding for the last group */
106 nbits = ((fs->super->s_blocks_count
107 - fs->super->s_first_data_block)
108 % EXT2_BLOCKS_PER_GROUP(fs->super));
110 for (j = nbits; j < fs->blocksize * 8; j++)
111 ext2fs_set_bit(j, block_buf);
113 blk = fs->group_desc[i].bg_block_bitmap;
115 #ifdef EXT2_BIG_ENDIAN_BITMAPS
116 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
117 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
118 ext2fs_swap_bitmap(fs, block_buf,
121 retval = io_channel_write_blk(fs->io, blk, 1,
124 return EXT2_ET_BLOCK_BITMAP_WRITE;
126 skip_this_block_bitmap:
127 blk_itr += block_nbytes << 3;
133 if (lazy_flag && fs->group_desc[i].bg_flags &
134 EXT2_BG_INODE_UNINIT)
135 goto skip_this_inode_bitmap;
137 retval = ext2fs_get_inode_bitmap_range(fs->inode_map,
138 ino_itr, inode_nbytes << 3, inode_buf);
142 blk = fs->group_desc[i].bg_inode_bitmap;
144 #ifdef EXT2_BIG_ENDIAN_BITMAPS
145 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
146 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
147 ext2fs_swap_bitmap(fs, inode_buf,
150 retval = io_channel_write_blk(fs->io, blk, 1,
153 return EXT2_ET_INODE_BITMAP_WRITE;
155 skip_this_inode_bitmap:
156 ino_itr += inode_nbytes << 3;
160 fs->flags &= ~EXT2_FLAG_BB_DIRTY;
161 ext2fs_free_mem(&block_buf);
164 fs->flags &= ~EXT2_FLAG_IB_DIRTY;
165 ext2fs_free_mem(&inode_buf);
170 static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
173 char *block_bitmap = 0, *inode_bitmap = 0;
176 unsigned int block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
177 unsigned inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
179 int do_image = fs->flags & EXT2_FLAG_IMAGE_FILE;
182 blk_t blk_itr = fs->super->s_first_data_block;
184 ext2_ino_t ino_itr = 1;
187 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
189 fs->write_bitmaps = ext2fs_write_bitmaps;
191 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
192 EXT2_FEATURE_COMPAT_LAZY_BG))
195 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
200 ext2fs_free_block_bitmap(fs->block_map);
201 sprintf(buf, "block bitmap for %s", fs->device_name);
202 retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
205 retval = ext2fs_get_mem(do_image ? fs->blocksize :
206 block_nbytes, &block_bitmap);
213 ext2fs_free_inode_bitmap(fs->inode_map);
214 sprintf(buf, "inode bitmap for %s", fs->device_name);
215 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
218 retval = ext2fs_get_mem(do_image ? fs->blocksize :
219 inode_nbytes, &inode_bitmap);
224 ext2fs_free_mem(&buf);
226 if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
227 blk = (fs->image_header->offset_inodemap / fs->blocksize);
228 ino_cnt = fs->super->s_inodes_count;
229 while (inode_nbytes > 0) {
230 retval = io_channel_read_blk(fs->image_io, blk++,
234 cnt = fs->blocksize << 3;
237 retval = ext2fs_set_inode_bitmap_range(fs->inode_map,
238 ino_itr, cnt, inode_bitmap);
241 ino_itr += fs->blocksize << 3;
242 ino_cnt -= fs->blocksize << 3;
243 inode_nbytes -= fs->blocksize;
245 blk = (fs->image_header->offset_blockmap /
247 blk_cnt = EXT2_BLOCKS_PER_GROUP(fs->super) *
248 fs->group_desc_count;
249 while (block_nbytes > 0) {
250 retval = io_channel_read_blk(fs->image_io, blk++,
254 cnt = fs->blocksize << 3;
257 retval = ext2fs_set_block_bitmap_range(fs->block_map,
258 blk_itr, cnt, block_bitmap);
261 blk_itr += fs->blocksize << 3;
262 blk_cnt -= fs->blocksize << 3;
263 block_nbytes -= fs->blocksize;
265 goto success_cleanup;
268 for (i = 0; i < fs->group_desc_count; i++) {
270 blk = fs->group_desc[i].bg_block_bitmap;
271 if (lazy_flag && fs->group_desc[i].bg_flags &
272 EXT2_BG_BLOCK_UNINIT)
275 retval = io_channel_read_blk(fs->io, blk,
276 -block_nbytes, block_bitmap);
278 retval = EXT2_ET_BLOCK_BITMAP_READ;
281 #ifdef EXT2_BIG_ENDIAN_BITMAPS
282 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
283 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
284 ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
287 memset(block_bitmap, 0xff, block_nbytes);
288 cnt = block_nbytes << 3;
289 retval = ext2fs_set_block_bitmap_range(fs->block_map,
290 blk_itr, cnt, block_bitmap);
293 blk_itr += block_nbytes << 3;
296 blk = fs->group_desc[i].bg_inode_bitmap;
297 if (lazy_flag && fs->group_desc[i].bg_flags &
298 EXT2_BG_INODE_UNINIT)
301 retval = io_channel_read_blk(fs->io, blk,
302 -inode_nbytes, inode_bitmap);
304 retval = EXT2_ET_INODE_BITMAP_READ;
307 #ifdef EXT2_BIG_ENDIAN_BITMAPS
308 if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
309 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
310 ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
313 memset(inode_bitmap, 0xff, inode_nbytes);
314 cnt = inode_nbytes << 3;
315 retval = ext2fs_set_inode_bitmap_range(fs->inode_map,
316 ino_itr, cnt, inode_bitmap);
319 ino_itr += inode_nbytes << 3;
324 ext2fs_free_mem(&inode_bitmap);
326 ext2fs_free_mem(&block_bitmap);
331 ext2fs_free_mem(&fs->block_map);
335 ext2fs_free_mem(&fs->inode_map);
339 ext2fs_free_mem(&inode_bitmap);
341 ext2fs_free_mem(&block_bitmap);
343 ext2fs_free_mem(&buf);
347 errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
349 return read_bitmaps(fs, 1, 0);
352 errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
354 return read_bitmaps(fs, 0, 1);
357 errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
359 return write_bitmaps(fs, 1, 0);
362 errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
364 return write_bitmaps(fs, 0, 1);
367 errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
369 if (fs->inode_map && fs->block_map)
372 return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
375 errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
377 int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
378 int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
380 if (!do_inode && !do_block)
383 return write_bitmaps(fs, do_inode, do_block);