]>
Commit | Line | Data |
---|---|---|
24b2c7a7 TT |
1 | /* |
2 | * resize2fs.c --- ext2 main routine | |
3 | * | |
0cee8a5c TT |
4 | * Copyright (C) 1997, 1998 by Theodore Ts'o and |
5 | * PowerQuest, Inc. | |
6 | * | |
7 | * Copyright (C) 1999, 2000 by Theosore Ts'o | |
efc6f628 | 8 | * |
24b2c7a7 | 9 | * %Begin-Header% |
0cee8a5c TT |
10 | * This file may be redistributed under the terms of the GNU Public |
11 | * License. | |
24b2c7a7 TT |
12 | * %End-Header% |
13 | */ | |
14 | ||
05e112a1 TT |
15 | /* |
16 | * Resizing a filesystem consists of the following phases: | |
17 | * | |
a8519a2d | 18 | * 1. Adjust superblock and write out new parts of the inode |
05e112a1 | 19 | * table |
a8519a2d TT |
20 | * 2. Determine blocks which need to be relocated, and copy the |
21 | * contents of blocks from their old locations to the new ones. | |
22 | * 3. Scan the inode table, doing the following: | |
23 | * a. If blocks have been moved, update the block | |
24 | * pointers in the inodes and indirect blocks to | |
25 | * point at the new block locations. | |
26 | * b. If parts of the inode table need to be evacuated, | |
27 | * copy inodes from their old locations to their | |
28 | * new ones. | |
29 | * c. If (b) needs to be done, note which blocks contain | |
30 | * directory information, since we will need to | |
31 | * update the directory information. | |
32 | * 4. Update the directory blocks with the new inode locations. | |
33 | * 5. Move the inode tables, if necessary. | |
05e112a1 | 34 | */ |
a8519a2d | 35 | |
d1154eb4 | 36 | #include "config.h" |
24b2c7a7 | 37 | #include "resize2fs.h" |
b969b1b8 | 38 | #include <time.h> |
24b2c7a7 | 39 | |
546a1ff1 | 40 | #ifdef __linux__ /* Kludge for debugging */ |
a8519a2d TT |
41 | #define RESIZE2FS_DEBUG |
42 | #endif | |
43 | ||
86acdebd | 44 | static void fix_uninit_block_bitmaps(ext2_filsys fs); |
8728d506 | 45 | static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size); |
a8519a2d TT |
46 | static errcode_t blocks_to_move(ext2_resize_t rfs); |
47 | static errcode_t block_mover(ext2_resize_t rfs); | |
48 | static errcode_t inode_scan_and_fix(ext2_resize_t rfs); | |
49 | static errcode_t inode_ref_fix(ext2_resize_t rfs); | |
50 | static errcode_t move_itables(ext2_resize_t rfs); | |
9213a93b | 51 | static errcode_t fix_resize_inode(ext2_filsys fs); |
a8519a2d | 52 | static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs); |
8a6ede8b | 53 | static errcode_t fix_sb_journal_backup(ext2_filsys fs); |
a8519a2d TT |
54 | |
55 | /* | |
56 | * Some helper CPP macros | |
57 | */ | |
d7cca6b0 VAH |
58 | #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i))) |
59 | #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i))) | |
a8519a2d | 60 | |
d7cca6b0 VAH |
61 | #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \ |
62 | ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \ | |
a8519a2d TT |
63 | (fs)->inode_blocks_per_group))) |
64 | ||
199ddaaa JB |
65 | #define META_OVERHEAD(fs) (2 + (fs)->inode_blocks_per_group) |
66 | #define SUPER_OVERHEAD(fs) (1 + (fs)->desc_blocks +\ | |
67 | (fs)->super->s_reserved_gdt_blocks) | |
a8519a2d TT |
68 | |
69 | /* | |
70 | * This is the top-level routine which does the dirty deed.... | |
71 | */ | |
8728d506 | 72 | errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags, |
3346084b | 73 | errcode_t (*progress)(ext2_resize_t rfs, int pass, |
86acdebd TT |
74 | unsigned long cur, |
75 | unsigned long max_val)) | |
a8519a2d TT |
76 | { |
77 | ext2_resize_t rfs; | |
78 | errcode_t retval; | |
79 | ||
80 | retval = ext2fs_read_bitmaps(fs); | |
81 | if (retval) | |
82 | return retval; | |
efc6f628 | 83 | |
96cdb37e TT |
84 | fs->super->s_state |= EXT2_ERROR_FS; |
85 | ext2fs_mark_super_dirty(fs); | |
86 | ext2fs_flush(fs); | |
87 | ||
a8519a2d TT |
88 | /* |
89 | * Create the data structure | |
90 | */ | |
c4e3d3f3 | 91 | retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs); |
a8519a2d TT |
92 | if (retval) |
93 | return retval; | |
94 | memset(rfs, 0, sizeof(struct ext2_resize_struct)); | |
95 | ||
86acdebd | 96 | fix_uninit_block_bitmaps(fs); |
7f9c96ee | 97 | fs->priv_data = rfs; |
a8519a2d TT |
98 | rfs->old_fs = fs; |
99 | rfs->flags = flags; | |
100 | rfs->itable_buf = 0; | |
101 | rfs->progress = progress; | |
102 | retval = ext2fs_dup_handle(fs, &rfs->new_fs); | |
103 | if (retval) | |
104 | goto errout; | |
105 | ||
116db1b5 | 106 | retval = adjust_superblock(rfs, *new_size); |
a8519a2d TT |
107 | if (retval) |
108 | goto errout; | |
109 | ||
86acdebd TT |
110 | fix_uninit_block_bitmaps(rfs->new_fs); |
111 | /* Clear the block bitmap uninit flag for the last block group */ | |
e633b58a | 112 | ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1, |
732c8cd5 | 113 | EXT2_BG_BLOCK_UNINIT); |
86acdebd | 114 | |
4efbac6f | 115 | *new_size = ext2fs_blocks_count(rfs->new_fs->super); |
116db1b5 | 116 | |
a8519a2d TT |
117 | retval = blocks_to_move(rfs); |
118 | if (retval) | |
119 | goto errout; | |
120 | ||
121 | #ifdef RESIZE2FS_DEBUG | |
122 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
8728d506 | 123 | printf("Number of free blocks: %llu/%llu, Needed: %llu\n", |
4efbac6f VAH |
124 | ext2fs_free_blocks_count(rfs->old_fs->super), |
125 | ext2fs_free_blocks_count(rfs->new_fs->super), | |
a8519a2d TT |
126 | rfs->needed_blocks); |
127 | #endif | |
efc6f628 | 128 | |
a8519a2d TT |
129 | retval = block_mover(rfs); |
130 | if (retval) | |
131 | goto errout; | |
132 | ||
133 | retval = inode_scan_and_fix(rfs); | |
134 | if (retval) | |
135 | goto errout; | |
136 | ||
137 | retval = inode_ref_fix(rfs); | |
138 | if (retval) | |
139 | goto errout; | |
140 | ||
a8519a2d TT |
141 | retval = move_itables(rfs); |
142 | if (retval) | |
143 | goto errout; | |
144 | ||
64ad98ac TT |
145 | retval = ext2fs_calculate_summary_stats(rfs->new_fs); |
146 | if (retval) | |
147 | goto errout; | |
efc6f628 | 148 | |
9213a93b TT |
149 | retval = fix_resize_inode(rfs->new_fs); |
150 | if (retval) | |
151 | goto errout; | |
152 | ||
8a6ede8b ES |
153 | retval = fix_sb_journal_backup(rfs->new_fs); |
154 | if (retval) | |
155 | goto errout; | |
156 | ||
96cdb37e | 157 | rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS; |
efc6f628 | 158 | rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
a8519a2d TT |
159 | retval = ext2fs_close(rfs->new_fs); |
160 | if (retval) | |
161 | goto errout; | |
162 | ||
163 | rfs->flags = flags; | |
efc6f628 | 164 | |
a8519a2d TT |
165 | ext2fs_free(rfs->old_fs); |
166 | if (rfs->itable_buf) | |
c4e3d3f3 | 167 | ext2fs_free_mem(&rfs->itable_buf); |
e2ca097f TT |
168 | if (rfs->reserve_blocks) |
169 | ext2fs_free_block_bitmap(rfs->reserve_blocks); | |
170 | if (rfs->move_blocks) | |
171 | ext2fs_free_block_bitmap(rfs->move_blocks); | |
c4e3d3f3 | 172 | ext2fs_free_mem(&rfs); |
efc6f628 | 173 | |
a8519a2d TT |
174 | return 0; |
175 | ||
176 | errout: | |
177 | if (rfs->new_fs) | |
178 | ext2fs_free(rfs->new_fs); | |
179 | if (rfs->itable_buf) | |
c4e3d3f3 TT |
180 | ext2fs_free_mem(&rfs->itable_buf); |
181 | ext2fs_free_mem(&rfs); | |
a8519a2d TT |
182 | return retval; |
183 | } | |
184 | ||
86acdebd TT |
185 | /* |
186 | * Clean up the bitmaps for unitialized bitmaps | |
187 | */ | |
188 | static void fix_uninit_block_bitmaps(ext2_filsys fs) | |
189 | { | |
8728d506 | 190 | blk64_t i, blk, super_blk, old_desc_blk, new_desc_blk; |
86acdebd TT |
191 | int old_desc_blocks; |
192 | dgrp_t g; | |
193 | ||
194 | if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super, | |
195 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM))) | |
196 | return; | |
197 | ||
198 | for (g=0; g < fs->group_desc_count; g++) { | |
cd65a24e | 199 | if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT))) |
86acdebd TT |
200 | continue; |
201 | ||
202 | blk = (g * fs->super->s_blocks_per_group) + | |
203 | fs->super->s_first_data_block; | |
204 | ||
8728d506 VAH |
205 | ext2fs_super_and_bgd_loc2(fs, g, &super_blk, |
206 | &old_desc_blk, &new_desc_blk, 0); | |
86acdebd TT |
207 | |
208 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) | |
209 | old_desc_blocks = fs->super->s_first_meta_bg; | |
210 | else | |
efc6f628 | 211 | old_desc_blocks = fs->desc_blocks + |
86acdebd TT |
212 | fs->super->s_reserved_gdt_blocks; |
213 | ||
214 | for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) { | |
4efbac6f | 215 | if (blk >= ext2fs_blocks_count(fs->super)) |
86acdebd TT |
216 | break; |
217 | if ((blk == super_blk) || | |
218 | (old_desc_blk && old_desc_blocks && | |
219 | (blk >= old_desc_blk) && | |
220 | (blk < old_desc_blk + old_desc_blocks)) || | |
221 | (new_desc_blk && (blk == new_desc_blk)) || | |
d7cca6b0 VAH |
222 | (blk == ext2fs_block_bitmap_loc(fs, g)) || |
223 | (blk == ext2fs_inode_bitmap_loc(fs, g)) || | |
224 | (blk >= ext2fs_inode_table_loc(fs, g) && | |
225 | (blk < ext2fs_inode_table_loc(fs, g) | |
86acdebd | 226 | + fs->inode_blocks_per_group))) |
3346084b | 227 | ext2fs_fast_mark_block_bitmap2(fs->block_map, blk); |
86acdebd | 228 | else |
3346084b | 229 | ext2fs_fast_unmark_block_bitmap2(fs->block_map, blk); |
86acdebd TT |
230 | } |
231 | } | |
232 | } | |
233 | ||
a8519a2d TT |
234 | /* -------------------------------------------------------------------- |
235 | * | |
236 | * Resize processing, phase 1. | |
237 | * | |
238 | * In this phase we adjust the in-memory superblock information, and | |
239 | * initialize any new parts of the inode table. The new parts of the | |
240 | * inode table are created in virgin disk space, so we can abort here | |
241 | * without any side effects. | |
242 | * -------------------------------------------------------------------- | |
243 | */ | |
244 | ||
9227c5bb TT |
245 | /* |
246 | * If the group descriptor's bitmap and inode table blocks are valid, | |
c09043f1 TT |
247 | * release them in the new filesystem data structure, and mark them as |
248 | * reserved so the old inode table blocks don't get overwritten. | |
9227c5bb | 249 | */ |
c09043f1 TT |
250 | static void free_gdp_blocks(ext2_filsys fs, |
251 | ext2fs_block_bitmap reserve_blocks, | |
252 | struct ext2_group_desc *gdp) | |
9227c5bb TT |
253 | { |
254 | blk_t blk; | |
255 | int j; | |
256 | ||
257 | if (gdp->bg_block_bitmap && | |
4efbac6f | 258 | (gdp->bg_block_bitmap < ext2fs_blocks_count(fs->super))) { |
9227c5bb | 259 | ext2fs_block_alloc_stats(fs, gdp->bg_block_bitmap, -1); |
3346084b | 260 | ext2fs_mark_block_bitmap2(reserve_blocks, |
c09043f1 TT |
261 | gdp->bg_block_bitmap); |
262 | } | |
9227c5bb TT |
263 | |
264 | if (gdp->bg_inode_bitmap && | |
4efbac6f | 265 | (gdp->bg_inode_bitmap < ext2fs_blocks_count(fs->super))) { |
9227c5bb | 266 | ext2fs_block_alloc_stats(fs, gdp->bg_inode_bitmap, -1); |
3346084b | 267 | ext2fs_mark_block_bitmap2(reserve_blocks, |
c09043f1 TT |
268 | gdp->bg_inode_bitmap); |
269 | } | |
9227c5bb TT |
270 | |
271 | if (gdp->bg_inode_table == 0 || | |
4efbac6f | 272 | (gdp->bg_inode_table >= ext2fs_blocks_count(fs->super))) |
9227c5bb TT |
273 | return; |
274 | ||
275 | for (blk = gdp->bg_inode_table, j = 0; | |
276 | j < fs->inode_blocks_per_group; j++, blk++) { | |
4efbac6f | 277 | if (blk >= ext2fs_blocks_count(fs->super)) |
9227c5bb TT |
278 | break; |
279 | ext2fs_block_alloc_stats(fs, blk, -1); | |
3346084b | 280 | ext2fs_mark_block_bitmap2(reserve_blocks, blk); |
9227c5bb TT |
281 | } |
282 | } | |
283 | ||
24b2c7a7 | 284 | /* |
bf69235a TT |
285 | * This routine is shared by the online and offline resize routines. |
286 | * All of the information which is adjusted in memory is done here. | |
c09043f1 TT |
287 | * |
288 | * The reserve_blocks parameter is only needed when shrinking the | |
289 | * filesystem. | |
24b2c7a7 | 290 | */ |
c09043f1 | 291 | errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, |
8728d506 | 292 | ext2fs_block_bitmap reserve_blocks, blk64_t new_size) |
24b2c7a7 | 293 | { |
24b2c7a7 | 294 | errcode_t retval; |
8728d506 VAH |
295 | blk64_t overhead = 0; |
296 | blk64_t rem; | |
297 | blk64_t blk, group_block; | |
2696f250 | 298 | blk64_t real_end; |
8728d506 | 299 | blk64_t adj, old_numblocks, numblocks, adjblocks; |
bf69235a | 300 | unsigned long i, j, old_desc_blocks, max_group; |
54434927 | 301 | unsigned int meta_bg, meta_bg_size; |
74128f8d | 302 | int has_super, csum_flag; |
de8f3a76 | 303 | unsigned long long new_inodes; /* u64 to check for overflow */ |
9ff8ece5 | 304 | double percent; |
bf69235a | 305 | |
4efbac6f | 306 | ext2fs_blocks_count_set(fs->super, new_size); |
24b2c7a7 TT |
307 | |
308 | retry: | |
4efbac6f | 309 | fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) - |
69022e02 TT |
310 | fs->super->s_first_data_block, |
311 | EXT2_BLOCKS_PER_GROUP(fs->super)); | |
24b2c7a7 TT |
312 | if (fs->group_desc_count == 0) |
313 | return EXT2_ET_TOOSMALL; | |
efc6f628 | 314 | fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count, |
69022e02 | 315 | EXT2_DESC_PER_BLOCK(fs->super)); |
24b2c7a7 TT |
316 | |
317 | /* | |
318 | * Overhead is the number of bookkeeping blocks per group. It | |
319 | * includes the superblock backup, the group descriptor | |
320 | * backups, the inode bitmap, the block bitmap, and the inode | |
321 | * table. | |
24b2c7a7 | 322 | */ |
9213a93b TT |
323 | overhead = (int) (2 + fs->inode_blocks_per_group); |
324 | ||
325 | if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1)) | |
efc6f628 | 326 | overhead += 1 + fs->desc_blocks + |
9213a93b TT |
327 | fs->super->s_reserved_gdt_blocks; |
328 | ||
24b2c7a7 TT |
329 | /* |
330 | * See if the last group is big enough to support the | |
331 | * necessary data structures. If not, we need to get rid of | |
332 | * it. | |
333 | */ | |
4efbac6f | 334 | rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) % |
24b2c7a7 TT |
335 | fs->super->s_blocks_per_group; |
336 | if ((fs->group_desc_count == 1) && rem && (rem < overhead)) | |
337 | return EXT2_ET_TOOSMALL; | |
515e555a | 338 | if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) { |
4efbac6f VAH |
339 | ext2fs_blocks_count_set(fs->super, |
340 | ext2fs_blocks_count(fs->super) - rem); | |
24b2c7a7 TT |
341 | goto retry; |
342 | } | |
343 | /* | |
344 | * Adjust the number of inodes | |
345 | */ | |
de8f3a76 | 346 | new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count; |
f3358643 ES |
347 | if (new_inodes > ~0U) { |
348 | fprintf(stderr, _("inodes (%llu) must be less than %u"), | |
349 | new_inodes, ~0U); | |
350 | return EXT2_ET_TOO_MANY_INODES; | |
351 | } | |
24b2c7a7 TT |
352 | fs->super->s_inodes_count = fs->super->s_inodes_per_group * |
353 | fs->group_desc_count; | |
354 | ||
355 | /* | |
356 | * Adjust the number of free blocks | |
357 | */ | |
4efbac6f VAH |
358 | blk = ext2fs_blocks_count(old_fs->super); |
359 | if (blk > ext2fs_blocks_count(fs->super)) | |
360 | ext2fs_free_blocks_count_set(fs->super, | |
361 | ext2fs_free_blocks_count(fs->super) - | |
362 | (blk - ext2fs_blocks_count(fs->super))); | |
24b2c7a7 | 363 | else |
4efbac6f VAH |
364 | ext2fs_free_blocks_count_set(fs->super, |
365 | ext2fs_free_blocks_count(fs->super) + | |
366 | (ext2fs_blocks_count(fs->super) - blk)); | |
24b2c7a7 | 367 | |
c762c8e6 TT |
368 | /* |
369 | * Adjust the number of reserved blocks | |
370 | */ | |
4efbac6f VAH |
371 | percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) / |
372 | ext2fs_blocks_count(old_fs->super); | |
373 | ext2fs_r_blocks_count_set(fs->super, | |
374 | (percent * ext2fs_blocks_count(fs->super) / | |
375 | 100.0)); | |
c762c8e6 | 376 | |
24b2c7a7 TT |
377 | /* |
378 | * Adjust the bitmaps for size | |
379 | */ | |
3346084b | 380 | retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count, |
24b2c7a7 TT |
381 | fs->super->s_inodes_count, |
382 | fs->inode_map); | |
c762c8e6 | 383 | if (retval) goto errout; |
efc6f628 | 384 | |
2696f250 TT |
385 | real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) * |
386 | fs->group_desc_count)) - 1 + | |
387 | fs->super->s_first_data_block; | |
4efbac6f | 388 | retval = ext2fs_resize_block_bitmap2(ext2fs_blocks_count(fs->super)-1, |
24b2c7a7 TT |
389 | real_end, fs->block_map); |
390 | ||
c762c8e6 | 391 | if (retval) goto errout; |
24b2c7a7 TT |
392 | |
393 | /* | |
394 | * Reallocate the group descriptors as necessary. | |
395 | */ | |
bf69235a TT |
396 | if (old_fs->desc_blocks != fs->desc_blocks) { |
397 | retval = ext2fs_resize_mem(old_fs->desc_blocks * | |
76f875da TT |
398 | fs->blocksize, |
399 | fs->desc_blocks * fs->blocksize, | |
c4e3d3f3 | 400 | &fs->group_desc); |
ca8abba7 | 401 | if (retval) |
a8519a2d | 402 | goto errout; |
efc6f628 TT |
403 | if (fs->desc_blocks > old_fs->desc_blocks) |
404 | memset((char *) fs->group_desc + | |
bf69235a TT |
405 | (old_fs->desc_blocks * fs->blocksize), 0, |
406 | (fs->desc_blocks - old_fs->desc_blocks) * | |
2787276e | 407 | fs->blocksize); |
24b2c7a7 | 408 | } |
1e1da29f | 409 | |
9213a93b TT |
410 | /* |
411 | * If the resize_inode feature is set, and we are changing the | |
412 | * number of descriptor blocks, then adjust | |
413 | * s_reserved_gdt_blocks if possible to avoid needing to move | |
414 | * the inode table either now or in the future. | |
415 | */ | |
efc6f628 | 416 | if ((fs->super->s_feature_compat & |
9213a93b | 417 | EXT2_FEATURE_COMPAT_RESIZE_INODE) && |
bf69235a | 418 | (old_fs->desc_blocks != fs->desc_blocks)) { |
9213a93b TT |
419 | int new; |
420 | ||
efc6f628 | 421 | new = ((int) fs->super->s_reserved_gdt_blocks) + |
bf69235a | 422 | (old_fs->desc_blocks - fs->desc_blocks); |
9213a93b TT |
423 | if (new < 0) |
424 | new = 0; | |
de8f3a76 | 425 | if (new > (int) fs->blocksize/4) |
9213a93b TT |
426 | new = fs->blocksize/4; |
427 | fs->super->s_reserved_gdt_blocks = new; | |
9213a93b TT |
428 | } |
429 | ||
a8519a2d | 430 | /* |
c09043f1 TT |
431 | * If we are shrinking the number of block groups, we're done |
432 | * and can exit now. | |
1e1da29f | 433 | */ |
bf69235a | 434 | if (old_fs->group_desc_count > fs->group_desc_count) { |
9227c5bb TT |
435 | /* |
436 | * Check the block groups that we are chopping off | |
437 | * and free any blocks associated with their metadata | |
438 | */ | |
439 | for (i = fs->group_desc_count; | |
440 | i < old_fs->group_desc_count; i++) { | |
c09043f1 | 441 | free_gdp_blocks(fs, reserve_blocks, |
d7cca6b0 VAH |
442 | ext2fs_group_desc(old_fs, |
443 | old_fs->group_desc, i)); | |
9227c5bb | 444 | } |
c762c8e6 TT |
445 | retval = 0; |
446 | goto errout; | |
447 | } | |
bf69235a | 448 | |
a8519a2d TT |
449 | /* |
450 | * Fix the count of the last (old) block group | |
451 | */ | |
4efbac6f | 452 | old_numblocks = (ext2fs_blocks_count(old_fs->super) - |
bf69235a TT |
453 | old_fs->super->s_first_data_block) % |
454 | old_fs->super->s_blocks_per_group; | |
1e1da29f | 455 | if (!old_numblocks) |
bf69235a TT |
456 | old_numblocks = old_fs->super->s_blocks_per_group; |
457 | if (old_fs->group_desc_count == fs->group_desc_count) { | |
4efbac6f | 458 | numblocks = (ext2fs_blocks_count(fs->super) - |
bf69235a TT |
459 | fs->super->s_first_data_block) % |
460 | fs->super->s_blocks_per_group; | |
1e1da29f | 461 | if (!numblocks) |
bf69235a | 462 | numblocks = fs->super->s_blocks_per_group; |
1e1da29f | 463 | } else |
bf69235a TT |
464 | numblocks = fs->super->s_blocks_per_group; |
465 | i = old_fs->group_desc_count - 1; | |
d7cca6b0 | 466 | ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks)); |
236efede JS |
467 | ext2fs_group_desc_csum_set(fs, i); |
468 | ||
1e1da29f | 469 | /* |
a8519a2d TT |
470 | * If the number of block groups is staying the same, we're |
471 | * done and can exit now. (If the number block groups is | |
472 | * shrinking, we had exited earlier.) | |
1e1da29f | 473 | */ |
bf69235a | 474 | if (old_fs->group_desc_count >= fs->group_desc_count) { |
c762c8e6 TT |
475 | retval = 0; |
476 | goto errout; | |
477 | } | |
bf69235a | 478 | |
a8519a2d TT |
479 | /* |
480 | * Initialize the new block group descriptors | |
481 | */ | |
1e1da29f | 482 | group_block = fs->super->s_first_data_block + |
bf69235a | 483 | old_fs->group_desc_count * fs->super->s_blocks_per_group; |
c762c8e6 | 484 | |
74128f8d TT |
485 | csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
486 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM); | |
bf69235a | 487 | adj = old_fs->group_desc_count; |
63b44fbe | 488 | max_group = fs->group_desc_count - adj; |
76dd5e5c TT |
489 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
490 | old_desc_blocks = fs->super->s_first_meta_bg; | |
491 | else | |
efc6f628 | 492 | old_desc_blocks = fs->desc_blocks + |
9213a93b | 493 | fs->super->s_reserved_gdt_blocks; |
bf69235a | 494 | for (i = old_fs->group_desc_count; |
1e1da29f | 495 | i < fs->group_desc_count; i++) { |
d7cca6b0 | 496 | memset(ext2fs_group_desc(fs, fs->group_desc, i), 0, |
1e1da29f TT |
497 | sizeof(struct ext2_group_desc)); |
498 | adjblocks = 0; | |
499 | ||
e633b58a | 500 | ext2fs_bg_flags_zap(fs, i); |
74128f8d | 501 | if (csum_flag) |
e633b58a | 502 | ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT | EXT2_BG_INODE_ZEROED); |
98f45471 ES |
503 | |
504 | numblocks = ext2fs_group_blocks_count(fs, i); | |
505 | if ((i < fs->group_desc_count - 1) && csum_flag) | |
506 | ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT); | |
1e1da29f | 507 | |
76dd5e5c TT |
508 | has_super = ext2fs_bg_has_super(fs, i); |
509 | if (has_super) { | |
48f23054 | 510 | ext2fs_block_alloc_stats2(fs, group_block, +1); |
76dd5e5c TT |
511 | adjblocks++; |
512 | } | |
f2de1d38 | 513 | meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super); |
76dd5e5c TT |
514 | meta_bg = i / meta_bg_size; |
515 | if (!(fs->super->s_feature_incompat & | |
516 | EXT2_FEATURE_INCOMPAT_META_BG) || | |
517 | (meta_bg < fs->super->s_first_meta_bg)) { | |
518 | if (has_super) { | |
519 | for (j=0; j < old_desc_blocks; j++) | |
48f23054 | 520 | ext2fs_block_alloc_stats2(fs, |
74128f8d | 521 | group_block + 1 + j, +1); |
76dd5e5c TT |
522 | adjblocks += old_desc_blocks; |
523 | } | |
524 | } else { | |
525 | if (has_super) | |
526 | has_super = 1; | |
527 | if (((i % meta_bg_size) == 0) || | |
528 | ((i % meta_bg_size) == 1) || | |
529 | ((i % meta_bg_size) == (meta_bg_size-1))) | |
48f23054 | 530 | ext2fs_block_alloc_stats2(fs, |
74128f8d | 531 | group_block + has_super, +1); |
24b2c7a7 | 532 | } |
efc6f628 | 533 | |
1e1da29f | 534 | adjblocks += 2 + fs->inode_blocks_per_group; |
efc6f628 | 535 | |
1e1da29f | 536 | numblocks -= adjblocks; |
4efbac6f VAH |
537 | ext2fs_free_blocks_count_set(fs->super, |
538 | ext2fs_free_blocks_count(fs->super) - adjblocks); | |
1e1da29f TT |
539 | fs->super->s_free_inodes_count += |
540 | fs->super->s_inodes_per_group; | |
d7cca6b0 VAH |
541 | ext2fs_bg_free_blocks_count_set(fs, i, numblocks); |
542 | ext2fs_bg_free_inodes_count_set(fs, i, | |
543 | fs->super->s_inodes_per_group); | |
544 | ext2fs_bg_used_dirs_count_set(fs, i, 0); | |
236efede | 545 | ext2fs_group_desc_csum_set(fs, i); |
24b2c7a7 | 546 | |
1e1da29f | 547 | retval = ext2fs_allocate_group_table(fs, i, 0); |
c762c8e6 | 548 | if (retval) goto errout; |
24b2c7a7 | 549 | |
bf69235a TT |
550 | group_block += fs->super->s_blocks_per_group; |
551 | } | |
552 | retval = 0; | |
553 | ||
554 | errout: | |
555 | return (retval); | |
556 | } | |
557 | ||
558 | /* | |
559 | * This routine adjusts the superblock and other data structures, both | |
560 | * in disk as well as in memory... | |
561 | */ | |
8728d506 | 562 | static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size) |
bf69235a TT |
563 | { |
564 | ext2_filsys fs; | |
565 | int adj = 0; | |
566 | errcode_t retval; | |
8728d506 | 567 | blk64_t group_block; |
bf69235a TT |
568 | unsigned long i; |
569 | unsigned long max_group; | |
efc6f628 | 570 | |
bf69235a TT |
571 | fs = rfs->new_fs; |
572 | ext2fs_mark_super_dirty(fs); | |
573 | ext2fs_mark_bb_dirty(fs); | |
574 | ext2fs_mark_ib_dirty(fs); | |
575 | ||
c09043f1 TT |
576 | retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"), |
577 | &rfs->reserve_blocks); | |
578 | if (retval) | |
579 | return retval; | |
580 | ||
581 | retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size); | |
bf69235a TT |
582 | if (retval) |
583 | goto errout; | |
584 | ||
585 | /* | |
586 | * Check to make sure there are enough inodes | |
587 | */ | |
588 | if ((rfs->old_fs->super->s_inodes_count - | |
589 | rfs->old_fs->super->s_free_inodes_count) > | |
590 | rfs->new_fs->super->s_inodes_count) { | |
591 | retval = ENOSPC; | |
592 | goto errout; | |
593 | } | |
594 | ||
595 | /* | |
596 | * If we are shrinking the number block groups, we're done and | |
597 | * can exit now. | |
598 | */ | |
599 | if (rfs->old_fs->group_desc_count > fs->group_desc_count) { | |
600 | retval = 0; | |
601 | goto errout; | |
602 | } | |
603 | ||
604 | /* | |
605 | * If the number of block groups is staying the same, we're | |
606 | * done and can exit now. (If the number block groups is | |
607 | * shrinking, we had exited earlier.) | |
608 | */ | |
609 | if (rfs->old_fs->group_desc_count >= fs->group_desc_count) { | |
610 | retval = 0; | |
611 | goto errout; | |
612 | } | |
613 | ||
614 | /* | |
615 | * Initialize the new block group descriptors | |
616 | */ | |
e5aace90 | 617 | retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group, |
bf69235a TT |
618 | &rfs->itable_buf); |
619 | if (retval) | |
620 | goto errout; | |
621 | ||
622 | memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group); | |
623 | group_block = fs->super->s_first_data_block + | |
624 | rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group; | |
625 | ||
626 | adj = rfs->old_fs->group_desc_count; | |
627 | max_group = fs->group_desc_count - adj; | |
628 | if (rfs->progress) { | |
629 | retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS, | |
630 | 0, max_group); | |
631 | if (retval) | |
632 | goto errout; | |
633 | } | |
634 | for (i = rfs->old_fs->group_desc_count; | |
635 | i < fs->group_desc_count; i++) { | |
05e112a1 TT |
636 | /* |
637 | * Write out the new inode table | |
638 | */ | |
24a117ab | 639 | retval = io_channel_write_blk64(fs->io, |
d7cca6b0 | 640 | ext2fs_inode_table_loc(fs, i), |
24a117ab VAH |
641 | fs->inode_blocks_per_group, |
642 | rfs->itable_buf); | |
c762c8e6 TT |
643 | if (retval) goto errout; |
644 | ||
a8519a2d | 645 | io_channel_flush(fs->io); |
3b627e8d TT |
646 | if (rfs->progress) { |
647 | retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS, | |
648 | i - adj + 1, max_group); | |
649 | if (retval) | |
650 | goto errout; | |
651 | } | |
1e1da29f | 652 | group_block += fs->super->s_blocks_per_group; |
24b2c7a7 | 653 | } |
c762c8e6 TT |
654 | io_channel_flush(fs->io); |
655 | retval = 0; | |
656 | ||
657 | errout: | |
c762c8e6 TT |
658 | return retval; |
659 | } | |
660 | ||
a8519a2d TT |
661 | /* -------------------------------------------------------------------- |
662 | * | |
663 | * Resize processing, phase 2. | |
664 | * | |
665 | * In this phase we adjust determine which blocks need to be moved, in | |
666 | * blocks_to_move(). We then copy the blocks to their ultimate new | |
667 | * destinations using block_mover(). Since we are copying blocks to | |
668 | * their new locations, again during this pass we can abort without | |
669 | * any problems. | |
670 | * -------------------------------------------------------------------- | |
671 | */ | |
672 | ||
c762c8e6 TT |
673 | /* |
674 | * This helper function creates a block bitmap with all of the | |
675 | * filesystem meta-data blocks. | |
676 | */ | |
677 | static errcode_t mark_table_blocks(ext2_filsys fs, | |
64ad98ac | 678 | ext2fs_block_bitmap bmap) |
c762c8e6 | 679 | { |
8728d506 | 680 | blk64_t b; |
54434927 TT |
681 | unsigned int j; |
682 | dgrp_t i; | |
68963d5a | 683 | unsigned long meta_bg_size; |
54434927 | 684 | unsigned int old_desc_blocks; |
c762c8e6 | 685 | |
f2de1d38 | 686 | meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super); |
76dd5e5c TT |
687 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
688 | old_desc_blocks = fs->super->s_first_meta_bg; | |
689 | else | |
efc6f628 | 690 | old_desc_blocks = fs->desc_blocks + |
9213a93b | 691 | fs->super->s_reserved_gdt_blocks; |
c762c8e6 | 692 | for (i = 0; i < fs->group_desc_count; i++) { |
64ad98ac | 693 | ext2fs_reserve_super_and_bgd(fs, i, bmap); |
efc6f628 | 694 | |
c762c8e6 TT |
695 | /* |
696 | * Mark the blocks used for the inode table | |
697 | */ | |
d7cca6b0 | 698 | for (j = 0, b = ext2fs_inode_table_loc(fs, i); |
54434927 | 699 | j < (unsigned int) fs->inode_blocks_per_group; |
c762c8e6 | 700 | j++, b++) |
3346084b | 701 | ext2fs_mark_block_bitmap2(bmap, b); |
efc6f628 | 702 | |
c762c8e6 | 703 | /* |
efc6f628 | 704 | * Mark block used for the block bitmap |
c762c8e6 | 705 | */ |
3346084b | 706 | ext2fs_mark_block_bitmap2(bmap, |
d7cca6b0 | 707 | ext2fs_block_bitmap_loc(fs, i)); |
64ad98ac | 708 | |
c762c8e6 | 709 | /* |
efc6f628 | 710 | * Mark block used for the inode bitmap |
c762c8e6 | 711 | */ |
3346084b | 712 | ext2fs_mark_block_bitmap2(bmap, |
d7cca6b0 | 713 | ext2fs_inode_bitmap_loc(fs, i)); |
c762c8e6 | 714 | } |
1e1da29f | 715 | return 0; |
24b2c7a7 TT |
716 | } |
717 | ||
76dd5e5c TT |
718 | /* |
719 | * This function checks to see if a particular block (either a | |
720 | * superblock or a block group descriptor) overlaps with an inode or | |
721 | * block bitmap block, or with the inode table. | |
722 | */ | |
723 | static void mark_fs_metablock(ext2_resize_t rfs, | |
724 | ext2fs_block_bitmap meta_bmap, | |
8728d506 | 725 | int group, blk64_t blk) |
76dd5e5c TT |
726 | { |
727 | ext2_filsys fs = rfs->new_fs; | |
efc6f628 | 728 | |
3346084b | 729 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk); |
48f23054 | 730 | ext2fs_block_alloc_stats2(fs, blk, +1); |
76dd5e5c TT |
731 | |
732 | /* | |
733 | * Check to see if we overlap with the inode or block bitmap, | |
734 | * or the inode tables. If not, and the block is in use, then | |
735 | * mark it as a block to be moved. | |
736 | */ | |
737 | if (IS_BLOCK_BM(fs, group, blk)) { | |
d7cca6b0 | 738 | ext2fs_block_bitmap_loc_set(fs, group, 0); |
76dd5e5c TT |
739 | rfs->needed_blocks++; |
740 | } else if (IS_INODE_BM(fs, group, blk)) { | |
d7cca6b0 | 741 | ext2fs_inode_bitmap_loc_set(fs, group, 0); |
76dd5e5c TT |
742 | rfs->needed_blocks++; |
743 | } else if (IS_INODE_TB(fs, group, blk)) { | |
d7cca6b0 | 744 | ext2fs_inode_table_loc_set(fs, group, 0); |
76dd5e5c | 745 | rfs->needed_blocks++; |
86acdebd TT |
746 | } else if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
747 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && | |
cd65a24e | 748 | (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) { |
86acdebd TT |
749 | /* |
750 | * If the block bitmap is uninitialized, which means | |
751 | * nothing other than standard metadata in use. | |
752 | */ | |
753 | return; | |
3346084b VAH |
754 | } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) && |
755 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) { | |
756 | ext2fs_mark_block_bitmap2(rfs->move_blocks, blk); | |
76dd5e5c TT |
757 | rfs->needed_blocks++; |
758 | } | |
759 | } | |
760 | ||
761 | ||
24b2c7a7 TT |
762 | /* |
763 | * This routine marks and unmarks reserved blocks in the new block | |
764 | * bitmap. It also determines which blocks need to be moved and | |
765 | * places this information into the move_blocks bitmap. | |
766 | */ | |
c762c8e6 | 767 | static errcode_t blocks_to_move(ext2_resize_t rfs) |
24b2c7a7 | 768 | { |
54434927 | 769 | int j, has_super; |
86acdebd | 770 | dgrp_t i, max_groups, g; |
8728d506 VAH |
771 | blk64_t blk, group_blk; |
772 | blk64_t old_blocks, new_blocks; | |
54434927 | 773 | unsigned int meta_bg, meta_bg_size; |
24b2c7a7 | 774 | errcode_t retval; |
c762c8e6 TT |
775 | ext2_filsys fs, old_fs; |
776 | ext2fs_block_bitmap meta_bmap; | |
1ac84a94 | 777 | __u32 save_incompat_flag; |
24b2c7a7 | 778 | |
c762c8e6 TT |
779 | fs = rfs->new_fs; |
780 | old_fs = rfs->old_fs; | |
4efbac6f | 781 | if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super)) |
c762c8e6 | 782 | fs = rfs->old_fs; |
efc6f628 | 783 | |
a13575f4 | 784 | retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"), |
c762c8e6 TT |
785 | &rfs->move_blocks); |
786 | if (retval) | |
787 | return retval; | |
788 | ||
efc6f628 | 789 | retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"), |
64ad98ac TT |
790 | &meta_bmap); |
791 | if (retval) | |
792 | return retval; | |
efc6f628 | 793 | |
64ad98ac | 794 | retval = mark_table_blocks(old_fs, meta_bmap); |
c762c8e6 TT |
795 | if (retval) |
796 | return retval; | |
797 | ||
798 | fs = rfs->new_fs; | |
efc6f628 | 799 | |
1e1da29f TT |
800 | /* |
801 | * If we're shrinking the filesystem, we need to move all of | |
802 | * the blocks that don't fit any more | |
803 | */ | |
4efbac6f VAH |
804 | for (blk = ext2fs_blocks_count(fs->super); |
805 | blk < ext2fs_blocks_count(old_fs->super); blk++) { | |
6493f8e8 | 806 | g = ext2fs_group_of_blk2(fs, blk); |
86acdebd TT |
807 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
808 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && | |
cd65a24e | 809 | ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) { |
86acdebd TT |
810 | /* |
811 | * The block bitmap is uninitialized, so skip | |
812 | * to the next block group. | |
813 | */ | |
814 | blk = ((g+1) * fs->super->s_blocks_per_group) + | |
2d7361eb | 815 | fs->super->s_first_data_block - 1; |
86acdebd TT |
816 | continue; |
817 | } | |
3346084b VAH |
818 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && |
819 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) { | |
820 | ext2fs_mark_block_bitmap2(rfs->move_blocks, blk); | |
1e1da29f | 821 | rfs->needed_blocks++; |
c762c8e6 | 822 | } |
3346084b | 823 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk); |
1e1da29f | 824 | } |
efc6f628 | 825 | |
76dd5e5c TT |
826 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) { |
827 | old_blocks = old_fs->super->s_first_meta_bg; | |
828 | new_blocks = fs->super->s_first_meta_bg; | |
829 | } else { | |
9213a93b TT |
830 | old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks; |
831 | new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks; | |
76dd5e5c | 832 | } |
efc6f628 | 833 | |
c762c8e6 TT |
834 | if (old_blocks == new_blocks) { |
835 | retval = 0; | |
836 | goto errout; | |
837 | } | |
24b2c7a7 | 838 | |
1333fe93 TT |
839 | max_groups = fs->group_desc_count; |
840 | if (max_groups > old_fs->group_desc_count) | |
841 | max_groups = old_fs->group_desc_count; | |
c762c8e6 | 842 | group_blk = old_fs->super->s_first_data_block; |
24b2c7a7 TT |
843 | /* |
844 | * If we're reducing the number of descriptor blocks, this | |
845 | * makes life easy. :-) We just have to mark some extra | |
846 | * blocks as free. | |
847 | */ | |
848 | if (old_blocks > new_blocks) { | |
1333fe93 | 849 | for (i = 0; i < max_groups; i++) { |
1e1da29f TT |
850 | if (!ext2fs_bg_has_super(fs, i)) { |
851 | group_blk += fs->super->s_blocks_per_group; | |
24b2c7a7 TT |
852 | continue; |
853 | } | |
c762c8e6 TT |
854 | for (blk = group_blk+1+new_blocks; |
855 | blk < group_blk+1+old_blocks; blk++) { | |
48f23054 | 856 | ext2fs_block_alloc_stats2(fs, blk, -1); |
052db4b7 TT |
857 | rfs->needed_blocks--; |
858 | } | |
1e1da29f | 859 | group_blk += fs->super->s_blocks_per_group; |
24b2c7a7 | 860 | } |
c762c8e6 TT |
861 | retval = 0; |
862 | goto errout; | |
24b2c7a7 TT |
863 | } |
864 | /* | |
865 | * If we're increasing the number of descriptor blocks, life | |
efc6f628 | 866 | * gets interesting.... |
24b2c7a7 | 867 | */ |
f2de1d38 | 868 | meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super); |
1333fe93 | 869 | for (i = 0; i < max_groups; i++) { |
76dd5e5c TT |
870 | has_super = ext2fs_bg_has_super(fs, i); |
871 | if (has_super) | |
872 | mark_fs_metablock(rfs, meta_bmap, i, group_blk); | |
873 | ||
874 | meta_bg = i / meta_bg_size; | |
875 | if (!(fs->super->s_feature_incompat & | |
876 | EXT2_FEATURE_INCOMPAT_META_BG) || | |
877 | (meta_bg < fs->super->s_first_meta_bg)) { | |
424cb7b6 TT |
878 | if (has_super) { |
879 | for (blk = group_blk+1; | |
880 | blk < group_blk + 1 + new_blocks; blk++) | |
efc6f628 | 881 | mark_fs_metablock(rfs, meta_bmap, |
424cb7b6 TT |
882 | i, blk); |
883 | } | |
76dd5e5c TT |
884 | } else { |
885 | if (has_super) | |
886 | has_super = 1; | |
887 | if (((i % meta_bg_size) == 0) || | |
888 | ((i % meta_bg_size) == 1) || | |
889 | ((i % meta_bg_size) == (meta_bg_size-1))) | |
890 | mark_fs_metablock(rfs, meta_bmap, i, | |
891 | group_blk + has_super); | |
24b2c7a7 | 892 | } |
76dd5e5c | 893 | |
d7cca6b0 VAH |
894 | if (ext2fs_inode_table_loc(fs, i) && |
895 | ext2fs_inode_bitmap_loc(fs, i) && | |
896 | ext2fs_block_bitmap_loc(fs, i)) | |
1e1da29f | 897 | goto next_group; |
24b2c7a7 | 898 | |
1e1da29f | 899 | /* |
c762c8e6 TT |
900 | * Reserve the existing meta blocks that we know |
901 | * aren't to be moved. | |
1e1da29f | 902 | */ |
d7cca6b0 | 903 | if (ext2fs_block_bitmap_loc(fs, i)) |
3346084b | 904 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, |
d7cca6b0 VAH |
905 | ext2fs_block_bitmap_loc(fs, i)); |
906 | if (ext2fs_inode_bitmap_loc(fs, i)) | |
3346084b | 907 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, |
d7cca6b0 VAH |
908 | ext2fs_inode_bitmap_loc(fs, i)); |
909 | if (ext2fs_inode_table_loc(fs, i)) | |
910 | for (blk = ext2fs_inode_table_loc(fs, i), j=0; | |
1e1da29f | 911 | j < fs->inode_blocks_per_group ; j++, blk++) |
3346084b | 912 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, |
1e1da29f | 913 | blk); |
24b2c7a7 | 914 | |
c762c8e6 TT |
915 | /* |
916 | * Allocate the missing data structures | |
1ac84a94 TT |
917 | * |
918 | * XXX We have a problem with FLEX_BG and off-line | |
919 | * resizing where we are growing the size of the | |
920 | * filesystem. ext2fs_allocate_group_table() will try | |
921 | * to reserve the inode table in the desired flex_bg | |
922 | * location. However, passing rfs->reserve_blocks | |
923 | * doesn't work since it only has reserved the blocks | |
924 | * that will be used in the new block group -- and | |
925 | * with flex_bg, we can and will allocate the tables | |
926 | * outside of the block group. And we can't pass in | |
927 | * the fs->block_map because it doesn't handle | |
928 | * overlapping inode table movements right. So for | |
929 | * now, we temporarily disable flex_bg to force | |
930 | * ext2fs_allocate_group_tables() to allocate the bg | |
931 | * metadata in side the block group, and the restore | |
932 | * it afterwards. Ugly, until we can fix this up | |
933 | * right later. | |
c762c8e6 | 934 | */ |
1ac84a94 TT |
935 | save_incompat_flag = fs->super->s_feature_incompat; |
936 | fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG; | |
1e1da29f TT |
937 | retval = ext2fs_allocate_group_table(fs, i, |
938 | rfs->reserve_blocks); | |
1ac84a94 | 939 | fs->super->s_feature_incompat = save_incompat_flag; |
1e1da29f | 940 | if (retval) |
c762c8e6 | 941 | goto errout; |
24b2c7a7 | 942 | |
1e1da29f | 943 | /* |
c762c8e6 TT |
944 | * For those structures that have changed, we need to |
945 | * do bookkeepping. | |
1e1da29f | 946 | */ |
d7cca6b0 VAH |
947 | if (ext2fs_block_bitmap_loc(old_fs, i) != |
948 | (blk = ext2fs_block_bitmap_loc(fs, i))) { | |
48f23054 | 949 | ext2fs_block_alloc_stats2(fs, blk, +1); |
3346084b VAH |
950 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && |
951 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) | |
952 | ext2fs_mark_block_bitmap2(rfs->move_blocks, | |
c762c8e6 TT |
953 | blk); |
954 | } | |
d7cca6b0 VAH |
955 | if (ext2fs_inode_bitmap_loc(old_fs, i) != |
956 | (blk = ext2fs_inode_bitmap_loc(fs, i))) { | |
48f23054 | 957 | ext2fs_block_alloc_stats2(fs, blk, +1); |
3346084b VAH |
958 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && |
959 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) | |
960 | ext2fs_mark_block_bitmap2(rfs->move_blocks, | |
c762c8e6 TT |
961 | blk); |
962 | } | |
24b2c7a7 | 963 | |
052db4b7 TT |
964 | /* |
965 | * The inode table, if we need to relocate it, is | |
966 | * handled specially. We have to reserve the blocks | |
967 | * for both the old and the new inode table, since we | |
968 | * can't have the inode table be destroyed during the | |
969 | * block relocation phase. | |
970 | */ | |
d7cca6b0 | 971 | if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i)) |
052db4b7 TT |
972 | goto next_group; /* inode table not moved */ |
973 | ||
c762c8e6 | 974 | rfs->needed_blocks += fs->inode_blocks_per_group; |
052db4b7 TT |
975 | |
976 | /* | |
977 | * Mark the new inode table as in use in the new block | |
efc6f628 | 978 | * allocation bitmap, and move any blocks that might |
c762c8e6 | 979 | * be necessary. |
052db4b7 | 980 | */ |
d7cca6b0 | 981 | for (blk = ext2fs_inode_table_loc(fs, i), j=0; |
c762c8e6 | 982 | j < fs->inode_blocks_per_group ; j++, blk++) { |
48f23054 | 983 | ext2fs_block_alloc_stats2(fs, blk, +1); |
3346084b VAH |
984 | if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) && |
985 | !ext2fs_test_block_bitmap2(meta_bmap, blk)) | |
986 | ext2fs_mark_block_bitmap2(rfs->move_blocks, | |
c762c8e6 TT |
987 | blk); |
988 | } | |
efc6f628 | 989 | |
1e1da29f | 990 | /* |
052db4b7 TT |
991 | * Make sure the old inode table is reserved in the |
992 | * block reservation bitmap. | |
1e1da29f | 993 | */ |
d7cca6b0 | 994 | for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0; |
052db4b7 | 995 | j < fs->inode_blocks_per_group ; j++, blk++) |
3346084b | 996 | ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk); |
efc6f628 | 997 | |
1e1da29f TT |
998 | next_group: |
999 | group_blk += rfs->new_fs->super->s_blocks_per_group; | |
1000 | } | |
c762c8e6 TT |
1001 | retval = 0; |
1002 | ||
1003 | errout: | |
1004 | if (meta_bmap) | |
1005 | ext2fs_free_block_bitmap(meta_bmap); | |
efc6f628 | 1006 | |
c762c8e6 | 1007 | return retval; |
1e1da29f | 1008 | } |
24b2c7a7 | 1009 | |
a8519a2d TT |
1010 | /* |
1011 | * This helper function tries to allocate a new block. We try to | |
1012 | * avoid hitting the original group descriptor blocks at least at | |
1013 | * first, since we want to make it possible to recover from a badly | |
1014 | * aborted resize operation as much as possible. | |
1015 | * | |
1016 | * In the future, I may further modify this routine to balance out | |
1017 | * where we get the new blocks across the various block groups. | |
1018 | * Ideally we would allocate blocks that corresponded with the block | |
1019 | * group of the containing inode, and keep contiguous blocks | |
1020 | * together. However, this very difficult to do efficiently, since we | |
1021 | * don't have the necessary information up front. | |
1022 | */ | |
1023 | ||
1024 | #define AVOID_OLD 1 | |
1025 | #define DESPERATION 2 | |
1026 | ||
1027 | static void init_block_alloc(ext2_resize_t rfs) | |
1028 | { | |
1029 | rfs->alloc_state = AVOID_OLD; | |
1030 | rfs->new_blk = rfs->new_fs->super->s_first_data_block; | |
2bc4d4f7 TT |
1031 | #if 0 |
1032 | /* HACK for testing */ | |
4efbac6f VAH |
1033 | if (ext2fs_blocks_count(rfs->new_fs->super) > |
1034 | ext2fs_blocks_count(rfs->old_fs->super)) | |
1035 | rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super); | |
2bc4d4f7 | 1036 | #endif |
a8519a2d TT |
1037 | } |
1038 | ||
8728d506 | 1039 | static blk64_t get_new_block(ext2_resize_t rfs) |
a8519a2d TT |
1040 | { |
1041 | ext2_filsys fs = rfs->new_fs; | |
efc6f628 | 1042 | |
a8519a2d | 1043 | while (1) { |
4efbac6f | 1044 | if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) { |
a8519a2d TT |
1045 | if (rfs->alloc_state == DESPERATION) |
1046 | return 0; | |
1047 | ||
1048 | #ifdef RESIZE2FS_DEBUG | |
1049 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
f35fd3d5 TT |
1050 | printf("Going into desperation mode " |
1051 | "for block allocations\n"); | |
efc6f628 | 1052 | #endif |
a8519a2d TT |
1053 | rfs->alloc_state = DESPERATION; |
1054 | rfs->new_blk = fs->super->s_first_data_block; | |
1055 | continue; | |
1056 | } | |
3346084b VAH |
1057 | if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) || |
1058 | ext2fs_test_block_bitmap2(rfs->reserve_blocks, | |
a8519a2d TT |
1059 | rfs->new_blk) || |
1060 | ((rfs->alloc_state == AVOID_OLD) && | |
4efbac6f | 1061 | (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) && |
3346084b | 1062 | ext2fs_test_block_bitmap2(rfs->old_fs->block_map, |
a8519a2d TT |
1063 | rfs->new_blk))) { |
1064 | rfs->new_blk++; | |
1065 | continue; | |
1066 | } | |
1067 | return rfs->new_blk; | |
1068 | } | |
1069 | } | |
1070 | ||
7f9c96ee TT |
1071 | static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal, |
1072 | blk64_t *ret) | |
1073 | { | |
1074 | ext2_resize_t rfs = (ext2_resize_t) fs->priv_data; | |
8728d506 | 1075 | blk64_t blk; |
7f9c96ee TT |
1076 | |
1077 | blk = get_new_block(rfs); | |
1078 | if (!blk) | |
1079 | return ENOSPC; | |
1080 | ||
1081 | #ifdef RESIZE2FS_DEBUG | |
1082 | if (rfs->flags & 0xF) | |
8728d506 | 1083 | printf("get_alloc_block allocating %llu\n", blk); |
7f9c96ee TT |
1084 | #endif |
1085 | ||
3346084b VAH |
1086 | ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk); |
1087 | ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk); | |
7f9c96ee TT |
1088 | *ret = (blk64_t) blk; |
1089 | return 0; | |
1090 | } | |
1091 | ||
a8519a2d TT |
1092 | static errcode_t block_mover(ext2_resize_t rfs) |
1093 | { | |
8728d506 | 1094 | blk64_t blk, old_blk, new_blk; |
a8519a2d TT |
1095 | ext2_filsys fs = rfs->new_fs; |
1096 | ext2_filsys old_fs = rfs->old_fs; | |
1097 | errcode_t retval; | |
8728d506 VAH |
1098 | __u64 size; |
1099 | int c; | |
a8519a2d | 1100 | int to_move, moved; |
7d7bdd57 TT |
1101 | ext2_badblocks_list badblock_list = 0; |
1102 | int bb_modified = 0; | |
efc6f628 | 1103 | |
7f9c96ee TT |
1104 | fs->get_alloc_block = resize2fs_get_alloc_block; |
1105 | old_fs->get_alloc_block = resize2fs_get_alloc_block; | |
1106 | ||
7d7bdd57 TT |
1107 | retval = ext2fs_read_bb_inode(old_fs, &badblock_list); |
1108 | if (retval) | |
1109 | return retval; | |
a8519a2d TT |
1110 | |
1111 | new_blk = fs->super->s_first_data_block; | |
1112 | if (!rfs->itable_buf) { | |
e5aace90 | 1113 | retval = ext2fs_get_array(fs->blocksize, |
a8519a2d | 1114 | fs->inode_blocks_per_group, |
c4e3d3f3 | 1115 | &rfs->itable_buf); |
a8519a2d TT |
1116 | if (retval) |
1117 | return retval; | |
1118 | } | |
1119 | retval = ext2fs_create_extent_table(&rfs->bmap, 0); | |
1120 | if (retval) | |
1121 | return retval; | |
1122 | ||
1123 | /* | |
1124 | * The first step is to figure out where all of the blocks | |
1125 | * will go. | |
1126 | */ | |
1127 | to_move = moved = 0; | |
1128 | init_block_alloc(rfs); | |
1129 | for (blk = old_fs->super->s_first_data_block; | |
4efbac6f | 1130 | blk < ext2fs_blocks_count(old_fs->super); blk++) { |
3346084b | 1131 | if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk)) |
a8519a2d | 1132 | continue; |
3346084b | 1133 | if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk)) |
a8519a2d | 1134 | continue; |
7d7bdd57 TT |
1135 | if (ext2fs_badblocks_list_test(badblock_list, blk)) { |
1136 | ext2fs_badblocks_list_del(badblock_list, blk); | |
1137 | bb_modified++; | |
1138 | continue; | |
1139 | } | |
a8519a2d TT |
1140 | |
1141 | new_blk = get_new_block(rfs); | |
1142 | if (!new_blk) { | |
1143 | retval = ENOSPC; | |
1144 | goto errout; | |
1145 | } | |
48f23054 | 1146 | ext2fs_block_alloc_stats2(fs, new_blk, +1); |
a8519a2d TT |
1147 | ext2fs_add_extent_entry(rfs->bmap, blk, new_blk); |
1148 | to_move++; | |
1149 | } | |
efc6f628 | 1150 | |
a8519a2d | 1151 | if (to_move == 0) { |
cefbf487 TT |
1152 | if (rfs->bmap) { |
1153 | ext2fs_free_extent_table(rfs->bmap); | |
1154 | rfs->bmap = 0; | |
1155 | } | |
a8519a2d TT |
1156 | retval = 0; |
1157 | goto errout; | |
1158 | } | |
1159 | ||
1160 | /* | |
1161 | * Step two is to actually move the blocks | |
1162 | */ | |
1163 | retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0); | |
1164 | if (retval) goto errout; | |
1165 | ||
3b627e8d TT |
1166 | if (rfs->progress) { |
1167 | retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS, | |
1168 | 0, to_move); | |
1169 | if (retval) | |
1170 | goto errout; | |
1171 | } | |
a8519a2d TT |
1172 | while (1) { |
1173 | retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size); | |
1174 | if (retval) goto errout; | |
1175 | if (!size) | |
1176 | break; | |
1177 | #ifdef RESIZE2FS_DEBUG | |
1178 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
8728d506 | 1179 | printf("Moving %llu blocks %llu->%llu\n", |
f35fd3d5 | 1180 | size, old_blk, new_blk); |
a8519a2d TT |
1181 | #endif |
1182 | do { | |
1183 | c = size; | |
1184 | if (c > fs->inode_blocks_per_group) | |
1185 | c = fs->inode_blocks_per_group; | |
24a117ab VAH |
1186 | retval = io_channel_read_blk64(fs->io, old_blk, c, |
1187 | rfs->itable_buf); | |
a8519a2d | 1188 | if (retval) goto errout; |
24a117ab VAH |
1189 | retval = io_channel_write_blk64(fs->io, new_blk, c, |
1190 | rfs->itable_buf); | |
a8519a2d TT |
1191 | if (retval) goto errout; |
1192 | size -= c; | |
1193 | new_blk += c; | |
1194 | old_blk += c; | |
1195 | moved += c; | |
1196 | if (rfs->progress) { | |
1197 | io_channel_flush(fs->io); | |
3b627e8d TT |
1198 | retval = (rfs->progress)(rfs, |
1199 | E2_RSZ_BLOCK_RELOC_PASS, | |
a8519a2d | 1200 | moved, to_move); |
3b627e8d TT |
1201 | if (retval) |
1202 | goto errout; | |
a8519a2d TT |
1203 | } |
1204 | } while (size > 0); | |
1205 | io_channel_flush(fs->io); | |
1206 | } | |
1207 | ||
1208 | errout: | |
7d7bdd57 TT |
1209 | if (badblock_list) { |
1210 | if (!retval && bb_modified) | |
1211 | retval = ext2fs_update_bb_inode(old_fs, | |
1212 | badblock_list); | |
1213 | ext2fs_badblocks_list_free(badblock_list); | |
1214 | } | |
a8519a2d TT |
1215 | return retval; |
1216 | } | |
1217 | ||
1218 | ||
1219 | /* -------------------------------------------------------------------- | |
1220 | * | |
1221 | * Resize processing, phase 3 | |
1222 | * | |
1223 | * -------------------------------------------------------------------- | |
1224 | */ | |
1225 | ||
1226 | ||
1227 | struct process_block_struct { | |
1228 | ext2_resize_t rfs; | |
dfcdc32f | 1229 | ext2_ino_t ino; |
a8519a2d TT |
1230 | struct ext2_inode * inode; |
1231 | errcode_t error; | |
1232 | int is_dir; | |
1233 | int changed; | |
1234 | }; | |
1235 | ||
8728d506 | 1236 | static int process_block(ext2_filsys fs, blk64_t *block_nr, |
efc6f628 | 1237 | e2_blkcnt_t blockcnt, |
8728d506 | 1238 | blk64_t ref_block EXT2FS_ATTR((unused)), |
54434927 | 1239 | int ref_offset EXT2FS_ATTR((unused)), void *priv_data) |
a8519a2d TT |
1240 | { |
1241 | struct process_block_struct *pb; | |
1242 | errcode_t retval; | |
8728d506 | 1243 | blk64_t block, new_block; |
a8519a2d TT |
1244 | int ret = 0; |
1245 | ||
1246 | pb = (struct process_block_struct *) priv_data; | |
1247 | block = *block_nr; | |
1248 | if (pb->rfs->bmap) { | |
1249 | new_block = ext2fs_extent_translate(pb->rfs->bmap, block); | |
1250 | if (new_block) { | |
1251 | *block_nr = new_block; | |
1252 | ret |= BLOCK_CHANGED; | |
1253 | pb->changed = 1; | |
1254 | #ifdef RESIZE2FS_DEBUG | |
1255 | if (pb->rfs->flags & RESIZE_DEBUG_BMOVE) | |
8728d506 | 1256 | printf("ino=%u, blockcnt=%lld, %llu->%llu\n", |
a8519a2d TT |
1257 | pb->ino, blockcnt, block, new_block); |
1258 | #endif | |
1259 | block = new_block; | |
1260 | } | |
1261 | } | |
1262 | if (pb->is_dir) { | |
8728d506 VAH |
1263 | retval = ext2fs_add_dir_block2(fs->dblist, pb->ino, |
1264 | block, (int) blockcnt); | |
a8519a2d TT |
1265 | if (retval) { |
1266 | pb->error = retval; | |
1267 | ret |= BLOCK_ABORT; | |
1268 | } | |
1269 | } | |
1270 | return ret; | |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * Progress callback | |
1275 | */ | |
efc6f628 | 1276 | static errcode_t progress_callback(ext2_filsys fs, |
54434927 | 1277 | ext2_inode_scan scan EXT2FS_ATTR((unused)), |
a8519a2d TT |
1278 | dgrp_t group, void * priv_data) |
1279 | { | |
1280 | ext2_resize_t rfs = (ext2_resize_t) priv_data; | |
3b627e8d | 1281 | errcode_t retval; |
a8519a2d TT |
1282 | |
1283 | /* | |
f4b2a6db TT |
1284 | * This check is to protect against old ext2 libraries. It |
1285 | * shouldn't be needed against new libraries. | |
a8519a2d | 1286 | */ |
f4b2a6db | 1287 | if ((group+1) == 0) |
a8519a2d TT |
1288 | return 0; |
1289 | ||
1290 | if (rfs->progress) { | |
1291 | io_channel_flush(fs->io); | |
3b627e8d TT |
1292 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS, |
1293 | group+1, fs->group_desc_count); | |
1294 | if (retval) | |
1295 | return retval; | |
a8519a2d | 1296 | } |
efc6f628 | 1297 | |
a8519a2d TT |
1298 | return 0; |
1299 | } | |
1300 | ||
1301 | static errcode_t inode_scan_and_fix(ext2_resize_t rfs) | |
1302 | { | |
1303 | struct process_block_struct pb; | |
dfcdc32f | 1304 | ext2_ino_t ino, new_inode; |
edfd9b0a | 1305 | struct ext2_inode *inode = NULL; |
a8519a2d TT |
1306 | ext2_inode_scan scan = NULL; |
1307 | errcode_t retval; | |
a8519a2d | 1308 | char *block_buf = 0; |
dfcdc32f | 1309 | ext2_ino_t start_to_move; |
8728d506 VAH |
1310 | blk64_t orig_size; |
1311 | blk64_t new_block; | |
4ef28824 | 1312 | int inode_size; |
efc6f628 | 1313 | |
a8519a2d TT |
1314 | if ((rfs->old_fs->group_desc_count <= |
1315 | rfs->new_fs->group_desc_count) && | |
1316 | !rfs->bmap) | |
1317 | return 0; | |
1318 | ||
2bc4d4f7 TT |
1319 | /* |
1320 | * Save the original size of the old filesystem, and | |
1321 | * temporarily set the size to be the new size if the new size | |
1322 | * is larger. We need to do this to avoid catching an error | |
1323 | * by the block iterator routines | |
1324 | */ | |
4efbac6f VAH |
1325 | orig_size = ext2fs_blocks_count(rfs->old_fs->super); |
1326 | if (orig_size < ext2fs_blocks_count(rfs->new_fs->super)) | |
1327 | ext2fs_blocks_count_set(rfs->old_fs->super, | |
1328 | ext2fs_blocks_count(rfs->new_fs->super)); | |
2bc4d4f7 | 1329 | |
a8519a2d TT |
1330 | retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan); |
1331 | if (retval) goto errout; | |
1332 | ||
1333 | retval = ext2fs_init_dblist(rfs->old_fs, 0); | |
1334 | if (retval) goto errout; | |
e5aace90 | 1335 | retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf); |
a8519a2d TT |
1336 | if (retval) goto errout; |
1337 | ||
1338 | start_to_move = (rfs->new_fs->group_desc_count * | |
1339 | rfs->new_fs->super->s_inodes_per_group); | |
efc6f628 | 1340 | |
3b627e8d TT |
1341 | if (rfs->progress) { |
1342 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS, | |
1343 | 0, rfs->old_fs->group_desc_count); | |
1344 | if (retval) | |
1345 | goto errout; | |
1346 | } | |
a8519a2d TT |
1347 | ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs); |
1348 | pb.rfs = rfs; | |
edfd9b0a | 1349 | pb.inode = inode; |
a8519a2d TT |
1350 | pb.error = 0; |
1351 | new_inode = EXT2_FIRST_INODE(rfs->new_fs->super); | |
4ef28824 | 1352 | inode_size = EXT2_INODE_SIZE(rfs->new_fs->super); |
edfd9b0a TT |
1353 | inode = malloc(inode_size); |
1354 | if (!inode) { | |
4ef28824 ES |
1355 | retval = ENOMEM; |
1356 | goto errout; | |
1357 | } | |
a8519a2d TT |
1358 | /* |
1359 | * First, copy all of the inodes that need to be moved | |
1360 | * elsewhere in the inode table | |
1361 | */ | |
1362 | while (1) { | |
edfd9b0a | 1363 | retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size); |
a8519a2d TT |
1364 | if (retval) goto errout; |
1365 | if (!ino) | |
1366 | break; | |
1367 | ||
edfd9b0a | 1368 | if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO) |
a8519a2d TT |
1369 | continue; /* inode not in use */ |
1370 | ||
edfd9b0a | 1371 | pb.is_dir = LINUX_S_ISDIR(inode->i_mode); |
a8519a2d TT |
1372 | pb.changed = 0; |
1373 | ||
0c80c44b | 1374 | if (ext2fs_file_acl_block(rfs->old_fs, inode) && rfs->bmap) { |
efc6f628 | 1375 | new_block = ext2fs_extent_translate(rfs->bmap, |
0c80c44b | 1376 | ext2fs_file_acl_block(rfs->old_fs, inode)); |
ed909bbe | 1377 | if (new_block) { |
0c80c44b TT |
1378 | ext2fs_file_acl_block_set(rfs->old_fs, inode, |
1379 | new_block); | |
efc6f628 | 1380 | retval = ext2fs_write_inode_full(rfs->old_fs, |
edfd9b0a | 1381 | ino, inode, inode_size); |
ed909bbe TT |
1382 | if (retval) goto errout; |
1383 | } | |
1384 | } | |
efc6f628 | 1385 | |
0c80c44b | 1386 | if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) && |
a8519a2d TT |
1387 | (rfs->bmap || pb.is_dir)) { |
1388 | pb.ino = ino; | |
8728d506 | 1389 | retval = ext2fs_block_iterate3(rfs->old_fs, |
a8519a2d TT |
1390 | ino, 0, block_buf, |
1391 | process_block, &pb); | |
1392 | if (retval) | |
1393 | goto errout; | |
1394 | if (pb.error) { | |
1395 | retval = pb.error; | |
1396 | goto errout; | |
1397 | } | |
1398 | } | |
1399 | ||
1400 | if (ino <= start_to_move) | |
1401 | continue; /* Don't need to move it. */ | |
1402 | ||
1403 | /* | |
1404 | * Find a new inode | |
1405 | */ | |
86acdebd TT |
1406 | retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode); |
1407 | if (retval) | |
1408 | goto errout; | |
1409 | ||
74128f8d TT |
1410 | ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1, |
1411 | pb.is_dir); | |
a8519a2d TT |
1412 | if (pb.changed) { |
1413 | /* Get the new version of the inode */ | |
4ef28824 | 1414 | retval = ext2fs_read_inode_full(rfs->old_fs, ino, |
edfd9b0a | 1415 | inode, inode_size); |
a8519a2d TT |
1416 | if (retval) goto errout; |
1417 | } | |
edfd9b0a | 1418 | inode->i_ctime = time(0); |
4ef28824 | 1419 | retval = ext2fs_write_inode_full(rfs->old_fs, new_inode, |
edfd9b0a | 1420 | inode, inode_size); |
a8519a2d TT |
1421 | if (retval) goto errout; |
1422 | ||
a8519a2d TT |
1423 | #ifdef RESIZE2FS_DEBUG |
1424 | if (rfs->flags & RESIZE_DEBUG_INODEMAP) | |
f35fd3d5 | 1425 | printf("Inode moved %u->%u\n", ino, new_inode); |
a8519a2d TT |
1426 | #endif |
1427 | if (!rfs->imap) { | |
1428 | retval = ext2fs_create_extent_table(&rfs->imap, 0); | |
1429 | if (retval) | |
1430 | goto errout; | |
1431 | } | |
1432 | ext2fs_add_extent_entry(rfs->imap, ino, new_inode); | |
1433 | } | |
1434 | io_channel_flush(rfs->old_fs->io); | |
1435 | ||
1436 | errout: | |
4efbac6f | 1437 | ext2fs_blocks_count_set(rfs->old_fs->super, orig_size); |
a8519a2d TT |
1438 | if (rfs->bmap) { |
1439 | ext2fs_free_extent_table(rfs->bmap); | |
1440 | rfs->bmap = 0; | |
1441 | } | |
1442 | if (scan) | |
1443 | ext2fs_close_inode_scan(scan); | |
1444 | if (block_buf) | |
c4e3d3f3 | 1445 | ext2fs_free_mem(&block_buf); |
45e338f5 | 1446 | free(inode); |
a8519a2d TT |
1447 | return retval; |
1448 | } | |
1449 | ||
1450 | /* -------------------------------------------------------------------- | |
1451 | * | |
1452 | * Resize processing, phase 4. | |
1453 | * | |
1454 | * -------------------------------------------------------------------- | |
1455 | */ | |
1456 | ||
1457 | struct istruct { | |
1458 | ext2_resize_t rfs; | |
3b627e8d | 1459 | errcode_t err; |
03fa6f8a TT |
1460 | unsigned int max_dirs; |
1461 | unsigned int num; | |
a8519a2d TT |
1462 | }; |
1463 | ||
efc6f628 | 1464 | static int check_and_change_inodes(ext2_ino_t dir, |
54434927 | 1465 | int entry EXT2FS_ATTR((unused)), |
a8519a2d | 1466 | struct ext2_dir_entry *dirent, int offset, |
54434927 | 1467 | int blocksize EXT2FS_ATTR((unused)), |
efc6f628 | 1468 | char *buf EXT2FS_ATTR((unused)), |
54434927 | 1469 | void *priv_data) |
a8519a2d TT |
1470 | { |
1471 | struct istruct *is = (struct istruct *) priv_data; | |
085d2a83 TT |
1472 | struct ext2_inode inode; |
1473 | ext2_ino_t new_inode; | |
1474 | errcode_t retval; | |
a8519a2d TT |
1475 | |
1476 | if (is->rfs->progress && offset == 0) { | |
1477 | io_channel_flush(is->rfs->old_fs->io); | |
3b627e8d TT |
1478 | is->err = (is->rfs->progress)(is->rfs, |
1479 | E2_RSZ_INODE_REF_UPD_PASS, | |
1333fe93 | 1480 | ++is->num, is->max_dirs); |
3b627e8d TT |
1481 | if (is->err) |
1482 | return DIRENT_ABORT; | |
a8519a2d TT |
1483 | } |
1484 | ||
1485 | if (!dirent->inode) | |
1486 | return 0; | |
1487 | ||
1488 | new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode); | |
1489 | ||
1490 | if (!new_inode) | |
1491 | return 0; | |
1492 | #ifdef RESIZE2FS_DEBUG | |
1493 | if (is->rfs->flags & RESIZE_DEBUG_INODEMAP) | |
f35fd3d5 | 1494 | printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n", |
06191693 | 1495 | dir, dirent->name_len&0xFF, dirent->name, |
a8519a2d TT |
1496 | dirent->inode, new_inode); |
1497 | #endif | |
1498 | ||
1499 | dirent->inode = new_inode; | |
1500 | ||
085d2a83 TT |
1501 | /* Update the directory mtime and ctime */ |
1502 | retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode); | |
1503 | if (retval == 0) { | |
1504 | inode.i_mtime = inode.i_ctime = time(0); | |
d2b2a488 BB |
1505 | is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode); |
1506 | if (is->err) | |
1507 | return DIRENT_ABORT; | |
085d2a83 TT |
1508 | } |
1509 | ||
a8519a2d TT |
1510 | return DIRENT_CHANGED; |
1511 | } | |
1512 | ||
1513 | static errcode_t inode_ref_fix(ext2_resize_t rfs) | |
1514 | { | |
1515 | errcode_t retval; | |
1516 | struct istruct is; | |
efc6f628 | 1517 | |
a8519a2d TT |
1518 | if (!rfs->imap) |
1519 | return 0; | |
efc6f628 | 1520 | |
a8519a2d TT |
1521 | /* |
1522 | * Now, we iterate over all of the directories to update the | |
1523 | * inode references | |
1524 | */ | |
1525 | is.num = 0; | |
8728d506 | 1526 | is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist); |
a8519a2d | 1527 | is.rfs = rfs; |
3b627e8d | 1528 | is.err = 0; |
a8519a2d | 1529 | |
3b627e8d TT |
1530 | if (rfs->progress) { |
1531 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS, | |
1333fe93 | 1532 | 0, is.max_dirs); |
3b627e8d TT |
1533 | if (retval) |
1534 | goto errout; | |
1535 | } | |
efc6f628 | 1536 | |
a8519a2d TT |
1537 | retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist, |
1538 | DIRENT_FLAG_INCLUDE_EMPTY, 0, | |
1539 | check_and_change_inodes, &is); | |
3b627e8d TT |
1540 | if (retval) |
1541 | goto errout; | |
1542 | if (is.err) { | |
1543 | retval = is.err; | |
1544 | goto errout; | |
1545 | } | |
a8519a2d | 1546 | |
88dbf828 TT |
1547 | if (rfs->progress && (is.num < is.max_dirs)) |
1548 | (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS, | |
1549 | is.max_dirs, is.max_dirs); | |
1550 | ||
3b627e8d | 1551 | errout: |
a8519a2d TT |
1552 | ext2fs_free_extent_table(rfs->imap); |
1553 | rfs->imap = 0; | |
1554 | return retval; | |
1555 | } | |
1556 | ||
1557 | ||
1558 | /* -------------------------------------------------------------------- | |
1559 | * | |
1560 | * Resize processing, phase 5. | |
1561 | * | |
1562 | * In this phase we actually move the inode table around, and then | |
1563 | * update the summary statistics. This is scary, since aborting here | |
1564 | * will potentially scramble the filesystem. (We are moving the | |
1565 | * inode tables around in place, and so the potential for lost data, | |
1566 | * or at the very least scrambling the mapping between filenames and | |
1567 | * inode numbers is very high in case of a power failure here.) | |
1568 | * -------------------------------------------------------------------- | |
1569 | */ | |
1570 | ||
24b2c7a7 | 1571 | |
052db4b7 TT |
1572 | /* |
1573 | * A very scary routine --- this one moves the inode table around!!! | |
1574 | * | |
1575 | * After this you have to use the rfs->new_fs file handle to read and | |
1576 | * write inodes. | |
1577 | */ | |
c762c8e6 | 1578 | static errcode_t move_itables(ext2_resize_t rfs) |
052db4b7 | 1579 | { |
8728d506 VAH |
1580 | int n, num, size; |
1581 | long long diff; | |
54434927 | 1582 | dgrp_t i, max_groups; |
052db4b7 | 1583 | ext2_filsys fs = rfs->new_fs; |
05e112a1 | 1584 | char *cp; |
8728d506 | 1585 | blk64_t old_blk, new_blk, blk; |
a8519a2d | 1586 | errcode_t retval; |
64ad98ac | 1587 | int j, to_move, moved; |
052db4b7 | 1588 | |
1333fe93 TT |
1589 | max_groups = fs->group_desc_count; |
1590 | if (max_groups > rfs->old_fs->group_desc_count) | |
1591 | max_groups = rfs->old_fs->group_desc_count; | |
052db4b7 | 1592 | |
05e112a1 TT |
1593 | size = fs->blocksize * fs->inode_blocks_per_group; |
1594 | if (!rfs->itable_buf) { | |
c4e3d3f3 | 1595 | retval = ext2fs_get_mem(size, &rfs->itable_buf); |
ca8abba7 TT |
1596 | if (retval) |
1597 | return retval; | |
05e112a1 | 1598 | } |
c762c8e6 TT |
1599 | |
1600 | /* | |
1601 | * Figure out how many inode tables we need to move | |
1602 | */ | |
1603 | to_move = moved = 0; | |
1333fe93 | 1604 | for (i=0; i < max_groups; i++) |
d7cca6b0 VAH |
1605 | if (ext2fs_inode_table_loc(rfs->old_fs, i) != |
1606 | ext2fs_inode_table_loc(fs, i)) | |
c762c8e6 TT |
1607 | to_move++; |
1608 | ||
1609 | if (to_move == 0) | |
1610 | return 0; | |
1611 | ||
3b627e8d TT |
1612 | if (rfs->progress) { |
1613 | retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, | |
1614 | 0, to_move); | |
1615 | if (retval) | |
1616 | goto errout; | |
1617 | } | |
63b44fbe | 1618 | |
a8519a2d TT |
1619 | rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
1620 | ||
1333fe93 | 1621 | for (i=0; i < max_groups; i++) { |
d7cca6b0 VAH |
1622 | old_blk = ext2fs_inode_table_loc(rfs->old_fs, i); |
1623 | new_blk = ext2fs_inode_table_loc(fs, i); | |
ca8abba7 | 1624 | diff = new_blk - old_blk; |
efc6f628 | 1625 | |
80c0fc34 | 1626 | #ifdef RESIZE2FS_DEBUG |
efc6f628 | 1627 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
8728d506 | 1628 | printf("Itable move group %d block %llu->%llu (diff %lld)\n", |
ca8abba7 | 1629 | i, old_blk, new_blk, diff); |
80c0fc34 | 1630 | #endif |
efc6f628 | 1631 | |
05e112a1 | 1632 | if (!diff) |
052db4b7 TT |
1633 | continue; |
1634 | ||
24a117ab VAH |
1635 | retval = io_channel_read_blk64(fs->io, old_blk, |
1636 | fs->inode_blocks_per_group, | |
1637 | rfs->itable_buf); | |
efc6f628 | 1638 | if (retval) |
a8519a2d | 1639 | goto errout; |
05e112a1 TT |
1640 | /* |
1641 | * The end of the inode table segment often contains | |
a8519a2d TT |
1642 | * all zeros, and we're often only moving the inode |
1643 | * table down a block or two. If so, we can optimize | |
1644 | * things by not rewriting blocks that we know to be zero | |
1645 | * already. | |
05e112a1 | 1646 | */ |
2787276e | 1647 | for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--) |
05e112a1 TT |
1648 | if (*cp) |
1649 | break; | |
1650 | n = n >> EXT2_BLOCK_SIZE_BITS(fs->super); | |
80c0fc34 | 1651 | #ifdef RESIZE2FS_DEBUG |
efc6f628 | 1652 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
f35fd3d5 | 1653 | printf("%d blocks of zeros...\n", n); |
80c0fc34 | 1654 | #endif |
05e112a1 TT |
1655 | num = fs->inode_blocks_per_group; |
1656 | if (n > diff) | |
1657 | num -= n; | |
1658 | ||
24a117ab VAH |
1659 | retval = io_channel_write_blk64(fs->io, new_blk, |
1660 | num, rfs->itable_buf); | |
052db4b7 | 1661 | if (retval) { |
24a117ab VAH |
1662 | io_channel_write_blk64(fs->io, old_blk, |
1663 | num, rfs->itable_buf); | |
a8519a2d | 1664 | goto errout; |
052db4b7 | 1665 | } |
05e112a1 | 1666 | if (n > diff) { |
24a117ab | 1667 | retval = io_channel_write_blk64(fs->io, |
ca8abba7 | 1668 | old_blk + fs->inode_blocks_per_group, |
a8519a2d TT |
1669 | diff, (rfs->itable_buf + |
1670 | (fs->inode_blocks_per_group - diff) * | |
1671 | fs->blocksize)); | |
05e112a1 | 1672 | if (retval) |
a8519a2d | 1673 | goto errout; |
c762c8e6 | 1674 | } |
64ad98ac | 1675 | |
d7cca6b0 | 1676 | for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0; |
64ad98ac | 1677 | j < fs->inode_blocks_per_group ; j++, blk++) |
48f23054 | 1678 | ext2fs_block_alloc_stats2(fs, blk, -1); |
64ad98ac | 1679 | |
d7cca6b0 | 1680 | ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk); |
236efede | 1681 | ext2fs_group_desc_csum_set(rfs->old_fs, i); |
a8519a2d | 1682 | ext2fs_mark_super_dirty(rfs->old_fs); |
64ad98ac TT |
1683 | ext2fs_flush(rfs->old_fs); |
1684 | ||
a8519a2d | 1685 | if (rfs->progress) { |
3b627e8d TT |
1686 | retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, |
1687 | ++moved, to_move); | |
1688 | if (retval) | |
1689 | goto errout; | |
a8519a2d | 1690 | } |
052db4b7 | 1691 | } |
64ad98ac | 1692 | mark_table_blocks(fs, fs->block_map); |
a8519a2d | 1693 | ext2fs_flush(fs); |
80c0fc34 | 1694 | #ifdef RESIZE2FS_DEBUG |
efc6f628 | 1695 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
f35fd3d5 | 1696 | printf("Inode table move finished.\n"); |
80c0fc34 | 1697 | #endif |
052db4b7 | 1698 | return 0; |
efc6f628 | 1699 | |
a8519a2d | 1700 | errout: |
052db4b7 TT |
1701 | return retval; |
1702 | } | |
1703 | ||
9213a93b | 1704 | /* |
efc6f628 | 1705 | * Fix the resize inode |
9213a93b TT |
1706 | */ |
1707 | static errcode_t fix_resize_inode(ext2_filsys fs) | |
1708 | { | |
1709 | struct ext2_inode inode; | |
1710 | errcode_t retval; | |
1711 | char * block_buf; | |
1712 | ||
efc6f628 | 1713 | if (!(fs->super->s_feature_compat & |
9213a93b TT |
1714 | EXT2_FEATURE_COMPAT_RESIZE_INODE)) |
1715 | return 0; | |
1716 | ||
1717 | retval = ext2fs_get_mem(fs->blocksize, &block_buf); | |
1718 | if (retval) goto errout; | |
1719 | ||
1720 | retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode); | |
1721 | if (retval) goto errout; | |
1722 | ||
1ca1059f | 1723 | ext2fs_iblk_set(fs, &inode, 1); |
9213a93b TT |
1724 | |
1725 | retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode); | |
1726 | if (retval) goto errout; | |
1727 | ||
1728 | if (!inode.i_block[EXT2_DIND_BLOCK]) { | |
efc6f628 | 1729 | /* |
9213a93b TT |
1730 | * Avoid zeroing out block #0; that's rude. This |
1731 | * should never happen anyway since the filesystem | |
1732 | * should be fsck'ed and we assume it is consistent. | |
1733 | */ | |
efc6f628 | 1734 | fprintf(stderr, |
f35fd3d5 | 1735 | _("Should never happen: resize inode corrupt!\n")); |
9213a93b TT |
1736 | exit(1); |
1737 | } | |
1738 | ||
1739 | memset(block_buf, 0, fs->blocksize); | |
1740 | ||
24a117ab VAH |
1741 | retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK], |
1742 | 1, block_buf); | |
9213a93b | 1743 | if (retval) goto errout; |
efc6f628 | 1744 | |
9213a93b TT |
1745 | retval = ext2fs_create_resize_inode(fs); |
1746 | if (retval) | |
1747 | goto errout; | |
1748 | ||
1749 | errout: | |
1750 | if (block_buf) | |
1751 | ext2fs_free_mem(&block_buf); | |
1752 | return retval; | |
1753 | } | |
1754 | ||
052db4b7 TT |
1755 | /* |
1756 | * Finally, recalculate the summary information | |
1757 | */ | |
1758 | static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs) | |
1759 | { | |
8728d506 | 1760 | blk64_t blk; |
dfcdc32f | 1761 | ext2_ino_t ino; |
54434927 TT |
1762 | unsigned int group = 0; |
1763 | unsigned int count = 0; | |
dfcdc32f TT |
1764 | int total_free = 0; |
1765 | int group_free = 0; | |
74128f8d | 1766 | int uninit = 0; |
8728d506 | 1767 | blk64_t super_blk, old_desc_blk, new_desc_blk; |
74128f8d | 1768 | int old_desc_blocks; |
052db4b7 TT |
1769 | |
1770 | /* | |
1771 | * First calculate the block statistics | |
1772 | */ | |
cd65a24e | 1773 | uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT); |
8728d506 VAH |
1774 | ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk, |
1775 | &new_desc_blk, 0); | |
74128f8d TT |
1776 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
1777 | old_desc_blocks = fs->super->s_first_meta_bg; | |
1778 | else | |
1779 | old_desc_blocks = fs->desc_blocks + | |
1780 | fs->super->s_reserved_gdt_blocks; | |
052db4b7 | 1781 | for (blk = fs->super->s_first_data_block; |
4efbac6f | 1782 | blk < ext2fs_blocks_count(fs->super); blk++) { |
74128f8d TT |
1783 | if ((uninit && |
1784 | !((blk == super_blk) || | |
1785 | ((old_desc_blk && old_desc_blocks && | |
1786 | (blk >= old_desc_blk) && | |
1787 | (blk < old_desc_blk + old_desc_blocks))) || | |
1788 | ((new_desc_blk && (blk == new_desc_blk))) || | |
d7cca6b0 VAH |
1789 | (blk == ext2fs_block_bitmap_loc(fs, group)) || |
1790 | (blk == ext2fs_inode_bitmap_loc(fs, group)) || | |
1791 | ((blk >= ext2fs_inode_table_loc(fs, group) && | |
1792 | (blk < ext2fs_inode_table_loc(fs, group) | |
74128f8d | 1793 | + fs->inode_blocks_per_group))))) || |
3346084b | 1794 | (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) { |
052db4b7 TT |
1795 | group_free++; |
1796 | total_free++; | |
1797 | } | |
1798 | count++; | |
1799 | if ((count == fs->super->s_blocks_per_group) || | |
4efbac6f | 1800 | (blk == ext2fs_blocks_count(fs->super)-1)) { |
d7cca6b0 | 1801 | ext2fs_bg_free_blocks_count_set(fs, group, group_free); |
236efede JS |
1802 | ext2fs_group_desc_csum_set(fs, group); |
1803 | group++; | |
4828bbe9 TT |
1804 | if (group >= fs->group_desc_count) |
1805 | break; | |
052db4b7 TT |
1806 | count = 0; |
1807 | group_free = 0; | |
8728d506 VAH |
1808 | uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT); |
1809 | ext2fs_super_and_bgd_loc2(fs, group, &super_blk, | |
1810 | &old_desc_blk, | |
1811 | &new_desc_blk, 0); | |
74128f8d TT |
1812 | if (fs->super->s_feature_incompat & |
1813 | EXT2_FEATURE_INCOMPAT_META_BG) | |
1814 | old_desc_blocks = fs->super->s_first_meta_bg; | |
1815 | else | |
1816 | old_desc_blocks = fs->desc_blocks + | |
1817 | fs->super->s_reserved_gdt_blocks; | |
052db4b7 TT |
1818 | } |
1819 | } | |
4efbac6f | 1820 | ext2fs_free_blocks_count_set(fs->super, total_free); |
efc6f628 | 1821 | |
052db4b7 TT |
1822 | /* |
1823 | * Next, calculate the inode statistics | |
1824 | */ | |
1825 | group_free = 0; | |
1826 | total_free = 0; | |
1827 | count = 0; | |
1828 | group = 0; | |
5830d6be ES |
1829 | |
1830 | /* Protect loop from wrap-around if s_inodes_count maxed */ | |
cd65a24e | 1831 | uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT); |
5830d6be | 1832 | for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) { |
74128f8d | 1833 | if (uninit || |
3346084b | 1834 | !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) { |
052db4b7 TT |
1835 | group_free++; |
1836 | total_free++; | |
1837 | } | |
1838 | count++; | |
1839 | if ((count == fs->super->s_inodes_per_group) || | |
1840 | (ino == fs->super->s_inodes_count)) { | |
d7cca6b0 | 1841 | ext2fs_bg_free_inodes_count_set(fs, group, group_free); |
236efede JS |
1842 | ext2fs_group_desc_csum_set(fs, group); |
1843 | group++; | |
4828bbe9 TT |
1844 | if (group >= fs->group_desc_count) |
1845 | break; | |
052db4b7 TT |
1846 | count = 0; |
1847 | group_free = 0; | |
cd65a24e | 1848 | uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT); |
052db4b7 TT |
1849 | } |
1850 | } | |
1851 | fs->super->s_free_inodes_count = total_free; | |
1852 | ext2fs_mark_super_dirty(fs); | |
1853 | return 0; | |
1854 | } | |
199ddaaa | 1855 | |
8a6ede8b ES |
1856 | /* |
1857 | * Journal may have been relocated; update the backup journal blocks | |
1858 | * in the superblock. | |
1859 | */ | |
1860 | static errcode_t fix_sb_journal_backup(ext2_filsys fs) | |
1861 | { | |
1862 | errcode_t retval; | |
1863 | struct ext2_inode inode; | |
1864 | ||
1865 | if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) | |
1866 | return 0; | |
1867 | ||
d93d5bbf ES |
1868 | /* External journal? Nothing to do. */ |
1869 | if (fs->super->s_journal_dev && !fs->super->s_journal_inum) | |
1870 | return 0; | |
1871 | ||
8a6ede8b ES |
1872 | retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode); |
1873 | if (retval) | |
1874 | return retval; | |
1875 | memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4); | |
931b58e1 | 1876 | fs->super->s_jnl_blocks[15] = inode.i_size_high; |
8a6ede8b ES |
1877 | fs->super->s_jnl_blocks[16] = inode.i_size; |
1878 | fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS; | |
1879 | ext2fs_mark_super_dirty(fs); | |
1880 | return 0; | |
1881 | } | |
1882 | ||
199ddaaa JB |
1883 | /* |
1884 | * calcluate the minimum number of blocks the given fs can be resized to | |
1885 | */ | |
8728d506 | 1886 | blk64_t calculate_minimum_resize_size(ext2_filsys fs) |
199ddaaa | 1887 | { |
8728d506 VAH |
1888 | ext2_ino_t inode_count; |
1889 | blk64_t blks_needed, groups, data_blocks; | |
1890 | blk64_t grp, data_needed, last_start; | |
1891 | blk64_t overhead = 0; | |
1892 | int num_of_superblocks = 0; | |
2884d208 PT |
1893 | int extra_groups = 0; |
1894 | int flexbg_size = 1 << fs->super->s_log_groups_per_flex; | |
199ddaaa JB |
1895 | |
1896 | /* | |
1897 | * first figure out how many group descriptors we need to | |
1898 | * handle the number of inodes we have | |
1899 | */ | |
1900 | inode_count = fs->super->s_inodes_count - | |
1901 | fs->super->s_free_inodes_count; | |
1902 | blks_needed = ext2fs_div_ceil(inode_count, | |
1903 | fs->super->s_inodes_per_group) * | |
1904 | EXT2_BLOCKS_PER_GROUP(fs->super); | |
8728d506 VAH |
1905 | groups = ext2fs_div64_ceil(blks_needed, |
1906 | EXT2_BLOCKS_PER_GROUP(fs->super)); | |
199ddaaa JB |
1907 | |
1908 | /* | |
1909 | * we need to figure out how many backup superblocks we have so we can | |
1910 | * account for that in the metadata | |
1911 | */ | |
1912 | for (grp = 0; grp < fs->group_desc_count; grp++) { | |
1913 | if (ext2fs_bg_has_super(fs, grp)) | |
1914 | num_of_superblocks++; | |
1915 | } | |
1916 | ||
1917 | /* calculate how many blocks are needed for data */ | |
4efbac6f VAH |
1918 | data_needed = ext2fs_blocks_count(fs->super) - |
1919 | ext2fs_free_blocks_count(fs->super); | |
199ddaaa JB |
1920 | data_needed -= SUPER_OVERHEAD(fs) * num_of_superblocks; |
1921 | data_needed -= META_OVERHEAD(fs) * fs->group_desc_count; | |
1922 | ||
c09043f1 TT |
1923 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) { |
1924 | /* | |
1925 | * For ext4 we need to allow for up to a flex_bg worth | |
1926 | * of inode tables of slack space so the resize | |
1927 | * operation can be guaranteed to finish. | |
1928 | */ | |
c09043f1 TT |
1929 | extra_groups = flexbg_size - (groups & (flexbg_size - 1)); |
1930 | data_needed += META_OVERHEAD(fs) * extra_groups; | |
2884d208 | 1931 | extra_groups = groups % flexbg_size; |
c09043f1 TT |
1932 | } |
1933 | ||
199ddaaa JB |
1934 | /* |
1935 | * figure out how many data blocks we have given the number of groups | |
1936 | * we need for our inodes | |
1937 | */ | |
1938 | data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super); | |
1939 | last_start = 0; | |
1940 | for (grp = 0; grp < groups; grp++) { | |
1941 | overhead = META_OVERHEAD(fs); | |
1942 | ||
1943 | if (ext2fs_bg_has_super(fs, grp)) | |
1944 | overhead += SUPER_OVERHEAD(fs); | |
1945 | ||
1946 | /* | |
1947 | * we want to keep track of how much data we can store in | |
1948 | * the groups leading up to the last group so we can determine | |
1949 | * how big the last group needs to be | |
1950 | */ | |
1951 | if (grp != (groups - 1)) | |
1952 | last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - | |
1953 | overhead; | |
1954 | ||
1955 | data_blocks -= overhead; | |
1956 | } | |
1957 | ||
1958 | /* | |
1959 | * if we need more group descriptors in order to accomodate our data | |
1960 | * then we need to add them here | |
1961 | */ | |
1962 | while (data_needed > data_blocks) { | |
8728d506 VAH |
1963 | blk64_t remainder = data_needed - data_blocks; |
1964 | blk64_t extra_grps; | |
199ddaaa JB |
1965 | |
1966 | /* figure out how many more groups we need for the data */ | |
8728d506 VAH |
1967 | extra_grps = ext2fs_div64_ceil(remainder, |
1968 | EXT2_BLOCKS_PER_GROUP(fs->super)); | |
199ddaaa JB |
1969 | |
1970 | data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super); | |
1971 | ||
1972 | /* ok we have to account for the last group */ | |
1973 | overhead = META_OVERHEAD(fs); | |
1974 | if (ext2fs_bg_has_super(fs, groups-1)) | |
1975 | overhead += SUPER_OVERHEAD(fs); | |
1976 | last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead; | |
1977 | ||
1978 | for (grp = groups; grp < groups+extra_grps; grp++) { | |
1979 | overhead = META_OVERHEAD(fs); | |
1980 | if (ext2fs_bg_has_super(fs, grp)) | |
1981 | overhead += SUPER_OVERHEAD(fs); | |
1982 | ||
1983 | /* | |
1984 | * again, we need to see how much data we cram into | |
1985 | * all of the groups leading up to the last group | |
1986 | */ | |
1987 | if (grp != (groups + extra_grps - 1)) | |
1988 | last_start += EXT2_BLOCKS_PER_GROUP(fs->super) | |
1989 | - overhead; | |
1990 | ||
1991 | data_blocks -= overhead; | |
1992 | } | |
1993 | ||
1994 | groups += extra_grps; | |
2884d208 PT |
1995 | extra_groups += extra_grps; |
1996 | if (fs->super->s_feature_incompat | |
1997 | & EXT4_FEATURE_INCOMPAT_FLEX_BG | |
1998 | && extra_groups > flexbg_size) { | |
1999 | /* | |
2000 | * For ext4 we need to allow for up to a flex_bg worth | |
2001 | * of inode tables of slack space so the resize | |
2002 | * operation can be guaranteed to finish. | |
2003 | */ | |
2004 | extra_groups = flexbg_size - | |
2005 | (groups & (flexbg_size - 1)); | |
2006 | data_needed += META_OVERHEAD(fs) * extra_groups; | |
2007 | extra_groups = groups % flexbg_size; | |
2008 | } | |
199ddaaa JB |
2009 | } |
2010 | ||
2011 | /* now for the fun voodoo */ | |
2012 | overhead = META_OVERHEAD(fs); | |
2013 | ||
2014 | /* | |
2015 | * if this is the case then the last group is going to have data in it | |
2016 | * so we need to adjust the size of the last group accordingly | |
2017 | */ | |
2018 | if (last_start < data_needed) { | |
8728d506 | 2019 | blk64_t remainder = data_needed - last_start; |
199ddaaa JB |
2020 | |
2021 | /* | |
2022 | * 50 is a magic number that mkfs/resize uses to see if its | |
2023 | * even worth making/resizing the fs. basically you need to | |
2024 | * have at least 50 blocks in addition to the blocks needed | |
2025 | * for the metadata in the last group | |
2026 | */ | |
2027 | if (remainder > 50) | |
2028 | overhead += remainder; | |
2029 | else | |
2030 | overhead += 50; | |
2031 | } else | |
2032 | overhead += 50; | |
2033 | ||
2034 | if (ext2fs_bg_has_super(fs, groups-1)) | |
2035 | overhead += SUPER_OVERHEAD(fs); | |
ba8bfa1a | 2036 | overhead += fs->super->s_first_data_block; |
199ddaaa JB |
2037 | |
2038 | /* | |
2039 | * since our last group doesn't have to be BLOCKS_PER_GROUP large, we | |
2040 | * only do groups-1, and then add the number of blocks needed to | |
2041 | * handle the group descriptor metadata+data that we need | |
2042 | */ | |
2043 | blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super); | |
2044 | blks_needed += overhead; | |
2045 | ||
69f7c80e ES |
2046 | /* |
2047 | * If at this point we've already added up more "needed" than | |
2048 | * the current size, just return current size as minimum. | |
2049 | */ | |
4efbac6f VAH |
2050 | if (blks_needed >= ext2fs_blocks_count(fs->super)) |
2051 | return ext2fs_blocks_count(fs->super); | |
793a04a0 TT |
2052 | /* |
2053 | * We need to reserve a few extra blocks if extents are | |
2054 | * enabled, in case we need to grow the extent tree. The more | |
2055 | * we shrink the file system, the more space we need. | |
2056 | */ | |
2057 | if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) | |
4efbac6f VAH |
2058 | blks_needed += (ext2fs_blocks_count(fs->super) - |
2059 | blks_needed)/500; | |
793a04a0 | 2060 | |
199ddaaa JB |
2061 | return blks_needed; |
2062 | } |