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