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