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