]>
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 | |
24b2c7a7 TT |
8 | * |
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 | |
24b2c7a7 | 36 | #include "resize2fs.h" |
b969b1b8 | 37 | #include <time.h> |
24b2c7a7 | 38 | |
546a1ff1 | 39 | #ifdef __linux__ /* Kludge for debugging */ |
a8519a2d TT |
40 | #define RESIZE2FS_DEBUG |
41 | #endif | |
42 | ||
43 | static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size); | |
44 | static errcode_t blocks_to_move(ext2_resize_t rfs); | |
45 | static errcode_t block_mover(ext2_resize_t rfs); | |
46 | static errcode_t inode_scan_and_fix(ext2_resize_t rfs); | |
47 | static errcode_t inode_ref_fix(ext2_resize_t rfs); | |
48 | static errcode_t move_itables(ext2_resize_t rfs); | |
9213a93b | 49 | static errcode_t fix_resize_inode(ext2_filsys fs); |
a8519a2d TT |
50 | static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs); |
51 | ||
52 | /* | |
53 | * Some helper CPP macros | |
54 | */ | |
55 | #define FS_BLOCK_BM(fs, i) ((fs)->group_desc[(i)].bg_block_bitmap) | |
56 | #define FS_INODE_BM(fs, i) ((fs)->group_desc[(i)].bg_inode_bitmap) | |
57 | #define FS_INODE_TB(fs, i) ((fs)->group_desc[(i)].bg_inode_table) | |
58 | ||
59 | #define IS_BLOCK_BM(fs, i, blk) ((blk) == FS_BLOCK_BM((fs),(i))) | |
60 | #define IS_INODE_BM(fs, i, blk) ((blk) == FS_INODE_BM((fs),(i))) | |
61 | ||
62 | #define IS_INODE_TB(fs, i, blk) (((blk) >= FS_INODE_TB((fs), (i))) && \ | |
63 | ((blk) < (FS_INODE_TB((fs), (i)) + \ | |
64 | (fs)->inode_blocks_per_group))) | |
65 | ||
66 | ||
67 | ||
68 | /* | |
69 | * This is the top-level routine which does the dirty deed.... | |
70 | */ | |
116db1b5 | 71 | errcode_t resize_fs(ext2_filsys fs, blk_t *new_size, int flags, |
3b627e8d | 72 | errcode_t (*progress)(ext2_resize_t rfs, int pass, |
a8519a2d | 73 | unsigned long cur, |
1333fe93 | 74 | unsigned long max_val)) |
a8519a2d TT |
75 | { |
76 | ext2_resize_t rfs; | |
77 | errcode_t retval; | |
78 | ||
79 | retval = ext2fs_read_bitmaps(fs); | |
80 | if (retval) | |
81 | return retval; | |
82 | ||
83 | /* | |
84 | * Create the data structure | |
85 | */ | |
c4e3d3f3 | 86 | retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs); |
a8519a2d TT |
87 | if (retval) |
88 | return retval; | |
89 | memset(rfs, 0, sizeof(struct ext2_resize_struct)); | |
90 | ||
91 | rfs->old_fs = fs; | |
92 | rfs->flags = flags; | |
93 | rfs->itable_buf = 0; | |
94 | rfs->progress = progress; | |
95 | retval = ext2fs_dup_handle(fs, &rfs->new_fs); | |
96 | if (retval) | |
97 | goto errout; | |
98 | ||
116db1b5 | 99 | retval = adjust_superblock(rfs, *new_size); |
a8519a2d TT |
100 | if (retval) |
101 | goto errout; | |
102 | ||
116db1b5 TT |
103 | *new_size = rfs->new_fs->super->s_blocks_count; |
104 | ||
a8519a2d TT |
105 | retval = blocks_to_move(rfs); |
106 | if (retval) | |
107 | goto errout; | |
108 | ||
109 | #ifdef RESIZE2FS_DEBUG | |
110 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
8deb80a5 | 111 | printf("Number of free blocks: %u/%u, Needed: %d\n", |
a8519a2d TT |
112 | rfs->old_fs->super->s_free_blocks_count, |
113 | rfs->new_fs->super->s_free_blocks_count, | |
114 | rfs->needed_blocks); | |
115 | #endif | |
116 | ||
117 | retval = block_mover(rfs); | |
118 | if (retval) | |
119 | goto errout; | |
120 | ||
121 | retval = inode_scan_and_fix(rfs); | |
122 | if (retval) | |
123 | goto errout; | |
124 | ||
125 | retval = inode_ref_fix(rfs); | |
126 | if (retval) | |
127 | goto errout; | |
128 | ||
a8519a2d TT |
129 | retval = move_itables(rfs); |
130 | if (retval) | |
131 | goto errout; | |
132 | ||
64ad98ac TT |
133 | retval = ext2fs_calculate_summary_stats(rfs->new_fs); |
134 | if (retval) | |
135 | goto errout; | |
136 | ||
9213a93b TT |
137 | retval = fix_resize_inode(rfs->new_fs); |
138 | if (retval) | |
139 | goto errout; | |
140 | ||
058ad1c7 | 141 | rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
a8519a2d TT |
142 | retval = ext2fs_close(rfs->new_fs); |
143 | if (retval) | |
144 | goto errout; | |
145 | ||
146 | rfs->flags = flags; | |
147 | ||
148 | ext2fs_free(rfs->old_fs); | |
149 | if (rfs->itable_buf) | |
c4e3d3f3 TT |
150 | ext2fs_free_mem(&rfs->itable_buf); |
151 | ext2fs_free_mem(&rfs); | |
a8519a2d TT |
152 | |
153 | return 0; | |
154 | ||
155 | errout: | |
156 | if (rfs->new_fs) | |
157 | ext2fs_free(rfs->new_fs); | |
158 | if (rfs->itable_buf) | |
c4e3d3f3 TT |
159 | ext2fs_free_mem(&rfs->itable_buf); |
160 | ext2fs_free_mem(&rfs); | |
a8519a2d TT |
161 | return retval; |
162 | } | |
163 | ||
164 | /* -------------------------------------------------------------------- | |
165 | * | |
166 | * Resize processing, phase 1. | |
167 | * | |
168 | * In this phase we adjust the in-memory superblock information, and | |
169 | * initialize any new parts of the inode table. The new parts of the | |
170 | * inode table are created in virgin disk space, so we can abort here | |
171 | * without any side effects. | |
172 | * -------------------------------------------------------------------- | |
173 | */ | |
174 | ||
24b2c7a7 | 175 | /* |
bf69235a TT |
176 | * This routine is shared by the online and offline resize routines. |
177 | * All of the information which is adjusted in memory is done here. | |
24b2c7a7 | 178 | */ |
bf69235a | 179 | errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size) |
24b2c7a7 | 180 | { |
24b2c7a7 | 181 | errcode_t retval; |
bf69235a TT |
182 | int overhead = 0; |
183 | int rem; | |
24b2c7a7 | 184 | blk_t blk, group_block; |
bf69235a TT |
185 | ext2_ino_t real_end; |
186 | int adj, old_numblocks, numblocks, adjblocks; | |
187 | unsigned long i, j, old_desc_blocks, max_group; | |
54434927 TT |
188 | unsigned int meta_bg, meta_bg_size; |
189 | int has_super; | |
de8f3a76 | 190 | unsigned long long new_inodes; /* u64 to check for overflow */ |
bf69235a | 191 | |
24b2c7a7 TT |
192 | fs->super->s_blocks_count = new_size; |
193 | ||
194 | retry: | |
69022e02 TT |
195 | fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count - |
196 | fs->super->s_first_data_block, | |
197 | EXT2_BLOCKS_PER_GROUP(fs->super)); | |
24b2c7a7 TT |
198 | if (fs->group_desc_count == 0) |
199 | return EXT2_ET_TOOSMALL; | |
69022e02 TT |
200 | fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count, |
201 | EXT2_DESC_PER_BLOCK(fs->super)); | |
24b2c7a7 TT |
202 | |
203 | /* | |
204 | * Overhead is the number of bookkeeping blocks per group. It | |
205 | * includes the superblock backup, the group descriptor | |
206 | * backups, the inode bitmap, the block bitmap, and the inode | |
207 | * table. | |
24b2c7a7 | 208 | */ |
9213a93b TT |
209 | overhead = (int) (2 + fs->inode_blocks_per_group); |
210 | ||
211 | if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1)) | |
212 | overhead += 1 + fs->desc_blocks + | |
213 | fs->super->s_reserved_gdt_blocks; | |
214 | ||
24b2c7a7 TT |
215 | /* |
216 | * See if the last group is big enough to support the | |
217 | * necessary data structures. If not, we need to get rid of | |
218 | * it. | |
219 | */ | |
220 | rem = (fs->super->s_blocks_count - fs->super->s_first_data_block) % | |
221 | fs->super->s_blocks_per_group; | |
222 | if ((fs->group_desc_count == 1) && rem && (rem < overhead)) | |
223 | return EXT2_ET_TOOSMALL; | |
224 | if (rem && (rem < overhead+50)) { | |
225 | fs->super->s_blocks_count -= rem; | |
226 | goto retry; | |
227 | } | |
228 | /* | |
229 | * Adjust the number of inodes | |
230 | */ | |
de8f3a76 | 231 | new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count; |
f3358643 ES |
232 | if (new_inodes > ~0U) { |
233 | fprintf(stderr, _("inodes (%llu) must be less than %u"), | |
234 | new_inodes, ~0U); | |
235 | return EXT2_ET_TOO_MANY_INODES; | |
236 | } | |
24b2c7a7 TT |
237 | fs->super->s_inodes_count = fs->super->s_inodes_per_group * |
238 | fs->group_desc_count; | |
239 | ||
240 | /* | |
241 | * Adjust the number of free blocks | |
242 | */ | |
bf69235a | 243 | blk = old_fs->super->s_blocks_count; |
24b2c7a7 TT |
244 | if (blk > fs->super->s_blocks_count) |
245 | fs->super->s_free_blocks_count -= | |
246 | (blk - fs->super->s_blocks_count); | |
247 | else | |
248 | fs->super->s_free_blocks_count += | |
249 | (fs->super->s_blocks_count - blk); | |
250 | ||
c762c8e6 TT |
251 | /* |
252 | * Adjust the number of reserved blocks | |
253 | */ | |
d1b4b85c | 254 | blk = (__u64)old_fs->super->s_r_blocks_count * 100 / |
bf69235a | 255 | old_fs->super->s_blocks_count; |
a8862d9e TT |
256 | fs->super->s_r_blocks_count = e2p_percent(blk, |
257 | fs->super->s_blocks_count); | |
c762c8e6 | 258 | |
24b2c7a7 TT |
259 | /* |
260 | * Adjust the bitmaps for size | |
261 | */ | |
262 | retval = ext2fs_resize_inode_bitmap(fs->super->s_inodes_count, | |
263 | fs->super->s_inodes_count, | |
264 | fs->inode_map); | |
c762c8e6 | 265 | if (retval) goto errout; |
24b2c7a7 TT |
266 | |
267 | real_end = ((EXT2_BLOCKS_PER_GROUP(fs->super) | |
268 | * fs->group_desc_count)) - 1 + | |
269 | fs->super->s_first_data_block; | |
270 | retval = ext2fs_resize_block_bitmap(fs->super->s_blocks_count-1, | |
271 | real_end, fs->block_map); | |
272 | ||
c762c8e6 | 273 | if (retval) goto errout; |
24b2c7a7 TT |
274 | |
275 | /* | |
276 | * Reallocate the group descriptors as necessary. | |
277 | */ | |
bf69235a TT |
278 | if (old_fs->desc_blocks != fs->desc_blocks) { |
279 | retval = ext2fs_resize_mem(old_fs->desc_blocks * | |
76f875da TT |
280 | fs->blocksize, |
281 | fs->desc_blocks * fs->blocksize, | |
c4e3d3f3 | 282 | &fs->group_desc); |
ca8abba7 | 283 | if (retval) |
a8519a2d | 284 | goto errout; |
bf69235a | 285 | if (fs->desc_blocks > old_fs->desc_blocks) |
2787276e | 286 | memset((char *) fs->group_desc + |
bf69235a TT |
287 | (old_fs->desc_blocks * fs->blocksize), 0, |
288 | (fs->desc_blocks - old_fs->desc_blocks) * | |
2787276e | 289 | fs->blocksize); |
24b2c7a7 | 290 | } |
1e1da29f | 291 | |
9213a93b TT |
292 | /* |
293 | * If the resize_inode feature is set, and we are changing the | |
294 | * number of descriptor blocks, then adjust | |
295 | * s_reserved_gdt_blocks if possible to avoid needing to move | |
296 | * the inode table either now or in the future. | |
297 | */ | |
298 | if ((fs->super->s_feature_compat & | |
299 | EXT2_FEATURE_COMPAT_RESIZE_INODE) && | |
bf69235a | 300 | (old_fs->desc_blocks != fs->desc_blocks)) { |
9213a93b TT |
301 | int new; |
302 | ||
303 | new = ((int) fs->super->s_reserved_gdt_blocks) + | |
bf69235a | 304 | (old_fs->desc_blocks - fs->desc_blocks); |
9213a93b TT |
305 | if (new < 0) |
306 | new = 0; | |
de8f3a76 | 307 | if (new > (int) fs->blocksize/4) |
9213a93b TT |
308 | new = fs->blocksize/4; |
309 | fs->super->s_reserved_gdt_blocks = new; | |
310 | if (new == 0) | |
311 | fs->super->s_feature_compat &= | |
312 | ~EXT2_FEATURE_COMPAT_RESIZE_INODE; | |
313 | } | |
314 | ||
a8519a2d TT |
315 | /* |
316 | * If we are shrinking the number block groups, we're done and | |
317 | * can exit now. | |
1e1da29f | 318 | */ |
bf69235a | 319 | if (old_fs->group_desc_count > fs->group_desc_count) { |
c762c8e6 TT |
320 | retval = 0; |
321 | goto errout; | |
322 | } | |
bf69235a | 323 | |
a8519a2d TT |
324 | /* |
325 | * Fix the count of the last (old) block group | |
326 | */ | |
bf69235a TT |
327 | old_numblocks = (old_fs->super->s_blocks_count - |
328 | old_fs->super->s_first_data_block) % | |
329 | old_fs->super->s_blocks_per_group; | |
1e1da29f | 330 | if (!old_numblocks) |
bf69235a TT |
331 | old_numblocks = old_fs->super->s_blocks_per_group; |
332 | if (old_fs->group_desc_count == fs->group_desc_count) { | |
333 | numblocks = (fs->super->s_blocks_count - | |
334 | fs->super->s_first_data_block) % | |
335 | fs->super->s_blocks_per_group; | |
1e1da29f | 336 | if (!numblocks) |
bf69235a | 337 | numblocks = fs->super->s_blocks_per_group; |
1e1da29f | 338 | } else |
bf69235a TT |
339 | numblocks = fs->super->s_blocks_per_group; |
340 | i = old_fs->group_desc_count - 1; | |
1e1da29f TT |
341 | fs->group_desc[i].bg_free_blocks_count += (numblocks-old_numblocks); |
342 | ||
343 | /* | |
a8519a2d TT |
344 | * If the number of block groups is staying the same, we're |
345 | * done and can exit now. (If the number block groups is | |
346 | * shrinking, we had exited earlier.) | |
1e1da29f | 347 | */ |
bf69235a | 348 | if (old_fs->group_desc_count >= fs->group_desc_count) { |
c762c8e6 TT |
349 | retval = 0; |
350 | goto errout; | |
351 | } | |
bf69235a | 352 | |
a8519a2d TT |
353 | /* |
354 | * Initialize the new block group descriptors | |
355 | */ | |
1e1da29f | 356 | group_block = fs->super->s_first_data_block + |
bf69235a | 357 | old_fs->group_desc_count * fs->super->s_blocks_per_group; |
c762c8e6 | 358 | |
bf69235a | 359 | adj = old_fs->group_desc_count; |
63b44fbe | 360 | max_group = fs->group_desc_count - adj; |
76dd5e5c TT |
361 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
362 | old_desc_blocks = fs->super->s_first_meta_bg; | |
363 | else | |
9213a93b TT |
364 | old_desc_blocks = fs->desc_blocks + |
365 | fs->super->s_reserved_gdt_blocks; | |
bf69235a | 366 | for (i = old_fs->group_desc_count; |
1e1da29f TT |
367 | i < fs->group_desc_count; i++) { |
368 | memset(&fs->group_desc[i], 0, | |
369 | sizeof(struct ext2_group_desc)); | |
370 | adjblocks = 0; | |
371 | ||
372 | if (i == fs->group_desc_count-1) { | |
373 | numblocks = (fs->super->s_blocks_count - | |
374 | fs->super->s_first_data_block) % | |
375 | fs->super->s_blocks_per_group; | |
376 | if (!numblocks) | |
377 | numblocks = fs->super->s_blocks_per_group; | |
378 | } else | |
379 | numblocks = fs->super->s_blocks_per_group; | |
380 | ||
76dd5e5c TT |
381 | has_super = ext2fs_bg_has_super(fs, i); |
382 | if (has_super) { | |
383 | ext2fs_mark_block_bitmap(fs->block_map, group_block); | |
384 | adjblocks++; | |
385 | } | |
f2de1d38 | 386 | meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super); |
76dd5e5c TT |
387 | meta_bg = i / meta_bg_size; |
388 | if (!(fs->super->s_feature_incompat & | |
389 | EXT2_FEATURE_INCOMPAT_META_BG) || | |
390 | (meta_bg < fs->super->s_first_meta_bg)) { | |
391 | if (has_super) { | |
392 | for (j=0; j < old_desc_blocks; j++) | |
393 | ext2fs_mark_block_bitmap(fs->block_map, | |
394 | group_block + 1 + j); | |
395 | adjblocks += old_desc_blocks; | |
396 | } | |
397 | } else { | |
398 | if (has_super) | |
399 | has_super = 1; | |
400 | if (((i % meta_bg_size) == 0) || | |
401 | ((i % meta_bg_size) == 1) || | |
402 | ((i % meta_bg_size) == (meta_bg_size-1))) | |
1e1da29f | 403 | ext2fs_mark_block_bitmap(fs->block_map, |
76dd5e5c | 404 | group_block + has_super); |
24b2c7a7 | 405 | } |
76dd5e5c | 406 | |
1e1da29f TT |
407 | adjblocks += 2 + fs->inode_blocks_per_group; |
408 | ||
409 | numblocks -= adjblocks; | |
410 | fs->super->s_free_blocks_count -= adjblocks; | |
411 | fs->super->s_free_inodes_count += | |
412 | fs->super->s_inodes_per_group; | |
413 | fs->group_desc[i].bg_free_blocks_count = numblocks; | |
414 | fs->group_desc[i].bg_free_inodes_count = | |
415 | fs->super->s_inodes_per_group; | |
416 | fs->group_desc[i].bg_used_dirs_count = 0; | |
24b2c7a7 | 417 | |
1e1da29f | 418 | retval = ext2fs_allocate_group_table(fs, i, 0); |
c762c8e6 | 419 | if (retval) goto errout; |
24b2c7a7 | 420 | |
bf69235a TT |
421 | group_block += fs->super->s_blocks_per_group; |
422 | } | |
423 | retval = 0; | |
424 | ||
425 | errout: | |
426 | return (retval); | |
427 | } | |
428 | ||
429 | /* | |
430 | * This routine adjusts the superblock and other data structures, both | |
431 | * in disk as well as in memory... | |
432 | */ | |
433 | static errcode_t adjust_superblock(ext2_resize_t rfs, blk_t new_size) | |
434 | { | |
435 | ext2_filsys fs; | |
436 | int adj = 0; | |
437 | errcode_t retval; | |
438 | blk_t group_block; | |
439 | unsigned long i; | |
440 | unsigned long max_group; | |
441 | ||
442 | fs = rfs->new_fs; | |
443 | ext2fs_mark_super_dirty(fs); | |
444 | ext2fs_mark_bb_dirty(fs); | |
445 | ext2fs_mark_ib_dirty(fs); | |
446 | ||
447 | retval = adjust_fs_info(fs, rfs->old_fs, new_size); | |
448 | if (retval) | |
449 | goto errout; | |
450 | ||
451 | /* | |
452 | * Check to make sure there are enough inodes | |
453 | */ | |
454 | if ((rfs->old_fs->super->s_inodes_count - | |
455 | rfs->old_fs->super->s_free_inodes_count) > | |
456 | rfs->new_fs->super->s_inodes_count) { | |
457 | retval = ENOSPC; | |
458 | goto errout; | |
459 | } | |
460 | ||
461 | /* | |
462 | * If we are shrinking the number block groups, we're done and | |
463 | * can exit now. | |
464 | */ | |
465 | if (rfs->old_fs->group_desc_count > fs->group_desc_count) { | |
466 | retval = 0; | |
467 | goto errout; | |
468 | } | |
469 | ||
470 | /* | |
471 | * If the number of block groups is staying the same, we're | |
472 | * done and can exit now. (If the number block groups is | |
473 | * shrinking, we had exited earlier.) | |
474 | */ | |
475 | if (rfs->old_fs->group_desc_count >= fs->group_desc_count) { | |
476 | retval = 0; | |
477 | goto errout; | |
478 | } | |
479 | ||
480 | /* | |
481 | * Initialize the new block group descriptors | |
482 | */ | |
e5aace90 | 483 | retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group, |
bf69235a TT |
484 | &rfs->itable_buf); |
485 | if (retval) | |
486 | goto errout; | |
487 | ||
488 | memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group); | |
489 | group_block = fs->super->s_first_data_block + | |
490 | rfs->old_fs->group_desc_count * fs->super->s_blocks_per_group; | |
491 | ||
492 | adj = rfs->old_fs->group_desc_count; | |
493 | max_group = fs->group_desc_count - adj; | |
494 | if (rfs->progress) { | |
495 | retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS, | |
496 | 0, max_group); | |
497 | if (retval) | |
498 | goto errout; | |
499 | } | |
500 | for (i = rfs->old_fs->group_desc_count; | |
501 | i < fs->group_desc_count; i++) { | |
05e112a1 TT |
502 | /* |
503 | * Write out the new inode table | |
504 | */ | |
505 | retval = io_channel_write_blk(fs->io, | |
506 | fs->group_desc[i].bg_inode_table, | |
507 | fs->inode_blocks_per_group, | |
508 | rfs->itable_buf); | |
c762c8e6 TT |
509 | if (retval) goto errout; |
510 | ||
a8519a2d | 511 | io_channel_flush(fs->io); |
3b627e8d TT |
512 | if (rfs->progress) { |
513 | retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS, | |
514 | i - adj + 1, max_group); | |
515 | if (retval) | |
516 | goto errout; | |
517 | } | |
1e1da29f | 518 | group_block += fs->super->s_blocks_per_group; |
24b2c7a7 | 519 | } |
c762c8e6 TT |
520 | io_channel_flush(fs->io); |
521 | retval = 0; | |
522 | ||
523 | errout: | |
c762c8e6 TT |
524 | return retval; |
525 | } | |
526 | ||
a8519a2d TT |
527 | /* -------------------------------------------------------------------- |
528 | * | |
529 | * Resize processing, phase 2. | |
530 | * | |
531 | * In this phase we adjust determine which blocks need to be moved, in | |
532 | * blocks_to_move(). We then copy the blocks to their ultimate new | |
533 | * destinations using block_mover(). Since we are copying blocks to | |
534 | * their new locations, again during this pass we can abort without | |
535 | * any problems. | |
536 | * -------------------------------------------------------------------- | |
537 | */ | |
538 | ||
c762c8e6 TT |
539 | /* |
540 | * This helper function creates a block bitmap with all of the | |
541 | * filesystem meta-data blocks. | |
542 | */ | |
543 | static errcode_t mark_table_blocks(ext2_filsys fs, | |
64ad98ac | 544 | ext2fs_block_bitmap bmap) |
c762c8e6 | 545 | { |
62c6d140 | 546 | blk_t b; |
54434927 TT |
547 | unsigned int j; |
548 | dgrp_t i; | |
68963d5a | 549 | unsigned long meta_bg_size; |
54434927 | 550 | unsigned int old_desc_blocks; |
c762c8e6 | 551 | |
f2de1d38 | 552 | meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super); |
76dd5e5c TT |
553 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
554 | old_desc_blocks = fs->super->s_first_meta_bg; | |
555 | else | |
9213a93b TT |
556 | old_desc_blocks = fs->desc_blocks + |
557 | fs->super->s_reserved_gdt_blocks; | |
c762c8e6 | 558 | for (i = 0; i < fs->group_desc_count; i++) { |
64ad98ac | 559 | ext2fs_reserve_super_and_bgd(fs, i, bmap); |
76dd5e5c | 560 | |
c762c8e6 TT |
561 | /* |
562 | * Mark the blocks used for the inode table | |
563 | */ | |
564 | for (j = 0, b = fs->group_desc[i].bg_inode_table; | |
54434927 | 565 | j < (unsigned int) fs->inode_blocks_per_group; |
c762c8e6 TT |
566 | j++, b++) |
567 | ext2fs_mark_block_bitmap(bmap, b); | |
568 | ||
569 | /* | |
570 | * Mark block used for the block bitmap | |
571 | */ | |
572 | ext2fs_mark_block_bitmap(bmap, | |
573 | fs->group_desc[i].bg_block_bitmap); | |
64ad98ac | 574 | |
c762c8e6 TT |
575 | /* |
576 | * Mark block used for the inode bitmap | |
577 | */ | |
578 | ext2fs_mark_block_bitmap(bmap, | |
579 | fs->group_desc[i].bg_inode_bitmap); | |
c762c8e6 | 580 | } |
1e1da29f | 581 | return 0; |
24b2c7a7 TT |
582 | } |
583 | ||
76dd5e5c TT |
584 | /* |
585 | * This function checks to see if a particular block (either a | |
586 | * superblock or a block group descriptor) overlaps with an inode or | |
587 | * block bitmap block, or with the inode table. | |
588 | */ | |
589 | static void mark_fs_metablock(ext2_resize_t rfs, | |
590 | ext2fs_block_bitmap meta_bmap, | |
591 | int group, blk_t blk) | |
592 | { | |
593 | ext2_filsys fs = rfs->new_fs; | |
594 | ||
595 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk); | |
596 | ext2fs_mark_block_bitmap(fs->block_map, blk); | |
597 | ||
598 | /* | |
599 | * Check to see if we overlap with the inode or block bitmap, | |
600 | * or the inode tables. If not, and the block is in use, then | |
601 | * mark it as a block to be moved. | |
602 | */ | |
603 | if (IS_BLOCK_BM(fs, group, blk)) { | |
604 | FS_BLOCK_BM(fs, group) = 0; | |
605 | rfs->needed_blocks++; | |
606 | } else if (IS_INODE_BM(fs, group, blk)) { | |
607 | FS_INODE_BM(fs, group) = 0; | |
608 | rfs->needed_blocks++; | |
609 | } else if (IS_INODE_TB(fs, group, blk)) { | |
610 | FS_INODE_TB(fs, group) = 0; | |
611 | rfs->needed_blocks++; | |
612 | } else if (ext2fs_test_block_bitmap(rfs->old_fs->block_map, blk) && | |
613 | !ext2fs_test_block_bitmap(meta_bmap, blk)) { | |
614 | ext2fs_mark_block_bitmap(rfs->move_blocks, blk); | |
615 | rfs->needed_blocks++; | |
616 | } | |
617 | } | |
618 | ||
619 | ||
24b2c7a7 TT |
620 | /* |
621 | * This routine marks and unmarks reserved blocks in the new block | |
622 | * bitmap. It also determines which blocks need to be moved and | |
623 | * places this information into the move_blocks bitmap. | |
624 | */ | |
c762c8e6 | 625 | static errcode_t blocks_to_move(ext2_resize_t rfs) |
24b2c7a7 | 626 | { |
54434927 TT |
627 | int j, has_super; |
628 | dgrp_t i, max_groups; | |
629 | blk_t blk, group_blk; | |
630 | unsigned long old_blocks, new_blocks; | |
631 | unsigned int meta_bg, meta_bg_size; | |
24b2c7a7 | 632 | errcode_t retval; |
c762c8e6 TT |
633 | ext2_filsys fs, old_fs; |
634 | ext2fs_block_bitmap meta_bmap; | |
24b2c7a7 | 635 | |
c762c8e6 TT |
636 | fs = rfs->new_fs; |
637 | old_fs = rfs->old_fs; | |
638 | if (old_fs->super->s_blocks_count > fs->super->s_blocks_count) | |
639 | fs = rfs->old_fs; | |
640 | ||
a13575f4 | 641 | retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"), |
1e1da29f | 642 | &rfs->reserve_blocks); |
24b2c7a7 TT |
643 | if (retval) |
644 | return retval; | |
1e1da29f | 645 | |
a13575f4 | 646 | retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"), |
c762c8e6 TT |
647 | &rfs->move_blocks); |
648 | if (retval) | |
649 | return retval; | |
650 | ||
64ad98ac TT |
651 | retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"), |
652 | &meta_bmap); | |
653 | if (retval) | |
654 | return retval; | |
655 | ||
656 | retval = mark_table_blocks(old_fs, meta_bmap); | |
c762c8e6 TT |
657 | if (retval) |
658 | return retval; | |
659 | ||
660 | fs = rfs->new_fs; | |
661 | ||
1e1da29f TT |
662 | /* |
663 | * If we're shrinking the filesystem, we need to move all of | |
664 | * the blocks that don't fit any more | |
665 | */ | |
666 | for (blk = fs->super->s_blocks_count; | |
c762c8e6 TT |
667 | blk < old_fs->super->s_blocks_count; blk++) { |
668 | if (ext2fs_test_block_bitmap(old_fs->block_map, blk) && | |
669 | !ext2fs_test_block_bitmap(meta_bmap, blk)) { | |
670 | ext2fs_mark_block_bitmap(rfs->move_blocks, blk); | |
1e1da29f | 671 | rfs->needed_blocks++; |
c762c8e6 | 672 | } |
1e1da29f TT |
673 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk); |
674 | } | |
24b2c7a7 | 675 | |
76dd5e5c TT |
676 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) { |
677 | old_blocks = old_fs->super->s_first_meta_bg; | |
678 | new_blocks = fs->super->s_first_meta_bg; | |
679 | } else { | |
9213a93b TT |
680 | old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks; |
681 | new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks; | |
76dd5e5c TT |
682 | } |
683 | ||
c762c8e6 TT |
684 | if (old_blocks == new_blocks) { |
685 | retval = 0; | |
686 | goto errout; | |
687 | } | |
24b2c7a7 | 688 | |
1333fe93 TT |
689 | max_groups = fs->group_desc_count; |
690 | if (max_groups > old_fs->group_desc_count) | |
691 | max_groups = old_fs->group_desc_count; | |
c762c8e6 | 692 | group_blk = old_fs->super->s_first_data_block; |
24b2c7a7 TT |
693 | /* |
694 | * If we're reducing the number of descriptor blocks, this | |
695 | * makes life easy. :-) We just have to mark some extra | |
696 | * blocks as free. | |
697 | */ | |
698 | if (old_blocks > new_blocks) { | |
1333fe93 | 699 | for (i = 0; i < max_groups; i++) { |
1e1da29f TT |
700 | if (!ext2fs_bg_has_super(fs, i)) { |
701 | group_blk += fs->super->s_blocks_per_group; | |
24b2c7a7 TT |
702 | continue; |
703 | } | |
c762c8e6 TT |
704 | for (blk = group_blk+1+new_blocks; |
705 | blk < group_blk+1+old_blocks; blk++) { | |
1e1da29f | 706 | ext2fs_unmark_block_bitmap(fs->block_map, |
24b2c7a7 | 707 | blk); |
052db4b7 TT |
708 | rfs->needed_blocks--; |
709 | } | |
1e1da29f | 710 | group_blk += fs->super->s_blocks_per_group; |
24b2c7a7 | 711 | } |
c762c8e6 TT |
712 | retval = 0; |
713 | goto errout; | |
24b2c7a7 TT |
714 | } |
715 | /* | |
716 | * If we're increasing the number of descriptor blocks, life | |
1e1da29f | 717 | * gets interesting.... |
24b2c7a7 | 718 | */ |
f2de1d38 | 719 | meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super); |
1333fe93 | 720 | for (i = 0; i < max_groups; i++) { |
76dd5e5c TT |
721 | has_super = ext2fs_bg_has_super(fs, i); |
722 | if (has_super) | |
723 | mark_fs_metablock(rfs, meta_bmap, i, group_blk); | |
724 | ||
725 | meta_bg = i / meta_bg_size; | |
726 | if (!(fs->super->s_feature_incompat & | |
727 | EXT2_FEATURE_INCOMPAT_META_BG) || | |
728 | (meta_bg < fs->super->s_first_meta_bg)) { | |
424cb7b6 TT |
729 | if (has_super) { |
730 | for (blk = group_blk+1; | |
731 | blk < group_blk + 1 + new_blocks; blk++) | |
732 | mark_fs_metablock(rfs, meta_bmap, | |
733 | i, blk); | |
734 | } | |
76dd5e5c TT |
735 | } else { |
736 | if (has_super) | |
737 | has_super = 1; | |
738 | if (((i % meta_bg_size) == 0) || | |
739 | ((i % meta_bg_size) == 1) || | |
740 | ((i % meta_bg_size) == (meta_bg_size-1))) | |
741 | mark_fs_metablock(rfs, meta_bmap, i, | |
742 | group_blk + has_super); | |
24b2c7a7 | 743 | } |
76dd5e5c | 744 | |
1e1da29f TT |
745 | if (fs->group_desc[i].bg_inode_table && |
746 | fs->group_desc[i].bg_inode_bitmap && | |
747 | fs->group_desc[i].bg_block_bitmap) | |
748 | goto next_group; | |
24b2c7a7 | 749 | |
1e1da29f | 750 | /* |
c762c8e6 TT |
751 | * Reserve the existing meta blocks that we know |
752 | * aren't to be moved. | |
1e1da29f TT |
753 | */ |
754 | if (fs->group_desc[i].bg_block_bitmap) | |
755 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, | |
756 | fs->group_desc[i].bg_block_bitmap); | |
757 | if (fs->group_desc[i].bg_inode_bitmap) | |
758 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, | |
759 | fs->group_desc[i].bg_inode_bitmap); | |
760 | if (fs->group_desc[i].bg_inode_table) | |
761 | for (blk = fs->group_desc[i].bg_inode_table, j=0; | |
762 | j < fs->inode_blocks_per_group ; j++, blk++) | |
763 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, | |
764 | blk); | |
24b2c7a7 | 765 | |
c762c8e6 TT |
766 | /* |
767 | * Allocate the missing data structures | |
768 | */ | |
1e1da29f TT |
769 | retval = ext2fs_allocate_group_table(fs, i, |
770 | rfs->reserve_blocks); | |
771 | if (retval) | |
c762c8e6 | 772 | goto errout; |
24b2c7a7 | 773 | |
1e1da29f | 774 | /* |
c762c8e6 TT |
775 | * For those structures that have changed, we need to |
776 | * do bookkeepping. | |
1e1da29f | 777 | */ |
c762c8e6 TT |
778 | if (FS_BLOCK_BM(old_fs, i) != |
779 | (blk = FS_BLOCK_BM(fs, i))) { | |
780 | ext2fs_mark_block_bitmap(fs->block_map, blk); | |
781 | if (ext2fs_test_block_bitmap(old_fs->block_map, blk) && | |
782 | !ext2fs_test_block_bitmap(meta_bmap, blk)) | |
783 | ext2fs_mark_block_bitmap(rfs->move_blocks, | |
784 | blk); | |
785 | } | |
786 | if (FS_INODE_BM(old_fs, i) != | |
787 | (blk = FS_INODE_BM(fs, i))) { | |
788 | ext2fs_mark_block_bitmap(fs->block_map, blk); | |
789 | if (ext2fs_test_block_bitmap(old_fs->block_map, blk) && | |
790 | !ext2fs_test_block_bitmap(meta_bmap, blk)) | |
791 | ext2fs_mark_block_bitmap(rfs->move_blocks, | |
792 | blk); | |
793 | } | |
24b2c7a7 | 794 | |
052db4b7 TT |
795 | /* |
796 | * The inode table, if we need to relocate it, is | |
797 | * handled specially. We have to reserve the blocks | |
798 | * for both the old and the new inode table, since we | |
799 | * can't have the inode table be destroyed during the | |
800 | * block relocation phase. | |
801 | */ | |
c762c8e6 | 802 | if (FS_INODE_TB(fs, i) == FS_INODE_TB(old_fs, i)) |
052db4b7 TT |
803 | goto next_group; /* inode table not moved */ |
804 | ||
c762c8e6 | 805 | rfs->needed_blocks += fs->inode_blocks_per_group; |
052db4b7 TT |
806 | |
807 | /* | |
808 | * Mark the new inode table as in use in the new block | |
c762c8e6 TT |
809 | * allocation bitmap, and move any blocks that might |
810 | * be necessary. | |
052db4b7 | 811 | */ |
1e1da29f | 812 | for (blk = fs->group_desc[i].bg_inode_table, j=0; |
c762c8e6 | 813 | j < fs->inode_blocks_per_group ; j++, blk++) { |
1e1da29f | 814 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
c762c8e6 TT |
815 | if (ext2fs_test_block_bitmap(old_fs->block_map, blk) && |
816 | !ext2fs_test_block_bitmap(meta_bmap, blk)) | |
817 | ext2fs_mark_block_bitmap(rfs->move_blocks, | |
818 | blk); | |
819 | } | |
820 | ||
1e1da29f | 821 | /* |
052db4b7 TT |
822 | * Make sure the old inode table is reserved in the |
823 | * block reservation bitmap. | |
1e1da29f | 824 | */ |
052db4b7 TT |
825 | for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0; |
826 | j < fs->inode_blocks_per_group ; j++, blk++) | |
827 | ext2fs_mark_block_bitmap(rfs->reserve_blocks, blk); | |
1e1da29f TT |
828 | |
829 | next_group: | |
830 | group_blk += rfs->new_fs->super->s_blocks_per_group; | |
831 | } | |
c762c8e6 TT |
832 | retval = 0; |
833 | ||
834 | errout: | |
835 | if (meta_bmap) | |
836 | ext2fs_free_block_bitmap(meta_bmap); | |
837 | ||
838 | return retval; | |
1e1da29f | 839 | } |
24b2c7a7 | 840 | |
a8519a2d TT |
841 | /* |
842 | * This helper function tries to allocate a new block. We try to | |
843 | * avoid hitting the original group descriptor blocks at least at | |
844 | * first, since we want to make it possible to recover from a badly | |
845 | * aborted resize operation as much as possible. | |
846 | * | |
847 | * In the future, I may further modify this routine to balance out | |
848 | * where we get the new blocks across the various block groups. | |
849 | * Ideally we would allocate blocks that corresponded with the block | |
850 | * group of the containing inode, and keep contiguous blocks | |
851 | * together. However, this very difficult to do efficiently, since we | |
852 | * don't have the necessary information up front. | |
853 | */ | |
854 | ||
855 | #define AVOID_OLD 1 | |
856 | #define DESPERATION 2 | |
857 | ||
858 | static void init_block_alloc(ext2_resize_t rfs) | |
859 | { | |
860 | rfs->alloc_state = AVOID_OLD; | |
861 | rfs->new_blk = rfs->new_fs->super->s_first_data_block; | |
2bc4d4f7 TT |
862 | #if 0 |
863 | /* HACK for testing */ | |
864 | if (rfs->new_fs->super->s_blocks_count > | |
865 | rfs->old_fs->super->s_blocks_count) | |
866 | rfs->new_blk = rfs->old_fs->super->s_blocks_count; | |
867 | #endif | |
a8519a2d TT |
868 | } |
869 | ||
870 | static blk_t get_new_block(ext2_resize_t rfs) | |
871 | { | |
872 | ext2_filsys fs = rfs->new_fs; | |
873 | ||
874 | while (1) { | |
875 | if (rfs->new_blk >= fs->super->s_blocks_count) { | |
876 | if (rfs->alloc_state == DESPERATION) | |
877 | return 0; | |
878 | ||
879 | #ifdef RESIZE2FS_DEBUG | |
880 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
f35fd3d5 TT |
881 | printf("Going into desperation mode " |
882 | "for block allocations\n"); | |
a8519a2d TT |
883 | #endif |
884 | rfs->alloc_state = DESPERATION; | |
885 | rfs->new_blk = fs->super->s_first_data_block; | |
886 | continue; | |
887 | } | |
888 | if (ext2fs_test_block_bitmap(fs->block_map, rfs->new_blk) || | |
889 | ext2fs_test_block_bitmap(rfs->reserve_blocks, | |
890 | rfs->new_blk) || | |
891 | ((rfs->alloc_state == AVOID_OLD) && | |
bce49798 | 892 | (rfs->new_blk < rfs->old_fs->super->s_blocks_count) && |
a8519a2d TT |
893 | ext2fs_test_block_bitmap(rfs->old_fs->block_map, |
894 | rfs->new_blk))) { | |
895 | rfs->new_blk++; | |
896 | continue; | |
897 | } | |
898 | return rfs->new_blk; | |
899 | } | |
900 | } | |
901 | ||
902 | static errcode_t block_mover(ext2_resize_t rfs) | |
903 | { | |
904 | blk_t blk, old_blk, new_blk; | |
905 | ext2_filsys fs = rfs->new_fs; | |
906 | ext2_filsys old_fs = rfs->old_fs; | |
907 | errcode_t retval; | |
908 | int size, c; | |
909 | int to_move, moved; | |
7d7bdd57 TT |
910 | ext2_badblocks_list badblock_list = 0; |
911 | int bb_modified = 0; | |
912 | ||
913 | retval = ext2fs_read_bb_inode(old_fs, &badblock_list); | |
914 | if (retval) | |
915 | return retval; | |
a8519a2d TT |
916 | |
917 | new_blk = fs->super->s_first_data_block; | |
918 | if (!rfs->itable_buf) { | |
e5aace90 | 919 | retval = ext2fs_get_array(fs->blocksize, |
a8519a2d | 920 | fs->inode_blocks_per_group, |
c4e3d3f3 | 921 | &rfs->itable_buf); |
a8519a2d TT |
922 | if (retval) |
923 | return retval; | |
924 | } | |
925 | retval = ext2fs_create_extent_table(&rfs->bmap, 0); | |
926 | if (retval) | |
927 | return retval; | |
928 | ||
929 | /* | |
930 | * The first step is to figure out where all of the blocks | |
931 | * will go. | |
932 | */ | |
933 | to_move = moved = 0; | |
934 | init_block_alloc(rfs); | |
935 | for (blk = old_fs->super->s_first_data_block; | |
936 | blk < old_fs->super->s_blocks_count; blk++) { | |
937 | if (!ext2fs_test_block_bitmap(old_fs->block_map, blk)) | |
938 | continue; | |
939 | if (!ext2fs_test_block_bitmap(rfs->move_blocks, blk)) | |
940 | continue; | |
7d7bdd57 TT |
941 | if (ext2fs_badblocks_list_test(badblock_list, blk)) { |
942 | ext2fs_badblocks_list_del(badblock_list, blk); | |
943 | bb_modified++; | |
944 | continue; | |
945 | } | |
a8519a2d TT |
946 | |
947 | new_blk = get_new_block(rfs); | |
948 | if (!new_blk) { | |
949 | retval = ENOSPC; | |
950 | goto errout; | |
951 | } | |
952 | ext2fs_mark_block_bitmap(fs->block_map, new_blk); | |
953 | ext2fs_add_extent_entry(rfs->bmap, blk, new_blk); | |
954 | to_move++; | |
955 | } | |
956 | ||
957 | if (to_move == 0) { | |
cefbf487 TT |
958 | if (rfs->bmap) { |
959 | ext2fs_free_extent_table(rfs->bmap); | |
960 | rfs->bmap = 0; | |
961 | } | |
a8519a2d TT |
962 | retval = 0; |
963 | goto errout; | |
964 | } | |
965 | ||
966 | /* | |
967 | * Step two is to actually move the blocks | |
968 | */ | |
969 | retval = ext2fs_iterate_extent(rfs->bmap, 0, 0, 0); | |
970 | if (retval) goto errout; | |
971 | ||
3b627e8d TT |
972 | if (rfs->progress) { |
973 | retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS, | |
974 | 0, to_move); | |
975 | if (retval) | |
976 | goto errout; | |
977 | } | |
a8519a2d TT |
978 | while (1) { |
979 | retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size); | |
980 | if (retval) goto errout; | |
981 | if (!size) | |
982 | break; | |
983 | #ifdef RESIZE2FS_DEBUG | |
984 | if (rfs->flags & RESIZE_DEBUG_BMOVE) | |
f35fd3d5 TT |
985 | printf("Moving %d blocks %u->%u\n", |
986 | size, old_blk, new_blk); | |
a8519a2d TT |
987 | #endif |
988 | do { | |
989 | c = size; | |
990 | if (c > fs->inode_blocks_per_group) | |
991 | c = fs->inode_blocks_per_group; | |
992 | retval = io_channel_read_blk(fs->io, old_blk, c, | |
993 | rfs->itable_buf); | |
994 | if (retval) goto errout; | |
995 | retval = io_channel_write_blk(fs->io, new_blk, c, | |
996 | rfs->itable_buf); | |
997 | if (retval) goto errout; | |
998 | size -= c; | |
999 | new_blk += c; | |
1000 | old_blk += c; | |
1001 | moved += c; | |
1002 | if (rfs->progress) { | |
1003 | io_channel_flush(fs->io); | |
3b627e8d TT |
1004 | retval = (rfs->progress)(rfs, |
1005 | E2_RSZ_BLOCK_RELOC_PASS, | |
a8519a2d | 1006 | moved, to_move); |
3b627e8d TT |
1007 | if (retval) |
1008 | goto errout; | |
a8519a2d TT |
1009 | } |
1010 | } while (size > 0); | |
1011 | io_channel_flush(fs->io); | |
1012 | } | |
1013 | ||
1014 | errout: | |
7d7bdd57 TT |
1015 | if (badblock_list) { |
1016 | if (!retval && bb_modified) | |
1017 | retval = ext2fs_update_bb_inode(old_fs, | |
1018 | badblock_list); | |
1019 | ext2fs_badblocks_list_free(badblock_list); | |
1020 | } | |
a8519a2d TT |
1021 | return retval; |
1022 | } | |
1023 | ||
1024 | ||
1025 | /* -------------------------------------------------------------------- | |
1026 | * | |
1027 | * Resize processing, phase 3 | |
1028 | * | |
1029 | * -------------------------------------------------------------------- | |
1030 | */ | |
1031 | ||
1032 | ||
1033 | struct process_block_struct { | |
1034 | ext2_resize_t rfs; | |
dfcdc32f | 1035 | ext2_ino_t ino; |
a8519a2d TT |
1036 | struct ext2_inode * inode; |
1037 | errcode_t error; | |
1038 | int is_dir; | |
1039 | int changed; | |
1040 | }; | |
1041 | ||
1042 | static int process_block(ext2_filsys fs, blk_t *block_nr, | |
54434927 TT |
1043 | e2_blkcnt_t blockcnt, |
1044 | blk_t ref_block EXT2FS_ATTR((unused)), | |
1045 | int ref_offset EXT2FS_ATTR((unused)), void *priv_data) | |
a8519a2d TT |
1046 | { |
1047 | struct process_block_struct *pb; | |
1048 | errcode_t retval; | |
1049 | blk_t block, new_block; | |
1050 | int ret = 0; | |
1051 | ||
1052 | pb = (struct process_block_struct *) priv_data; | |
1053 | block = *block_nr; | |
1054 | if (pb->rfs->bmap) { | |
1055 | new_block = ext2fs_extent_translate(pb->rfs->bmap, block); | |
1056 | if (new_block) { | |
1057 | *block_nr = new_block; | |
1058 | ret |= BLOCK_CHANGED; | |
1059 | pb->changed = 1; | |
1060 | #ifdef RESIZE2FS_DEBUG | |
1061 | if (pb->rfs->flags & RESIZE_DEBUG_BMOVE) | |
f35fd3d5 | 1062 | printf("ino=%u, blockcnt=%lld, %u->%u\n", |
a8519a2d TT |
1063 | pb->ino, blockcnt, block, new_block); |
1064 | #endif | |
1065 | block = new_block; | |
1066 | } | |
1067 | } | |
1068 | if (pb->is_dir) { | |
1069 | retval = ext2fs_add_dir_block(fs->dblist, pb->ino, | |
101c84f2 | 1070 | block, (int) blockcnt); |
a8519a2d TT |
1071 | if (retval) { |
1072 | pb->error = retval; | |
1073 | ret |= BLOCK_ABORT; | |
1074 | } | |
1075 | } | |
1076 | return ret; | |
1077 | } | |
1078 | ||
1079 | /* | |
1080 | * Progress callback | |
1081 | */ | |
54434927 TT |
1082 | static errcode_t progress_callback(ext2_filsys fs, |
1083 | ext2_inode_scan scan EXT2FS_ATTR((unused)), | |
a8519a2d TT |
1084 | dgrp_t group, void * priv_data) |
1085 | { | |
1086 | ext2_resize_t rfs = (ext2_resize_t) priv_data; | |
3b627e8d | 1087 | errcode_t retval; |
a8519a2d TT |
1088 | |
1089 | /* | |
f4b2a6db TT |
1090 | * This check is to protect against old ext2 libraries. It |
1091 | * shouldn't be needed against new libraries. | |
a8519a2d | 1092 | */ |
f4b2a6db | 1093 | if ((group+1) == 0) |
a8519a2d TT |
1094 | return 0; |
1095 | ||
1096 | if (rfs->progress) { | |
1097 | io_channel_flush(fs->io); | |
3b627e8d TT |
1098 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS, |
1099 | group+1, fs->group_desc_count); | |
1100 | if (retval) | |
1101 | return retval; | |
a8519a2d TT |
1102 | } |
1103 | ||
1104 | return 0; | |
1105 | } | |
1106 | ||
1107 | static errcode_t inode_scan_and_fix(ext2_resize_t rfs) | |
1108 | { | |
1109 | struct process_block_struct pb; | |
dfcdc32f | 1110 | ext2_ino_t ino, new_inode; |
edfd9b0a | 1111 | struct ext2_inode *inode = NULL; |
a8519a2d TT |
1112 | ext2_inode_scan scan = NULL; |
1113 | errcode_t retval; | |
1114 | int group; | |
1115 | char *block_buf = 0; | |
dfcdc32f | 1116 | ext2_ino_t start_to_move; |
0ccd488a | 1117 | blk_t orig_size, new_block; |
4ef28824 | 1118 | int inode_size; |
a8519a2d TT |
1119 | |
1120 | if ((rfs->old_fs->group_desc_count <= | |
1121 | rfs->new_fs->group_desc_count) && | |
1122 | !rfs->bmap) | |
1123 | return 0; | |
1124 | ||
2bc4d4f7 TT |
1125 | /* |
1126 | * Save the original size of the old filesystem, and | |
1127 | * temporarily set the size to be the new size if the new size | |
1128 | * is larger. We need to do this to avoid catching an error | |
1129 | * by the block iterator routines | |
1130 | */ | |
1131 | orig_size = rfs->old_fs->super->s_blocks_count; | |
1132 | if (orig_size < rfs->new_fs->super->s_blocks_count) | |
1133 | rfs->old_fs->super->s_blocks_count = | |
1134 | rfs->new_fs->super->s_blocks_count; | |
1135 | ||
a8519a2d TT |
1136 | retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan); |
1137 | if (retval) goto errout; | |
1138 | ||
1139 | retval = ext2fs_init_dblist(rfs->old_fs, 0); | |
1140 | if (retval) goto errout; | |
e5aace90 | 1141 | retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf); |
a8519a2d TT |
1142 | if (retval) goto errout; |
1143 | ||
1144 | start_to_move = (rfs->new_fs->group_desc_count * | |
1145 | rfs->new_fs->super->s_inodes_per_group); | |
1146 | ||
3b627e8d TT |
1147 | if (rfs->progress) { |
1148 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS, | |
1149 | 0, rfs->old_fs->group_desc_count); | |
1150 | if (retval) | |
1151 | goto errout; | |
1152 | } | |
a8519a2d TT |
1153 | ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs); |
1154 | pb.rfs = rfs; | |
edfd9b0a | 1155 | pb.inode = inode; |
a8519a2d TT |
1156 | pb.error = 0; |
1157 | new_inode = EXT2_FIRST_INODE(rfs->new_fs->super); | |
4ef28824 | 1158 | inode_size = EXT2_INODE_SIZE(rfs->new_fs->super); |
edfd9b0a TT |
1159 | inode = malloc(inode_size); |
1160 | if (!inode) { | |
4ef28824 ES |
1161 | retval = ENOMEM; |
1162 | goto errout; | |
1163 | } | |
a8519a2d TT |
1164 | /* |
1165 | * First, copy all of the inodes that need to be moved | |
1166 | * elsewhere in the inode table | |
1167 | */ | |
1168 | while (1) { | |
edfd9b0a | 1169 | retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size); |
a8519a2d TT |
1170 | if (retval) goto errout; |
1171 | if (!ino) | |
1172 | break; | |
1173 | ||
edfd9b0a | 1174 | if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO) |
a8519a2d TT |
1175 | continue; /* inode not in use */ |
1176 | ||
edfd9b0a | 1177 | pb.is_dir = LINUX_S_ISDIR(inode->i_mode); |
a8519a2d TT |
1178 | pb.changed = 0; |
1179 | ||
edfd9b0a | 1180 | if (inode->i_file_acl && rfs->bmap) { |
0ccd488a | 1181 | new_block = ext2fs_extent_translate(rfs->bmap, |
edfd9b0a | 1182 | inode->i_file_acl); |
ed909bbe | 1183 | if (new_block) { |
edfd9b0a TT |
1184 | inode->i_file_acl = new_block; |
1185 | retval = ext2fs_write_inode_full(rfs->old_fs, | |
1186 | ino, inode, inode_size); | |
ed909bbe TT |
1187 | if (retval) goto errout; |
1188 | } | |
1189 | } | |
1190 | ||
edfd9b0a | 1191 | if (ext2fs_inode_has_valid_blocks(inode) && |
a8519a2d TT |
1192 | (rfs->bmap || pb.is_dir)) { |
1193 | pb.ino = ino; | |
1194 | retval = ext2fs_block_iterate2(rfs->old_fs, | |
1195 | ino, 0, block_buf, | |
1196 | process_block, &pb); | |
1197 | if (retval) | |
1198 | goto errout; | |
1199 | if (pb.error) { | |
1200 | retval = pb.error; | |
1201 | goto errout; | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | if (ino <= start_to_move) | |
1206 | continue; /* Don't need to move it. */ | |
1207 | ||
1208 | /* | |
1209 | * Find a new inode | |
1210 | */ | |
1211 | while (1) { | |
1212 | if (!ext2fs_test_inode_bitmap(rfs->new_fs->inode_map, | |
1213 | new_inode)) | |
1214 | break; | |
1215 | new_inode++; | |
1216 | if (new_inode > rfs->new_fs->super->s_inodes_count) { | |
1217 | retval = ENOSPC; | |
1218 | goto errout; | |
1219 | } | |
1220 | } | |
1221 | ext2fs_mark_inode_bitmap(rfs->new_fs->inode_map, new_inode); | |
1222 | if (pb.changed) { | |
1223 | /* Get the new version of the inode */ | |
4ef28824 | 1224 | retval = ext2fs_read_inode_full(rfs->old_fs, ino, |
edfd9b0a | 1225 | inode, inode_size); |
a8519a2d TT |
1226 | if (retval) goto errout; |
1227 | } | |
edfd9b0a | 1228 | inode->i_ctime = time(0); |
4ef28824 | 1229 | retval = ext2fs_write_inode_full(rfs->old_fs, new_inode, |
edfd9b0a | 1230 | inode, inode_size); |
a8519a2d TT |
1231 | if (retval) goto errout; |
1232 | ||
1233 | group = (new_inode-1) / EXT2_INODES_PER_GROUP(rfs->new_fs->super); | |
edfd9b0a | 1234 | if (LINUX_S_ISDIR(inode->i_mode)) |
a8519a2d TT |
1235 | rfs->new_fs->group_desc[group].bg_used_dirs_count++; |
1236 | ||
1237 | #ifdef RESIZE2FS_DEBUG | |
1238 | if (rfs->flags & RESIZE_DEBUG_INODEMAP) | |
f35fd3d5 | 1239 | printf("Inode moved %u->%u\n", ino, new_inode); |
a8519a2d TT |
1240 | #endif |
1241 | if (!rfs->imap) { | |
1242 | retval = ext2fs_create_extent_table(&rfs->imap, 0); | |
1243 | if (retval) | |
1244 | goto errout; | |
1245 | } | |
1246 | ext2fs_add_extent_entry(rfs->imap, ino, new_inode); | |
1247 | } | |
1248 | io_channel_flush(rfs->old_fs->io); | |
1249 | ||
1250 | errout: | |
2bc4d4f7 | 1251 | rfs->old_fs->super->s_blocks_count = orig_size; |
a8519a2d TT |
1252 | if (rfs->bmap) { |
1253 | ext2fs_free_extent_table(rfs->bmap); | |
1254 | rfs->bmap = 0; | |
1255 | } | |
1256 | if (scan) | |
1257 | ext2fs_close_inode_scan(scan); | |
1258 | if (block_buf) | |
c4e3d3f3 | 1259 | ext2fs_free_mem(&block_buf); |
edfd9b0a TT |
1260 | if (inode) |
1261 | free(inode); | |
a8519a2d TT |
1262 | return retval; |
1263 | } | |
1264 | ||
1265 | /* -------------------------------------------------------------------- | |
1266 | * | |
1267 | * Resize processing, phase 4. | |
1268 | * | |
1269 | * -------------------------------------------------------------------- | |
1270 | */ | |
1271 | ||
1272 | struct istruct { | |
1273 | ext2_resize_t rfs; | |
3b627e8d | 1274 | errcode_t err; |
1333fe93 | 1275 | unsigned long max_dirs; |
a8519a2d TT |
1276 | int num; |
1277 | }; | |
1278 | ||
54434927 TT |
1279 | static int check_and_change_inodes(ext2_ino_t dir, |
1280 | int entry EXT2FS_ATTR((unused)), | |
a8519a2d | 1281 | struct ext2_dir_entry *dirent, int offset, |
54434927 TT |
1282 | int blocksize EXT2FS_ATTR((unused)), |
1283 | char *buf EXT2FS_ATTR((unused)), | |
1284 | void *priv_data) | |
a8519a2d TT |
1285 | { |
1286 | struct istruct *is = (struct istruct *) priv_data; | |
085d2a83 TT |
1287 | struct ext2_inode inode; |
1288 | ext2_ino_t new_inode; | |
1289 | errcode_t retval; | |
a8519a2d TT |
1290 | |
1291 | if (is->rfs->progress && offset == 0) { | |
1292 | io_channel_flush(is->rfs->old_fs->io); | |
3b627e8d TT |
1293 | is->err = (is->rfs->progress)(is->rfs, |
1294 | E2_RSZ_INODE_REF_UPD_PASS, | |
1333fe93 | 1295 | ++is->num, is->max_dirs); |
3b627e8d TT |
1296 | if (is->err) |
1297 | return DIRENT_ABORT; | |
a8519a2d TT |
1298 | } |
1299 | ||
1300 | if (!dirent->inode) | |
1301 | return 0; | |
1302 | ||
1303 | new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode); | |
1304 | ||
1305 | if (!new_inode) | |
1306 | return 0; | |
1307 | #ifdef RESIZE2FS_DEBUG | |
1308 | if (is->rfs->flags & RESIZE_DEBUG_INODEMAP) | |
f35fd3d5 | 1309 | printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n", |
06191693 | 1310 | dir, dirent->name_len&0xFF, dirent->name, |
a8519a2d TT |
1311 | dirent->inode, new_inode); |
1312 | #endif | |
1313 | ||
1314 | dirent->inode = new_inode; | |
1315 | ||
085d2a83 TT |
1316 | /* Update the directory mtime and ctime */ |
1317 | retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode); | |
1318 | if (retval == 0) { | |
1319 | inode.i_mtime = inode.i_ctime = time(0); | |
d2b2a488 BB |
1320 | is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode); |
1321 | if (is->err) | |
1322 | return DIRENT_ABORT; | |
085d2a83 TT |
1323 | } |
1324 | ||
a8519a2d TT |
1325 | return DIRENT_CHANGED; |
1326 | } | |
1327 | ||
1328 | static errcode_t inode_ref_fix(ext2_resize_t rfs) | |
1329 | { | |
1330 | errcode_t retval; | |
1331 | struct istruct is; | |
1332 | ||
1333 | if (!rfs->imap) | |
1334 | return 0; | |
1335 | ||
1336 | /* | |
1337 | * Now, we iterate over all of the directories to update the | |
1338 | * inode references | |
1339 | */ | |
1340 | is.num = 0; | |
1333fe93 | 1341 | is.max_dirs = ext2fs_dblist_count(rfs->old_fs->dblist); |
a8519a2d | 1342 | is.rfs = rfs; |
3b627e8d | 1343 | is.err = 0; |
a8519a2d | 1344 | |
3b627e8d TT |
1345 | if (rfs->progress) { |
1346 | retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS, | |
1333fe93 | 1347 | 0, is.max_dirs); |
3b627e8d TT |
1348 | if (retval) |
1349 | goto errout; | |
1350 | } | |
a8519a2d TT |
1351 | |
1352 | retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist, | |
1353 | DIRENT_FLAG_INCLUDE_EMPTY, 0, | |
1354 | check_and_change_inodes, &is); | |
3b627e8d TT |
1355 | if (retval) |
1356 | goto errout; | |
1357 | if (is.err) { | |
1358 | retval = is.err; | |
1359 | goto errout; | |
1360 | } | |
a8519a2d | 1361 | |
3b627e8d | 1362 | errout: |
a8519a2d TT |
1363 | ext2fs_free_extent_table(rfs->imap); |
1364 | rfs->imap = 0; | |
1365 | return retval; | |
1366 | } | |
1367 | ||
1368 | ||
1369 | /* -------------------------------------------------------------------- | |
1370 | * | |
1371 | * Resize processing, phase 5. | |
1372 | * | |
1373 | * In this phase we actually move the inode table around, and then | |
1374 | * update the summary statistics. This is scary, since aborting here | |
1375 | * will potentially scramble the filesystem. (We are moving the | |
1376 | * inode tables around in place, and so the potential for lost data, | |
1377 | * or at the very least scrambling the mapping between filenames and | |
1378 | * inode numbers is very high in case of a power failure here.) | |
1379 | * -------------------------------------------------------------------- | |
1380 | */ | |
1381 | ||
24b2c7a7 | 1382 | |
052db4b7 TT |
1383 | /* |
1384 | * A very scary routine --- this one moves the inode table around!!! | |
1385 | * | |
1386 | * After this you have to use the rfs->new_fs file handle to read and | |
1387 | * write inodes. | |
1388 | */ | |
c762c8e6 | 1389 | static errcode_t move_itables(ext2_resize_t rfs) |
052db4b7 | 1390 | { |
54434927 TT |
1391 | int n, num, size, diff; |
1392 | dgrp_t i, max_groups; | |
052db4b7 | 1393 | ext2_filsys fs = rfs->new_fs; |
05e112a1 | 1394 | char *cp; |
64ad98ac | 1395 | blk_t old_blk, new_blk, blk; |
a8519a2d | 1396 | errcode_t retval; |
64ad98ac | 1397 | int j, to_move, moved; |
052db4b7 | 1398 | |
1333fe93 TT |
1399 | max_groups = fs->group_desc_count; |
1400 | if (max_groups > rfs->old_fs->group_desc_count) | |
1401 | max_groups = rfs->old_fs->group_desc_count; | |
052db4b7 | 1402 | |
05e112a1 TT |
1403 | size = fs->blocksize * fs->inode_blocks_per_group; |
1404 | if (!rfs->itable_buf) { | |
c4e3d3f3 | 1405 | retval = ext2fs_get_mem(size, &rfs->itable_buf); |
ca8abba7 TT |
1406 | if (retval) |
1407 | return retval; | |
05e112a1 | 1408 | } |
c762c8e6 TT |
1409 | |
1410 | /* | |
1411 | * Figure out how many inode tables we need to move | |
1412 | */ | |
1413 | to_move = moved = 0; | |
1333fe93 | 1414 | for (i=0; i < max_groups; i++) |
c762c8e6 TT |
1415 | if (rfs->old_fs->group_desc[i].bg_inode_table != |
1416 | fs->group_desc[i].bg_inode_table) | |
1417 | to_move++; | |
1418 | ||
1419 | if (to_move == 0) | |
1420 | return 0; | |
1421 | ||
3b627e8d TT |
1422 | if (rfs->progress) { |
1423 | retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, | |
1424 | 0, to_move); | |
1425 | if (retval) | |
1426 | goto errout; | |
1427 | } | |
63b44fbe | 1428 | |
a8519a2d TT |
1429 | rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
1430 | ||
1333fe93 | 1431 | for (i=0; i < max_groups; i++) { |
ca8abba7 TT |
1432 | old_blk = rfs->old_fs->group_desc[i].bg_inode_table; |
1433 | new_blk = fs->group_desc[i].bg_inode_table; | |
1434 | diff = new_blk - old_blk; | |
052db4b7 | 1435 | |
80c0fc34 | 1436 | #ifdef RESIZE2FS_DEBUG |
05e112a1 | 1437 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
f35fd3d5 | 1438 | printf("Itable move group %d block %u->%u (diff %d)\n", |
ca8abba7 | 1439 | i, old_blk, new_blk, diff); |
80c0fc34 | 1440 | #endif |
052db4b7 | 1441 | |
05e112a1 | 1442 | if (!diff) |
052db4b7 TT |
1443 | continue; |
1444 | ||
ca8abba7 | 1445 | retval = io_channel_read_blk(fs->io, old_blk, |
05e112a1 TT |
1446 | fs->inode_blocks_per_group, |
1447 | rfs->itable_buf); | |
052db4b7 | 1448 | if (retval) |
a8519a2d | 1449 | goto errout; |
05e112a1 TT |
1450 | /* |
1451 | * The end of the inode table segment often contains | |
a8519a2d TT |
1452 | * all zeros, and we're often only moving the inode |
1453 | * table down a block or two. If so, we can optimize | |
1454 | * things by not rewriting blocks that we know to be zero | |
1455 | * already. | |
05e112a1 | 1456 | */ |
2787276e | 1457 | for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--) |
05e112a1 TT |
1458 | if (*cp) |
1459 | break; | |
1460 | n = n >> EXT2_BLOCK_SIZE_BITS(fs->super); | |
80c0fc34 | 1461 | #ifdef RESIZE2FS_DEBUG |
05e112a1 | 1462 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
f35fd3d5 | 1463 | printf("%d blocks of zeros...\n", n); |
80c0fc34 | 1464 | #endif |
05e112a1 TT |
1465 | num = fs->inode_blocks_per_group; |
1466 | if (n > diff) | |
1467 | num -= n; | |
1468 | ||
ca8abba7 | 1469 | retval = io_channel_write_blk(fs->io, new_blk, |
05e112a1 | 1470 | num, rfs->itable_buf); |
052db4b7 | 1471 | if (retval) { |
ca8abba7 | 1472 | io_channel_write_blk(fs->io, old_blk, |
05e112a1 | 1473 | num, rfs->itable_buf); |
a8519a2d | 1474 | goto errout; |
052db4b7 | 1475 | } |
05e112a1 TT |
1476 | if (n > diff) { |
1477 | retval = io_channel_write_blk(fs->io, | |
ca8abba7 | 1478 | old_blk + fs->inode_blocks_per_group, |
a8519a2d TT |
1479 | diff, (rfs->itable_buf + |
1480 | (fs->inode_blocks_per_group - diff) * | |
1481 | fs->blocksize)); | |
05e112a1 | 1482 | if (retval) |
a8519a2d | 1483 | goto errout; |
c762c8e6 | 1484 | } |
64ad98ac TT |
1485 | |
1486 | for (blk = rfs->old_fs->group_desc[i].bg_inode_table, j=0; | |
1487 | j < fs->inode_blocks_per_group ; j++, blk++) | |
1488 | ext2fs_unmark_block_bitmap(fs->block_map, blk); | |
1489 | ||
a8519a2d TT |
1490 | rfs->old_fs->group_desc[i].bg_inode_table = new_blk; |
1491 | ext2fs_mark_super_dirty(rfs->old_fs); | |
64ad98ac TT |
1492 | ext2fs_flush(rfs->old_fs); |
1493 | ||
a8519a2d | 1494 | if (rfs->progress) { |
3b627e8d TT |
1495 | retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS, |
1496 | ++moved, to_move); | |
1497 | if (retval) | |
1498 | goto errout; | |
a8519a2d | 1499 | } |
052db4b7 | 1500 | } |
64ad98ac | 1501 | mark_table_blocks(fs, fs->block_map); |
a8519a2d | 1502 | ext2fs_flush(fs); |
80c0fc34 | 1503 | #ifdef RESIZE2FS_DEBUG |
05e112a1 | 1504 | if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE) |
f35fd3d5 | 1505 | printf("Inode table move finished.\n"); |
80c0fc34 | 1506 | #endif |
052db4b7 TT |
1507 | return 0; |
1508 | ||
a8519a2d | 1509 | errout: |
052db4b7 TT |
1510 | return retval; |
1511 | } | |
1512 | ||
9213a93b TT |
1513 | /* |
1514 | * Fix the resize inode | |
1515 | */ | |
1516 | static errcode_t fix_resize_inode(ext2_filsys fs) | |
1517 | { | |
1518 | struct ext2_inode inode; | |
1519 | errcode_t retval; | |
1520 | char * block_buf; | |
1521 | ||
1522 | if (!(fs->super->s_feature_compat & | |
1523 | EXT2_FEATURE_COMPAT_RESIZE_INODE)) | |
1524 | return 0; | |
1525 | ||
1526 | retval = ext2fs_get_mem(fs->blocksize, &block_buf); | |
1527 | if (retval) goto errout; | |
1528 | ||
1529 | retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode); | |
1530 | if (retval) goto errout; | |
1531 | ||
1532 | inode.i_blocks = fs->blocksize/512; | |
1533 | ||
1534 | retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode); | |
1535 | if (retval) goto errout; | |
1536 | ||
1537 | if (!inode.i_block[EXT2_DIND_BLOCK]) { | |
1538 | /* | |
1539 | * Avoid zeroing out block #0; that's rude. This | |
1540 | * should never happen anyway since the filesystem | |
1541 | * should be fsck'ed and we assume it is consistent. | |
1542 | */ | |
1543 | fprintf(stderr, | |
f35fd3d5 | 1544 | _("Should never happen: resize inode corrupt!\n")); |
9213a93b TT |
1545 | exit(1); |
1546 | } | |
1547 | ||
1548 | memset(block_buf, 0, fs->blocksize); | |
1549 | ||
1550 | retval = io_channel_write_blk(fs->io, inode.i_block[EXT2_DIND_BLOCK], | |
1551 | 1, block_buf); | |
1552 | if (retval) goto errout; | |
1553 | ||
1554 | retval = ext2fs_create_resize_inode(fs); | |
1555 | if (retval) | |
1556 | goto errout; | |
1557 | ||
1558 | errout: | |
1559 | if (block_buf) | |
1560 | ext2fs_free_mem(&block_buf); | |
1561 | return retval; | |
1562 | } | |
1563 | ||
052db4b7 TT |
1564 | /* |
1565 | * Finally, recalculate the summary information | |
1566 | */ | |
1567 | static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs) | |
1568 | { | |
dfcdc32f TT |
1569 | blk_t blk; |
1570 | ext2_ino_t ino; | |
54434927 TT |
1571 | unsigned int group = 0; |
1572 | unsigned int count = 0; | |
dfcdc32f TT |
1573 | int total_free = 0; |
1574 | int group_free = 0; | |
052db4b7 TT |
1575 | |
1576 | /* | |
1577 | * First calculate the block statistics | |
1578 | */ | |
1579 | for (blk = fs->super->s_first_data_block; | |
1580 | blk < fs->super->s_blocks_count; blk++) { | |
1581 | if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) { | |
1582 | group_free++; | |
1583 | total_free++; | |
1584 | } | |
1585 | count++; | |
1586 | if ((count == fs->super->s_blocks_per_group) || | |
1587 | (blk == fs->super->s_blocks_count-1)) { | |
1588 | fs->group_desc[group++].bg_free_blocks_count = | |
1589 | group_free; | |
1590 | count = 0; | |
1591 | group_free = 0; | |
1592 | } | |
1593 | } | |
1594 | fs->super->s_free_blocks_count = total_free; | |
1595 | ||
1596 | /* | |
1597 | * Next, calculate the inode statistics | |
1598 | */ | |
1599 | group_free = 0; | |
1600 | total_free = 0; | |
1601 | count = 0; | |
1602 | group = 0; | |
5830d6be ES |
1603 | |
1604 | /* Protect loop from wrap-around if s_inodes_count maxed */ | |
1605 | for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) { | |
052db4b7 TT |
1606 | if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) { |
1607 | group_free++; | |
1608 | total_free++; | |
1609 | } | |
1610 | count++; | |
1611 | if ((count == fs->super->s_inodes_per_group) || | |
1612 | (ino == fs->super->s_inodes_count)) { | |
1613 | fs->group_desc[group++].bg_free_inodes_count = | |
1614 | group_free; | |
1615 | count = 0; | |
1616 | group_free = 0; | |
1617 | } | |
1618 | } | |
1619 | fs->super->s_free_inodes_count = total_free; | |
1620 | ext2fs_mark_super_dirty(fs); | |
1621 | return 0; | |
1622 | } |