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