]>
Commit | Line | Data |
---|---|---|
24b2c7a7 TT |
1 | /* |
2 | * resize2fs.c --- ext2 main routine | |
3 | * | |
0cee8a5c TT |
4 | * Copyright (C) 1997, 1998 by Theodore Ts'o and |
5 | * PowerQuest, Inc. | |
6 | * | |
7 | * Copyright (C) 1999, 2000 by Theosore Ts'o | |
efc6f628 | 8 | * |
24b2c7a7 | 9 | * %Begin-Header% |
0cee8a5c TT |
10 | * This file may be redistributed under the terms of the GNU Public |
11 | * License. | |
24b2c7a7 TT |
12 | * %End-Header% |
13 | */ | |
14 | ||
05e112a1 TT |
15 | /* |
16 | * Resizing a filesystem consists of the following phases: | |
17 | * | |
a8519a2d | 18 | * 1. Adjust superblock and write out new parts of the inode |
05e112a1 | 19 | * table |
a8519a2d TT |
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. | |
05e112a1 | 34 | */ |
a8519a2d | 35 | |
d1154eb4 | 36 | #include "config.h" |
24b2c7a7 | 37 | #include "resize2fs.h" |
b969b1b8 | 38 | #include <time.h> |
24b2c7a7 | 39 | |
546a1ff1 | 40 | #ifdef __linux__ /* Kludge for debugging */ |
a8519a2d TT |
41 | #define RESIZE2FS_DEBUG |
42 | #endif | |
43 | ||
86acdebd | 44 | static void fix_uninit_block_bitmaps(ext2_filsys fs); |
8728d506 | 45 | static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size); |
a8519a2d TT |
46 | static errcode_t blocks_to_move(ext2_resize_t rfs); |
47 | static errcode_t block_mover(ext2_resize_t rfs); | |
48 | static errcode_t inode_scan_and_fix(ext2_resize_t rfs); | |
49 | static errcode_t inode_ref_fix(ext2_resize_t rfs); | |
50 | static errcode_t move_itables(ext2_resize_t rfs); | |
9213a93b | 51 | static errcode_t fix_resize_inode(ext2_filsys fs); |
a8519a2d | 52 | static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs); |
8a6ede8b | 53 | static errcode_t fix_sb_journal_backup(ext2_filsys fs); |
f026f1a3 TT |
54 | static errcode_t mark_table_blocks(ext2_filsys fs, |
55 | ext2fs_block_bitmap bmap); | |
65c6c3e0 TT |
56 | static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs); |
57 | static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs, | |
58 | ext2fs_block_bitmap meta_bmap); | |
a8519a2d TT |
59 | |
60 | /* | |
61 | * Some helper CPP macros | |
62 | */ | |
d7cca6b0 VAH |
63 | #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i))) |
64 | #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i))) | |
a8519a2d | 65 | |
d7cca6b0 VAH |
66 | #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \ |
67 | ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \ | |
a8519a2d TT |
68 | (fs)->inode_blocks_per_group))) |
69 | ||
1773c87c TT |
70 | /* Some bigalloc helper macros which are more succint... */ |
71 | #define B2C(x) EXT2FS_B2C(fs, (x)) | |
72 | #define C2B(x) EXT2FS_C2B(fs, (x)) | |
73 | #define EQ_CLSTR(x, y) (B2C(x) == B2C(y)) | |
74 | #define LE_CLSTR(x, y) (B2C(x) <= B2C(y)) | |
75 | #define LT_CLSTR(x, y) (B2C(x) < B2C(y)) | |
76 | #define GE_CLSTR(x, y) (B2C(x) >= B2C(y)) | |
77 | #define GT_CLSTR(x, y) (B2C(x) > B2C(y)) | |
78 | ||
f404167d | 79 | static int lazy_itable_init; |
2d8c0c8a | 80 | |
a8519a2d TT |
81 | /* |
82 | * This is the top-level routine which does the dirty deed.... | |
83 | */ | |
8728d506 | 84 | errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags, |
3346084b | 85 | errcode_t (*progress)(ext2_resize_t rfs, int pass, |
86acdebd TT |
86 | unsigned long cur, |
87 | unsigned long max_val)) | |
a8519a2d TT |
88 | { |
89 | ext2_resize_t rfs; | |
90 | errcode_t retval; | |
1eb31c48 | 91 | struct resource_track rtrack, overall_track; |
96cdb37e | 92 | |
a8519a2d TT |
93 | /* |
94 | * Create the data structure | |
95 | */ | |
c4e3d3f3 | 96 | retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs); |
a8519a2d TT |
97 | if (retval) |
98 | return retval; | |
a8519a2d | 99 | |
1eb31c48 | 100 | memset(rfs, 0, sizeof(struct ext2_resize_struct)); |
7f9c96ee | 101 | fs->priv_data = rfs; |
a8519a2d TT |
102 | rfs->old_fs = fs; |
103 | rfs->flags = flags; | |
104 | rfs->itable_buf = 0; | |
105 | rfs->progress = progress; | |
1eb31c48 TT |
106 | |
107 | init_resource_track(&overall_track, "overall resize2fs", fs->io); | |
108 | init_resource_track(&rtrack, "read_bitmaps", fs->io); | |
109 | retval = ext2fs_read_bitmaps(fs); | |
110 | if (retval) | |
111 | goto errout; | |
112 | print_resource_track(rfs, &rtrack, fs->io); | |
113 | ||
114 | fs->super->s_state |= EXT2_ERROR_FS; | |
115 | ext2fs_mark_super_dirty(fs); | |
116 | ext2fs_flush(fs); | |
117 | ||
118 | init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io); | |
119 | fix_uninit_block_bitmaps(fs); | |
120 | print_resource_track(rfs, &rtrack, fs->io); | |
a8519a2d TT |
121 | retval = ext2fs_dup_handle(fs, &rfs->new_fs); |
122 | if (retval) | |
123 | goto errout; | |
124 | ||
1eb31c48 | 125 | init_resource_track(&rtrack, "adjust_superblock", fs->io); |
116db1b5 | 126 | retval = adjust_superblock(rfs, *new_size); |
a8519a2d TT |
127 | if (retval) |
128 | goto errout; | |
1eb31c48 | 129 | print_resource_track(rfs, &rtrack, fs->io); |
a8519a2d | 130 | |
1eb31c48 TT |
131 | |
132 | init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io); | |
86acdebd | 133 | fix_uninit_block_bitmaps(rfs->new_fs); |
1eb31c48 | 134 | print_resource_track(rfs, &rtrack, fs->io); |
86acdebd | 135 | /* Clear the block bitmap uninit flag for the last block group */ |
e633b58a | 136 | ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1, |
732c8cd5 | 137 | EXT2_BG_BLOCK_UNINIT); |
86acdebd | 138 | |
4efbac6f | 139 | *new_size = ext2fs_blocks_count(rfs->new_fs->super); |
116db1b5 | 140 | |
1eb31c48 | 141 | init_resource_track(&rtrack, "blocks_to_move", fs->io); |
a8519a2d TT |
142 | retval = blocks_to_move(rfs); |
143 | if (retval) | |
144 | goto errout; | |
1eb31c48 | 145 | print_resource_track(rfs, &rtrack, fs->io); |
a8519a2d TT |
146 | |
147 | #ifdef RESIZE2FS_DEBUG | |
148 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
8728d506 | 149 | printf("Number of free blocks: %llu/%llu, Needed: %llu\n", |
4efbac6f VAH |
150 | ext2fs_free_blocks_count(rfs->old_fs->super), |
151 | ext2fs_free_blocks_count(rfs->new_fs->super), | |
a8519a2d TT |
152 | rfs->needed_blocks); |
153 | #endif | |
efc6f628 | 154 | |
1eb31c48 | 155 | init_resource_track(&rtrack, "block_mover", fs->io); |
a8519a2d TT |
156 | retval = block_mover(rfs); |
157 | if (retval) | |
158 | goto errout; | |
1eb31c48 | 159 | print_resource_track(rfs, &rtrack, fs->io); |
a8519a2d | 160 | |
1eb31c48 | 161 | init_resource_track(&rtrack, "inode_scan_and_fix", fs->io); |
a8519a2d TT |
162 | retval = inode_scan_and_fix(rfs); |
163 | if (retval) | |
164 | goto errout; | |
1eb31c48 | 165 | print_resource_track(rfs, &rtrack, fs->io); |
a8519a2d | 166 | |
1eb31c48 | 167 | init_resource_track(&rtrack, "inode_ref_fix", fs->io); |
a8519a2d TT |
168 | retval = inode_ref_fix(rfs); |
169 | if (retval) | |
170 | goto errout; | |
1eb31c48 | 171 | print_resource_track(rfs, &rtrack, fs->io); |
a8519a2d | 172 | |
1eb31c48 | 173 | init_resource_track(&rtrack, "move_itables", fs->io); |
a8519a2d TT |
174 | retval = move_itables(rfs); |
175 | if (retval) | |
176 | goto errout; | |
1eb31c48 | 177 | print_resource_track(rfs, &rtrack, fs->io); |
a8519a2d | 178 | |
1eb31c48 | 179 | init_resource_track(&rtrack, "calculate_summary_stats", fs->io); |
64ad98ac TT |
180 | retval = ext2fs_calculate_summary_stats(rfs->new_fs); |
181 | if (retval) | |
182 | goto errout; | |
1eb31c48 | 183 | print_resource_track(rfs, &rtrack, fs->io); |
efc6f628 | 184 | |
1eb31c48 | 185 | init_resource_track(&rtrack, "fix_resize_inode", fs->io); |
9213a93b TT |
186 | retval = fix_resize_inode(rfs->new_fs); |
187 | if (retval) | |
188 | goto errout; | |
1eb31c48 | 189 | print_resource_track(rfs, &rtrack, fs->io); |
9213a93b | 190 | |
1eb31c48 | 191 | init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io); |
8a6ede8b ES |
192 | retval = fix_sb_journal_backup(rfs->new_fs); |
193 | if (retval) | |
194 | goto errout; | |
1eb31c48 | 195 | print_resource_track(rfs, &rtrack, fs->io); |
8a6ede8b | 196 | |
65c6c3e0 TT |
197 | retval = clear_sparse_super2_last_group(rfs); |
198 | if (retval) | |
199 | goto errout; | |
200 | ||
96cdb37e | 201 | rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS; |
efc6f628 | 202 | rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
1eb31c48 TT |
203 | |
204 | print_resource_track(rfs, &overall_track, fs->io); | |
47fee2ef | 205 | retval = ext2fs_close_free(&rfs->new_fs); |
a8519a2d TT |
206 | if (retval) |
207 | goto errout; | |
208 | ||
209 | rfs->flags = flags; | |
efc6f628 | 210 | |
a8519a2d | 211 | ext2fs_free(rfs->old_fs); |
585bca68 | 212 | rfs->old_fs = NULL; |
a8519a2d | 213 | if (rfs->itable_buf) |
c4e3d3f3 | 214 | ext2fs_free_mem(&rfs->itable_buf); |
e2ca097f TT |
215 | if (rfs->reserve_blocks) |
216 | ext2fs_free_block_bitmap(rfs->reserve_blocks); | |
217 | if (rfs->move_blocks) | |
218 | ext2fs_free_block_bitmap(rfs->move_blocks); | |
c4e3d3f3 | 219 | ext2fs_free_mem(&rfs); |
efc6f628 | 220 | |
a8519a2d TT |
221 | return 0; |
222 | ||
223 | errout: | |
585bca68 | 224 | if (rfs->new_fs) { |
a8519a2d | 225 | ext2fs_free(rfs->new_fs); |
585bca68 LC |
226 | rfs->new_fs = NULL; |
227 | } | |
a8519a2d | 228 | if (rfs->itable_buf) |
c4e3d3f3 TT |
229 | ext2fs_free_mem(&rfs->itable_buf); |
230 | ext2fs_free_mem(&rfs); | |
a8519a2d TT |
231 | return retval; |
232 | } | |
233 | ||
86acdebd TT |
234 | /* |
235 | * Clean up the bitmaps for unitialized bitmaps | |
236 | */ | |
237 | static void fix_uninit_block_bitmaps(ext2_filsys fs) | |
238 | { | |
a0ba54ec | 239 | blk64_t blk, lblk; |
86acdebd | 240 | dgrp_t g; |
a0ba54ec | 241 | int i; |
86acdebd TT |
242 | |
243 | if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super, | |
244 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM))) | |
245 | return; | |
246 | ||
247 | for (g=0; g < fs->group_desc_count; g++) { | |
cd65a24e | 248 | if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT))) |
86acdebd TT |
249 | continue; |
250 | ||
5d3a88fb | 251 | blk = ext2fs_group_first_block2(fs, g); |
a0ba54ec TT |
252 | lblk = ext2fs_group_last_block2(fs, g); |
253 | ext2fs_unmark_block_bitmap_range2(fs->block_map, blk, | |
254 | lblk - blk + 1); | |
255 | ||
256 | ext2fs_reserve_super_and_bgd(fs, g, fs->block_map); | |
257 | ext2fs_mark_block_bitmap2(fs->block_map, | |
258 | ext2fs_block_bitmap_loc(fs, g)); | |
259 | ext2fs_mark_block_bitmap2(fs->block_map, | |
260 | ext2fs_inode_bitmap_loc(fs, g)); | |
261 | for (i = 0, blk = ext2fs_inode_table_loc(fs, g); | |
262 | i < (unsigned int) fs->inode_blocks_per_group; | |
263 | i++, blk++) | |
264 | ext2fs_mark_block_bitmap2(fs->block_map, blk); | |
86acdebd TT |
265 | } |
266 | } | |
267 | ||
a8519a2d TT |
268 | /* -------------------------------------------------------------------- |
269 | * | |
270 | * Resize processing, phase 1. | |
271 | * | |
272 | * In this phase we adjust the in-memory superblock information, and | |
273 | * initialize any new parts of the inode table. The new parts of the | |
274 | * inode table are created in virgin disk space, so we can abort here | |
275 | * without any side effects. | |
276 | * -------------------------------------------------------------------- | |
277 | */ | |
278 | ||
9227c5bb TT |
279 | /* |
280 | * If the group descriptor's bitmap and inode table blocks are valid, | |
c09043f1 TT |
281 | * release them in the new filesystem data structure, and mark them as |
282 | * reserved so the old inode table blocks don't get overwritten. | |
9227c5bb | 283 | */ |
01d6aa9d DW |
284 | static errcode_t free_gdp_blocks(ext2_filsys fs, |
285 | ext2fs_block_bitmap reserve_blocks, | |
286 | ext2_filsys old_fs, | |
287 | dgrp_t group) | |
9227c5bb | 288 | { |
0851517d | 289 | blk64_t blk; |
9227c5bb | 290 | int j; |
01d6aa9d DW |
291 | dgrp_t i; |
292 | ext2fs_block_bitmap bg_map = NULL; | |
293 | errcode_t retval = 0; | |
294 | dgrp_t count = old_fs->group_desc_count - fs->group_desc_count; | |
295 | ||
296 | /* If bigalloc, don't free metadata living in the same cluster */ | |
297 | if (EXT2FS_CLUSTER_RATIO(fs) > 1) { | |
298 | retval = ext2fs_allocate_block_bitmap(fs, "bgdata", &bg_map); | |
299 | if (retval) | |
300 | goto out; | |
9227c5bb | 301 | |
01d6aa9d DW |
302 | retval = mark_table_blocks(fs, bg_map); |
303 | if (retval) | |
304 | goto out; | |
c09043f1 | 305 | } |
9227c5bb | 306 | |
01d6aa9d DW |
307 | for (i = group; i < group + count; i++) { |
308 | blk = ext2fs_block_bitmap_loc(old_fs, i); | |
309 | if (blk && | |
310 | (blk < ext2fs_blocks_count(fs->super)) && | |
311 | !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) { | |
312 | ext2fs_block_alloc_stats2(fs, blk, -1); | |
313 | ext2fs_mark_block_bitmap2(reserve_blocks, blk); | |
314 | } | |
9227c5bb | 315 | |
01d6aa9d DW |
316 | blk = ext2fs_inode_bitmap_loc(old_fs, i); |
317 | if (blk && | |
318 | (blk < ext2fs_blocks_count(fs->super)) && | |
319 | !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) { | |
320 | ext2fs_block_alloc_stats2(fs, blk, -1); | |
321 | ext2fs_mark_block_bitmap2(reserve_blocks, blk); | |
322 | } | |
9227c5bb | 323 | |
01d6aa9d DW |
324 | blk = ext2fs_inode_table_loc(old_fs, i); |
325 | for (j = 0; | |
326 | j < fs->inode_blocks_per_group; j++, blk++) { | |
327 | if (blk >= ext2fs_blocks_count(fs->super) || | |
328 | (bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) | |
329 | continue; | |
330 | ext2fs_block_alloc_stats2(fs, blk, -1); | |
331 | ext2fs_mark_block_bitmap2(reserve_blocks, blk); | |
332 | } | |
9227c5bb | 333 | } |
01d6aa9d DW |
334 | |
335 | out: | |
336 | if (bg_map) | |
337 | ext2fs_free_block_bitmap(bg_map); | |
338 | return retval; | |
9227c5bb TT |
339 | } |
340 | ||
24b2c7a7 | 341 | /* |
bf69235a TT |
342 | * This routine is shared by the online and offline resize routines. |
343 | * All of the information which is adjusted in memory is done here. | |
24b2c7a7 | 344 | */ |
c09043f1 | 345 | errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, |
8728d506 | 346 | ext2fs_block_bitmap reserve_blocks, blk64_t new_size) |
24b2c7a7 | 347 | { |
24b2c7a7 | 348 | errcode_t retval; |
8728d506 VAH |
349 | blk64_t overhead = 0; |
350 | blk64_t rem; | |
351 | blk64_t blk, group_block; | |
2696f250 | 352 | blk64_t real_end; |
5e27a274 TT |
353 | blk64_t old_numblocks, numblocks, adjblocks; |
354 | unsigned long i, j, old_desc_blocks; | |
54434927 | 355 | unsigned int meta_bg, meta_bg_size; |
74128f8d | 356 | int has_super, csum_flag; |
de8f3a76 | 357 | unsigned long long new_inodes; /* u64 to check for overflow */ |
9ff8ece5 | 358 | double percent; |
bf69235a | 359 | |
4efbac6f | 360 | ext2fs_blocks_count_set(fs->super, new_size); |
24b2c7a7 TT |
361 | |
362 | retry: | |
4efbac6f | 363 | fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) - |
69022e02 TT |
364 | fs->super->s_first_data_block, |
365 | EXT2_BLOCKS_PER_GROUP(fs->super)); | |
24b2c7a7 TT |
366 | if (fs->group_desc_count == 0) |
367 | return EXT2_ET_TOOSMALL; | |
efc6f628 | 368 | fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count, |
69022e02 | 369 | EXT2_DESC_PER_BLOCK(fs->super)); |
24b2c7a7 TT |
370 | |
371 | /* | |
372 | * Overhead is the number of bookkeeping blocks per group. It | |
373 | * includes the superblock backup, the group descriptor | |
374 | * backups, the inode bitmap, the block bitmap, and the inode | |
375 | * table. | |
24b2c7a7 | 376 | */ |
9213a93b TT |
377 | overhead = (int) (2 + fs->inode_blocks_per_group); |
378 | ||
379 | if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1)) | |
efc6f628 | 380 | overhead += 1 + fs->desc_blocks + |
9213a93b TT |
381 | fs->super->s_reserved_gdt_blocks; |
382 | ||
24b2c7a7 TT |
383 | /* |
384 | * See if the last group is big enough to support the | |
385 | * necessary data structures. If not, we need to get rid of | |
386 | * it. | |
387 | */ | |
4efbac6f | 388 | rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) % |
24b2c7a7 TT |
389 | fs->super->s_blocks_per_group; |
390 | if ((fs->group_desc_count == 1) && rem && (rem < overhead)) | |
391 | return EXT2_ET_TOOSMALL; | |
515e555a | 392 | if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) { |
4efbac6f VAH |
393 | ext2fs_blocks_count_set(fs->super, |
394 | ext2fs_blocks_count(fs->super) - rem); | |
24b2c7a7 TT |
395 | goto retry; |
396 | } | |
397 | /* | |
398 | * Adjust the number of inodes | |
399 | */ | |
de8f3a76 | 400 | new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count; |
f3358643 ES |
401 | if (new_inodes > ~0U) { |
402 | fprintf(stderr, _("inodes (%llu) must be less than %u"), | |
403 | new_inodes, ~0U); | |
404 | return EXT2_ET_TOO_MANY_INODES; | |
405 | } | |
24b2c7a7 TT |
406 | fs->super->s_inodes_count = fs->super->s_inodes_per_group * |
407 | fs->group_desc_count; | |
408 | ||
409 | /* | |
410 | * Adjust the number of free blocks | |
411 | */ | |
4efbac6f VAH |
412 | blk = ext2fs_blocks_count(old_fs->super); |
413 | if (blk > ext2fs_blocks_count(fs->super)) | |
414 | ext2fs_free_blocks_count_set(fs->super, | |
415 | ext2fs_free_blocks_count(fs->super) - | |
416 | (blk - ext2fs_blocks_count(fs->super))); | |
24b2c7a7 | 417 | else |
4efbac6f VAH |
418 | ext2fs_free_blocks_count_set(fs->super, |
419 | ext2fs_free_blocks_count(fs->super) + | |
420 | (ext2fs_blocks_count(fs->super) - blk)); | |
24b2c7a7 | 421 | |
c762c8e6 TT |
422 | /* |
423 | * Adjust the number of reserved blocks | |
424 | */ | |
4efbac6f VAH |
425 | percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) / |
426 | ext2fs_blocks_count(old_fs->super); | |
427 | ext2fs_r_blocks_count_set(fs->super, | |
428 | (percent * ext2fs_blocks_count(fs->super) / | |
429 | 100.0)); | |
c762c8e6 | 430 | |
24b2c7a7 TT |
431 | /* |
432 | * Adjust the bitmaps for size | |
433 | */ | |
3346084b | 434 | retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count, |
24b2c7a7 TT |
435 | fs->super->s_inodes_count, |
436 | fs->inode_map); | |
c762c8e6 | 437 | if (retval) goto errout; |
efc6f628 | 438 | |
1e33a8b4 | 439 | real_end = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count) - 1 + |
2696f250 | 440 | fs->super->s_first_data_block; |
f026f1a3 TT |
441 | retval = ext2fs_resize_block_bitmap2(new_size - 1, |
442 | real_end, fs->block_map); | |
c762c8e6 | 443 | if (retval) goto errout; |
24b2c7a7 | 444 | |
f026f1a3 TT |
445 | /* |
446 | * If we are growing the file system, also grow the size of | |
447 | * the reserve_blocks bitmap | |
448 | */ | |
449 | if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) { | |
450 | retval = ext2fs_resize_block_bitmap2(new_size - 1, | |
451 | real_end, reserve_blocks); | |
452 | if (retval) goto errout; | |
453 | } | |
454 | ||
24b2c7a7 TT |
455 | /* |
456 | * Reallocate the group descriptors as necessary. | |
457 | */ | |
bf69235a TT |
458 | if (old_fs->desc_blocks != fs->desc_blocks) { |
459 | retval = ext2fs_resize_mem(old_fs->desc_blocks * | |
76f875da TT |
460 | fs->blocksize, |
461 | fs->desc_blocks * fs->blocksize, | |
c4e3d3f3 | 462 | &fs->group_desc); |
ca8abba7 | 463 | if (retval) |
a8519a2d | 464 | goto errout; |
efc6f628 TT |
465 | if (fs->desc_blocks > old_fs->desc_blocks) |
466 | memset((char *) fs->group_desc + | |
bf69235a TT |
467 | (old_fs->desc_blocks * fs->blocksize), 0, |
468 | (fs->desc_blocks - old_fs->desc_blocks) * | |
2787276e | 469 | fs->blocksize); |
24b2c7a7 | 470 | } |
1e1da29f | 471 | |
9213a93b TT |
472 | /* |
473 | * If the resize_inode feature is set, and we are changing the | |
474 | * number of descriptor blocks, then adjust | |
475 | * s_reserved_gdt_blocks if possible to avoid needing to move | |
476 | * the inode table either now or in the future. | |
477 | */ | |
efc6f628 | 478 | if ((fs->super->s_feature_compat & |
9213a93b | 479 | EXT2_FEATURE_COMPAT_RESIZE_INODE) && |
bf69235a | 480 | (old_fs->desc_blocks != fs->desc_blocks)) { |
9213a93b TT |
481 | int new; |
482 | ||
efc6f628 | 483 | new = ((int) fs->super->s_reserved_gdt_blocks) + |
bf69235a | 484 | (old_fs->desc_blocks - fs->desc_blocks); |
9213a93b TT |
485 | if (new < 0) |
486 | new = 0; | |
de8f3a76 | 487 | if (new > (int) fs->blocksize/4) |
9213a93b TT |
488 | new = fs->blocksize/4; |
489 | fs->super->s_reserved_gdt_blocks = new; | |
9213a93b TT |
490 | } |
491 | ||
c82815e5 TT |
492 | if ((fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) && |
493 | (fs->super->s_first_meta_bg > fs->desc_blocks)) { | |
494 | fs->super->s_feature_incompat &= | |
495 | ~EXT2_FEATURE_INCOMPAT_META_BG; | |
496 | fs->super->s_first_meta_bg = 0; | |
497 | } | |
498 | ||
65c6c3e0 TT |
499 | /* |
500 | * Update the location of the backup superblocks if the | |
501 | * sparse_super2 feature is enabled. | |
502 | */ | |
503 | if (fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) { | |
504 | dgrp_t last_bg = fs->group_desc_count - 1; | |
505 | dgrp_t old_last_bg = old_fs->group_desc_count - 1; | |
506 | ||
507 | if (last_bg > old_last_bg) { | |
508 | if (old_fs->group_desc_count == 1) | |
509 | fs->super->s_backup_bgs[0] = 1; | |
510 | if (old_fs->group_desc_count == 1 && | |
511 | fs->super->s_backup_bgs[0]) | |
512 | fs->super->s_backup_bgs[0] = last_bg; | |
513 | else if (fs->super->s_backup_bgs[1]) | |
514 | fs->super->s_backup_bgs[1] = last_bg; | |
515 | } else if (last_bg < old_last_bg) { | |
516 | if (fs->super->s_backup_bgs[0] > last_bg) | |
517 | fs->super->s_backup_bgs[0] = 0; | |
518 | if (fs->super->s_backup_bgs[1] > last_bg) | |
519 | fs->super->s_backup_bgs[1] = 0; | |
520 | if (last_bg > 1 && | |
521 | old_fs->super->s_backup_bgs[1] == old_last_bg) | |
522 | fs->super->s_backup_bgs[1] = last_bg; | |
523 | } | |
524 | } | |
525 | ||
a8519a2d | 526 | /* |
c09043f1 TT |
527 | * If we are shrinking the number of block groups, we're done |
528 | * and can exit now. | |
1e1da29f | 529 | */ |
bf69235a | 530 | if (old_fs->group_desc_count > fs->group_desc_count) { |
9227c5bb TT |
531 | /* |
532 | * Check the block groups that we are chopping off | |
533 | * and free any blocks associated with their metadata | |
534 | */ | |
01d6aa9d DW |
535 | retval = free_gdp_blocks(fs, reserve_blocks, old_fs, |
536 | fs->group_desc_count); | |
c762c8e6 TT |
537 | goto errout; |
538 | } | |
bf69235a | 539 | |
a8519a2d TT |
540 | /* |
541 | * Fix the count of the last (old) block group | |
542 | */ | |
4efbac6f | 543 | old_numblocks = (ext2fs_blocks_count(old_fs->super) - |
bf69235a TT |
544 | old_fs->super->s_first_data_block) % |
545 | old_fs->super->s_blocks_per_group; | |
1e1da29f | 546 | if (!old_numblocks) |
bf69235a TT |
547 | old_numblocks = old_fs->super->s_blocks_per_group; |
548 | if (old_fs->group_desc_count == fs->group_desc_count) { | |
4efbac6f | 549 | numblocks = (ext2fs_blocks_count(fs->super) - |
bf69235a TT |
550 | fs->super->s_first_data_block) % |
551 | fs->super->s_blocks_per_group; | |
1e1da29f | 552 | if (!numblocks) |
bf69235a | 553 | numblocks = fs->super->s_blocks_per_group; |
1e1da29f | 554 | } else |
bf69235a TT |
555 | numblocks = fs->super->s_blocks_per_group; |
556 | i = old_fs->group_desc_count - 1; | |
d7cca6b0 | 557 | ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks)); |
236efede JS |
558 | ext2fs_group_desc_csum_set(fs, i); |
559 | ||
1e1da29f | 560 | /* |
a8519a2d TT |
561 | * If the number of block groups is staying the same, we're |
562 | * done and can exit now. (If the number block groups is | |
563 | * shrinking, we had exited earlier.) | |
1e1da29f | 564 | */ |
bf69235a | 565 | if (old_fs->group_desc_count >= fs->group_desc_count) { |
c762c8e6 TT |
566 | retval = 0; |
567 | goto errout; | |
568 | } | |
bf69235a | 569 | |
a8519a2d TT |
570 | /* |
571 | * Initialize the new block group descriptors | |
572 | */ | |
027b0577 TT |
573 | group_block = ext2fs_group_first_block2(fs, |
574 | old_fs->group_desc_count); | |
74128f8d TT |
575 | csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
576 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM); | |
2d8c0c8a TT |
577 | if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0) |
578 | lazy_itable_init = 1; | |
76dd5e5c TT |
579 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
580 | old_desc_blocks = fs->super->s_first_meta_bg; | |
581 | else | |
efc6f628 | 582 | old_desc_blocks = fs->desc_blocks + |
9213a93b | 583 | fs->super->s_reserved_gdt_blocks; |
f026f1a3 TT |
584 | |
585 | /* | |
586 | * If we changed the number of block_group descriptor blocks, | |
587 | * we need to make sure they are all marked as reserved in the | |
588 | * file systems's block allocation map. | |
589 | */ | |
590 | for (i = 0; i < old_fs->group_desc_count; i++) | |
591 | ext2fs_reserve_super_and_bgd(fs, i, fs->block_map); | |
592 | ||
bf69235a | 593 | for (i = old_fs->group_desc_count; |
1e1da29f | 594 | i < fs->group_desc_count; i++) { |
d7cca6b0 | 595 | memset(ext2fs_group_desc(fs, fs->group_desc, i), 0, |
1e1da29f TT |
596 | sizeof(struct ext2_group_desc)); |
597 | adjblocks = 0; | |
598 | ||
e633b58a | 599 | ext2fs_bg_flags_zap(fs, i); |
2d8c0c8a TT |
600 | if (csum_flag) { |
601 | ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT); | |
602 | if (!lazy_itable_init) | |
603 | ext2fs_bg_flags_set(fs, i, | |
604 | EXT2_BG_INODE_ZEROED); | |
605 | ext2fs_bg_itable_unused_set(fs, i, | |
606 | fs->super->s_inodes_per_group); | |
607 | } | |
98f45471 ES |
608 | |
609 | numblocks = ext2fs_group_blocks_count(fs, i); | |
610 | if ((i < fs->group_desc_count - 1) && csum_flag) | |
611 | ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT); | |
1e1da29f | 612 | |
76dd5e5c TT |
613 | has_super = ext2fs_bg_has_super(fs, i); |
614 | if (has_super) { | |
48f23054 | 615 | ext2fs_block_alloc_stats2(fs, group_block, +1); |
76dd5e5c TT |
616 | adjblocks++; |
617 | } | |
f2de1d38 | 618 | meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super); |
76dd5e5c TT |
619 | meta_bg = i / meta_bg_size; |
620 | if (!(fs->super->s_feature_incompat & | |
621 | EXT2_FEATURE_INCOMPAT_META_BG) || | |
622 | (meta_bg < fs->super->s_first_meta_bg)) { | |
623 | if (has_super) { | |
624 | for (j=0; j < old_desc_blocks; j++) | |
48f23054 | 625 | ext2fs_block_alloc_stats2(fs, |
74128f8d | 626 | group_block + 1 + j, +1); |
76dd5e5c TT |
627 | adjblocks += old_desc_blocks; |
628 | } | |
629 | } else { | |
630 | if (has_super) | |
631 | has_super = 1; | |
632 | if (((i % meta_bg_size) == 0) || | |
633 | ((i % meta_bg_size) == 1) || | |
634 | ((i % meta_bg_size) == (meta_bg_size-1))) | |
48f23054 | 635 | ext2fs_block_alloc_stats2(fs, |
74128f8d | 636 | group_block + has_super, +1); |
24b2c7a7 | 637 | } |
efc6f628 | 638 | |
1e1da29f | 639 | adjblocks += 2 + fs->inode_blocks_per_group; |
efc6f628 | 640 | |
1e1da29f | 641 | numblocks -= adjblocks; |
4efbac6f VAH |
642 | ext2fs_free_blocks_count_set(fs->super, |
643 | ext2fs_free_blocks_count(fs->super) - adjblocks); | |
1e1da29f TT |
644 | fs->super->s_free_inodes_count += |
645 | fs->super->s_inodes_per_group; | |
d7cca6b0 VAH |
646 | ext2fs_bg_free_blocks_count_set(fs, i, numblocks); |
647 | ext2fs_bg_free_inodes_count_set(fs, i, | |
648 | fs->super->s_inodes_per_group); | |
649 | ext2fs_bg_used_dirs_count_set(fs, i, 0); | |
236efede | 650 | ext2fs_group_desc_csum_set(fs, i); |
24b2c7a7 | 651 | |
1e1da29f | 652 | retval = ext2fs_allocate_group_table(fs, i, 0); |
c762c8e6 | 653 | if (retval) goto errout; |
24b2c7a7 | 654 | |
bf69235a TT |
655 | group_block += fs->super->s_blocks_per_group; |
656 | } | |
657 | retval = 0; | |
658 | ||
f026f1a3 TT |
659 | /* |
660 | * Mark all of the metadata blocks as reserved so they won't | |
661 | * get allocated by the call to ext2fs_allocate_group_table() | |
662 | * in blocks_to_move(), where we allocate new blocks to | |
663 | * replace those allocation bitmap and inode table blocks | |
664 | * which have to get relocated to make space for an increased | |
665 | * number of the block group descriptors. | |
666 | */ | |
667 | if (reserve_blocks) | |
668 | mark_table_blocks(fs, reserve_blocks); | |
669 | ||
bf69235a TT |
670 | errout: |
671 | return (retval); | |
672 | } | |
673 | ||
674 | /* | |
675 | * This routine adjusts the superblock and other data structures, both | |
676 | * in disk as well as in memory... | |
677 | */ | |
8728d506 | 678 | static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size) |
bf69235a | 679 | { |
65c6c3e0 | 680 | ext2_filsys fs = rfs->new_fs; |
bf69235a TT |
681 | int adj = 0; |
682 | errcode_t retval; | |
8728d506 | 683 | blk64_t group_block; |
bf69235a TT |
684 | unsigned long i; |
685 | unsigned long max_group; | |
efc6f628 | 686 | |
bf69235a TT |
687 | ext2fs_mark_super_dirty(fs); |
688 | ext2fs_mark_bb_dirty(fs); | |
689 | ext2fs_mark_ib_dirty(fs); | |
690 | ||
c09043f1 TT |
691 | retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"), |
692 | &rfs->reserve_blocks); | |
693 | if (retval) | |
694 | return retval; | |
695 | ||
696 | retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size); | |
bf69235a TT |
697 | if (retval) |
698 | goto errout; | |
699 | ||
700 | /* | |
701 | * Check to make sure there are enough inodes | |
702 | */ | |
703 | if ((rfs->old_fs->super->s_inodes_count - | |
704 | rfs->old_fs->super->s_free_inodes_count) > | |
705 | rfs->new_fs->super->s_inodes_count) { | |
706 | retval = ENOSPC; | |
707 | goto errout; | |
708 | } | |
709 | ||
710 | /* | |
711 | * If we are shrinking the number block groups, we're done and | |
712 | * can exit now. | |
713 | */ | |
714 | if (rfs->old_fs->group_desc_count > fs->group_desc_count) { | |
715 | retval = 0; | |
716 | goto errout; | |
717 | } | |
718 | ||
719 | /* | |
720 | * If the number of block groups is staying the same, we're | |
721 | * done and can exit now. (If the number block groups is | |
722 | * shrinking, we had exited earlier.) | |
723 | */ | |
724 | if (rfs->old_fs->group_desc_count >= fs->group_desc_count) { | |
725 | retval = 0; | |
726 | goto errout; | |
727 | } | |
728 | ||
729 | /* | |
2d8c0c8a TT |
730 | * If we are using uninit_bg (aka GDT_CSUM) and the kernel |
731 | * supports lazy inode initialization, we can skip | |
732 | * initializing the inode table. | |
733 | */ | |
734 | if (lazy_itable_init && | |
735 | EXT2_HAS_RO_COMPAT_FEATURE(fs->super, | |
736 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { | |
737 | retval = 0; | |
738 | goto errout; | |
739 | } | |
740 | ||
741 | /* | |
742 | * Initialize the inode table | |
bf69235a | 743 | */ |
e5aace90 | 744 | retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group, |
bf69235a TT |
745 | &rfs->itable_buf); |
746 | if (retval) | |
747 | goto errout; | |
748 | ||
749 | memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group); | |
027b0577 TT |
750 | group_block = ext2fs_group_first_block2(fs, |
751 | rfs->old_fs->group_desc_count); | |
bf69235a TT |
752 | adj = rfs->old_fs->group_desc_count; |
753 | max_group = fs->group_desc_count - adj; | |
754 | if (rfs->progress) { | |
755 | retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS, | |
756 | 0, max_group); | |
757 | if (retval) | |
758 | goto errout; | |
759 | } | |
760 | for (i = rfs->old_fs->group_desc_count; | |
761 | i < fs->group_desc_count; i++) { | |
05e112a1 TT |
762 | /* |
763 | * Write out the new inode table | |
764 | */ | |
24a117ab | 765 | retval = io_channel_write_blk64(fs->io, |
d7cca6b0 | 766 | ext2fs_inode_table_loc(fs, i), |
24a117ab VAH |
767 | fs->inode_blocks_per_group, |
768 | rfs->itable_buf); | |
c762c8e6 TT |
769 | if (retval) goto errout; |
770 | ||
a8519a2d | 771 | io_channel_flush(fs->io); |
3b627e8d TT |
772 | if (rfs->progress) { |
773 | retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS, | |
774 | i - adj + 1, max_group); | |
775 | if (retval) | |
776 | goto errout; | |
777 | } | |
1e1da29f | 778 | group_block += fs->super->s_blocks_per_group; |
24b2c7a7 | 779 | } |
c762c8e6 TT |
780 | io_channel_flush(fs->io); |
781 | retval = 0; | |
782 | ||
783 | errout: | |
c762c8e6 TT |
784 | return retval; |
785 | } | |
786 | ||
a8519a2d TT |
787 | /* -------------------------------------------------------------------- |
788 | * | |
789 | * Resize processing, phase 2. | |
790 | * | |
791 | * In this phase we adjust determine which blocks need to be moved, in | |
792 | * blocks_to_move(). We then copy the blocks to their ultimate new | |
793 | * destinations using block_mover(). Since we are copying blocks to | |
794 | * their new locations, again during this pass we can abort without | |
795 | * any problems. | |
796 | * -------------------------------------------------------------------- | |
797 | */ | |
798 | ||
c762c8e6 TT |
799 | /* |
800 | * This helper function creates a block bitmap with all of the | |
801 | * filesystem meta-data blocks. | |
802 | */ | |
803 | static errcode_t mark_table_blocks(ext2_filsys fs, | |
64ad98ac | 804 | ext2fs_block_bitmap bmap) |
c762c8e6 | 805 | { |
54434927 | 806 | dgrp_t i; |
f026f1a3 | 807 | blk64_t blk; |
c762c8e6 | 808 | |
c762c8e6 | 809 | for (i = 0; i < fs->group_desc_count; i++) { |
64ad98ac | 810 | ext2fs_reserve_super_and_bgd(fs, i, bmap); |
efc6f628 | 811 | |
c762c8e6 TT |
812 | /* |
813 | * Mark the blocks used for the inode table | |
814 | */ | |
f026f1a3 TT |
815 | blk = ext2fs_inode_table_loc(fs, i); |
816 | if (blk) | |
817 | ext2fs_mark_block_bitmap_range2(bmap, blk, | |
818 | fs->inode_blocks_per_group); | |
efc6f628 | 819 | |
c762c8e6 | 820 | /* |
efc6f628 | 821 | * Mark block used for the block bitmap |
c762c8e6 | 822 | */ |
f026f1a3 TT |
823 | blk = ext2fs_block_bitmap_loc(fs, i); |
824 | if (blk) | |
825 | ext2fs_mark_block_bitmap2(bmap, blk); | |
64ad98ac | 826 | |
c762c8e6 | 827 | /* |
efc6f628 | 828 | * Mark block used for the inode bitmap |
c762c8e6 | 829 | */ |
f026f1a3 TT |
830 | blk = ext2fs_inode_bitmap_loc(fs, i); |
831 | if (blk) | |
832 | ext2fs_mark_block_bitmap2(bmap, blk); | |
c762c8e6 | 833 | } |
1e1da29f | 834 | return 0; |
24b2c7a7 TT |
835 | } |
836 | ||
76dd5e5c TT |
837 | /* |
838 | * This function checks to see if a particular block (either a | |
839 | * superblock or a block group descriptor) overlaps with an inode or | |
840 | * block bitmap block, or with the inode table. | |
841 | */ | |
842 | static void mark_fs_metablock(ext2_resize_t rfs, | |
843 | ext2fs_block_bitmap meta_bmap, | |
8728d506 | 844 | int group, blk64_t blk) |
76dd5e5c TT |
845 | { |
846 | ext2_filsys fs = rfs->new_fs; | |
efc6f628 | 847 | |
3346084b | 848 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk); |
48f23054 | 849 | ext2fs_block_alloc_stats2(fs, blk, +1); |
76dd5e5c TT |
850 | |
851 | /* | |
852 | * Check to see if we overlap with the inode or block bitmap, | |
853 | * or the inode tables. If not, and the block is in use, then | |
854 | * mark it as a block to be moved. | |
855 | */ | |
856 | if (IS_BLOCK_BM(fs, group, blk)) { | |
d7cca6b0 | 857 | ext2fs_block_bitmap_loc_set(fs, group, 0); |
76dd5e5c | 858 | rfs->needed_blocks++; |
ddcf1dbf TT |
859 | return; |
860 | } | |
861 | if (IS_INODE_BM(fs, group, blk)) { | |
d7cca6b0 | 862 | ext2fs_inode_bitmap_loc_set(fs, group, 0); |
76dd5e5c | 863 | rfs->needed_blocks++; |
ddcf1dbf TT |
864 | return; |
865 | } | |
866 | if (IS_INODE_TB(fs, group, blk)) { | |
d7cca6b0 | 867 | ext2fs_inode_table_loc_set(fs, group, 0); |
76dd5e5c | 868 | rfs->needed_blocks++; |
ddcf1dbf TT |
869 | return; |
870 | } | |
871 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) { | |
872 | dgrp_t i; | |
873 | ||
874 | for (i=0; i < rfs->old_fs->group_desc_count; i++) { | |
875 | if (IS_BLOCK_BM(fs, i, blk)) { | |
876 | ext2fs_block_bitmap_loc_set(fs, i, 0); | |
877 | rfs->needed_blocks++; | |
878 | return; | |
879 | } | |
880 | if (IS_INODE_BM(fs, i, blk)) { | |
881 | ext2fs_inode_bitmap_loc_set(fs, i, 0); | |
882 | rfs->needed_blocks++; | |
883 | return; | |
884 | } | |
885 | if (IS_INODE_TB(fs, i, blk)) { | |
886 | ext2fs_inode_table_loc_set(fs, i, 0); | |
887 | rfs->needed_blocks++; | |
888 | return; | |
889 | } | |
890 | } | |
891 | } | |
892 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, | |
893 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && | |
cd65a24e | 894 | (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) { |
86acdebd TT |
895 | /* |
896 | * If the block bitmap is uninitialized, which means | |
897 | * nothing other than standard metadata in use. | |
898 | */ | |
899 | return; | |
3346084b VAH |
900 | } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) && |
901 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) { | |
902 | ext2fs_mark_block_bitmap2(rfs->move_blocks, blk); | |
76dd5e5c TT |
903 | rfs->needed_blocks++; |
904 | } | |
905 | } | |
906 | ||
907 | ||
24b2c7a7 TT |
908 | /* |
909 | * This routine marks and unmarks reserved blocks in the new block | |
910 | * bitmap. It also determines which blocks need to be moved and | |
911 | * places this information into the move_blocks bitmap. | |
912 | */ | |
c762c8e6 | 913 | static errcode_t blocks_to_move(ext2_resize_t rfs) |
24b2c7a7 | 914 | { |
54434927 | 915 | int j, has_super; |
86acdebd | 916 | dgrp_t i, max_groups, g; |
8728d506 | 917 | blk64_t blk, group_blk; |
118d3f0b | 918 | blk64_t old_blocks, new_blocks, group_end, cluster_freed; |
80391dcd | 919 | blk64_t new_size; |
54434927 | 920 | unsigned int meta_bg, meta_bg_size; |
24b2c7a7 | 921 | errcode_t retval; |
c762c8e6 | 922 | ext2_filsys fs, old_fs; |
118d3f0b | 923 | ext2fs_block_bitmap meta_bmap, new_meta_bmap = NULL; |
4b04fb30 | 924 | int flex_bg; |
24b2c7a7 | 925 | |
c762c8e6 TT |
926 | fs = rfs->new_fs; |
927 | old_fs = rfs->old_fs; | |
4efbac6f | 928 | if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super)) |
c762c8e6 | 929 | fs = rfs->old_fs; |
efc6f628 | 930 | |
a13575f4 | 931 | retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"), |
c762c8e6 TT |
932 | &rfs->move_blocks); |
933 | if (retval) | |
934 | return retval; | |
935 | ||
efc6f628 | 936 | retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"), |
64ad98ac TT |
937 | &meta_bmap); |
938 | if (retval) | |
939 | return retval; | |
efc6f628 | 940 | |
64ad98ac | 941 | retval = mark_table_blocks(old_fs, meta_bmap); |
c762c8e6 TT |
942 | if (retval) |
943 | return retval; | |
944 | ||
945 | fs = rfs->new_fs; | |
efc6f628 | 946 | |
80391dcd | 947 | /* |
e9736a3b TT |
948 | * If we're shrinking the filesystem, we need to move any |
949 | * group's metadata blocks (either allocation bitmaps or the | |
950 | * inode table) which are beyond the end of the new | |
951 | * filesystem. | |
80391dcd ES |
952 | */ |
953 | new_size = ext2fs_blocks_count(fs->super); | |
954 | if (new_size < ext2fs_blocks_count(old_fs->super)) { | |
955 | for (g = 0; g < fs->group_desc_count; g++) { | |
e9736a3b | 956 | int realloc = 0; |
80391dcd | 957 | /* |
e9736a3b TT |
958 | * ext2fs_allocate_group_table will re-allocate any |
959 | * metadata blocks whose location is set to zero. | |
80391dcd ES |
960 | */ |
961 | if (ext2fs_block_bitmap_loc(fs, g) >= new_size) { | |
962 | ext2fs_block_bitmap_loc_set(fs, g, 0); | |
e9736a3b | 963 | realloc = 1; |
80391dcd ES |
964 | } |
965 | if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) { | |
966 | ext2fs_inode_bitmap_loc_set(fs, g, 0); | |
e9736a3b TT |
967 | realloc = 1; |
968 | } | |
969 | if ((ext2fs_inode_table_loc(fs, g) + | |
970 | fs->inode_blocks_per_group) > new_size) { | |
971 | ext2fs_inode_table_loc_set(fs, g, 0); | |
972 | realloc = 1; | |
973 | } | |
974 | ||
975 | if (realloc) { | |
80391dcd ES |
976 | retval = ext2fs_allocate_group_table(fs, g, 0); |
977 | if (retval) | |
978 | return retval; | |
979 | } | |
980 | } | |
981 | } | |
982 | ||
1e1da29f TT |
983 | /* |
984 | * If we're shrinking the filesystem, we need to move all of | |
985 | * the blocks that don't fit any more | |
986 | */ | |
4efbac6f VAH |
987 | for (blk = ext2fs_blocks_count(fs->super); |
988 | blk < ext2fs_blocks_count(old_fs->super); blk++) { | |
6493f8e8 | 989 | g = ext2fs_group_of_blk2(fs, blk); |
86acdebd TT |
990 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
991 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && | |
cd65a24e | 992 | ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) { |
86acdebd TT |
993 | /* |
994 | * The block bitmap is uninitialized, so skip | |
995 | * to the next block group. | |
996 | */ | |
5d3a88fb | 997 | blk = ext2fs_group_first_block2(fs, g+1) - 1; |
86acdebd TT |
998 | continue; |
999 | } | |
3346084b VAH |
1000 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && |
1001 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) { | |
1002 | ext2fs_mark_block_bitmap2(rfs->move_blocks, blk); | |
1e1da29f | 1003 | rfs->needed_blocks++; |
c762c8e6 | 1004 | } |
3346084b | 1005 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk); |
1e1da29f | 1006 | } |
efc6f628 | 1007 | |
c82815e5 | 1008 | if (old_fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
76dd5e5c | 1009 | old_blocks = old_fs->super->s_first_meta_bg; |
c82815e5 TT |
1010 | else |
1011 | old_blocks = old_fs->desc_blocks + | |
1012 | old_fs->super->s_reserved_gdt_blocks; | |
1013 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) | |
76dd5e5c | 1014 | new_blocks = fs->super->s_first_meta_bg; |
c82815e5 | 1015 | else |
9213a93b | 1016 | new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks; |
efc6f628 | 1017 | |
65c6c3e0 TT |
1018 | retval = reserve_sparse_super2_last_group(rfs, meta_bmap); |
1019 | if (retval) | |
1020 | goto errout; | |
1021 | ||
c762c8e6 TT |
1022 | if (old_blocks == new_blocks) { |
1023 | retval = 0; | |
1024 | goto errout; | |
1025 | } | |
24b2c7a7 | 1026 | |
1333fe93 TT |
1027 | max_groups = fs->group_desc_count; |
1028 | if (max_groups > old_fs->group_desc_count) | |
1029 | max_groups = old_fs->group_desc_count; | |
c762c8e6 | 1030 | group_blk = old_fs->super->s_first_data_block; |
24b2c7a7 TT |
1031 | /* |
1032 | * If we're reducing the number of descriptor blocks, this | |
1033 | * makes life easy. :-) We just have to mark some extra | |
1034 | * blocks as free. | |
1035 | */ | |
1036 | if (old_blocks > new_blocks) { | |
118d3f0b DW |
1037 | if (EXT2FS_CLUSTER_RATIO(fs) > 1) { |
1038 | retval = ext2fs_allocate_block_bitmap(fs, | |
1039 | _("new meta blocks"), | |
1040 | &new_meta_bmap); | |
1041 | if (retval) | |
1042 | goto errout; | |
1043 | ||
1044 | retval = mark_table_blocks(fs, new_meta_bmap); | |
1045 | if (retval) | |
1046 | goto errout; | |
1047 | } | |
1048 | ||
1333fe93 | 1049 | for (i = 0; i < max_groups; i++) { |
1e1da29f TT |
1050 | if (!ext2fs_bg_has_super(fs, i)) { |
1051 | group_blk += fs->super->s_blocks_per_group; | |
24b2c7a7 TT |
1052 | continue; |
1053 | } | |
118d3f0b DW |
1054 | group_end = group_blk + 1 + old_blocks; |
1055 | for (blk = group_blk + 1 + new_blocks; | |
1056 | blk < group_end;) { | |
1057 | if (new_meta_bmap == NULL || | |
1058 | !ext2fs_test_block_bitmap2(new_meta_bmap, | |
1059 | blk)) { | |
1060 | cluster_freed = | |
1061 | EXT2FS_CLUSTER_RATIO(fs) - | |
1062 | (blk & | |
1063 | EXT2FS_CLUSTER_MASK(fs)); | |
1064 | if (cluster_freed > group_end - blk) | |
1065 | cluster_freed = group_end - blk; | |
1066 | ext2fs_block_alloc_stats2(fs, blk, -1); | |
1067 | blk += EXT2FS_CLUSTER_RATIO(fs); | |
1068 | rfs->needed_blocks -= cluster_freed; | |
1069 | continue; | |
1070 | } | |
052db4b7 | 1071 | rfs->needed_blocks--; |
118d3f0b | 1072 | blk++; |
052db4b7 | 1073 | } |
1e1da29f | 1074 | group_blk += fs->super->s_blocks_per_group; |
24b2c7a7 | 1075 | } |
c762c8e6 TT |
1076 | retval = 0; |
1077 | goto errout; | |
24b2c7a7 TT |
1078 | } |
1079 | /* | |
1080 | * If we're increasing the number of descriptor blocks, life | |
efc6f628 | 1081 | * gets interesting.... |
24b2c7a7 | 1082 | */ |
f2de1d38 | 1083 | meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super); |
4b04fb30 TT |
1084 | flex_bg = fs->super->s_feature_incompat & |
1085 | EXT4_FEATURE_INCOMPAT_FLEX_BG; | |
2ebaeb35 | 1086 | /* first reserve all of the existing fs meta blocks */ |
1333fe93 | 1087 | for (i = 0; i < max_groups; i++) { |
76dd5e5c TT |
1088 | has_super = ext2fs_bg_has_super(fs, i); |
1089 | if (has_super) | |
1090 | mark_fs_metablock(rfs, meta_bmap, i, group_blk); | |
1091 | ||
1092 | meta_bg = i / meta_bg_size; | |
1093 | if (!(fs->super->s_feature_incompat & | |
1094 | EXT2_FEATURE_INCOMPAT_META_BG) || | |
1095 | (meta_bg < fs->super->s_first_meta_bg)) { | |
424cb7b6 TT |
1096 | if (has_super) { |
1097 | for (blk = group_blk+1; | |
1098 | blk < group_blk + 1 + new_blocks; blk++) | |
efc6f628 | 1099 | mark_fs_metablock(rfs, meta_bmap, |
424cb7b6 TT |
1100 | i, blk); |
1101 | } | |
76dd5e5c TT |
1102 | } else { |
1103 | if (has_super) | |
1104 | has_super = 1; | |
1105 | if (((i % meta_bg_size) == 0) || | |
1106 | ((i % meta_bg_size) == 1) || | |
1107 | ((i % meta_bg_size) == (meta_bg_size-1))) | |
1108 | mark_fs_metablock(rfs, meta_bmap, i, | |
1109 | group_blk + has_super); | |
24b2c7a7 | 1110 | } |
76dd5e5c | 1111 | |
1e1da29f | 1112 | /* |
c762c8e6 TT |
1113 | * Reserve the existing meta blocks that we know |
1114 | * aren't to be moved. | |
4b04fb30 TT |
1115 | * |
1116 | * For flex_bg file systems, in order to avoid | |
1117 | * overwriting fs metadata (especially inode table | |
1118 | * blocks) belonging to a different block group when | |
1119 | * we are relocating the inode tables, we need to | |
1120 | * reserve all existing fs metadata blocks. | |
1e1da29f | 1121 | */ |
d7cca6b0 | 1122 | if (ext2fs_block_bitmap_loc(fs, i)) |
3346084b | 1123 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, |
d7cca6b0 | 1124 | ext2fs_block_bitmap_loc(fs, i)); |
4b04fb30 TT |
1125 | else if (flex_bg && i < old_fs->group_desc_count) |
1126 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, | |
1127 | ext2fs_block_bitmap_loc(old_fs, i)); | |
1128 | ||
d7cca6b0 | 1129 | if (ext2fs_inode_bitmap_loc(fs, i)) |
3346084b | 1130 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, |
d7cca6b0 | 1131 | ext2fs_inode_bitmap_loc(fs, i)); |
4b04fb30 TT |
1132 | else if (flex_bg && i < old_fs->group_desc_count) |
1133 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, | |
1134 | ext2fs_inode_bitmap_loc(old_fs, i)); | |
1135 | ||
d7cca6b0 | 1136 | if (ext2fs_inode_table_loc(fs, i)) |
a0ba54ec TT |
1137 | ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks, |
1138 | ext2fs_inode_table_loc(fs, i), | |
1139 | fs->inode_blocks_per_group); | |
4b04fb30 | 1140 | else if (flex_bg && i < old_fs->group_desc_count) |
a0ba54ec TT |
1141 | ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks, |
1142 | ext2fs_inode_table_loc(old_fs, i), | |
1143 | old_fs->inode_blocks_per_group); | |
24b2c7a7 | 1144 | |
2ebaeb35 TT |
1145 | group_blk += rfs->new_fs->super->s_blocks_per_group; |
1146 | } | |
1147 | ||
1148 | /* Allocate the missing data structures */ | |
1149 | for (i = 0; i < max_groups; i++) { | |
1150 | if (ext2fs_inode_table_loc(fs, i) && | |
1151 | ext2fs_inode_bitmap_loc(fs, i) && | |
1152 | ext2fs_block_bitmap_loc(fs, i)) | |
1153 | continue; | |
1154 | ||
1e1da29f TT |
1155 | retval = ext2fs_allocate_group_table(fs, i, |
1156 | rfs->reserve_blocks); | |
1157 | if (retval) | |
c762c8e6 | 1158 | goto errout; |
24b2c7a7 | 1159 | |
1e1da29f | 1160 | /* |
c762c8e6 TT |
1161 | * For those structures that have changed, we need to |
1162 | * do bookkeepping. | |
1e1da29f | 1163 | */ |
d7cca6b0 VAH |
1164 | if (ext2fs_block_bitmap_loc(old_fs, i) != |
1165 | (blk = ext2fs_block_bitmap_loc(fs, i))) { | |
48f23054 | 1166 | ext2fs_block_alloc_stats2(fs, blk, +1); |
3346084b VAH |
1167 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && |
1168 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) | |
1169 | ext2fs_mark_block_bitmap2(rfs->move_blocks, | |
c762c8e6 TT |
1170 | blk); |
1171 | } | |
d7cca6b0 VAH |
1172 | if (ext2fs_inode_bitmap_loc(old_fs, i) != |
1173 | (blk = ext2fs_inode_bitmap_loc(fs, i))) { | |
48f23054 | 1174 | ext2fs_block_alloc_stats2(fs, blk, +1); |
3346084b VAH |
1175 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && |
1176 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) | |
1177 | ext2fs_mark_block_bitmap2(rfs->move_blocks, | |
c762c8e6 TT |
1178 | blk); |
1179 | } | |
24b2c7a7 | 1180 | |
052db4b7 TT |
1181 | /* |
1182 | * The inode table, if we need to relocate it, is | |
1183 | * handled specially. We have to reserve the blocks | |
1184 | * for both the old and the new inode table, since we | |
1185 | * can't have the inode table be destroyed during the | |
1186 | * block relocation phase. | |
1187 | */ | |
d7cca6b0 | 1188 | if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i)) |
2ebaeb35 | 1189 | continue; /* inode table not moved */ |
052db4b7 | 1190 | |
c762c8e6 | 1191 | rfs->needed_blocks += fs->inode_blocks_per_group; |
052db4b7 TT |
1192 | |
1193 | /* | |
1194 | * Mark the new inode table as in use in the new block | |
efc6f628 | 1195 | * allocation bitmap, and move any blocks that might |
c762c8e6 | 1196 | * be necessary. |
052db4b7 | 1197 | */ |
d7cca6b0 | 1198 | for (blk = ext2fs_inode_table_loc(fs, i), j=0; |
c762c8e6 | 1199 | j < fs->inode_blocks_per_group ; j++, blk++) { |
48f23054 | 1200 | ext2fs_block_alloc_stats2(fs, blk, +1); |
3346084b VAH |
1201 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && |
1202 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) | |
1203 | ext2fs_mark_block_bitmap2(rfs->move_blocks, | |
c762c8e6 TT |
1204 | blk); |
1205 | } | |
efc6f628 | 1206 | |
1e1da29f | 1207 | /* |
052db4b7 TT |
1208 | * Make sure the old inode table is reserved in the |
1209 | * block reservation bitmap. | |
1e1da29f | 1210 | */ |
d7cca6b0 | 1211 | for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0; |
052db4b7 | 1212 | j < fs->inode_blocks_per_group ; j++, blk++) |
3346084b | 1213 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk); |
1e1da29f | 1214 | } |
c762c8e6 TT |
1215 | retval = 0; |
1216 | ||
1217 | errout: | |
118d3f0b DW |
1218 | if (new_meta_bmap) |
1219 | ext2fs_free_block_bitmap(new_meta_bmap); | |
c762c8e6 TT |
1220 | if (meta_bmap) |
1221 | ext2fs_free_block_bitmap(meta_bmap); | |
efc6f628 | 1222 | |
c762c8e6 | 1223 | return retval; |
1e1da29f | 1224 | } |
24b2c7a7 | 1225 | |
a8519a2d TT |
1226 | /* |
1227 | * This helper function tries to allocate a new block. We try to | |
1228 | * avoid hitting the original group descriptor blocks at least at | |
1229 | * first, since we want to make it possible to recover from a badly | |
1230 | * aborted resize operation as much as possible. | |
1231 | * | |
1232 | * In the future, I may further modify this routine to balance out | |
1233 | * where we get the new blocks across the various block groups. | |
1234 | * Ideally we would allocate blocks that corresponded with the block | |
1235 | * group of the containing inode, and keep contiguous blocks | |
1236 | * together. However, this very difficult to do efficiently, since we | |
1237 | * don't have the necessary information up front. | |
1238 | */ | |
1239 | ||
1240 | #define AVOID_OLD 1 | |
1241 | #define DESPERATION 2 | |
1242 | ||
1243 | static void init_block_alloc(ext2_resize_t rfs) | |
1244 | { | |
1245 | rfs->alloc_state = AVOID_OLD; | |
1246 | rfs->new_blk = rfs->new_fs->super->s_first_data_block; | |
2bc4d4f7 TT |
1247 | #if 0 |
1248 | /* HACK for testing */ | |
4efbac6f VAH |
1249 | if (ext2fs_blocks_count(rfs->new_fs->super) > |
1250 | ext2fs_blocks_count(rfs->old_fs->super)) | |
1251 | rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super); | |
2bc4d4f7 | 1252 | #endif |
a8519a2d TT |
1253 | } |
1254 | ||
8728d506 | 1255 | static blk64_t get_new_block(ext2_resize_t rfs) |
a8519a2d TT |
1256 | { |
1257 | ext2_filsys fs = rfs->new_fs; | |
efc6f628 | 1258 | |
a8519a2d | 1259 | while (1) { |
4efbac6f | 1260 | if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) { |
a8519a2d TT |
1261 | if (rfs->alloc_state == DESPERATION) |
1262 | return 0; | |
1263 | ||
1264 | #ifdef RESIZE2FS_DEBUG | |
1265 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
f35fd3d5 TT |
1266 | printf("Going into desperation mode " |
1267 | "for block allocations\n"); | |
efc6f628 | 1268 | #endif |
a8519a2d TT |
1269 | rfs->alloc_state = DESPERATION; |
1270 | rfs->new_blk = fs->super->s_first_data_block; | |
1271 | continue; | |
1272 | } | |
3346084b VAH |
1273 | if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) || |
1274 | ext2fs_test_block_bitmap2(rfs->reserve_blocks, | |
a8519a2d TT |
1275 | rfs->new_blk) || |
1276 | ((rfs->alloc_state == AVOID_OLD) && | |
4efbac6f | 1277 | (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) && |
3346084b | 1278 | ext2fs_test_block_bitmap2(rfs->old_fs->block_map, |
a8519a2d TT |
1279 | rfs->new_blk))) { |
1280 | rfs->new_blk++; | |
1281 | continue; | |
1282 | } | |
1283 | return rfs->new_blk; | |
1284 | } | |
1285 | } | |
1286 | ||
7f9c96ee TT |
1287 | static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal, |
1288 | blk64_t *ret) | |
1289 | { | |
1290 | ext2_resize_t rfs = (ext2_resize_t) fs->priv_data; | |
8728d506 | 1291 | blk64_t blk; |
7f9c96ee TT |
1292 | |
1293 | blk = get_new_block(rfs); | |
1294 | if (!blk) | |
1295 | return ENOSPC; | |
1296 | ||
1297 | #ifdef RESIZE2FS_DEBUG | |
1298 | if (rfs->flags & 0xF) | |
8728d506 | 1299 | printf("get_alloc_block allocating %llu\n", blk); |
7f9c96ee TT |
1300 | #endif |
1301 | ||
3346084b VAH |
1302 | ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk); |
1303 | ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk); | |
7f9c96ee TT |
1304 | *ret = (blk64_t) blk; |
1305 | return 0; | |
1306 | } | |
1307 | ||
a8519a2d TT |
1308 | static errcode_t block_mover(ext2_resize_t rfs) |
1309 | { | |
8728d506 | 1310 | blk64_t blk, old_blk, new_blk; |
a8519a2d TT |
1311 | ext2_filsys fs = rfs->new_fs; |
1312 | ext2_filsys old_fs = rfs->old_fs; | |
1313 | errcode_t retval; | |
8728d506 VAH |
1314 | __u64 size; |
1315 | int c; | |
a8519a2d | 1316 | int to_move, moved; |
7d7bdd57 TT |
1317 | ext2_badblocks_list badblock_list = 0; |
1318 | int bb_modified = 0; | |
efc6f628 | 1319 | |
7f9c96ee TT |
1320 | fs->get_alloc_block = resize2fs_get_alloc_block; |
1321 | old_fs->get_alloc_block = resize2fs_get_alloc_block; | |
1322 | ||
7d7bdd57 TT |
1323 | retval = ext2fs_read_bb_inode(old_fs, &badblock_list); |
1324 | if (retval) | |
1325 | return retval; | |
a8519a2d TT |
1326 | |
1327 | new_blk = fs->super->s_first_data_block; | |
1328 | if (!rfs->itable_buf) { | |
e5aace90 | 1329 | retval = ext2fs_get_array(fs->blocksize, |
a8519a2d | 1330 | fs->inode_blocks_per_group, |
c4e3d3f3 | 1331 | &rfs->itable_buf); |
a8519a2d TT |
1332 | if (retval) |
1333 | return retval; | |
1334 | } | |
1335 | retval = ext2fs_create_extent_table(&rfs->bmap, 0); | |
1336 | if (retval) | |
1337 | return retval; | |
1338 | ||
1339 | /* | |
1340 | * The first step is to figure out where all of the blocks | |
1341 | * will go. | |
1342 | */ | |
1343 | to_move = moved = 0; | |
1344 | init_block_alloc(rfs); | |
d1a1a583 TT |
1345 | for (blk = B2C(old_fs->super->s_first_data_block); |
1346 | blk < ext2fs_blocks_count(old_fs->super); | |
1347 | blk += EXT2FS_CLUSTER_RATIO(fs)) { | |
3346084b | 1348 | if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk)) |
a8519a2d | 1349 | continue; |
3346084b | 1350 | if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk)) |
a8519a2d | 1351 | continue; |
7d7bdd57 TT |
1352 | if (ext2fs_badblocks_list_test(badblock_list, blk)) { |
1353 | ext2fs_badblocks_list_del(badblock_list, blk); | |
1354 | bb_modified++; | |
1355 | continue; | |
1356 | } | |
a8519a2d TT |
1357 | |
1358 | new_blk = get_new_block(rfs); | |
1359 | if (!new_blk) { | |
1360 | retval = ENOSPC; | |
1361 | goto errout; | |
1362 | } | |
48f23054 | 1363 | ext2fs_block_alloc_stats2(fs, new_blk, +1); |
d1a1a583 | 1364 | ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk)); |
a8519a2d TT |
1365 | to_move++; |
1366 | } | |
efc6f628 | 1367 | |
a8519a2d | 1368 | if (to_move == 0) { |
cefbf487 TT |
1369 | if (rfs->bmap) { |
1370 | ext2fs_free_extent_table(rfs->bmap); | |
1371 | rfs->bmap = 0; | |
1372 | } | |
a8519a2d TT |
1373 | retval = 0; |
1374 | goto errout; | |
1375 | } | |
1376 | ||
1377 | /* | |
1378 | * Step two is to actually move the blocks | |
1379 | */ | |
1380 | retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0); | |
1381 | if (retval) goto errout; | |
1382 | ||
3b627e8d TT |
1383 | if (rfs->progress) { |
1384 | retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS, | |
1385 | 0, to_move); | |
1386 | if (retval) | |
1387 | goto errout; | |
1388 | } | |
a8519a2d TT |
1389 | while (1) { |
1390 | retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size); | |
1391 | if (retval) goto errout; | |
1392 | if (!size) | |
1393 | break; | |
d1a1a583 TT |
1394 | old_blk = C2B(old_blk); |
1395 | new_blk = C2B(new_blk); | |
1396 | size = C2B(size); | |
a8519a2d TT |
1397 | #ifdef RESIZE2FS_DEBUG |
1398 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
8728d506 | 1399 | printf("Moving %llu blocks %llu->%llu\n", |
f35fd3d5 | 1400 | size, old_blk, new_blk); |
a8519a2d TT |
1401 | #endif |
1402 | do { | |
1403 | c = size; | |
1404 | if (c > fs->inode_blocks_per_group) | |
1405 | c = fs->inode_blocks_per_group; | |
24a117ab VAH |
1406 | retval = io_channel_read_blk64(fs->io, old_blk, c, |
1407 | rfs->itable_buf); | |
a8519a2d | 1408 | if (retval) goto errout; |
24a117ab VAH |
1409 | retval = io_channel_write_blk64(fs->io, new_blk, c, |
1410 | rfs->itable_buf); | |
a8519a2d TT |
1411 | if (retval) goto errout; |
1412 | size -= c; | |
1413 | new_blk += c; | |
1414 | old_blk += c; | |
1415 | moved += c; | |
1416 | if (rfs->progress) { | |
1417 | io_channel_flush(fs->io); | |
3b627e8d TT |
1418 | retval = (rfs->progress)(rfs, |
1419 | E2_RSZ_BLOCK_RELOC_PASS, | |
a8519a2d | 1420 | moved, to_move); |
3b627e8d TT |
1421 | if (retval) |
1422 | goto errout; | |
a8519a2d TT |
1423 | } |
1424 | } while (size > 0); | |
1425 | io_channel_flush(fs->io); | |
1426 | } | |
1427 | ||
1428 | errout: | |
7d7bdd57 TT |
1429 | if (badblock_list) { |
1430 | if (!retval && bb_modified) | |
1431 | retval = ext2fs_update_bb_inode(old_fs, | |
1432 | badblock_list); | |
1433 | ext2fs_badblocks_list_free(badblock_list); | |
1434 | } | |
a8519a2d TT |
1435 | return retval; |
1436 | } | |
1437 | ||
1438 | ||
1439 | /* -------------------------------------------------------------------- | |
1440 | * | |
1441 | * Resize processing, phase 3 | |
1442 | * | |
1443 | * -------------------------------------------------------------------- | |
1444 | */ | |
1445 | ||
1446 | ||
d1a1a583 TT |
1447 | /* |
1448 | * The extent translation table is stored in clusters so we need to | |
1449 | * take special care when mapping a source block number to its | |
1450 | * destination block number. | |
1451 | */ | |
f404167d | 1452 | static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc) |
d1a1a583 TT |
1453 | { |
1454 | __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc))); | |
1455 | ||
1456 | if (new_block != 0) | |
1457 | new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1); | |
1458 | return new_block; | |
1459 | } | |
1460 | ||
a8519a2d TT |
1461 | struct process_block_struct { |
1462 | ext2_resize_t rfs; | |
dfcdc32f | 1463 | ext2_ino_t ino; |
a8519a2d TT |
1464 | struct ext2_inode * inode; |
1465 | errcode_t error; | |
1466 | int is_dir; | |
1467 | int changed; | |
1468 | }; | |
1469 | ||
8728d506 | 1470 | static int process_block(ext2_filsys fs, blk64_t *block_nr, |
efc6f628 | 1471 | e2_blkcnt_t blockcnt, |
8728d506 | 1472 | blk64_t ref_block EXT2FS_ATTR((unused)), |
54434927 | 1473 | int ref_offset EXT2FS_ATTR((unused)), void *priv_data) |
a8519a2d TT |
1474 | { |
1475 | struct process_block_struct *pb; | |
1476 | errcode_t retval; | |
8728d506 | 1477 | blk64_t block, new_block; |
a8519a2d TT |
1478 | int ret = 0; |
1479 | ||
1480 | pb = (struct process_block_struct *) priv_data; | |
1481 | block = *block_nr; | |
1482 | if (pb->rfs->bmap) { | |
d1a1a583 | 1483 | new_block = extent_translate(fs, pb->rfs->bmap, block); |
a8519a2d TT |
1484 | if (new_block) { |
1485 | *block_nr = new_block; | |
1486 | ret |= BLOCK_CHANGED; | |
1487 | pb->changed = 1; | |
1488 | #ifdef RESIZE2FS_DEBUG | |
1489 | if (pb->rfs->flags & RESIZE_DEBUG_BMOVE) | |
8728d506 | 1490 | printf("ino=%u, blockcnt=%lld, %llu->%llu\n", |
a8519a2d TT |
1491 | pb->ino, blockcnt, block, new_block); |
1492 | #endif | |
1493 | block = new_block; | |
1494 | } | |
1495 | } | |
1496 | if (pb->is_dir) { | |
8728d506 VAH |
1497 | retval = ext2fs_add_dir_block2(fs->dblist, pb->ino, |
1498 | block, (int) blockcnt); | |
a8519a2d TT |
1499 | if (retval) { |
1500 | pb->error = retval; | |
1501 | ret |= BLOCK_ABORT; | |
1502 | } | |
1503 | } | |
1504 | return ret; | |
1505 | } | |
1506 | ||
1507 | /* | |
1508 | * Progress callback | |
1509 | */ | |
efc6f628 | 1510 | static errcode_t progress_callback(ext2_filsys fs, |
54434927 | 1511 | ext2_inode_scan scan EXT2FS_ATTR((unused)), |
a8519a2d TT |
1512 | dgrp_t group, void * priv_data) |
1513 | { | |
1514 | ext2_resize_t rfs = (ext2_resize_t) priv_data; | |
3b627e8d | 1515 | errcode_t retval; |
a8519a2d TT |
1516 | |
1517 | /* | |
f4b2a6db TT |
1518 | * This check is to protect against old ext2 libraries. It |
1519 | * shouldn't be needed against new libraries. | |
a8519a2d | 1520 | */ |
f4b2a6db | 1521 | if ((group+1) == 0) |
a8519a2d TT |
1522 | return 0; |
1523 | ||
1524 | if (rfs->progress) { | |
1525 | io_channel_flush(fs->io); | |
3b627e8d TT |
1526 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS, |
1527 | group+1, fs->group_desc_count); | |
1528 | if (retval) | |
1529 | return retval; | |
a8519a2d | 1530 | } |
efc6f628 | 1531 | |
a8519a2d TT |
1532 | return 0; |
1533 | } | |
1534 | ||
1535 | static errcode_t inode_scan_and_fix(ext2_resize_t rfs) | |
1536 | { | |
1537 | struct process_block_struct pb; | |
dfcdc32f | 1538 | ext2_ino_t ino, new_inode; |
edfd9b0a | 1539 | struct ext2_inode *inode = NULL; |
a8519a2d TT |
1540 | ext2_inode_scan scan = NULL; |
1541 | errcode_t retval; | |
a8519a2d | 1542 | char *block_buf = 0; |
dfcdc32f | 1543 | ext2_ino_t start_to_move; |
8728d506 VAH |
1544 | blk64_t orig_size; |
1545 | blk64_t new_block; | |
4ef28824 | 1546 | int inode_size; |
efc6f628 | 1547 | |
a8519a2d TT |
1548 | if ((rfs->old_fs->group_desc_count <= |
1549 | rfs->new_fs->group_desc_count) && | |
1550 | !rfs->bmap) | |
1551 | return 0; | |
1552 | ||
2bc4d4f7 TT |
1553 | /* |
1554 | * Save the original size of the old filesystem, and | |
1555 | * temporarily set the size to be the new size if the new size | |
1556 | * is larger. We need to do this to avoid catching an error | |
1557 | * by the block iterator routines | |
1558 | */ | |
4efbac6f VAH |
1559 | orig_size = ext2fs_blocks_count(rfs->old_fs->super); |
1560 | if (orig_size < ext2fs_blocks_count(rfs->new_fs->super)) | |
1561 | ext2fs_blocks_count_set(rfs->old_fs->super, | |
1562 | ext2fs_blocks_count(rfs->new_fs->super)); | |
2bc4d4f7 | 1563 | |
a8519a2d TT |
1564 | retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan); |
1565 | if (retval) goto errout; | |
1566 | ||
1567 | retval = ext2fs_init_dblist(rfs->old_fs, 0); | |
1568 | if (retval) goto errout; | |
e5aace90 | 1569 | retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf); |
a8519a2d TT |
1570 | if (retval) goto errout; |
1571 | ||
1572 | start_to_move = (rfs->new_fs->group_desc_count * | |
1573 | rfs->new_fs->super->s_inodes_per_group); | |
efc6f628 | 1574 | |
3b627e8d TT |
1575 | if (rfs->progress) { |
1576 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS, | |
1577 | 0, rfs->old_fs->group_desc_count); | |
1578 | if (retval) | |
1579 | goto errout; | |
1580 | } | |
a8519a2d TT |
1581 | ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs); |
1582 | pb.rfs = rfs; | |
edfd9b0a | 1583 | pb.inode = inode; |
a8519a2d TT |
1584 | pb.error = 0; |
1585 | new_inode = EXT2_FIRST_INODE(rfs->new_fs->super); | |
4ef28824 | 1586 | inode_size = EXT2_INODE_SIZE(rfs->new_fs->super); |
edfd9b0a TT |
1587 | inode = malloc(inode_size); |
1588 | if (!inode) { | |
4ef28824 ES |
1589 | retval = ENOMEM; |
1590 | goto errout; | |
1591 | } | |
a8519a2d TT |
1592 | /* |
1593 | * First, copy all of the inodes that need to be moved | |
1594 | * elsewhere in the inode table | |
1595 | */ | |
1596 | while (1) { | |
edfd9b0a | 1597 | retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size); |
a8519a2d TT |
1598 | if (retval) goto errout; |
1599 | if (!ino) | |
1600 | break; | |
1601 | ||
edfd9b0a | 1602 | if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO) |
a8519a2d TT |
1603 | continue; /* inode not in use */ |
1604 | ||
edfd9b0a | 1605 | pb.is_dir = LINUX_S_ISDIR(inode->i_mode); |
a8519a2d TT |
1606 | pb.changed = 0; |
1607 | ||
0c80c44b | 1608 | if (ext2fs_file_acl_block(rfs->old_fs, inode) && rfs->bmap) { |
d1a1a583 | 1609 | new_block = extent_translate(rfs->old_fs, rfs->bmap, |
0c80c44b | 1610 | ext2fs_file_acl_block(rfs->old_fs, inode)); |
ed909bbe | 1611 | if (new_block) { |
0c80c44b TT |
1612 | ext2fs_file_acl_block_set(rfs->old_fs, inode, |
1613 | new_block); | |
efc6f628 | 1614 | retval = ext2fs_write_inode_full(rfs->old_fs, |
edfd9b0a | 1615 | ino, inode, inode_size); |
ed909bbe TT |
1616 | if (retval) goto errout; |
1617 | } | |
1618 | } | |
efc6f628 | 1619 | |
0c80c44b | 1620 | if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) && |
a8519a2d TT |
1621 | (rfs->bmap || pb.is_dir)) { |
1622 | pb.ino = ino; | |
8728d506 | 1623 | retval = ext2fs_block_iterate3(rfs->old_fs, |
a8519a2d TT |
1624 | ino, 0, block_buf, |
1625 | process_block, &pb); | |
1626 | if (retval) | |
1627 | goto errout; | |
1628 | if (pb.error) { | |
1629 | retval = pb.error; | |
1630 | goto errout; | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | if (ino <= start_to_move) | |
1635 | continue; /* Don't need to move it. */ | |
1636 | ||
1637 | /* | |
1638 | * Find a new inode | |
1639 | */ | |
86acdebd TT |
1640 | retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode); |
1641 | if (retval) | |
1642 | goto errout; | |
1643 | ||
74128f8d TT |
1644 | ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1, |
1645 | pb.is_dir); | |
a8519a2d TT |
1646 | if (pb.changed) { |
1647 | /* Get the new version of the inode */ | |
4ef28824 | 1648 | retval = ext2fs_read_inode_full(rfs->old_fs, ino, |
edfd9b0a | 1649 | inode, inode_size); |
a8519a2d TT |
1650 | if (retval) goto errout; |
1651 | } | |
edfd9b0a | 1652 | inode->i_ctime = time(0); |
4ef28824 | 1653 | retval = ext2fs_write_inode_full(rfs->old_fs, new_inode, |
edfd9b0a | 1654 | inode, inode_size); |
a8519a2d TT |
1655 | if (retval) goto errout; |
1656 | ||
a8519a2d TT |
1657 | #ifdef RESIZE2FS_DEBUG |
1658 | if (rfs->flags & RESIZE_DEBUG_INODEMAP) | |
f35fd3d5 | 1659 | printf("Inode moved %u->%u\n", ino, new_inode); |
a8519a2d TT |
1660 | #endif |
1661 | if (!rfs->imap) { | |
1662 | retval = ext2fs_create_extent_table(&rfs->imap, 0); | |
1663 | if (retval) | |
1664 | goto errout; | |
1665 | } | |
1666 | ext2fs_add_extent_entry(rfs->imap, ino, new_inode); | |
1667 | } | |
1668 | io_channel_flush(rfs->old_fs->io); | |
1669 | ||
1670 | errout: | |
4efbac6f | 1671 | ext2fs_blocks_count_set(rfs->old_fs->super, orig_size); |
a8519a2d TT |
1672 | if (rfs->bmap) { |
1673 | ext2fs_free_extent_table(rfs->bmap); | |
1674 | rfs->bmap = 0; | |
1675 | } | |
1676 | if (scan) | |
1677 | ext2fs_close_inode_scan(scan); | |
1678 | if (block_buf) | |
c4e3d3f3 | 1679 | ext2fs_free_mem(&block_buf); |
45e338f5 | 1680 | free(inode); |
a8519a2d TT |
1681 | return retval; |
1682 | } | |
1683 | ||
1684 | /* -------------------------------------------------------------------- | |
1685 | * | |
1686 | * Resize processing, phase 4. | |
1687 | * | |
1688 | * -------------------------------------------------------------------- | |
1689 | */ | |
1690 | ||
1691 | struct istruct { | |
1692 | ext2_resize_t rfs; | |
3b627e8d | 1693 | errcode_t err; |
03fa6f8a TT |
1694 | unsigned int max_dirs; |
1695 | unsigned int num; | |
a8519a2d TT |
1696 | }; |
1697 | ||
efc6f628 | 1698 | static int check_and_change_inodes(ext2_ino_t dir, |
54434927 | 1699 | int entry EXT2FS_ATTR((unused)), |
a8519a2d | 1700 | struct ext2_dir_entry *dirent, int offset, |
54434927 | 1701 | int blocksize EXT2FS_ATTR((unused)), |
efc6f628 | 1702 | char *buf EXT2FS_ATTR((unused)), |
54434927 | 1703 | void *priv_data) |
a8519a2d TT |
1704 | { |
1705 | struct istruct *is = (struct istruct *) priv_data; | |
085d2a83 TT |
1706 | struct ext2_inode inode; |
1707 | ext2_ino_t new_inode; | |
1708 | errcode_t retval; | |
a8519a2d TT |
1709 | |
1710 | if (is->rfs->progress && offset == 0) { | |
1711 | io_channel_flush(is->rfs->old_fs->io); | |
3b627e8d TT |
1712 | is->err = (is->rfs->progress)(is->rfs, |
1713 | E2_RSZ_INODE_REF_UPD_PASS, | |
1333fe93 | 1714 | ++is->num, is->max_dirs); |
3b627e8d TT |
1715 | if (is->err) |
1716 | return DIRENT_ABORT; | |
a8519a2d TT |
1717 | } |
1718 | ||
1719 | if (!dirent->inode) | |
1720 | return 0; | |
1721 | ||
1722 | new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode); | |
1723 | ||
1724 | if (!new_inode) | |
1725 | return 0; | |
1726 | #ifdef RESIZE2FS_DEBUG | |
1727 | if (is->rfs->flags & RESIZE_DEBUG_INODEMAP) | |
f35fd3d5 | 1728 | printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n", |
06191693 | 1729 | dir, dirent->name_len&0xFF, dirent->name, |
a8519a2d TT |
1730 | dirent->inode, new_inode); |
1731 | #endif | |
1732 | ||
1733 | dirent->inode = new_inode; | |
1734 | ||
085d2a83 TT |
1735 | /* Update the directory mtime and ctime */ |
1736 | retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode); | |
1737 | if (retval == 0) { | |
1738 | inode.i_mtime = inode.i_ctime = time(0); | |
d2b2a488 BB |
1739 | is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode); |
1740 | if (is->err) | |
1741 | return DIRENT_ABORT; | |
085d2a83 TT |
1742 | } |
1743 | ||
a8519a2d TT |
1744 | return DIRENT_CHANGED; |
1745 | } | |
1746 | ||
1747 | static errcode_t inode_ref_fix(ext2_resize_t rfs) | |
1748 | { | |
1749 | errcode_t retval; | |
1750 | struct istruct is; | |
efc6f628 | 1751 | |
a8519a2d TT |
1752 | if (!rfs->imap) |
1753 | return 0; | |
efc6f628 | 1754 | |
a8519a2d TT |
1755 | /* |
1756 | * Now, we iterate over all of the directories to update the | |
1757 | * inode references | |
1758 | */ | |
1759 | is.num = 0; | |
8728d506 | 1760 | is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist); |
a8519a2d | 1761 | is.rfs = rfs; |
3b627e8d | 1762 | is.err = 0; |
a8519a2d | 1763 | |
3b627e8d TT |
1764 | if (rfs->progress) { |
1765 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS, | |
1333fe93 | 1766 | 0, is.max_dirs); |
3b627e8d TT |
1767 | if (retval) |
1768 | goto errout; | |
1769 | } | |
efc6f628 | 1770 | |
a8519a2d TT |
1771 | retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist, |
1772 | DIRENT_FLAG_INCLUDE_EMPTY, 0, | |
1773 | check_and_change_inodes, &is); | |
3b627e8d TT |
1774 | if (retval) |
1775 | goto errout; | |
1776 | if (is.err) { | |
1777 | retval = is.err; | |
1778 | goto errout; | |
1779 | } | |
a8519a2d | 1780 | |
88dbf828 TT |
1781 | if (rfs->progress && (is.num < is.max_dirs)) |
1782 | (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS, | |
1783 | is.max_dirs, is.max_dirs); | |
1784 | ||
3b627e8d | 1785 | errout: |
a8519a2d TT |
1786 | ext2fs_free_extent_table(rfs->imap); |
1787 | rfs->imap = 0; | |
1788 | return retval; | |
1789 | } | |
1790 | ||
1791 | ||
1792 | /* -------------------------------------------------------------------- | |
1793 | * | |
1794 | * Resize processing, phase 5. | |
1795 | * | |
1796 | * In this phase we actually move the inode table around, and then | |
1797 | * update the summary statistics. This is scary, since aborting here | |
1798 | * will potentially scramble the filesystem. (We are moving the | |
1799 | * inode tables around in place, and so the potential for lost data, | |
1800 | * or at the very least scrambling the mapping between filenames and | |
1801 | * inode numbers is very high in case of a power failure here.) | |
1802 | * -------------------------------------------------------------------- | |
1803 | */ | |
1804 | ||
24b2c7a7 | 1805 | |
052db4b7 TT |
1806 | /* |
1807 | * A very scary routine --- this one moves the inode table around!!! | |
1808 | * | |
1809 | * After this you have to use the rfs->new_fs file handle to read and | |
1810 | * write inodes. | |
1811 | */ | |
c762c8e6 | 1812 | static errcode_t move_itables(ext2_resize_t rfs) |
052db4b7 | 1813 | { |
8728d506 VAH |
1814 | int n, num, size; |
1815 | long long diff; | |
54434927 | 1816 | dgrp_t i, max_groups; |
052db4b7 | 1817 | ext2_filsys fs = rfs->new_fs; |
05e112a1 | 1818 | char *cp; |
118d3f0b | 1819 | blk64_t old_blk, new_blk, blk, cluster_freed; |
a8519a2d | 1820 | errcode_t retval; |
64ad98ac | 1821 | int j, to_move, moved; |
118d3f0b | 1822 | ext2fs_block_bitmap new_bmap = NULL; |
052db4b7 | 1823 | |
1333fe93 TT |
1824 | max_groups = fs->group_desc_count; |
1825 | if (max_groups > rfs->old_fs->group_desc_count) | |
1826 | max_groups = rfs->old_fs->group_desc_count; | |
052db4b7 | 1827 | |
05e112a1 TT |
1828 | size = fs->blocksize * fs->inode_blocks_per_group; |
1829 | if (!rfs->itable_buf) { | |
c4e3d3f3 | 1830 | retval = ext2fs_get_mem(size, &rfs->itable_buf); |
ca8abba7 TT |
1831 | if (retval) |
1832 | return retval; | |
05e112a1 | 1833 | } |
c762c8e6 | 1834 | |
118d3f0b DW |
1835 | if (EXT2FS_CLUSTER_RATIO(fs) > 1) { |
1836 | retval = ext2fs_allocate_block_bitmap(fs, _("new meta blocks"), | |
1837 | &new_bmap); | |
1838 | if (retval) | |
1839 | return retval; | |
1840 | ||
1841 | retval = mark_table_blocks(fs, new_bmap); | |
1842 | if (retval) | |
1843 | goto errout; | |
1844 | } | |
1845 | ||
c762c8e6 TT |
1846 | /* |
1847 | * Figure out how many inode tables we need to move | |
1848 | */ | |
1849 | to_move = moved = 0; | |
1333fe93 | 1850 | for (i=0; i < max_groups; i++) |
d7cca6b0 VAH |
1851 | if (ext2fs_inode_table_loc(rfs->old_fs, i) != |
1852 | ext2fs_inode_table_loc(fs, i)) | |
c762c8e6 TT |
1853 | to_move++; |
1854 | ||
1855 | if (to_move == 0) | |
1856 | return 0; | |
1857 | ||
3b627e8d TT |
1858 | if (rfs->progress) { |
1859 | retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, | |
1860 | 0, to_move); | |
1861 | if (retval) | |
1862 | goto errout; | |
1863 | } | |
63b44fbe | 1864 | |
a8519a2d TT |
1865 | rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
1866 | ||
1333fe93 | 1867 | for (i=0; i < max_groups; i++) { |
d7cca6b0 VAH |
1868 | old_blk = ext2fs_inode_table_loc(rfs->old_fs, i); |
1869 | new_blk = ext2fs_inode_table_loc(fs, i); | |
ca8abba7 | 1870 | diff = new_blk - old_blk; |
efc6f628 | 1871 | |
80c0fc34 | 1872 | #ifdef RESIZE2FS_DEBUG |
efc6f628 | 1873 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
8728d506 | 1874 | printf("Itable move group %d block %llu->%llu (diff %lld)\n", |
ca8abba7 | 1875 | i, old_blk, new_blk, diff); |
80c0fc34 | 1876 | #endif |
efc6f628 | 1877 | |
05e112a1 | 1878 | if (!diff) |
052db4b7 | 1879 | continue; |
cd84e9a3 TT |
1880 | if (diff < 0) |
1881 | diff = 0; | |
052db4b7 | 1882 | |
24a117ab VAH |
1883 | retval = io_channel_read_blk64(fs->io, old_blk, |
1884 | fs->inode_blocks_per_group, | |
1885 | rfs->itable_buf); | |
efc6f628 | 1886 | if (retval) |
a8519a2d | 1887 | goto errout; |
05e112a1 TT |
1888 | /* |
1889 | * The end of the inode table segment often contains | |
a8519a2d TT |
1890 | * all zeros, and we're often only moving the inode |
1891 | * table down a block or two. If so, we can optimize | |
1892 | * things by not rewriting blocks that we know to be zero | |
1893 | * already. | |
05e112a1 | 1894 | */ |
2787276e | 1895 | for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--) |
05e112a1 TT |
1896 | if (*cp) |
1897 | break; | |
1898 | n = n >> EXT2_BLOCK_SIZE_BITS(fs->super); | |
80c0fc34 | 1899 | #ifdef RESIZE2FS_DEBUG |
efc6f628 | 1900 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
f35fd3d5 | 1901 | printf("%d blocks of zeros...\n", n); |
80c0fc34 | 1902 | #endif |
05e112a1 TT |
1903 | num = fs->inode_blocks_per_group; |
1904 | if (n > diff) | |
1905 | num -= n; | |
1906 | ||
24a117ab VAH |
1907 | retval = io_channel_write_blk64(fs->io, new_blk, |
1908 | num, rfs->itable_buf); | |
052db4b7 | 1909 | if (retval) { |
24a117ab VAH |
1910 | io_channel_write_blk64(fs->io, old_blk, |
1911 | num, rfs->itable_buf); | |
a8519a2d | 1912 | goto errout; |
052db4b7 | 1913 | } |
05e112a1 | 1914 | if (n > diff) { |
24a117ab | 1915 | retval = io_channel_write_blk64(fs->io, |
ca8abba7 | 1916 | old_blk + fs->inode_blocks_per_group, |
a8519a2d TT |
1917 | diff, (rfs->itable_buf + |
1918 | (fs->inode_blocks_per_group - diff) * | |
1919 | fs->blocksize)); | |
05e112a1 | 1920 | if (retval) |
a8519a2d | 1921 | goto errout; |
c762c8e6 | 1922 | } |
64ad98ac | 1923 | |
d7cca6b0 | 1924 | for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0; |
118d3f0b DW |
1925 | j < fs->inode_blocks_per_group;) { |
1926 | if (new_bmap == NULL || | |
1927 | !ext2fs_test_block_bitmap2(new_bmap, blk)) { | |
1928 | ext2fs_block_alloc_stats2(fs, blk, -1); | |
1929 | cluster_freed = EXT2FS_CLUSTER_RATIO(fs) - | |
1930 | (blk & EXT2FS_CLUSTER_MASK(fs)); | |
1931 | blk += cluster_freed; | |
1932 | j += cluster_freed; | |
1933 | continue; | |
1934 | } | |
1935 | blk++; | |
1936 | j++; | |
1937 | } | |
64ad98ac | 1938 | |
d7cca6b0 | 1939 | ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk); |
236efede | 1940 | ext2fs_group_desc_csum_set(rfs->old_fs, i); |
a8519a2d | 1941 | ext2fs_mark_super_dirty(rfs->old_fs); |
64ad98ac TT |
1942 | ext2fs_flush(rfs->old_fs); |
1943 | ||
a8519a2d | 1944 | if (rfs->progress) { |
3b627e8d TT |
1945 | retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, |
1946 | ++moved, to_move); | |
1947 | if (retval) | |
1948 | goto errout; | |
a8519a2d | 1949 | } |
052db4b7 | 1950 | } |
64ad98ac | 1951 | mark_table_blocks(fs, fs->block_map); |
a8519a2d | 1952 | ext2fs_flush(fs); |
80c0fc34 | 1953 | #ifdef RESIZE2FS_DEBUG |
efc6f628 | 1954 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
f35fd3d5 | 1955 | printf("Inode table move finished.\n"); |
80c0fc34 | 1956 | #endif |
118d3f0b | 1957 | retval = 0; |
efc6f628 | 1958 | |
a8519a2d | 1959 | errout: |
118d3f0b DW |
1960 | if (new_bmap) |
1961 | ext2fs_free_block_bitmap(new_bmap); | |
052db4b7 TT |
1962 | return retval; |
1963 | } | |
1964 | ||
65c6c3e0 TT |
1965 | /* |
1966 | * This function is used when expanding a file system. It frees the | |
1967 | * superblock and block group descriptor blocks from the block group | |
1968 | * which is no longer the last block group. | |
1969 | */ | |
1970 | static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs) | |
1971 | { | |
1972 | ext2_filsys fs = rfs->new_fs; | |
1973 | ext2_filsys old_fs = rfs->old_fs; | |
1974 | errcode_t retval; | |
1975 | dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1; | |
1976 | dgrp_t last_bg = fs->group_desc_count - 1; | |
1977 | blk64_t sb, old_desc; | |
1978 | blk_t num; | |
1979 | ||
1980 | if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2)) | |
1981 | return 0; | |
1982 | ||
1983 | if (last_bg <= old_last_bg) | |
1984 | return 0; | |
1985 | ||
1986 | if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] && | |
1987 | fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1]) | |
1988 | return 0; | |
1989 | ||
1990 | if (old_fs->super->s_backup_bgs[0] != old_last_bg && | |
1991 | old_fs->super->s_backup_bgs[1] != old_last_bg) | |
1992 | return 0; | |
1993 | ||
1994 | if (fs->super->s_backup_bgs[0] == old_last_bg || | |
1995 | fs->super->s_backup_bgs[1] == old_last_bg) | |
1996 | return 0; | |
1997 | ||
1998 | retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg, | |
1999 | &sb, &old_desc, NULL, &num); | |
2000 | if (retval) | |
2001 | return retval; | |
2002 | ||
2003 | if (sb) | |
2004 | ext2fs_unmark_block_bitmap2(fs->block_map, sb); | |
2005 | if (old_desc) | |
2006 | ext2fs_unmark_block_bitmap_range2(fs->block_map, old_desc, num); | |
2007 | return 0; | |
2008 | } | |
2009 | ||
2010 | /* | |
2011 | * This function is used when shrinking a file system. We need to | |
2012 | * utilize blocks from what will be the new last block group for the | |
2013 | * backup superblock and block group descriptor blocks. | |
2014 | * Unfortunately, those blocks may be used by other files or fs | |
2015 | * metadata blocks. We need to mark them as being in use. | |
2016 | */ | |
2017 | static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs, | |
2018 | ext2fs_block_bitmap meta_bmap) | |
2019 | { | |
2020 | ext2_filsys fs = rfs->new_fs; | |
2021 | ext2_filsys old_fs = rfs->old_fs; | |
2022 | errcode_t retval; | |
2023 | dgrp_t old_last_bg = rfs->old_fs->group_desc_count - 1; | |
2024 | dgrp_t last_bg = fs->group_desc_count - 1; | |
2025 | dgrp_t g; | |
2026 | blk64_t blk, sb, old_desc; | |
2027 | blk_t i, num; | |
2028 | int realloc = 0; | |
2029 | ||
2030 | if (!(fs->super->s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2)) | |
2031 | return 0; | |
2032 | ||
2033 | if (last_bg >= old_last_bg) | |
2034 | return 0; | |
2035 | ||
2036 | if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] && | |
2037 | fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1]) | |
2038 | return 0; | |
2039 | ||
2040 | if (fs->super->s_backup_bgs[0] != last_bg && | |
2041 | fs->super->s_backup_bgs[1] != last_bg) | |
2042 | return 0; | |
2043 | ||
2044 | if (old_fs->super->s_backup_bgs[0] == last_bg || | |
2045 | old_fs->super->s_backup_bgs[1] == last_bg) | |
2046 | return 0; | |
2047 | ||
2048 | retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, last_bg, | |
2049 | &sb, &old_desc, NULL, &num); | |
2050 | if (retval) | |
2051 | return retval; | |
2052 | ||
2053 | if (!sb) { | |
2054 | fputs(_("Should never happen! No sb in last super_sparse bg?\n"), | |
2055 | stderr); | |
2056 | exit(1); | |
2057 | } | |
1244cacc | 2058 | if (old_desc && old_desc != sb+1) { |
65c6c3e0 TT |
2059 | fputs(_("Should never happen! Unexpected old_desc in " |
2060 | "super_sparse bg?\n"), | |
2061 | stderr); | |
2062 | exit(1); | |
2063 | } | |
2064 | num = (old_desc) ? num : 1; | |
2065 | ||
2066 | /* Reserve the backup blocks */ | |
2067 | ext2fs_mark_block_bitmap_range2(fs->block_map, sb, num); | |
2068 | ||
2069 | for (g = 0; g < fs->group_desc_count; g++) { | |
2070 | blk64_t mb; | |
2071 | ||
2072 | mb = ext2fs_block_bitmap_loc(fs, g); | |
2073 | if ((mb >= sb) && (mb < sb + num)) { | |
2074 | ext2fs_block_bitmap_loc_set(fs, g, 0); | |
2075 | realloc = 1; | |
2076 | } | |
2077 | mb = ext2fs_inode_bitmap_loc(fs, g); | |
2078 | if ((mb >= sb) && (mb < sb + num)) { | |
2079 | ext2fs_inode_bitmap_loc_set(fs, g, 0); | |
2080 | realloc = 1; | |
2081 | } | |
2082 | mb = ext2fs_inode_table_loc(fs, g); | |
2083 | if ((mb < sb + num) && | |
2084 | (sb < mb + fs->inode_blocks_per_group)) { | |
2085 | ext2fs_inode_table_loc_set(fs, g, 0); | |
2086 | realloc = 1; | |
2087 | } | |
2088 | if (realloc) { | |
2089 | retval = ext2fs_allocate_group_table(fs, g, 0); | |
2090 | if (retval) | |
2091 | return retval; | |
2092 | } | |
2093 | } | |
2094 | ||
2095 | for (blk = sb, i = 0; i < num; blk++, i++) { | |
2096 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && | |
2097 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) { | |
2098 | ext2fs_mark_block_bitmap2(rfs->move_blocks, blk); | |
2099 | rfs->needed_blocks++; | |
2100 | } | |
2101 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk); | |
2102 | } | |
2103 | return 0; | |
2104 | } | |
2105 | ||
9213a93b | 2106 | /* |
efc6f628 | 2107 | * Fix the resize inode |
9213a93b TT |
2108 | */ |
2109 | static errcode_t fix_resize_inode(ext2_filsys fs) | |
2110 | { | |
2111 | struct ext2_inode inode; | |
2112 | errcode_t retval; | |
5e27a274 | 2113 | char *block_buf = NULL; |
9213a93b | 2114 | |
efc6f628 | 2115 | if (!(fs->super->s_feature_compat & |
9213a93b TT |
2116 | EXT2_FEATURE_COMPAT_RESIZE_INODE)) |
2117 | return 0; | |
2118 | ||
2119 | retval = ext2fs_get_mem(fs->blocksize, &block_buf); | |
2120 | if (retval) goto errout; | |
2121 | ||
2122 | retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode); | |
2123 | if (retval) goto errout; | |
2124 | ||
1ca1059f | 2125 | ext2fs_iblk_set(fs, &inode, 1); |
9213a93b TT |
2126 | |
2127 | retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode); | |
2128 | if (retval) goto errout; | |
2129 | ||
2130 | if (!inode.i_block[EXT2_DIND_BLOCK]) { | |
efc6f628 | 2131 | /* |
9213a93b TT |
2132 | * Avoid zeroing out block #0; that's rude. This |
2133 | * should never happen anyway since the filesystem | |
2134 | * should be fsck'ed and we assume it is consistent. | |
2135 | */ | |
45ff69ff | 2136 | fprintf(stderr, "%s", |
f35fd3d5 | 2137 | _("Should never happen: resize inode corrupt!\n")); |
9213a93b TT |
2138 | exit(1); |
2139 | } | |
2140 | ||
2141 | memset(block_buf, 0, fs->blocksize); | |
2142 | ||
24a117ab VAH |
2143 | retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK], |
2144 | 1, block_buf); | |
9213a93b | 2145 | if (retval) goto errout; |
efc6f628 | 2146 | |
9213a93b TT |
2147 | retval = ext2fs_create_resize_inode(fs); |
2148 | if (retval) | |
2149 | goto errout; | |
2150 | ||
2151 | errout: | |
2152 | if (block_buf) | |
2153 | ext2fs_free_mem(&block_buf); | |
2154 | return retval; | |
2155 | } | |
2156 | ||
052db4b7 TT |
2157 | /* |
2158 | * Finally, recalculate the summary information | |
2159 | */ | |
2160 | static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs) | |
2161 | { | |
8728d506 | 2162 | blk64_t blk; |
dfcdc32f | 2163 | ext2_ino_t ino; |
54434927 TT |
2164 | unsigned int group = 0; |
2165 | unsigned int count = 0; | |
2279fd78 TT |
2166 | blk64_t total_blocks_free = 0; |
2167 | int total_inodes_free = 0; | |
dfcdc32f | 2168 | int group_free = 0; |
74128f8d | 2169 | int uninit = 0; |
8728d506 | 2170 | blk64_t super_blk, old_desc_blk, new_desc_blk; |
74128f8d | 2171 | int old_desc_blocks; |
052db4b7 TT |
2172 | |
2173 | /* | |
2174 | * First calculate the block statistics | |
2175 | */ | |
cd65a24e | 2176 | uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT); |
8728d506 VAH |
2177 | ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk, |
2178 | &new_desc_blk, 0); | |
74128f8d TT |
2179 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
2180 | old_desc_blocks = fs->super->s_first_meta_bg; | |
2181 | else | |
2182 | old_desc_blocks = fs->desc_blocks + | |
2183 | fs->super->s_reserved_gdt_blocks; | |
1773c87c TT |
2184 | for (blk = B2C(fs->super->s_first_data_block); |
2185 | blk < ext2fs_blocks_count(fs->super); | |
2186 | blk += EXT2FS_CLUSTER_RATIO(fs)) { | |
74128f8d | 2187 | if ((uninit && |
1773c87c | 2188 | !(EQ_CLSTR(blk, super_blk) || |
74128f8d | 2189 | ((old_desc_blk && old_desc_blocks && |
1773c87c TT |
2190 | GE_CLSTR(blk, old_desc_blk) && |
2191 | LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) || | |
2192 | ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) || | |
2193 | EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) || | |
2194 | EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) || | |
2195 | ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) && | |
2196 | LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group) | |
2197 | + fs->inode_blocks_per_group))))) || | |
3346084b | 2198 | (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) { |
052db4b7 | 2199 | group_free++; |
2279fd78 | 2200 | total_blocks_free++; |
052db4b7 TT |
2201 | } |
2202 | count++; | |
1773c87c TT |
2203 | if ((count == fs->super->s_clusters_per_group) || |
2204 | EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) { | |
d7cca6b0 | 2205 | ext2fs_bg_free_blocks_count_set(fs, group, group_free); |
236efede JS |
2206 | ext2fs_group_desc_csum_set(fs, group); |
2207 | group++; | |
4828bbe9 TT |
2208 | if (group >= fs->group_desc_count) |
2209 | break; | |
052db4b7 TT |
2210 | count = 0; |
2211 | group_free = 0; | |
8728d506 VAH |
2212 | uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT); |
2213 | ext2fs_super_and_bgd_loc2(fs, group, &super_blk, | |
2214 | &old_desc_blk, | |
2215 | &new_desc_blk, 0); | |
74128f8d TT |
2216 | if (fs->super->s_feature_incompat & |
2217 | EXT2_FEATURE_INCOMPAT_META_BG) | |
2218 | old_desc_blocks = fs->super->s_first_meta_bg; | |
2219 | else | |
2220 | old_desc_blocks = fs->desc_blocks + | |
2221 | fs->super->s_reserved_gdt_blocks; | |
052db4b7 TT |
2222 | } |
2223 | } | |
1773c87c | 2224 | total_blocks_free = C2B(total_blocks_free); |
2279fd78 | 2225 | ext2fs_free_blocks_count_set(fs->super, total_blocks_free); |
efc6f628 | 2226 | |
052db4b7 TT |
2227 | /* |
2228 | * Next, calculate the inode statistics | |
2229 | */ | |
2230 | group_free = 0; | |
052db4b7 TT |
2231 | count = 0; |
2232 | group = 0; | |
5830d6be ES |
2233 | |
2234 | /* Protect loop from wrap-around if s_inodes_count maxed */ | |
cd65a24e | 2235 | uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT); |
5830d6be | 2236 | for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) { |
74128f8d | 2237 | if (uninit || |
3346084b | 2238 | !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) { |
052db4b7 | 2239 | group_free++; |
2279fd78 | 2240 | total_inodes_free++; |
052db4b7 TT |
2241 | } |
2242 | count++; | |
2243 | if ((count == fs->super->s_inodes_per_group) || | |
2244 | (ino == fs->super->s_inodes_count)) { | |
d7cca6b0 | 2245 | ext2fs_bg_free_inodes_count_set(fs, group, group_free); |
236efede JS |
2246 | ext2fs_group_desc_csum_set(fs, group); |
2247 | group++; | |
4828bbe9 TT |
2248 | if (group >= fs->group_desc_count) |
2249 | break; | |
052db4b7 TT |
2250 | count = 0; |
2251 | group_free = 0; | |
cd65a24e | 2252 | uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT); |
052db4b7 TT |
2253 | } |
2254 | } | |
2279fd78 | 2255 | fs->super->s_free_inodes_count = total_inodes_free; |
052db4b7 TT |
2256 | ext2fs_mark_super_dirty(fs); |
2257 | return 0; | |
2258 | } | |
199ddaaa | 2259 | |
8a6ede8b ES |
2260 | /* |
2261 | * Journal may have been relocated; update the backup journal blocks | |
2262 | * in the superblock. | |
2263 | */ | |
2264 | static errcode_t fix_sb_journal_backup(ext2_filsys fs) | |
2265 | { | |
2266 | errcode_t retval; | |
2267 | struct ext2_inode inode; | |
2268 | ||
2269 | if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) | |
2270 | return 0; | |
2271 | ||
d93d5bbf ES |
2272 | /* External journal? Nothing to do. */ |
2273 | if (fs->super->s_journal_dev && !fs->super->s_journal_inum) | |
2274 | return 0; | |
2275 | ||
8a6ede8b ES |
2276 | retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode); |
2277 | if (retval) | |
2278 | return retval; | |
2279 | memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4); | |
931b58e1 | 2280 | fs->super->s_jnl_blocks[15] = inode.i_size_high; |
8a6ede8b ES |
2281 | fs->super->s_jnl_blocks[16] = inode.i_size; |
2282 | fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS; | |
2283 | ext2fs_mark_super_dirty(fs); | |
2284 | return 0; | |
2285 | } | |
2286 | ||
7f820344 TT |
2287 | static int calc_group_overhead(ext2_filsys fs, blk64_t grp, |
2288 | int old_desc_blocks) | |
2289 | { | |
2290 | blk64_t super_blk, old_desc_blk, new_desc_blk; | |
2291 | int overhead; | |
2292 | ||
2293 | /* inode table blocks plus allocation bitmaps */ | |
2294 | overhead = fs->inode_blocks_per_group + 2; | |
2295 | ||
2296 | ext2fs_super_and_bgd_loc2(fs, grp, &super_blk, | |
2297 | &old_desc_blk, &new_desc_blk, 0); | |
2298 | if ((grp == 0) || super_blk) | |
2299 | overhead++; | |
2300 | if (old_desc_blk) | |
2301 | overhead += old_desc_blocks; | |
2302 | else if (new_desc_blk) | |
2303 | overhead++; | |
2304 | return overhead; | |
2305 | } | |
2306 | ||
2307 | ||
199ddaaa JB |
2308 | /* |
2309 | * calcluate the minimum number of blocks the given fs can be resized to | |
2310 | */ | |
e231f175 | 2311 | blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags) |
199ddaaa | 2312 | { |
8728d506 | 2313 | ext2_ino_t inode_count; |
45a78b88 | 2314 | dgrp_t groups, flex_groups; |
e231f175 | 2315 | blk64_t blks_needed, data_blocks; |
8728d506 VAH |
2316 | blk64_t grp, data_needed, last_start; |
2317 | blk64_t overhead = 0; | |
7f820344 | 2318 | int old_desc_blocks; |
2884d208 | 2319 | int flexbg_size = 1 << fs->super->s_log_groups_per_flex; |
199ddaaa JB |
2320 | |
2321 | /* | |
2322 | * first figure out how many group descriptors we need to | |
2323 | * handle the number of inodes we have | |
2324 | */ | |
2325 | inode_count = fs->super->s_inodes_count - | |
2326 | fs->super->s_free_inodes_count; | |
2327 | blks_needed = ext2fs_div_ceil(inode_count, | |
2328 | fs->super->s_inodes_per_group) * | |
1e33a8b4 | 2329 | (blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super); |
8728d506 VAH |
2330 | groups = ext2fs_div64_ceil(blks_needed, |
2331 | EXT2_BLOCKS_PER_GROUP(fs->super)); | |
e231f175 TT |
2332 | #ifdef RESIZE2FS_DEBUG |
2333 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2334 | printf("fs has %d inodes, %d groups required.\n", | |
2335 | inode_count, groups); | |
2336 | #endif | |
199ddaaa JB |
2337 | |
2338 | /* | |
7f820344 | 2339 | * number of old-style block group descriptor blocks |
199ddaaa | 2340 | */ |
7f820344 TT |
2341 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
2342 | old_desc_blocks = fs->super->s_first_meta_bg; | |
2343 | else | |
2344 | old_desc_blocks = fs->desc_blocks + | |
2345 | fs->super->s_reserved_gdt_blocks; | |
199ddaaa JB |
2346 | |
2347 | /* calculate how many blocks are needed for data */ | |
4efbac6f VAH |
2348 | data_needed = ext2fs_blocks_count(fs->super) - |
2349 | ext2fs_free_blocks_count(fs->super); | |
199ddaaa | 2350 | |
7f820344 TT |
2351 | for (grp = 0; grp < fs->group_desc_count; grp++) |
2352 | data_needed -= calc_group_overhead(fs, grp, old_desc_blocks); | |
e231f175 TT |
2353 | #ifdef RESIZE2FS_DEBUG |
2354 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2355 | printf("fs requires %llu data blocks.\n", data_needed); | |
2356 | #endif | |
7f820344 TT |
2357 | |
2358 | /* | |
2359 | * For ext4 we need to allow for up to a flex_bg worth of | |
2360 | * inode tables of slack space so the resize operation can be | |
2361 | * guaranteed to finish. | |
2362 | */ | |
45a78b88 | 2363 | flex_groups = groups; |
c09043f1 | 2364 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) { |
45a78b88 TT |
2365 | dgrp_t remainder = groups & (flexbg_size - 1); |
2366 | ||
2367 | flex_groups += flexbg_size - remainder; | |
2368 | if (flex_groups > fs->group_desc_count) | |
2369 | flex_groups = fs->group_desc_count; | |
c09043f1 TT |
2370 | } |
2371 | ||
199ddaaa JB |
2372 | /* |
2373 | * figure out how many data blocks we have given the number of groups | |
2374 | * we need for our inodes | |
2375 | */ | |
1e33a8b4 | 2376 | data_blocks = EXT2_GROUPS_TO_BLOCKS(fs->super, groups); |
199ddaaa | 2377 | last_start = 0; |
45a78b88 | 2378 | for (grp = 0; grp < flex_groups; grp++) { |
7f820344 | 2379 | overhead = calc_group_overhead(fs, grp, old_desc_blocks); |
199ddaaa JB |
2380 | |
2381 | /* | |
2382 | * we want to keep track of how much data we can store in | |
2383 | * the groups leading up to the last group so we can determine | |
2384 | * how big the last group needs to be | |
2385 | */ | |
45a78b88 | 2386 | if (grp < (groups - 1)) |
199ddaaa JB |
2387 | last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - |
2388 | overhead; | |
2389 | ||
45a78b88 TT |
2390 | if (data_blocks > overhead) |
2391 | data_blocks -= overhead; | |
2392 | else | |
2393 | data_blocks = 0; | |
199ddaaa | 2394 | } |
e231f175 TT |
2395 | #ifdef RESIZE2FS_DEBUG |
2396 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2397 | printf("With %d group(s), we have %llu blocks available.\n", | |
2398 | groups, data_blocks); | |
2399 | #endif | |
199ddaaa JB |
2400 | |
2401 | /* | |
2402 | * if we need more group descriptors in order to accomodate our data | |
2403 | * then we need to add them here | |
2404 | */ | |
45a78b88 | 2405 | blks_needed = data_needed; |
b4f30fcf TT |
2406 | while (blks_needed > data_blocks) { |
2407 | blk64_t remainder = blks_needed - data_blocks; | |
e231f175 | 2408 | dgrp_t extra_grps; |
199ddaaa JB |
2409 | |
2410 | /* figure out how many more groups we need for the data */ | |
8728d506 VAH |
2411 | extra_grps = ext2fs_div64_ceil(remainder, |
2412 | EXT2_BLOCKS_PER_GROUP(fs->super)); | |
199ddaaa | 2413 | |
1e33a8b4 | 2414 | data_blocks += EXT2_GROUPS_TO_BLOCKS(fs->super, extra_grps); |
199ddaaa JB |
2415 | |
2416 | /* ok we have to account for the last group */ | |
7f820344 | 2417 | overhead = calc_group_overhead(fs, groups-1, old_desc_blocks); |
199ddaaa JB |
2418 | last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead; |
2419 | ||
45a78b88 TT |
2420 | grp = flex_groups; |
2421 | groups += extra_grps; | |
2422 | if (!(fs->super->s_feature_incompat & | |
2423 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) | |
2424 | flex_groups = groups; | |
2425 | else if (groups > flex_groups) { | |
2426 | dgrp_t r = groups & (flexbg_size - 1); | |
2427 | ||
2428 | flex_groups = groups + flexbg_size - r; | |
2429 | if (flex_groups > fs->group_desc_count) | |
2430 | flex_groups = fs->group_desc_count; | |
2431 | } | |
2432 | ||
2433 | for (; grp < flex_groups; grp++) { | |
7f820344 TT |
2434 | overhead = calc_group_overhead(fs, grp, |
2435 | old_desc_blocks); | |
199ddaaa JB |
2436 | |
2437 | /* | |
2438 | * again, we need to see how much data we cram into | |
2439 | * all of the groups leading up to the last group | |
2440 | */ | |
45a78b88 | 2441 | if (grp < groups - 1) |
199ddaaa JB |
2442 | last_start += EXT2_BLOCKS_PER_GROUP(fs->super) |
2443 | - overhead; | |
2444 | ||
2445 | data_blocks -= overhead; | |
2446 | } | |
2447 | ||
e231f175 TT |
2448 | #ifdef RESIZE2FS_DEBUG |
2449 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2450 | printf("Added %d extra group(s), " | |
b4f30fcf TT |
2451 | "blks_needed %llu, data_blocks %llu, " |
2452 | "last_start %llu\n", extra_grps, blks_needed, | |
2453 | data_blocks, last_start); | |
e231f175 | 2454 | #endif |
199ddaaa JB |
2455 | } |
2456 | ||
2457 | /* now for the fun voodoo */ | |
45a78b88 TT |
2458 | grp = groups - 1; |
2459 | if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) && | |
2460 | (grp & ~(flexbg_size - 1)) == 0) | |
2461 | grp = grp & ~(flexbg_size - 1); | |
2462 | overhead = 0; | |
2463 | for (; grp < flex_groups; grp++) | |
2464 | overhead += calc_group_overhead(fs, grp, old_desc_blocks); | |
2465 | ||
e231f175 TT |
2466 | #ifdef RESIZE2FS_DEBUG |
2467 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2468 | printf("Last group's overhead is %llu\n", overhead); | |
2469 | #endif | |
199ddaaa JB |
2470 | |
2471 | /* | |
2472 | * if this is the case then the last group is going to have data in it | |
2473 | * so we need to adjust the size of the last group accordingly | |
2474 | */ | |
b4f30fcf TT |
2475 | if (last_start < blks_needed) { |
2476 | blk64_t remainder = blks_needed - last_start; | |
199ddaaa | 2477 | |
e231f175 TT |
2478 | #ifdef RESIZE2FS_DEBUG |
2479 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2480 | printf("Need %llu data blocks in last group\n", | |
2481 | remainder); | |
2482 | #endif | |
199ddaaa JB |
2483 | /* |
2484 | * 50 is a magic number that mkfs/resize uses to see if its | |
2485 | * even worth making/resizing the fs. basically you need to | |
2486 | * have at least 50 blocks in addition to the blocks needed | |
2487 | * for the metadata in the last group | |
2488 | */ | |
2489 | if (remainder > 50) | |
2490 | overhead += remainder; | |
2491 | else | |
2492 | overhead += 50; | |
2493 | } else | |
2494 | overhead += 50; | |
2495 | ||
ba8bfa1a | 2496 | overhead += fs->super->s_first_data_block; |
e231f175 TT |
2497 | #ifdef RESIZE2FS_DEBUG |
2498 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2499 | printf("Final size of last group is %lld\n", overhead); | |
2500 | #endif | |
199ddaaa | 2501 | |
45a78b88 TT |
2502 | /* Add extra slack for bigalloc file systems */ |
2503 | if (EXT2FS_CLUSTER_RATIO(fs) > 1) | |
2504 | overhead += EXT2FS_CLUSTER_RATIO(fs) * 2; | |
2505 | ||
199ddaaa | 2506 | /* |
45a78b88 TT |
2507 | * since our last group doesn't have to be BLOCKS_PER_GROUP |
2508 | * large, we only do groups-1, and then add the number of | |
2509 | * blocks needed to handle the group descriptor metadata+data | |
2510 | * that we need | |
199ddaaa | 2511 | */ |
1e33a8b4 | 2512 | blks_needed = EXT2_GROUPS_TO_BLOCKS(fs->super, groups - 1); |
199ddaaa JB |
2513 | blks_needed += overhead; |
2514 | ||
2215293c TT |
2515 | /* |
2516 | * Make sure blks_needed covers the end of the inode table in | |
2517 | * the last block group. | |
2518 | */ | |
2519 | overhead = ext2fs_inode_table_loc(fs, groups-1) + | |
2520 | fs->inode_blocks_per_group; | |
2521 | if (blks_needed < overhead) | |
2522 | blks_needed = overhead; | |
2523 | ||
e231f175 TT |
2524 | #ifdef RESIZE2FS_DEBUG |
2525 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2526 | printf("Estimated blocks needed: %llu\n", blks_needed); | |
2527 | #endif | |
2528 | ||
69f7c80e ES |
2529 | /* |
2530 | * If at this point we've already added up more "needed" than | |
2531 | * the current size, just return current size as minimum. | |
2532 | */ | |
4efbac6f VAH |
2533 | if (blks_needed >= ext2fs_blocks_count(fs->super)) |
2534 | return ext2fs_blocks_count(fs->super); | |
793a04a0 TT |
2535 | /* |
2536 | * We need to reserve a few extra blocks if extents are | |
2537 | * enabled, in case we need to grow the extent tree. The more | |
2538 | * we shrink the file system, the more space we need. | |
b4f30fcf TT |
2539 | * |
2540 | * The absolute worst case is every single data block is in | |
2541 | * the part of the file system that needs to be evacuated, | |
2542 | * with each data block needs to be in its own extent, and | |
2543 | * with each inode needing at least one extent block. | |
793a04a0 | 2544 | */ |
e231f175 TT |
2545 | if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) { |
2546 | blk64_t safe_margin = (ext2fs_blocks_count(fs->super) - | |
2547 | blks_needed)/500; | |
b4f30fcf TT |
2548 | unsigned int exts_per_blk = (fs->blocksize / |
2549 | sizeof(struct ext3_extent)) - 1; | |
2550 | blk64_t worst_case = ((data_needed + exts_per_blk - 1) / | |
2551 | exts_per_blk); | |
2552 | ||
2553 | if (worst_case < inode_count) | |
2554 | worst_case = inode_count; | |
2555 | ||
2556 | if (safe_margin > worst_case) | |
2557 | safe_margin = worst_case; | |
2558 | ||
e231f175 TT |
2559 | #ifdef RESIZE2FS_DEBUG |
2560 | if (flags & RESIZE_DEBUG_MIN_CALC) | |
2561 | printf("Extents safety margin: %llu\n", safe_margin); | |
2562 | #endif | |
2563 | blks_needed += safe_margin; | |
2564 | } | |
793a04a0 | 2565 | |
199ddaaa JB |
2566 | return blks_needed; |
2567 | } |