Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rw_bitmaps.c --- routines to read and write the inode and block bitmaps. |
| 3 | * |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 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% |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
| 18 | #include <time.h> |
Theodore Ts'o | 519ee04 | 1997-10-20 00:52:43 +0000 | [diff] [blame] | 19 | #ifdef HAVE_SYS_STAT_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Theodore Ts'o | 519ee04 | 1997-10-20 00:52:43 +0000 | [diff] [blame] | 21 | #endif |
| 22 | #ifdef HAVE_SYS_TYPES_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
Theodore Ts'o | 519ee04 | 1997-10-20 00:52:43 +0000 | [diff] [blame] | 24 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 25 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 26 | #include "ext2_fs.h" |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 27 | #include "ext2fs.h" |
Theodore Ts'o | a78926e | 2001-05-03 04:02:29 +0000 | [diff] [blame] | 28 | #include "e2image.h" |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 29 | |
Theodore Ts'o | 5df55d7 | 2001-06-11 07:00:04 +0000 | [diff] [blame^] | 30 | #if defined(__powerpc__) && defined(EXT2FS_ENABLE_SWAPFS) |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20: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 | void ext2fs_swap_bitmap(ext2_filsys fs, char *bitmap, int nbytes) |
| 45 | { |
| 46 | __u32 *p = (__u32 *) bitmap; |
| 47 | int n; |
| 48 | |
| 49 | for (n = nbytes / sizeof(__u32); n > 0; --n, ++p) |
| 50 | *p = ext2fs_swab32(*p); |
| 51 | } |
| 52 | #endif |
| 53 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 54 | errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs) |
| 55 | { |
Theodore Ts'o | 2eb374c | 1998-09-03 01:22:57 +0000 | [diff] [blame] | 56 | dgrp_t i; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 57 | size_t nbytes; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 58 | errcode_t retval; |
| 59 | char * inode_bitmap = fs->inode_map->bitmap; |
| 60 | char * bitmap_block = NULL; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 61 | blk_t blk; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 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; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 69 | nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 70 | |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 71 | retval = ext2fs_get_mem(fs->blocksize, (void **) &bitmap_block); |
| 72 | if (retval) |
| 73 | return retval; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 74 | memset(bitmap_block, 0xff, fs->blocksize); |
| 75 | for (i = 0; i < fs->group_desc_count; i++) { |
| 76 | memcpy(bitmap_block, inode_bitmap, nbytes); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 77 | blk = fs->group_desc[i].bg_inode_bitmap; |
| 78 | if (blk) { |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 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 |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 84 | retval = io_channel_write_blk(fs->io, blk, 1, |
| 85 | bitmap_block); |
| 86 | if (retval) |
| 87 | return EXT2_ET_INODE_BITMAP_WRITE; |
| 88 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 89 | inode_bitmap += nbytes; |
| 90 | } |
| 91 | fs->flags |= EXT2_FLAG_CHANGED; |
| 92 | fs->flags &= ~EXT2_FLAG_IB_DIRTY; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 93 | ext2fs_free_mem((void **) &bitmap_block); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | errcode_t ext2fs_write_block_bitmap (ext2_filsys fs) |
| 98 | { |
Theodore Ts'o | 2eb374c | 1998-09-03 01:22:57 +0000 | [diff] [blame] | 99 | dgrp_t i; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 100 | int j; |
| 101 | int nbytes; |
| 102 | int nbits; |
| 103 | errcode_t retval; |
| 104 | char * block_bitmap = fs->block_map->bitmap; |
| 105 | char * bitmap_block = NULL; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 106 | blk_t blk; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 107 | |
| 108 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 109 | |
| 110 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 111 | return EXT2_ET_RO_FILSYS; |
| 112 | if (!block_bitmap) |
| 113 | return 0; |
| 114 | nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 115 | retval = ext2fs_get_mem(fs->blocksize, (void **) &bitmap_block); |
| 116 | if (retval) |
| 117 | return retval; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 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 */ |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 123 | nbits = (int) ((fs->super->s_blocks_count |
| 124 | - fs->super->s_first_data_block) |
| 125 | % EXT2_BLOCKS_PER_GROUP(fs->super)); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 126 | if (nbits) |
| 127 | for (j = nbits; j < fs->blocksize * 8; j++) |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame] | 128 | ext2fs_set_bit(j, bitmap_block); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 129 | } |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 130 | blk = fs->group_desc[i].bg_block_bitmap; |
| 131 | if (blk) { |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 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); |
| 136 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 137 | retval = io_channel_write_blk(fs->io, blk, 1, |
| 138 | bitmap_block); |
| 139 | if (retval) |
| 140 | return EXT2_ET_BLOCK_BITMAP_WRITE; |
| 141 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 142 | block_bitmap += nbytes; |
| 143 | } |
| 144 | fs->flags |= EXT2_FLAG_CHANGED; |
| 145 | fs->flags &= ~EXT2_FLAG_BB_DIRTY; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 146 | ext2fs_free_mem((void **) &bitmap_block); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block) |
| 151 | { |
Theodore Ts'o | 2eb374c | 1998-09-03 01:22:57 +0000 | [diff] [blame] | 152 | dgrp_t i; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 153 | char *block_bitmap = 0, *inode_bitmap = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 154 | char *buf; |
| 155 | errcode_t retval; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 156 | int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8; |
| 157 | int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 158 | blk_t blk; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 159 | |
| 160 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 161 | |
| 162 | fs->write_bitmaps = ext2fs_write_bitmaps; |
| 163 | |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 164 | retval = ext2fs_get_mem(strlen(fs->device_name) + 80, (void **) &buf); |
| 165 | if (retval) |
| 166 | return retval; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 167 | if (do_block) { |
| 168 | if (fs->block_map) |
| 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); |
| 172 | if (retval) |
| 173 | goto cleanup; |
| 174 | block_bitmap = fs->block_map->bitmap; |
| 175 | } |
| 176 | if (do_inode) { |
| 177 | if (fs->inode_map) |
| 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); |
| 181 | if (retval) |
| 182 | goto cleanup; |
| 183 | inode_bitmap = fs->inode_map->bitmap; |
| 184 | } |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 185 | ext2fs_free_mem((void **) &buf); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 186 | |
Theodore Ts'o | a78926e | 2001-05-03 04:02:29 +0000 | [diff] [blame] | 187 | if (fs->flags & EXT2_FLAG_IMAGE_FILE) { |
| 188 | if (inode_bitmap) { |
| 189 | blk = (fs->image_header->offset_inodemap / |
| 190 | fs->blocksize); |
| 191 | retval = io_channel_read_blk(fs->io, blk, |
| 192 | -(inode_nbytes * fs->group_desc_count), |
| 193 | inode_bitmap); |
| 194 | if (retval) |
| 195 | goto cleanup; |
| 196 | } |
| 197 | if (block_bitmap) { |
| 198 | blk = (fs->image_header->offset_blockmap / |
| 199 | fs->blocksize); |
| 200 | retval = io_channel_read_blk(fs->io, blk, |
| 201 | -(block_nbytes * fs->group_desc_count), |
| 202 | block_bitmap); |
| 203 | if (retval) |
| 204 | goto cleanup; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 209 | for (i = 0; i < fs->group_desc_count; i++) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 210 | if (block_bitmap) { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 211 | blk = fs->group_desc[i].bg_block_bitmap; |
| 212 | if (blk) { |
| 213 | retval = io_channel_read_blk(fs->io, blk, |
| 214 | -block_nbytes, block_bitmap); |
| 215 | if (retval) { |
| 216 | retval = EXT2_ET_BLOCK_BITMAP_READ; |
| 217 | goto cleanup; |
| 218 | } |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 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); |
| 223 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 224 | } else |
| 225 | memset(block_bitmap, 0, block_nbytes); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 226 | block_bitmap += block_nbytes; |
| 227 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 228 | if (inode_bitmap) { |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 229 | blk = fs->group_desc[i].bg_inode_bitmap; |
| 230 | if (blk) { |
| 231 | retval = io_channel_read_blk(fs->io, blk, |
| 232 | -inode_nbytes, inode_bitmap); |
| 233 | if (retval) { |
| 234 | retval = EXT2_ET_INODE_BITMAP_READ; |
| 235 | goto cleanup; |
| 236 | } |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 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); |
| 241 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 242 | } else |
| 243 | memset(inode_bitmap, 0, inode_nbytes); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 244 | inode_bitmap += inode_nbytes; |
| 245 | } |
| 246 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 247 | return 0; |
| 248 | |
| 249 | cleanup: |
| 250 | if (do_block) { |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 251 | ext2fs_free_mem((void **) &fs->block_map); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 252 | fs->block_map = 0; |
| 253 | } |
| 254 | if (do_inode) { |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 255 | ext2fs_free_mem((void **) &fs->inode_map); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 256 | fs->inode_map = 0; |
| 257 | } |
| 258 | if (buf) |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 259 | ext2fs_free_mem((void **) &buf); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 260 | return retval; |
| 261 | } |
| 262 | |
| 263 | errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs) |
| 264 | { |
| 265 | return read_bitmaps(fs, 1, 0); |
| 266 | } |
| 267 | |
| 268 | errcode_t ext2fs_read_block_bitmap(ext2_filsys fs) |
| 269 | { |
| 270 | return read_bitmaps(fs, 0, 1); |
| 271 | } |
| 272 | |
| 273 | errcode_t ext2fs_read_bitmaps(ext2_filsys fs) |
| 274 | { |
| 275 | |
| 276 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 277 | |
Theodore Ts'o | 9abd2ce | 1998-02-16 22:00:37 +0000 | [diff] [blame] | 278 | if (fs->inode_map && fs->block_map) |
| 279 | return 0; |
| 280 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 281 | return read_bitmaps(fs, !fs->inode_map, !fs->block_map); |
| 282 | } |
| 283 | |
| 284 | errcode_t ext2fs_write_bitmaps(ext2_filsys fs) |
| 285 | { |
| 286 | errcode_t retval; |
| 287 | |
| 288 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 289 | |
| 290 | if (fs->block_map && ext2fs_test_bb_dirty(fs)) { |
| 291 | retval = ext2fs_write_block_bitmap(fs); |
| 292 | if (retval) |
| 293 | return retval; |
| 294 | } |
| 295 | if (fs->inode_map && ext2fs_test_ib_dirty(fs)) { |
| 296 | retval = ext2fs_write_inode_bitmap(fs); |
| 297 | if (retval) |
| 298 | return retval; |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |