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