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