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