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