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