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