Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rw_bitmaps.c --- routines to read and write the inode and block bitmaps. |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #if HAVE_UNISTD_H |
| 15 | #include <unistd.h> |
| 16 | #endif |
| 17 | #include <fcntl.h> |
| 18 | #include <time.h> |
| 19 | #ifdef HAVE_SYS_STAT_H |
| 20 | #include <sys/stat.h> |
| 21 | #endif |
| 22 | #ifdef HAVE_SYS_TYPES_H |
| 23 | #include <sys/types.h> |
| 24 | #endif |
| 25 | |
| 26 | #include "ext2_fs.h" |
| 27 | #include "ext2fs.h" |
| 28 | #include "e2image.h" |
| 29 | |
Rob Landley | 7c94bed | 2006-05-03 21:58:45 +0000 | [diff] [blame^] | 30 | #if defined(__powerpc__) && BB_BIG_ENDIAN |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 31 | /* |
| 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. |
| 39 | */ |
| 40 | #define EXT2_BIG_ENDIAN_BITMAPS |
| 41 | #endif |
| 42 | |
| 43 | #ifdef EXT2_BIG_ENDIAN_BITMAPS |
| 44 | static void ext2fs_swap_bitmap(ext2_filsys fs, char *bitmap, int nbytes) |
| 45 | { |
| 46 | __u32 *p = (__u32 *) bitmap; |
| 47 | int n; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 48 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 49 | for (n = nbytes / sizeof(__u32); n > 0; --n, ++p) |
| 50 | *p = ext2fs_swab32(*p); |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs) |
| 55 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 56 | dgrp_t i; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 57 | size_t nbytes; |
| 58 | errcode_t retval; |
| 59 | char * inode_bitmap = fs->inode_map->bitmap; |
| 60 | char * bitmap_block = NULL; |
| 61 | blk_t blk; |
| 62 | |
| 63 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 64 | |
| 65 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 66 | return EXT2_ET_RO_FILSYS; |
| 67 | if (!inode_bitmap) |
| 68 | return 0; |
| 69 | nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 70 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 71 | retval = ext2fs_get_mem(fs->blocksize, &bitmap_block); |
| 72 | if (retval) |
| 73 | return retval; |
| 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; |
| 78 | if (blk) { |
| 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); |
| 83 | #endif |
| 84 | retval = io_channel_write_blk(fs->io, blk, 1, |
| 85 | bitmap_block); |
| 86 | if (retval) |
| 87 | return EXT2_ET_INODE_BITMAP_WRITE; |
| 88 | } |
| 89 | inode_bitmap += nbytes; |
| 90 | } |
| 91 | fs->flags &= ~EXT2_FLAG_IB_DIRTY; |
| 92 | ext2fs_free_mem(&bitmap_block); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | errcode_t ext2fs_write_block_bitmap (ext2_filsys fs) |
| 97 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 98 | dgrp_t i; |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 99 | unsigned int j; |
| 100 | int nbytes; |
| 101 | unsigned int nbits; |
| 102 | errcode_t retval; |
| 103 | char * block_bitmap = fs->block_map->bitmap; |
| 104 | char * bitmap_block = NULL; |
| 105 | blk_t blk; |
| 106 | |
| 107 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 108 | |
| 109 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 110 | return EXT2_ET_RO_FILSYS; |
| 111 | if (!block_bitmap) |
| 112 | return 0; |
| 113 | nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; |
| 114 | retval = ext2fs_get_mem(fs->blocksize, &bitmap_block); |
| 115 | if (retval) |
| 116 | return retval; |
| 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)); |
| 125 | if (nbits) |
| 126 | for (j = nbits; j < fs->blocksize * 8; j++) |
| 127 | ext2fs_set_bit(j, bitmap_block); |
| 128 | } |
| 129 | blk = fs->group_desc[i].bg_block_bitmap; |
| 130 | if (blk) { |
| 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); |
| 135 | #endif |
| 136 | retval = io_channel_write_blk(fs->io, blk, 1, |
| 137 | bitmap_block); |
| 138 | if (retval) |
| 139 | return EXT2_ET_BLOCK_BITMAP_WRITE; |
| 140 | } |
| 141 | block_bitmap += nbytes; |
| 142 | } |
| 143 | fs->flags &= ~EXT2_FLAG_BB_DIRTY; |
| 144 | ext2fs_free_mem(&bitmap_block); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) |
| 149 | { |
| 150 | dgrp_t i; |
| 151 | char *block_bitmap = 0, *inode_bitmap = 0; |
| 152 | char *buf; |
| 153 | errcode_t retval; |
| 154 | int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8; |
| 155 | int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8; |
| 156 | blk_t blk; |
| 157 | |
| 158 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 159 | |
| 160 | fs->write_bitmaps = ext2fs_write_bitmaps; |
| 161 | |
| 162 | retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf); |
| 163 | if (retval) |
| 164 | return retval; |
| 165 | if (do_block) { |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 166 | ext2fs_free_block_bitmap(fs->block_map); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 167 | sprintf(buf, "block bitmap for %s", fs->device_name); |
| 168 | retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map); |
| 169 | if (retval) |
| 170 | goto cleanup; |
| 171 | block_bitmap = fs->block_map->bitmap; |
| 172 | } |
| 173 | if (do_inode) { |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 174 | ext2fs_free_inode_bitmap(fs->inode_map); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 175 | sprintf(buf, "inode bitmap for %s", fs->device_name); |
| 176 | retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map); |
| 177 | if (retval) |
| 178 | goto cleanup; |
| 179 | inode_bitmap = fs->inode_map->bitmap; |
| 180 | } |
| 181 | ext2fs_free_mem(&buf); |
| 182 | |
| 183 | if (fs->flags & EXT2_FLAG_IMAGE_FILE) { |
| 184 | if (inode_bitmap) { |
| 185 | blk = (fs->image_header->offset_inodemap / |
| 186 | fs->blocksize); |
| 187 | retval = io_channel_read_blk(fs->image_io, blk, |
| 188 | -(inode_nbytes * fs->group_desc_count), |
| 189 | inode_bitmap); |
| 190 | if (retval) |
| 191 | goto cleanup; |
| 192 | } |
| 193 | if (block_bitmap) { |
| 194 | blk = (fs->image_header->offset_blockmap / |
| 195 | fs->blocksize); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 196 | retval = io_channel_read_blk(fs->image_io, blk, |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 197 | -(block_nbytes * fs->group_desc_count), |
| 198 | block_bitmap); |
| 199 | if (retval) |
| 200 | goto cleanup; |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | for (i = 0; i < fs->group_desc_count; i++) { |
| 206 | if (block_bitmap) { |
| 207 | blk = fs->group_desc[i].bg_block_bitmap; |
| 208 | if (blk) { |
| 209 | retval = io_channel_read_blk(fs->io, blk, |
| 210 | -block_nbytes, block_bitmap); |
| 211 | if (retval) { |
| 212 | retval = EXT2_ET_BLOCK_BITMAP_READ; |
| 213 | goto cleanup; |
| 214 | } |
| 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); |
| 219 | #endif |
| 220 | } else |
| 221 | memset(block_bitmap, 0, block_nbytes); |
| 222 | block_bitmap += block_nbytes; |
| 223 | } |
| 224 | if (inode_bitmap) { |
| 225 | blk = fs->group_desc[i].bg_inode_bitmap; |
| 226 | if (blk) { |
| 227 | retval = io_channel_read_blk(fs->io, blk, |
| 228 | -inode_nbytes, inode_bitmap); |
| 229 | if (retval) { |
| 230 | retval = EXT2_ET_INODE_BITMAP_READ; |
| 231 | goto cleanup; |
| 232 | } |
| 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); |
| 237 | #endif |
| 238 | } else |
| 239 | memset(inode_bitmap, 0, inode_nbytes); |
| 240 | inode_bitmap += inode_nbytes; |
| 241 | } |
| 242 | } |
| 243 | return 0; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 244 | |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 245 | cleanup: |
| 246 | if (do_block) { |
| 247 | ext2fs_free_mem(&fs->block_map); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 248 | } |
| 249 | if (do_inode) { |
| 250 | ext2fs_free_mem(&fs->inode_map); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 251 | } |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 252 | ext2fs_free_mem(&buf); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 253 | return retval; |
| 254 | } |
| 255 | |
| 256 | errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs) |
| 257 | { |
| 258 | return read_bitmaps(fs, 1, 0); |
| 259 | } |
| 260 | |
| 261 | errcode_t ext2fs_read_block_bitmap(ext2_filsys fs) |
| 262 | { |
| 263 | return read_bitmaps(fs, 0, 1); |
| 264 | } |
| 265 | |
| 266 | errcode_t ext2fs_read_bitmaps(ext2_filsys fs) |
| 267 | { |
| 268 | |
| 269 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 270 | |
| 271 | if (fs->inode_map && fs->block_map) |
| 272 | return 0; |
| 273 | |
| 274 | return read_bitmaps(fs, !fs->inode_map, !fs->block_map); |
| 275 | } |
| 276 | |
| 277 | errcode_t ext2fs_write_bitmaps(ext2_filsys fs) |
| 278 | { |
| 279 | errcode_t retval; |
| 280 | |
| 281 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 282 | |
| 283 | if (fs->block_map && ext2fs_test_bb_dirty(fs)) { |
| 284 | retval = ext2fs_write_block_bitmap(fs); |
| 285 | if (retval) |
| 286 | return retval; |
| 287 | } |
| 288 | if (fs->inode_map && ext2fs_test_ib_dirty(fs)) { |
| 289 | retval = ext2fs_write_inode_bitmap(fs); |
| 290 | if (retval) |
| 291 | return retval; |
| 292 | } |
| 293 | return 0; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 294 | } |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 295 | |