Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * resize2fs.c --- ext2 main routine |
| 3 | * |
| 4 | * Copyright (C) 1997 Theodore Ts'o |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * All rights reserved. |
| 8 | * %End-Header% |
| 9 | */ |
| 10 | |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 11 | /* |
| 12 | * Resizing a filesystem consists of the following phases: |
| 13 | * |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 14 | * 1. Adjust superblock and write out new parts of the inode |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 15 | * table |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 16 | * 2. Determine blocks which need to be relocated, and copy the |
| 17 | * contents of blocks from their old locations to the new ones. |
| 18 | * 3. Scan the inode table, doing the following: |
| 19 | * a. If blocks have been moved, update the block |
| 20 | * pointers in the inodes and indirect blocks to |
| 21 | * point at the new block locations. |
| 22 | * b. If parts of the inode table need to be evacuated, |
| 23 | * copy inodes from their old locations to their |
| 24 | * new ones. |
| 25 | * c. If (b) needs to be done, note which blocks contain |
| 26 | * directory information, since we will need to |
| 27 | * update the directory information. |
| 28 | * 4. Update the directory blocks with the new inode locations. |
| 29 | * 5. Move the inode tables, if necessary. |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 30 | */ |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 31 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 32 | #include "resize2fs.h" |
| 33 | |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 34 | #ifdef linux /* Kludge for debugging */ |
| 35 | #define RESIZE2FS_DEBUG |
| 36 | #endif |
| 37 | |
| 38 | static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size); |
| 39 | static errcode_t blocks_to_move(ext2_resize_t rfs); |
| 40 | static errcode_t block_mover(ext2_resize_t rfs); |
| 41 | static errcode_t inode_scan_and_fix(ext2_resize_t rfs); |
| 42 | static errcode_t inode_ref_fix(ext2_resize_t rfs); |
| 43 | static errcode_t move_itables(ext2_resize_t rfs); |
| 44 | static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs); |
| 45 | |
| 46 | /* |
| 47 | * Some helper CPP macros |
| 48 | */ |
| 49 | #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap) |
| 50 | #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap) |
| 51 | #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table) |
| 52 | |
| 53 | #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i))) |
| 54 | #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i))) |
| 55 | |
| 56 | #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \ |
| 57 | ((blk) < (FS_INODE_TB((fs), (i)) + \ |
| 58 | (fs)->inode_blocks_per_group))) |
| 59 | |
| 60 | |
| 61 | |
| 62 | /* |
| 63 | * This is the top-level routine which does the dirty deed.... |
| 64 | */ |
| 65 | errcode_t resize_fs(ext2_filsys fs, blk_t new_size, int flags, |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 66 | errcode_t (*progress)(ext2_resize_t rfs, int pass, |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 67 | unsigned long cur, |
| 68 | unsigned long max)) |
| 69 | { |
| 70 | ext2_resize_t rfs; |
| 71 | errcode_t retval; |
| 72 | |
| 73 | retval = ext2fs_read_bitmaps(fs); |
| 74 | if (retval) |
| 75 | return retval; |
| 76 | |
| 77 | /* |
| 78 | * Create the data structure |
| 79 | */ |
| 80 | retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), |
| 81 | (void **) &rfs); |
| 82 | if (retval) |
| 83 | return retval; |
| 84 | memset(rfs, 0, sizeof(struct ext2_resize_struct)); |
| 85 | |
| 86 | rfs->old_fs = fs; |
| 87 | rfs->flags = flags; |
| 88 | rfs->itable_buf = 0; |
| 89 | rfs->progress = progress; |
| 90 | retval = ext2fs_dup_handle(fs, &rfs->new_fs); |
| 91 | if (retval) |
| 92 | goto errout; |
| 93 | |
| 94 | retval = adjust_superblock(rfs, new_size); |
| 95 | if (retval) |
| 96 | goto errout; |
| 97 | |
| 98 | retval = blocks_to_move(rfs); |
| 99 | if (retval) |
| 100 | goto errout; |
| 101 | |
| 102 | #ifdef RESIZE2FS_DEBUG |
| 103 | if (rfs->flags & RESIZE_DEBUG_BMOVE) |
| 104 | printf("Number of free blocks: %d/%d, Needed: %d\n", |
| 105 | rfs->old_fs->super->s_free_blocks_count, |
| 106 | rfs->new_fs->super->s_free_blocks_count, |
| 107 | rfs->needed_blocks); |
| 108 | #endif |
| 109 | |
| 110 | retval = block_mover(rfs); |
| 111 | if (retval) |
| 112 | goto errout; |
| 113 | |
| 114 | retval = inode_scan_and_fix(rfs); |
| 115 | if (retval) |
| 116 | goto errout; |
| 117 | |
| 118 | retval = inode_ref_fix(rfs); |
| 119 | if (retval) |
| 120 | goto errout; |
| 121 | |
| 122 | retval = ext2fs_calculate_summary_stats(rfs->new_fs); |
| 123 | if (retval) |
| 124 | goto errout; |
| 125 | |
| 126 | retval = move_itables(rfs); |
| 127 | if (retval) |
| 128 | goto errout; |
| 129 | |
| 130 | retval = ext2fs_close(rfs->new_fs); |
| 131 | if (retval) |
| 132 | goto errout; |
| 133 | |
| 134 | rfs->flags = flags; |
| 135 | |
| 136 | ext2fs_free(rfs->old_fs); |
| 137 | if (rfs->itable_buf) |
| 138 | ext2fs_free_mem((void **) &rfs->itable_buf); |
| 139 | ext2fs_free_mem((void **) &rfs); |
| 140 | |
| 141 | return 0; |
| 142 | |
| 143 | errout: |
| 144 | if (rfs->new_fs) |
| 145 | ext2fs_free(rfs->new_fs); |
| 146 | if (rfs->itable_buf) |
| 147 | ext2fs_free_mem((void **) &rfs->itable_buf); |
| 148 | ext2fs_free_mem((void **) &rfs); |
| 149 | return retval; |
| 150 | } |
| 151 | |
| 152 | /* -------------------------------------------------------------------- |
| 153 | * |
| 154 | * Resize processing, phase 1. |
| 155 | * |
| 156 | * In this phase we adjust the in-memory superblock information, and |
| 157 | * initialize any new parts of the inode table. The new parts of the |
| 158 | * inode table are created in virgin disk space, so we can abort here |
| 159 | * without any side effects. |
| 160 | * -------------------------------------------------------------------- |
| 161 | */ |
| 162 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 163 | /* |
| 164 | * This routine adjusts the superblock and other data structures... |
| 165 | */ |
| 166 | static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size) |
| 167 | { |
| 168 | ext2_filsys fs; |
| 169 | int overhead = 0; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 170 | int rem, adj = 0; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 171 | errcode_t retval; |
| 172 | ino_t real_end; |
| 173 | blk_t blk, group_block; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 174 | unsigned long i, j; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 175 | int old_numblocks, numblocks, adjblocks; |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 176 | unsigned long max_group; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 177 | |
| 178 | fs = rfs->new_fs; |
| 179 | fs->super->s_blocks_count = new_size; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 180 | ext2fs_mark_super_dirty(fs); |
| 181 | ext2fs_mark_bb_dirty(fs); |
| 182 | ext2fs_mark_ib_dirty(fs); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 183 | |
| 184 | retry: |
| 185 | fs->group_desc_count = (fs->super->s_blocks_count - |
| 186 | fs->super->s_first_data_block + |
| 187 | EXT2_BLOCKS_PER_GROUP(fs->super) - 1) |
| 188 | / EXT2_BLOCKS_PER_GROUP(fs->super); |
| 189 | if (fs->group_desc_count == 0) |
| 190 | return EXT2_ET_TOOSMALL; |
| 191 | fs->desc_blocks = (fs->group_desc_count + |
| 192 | EXT2_DESC_PER_BLOCK(fs->super) - 1) |
| 193 | / EXT2_DESC_PER_BLOCK(fs->super); |
| 194 | |
| 195 | /* |
| 196 | * Overhead is the number of bookkeeping blocks per group. It |
| 197 | * includes the superblock backup, the group descriptor |
| 198 | * backups, the inode bitmap, the block bitmap, and the inode |
| 199 | * table. |
| 200 | * |
| 201 | * XXX Not all block groups need the descriptor blocks, but |
| 202 | * being clever is tricky... |
| 203 | */ |
| 204 | overhead = 3 + fs->desc_blocks + fs->inode_blocks_per_group; |
| 205 | |
| 206 | /* |
| 207 | * See if the last group is big enough to support the |
| 208 | * necessary data structures. If not, we need to get rid of |
| 209 | * it. |
| 210 | */ |
| 211 | rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) % |
| 212 | fs->super->s_blocks_per_group; |
| 213 | if ((fs->group_desc_count == 1) && rem && (rem < overhead)) |
| 214 | return EXT2_ET_TOOSMALL; |
| 215 | if (rem && (rem < overhead+50)) { |
| 216 | fs->super->s_blocks_count -= rem; |
| 217 | goto retry; |
| 218 | } |
| 219 | /* |
| 220 | * Adjust the number of inodes |
| 221 | */ |
| 222 | fs->super->s_inodes_count = fs->super->s_inodes_per_group * |
| 223 | fs->group_desc_count; |
| 224 | |
| 225 | /* |
| 226 | * Adjust the number of free blocks |
| 227 | */ |
| 228 | blk = rfs->old_fs->super->s_blocks_count; |
| 229 | if (blk > fs->super->s_blocks_count) |
| 230 | fs->super->s_free_blocks_count -= |
| 231 | (blk - fs->super->s_blocks_count); |
| 232 | else |
| 233 | fs->super->s_free_blocks_count += |
| 234 | (fs->super->s_blocks_count - blk); |
| 235 | |
| 236 | /* |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 237 | * Adjust the number of reserved blocks |
| 238 | */ |
| 239 | blk = rfs->old_fs->super->s_r_blocks_count * 100 / |
| 240 | rfs->old_fs->super->s_blocks_count; |
| 241 | fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk) |
| 242 | / 100); |
| 243 | |
| 244 | /* |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 245 | * Adjust the bitmaps for size |
| 246 | */ |
| 247 | retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count, |
| 248 | fs->super->s_inodes_count, |
| 249 | fs->inode_map); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 250 | if (retval) goto errout; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 251 | |
| 252 | real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super) |
| 253 | * fs->group_desc_count)) - 1 + |
| 254 | fs->super->s_first_data_block; |
| 255 | retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1, |
| 256 | real_end, fs->block_map); |
| 257 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 258 | if (retval) goto errout; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * Reallocate the group descriptors as necessary. |
| 262 | */ |
| 263 | if (rfs->old_fs->desc_blocks != fs->desc_blocks) { |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 264 | retval = ext2fs_resize_mem(fs->desc_blocks * fs->blocksize, |
| 265 | (void **) &fs->group_desc); |
| 266 | if (retval) |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 267 | goto errout; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 268 | } |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 269 | |
| 270 | /* |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 271 | * Check to make sure there are enough inodes |
| 272 | */ |
| 273 | if ((rfs->old_fs->super->s_inodes_count - |
| 274 | rfs->old_fs->super->s_free_inodes_count) > |
| 275 | rfs->new_fs->super->s_inodes_count) { |
| 276 | retval = ENOSPC; |
| 277 | goto errout; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * If we are shrinking the number block groups, we're done and |
| 282 | * can exit now. |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 283 | */ |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 284 | if (rfs->old_fs->group_desc_count > fs->group_desc_count) { |
| 285 | retval = 0; |
| 286 | goto errout; |
| 287 | } |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 288 | /* |
| 289 | * Fix the count of the last (old) block group |
| 290 | */ |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 291 | old_numblocks = (rfs->old_fs->super->s_blocks_count - |
| 292 | rfs->old_fs->super->s_first_data_block) % |
| 293 | rfs->old_fs->super->s_blocks_per_group; |
| 294 | if (!old_numblocks) |
| 295 | old_numblocks = rfs->old_fs->super->s_blocks_per_group; |
| 296 | if (rfs->old_fs->group_desc_count == fs->group_desc_count) { |
| 297 | numblocks = (rfs->new_fs->super->s_blocks_count - |
| 298 | rfs->new_fs->super->s_first_data_block) % |
| 299 | rfs->new_fs->super->s_blocks_per_group; |
| 300 | if (!numblocks) |
| 301 | numblocks = rfs->new_fs->super->s_blocks_per_group; |
| 302 | } else |
| 303 | numblocks = rfs->new_fs->super->s_blocks_per_group; |
| 304 | i = rfs->old_fs->group_desc_count - 1; |
| 305 | fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks); |
| 306 | |
| 307 | /* |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 308 | * If the number of block groups is staying the same, we're |
| 309 | * done and can exit now. (If the number block groups is |
| 310 | * shrinking, we had exited earlier.) |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 311 | */ |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 312 | if (rfs->old_fs->group_desc_count >= fs->group_desc_count) { |
| 313 | retval = 0; |
| 314 | goto errout; |
| 315 | } |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 316 | /* |
| 317 | * Initialize the new block group descriptors |
| 318 | */ |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 319 | retval = ext2fs_get_mem(fs->blocksize * fs->inode_blocks_per_group, |
| 320 | (void **) &rfs->itable_buf); |
| 321 | if (retval) |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 322 | goto errout; |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 323 | |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 324 | memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 325 | group_block = fs->super->s_first_data_block + |
| 326 | rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 327 | |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 328 | adj = rfs->old_fs->group_desc_count; |
| 329 | max_group = fs->group_desc_count - adj; |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 330 | if (rfs->progress) { |
| 331 | retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS, |
| 332 | 0, max_group); |
| 333 | if (retval) |
| 334 | goto errout; |
| 335 | } |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 336 | for (i = rfs->old_fs->group_desc_count; |
| 337 | i < fs->group_desc_count; i++) { |
| 338 | memset(&fs->group_desc[i], 0, |
| 339 | sizeof(struct ext2_group_desc)); |
| 340 | adjblocks = 0; |
| 341 | |
| 342 | if (i == fs->group_desc_count-1) { |
| 343 | numblocks = (fs->super->s_blocks_count - |
| 344 | fs->super->s_first_data_block) % |
| 345 | fs->super->s_blocks_per_group; |
| 346 | if (!numblocks) |
| 347 | numblocks = fs->super->s_blocks_per_group; |
| 348 | } else |
| 349 | numblocks = fs->super->s_blocks_per_group; |
| 350 | |
| 351 | if (ext2fs_bg_has_super(fs, i)) { |
| 352 | for (j=0; j < fs->desc_blocks+1; j++) |
| 353 | ext2fs_mark_block_bitmap(fs->block_map, |
| 354 | group_block + j); |
| 355 | adjblocks = 1 + fs->desc_blocks; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 356 | } |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 357 | adjblocks += 2 + fs->inode_blocks_per_group; |
| 358 | |
| 359 | numblocks -= adjblocks; |
| 360 | fs->super->s_free_blocks_count -= adjblocks; |
| 361 | fs->super->s_free_inodes_count += |
| 362 | fs->super->s_inodes_per_group; |
| 363 | fs->group_desc[i].bg_free_blocks_count = numblocks; |
| 364 | fs->group_desc[i].bg_free_inodes_count = |
| 365 | fs->super->s_inodes_per_group; |
| 366 | fs->group_desc[i].bg_used_dirs_count = 0; |
| 367 | |
| 368 | retval = ext2fs_allocate_group_table(fs, i, 0); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 369 | if (retval) goto errout; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 370 | |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 371 | /* |
| 372 | * Write out the new inode table |
| 373 | */ |
| 374 | retval = io_channel_write_blk(fs->io, |
| 375 | fs->group_desc[i].bg_inode_table, |
| 376 | fs->inode_blocks_per_group, |
| 377 | rfs->itable_buf); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 378 | if (retval) goto errout; |
| 379 | |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 380 | io_channel_flush(fs->io); |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 381 | if (rfs->progress) { |
| 382 | retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS, |
| 383 | i - adj + 1, max_group); |
| 384 | if (retval) |
| 385 | goto errout; |
| 386 | } |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 387 | group_block += fs->super->s_blocks_per_group; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 388 | } |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 389 | io_channel_flush(fs->io); |
| 390 | retval = 0; |
| 391 | |
| 392 | errout: |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 393 | return retval; |
| 394 | } |
| 395 | |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 396 | /* -------------------------------------------------------------------- |
| 397 | * |
| 398 | * Resize processing, phase 2. |
| 399 | * |
| 400 | * In this phase we adjust determine which blocks need to be moved, in |
| 401 | * blocks_to_move(). We then copy the blocks to their ultimate new |
| 402 | * destinations using block_mover(). Since we are copying blocks to |
| 403 | * their new locations, again during this pass we can abort without |
| 404 | * any problems. |
| 405 | * -------------------------------------------------------------------- |
| 406 | */ |
| 407 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 408 | /* |
| 409 | * This helper function creates a block bitmap with all of the |
| 410 | * filesystem meta-data blocks. |
| 411 | */ |
| 412 | static errcode_t mark_table_blocks(ext2_filsys fs, |
| 413 | ext2fs_block_bitmap *ret_bmap) |
| 414 | { |
| 415 | blk_t block, b; |
| 416 | int i,j; |
| 417 | ext2fs_block_bitmap bmap; |
| 418 | errcode_t retval; |
| 419 | |
| 420 | retval = ext2fs_allocate_block_bitmap(fs, "meta-data blocks", &bmap); |
| 421 | if (retval) |
| 422 | return retval; |
| 423 | |
| 424 | block = fs->super->s_first_data_block; |
| 425 | for (i = 0; i < fs->group_desc_count; i++) { |
| 426 | if (ext2fs_bg_has_super(fs, i)) { |
| 427 | /* |
| 428 | * Mark this group's copy of the superblock |
| 429 | */ |
| 430 | ext2fs_mark_block_bitmap(bmap, block); |
| 431 | |
| 432 | /* |
| 433 | * Mark this group's copy of the descriptors |
| 434 | */ |
| 435 | for (j = 0; j < fs->desc_blocks; j++) |
| 436 | ext2fs_mark_block_bitmap(bmap, block + j + 1); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Mark the blocks used for the inode table |
| 441 | */ |
| 442 | for (j = 0, b = fs->group_desc[i].bg_inode_table; |
| 443 | j < fs->inode_blocks_per_group; |
| 444 | j++, b++) |
| 445 | ext2fs_mark_block_bitmap(bmap, b); |
| 446 | |
| 447 | /* |
| 448 | * Mark block used for the block bitmap |
| 449 | */ |
| 450 | ext2fs_mark_block_bitmap(bmap, |
| 451 | fs->group_desc[i].bg_block_bitmap); |
| 452 | /* |
| 453 | * Mark block used for the inode bitmap |
| 454 | */ |
| 455 | ext2fs_mark_block_bitmap(bmap, |
| 456 | fs->group_desc[i].bg_inode_bitmap); |
| 457 | block += fs->super->s_blocks_per_group; |
| 458 | } |
| 459 | *ret_bmap = bmap; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /* |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 464 | * This routine marks and unmarks reserved blocks in the new block |
| 465 | * bitmap. It also determines which blocks need to be moved and |
| 466 | * places this information into the move_blocks bitmap. |
| 467 | */ |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 468 | static errcode_t blocks_to_move(ext2_resize_t rfs) |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 469 | { |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 470 | int i, j, max; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 471 | blk_t blk, group_blk; |
| 472 | unsigned long old_blocks, new_blocks; |
| 473 | errcode_t retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 474 | ext2_filsys fs, old_fs; |
| 475 | ext2fs_block_bitmap meta_bmap; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 476 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 477 | fs = rfs->new_fs; |
| 478 | old_fs = rfs->old_fs; |
| 479 | if (old_fs->super->s_blocks_count > fs->super->s_blocks_count) |
| 480 | fs = rfs->old_fs; |
| 481 | |
| 482 | retval = ext2fs_allocate_block_bitmap(fs, "reserved blocks", |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 483 | &rfs->reserve_blocks); |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 484 | if (retval) |
| 485 | return retval; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 486 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 487 | retval = ext2fs_allocate_block_bitmap(fs, "blocks to be moved", |
| 488 | &rfs->move_blocks); |
| 489 | if (retval) |
| 490 | return retval; |
| 491 | |
Theodore Ts'o | bce4979 | 1998-03-07 23:24:01 +0000 | [diff] [blame] | 492 | retval = mark_table_blocks(old_fs, &meta_bmap); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 493 | if (retval) |
| 494 | return retval; |
| 495 | |
| 496 | fs = rfs->new_fs; |
| 497 | |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 498 | /* |
| 499 | * If we're shrinking the filesystem, we need to move all of |
| 500 | * the blocks that don't fit any more |
| 501 | */ |
| 502 | for (blk = fs->super->s_blocks_count; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 503 | blk < old_fs->super->s_blocks_count; blk++) { |
| 504 | if (ext2fs_test_block_bitmap(old_fs->block_map, blk) && |
| 505 | !ext2fs_test_block_bitmap(meta_bmap, blk)) { |
| 506 | ext2fs_mark_block_bitmap(rfs->move_blocks, blk); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 507 | rfs->needed_blocks++; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 508 | } |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 509 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk); |
| 510 | } |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 511 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 512 | old_blocks = old_fs->desc_blocks; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 513 | new_blocks = fs->desc_blocks; |
| 514 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 515 | if (old_blocks == new_blocks) { |
| 516 | retval = 0; |
| 517 | goto errout; |
| 518 | } |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 519 | |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 520 | max = fs->group_desc_count; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 521 | if (max > old_fs->group_desc_count) |
| 522 | max = old_fs->group_desc_count; |
| 523 | group_blk = old_fs->super->s_first_data_block; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 524 | /* |
| 525 | * If we're reducing the number of descriptor blocks, this |
| 526 | * makes life easy. :-) We just have to mark some extra |
| 527 | * blocks as free. |
| 528 | */ |
| 529 | if (old_blocks > new_blocks) { |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 530 | for (i = 0; i < max; i++) { |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 531 | if (!ext2fs_bg_has_super(fs, i)) { |
| 532 | group_blk += fs->super->s_blocks_per_group; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 533 | continue; |
| 534 | } |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 535 | for (blk = group_blk+1+new_blocks; |
| 536 | blk < group_blk+1+old_blocks; blk++) { |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 537 | ext2fs_unmark_block_bitmap(fs->block_map, |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 538 | blk); |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 539 | rfs->needed_blocks--; |
| 540 | } |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 541 | group_blk += fs->super->s_blocks_per_group; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 542 | } |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 543 | retval = 0; |
| 544 | goto errout; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 545 | } |
| 546 | /* |
| 547 | * If we're increasing the number of descriptor blocks, life |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 548 | * gets interesting.... |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 549 | */ |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 550 | for (i = 0; i < max; i++) { |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 551 | if (!ext2fs_bg_has_super(fs, i)) |
| 552 | goto next_group; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 553 | |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 554 | for (blk = group_blk; |
| 555 | blk < group_blk + 1 + new_blocks; blk++) { |
| 556 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk); |
| 557 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| 558 | |
| 559 | /* |
| 560 | * Check to see if we overlap with the inode |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 561 | * or block bitmap, or the inode tables. If |
| 562 | * not, and the block is in use, then mark it |
| 563 | * as a block to be moved. |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 564 | */ |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 565 | if (IS_BLOCK_BM(fs, i, blk)) { |
| 566 | FS_BLOCK_BM(fs, i) = 0; |
| 567 | rfs->needed_blocks++; |
| 568 | } else if (IS_INODE_BM(fs, i, blk)) { |
| 569 | FS_INODE_BM(fs, i) = 0; |
| 570 | rfs->needed_blocks++; |
| 571 | } else if (IS_INODE_TB(fs, i, blk)) { |
| 572 | FS_INODE_TB(fs, i) = 0; |
| 573 | rfs->needed_blocks++; |
| 574 | } else if (ext2fs_test_block_bitmap(old_fs->block_map, |
| 575 | blk) && |
| 576 | !ext2fs_test_block_bitmap(meta_bmap, blk)) { |
| 577 | ext2fs_mark_block_bitmap(rfs->move_blocks, |
| 578 | blk); |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 579 | rfs->needed_blocks++; |
| 580 | } |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 581 | } |
| 582 | if (fs->group_desc[i].bg_inode_table && |
| 583 | fs->group_desc[i].bg_inode_bitmap && |
| 584 | fs->group_desc[i].bg_block_bitmap) |
| 585 | goto next_group; |
| 586 | |
| 587 | /* |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 588 | * Reserve the existing meta blocks that we know |
| 589 | * aren't to be moved. |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 590 | */ |
| 591 | if (fs->group_desc[i].bg_block_bitmap) |
| 592 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, |
| 593 | fs->group_desc[i].bg_block_bitmap); |
| 594 | if (fs->group_desc[i].bg_inode_bitmap) |
| 595 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, |
| 596 | fs->group_desc[i].bg_inode_bitmap); |
| 597 | if (fs->group_desc[i].bg_inode_table) |
| 598 | for (blk = fs->group_desc[i].bg_inode_table, j=0; |
| 599 | j < fs->inode_blocks_per_group ; j++, blk++) |
| 600 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, |
| 601 | blk); |
| 602 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 603 | /* |
| 604 | * Allocate the missing data structures |
| 605 | */ |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 606 | retval = ext2fs_allocate_group_table(fs, i, |
| 607 | rfs->reserve_blocks); |
| 608 | if (retval) |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 609 | goto errout; |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 610 | |
| 611 | /* |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 612 | * For those structures that have changed, we need to |
| 613 | * do bookkeepping. |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 614 | */ |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 615 | if (FS_BLOCK_BM(old_fs, i) != |
| 616 | (blk = FS_BLOCK_BM(fs, i))) { |
| 617 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| 618 | if (ext2fs_test_block_bitmap(old_fs->block_map, blk) && |
| 619 | !ext2fs_test_block_bitmap(meta_bmap, blk)) |
| 620 | ext2fs_mark_block_bitmap(rfs->move_blocks, |
| 621 | blk); |
| 622 | } |
| 623 | if (FS_INODE_BM(old_fs, i) != |
| 624 | (blk = FS_INODE_BM(fs, i))) { |
| 625 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| 626 | if (ext2fs_test_block_bitmap(old_fs->block_map, blk) && |
| 627 | !ext2fs_test_block_bitmap(meta_bmap, blk)) |
| 628 | ext2fs_mark_block_bitmap(rfs->move_blocks, |
| 629 | blk); |
| 630 | } |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 631 | |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 632 | /* |
| 633 | * The inode table, if we need to relocate it, is |
| 634 | * handled specially. We have to reserve the blocks |
| 635 | * for both the old and the new inode table, since we |
| 636 | * can't have the inode table be destroyed during the |
| 637 | * block relocation phase. |
| 638 | */ |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 639 | if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i)) |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 640 | goto next_group; /* inode table not moved */ |
| 641 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 642 | rfs->needed_blocks += fs->inode_blocks_per_group; |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Mark the new inode table as in use in the new block |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 646 | * allocation bitmap, and move any blocks that might |
| 647 | * be necessary. |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 648 | */ |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 649 | for (blk = fs->group_desc[i].bg_inode_table, j=0; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 650 | j < fs->inode_blocks_per_group ; j++, blk++) { |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 651 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 652 | if (ext2fs_test_block_bitmap(old_fs->block_map, blk) && |
| 653 | !ext2fs_test_block_bitmap(meta_bmap, blk)) |
| 654 | ext2fs_mark_block_bitmap(rfs->move_blocks, |
| 655 | blk); |
| 656 | } |
| 657 | |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 658 | /* |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 659 | * Make sure the old inode table is reserved in the |
| 660 | * block reservation bitmap. |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 661 | */ |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 662 | for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0; |
| 663 | j < fs->inode_blocks_per_group ; j++, blk++) |
| 664 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk); |
Theodore Ts'o | 1e1da29 | 1997-06-09 14:51:29 +0000 | [diff] [blame] | 665 | |
| 666 | next_group: |
| 667 | group_blk += rfs->new_fs->super->s_blocks_per_group; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 668 | } |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 669 | retval = 0; |
| 670 | |
| 671 | errout: |
| 672 | if (meta_bmap) |
| 673 | ext2fs_free_block_bitmap(meta_bmap); |
| 674 | |
| 675 | return retval; |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 678 | /* |
| 679 | * This helper function tries to allocate a new block. We try to |
| 680 | * avoid hitting the original group descriptor blocks at least at |
| 681 | * first, since we want to make it possible to recover from a badly |
| 682 | * aborted resize operation as much as possible. |
| 683 | * |
| 684 | * In the future, I may further modify this routine to balance out |
| 685 | * where we get the new blocks across the various block groups. |
| 686 | * Ideally we would allocate blocks that corresponded with the block |
| 687 | * group of the containing inode, and keep contiguous blocks |
| 688 | * together. However, this very difficult to do efficiently, since we |
| 689 | * don't have the necessary information up front. |
| 690 | */ |
| 691 | |
| 692 | #define AVOID_OLD 1 |
| 693 | #define DESPERATION 2 |
| 694 | |
| 695 | static void init_block_alloc(ext2_resize_t rfs) |
| 696 | { |
| 697 | rfs->alloc_state = AVOID_OLD; |
| 698 | rfs->new_blk = rfs->new_fs->super->s_first_data_block; |
Theodore Ts'o | 2bc4d4f | 1998-03-21 03:27:48 +0000 | [diff] [blame] | 699 | #if 0 |
| 700 | /* HACK for testing */ |
| 701 | if (rfs->new_fs->super->s_blocks_count > |
| 702 | rfs->old_fs->super->s_blocks_count) |
| 703 | rfs->new_blk = rfs->old_fs->super->s_blocks_count; |
| 704 | #endif |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static blk_t get_new_block(ext2_resize_t rfs) |
| 708 | { |
| 709 | ext2_filsys fs = rfs->new_fs; |
| 710 | |
| 711 | while (1) { |
| 712 | if (rfs->new_blk >= fs->super->s_blocks_count) { |
| 713 | if (rfs->alloc_state == DESPERATION) |
| 714 | return 0; |
| 715 | |
| 716 | #ifdef RESIZE2FS_DEBUG |
| 717 | if (rfs->flags & RESIZE_DEBUG_BMOVE) |
| 718 | printf("Going into desperation " |
| 719 | "mode for block allocations\n"); |
| 720 | #endif |
| 721 | rfs->alloc_state = DESPERATION; |
| 722 | rfs->new_blk = fs->super->s_first_data_block; |
| 723 | continue; |
| 724 | } |
| 725 | if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) || |
| 726 | ext2fs_test_block_bitmap(rfs->reserve_blocks, |
| 727 | rfs->new_blk) || |
| 728 | ((rfs->alloc_state == AVOID_OLD) && |
Theodore Ts'o | bce4979 | 1998-03-07 23:24:01 +0000 | [diff] [blame] | 729 | (rfs->new_blk < rfs->old_fs->super->s_blocks_count) && |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 730 | ext2fs_test_block_bitmap(rfs->old_fs->block_map, |
| 731 | rfs->new_blk))) { |
| 732 | rfs->new_blk++; |
| 733 | continue; |
| 734 | } |
| 735 | return rfs->new_blk; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | static errcode_t block_mover(ext2_resize_t rfs) |
| 740 | { |
| 741 | blk_t blk, old_blk, new_blk; |
| 742 | ext2_filsys fs = rfs->new_fs; |
| 743 | ext2_filsys old_fs = rfs->old_fs; |
| 744 | errcode_t retval; |
| 745 | int size, c; |
| 746 | int to_move, moved; |
| 747 | |
| 748 | new_blk = fs->super->s_first_data_block; |
| 749 | if (!rfs->itable_buf) { |
| 750 | retval = ext2fs_get_mem(fs->blocksize * |
| 751 | fs->inode_blocks_per_group, |
| 752 | (void **) &rfs->itable_buf); |
| 753 | if (retval) |
| 754 | return retval; |
| 755 | } |
| 756 | retval = ext2fs_create_extent_table(&rfs->bmap, 0); |
| 757 | if (retval) |
| 758 | return retval; |
| 759 | |
| 760 | /* |
| 761 | * The first step is to figure out where all of the blocks |
| 762 | * will go. |
| 763 | */ |
| 764 | to_move = moved = 0; |
| 765 | init_block_alloc(rfs); |
| 766 | for (blk = old_fs->super->s_first_data_block; |
| 767 | blk < old_fs->super->s_blocks_count; blk++) { |
| 768 | if (!ext2fs_test_block_bitmap(old_fs->block_map, blk)) |
| 769 | continue; |
| 770 | if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk)) |
| 771 | continue; |
| 772 | |
| 773 | new_blk = get_new_block(rfs); |
| 774 | if (!new_blk) { |
| 775 | retval = ENOSPC; |
| 776 | goto errout; |
| 777 | } |
| 778 | ext2fs_mark_block_bitmap(fs->block_map, new_blk); |
| 779 | ext2fs_add_extent_entry(rfs->bmap, blk, new_blk); |
| 780 | to_move++; |
| 781 | } |
| 782 | |
| 783 | if (to_move == 0) { |
| 784 | retval = 0; |
| 785 | goto errout; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Step two is to actually move the blocks |
| 790 | */ |
| 791 | retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0); |
| 792 | if (retval) goto errout; |
| 793 | |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 794 | if (rfs->progress) { |
| 795 | retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS, |
| 796 | 0, to_move); |
| 797 | if (retval) |
| 798 | goto errout; |
| 799 | } |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 800 | while (1) { |
| 801 | retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size); |
| 802 | if (retval) goto errout; |
| 803 | if (!size) |
| 804 | break; |
| 805 | #ifdef RESIZE2FS_DEBUG |
| 806 | if (rfs->flags & RESIZE_DEBUG_BMOVE) |
| 807 | printf("Moving %d blocks %u->%u\n", size, |
| 808 | old_blk, new_blk); |
| 809 | #endif |
| 810 | do { |
| 811 | c = size; |
| 812 | if (c > fs->inode_blocks_per_group) |
| 813 | c = fs->inode_blocks_per_group; |
| 814 | retval = io_channel_read_blk(fs->io, old_blk, c, |
| 815 | rfs->itable_buf); |
| 816 | if (retval) goto errout; |
| 817 | retval = io_channel_write_blk(fs->io, new_blk, c, |
| 818 | rfs->itable_buf); |
| 819 | if (retval) goto errout; |
| 820 | size -= c; |
| 821 | new_blk += c; |
| 822 | old_blk += c; |
| 823 | moved += c; |
| 824 | if (rfs->progress) { |
| 825 | io_channel_flush(fs->io); |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 826 | retval = (rfs->progress)(rfs, |
| 827 | E2_RSZ_BLOCK_RELOC_PASS, |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 828 | moved, to_move); |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 829 | if (retval) |
| 830 | goto errout; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 831 | } |
| 832 | } while (size > 0); |
| 833 | io_channel_flush(fs->io); |
| 834 | } |
| 835 | |
| 836 | errout: |
| 837 | return retval; |
| 838 | } |
| 839 | |
| 840 | |
| 841 | /* -------------------------------------------------------------------- |
| 842 | * |
| 843 | * Resize processing, phase 3 |
| 844 | * |
| 845 | * -------------------------------------------------------------------- |
| 846 | */ |
| 847 | |
| 848 | |
| 849 | struct process_block_struct { |
| 850 | ext2_resize_t rfs; |
| 851 | ino_t ino; |
| 852 | struct ext2_inode * inode; |
| 853 | errcode_t error; |
| 854 | int is_dir; |
| 855 | int changed; |
| 856 | }; |
| 857 | |
| 858 | static int process_block(ext2_filsys fs, blk_t *block_nr, |
Theodore Ts'o | 101c84f | 1998-03-24 16:27:11 +0000 | [diff] [blame^] | 859 | blkcnt_t blockcnt, blk_t ref_block, |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 860 | int ref_offset, void *priv_data) |
| 861 | { |
| 862 | struct process_block_struct *pb; |
| 863 | errcode_t retval; |
| 864 | blk_t block, new_block; |
| 865 | int ret = 0; |
| 866 | |
| 867 | pb = (struct process_block_struct *) priv_data; |
| 868 | block = *block_nr; |
| 869 | if (pb->rfs->bmap) { |
| 870 | new_block = ext2fs_extent_translate(pb->rfs->bmap, block); |
| 871 | if (new_block) { |
| 872 | *block_nr = new_block; |
| 873 | ret |= BLOCK_CHANGED; |
| 874 | pb->changed = 1; |
| 875 | #ifdef RESIZE2FS_DEBUG |
| 876 | if (pb->rfs->flags & RESIZE_DEBUG_BMOVE) |
Theodore Ts'o | 101c84f | 1998-03-24 16:27:11 +0000 | [diff] [blame^] | 877 | printf("ino=%ld, blockcnt=%ld, %u->%u\n", |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 878 | pb->ino, blockcnt, block, new_block); |
| 879 | #endif |
| 880 | block = new_block; |
| 881 | } |
| 882 | } |
| 883 | if (pb->is_dir) { |
| 884 | retval = ext2fs_add_dir_block(fs->dblist, pb->ino, |
Theodore Ts'o | 101c84f | 1998-03-24 16:27:11 +0000 | [diff] [blame^] | 885 | block, (int) blockcnt); |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 886 | if (retval) { |
| 887 | pb->error = retval; |
| 888 | ret |= BLOCK_ABORT; |
| 889 | } |
| 890 | } |
| 891 | return ret; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * Progress callback |
| 896 | */ |
| 897 | static errcode_t progress_callback(ext2_filsys fs, ext2_inode_scan scan, |
| 898 | dgrp_t group, void * priv_data) |
| 899 | { |
| 900 | ext2_resize_t rfs = (ext2_resize_t) priv_data; |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 901 | errcode_t retval; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 902 | |
| 903 | /* |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 904 | * This check is to protect against old ext2 libraries. It |
| 905 | * shouldn't be needed against new libraries. |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 906 | */ |
Theodore Ts'o | f4b2a6d | 1998-02-21 04:20:44 +0000 | [diff] [blame] | 907 | if ((group+1) == 0) |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 908 | return 0; |
| 909 | |
| 910 | if (rfs->progress) { |
| 911 | io_channel_flush(fs->io); |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 912 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS, |
| 913 | group+1, fs->group_desc_count); |
| 914 | if (retval) |
| 915 | return retval; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static errcode_t inode_scan_and_fix(ext2_resize_t rfs) |
| 922 | { |
| 923 | struct process_block_struct pb; |
| 924 | ino_t ino, new_inode; |
| 925 | struct ext2_inode inode; |
| 926 | ext2_inode_scan scan = NULL; |
| 927 | errcode_t retval; |
| 928 | int group; |
| 929 | char *block_buf = 0; |
| 930 | ino_t start_to_move; |
Theodore Ts'o | 2bc4d4f | 1998-03-21 03:27:48 +0000 | [diff] [blame] | 931 | blk_t orig_size; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 932 | |
| 933 | if ((rfs->old_fs->group_desc_count <= |
| 934 | rfs->new_fs->group_desc_count) && |
| 935 | !rfs->bmap) |
| 936 | return 0; |
| 937 | |
Theodore Ts'o | 2bc4d4f | 1998-03-21 03:27:48 +0000 | [diff] [blame] | 938 | /* |
| 939 | * Save the original size of the old filesystem, and |
| 940 | * temporarily set the size to be the new size if the new size |
| 941 | * is larger. We need to do this to avoid catching an error |
| 942 | * by the block iterator routines |
| 943 | */ |
| 944 | orig_size = rfs->old_fs->super->s_blocks_count; |
| 945 | if (orig_size < rfs->new_fs->super->s_blocks_count) |
| 946 | rfs->old_fs->super->s_blocks_count = |
| 947 | rfs->new_fs->super->s_blocks_count; |
| 948 | |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 949 | retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan); |
| 950 | if (retval) goto errout; |
| 951 | |
| 952 | retval = ext2fs_init_dblist(rfs->old_fs, 0); |
| 953 | if (retval) goto errout; |
| 954 | retval = ext2fs_get_mem(rfs->old_fs->blocksize * 3, |
| 955 | (void **) &block_buf); |
| 956 | if (retval) goto errout; |
| 957 | |
| 958 | start_to_move = (rfs->new_fs->group_desc_count * |
| 959 | rfs->new_fs->super->s_inodes_per_group); |
| 960 | |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 961 | if (rfs->progress) { |
| 962 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS, |
| 963 | 0, rfs->old_fs->group_desc_count); |
| 964 | if (retval) |
| 965 | goto errout; |
| 966 | } |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 967 | ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs); |
| 968 | pb.rfs = rfs; |
| 969 | pb.inode = &inode; |
| 970 | pb.error = 0; |
| 971 | new_inode = EXT2_FIRST_INODE(rfs->new_fs->super); |
| 972 | /* |
| 973 | * First, copy all of the inodes that need to be moved |
| 974 | * elsewhere in the inode table |
| 975 | */ |
| 976 | while (1) { |
| 977 | retval = ext2fs_get_next_inode(scan, &ino, &inode); |
| 978 | if (retval) goto errout; |
| 979 | if (!ino) |
| 980 | break; |
| 981 | |
| 982 | if (inode.i_links_count == 0) |
| 983 | continue; /* inode not in use */ |
| 984 | |
| 985 | pb.is_dir = LINUX_S_ISDIR(inode.i_mode); |
| 986 | pb.changed = 0; |
| 987 | |
| 988 | if (ext2fs_inode_has_valid_blocks(&inode) && |
| 989 | (rfs->bmap || pb.is_dir)) { |
| 990 | pb.ino = ino; |
| 991 | retval = ext2fs_block_iterate2(rfs->old_fs, |
| 992 | ino, 0, block_buf, |
| 993 | process_block, &pb); |
| 994 | if (retval) |
| 995 | goto errout; |
| 996 | if (pb.error) { |
| 997 | retval = pb.error; |
| 998 | goto errout; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | if (ino <= start_to_move) |
| 1003 | continue; /* Don't need to move it. */ |
| 1004 | |
| 1005 | /* |
| 1006 | * Find a new inode |
| 1007 | */ |
| 1008 | while (1) { |
| 1009 | if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map, |
| 1010 | new_inode)) |
| 1011 | break; |
| 1012 | new_inode++; |
| 1013 | if (new_inode > rfs->new_fs->super->s_inodes_count) { |
| 1014 | retval = ENOSPC; |
| 1015 | goto errout; |
| 1016 | } |
| 1017 | } |
| 1018 | ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode); |
| 1019 | if (pb.changed) { |
| 1020 | /* Get the new version of the inode */ |
| 1021 | retval = ext2fs_read_inode(rfs->old_fs, ino, &inode); |
| 1022 | if (retval) goto errout; |
| 1023 | } |
| 1024 | retval = ext2fs_write_inode(rfs->old_fs, new_inode, &inode); |
| 1025 | if (retval) goto errout; |
| 1026 | |
| 1027 | group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super); |
| 1028 | if (LINUX_S_ISDIR(inode.i_mode)) |
| 1029 | rfs->new_fs->group_desc[group].bg_used_dirs_count++; |
| 1030 | |
| 1031 | #ifdef RESIZE2FS_DEBUG |
| 1032 | if (rfs->flags & RESIZE_DEBUG_INODEMAP) |
| 1033 | printf("Inode moved %ld->%ld\n", ino, new_inode); |
| 1034 | #endif |
| 1035 | if (!rfs->imap) { |
| 1036 | retval = ext2fs_create_extent_table(&rfs->imap, 0); |
| 1037 | if (retval) |
| 1038 | goto errout; |
| 1039 | } |
| 1040 | ext2fs_add_extent_entry(rfs->imap, ino, new_inode); |
| 1041 | } |
| 1042 | io_channel_flush(rfs->old_fs->io); |
| 1043 | |
| 1044 | errout: |
Theodore Ts'o | 2bc4d4f | 1998-03-21 03:27:48 +0000 | [diff] [blame] | 1045 | rfs->old_fs->super->s_blocks_count = orig_size; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1046 | if (rfs->bmap) { |
| 1047 | ext2fs_free_extent_table(rfs->bmap); |
| 1048 | rfs->bmap = 0; |
| 1049 | } |
| 1050 | if (scan) |
| 1051 | ext2fs_close_inode_scan(scan); |
| 1052 | if (block_buf) |
| 1053 | ext2fs_free_mem((void **) &block_buf); |
| 1054 | return retval; |
| 1055 | } |
| 1056 | |
| 1057 | /* -------------------------------------------------------------------- |
| 1058 | * |
| 1059 | * Resize processing, phase 4. |
| 1060 | * |
| 1061 | * -------------------------------------------------------------------- |
| 1062 | */ |
| 1063 | |
| 1064 | struct istruct { |
| 1065 | ext2_resize_t rfs; |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1066 | errcode_t err; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1067 | unsigned long max; |
| 1068 | int num; |
| 1069 | }; |
| 1070 | |
| 1071 | static int check_and_change_inodes(ino_t dir, int entry, |
| 1072 | struct ext2_dir_entry *dirent, int offset, |
| 1073 | int blocksize, char *buf, void *priv_data) |
| 1074 | { |
| 1075 | struct istruct *is = (struct istruct *) priv_data; |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1076 | ino_t new_inode; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (is->rfs->progress && offset == 0) { |
| 1079 | io_channel_flush(is->rfs->old_fs->io); |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1080 | is->err = (is->rfs->progress)(is->rfs, |
| 1081 | E2_RSZ_INODE_REF_UPD_PASS, |
| 1082 | ++is->num, is->max); |
| 1083 | if (is->err) |
| 1084 | return DIRENT_ABORT; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | if (!dirent->inode) |
| 1088 | return 0; |
| 1089 | |
| 1090 | new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode); |
| 1091 | |
| 1092 | if (!new_inode) |
| 1093 | return 0; |
| 1094 | #ifdef RESIZE2FS_DEBUG |
| 1095 | if (is->rfs->flags & RESIZE_DEBUG_INODEMAP) |
| 1096 | printf("Inode translate (dir=%ld, name=%.*s, %u->%ld)\n", |
| 1097 | dir, dirent->name_len, dirent->name, |
| 1098 | dirent->inode, new_inode); |
| 1099 | #endif |
| 1100 | |
| 1101 | dirent->inode = new_inode; |
| 1102 | |
| 1103 | return DIRENT_CHANGED; |
| 1104 | } |
| 1105 | |
| 1106 | static errcode_t inode_ref_fix(ext2_resize_t rfs) |
| 1107 | { |
| 1108 | errcode_t retval; |
| 1109 | struct istruct is; |
| 1110 | |
| 1111 | if (!rfs->imap) |
| 1112 | return 0; |
| 1113 | |
| 1114 | /* |
| 1115 | * Now, we iterate over all of the directories to update the |
| 1116 | * inode references |
| 1117 | */ |
| 1118 | is.num = 0; |
| 1119 | is.max = ext2fs_dblist_count(rfs->old_fs->dblist); |
| 1120 | is.rfs = rfs; |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1121 | is.err = 0; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1122 | |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1123 | if (rfs->progress) { |
| 1124 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS, |
| 1125 | 0, is.max); |
| 1126 | if (retval) |
| 1127 | goto errout; |
| 1128 | } |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1129 | |
| 1130 | retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist, |
| 1131 | DIRENT_FLAG_INCLUDE_EMPTY, 0, |
| 1132 | check_and_change_inodes, &is); |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1133 | if (retval) |
| 1134 | goto errout; |
| 1135 | if (is.err) { |
| 1136 | retval = is.err; |
| 1137 | goto errout; |
| 1138 | } |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1139 | |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1140 | errout: |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1141 | ext2fs_free_extent_table(rfs->imap); |
| 1142 | rfs->imap = 0; |
| 1143 | return retval; |
| 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | /* -------------------------------------------------------------------- |
| 1148 | * |
| 1149 | * Resize processing, phase 5. |
| 1150 | * |
| 1151 | * In this phase we actually move the inode table around, and then |
| 1152 | * update the summary statistics. This is scary, since aborting here |
| 1153 | * will potentially scramble the filesystem. (We are moving the |
| 1154 | * inode tables around in place, and so the potential for lost data, |
| 1155 | * or at the very least scrambling the mapping between filenames and |
| 1156 | * inode numbers is very high in case of a power failure here.) |
| 1157 | * -------------------------------------------------------------------- |
| 1158 | */ |
| 1159 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 1160 | |
Theodore Ts'o | 24b2c7a | 1997-06-07 20:42:58 +0000 | [diff] [blame] | 1161 | /* |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1162 | * A very scary routine --- this one moves the inode table around!!! |
| 1163 | * |
| 1164 | * After this you have to use the rfs->new_fs file handle to read and |
| 1165 | * write inodes. |
| 1166 | */ |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 1167 | static errcode_t move_itables(ext2_resize_t rfs) |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1168 | { |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1169 | int i, n, num, max, size, diff; |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1170 | ext2_filsys fs = rfs->new_fs; |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1171 | char *cp; |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 1172 | blk_t old_blk, new_blk; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1173 | errcode_t retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 1174 | int to_move, moved; |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1175 | |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1176 | max = fs->group_desc_count; |
| 1177 | if (max > rfs->old_fs->group_desc_count) |
| 1178 | max = rfs->old_fs->group_desc_count; |
| 1179 | |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1180 | size = fs->blocksize * fs->inode_blocks_per_group; |
| 1181 | if (!rfs->itable_buf) { |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 1182 | retval = ext2fs_get_mem(size, (void **) &rfs->itable_buf); |
| 1183 | if (retval) |
| 1184 | return retval; |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1185 | } |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* |
| 1188 | * Figure out how many inode tables we need to move |
| 1189 | */ |
| 1190 | to_move = moved = 0; |
| 1191 | for (i=0; i < max; i++) |
| 1192 | if (rfs->old_fs->group_desc[i].bg_inode_table != |
| 1193 | fs->group_desc[i].bg_inode_table) |
| 1194 | to_move++; |
| 1195 | |
| 1196 | if (to_move == 0) |
| 1197 | return 0; |
| 1198 | |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1199 | if (rfs->progress) { |
| 1200 | retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, |
| 1201 | 0, to_move); |
| 1202 | if (retval) |
| 1203 | goto errout; |
| 1204 | } |
Theodore Ts'o | 63b44fb | 1998-02-13 22:58:18 +0000 | [diff] [blame] | 1205 | |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1206 | rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
| 1207 | |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1208 | for (i=0; i < max; i++) { |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 1209 | old_blk = rfs->old_fs->group_desc[i].bg_inode_table; |
| 1210 | new_blk = fs->group_desc[i].bg_inode_table; |
| 1211 | diff = new_blk - old_blk; |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1212 | |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 1213 | #ifdef RESIZE2FS_DEBUG |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1214 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
| 1215 | printf("Itable move group %d block " |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 1216 | "%u->%u (diff %d)\n", |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 1217 | i, old_blk, new_blk, diff); |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 1218 | #endif |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1219 | |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1220 | if (!diff) |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1221 | continue; |
| 1222 | |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 1223 | retval = io_channel_read_blk(fs->io, old_blk, |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1224 | fs->inode_blocks_per_group, |
| 1225 | rfs->itable_buf); |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1226 | if (retval) |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1227 | goto errout; |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1228 | /* |
| 1229 | * The end of the inode table segment often contains |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1230 | * all zeros, and we're often only moving the inode |
| 1231 | * table down a block or two. If so, we can optimize |
| 1232 | * things by not rewriting blocks that we know to be zero |
| 1233 | * already. |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1234 | */ |
| 1235 | for (cp = rfs->itable_buf+size, n=0; n < size; n++, cp--) |
| 1236 | if (*cp) |
| 1237 | break; |
| 1238 | n = n >> EXT2_BLOCK_SIZE_BITS(fs->super); |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 1239 | #ifdef RESIZE2FS_DEBUG |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1240 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
| 1241 | printf("%d blocks of zeros...\n", n); |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 1242 | #endif |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1243 | num = fs->inode_blocks_per_group; |
| 1244 | if (n > diff) |
| 1245 | num -= n; |
| 1246 | |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 1247 | retval = io_channel_write_blk(fs->io, new_blk, |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1248 | num, rfs->itable_buf); |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1249 | if (retval) { |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 1250 | io_channel_write_blk(fs->io, old_blk, |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1251 | num, rfs->itable_buf); |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1252 | goto errout; |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1253 | } |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1254 | if (n > diff) { |
| 1255 | retval = io_channel_write_blk(fs->io, |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 1256 | old_blk + fs->inode_blocks_per_group, |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1257 | diff, (rfs->itable_buf + |
| 1258 | (fs->inode_blocks_per_group - diff) * |
| 1259 | fs->blocksize)); |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1260 | if (retval) |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1261 | goto errout; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 1262 | } |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1263 | rfs->old_fs->group_desc[i].bg_inode_table = new_blk; |
| 1264 | ext2fs_mark_super_dirty(rfs->old_fs); |
| 1265 | if (rfs->progress) { |
| 1266 | ext2fs_flush(rfs->old_fs); |
Theodore Ts'o | 3b627e8 | 1998-02-24 20:24:49 +0000 | [diff] [blame] | 1267 | retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, |
| 1268 | ++moved, to_move); |
| 1269 | if (retval) |
| 1270 | goto errout; |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1271 | } |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1272 | } |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1273 | ext2fs_flush(fs); |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 1274 | #ifdef RESIZE2FS_DEBUG |
Theodore Ts'o | 05e112a | 1997-06-14 07:28:44 +0000 | [diff] [blame] | 1275 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
| 1276 | printf("Inode table move finished.\n"); |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 1277 | #endif |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1278 | return 0; |
| 1279 | |
Theodore Ts'o | a8519a2 | 1998-02-16 22:16:20 +0000 | [diff] [blame] | 1280 | errout: |
Theodore Ts'o | 052db4b | 1997-06-12 07:14:32 +0000 | [diff] [blame] | 1281 | return retval; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * Finally, recalculate the summary information |
| 1286 | */ |
| 1287 | static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs) |
| 1288 | { |
| 1289 | blk_t blk; |
| 1290 | ino_t ino; |
| 1291 | int group = 0; |
| 1292 | int count = 0; |
| 1293 | int total_free = 0; |
| 1294 | int group_free = 0; |
| 1295 | |
| 1296 | /* |
| 1297 | * First calculate the block statistics |
| 1298 | */ |
| 1299 | for (blk = fs->super->s_first_data_block; |
| 1300 | blk < fs->super->s_blocks_count; blk++) { |
| 1301 | if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) { |
| 1302 | group_free++; |
| 1303 | total_free++; |
| 1304 | } |
| 1305 | count++; |
| 1306 | if ((count == fs->super->s_blocks_per_group) || |
| 1307 | (blk == fs->super->s_blocks_count-1)) { |
| 1308 | fs->group_desc[group++].bg_free_blocks_count = |
| 1309 | group_free; |
| 1310 | count = 0; |
| 1311 | group_free = 0; |
| 1312 | } |
| 1313 | } |
| 1314 | fs->super->s_free_blocks_count = total_free; |
| 1315 | |
| 1316 | /* |
| 1317 | * Next, calculate the inode statistics |
| 1318 | */ |
| 1319 | group_free = 0; |
| 1320 | total_free = 0; |
| 1321 | count = 0; |
| 1322 | group = 0; |
| 1323 | for (ino = 1; ino <= fs->super->s_inodes_count; ino++) { |
| 1324 | if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) { |
| 1325 | group_free++; |
| 1326 | total_free++; |
| 1327 | } |
| 1328 | count++; |
| 1329 | if ((count == fs->super->s_inodes_per_group) || |
| 1330 | (ino == fs->super->s_inodes_count)) { |
| 1331 | fs->group_desc[group++].bg_free_inodes_count = |
| 1332 | group_free; |
| 1333 | count = 0; |
| 1334 | group_free = 0; |
| 1335 | } |
| 1336 | } |
| 1337 | fs->super->s_free_inodes_count = total_free; |
| 1338 | ext2fs_mark_super_dirty(fs); |
| 1339 | return 0; |
| 1340 | } |