Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / resize / resize2fs.c
1 /*
2  * resize2fs.c --- ext2 main routine
3  *
4  * Copyright (C) 1997, 1998 by Theodore Ts'o and
5  *      PowerQuest, Inc.
6  *
7  * Copyright (C) 1999, 2000 by Theosore Ts'o
8  *
9  * %Begin-Header%
10  * This file may be redistributed under the terms of the GNU Public
11  * License.
12  * %End-Header%
13  */
14
15 /*
16  * Resizing a filesystem consists of the following phases:
17  *
18  *      1.  Adjust superblock and write out new parts of the inode
19  *              table
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.
34  */
35
36 #include "config.h"
37 #include "resize2fs.h"
38 #include <time.h>
39
40 #ifdef __linux__                        /* Kludge for debugging */
41 #define RESIZE2FS_DEBUG
42 #endif
43
44 static void fix_uninit_block_bitmaps(ext2_filsys fs);
45 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size);
46 static errcode_t blocks_to_move(ext2_resize_t rfs);
47 static errcode_t block_mover(ext2_resize_t rfs);
48 static errcode_t inode_scan_and_fix(ext2_resize_t rfs);
49 static errcode_t inode_ref_fix(ext2_resize_t rfs);
50 static errcode_t move_itables(ext2_resize_t rfs);
51 static errcode_t fix_resize_inode(ext2_filsys fs);
52 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs);
53 static errcode_t fix_sb_journal_backup(ext2_filsys fs);
54 static errcode_t mark_table_blocks(ext2_filsys fs,
55                                    ext2fs_block_bitmap bmap);
56
57 /*
58  * Some helper CPP macros
59  */
60 #define IS_BLOCK_BM(fs, i, blk) ((blk) == ext2fs_block_bitmap_loc((fs),(i)))
61 #define IS_INODE_BM(fs, i, blk) ((blk) == ext2fs_inode_bitmap_loc((fs),(i)))
62
63 #define IS_INODE_TB(fs, i, blk) (((blk) >= ext2fs_inode_table_loc((fs), (i))) && \
64                                  ((blk) < (ext2fs_inode_table_loc((fs), (i)) + \
65                                            (fs)->inode_blocks_per_group)))
66
67 /* Some bigalloc helper macros which are more succint... */
68 #define B2C(x)  EXT2FS_B2C(fs, (x))
69 #define C2B(x)  EXT2FS_C2B(fs, (x))
70 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
71 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
72 #define LT_CLSTR(x, y) (B2C(x) <  B2C(y))
73 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
74 #define GT_CLSTR(x, y) (B2C(x) >  B2C(y))
75
76 static int lazy_itable_init;
77
78 /*
79  * This is the top-level routine which does the dirty deed....
80  */
81 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
82             errcode_t (*progress)(ext2_resize_t rfs, int pass,
83                                           unsigned long cur,
84                                           unsigned long max_val))
85 {
86         ext2_resize_t   rfs;
87         errcode_t       retval;
88         struct resource_track   rtrack, overall_track;
89
90         /*
91          * Create the data structure
92          */
93         retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
94         if (retval)
95                 return retval;
96
97         memset(rfs, 0, sizeof(struct ext2_resize_struct));
98         fs->priv_data = rfs;
99         rfs->old_fs = fs;
100         rfs->flags = flags;
101         rfs->itable_buf  = 0;
102         rfs->progress = progress;
103
104         init_resource_track(&overall_track, "overall resize2fs", fs->io);
105         init_resource_track(&rtrack, "read_bitmaps", fs->io);
106         retval = ext2fs_read_bitmaps(fs);
107         if (retval)
108                 goto errout;
109         print_resource_track(rfs, &rtrack, fs->io);
110
111         fs->super->s_state |= EXT2_ERROR_FS;
112         ext2fs_mark_super_dirty(fs);
113         ext2fs_flush(fs);
114
115         init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
116         fix_uninit_block_bitmaps(fs);
117         print_resource_track(rfs, &rtrack, fs->io);
118         retval = ext2fs_dup_handle(fs, &rfs->new_fs);
119         if (retval)
120                 goto errout;
121
122         init_resource_track(&rtrack, "adjust_superblock", fs->io);
123         retval = adjust_superblock(rfs, *new_size);
124         if (retval)
125                 goto errout;
126         print_resource_track(rfs, &rtrack, fs->io);
127
128
129         init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
130         fix_uninit_block_bitmaps(rfs->new_fs);
131         print_resource_track(rfs, &rtrack, fs->io);
132         /* Clear the block bitmap uninit flag for the last block group */
133         ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
134                              EXT2_BG_BLOCK_UNINIT);
135
136         *new_size = ext2fs_blocks_count(rfs->new_fs->super);
137
138         init_resource_track(&rtrack, "blocks_to_move", fs->io);
139         retval = blocks_to_move(rfs);
140         if (retval)
141                 goto errout;
142         print_resource_track(rfs, &rtrack, fs->io);
143
144 #ifdef RESIZE2FS_DEBUG
145         if (rfs->flags & RESIZE_DEBUG_BMOVE)
146                 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
147                        ext2fs_free_blocks_count(rfs->old_fs->super),
148                        ext2fs_free_blocks_count(rfs->new_fs->super),
149                        rfs->needed_blocks);
150 #endif
151
152         init_resource_track(&rtrack, "block_mover", fs->io);
153         retval = block_mover(rfs);
154         if (retval)
155                 goto errout;
156         print_resource_track(rfs, &rtrack, fs->io);
157
158         init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
159         retval = inode_scan_and_fix(rfs);
160         if (retval)
161                 goto errout;
162         print_resource_track(rfs, &rtrack, fs->io);
163
164         init_resource_track(&rtrack, "inode_ref_fix", fs->io);
165         retval = inode_ref_fix(rfs);
166         if (retval)
167                 goto errout;
168         print_resource_track(rfs, &rtrack, fs->io);
169
170         init_resource_track(&rtrack, "move_itables", fs->io);
171         retval = move_itables(rfs);
172         if (retval)
173                 goto errout;
174         print_resource_track(rfs, &rtrack, fs->io);
175
176         init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
177         retval = ext2fs_calculate_summary_stats(rfs->new_fs);
178         if (retval)
179                 goto errout;
180         print_resource_track(rfs, &rtrack, fs->io);
181
182         init_resource_track(&rtrack, "fix_resize_inode", fs->io);
183         retval = fix_resize_inode(rfs->new_fs);
184         if (retval)
185                 goto errout;
186         print_resource_track(rfs, &rtrack, fs->io);
187
188         init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
189         retval = fix_sb_journal_backup(rfs->new_fs);
190         if (retval)
191                 goto errout;
192         print_resource_track(rfs, &rtrack, fs->io);
193
194         rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
195         rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
196
197         print_resource_track(rfs, &overall_track, fs->io);
198         retval = ext2fs_close(rfs->new_fs);
199         if (retval)
200                 goto errout;
201
202         rfs->flags = flags;
203
204         ext2fs_free(rfs->old_fs);
205         if (rfs->itable_buf)
206                 ext2fs_free_mem(&rfs->itable_buf);
207         if (rfs->reserve_blocks)
208                 ext2fs_free_block_bitmap(rfs->reserve_blocks);
209         if (rfs->move_blocks)
210                 ext2fs_free_block_bitmap(rfs->move_blocks);
211         ext2fs_free_mem(&rfs);
212
213         return 0;
214
215 errout:
216         if (rfs->new_fs)
217                 ext2fs_free(rfs->new_fs);
218         if (rfs->itable_buf)
219                 ext2fs_free_mem(&rfs->itable_buf);
220         ext2fs_free_mem(&rfs);
221         return retval;
222 }
223
224 /*
225  * Clean up the bitmaps for unitialized bitmaps
226  */
227 static void fix_uninit_block_bitmaps(ext2_filsys fs)
228 {
229         blk64_t         blk, lblk;
230         dgrp_t          g;
231         int             i;
232
233         if (!ext2fs_has_group_desc_csum(fs))
234                 return;
235
236         for (g=0; g < fs->group_desc_count; g++) {
237                 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
238                         continue;
239
240                 blk = ext2fs_group_first_block2(fs, g);
241                 lblk = ext2fs_group_last_block2(fs, g);
242                 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
243                                                   lblk - blk + 1);
244
245                 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
246                 ext2fs_mark_block_bitmap2(fs->block_map,
247                                           ext2fs_block_bitmap_loc(fs, g));
248                 ext2fs_mark_block_bitmap2(fs->block_map,
249                                           ext2fs_inode_bitmap_loc(fs, g));
250                 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
251                      i < (unsigned int) fs->inode_blocks_per_group;
252                      i++, blk++)
253                         ext2fs_mark_block_bitmap2(fs->block_map, blk);
254         }
255 }
256
257 /* --------------------------------------------------------------------
258  *
259  * Resize processing, phase 1.
260  *
261  * In this phase we adjust the in-memory superblock information, and
262  * initialize any new parts of the inode table.  The new parts of the
263  * inode table are created in virgin disk space, so we can abort here
264  * without any side effects.
265  * --------------------------------------------------------------------
266  */
267
268 /*
269  * If the group descriptor's bitmap and inode table blocks are valid,
270  * release them in the new filesystem data structure, and mark them as
271  * reserved so the old inode table blocks don't get overwritten.
272  */
273 static void free_gdp_blocks(ext2_filsys fs,
274                             ext2fs_block_bitmap reserve_blocks,
275                             ext2_filsys old_fs,
276                             dgrp_t group)
277 {
278         blk64_t blk;
279         int     j;
280
281         blk = ext2fs_block_bitmap_loc(old_fs, group);
282         if (blk &&
283             (blk < ext2fs_blocks_count(fs->super))) {
284                 ext2fs_block_alloc_stats2(fs, blk, -1);
285                 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
286         }
287
288         blk = ext2fs_inode_bitmap_loc(old_fs, group);
289         if (blk &&
290             (blk < ext2fs_blocks_count(fs->super))) {
291                 ext2fs_block_alloc_stats2(fs, blk, -1);
292                 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
293         }
294
295         blk = ext2fs_inode_table_loc(old_fs, group);
296         if (blk == 0 ||
297             (blk >= ext2fs_blocks_count(fs->super)))
298                 return;
299
300         for (j = 0;
301              j < fs->inode_blocks_per_group; j++, blk++) {
302                 if (blk >= ext2fs_blocks_count(fs->super))
303                         break;
304                 ext2fs_block_alloc_stats2(fs, blk, -1);
305                 ext2fs_mark_block_bitmap2(reserve_blocks, blk);
306         }
307 }
308
309 /*
310  * This routine is shared by the online and offline resize routines.
311  * All of the information which is adjusted in memory is done here.
312  */
313 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
314                          ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
315 {
316         errcode_t       retval;
317         blk64_t         overhead = 0;
318         blk64_t         rem;
319         blk64_t         blk, group_block;
320         blk64_t         real_end;
321         blk64_t         old_numblocks, numblocks, adjblocks;
322         unsigned long   i, j, old_desc_blocks;
323         unsigned int    meta_bg, meta_bg_size;
324         int             has_super, csum_flag;
325         unsigned long long new_inodes;  /* u64 to check for overflow */
326         double          percent;
327
328         ext2fs_blocks_count_set(fs->super, new_size);
329
330 retry:
331         fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
332                                        fs->super->s_first_data_block,
333                                        EXT2_BLOCKS_PER_GROUP(fs->super));
334         if (fs->group_desc_count == 0)
335                 return EXT2_ET_TOOSMALL;
336         fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
337                                           EXT2_DESC_PER_BLOCK(fs->super));
338
339         /*
340          * Overhead is the number of bookkeeping blocks per group.  It
341          * includes the superblock backup, the group descriptor
342          * backups, the inode bitmap, the block bitmap, and the inode
343          * table.
344          */
345         overhead = (int) (2 + fs->inode_blocks_per_group);
346
347         if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
348                 overhead += 1 + fs->desc_blocks +
349                         fs->super->s_reserved_gdt_blocks;
350
351         /*
352          * See if the last group is big enough to support the
353          * necessary data structures.  If not, we need to get rid of
354          * it.
355          */
356         rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
357                 fs->super->s_blocks_per_group;
358         if ((fs->group_desc_count == 1) && rem && (rem < overhead))
359                 return EXT2_ET_TOOSMALL;
360         if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
361                 ext2fs_blocks_count_set(fs->super,
362                                         ext2fs_blocks_count(fs->super) - rem);
363                 goto retry;
364         }
365         /*
366          * Adjust the number of inodes
367          */
368         new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
369         if (new_inodes > ~0U) {
370                 fprintf(stderr, _("inodes (%llu) must be less than %u"),
371                                    new_inodes, ~0U);
372                 return EXT2_ET_TOO_MANY_INODES;
373         }
374         fs->super->s_inodes_count = fs->super->s_inodes_per_group *
375                 fs->group_desc_count;
376
377         /*
378          * Adjust the number of free blocks
379          */
380         blk = ext2fs_blocks_count(old_fs->super);
381         if (blk > ext2fs_blocks_count(fs->super))
382                 ext2fs_free_blocks_count_set(fs->super, 
383                         ext2fs_free_blocks_count(fs->super) -
384                         (blk - ext2fs_blocks_count(fs->super)));
385         else
386                 ext2fs_free_blocks_count_set(fs->super, 
387                         ext2fs_free_blocks_count(fs->super) +
388                         (ext2fs_blocks_count(fs->super) - blk));
389
390         /*
391          * Adjust the number of reserved blocks
392          */
393         percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
394                 ext2fs_blocks_count(old_fs->super);
395         ext2fs_r_blocks_count_set(fs->super,
396                                   (percent * ext2fs_blocks_count(fs->super) /
397                                    100.0));
398
399         /*
400          * Adjust the bitmaps for size
401          */
402         retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
403                                             fs->super->s_inodes_count,
404                                             fs->inode_map);
405         if (retval) goto errout;
406
407         real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
408                      fs->group_desc_count)) - 1 +
409                 fs->super->s_first_data_block;
410         retval = ext2fs_resize_block_bitmap2(new_size - 1,
411                                              real_end, fs->block_map);
412         if (retval) goto errout;
413
414         /*
415          * If we are growing the file system, also grow the size of
416          * the reserve_blocks bitmap
417          */
418         if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
419                 retval = ext2fs_resize_block_bitmap2(new_size - 1,
420                                                      real_end, reserve_blocks);
421                 if (retval) goto errout;
422         }
423
424         /*
425          * Reallocate the group descriptors as necessary.
426          */
427         if (old_fs->desc_blocks != fs->desc_blocks) {
428                 retval = ext2fs_resize_mem(old_fs->desc_blocks *
429                                            fs->blocksize,
430                                            fs->desc_blocks * fs->blocksize,
431                                            &fs->group_desc);
432                 if (retval)
433                         goto errout;
434                 if (fs->desc_blocks > old_fs->desc_blocks)
435                         memset((char *) fs->group_desc +
436                                (old_fs->desc_blocks * fs->blocksize), 0,
437                                (fs->desc_blocks - old_fs->desc_blocks) *
438                                fs->blocksize);
439         }
440
441         /*
442          * If the resize_inode feature is set, and we are changing the
443          * number of descriptor blocks, then adjust
444          * s_reserved_gdt_blocks if possible to avoid needing to move
445          * the inode table either now or in the future.
446          */
447         if ((fs->super->s_feature_compat &
448              EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
449             (old_fs->desc_blocks != fs->desc_blocks)) {
450                 int new;
451
452                 new = ((int) fs->super->s_reserved_gdt_blocks) +
453                         (old_fs->desc_blocks - fs->desc_blocks);
454                 if (new < 0)
455                         new = 0;
456                 if (new > (int) fs->blocksize/4)
457                         new = fs->blocksize/4;
458                 fs->super->s_reserved_gdt_blocks = new;
459         }
460
461         /*
462          * If we are shrinking the number of block groups, we're done
463          * and can exit now.
464          */
465         if (old_fs->group_desc_count > fs->group_desc_count) {
466                 /*
467                  * Check the block groups that we are chopping off
468                  * and free any blocks associated with their metadata
469                  */
470                 for (i = fs->group_desc_count;
471                      i < old_fs->group_desc_count; i++)
472                         free_gdp_blocks(fs, reserve_blocks, old_fs, i);
473                 retval = 0;
474                 goto errout;
475         }
476
477         /*
478          * Fix the count of the last (old) block group
479          */
480         old_numblocks = (ext2fs_blocks_count(old_fs->super) -
481                          old_fs->super->s_first_data_block) %
482                                  old_fs->super->s_blocks_per_group;
483         if (!old_numblocks)
484                 old_numblocks = old_fs->super->s_blocks_per_group;
485         if (old_fs->group_desc_count == fs->group_desc_count) {
486                 numblocks = (ext2fs_blocks_count(fs->super) -
487                              fs->super->s_first_data_block) %
488                         fs->super->s_blocks_per_group;
489                 if (!numblocks)
490                         numblocks = fs->super->s_blocks_per_group;
491         } else
492                 numblocks = fs->super->s_blocks_per_group;
493         i = old_fs->group_desc_count - 1;
494         ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
495         ext2fs_group_desc_csum_set(fs, i);
496
497         /*
498          * If the number of block groups is staying the same, we're
499          * done and can exit now.  (If the number block groups is
500          * shrinking, we had exited earlier.)
501          */
502         if (old_fs->group_desc_count >= fs->group_desc_count) {
503                 retval = 0;
504                 goto errout;
505         }
506
507         /*
508          * Initialize the new block group descriptors
509          */
510         group_block = ext2fs_group_first_block2(fs,
511                                                 old_fs->group_desc_count);
512         csum_flag = ext2fs_has_group_desc_csum(fs);
513         if (access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
514                 lazy_itable_init = 1;
515         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
516                 old_desc_blocks = fs->super->s_first_meta_bg;
517         else
518                 old_desc_blocks = fs->desc_blocks +
519                         fs->super->s_reserved_gdt_blocks;
520
521         /*
522          * If we changed the number of block_group descriptor blocks,
523          * we need to make sure they are all marked as reserved in the
524          * file systems's block allocation map.
525          */
526         for (i = 0; i < old_fs->group_desc_count; i++)
527                 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
528
529         for (i = old_fs->group_desc_count;
530              i < fs->group_desc_count; i++) {
531                 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
532                        sizeof(struct ext2_group_desc));
533                 adjblocks = 0;
534
535                 ext2fs_bg_flags_zap(fs, i);
536                 if (csum_flag) {
537                         ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
538                         if (!lazy_itable_init)
539                                 ext2fs_bg_flags_set(fs, i,
540                                                     EXT2_BG_INODE_ZEROED);
541                         ext2fs_bg_itable_unused_set(fs, i,
542                                         fs->super->s_inodes_per_group);
543                 }
544
545                 numblocks = ext2fs_group_blocks_count(fs, i);
546                 if ((i < fs->group_desc_count - 1) && csum_flag)
547                         ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
548
549                 has_super = ext2fs_bg_has_super(fs, i);
550                 if (has_super) {
551                         ext2fs_block_alloc_stats2(fs, group_block, +1);
552                         adjblocks++;
553                 }
554                 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
555                 meta_bg = i / meta_bg_size;
556                 if (!(fs->super->s_feature_incompat &
557                       EXT2_FEATURE_INCOMPAT_META_BG) ||
558                     (meta_bg < fs->super->s_first_meta_bg)) {
559                         if (has_super) {
560                                 for (j=0; j < old_desc_blocks; j++)
561                                         ext2fs_block_alloc_stats2(fs,
562                                                  group_block + 1 + j, +1);
563                                 adjblocks += old_desc_blocks;
564                         }
565                 } else {
566                         if (has_super)
567                                 has_super = 1;
568                         if (((i % meta_bg_size) == 0) ||
569                             ((i % meta_bg_size) == 1) ||
570                             ((i % meta_bg_size) == (meta_bg_size-1)))
571                                 ext2fs_block_alloc_stats2(fs,
572                                                  group_block + has_super, +1);
573                 }
574
575                 adjblocks += 2 + fs->inode_blocks_per_group;
576
577                 numblocks -= adjblocks;
578                 ext2fs_free_blocks_count_set(fs->super,
579                              ext2fs_free_blocks_count(fs->super) - adjblocks);
580                 fs->super->s_free_inodes_count +=
581                         fs->super->s_inodes_per_group;
582                 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
583                 ext2fs_bg_free_inodes_count_set(fs, i,
584                                                 fs->super->s_inodes_per_group);
585                 ext2fs_bg_used_dirs_count_set(fs, i, 0);
586                 ext2fs_group_desc_csum_set(fs, i);
587
588                 retval = ext2fs_allocate_group_table(fs, i, 0);
589                 if (retval) goto errout;
590
591                 group_block += fs->super->s_blocks_per_group;
592         }
593         retval = 0;
594
595         /*
596          * Mark all of the metadata blocks as reserved so they won't
597          * get allocated by the call to ext2fs_allocate_group_table()
598          * in blocks_to_move(), where we allocate new blocks to
599          * replace those allocation bitmap and inode table blocks
600          * which have to get relocated to make space for an increased
601          * number of the block group descriptors.
602          */
603         if (reserve_blocks)
604                 mark_table_blocks(fs, reserve_blocks);
605
606 errout:
607         return (retval);
608 }
609
610 /*
611  * This routine adjusts the superblock and other data structures, both
612  * in disk as well as in memory...
613  */
614 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
615 {
616         ext2_filsys fs;
617         int             adj = 0;
618         errcode_t       retval;
619         blk64_t         group_block;
620         unsigned long   i;
621         unsigned long   max_group;
622
623         fs = rfs->new_fs;
624         ext2fs_mark_super_dirty(fs);
625         ext2fs_mark_bb_dirty(fs);
626         ext2fs_mark_ib_dirty(fs);
627
628         retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
629                                               &rfs->reserve_blocks);
630         if (retval)
631                 return retval;
632
633         retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
634         if (retval)
635                 goto errout;
636
637         /*
638          * Check to make sure there are enough inodes
639          */
640         if ((rfs->old_fs->super->s_inodes_count -
641              rfs->old_fs->super->s_free_inodes_count) >
642             rfs->new_fs->super->s_inodes_count) {
643                 retval = ENOSPC;
644                 goto errout;
645         }
646
647         /*
648          * If we are shrinking the number block groups, we're done and
649          * can exit now.
650          */
651         if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
652                 retval = 0;
653                 goto errout;
654         }
655
656         /*
657          * If the number of block groups is staying the same, we're
658          * done and can exit now.  (If the number block groups is
659          * shrinking, we had exited earlier.)
660          */
661         if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
662                 retval = 0;
663                 goto errout;
664         }
665
666         /*
667          * If we are using uninit_bg (aka GDT_CSUM) and the kernel
668          * supports lazy inode initialization, we can skip
669          * initializing the inode table.
670          */
671         if (lazy_itable_init && ext2fs_has_group_desc_csum(fs)) {
672                 retval = 0;
673                 goto errout;
674         }
675
676         /*
677          * Initialize the inode table
678          */
679         retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
680                                 &rfs->itable_buf);
681         if (retval)
682                 goto errout;
683
684         memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
685         group_block = ext2fs_group_first_block2(fs,
686                                                 rfs->old_fs->group_desc_count);
687         adj = rfs->old_fs->group_desc_count;
688         max_group = fs->group_desc_count - adj;
689         if (rfs->progress) {
690                 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
691                                        0, max_group);
692                 if (retval)
693                         goto errout;
694         }
695         for (i = rfs->old_fs->group_desc_count;
696              i < fs->group_desc_count; i++) {
697                 /*
698                  * Write out the new inode table
699                  */
700                 retval = io_channel_write_blk64(fs->io,
701                                                 ext2fs_inode_table_loc(fs, i),
702                                                 fs->inode_blocks_per_group,
703                                                 rfs->itable_buf);
704                 if (retval) goto errout;
705
706                 io_channel_flush(fs->io);
707                 if (rfs->progress) {
708                         retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
709                                                i - adj + 1, max_group);
710                         if (retval)
711                                 goto errout;
712                 }
713                 group_block += fs->super->s_blocks_per_group;
714         }
715         io_channel_flush(fs->io);
716         retval = 0;
717
718 errout:
719         return retval;
720 }
721
722 /* --------------------------------------------------------------------
723  *
724  * Resize processing, phase 2.
725  *
726  * In this phase we adjust determine which blocks need to be moved, in
727  * blocks_to_move().  We then copy the blocks to their ultimate new
728  * destinations using block_mover().  Since we are copying blocks to
729  * their new locations, again during this pass we can abort without
730  * any problems.
731  * --------------------------------------------------------------------
732  */
733
734 /*
735  * This helper function creates a block bitmap with all of the
736  * filesystem meta-data blocks.
737  */
738 static errcode_t mark_table_blocks(ext2_filsys fs,
739                                    ext2fs_block_bitmap bmap)
740 {
741         dgrp_t                  i;
742         blk64_t                 blk;
743
744         for (i = 0; i < fs->group_desc_count; i++) {
745                 ext2fs_reserve_super_and_bgd(fs, i, bmap);
746
747                 /*
748                  * Mark the blocks used for the inode table
749                  */
750                 blk = ext2fs_inode_table_loc(fs, i);
751                 if (blk)
752                         ext2fs_mark_block_bitmap_range2(bmap, blk,
753                                                 fs->inode_blocks_per_group);
754
755                 /*
756                  * Mark block used for the block bitmap
757                  */
758                 blk = ext2fs_block_bitmap_loc(fs, i);
759                 if (blk)
760                         ext2fs_mark_block_bitmap2(bmap, blk);
761
762                 /*
763                  * Mark block used for the inode bitmap
764                  */
765                 blk = ext2fs_inode_bitmap_loc(fs, i);
766                 if (blk)
767                         ext2fs_mark_block_bitmap2(bmap, blk);
768         }
769         return 0;
770 }
771
772 /*
773  * This function checks to see if a particular block (either a
774  * superblock or a block group descriptor) overlaps with an inode or
775  * block bitmap block, or with the inode table.
776  */
777 static void mark_fs_metablock(ext2_resize_t rfs,
778                               ext2fs_block_bitmap meta_bmap,
779                               int group, blk64_t blk)
780 {
781         ext2_filsys     fs = rfs->new_fs;
782
783         ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
784         ext2fs_block_alloc_stats2(fs, blk, +1);
785
786         /*
787          * Check to see if we overlap with the inode or block bitmap,
788          * or the inode tables.  If not, and the block is in use, then
789          * mark it as a block to be moved.
790          */
791         if (IS_BLOCK_BM(fs, group, blk)) {
792                 ext2fs_block_bitmap_loc_set(fs, group, 0);
793                 rfs->needed_blocks++;
794                 return;
795         }
796         if (IS_INODE_BM(fs, group, blk)) {
797                 ext2fs_inode_bitmap_loc_set(fs, group, 0);
798                 rfs->needed_blocks++;
799                 return;
800         }
801         if (IS_INODE_TB(fs, group, blk)) {
802                 ext2fs_inode_table_loc_set(fs, group, 0);
803                 rfs->needed_blocks++;
804                 return;
805         }
806         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
807                 dgrp_t i;
808
809                 for (i=0; i < rfs->old_fs->group_desc_count; i++) {
810                         if (IS_BLOCK_BM(fs, i, blk)) {
811                                 ext2fs_block_bitmap_loc_set(fs, i, 0);
812                                 rfs->needed_blocks++;
813                                 return;
814                         }
815                         if (IS_INODE_BM(fs, i, blk)) {
816                                 ext2fs_inode_bitmap_loc_set(fs, i, 0);
817                                 rfs->needed_blocks++;
818                                 return;
819                         }
820                         if (IS_INODE_TB(fs, i, blk)) {
821                                 ext2fs_inode_table_loc_set(fs, i, 0);
822                                 rfs->needed_blocks++;
823                                 return;
824                         }
825                 }
826         }
827
828         if (ext2fs_has_group_desc_csum(fs) &&
829             (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
830                 /*
831                  * If the block bitmap is uninitialized, which means
832                  * nothing other than standard metadata in use.
833                  */
834                 return;
835         } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
836                    !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
837                 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
838                 rfs->needed_blocks++;
839         }
840 }
841
842
843 /*
844  * This routine marks and unmarks reserved blocks in the new block
845  * bitmap.  It also determines which blocks need to be moved and
846  * places this information into the move_blocks bitmap.
847  */
848 static errcode_t blocks_to_move(ext2_resize_t rfs)
849 {
850         int             j, has_super;
851         dgrp_t          i, max_groups, g;
852         blk64_t         blk, group_blk;
853         blk64_t         old_blocks, new_blocks;
854         blk64_t         new_size;
855         unsigned int    meta_bg, meta_bg_size;
856         errcode_t       retval;
857         ext2_filsys     fs, old_fs;
858         ext2fs_block_bitmap     meta_bmap;
859         int             flex_bg;
860
861         fs = rfs->new_fs;
862         old_fs = rfs->old_fs;
863         if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
864                 fs = rfs->old_fs;
865
866         retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
867                                               &rfs->move_blocks);
868         if (retval)
869                 return retval;
870
871         retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
872                                               &meta_bmap);
873         if (retval)
874                 return retval;
875
876         retval = mark_table_blocks(old_fs, meta_bmap);
877         if (retval)
878                 return retval;
879
880         fs = rfs->new_fs;
881
882         /*
883          * If we're shrinking the filesystem, we need to move any
884          * group's metadata blocks (either allocation bitmaps or the
885          * inode table) which are beyond the end of the new
886          * filesystem.
887          */
888         new_size = ext2fs_blocks_count(fs->super);
889         if (new_size < ext2fs_blocks_count(old_fs->super)) {
890                 for (g = 0; g < fs->group_desc_count; g++) {
891                         int realloc = 0;
892                         /*
893                          * ext2fs_allocate_group_table will re-allocate any
894                          * metadata blocks whose location is set to zero.
895                          */
896                         if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
897                                 ext2fs_block_bitmap_loc_set(fs, g, 0);
898                                 realloc = 1;
899                         }
900                         if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
901                                 ext2fs_inode_bitmap_loc_set(fs, g, 0);
902                                 realloc = 1;
903                         }
904                         if ((ext2fs_inode_table_loc(fs, g) +
905                              fs->inode_blocks_per_group) > new_size) {
906                                 ext2fs_inode_table_loc_set(fs, g, 0);
907                                 realloc = 1;
908                         }
909
910                         if (realloc) {
911                                 retval = ext2fs_allocate_group_table(fs, g, 0);
912                                 if (retval)
913                                         return retval;
914                         }
915                 }
916         }
917
918         /*
919          * If we're shrinking the filesystem, we need to move all of
920          * the blocks that don't fit any more
921          */
922         for (blk = ext2fs_blocks_count(fs->super);
923              blk < ext2fs_blocks_count(old_fs->super); blk++) {
924                 g = ext2fs_group_of_blk2(fs, blk);
925                 if (ext2fs_has_group_desc_csum(fs) &&
926                     ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
927                         /*
928                          * The block bitmap is uninitialized, so skip
929                          * to the next block group.
930                          */
931                         blk = ext2fs_group_first_block2(fs, g+1) - 1;
932                         continue;
933                 }
934                 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
935                     !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
936                         ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
937                         rfs->needed_blocks++;
938                 }
939                 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
940         }
941
942         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) {
943                 old_blocks = old_fs->super->s_first_meta_bg;
944                 new_blocks = fs->super->s_first_meta_bg;
945         } else {
946                 old_blocks = old_fs->desc_blocks + old_fs->super->s_reserved_gdt_blocks;
947                 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
948         }
949
950         if (old_blocks == new_blocks) {
951                 retval = 0;
952                 goto errout;
953         }
954
955         max_groups = fs->group_desc_count;
956         if (max_groups > old_fs->group_desc_count)
957                 max_groups = old_fs->group_desc_count;
958         group_blk = old_fs->super->s_first_data_block;
959         /*
960          * If we're reducing the number of descriptor blocks, this
961          * makes life easy.  :-)   We just have to mark some extra
962          * blocks as free.
963          */
964         if (old_blocks > new_blocks) {
965                 for (i = 0; i < max_groups; i++) {
966                         if (!ext2fs_bg_has_super(fs, i)) {
967                                 group_blk += fs->super->s_blocks_per_group;
968                                 continue;
969                         }
970                         for (blk = group_blk+1+new_blocks;
971                              blk < group_blk+1+old_blocks; blk++) {
972                                 ext2fs_block_alloc_stats2(fs, blk, -1);
973                                 rfs->needed_blocks--;
974                         }
975                         group_blk += fs->super->s_blocks_per_group;
976                 }
977                 retval = 0;
978                 goto errout;
979         }
980         /*
981          * If we're increasing the number of descriptor blocks, life
982          * gets interesting....
983          */
984         meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
985         flex_bg = fs->super->s_feature_incompat &
986                 EXT4_FEATURE_INCOMPAT_FLEX_BG;
987         /* first reserve all of the existing fs meta blocks */
988         for (i = 0; i < max_groups; i++) {
989                 has_super = ext2fs_bg_has_super(fs, i);
990                 if (has_super)
991                         mark_fs_metablock(rfs, meta_bmap, i, group_blk);
992
993                 meta_bg = i / meta_bg_size;
994                 if (!(fs->super->s_feature_incompat &
995                       EXT2_FEATURE_INCOMPAT_META_BG) ||
996                     (meta_bg < fs->super->s_first_meta_bg)) {
997                         if (has_super) {
998                                 for (blk = group_blk+1;
999                                      blk < group_blk + 1 + new_blocks; blk++)
1000                                         mark_fs_metablock(rfs, meta_bmap,
1001                                                           i, blk);
1002                         }
1003                 } else {
1004                         if (has_super)
1005                                 has_super = 1;
1006                         if (((i % meta_bg_size) == 0) ||
1007                             ((i % meta_bg_size) == 1) ||
1008                             ((i % meta_bg_size) == (meta_bg_size-1)))
1009                                 mark_fs_metablock(rfs, meta_bmap, i,
1010                                                   group_blk + has_super);
1011                 }
1012
1013                 /*
1014                  * Reserve the existing meta blocks that we know
1015                  * aren't to be moved.
1016                  *
1017                  * For flex_bg file systems, in order to avoid
1018                  * overwriting fs metadata (especially inode table
1019                  * blocks) belonging to a different block group when
1020                  * we are relocating the inode tables, we need to
1021                  * reserve all existing fs metadata blocks.
1022                  */
1023                 if (ext2fs_block_bitmap_loc(fs, i))
1024                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1025                                  ext2fs_block_bitmap_loc(fs, i));
1026                 else if (flex_bg && i < old_fs->group_desc_count)
1027                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1028                                  ext2fs_block_bitmap_loc(old_fs, i));
1029
1030                 if (ext2fs_inode_bitmap_loc(fs, i))
1031                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1032                                  ext2fs_inode_bitmap_loc(fs, i));
1033                 else if (flex_bg && i < old_fs->group_desc_count)
1034                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1035                                  ext2fs_inode_bitmap_loc(old_fs, i));
1036
1037                 if (ext2fs_inode_table_loc(fs, i))
1038                         ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1039                                         ext2fs_inode_table_loc(fs, i),
1040                                         fs->inode_blocks_per_group);
1041                 else if (flex_bg && i < old_fs->group_desc_count)
1042                         ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1043                                         ext2fs_inode_table_loc(old_fs, i),
1044                                         old_fs->inode_blocks_per_group);
1045
1046                 group_blk += rfs->new_fs->super->s_blocks_per_group;
1047         }
1048
1049         /* Allocate the missing data structures */
1050         for (i = 0; i < max_groups; i++) {
1051                 if (ext2fs_inode_table_loc(fs, i) &&
1052                     ext2fs_inode_bitmap_loc(fs, i) &&
1053                     ext2fs_block_bitmap_loc(fs, i))
1054                         continue;
1055
1056                 retval = ext2fs_allocate_group_table(fs, i,
1057                                                      rfs->reserve_blocks);
1058                 if (retval)
1059                         goto errout;
1060
1061                 /*
1062                  * For those structures that have changed, we need to
1063                  * do bookkeepping.
1064                  */
1065                 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1066                     (blk = ext2fs_block_bitmap_loc(fs, i))) {
1067                         ext2fs_block_alloc_stats2(fs, blk, +1);
1068                         if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1069                             !ext2fs_test_block_bitmap2(meta_bmap, blk))
1070                                 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1071                                                          blk);
1072                 }
1073                 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1074                     (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1075                         ext2fs_block_alloc_stats2(fs, blk, +1);
1076                         if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1077                             !ext2fs_test_block_bitmap2(meta_bmap, blk))
1078                                 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1079                                                          blk);
1080                 }
1081
1082                 /*
1083                  * The inode table, if we need to relocate it, is
1084                  * handled specially.  We have to reserve the blocks
1085                  * for both the old and the new inode table, since we
1086                  * can't have the inode table be destroyed during the
1087                  * block relocation phase.
1088                  */
1089                 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1090                         continue;       /* inode table not moved */
1091
1092                 rfs->needed_blocks += fs->inode_blocks_per_group;
1093
1094                 /*
1095                  * Mark the new inode table as in use in the new block
1096                  * allocation bitmap, and move any blocks that might
1097                  * be necessary.
1098                  */
1099                 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1100                      j < fs->inode_blocks_per_group ; j++, blk++) {
1101                         ext2fs_block_alloc_stats2(fs, blk, +1);
1102                         if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1103                             !ext2fs_test_block_bitmap2(meta_bmap, blk))
1104                                 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1105                                                          blk);
1106                 }
1107
1108                 /*
1109                  * Make sure the old inode table is reserved in the
1110                  * block reservation bitmap.
1111                  */
1112                 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1113                      j < fs->inode_blocks_per_group ; j++, blk++)
1114                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1115         }
1116         retval = 0;
1117
1118 errout:
1119         if (meta_bmap)
1120                 ext2fs_free_block_bitmap(meta_bmap);
1121
1122         return retval;
1123 }
1124
1125 /*
1126  * This helper function tries to allocate a new block.  We try to
1127  * avoid hitting the original group descriptor blocks at least at
1128  * first, since we want to make it possible to recover from a badly
1129  * aborted resize operation as much as possible.
1130  *
1131  * In the future, I may further modify this routine to balance out
1132  * where we get the new blocks across the various block groups.
1133  * Ideally we would allocate blocks that corresponded with the block
1134  * group of the containing inode, and keep contiguous blocks
1135  * together.  However, this very difficult to do efficiently, since we
1136  * don't have the necessary information up front.
1137  */
1138
1139 #define AVOID_OLD       1
1140 #define DESPERATION     2
1141
1142 static void init_block_alloc(ext2_resize_t rfs)
1143 {
1144         rfs->alloc_state = AVOID_OLD;
1145         rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1146 #if 0
1147         /* HACK for testing */
1148         if (ext2fs_blocks_count(rfs->new_fs->super) >
1149             ext2fs_blocks_count(rfs->old_fs->super))
1150                 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1151 #endif
1152 }
1153
1154 static blk64_t get_new_block(ext2_resize_t rfs)
1155 {
1156         ext2_filsys     fs = rfs->new_fs;
1157
1158         while (1) {
1159                 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1160                         if (rfs->alloc_state == DESPERATION)
1161                                 return 0;
1162
1163 #ifdef RESIZE2FS_DEBUG
1164                         if (rfs->flags & RESIZE_DEBUG_BMOVE)
1165                                 printf("Going into desperation mode "
1166                                        "for block allocations\n");
1167 #endif
1168                         rfs->alloc_state = DESPERATION;
1169                         rfs->new_blk = fs->super->s_first_data_block;
1170                         continue;
1171                 }
1172                 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1173                     ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1174                                              rfs->new_blk) ||
1175                     ((rfs->alloc_state == AVOID_OLD) &&
1176                      (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1177                      ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1178                                               rfs->new_blk))) {
1179                         rfs->new_blk++;
1180                         continue;
1181                 }
1182                 return rfs->new_blk;
1183         }
1184 }
1185
1186 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs, blk64_t goal,
1187                                            blk64_t *ret)
1188 {
1189         ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1190         blk64_t blk;
1191
1192         blk = get_new_block(rfs);
1193         if (!blk)
1194                 return ENOSPC;
1195
1196 #ifdef RESIZE2FS_DEBUG
1197         if (rfs->flags & 0xF)
1198                 printf("get_alloc_block allocating %llu\n", blk);
1199 #endif
1200
1201         ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1202         ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1203         *ret = (blk64_t) blk;
1204         return 0;
1205 }
1206
1207 static errcode_t block_mover(ext2_resize_t rfs)
1208 {
1209         blk64_t                 blk, old_blk, new_blk;
1210         ext2_filsys             fs = rfs->new_fs;
1211         ext2_filsys             old_fs = rfs->old_fs;
1212         errcode_t               retval;
1213         __u64                   size;
1214         int                     c;
1215         int                     to_move, moved;
1216         ext2_badblocks_list     badblock_list = 0;
1217         int                     bb_modified = 0;
1218
1219         fs->get_alloc_block = resize2fs_get_alloc_block;
1220         old_fs->get_alloc_block = resize2fs_get_alloc_block;
1221
1222         retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1223         if (retval)
1224                 return retval;
1225
1226         new_blk = fs->super->s_first_data_block;
1227         if (!rfs->itable_buf) {
1228                 retval = ext2fs_get_array(fs->blocksize,
1229                                         fs->inode_blocks_per_group,
1230                                         &rfs->itable_buf);
1231                 if (retval)
1232                         return retval;
1233         }
1234         retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1235         if (retval)
1236                 return retval;
1237
1238         /*
1239          * The first step is to figure out where all of the blocks
1240          * will go.
1241          */
1242         to_move = moved = 0;
1243         init_block_alloc(rfs);
1244         for (blk = B2C(old_fs->super->s_first_data_block);
1245              blk < ext2fs_blocks_count(old_fs->super);
1246              blk += EXT2FS_CLUSTER_RATIO(fs)) {
1247                 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1248                         continue;
1249                 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1250                         continue;
1251                 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1252                         ext2fs_badblocks_list_del(badblock_list, blk);
1253                         bb_modified++;
1254                         continue;
1255                 }
1256
1257                 new_blk = get_new_block(rfs);
1258                 if (!new_blk) {
1259                         retval = ENOSPC;
1260                         goto errout;
1261                 }
1262                 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1263                 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1264                 to_move++;
1265         }
1266
1267         if (to_move == 0) {
1268                 if (rfs->bmap) {
1269                         ext2fs_free_extent_table(rfs->bmap);
1270                         rfs->bmap = 0;
1271                 }
1272                 retval = 0;
1273                 goto errout;
1274         }
1275
1276         /*
1277          * Step two is to actually move the blocks
1278          */
1279         retval =  ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1280         if (retval) goto errout;
1281
1282         if (rfs->progress) {
1283                 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1284                                          0, to_move);
1285                 if (retval)
1286                         goto errout;
1287         }
1288         while (1) {
1289                 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1290                 if (retval) goto errout;
1291                 if (!size)
1292                         break;
1293                 old_blk = C2B(old_blk);
1294                 new_blk = C2B(new_blk);
1295                 size = C2B(size);
1296 #ifdef RESIZE2FS_DEBUG
1297                 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1298                         printf("Moving %llu blocks %llu->%llu\n",
1299                                size, old_blk, new_blk);
1300 #endif
1301                 do {
1302                         c = size;
1303                         if (c > fs->inode_blocks_per_group)
1304                                 c = fs->inode_blocks_per_group;
1305                         retval = io_channel_read_blk64(fs->io, old_blk, c,
1306                                                        rfs->itable_buf);
1307                         if (retval) goto errout;
1308                         retval = io_channel_write_blk64(fs->io, new_blk, c,
1309                                                         rfs->itable_buf);
1310                         if (retval) goto errout;
1311                         size -= c;
1312                         new_blk += c;
1313                         old_blk += c;
1314                         moved += c;
1315                         if (rfs->progress) {
1316                                 io_channel_flush(fs->io);
1317                                 retval = (rfs->progress)(rfs,
1318                                                 E2_RSZ_BLOCK_RELOC_PASS,
1319                                                 moved, to_move);
1320                                 if (retval)
1321                                         goto errout;
1322                         }
1323                 } while (size > 0);
1324                 io_channel_flush(fs->io);
1325         }
1326
1327 errout:
1328         if (badblock_list) {
1329                 if (!retval && bb_modified)
1330                         retval = ext2fs_update_bb_inode(old_fs,
1331                                                         badblock_list);
1332                 ext2fs_badblocks_list_free(badblock_list);
1333         }
1334         return retval;
1335 }
1336
1337
1338 /* --------------------------------------------------------------------
1339  *
1340  * Resize processing, phase 3
1341  *
1342  * --------------------------------------------------------------------
1343  */
1344
1345
1346 /*
1347  * The extent translation table is stored in clusters so we need to
1348  * take special care when mapping a source block number to its
1349  * destination block number.
1350  */
1351 static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1352 {
1353         __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1354
1355         if (new_block != 0)
1356                 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1357         return new_block;
1358 }
1359
1360 struct process_block_struct {
1361         ext2_resize_t           rfs;
1362         ext2_ino_t              ino;
1363         ext2_ino_t              old_ino;
1364         struct ext2_inode *     inode;
1365         errcode_t               error;
1366         int                     is_dir;
1367         int                     changed;
1368         int                     has_extents;
1369 };
1370
1371 static int process_block(ext2_filsys fs, blk64_t        *block_nr,
1372                          e2_blkcnt_t blockcnt,
1373                          blk64_t ref_block EXT2FS_ATTR((unused)),
1374                          int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1375 {
1376         struct process_block_struct *pb;
1377         errcode_t       retval;
1378         blk64_t         block, new_block;
1379         int             ret = 0;
1380
1381         pb = (struct process_block_struct *) priv_data;
1382         block = *block_nr;
1383         if (pb->rfs->bmap) {
1384                 new_block = extent_translate(fs, pb->rfs->bmap, block);
1385                 if (new_block) {
1386                         *block_nr = new_block;
1387                         ret |= BLOCK_CHANGED;
1388                         pb->changed = 1;
1389 #ifdef RESIZE2FS_DEBUG
1390                         if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1391                                 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1392                                        pb->old_ino, blockcnt, block,
1393                                        new_block);
1394 #endif
1395                         block = new_block;
1396                 }
1397         }
1398
1399         /*
1400          * If we moved inodes and metadata_csum is enabled, we must force the
1401          * extent block to be rewritten with new checksum.
1402          */
1403         if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
1404                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1405             pb->has_extents &&
1406             pb->old_ino != pb->ino)
1407                 ret |= BLOCK_CHANGED;
1408
1409         if (pb->is_dir) {
1410                 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1411                                                block, (int) blockcnt);
1412                 if (retval) {
1413                         pb->error = retval;
1414                         ret |= BLOCK_ABORT;
1415                 }
1416         }
1417         return ret;
1418 }
1419
1420 /*
1421  * Progress callback
1422  */
1423 static errcode_t progress_callback(ext2_filsys fs,
1424                                    ext2_inode_scan scan EXT2FS_ATTR((unused)),
1425                                    dgrp_t group, void * priv_data)
1426 {
1427         ext2_resize_t rfs = (ext2_resize_t) priv_data;
1428         errcode_t               retval;
1429
1430         /*
1431          * This check is to protect against old ext2 libraries.  It
1432          * shouldn't be needed against new libraries.
1433          */
1434         if ((group+1) == 0)
1435                 return 0;
1436
1437         if (rfs->progress) {
1438                 io_channel_flush(fs->io);
1439                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1440                                          group+1, fs->group_desc_count);
1441                 if (retval)
1442                         return retval;
1443         }
1444
1445         return 0;
1446 }
1447
1448 static errcode_t migrate_ea_block(ext2_resize_t rfs, ext2_ino_t ino,
1449                                   struct ext2_inode *inode, int *changed)
1450 {
1451         char *buf;
1452         blk64_t new_block;
1453         errcode_t err = 0;
1454
1455         /* No EA block or no remapping?  Quit early. */
1456         if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 && !rfs->bmap)
1457                 return 0;
1458         new_block = extent_translate(rfs->old_fs, rfs->bmap,
1459                 ext2fs_file_acl_block(rfs->old_fs, inode));
1460         if (new_block == 0)
1461                 return 0;
1462
1463         /* Set the new ACL block */
1464         ext2fs_file_acl_block_set(rfs->old_fs, inode, new_block);
1465
1466         /* Update checksum */
1467         if (EXT2_HAS_RO_COMPAT_FEATURE(rfs->new_fs->super,
1468                         EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1469                 err = ext2fs_get_mem(rfs->old_fs->blocksize, &buf);
1470                 if (err)
1471                         return err;
1472                 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1473                 err = ext2fs_read_ext_attr3(rfs->old_fs, new_block, buf, ino);
1474                 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1475                 if (err)
1476                         goto out;
1477                 err = ext2fs_write_ext_attr3(rfs->old_fs, new_block, buf, ino);
1478                 if (err)
1479                         goto out;
1480         }
1481         *changed = 1;
1482
1483 out:
1484         ext2fs_free_mem(&buf);
1485         return err;
1486 }
1487
1488 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
1489 {
1490         struct process_block_struct     pb;
1491         ext2_ino_t              ino, new_inode;
1492         struct ext2_inode       *inode = NULL;
1493         ext2_inode_scan         scan = NULL;
1494         errcode_t               retval;
1495         char                    *block_buf = 0;
1496         ext2_ino_t              start_to_move;
1497         blk64_t                 orig_size;
1498         int                     inode_size;
1499
1500         if ((rfs->old_fs->group_desc_count <=
1501              rfs->new_fs->group_desc_count) &&
1502             !rfs->bmap)
1503                 return 0;
1504
1505         /*
1506          * Save the original size of the old filesystem, and
1507          * temporarily set the size to be the new size if the new size
1508          * is larger.  We need to do this to avoid catching an error
1509          * by the block iterator routines
1510          */
1511         orig_size = ext2fs_blocks_count(rfs->old_fs->super);
1512         if (orig_size < ext2fs_blocks_count(rfs->new_fs->super))
1513                 ext2fs_blocks_count_set(rfs->old_fs->super,
1514                                 ext2fs_blocks_count(rfs->new_fs->super));
1515
1516         retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
1517         if (retval) goto errout;
1518
1519         retval = ext2fs_init_dblist(rfs->old_fs, 0);
1520         if (retval) goto errout;
1521         retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
1522         if (retval) goto errout;
1523
1524         start_to_move = (rfs->new_fs->group_desc_count *
1525                          rfs->new_fs->super->s_inodes_per_group);
1526
1527         if (rfs->progress) {
1528                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1529                                          0, rfs->old_fs->group_desc_count);
1530                 if (retval)
1531                         goto errout;
1532         }
1533         ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
1534         pb.rfs = rfs;
1535         pb.inode = inode;
1536         pb.error = 0;
1537         new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
1538         inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
1539         inode = malloc(inode_size);
1540         if (!inode) {
1541                 retval = ENOMEM;
1542                 goto errout;
1543         }
1544         /*
1545          * First, copy all of the inodes that need to be moved
1546          * elsewhere in the inode table
1547          */
1548         while (1) {
1549                 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
1550                 if (retval) goto errout;
1551                 if (!ino)
1552                         break;
1553
1554                 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
1555                         continue; /* inode not in use */
1556
1557                 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1558                 pb.changed = 0;
1559
1560                 /* Remap EA block */
1561                 retval = migrate_ea_block(rfs, ino, inode, &pb.changed);
1562                 if (retval)
1563                         goto errout;
1564
1565                 new_inode = ino;
1566                 if (ino <= start_to_move)
1567                         goto remap_blocks; /* Don't need to move inode. */
1568
1569                 /*
1570                  * Find a new inode.  Now that extents and directory blocks
1571                  * are tied to the inode number through the checksum, we must
1572                  * set up the new inode before we start rewriting blocks.
1573                  */
1574                 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
1575                 if (retval)
1576                         goto errout;
1577
1578                 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
1579                                           pb.is_dir);
1580                 inode->i_ctime = time(0);
1581                 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
1582                                                 inode, inode_size);
1583                 if (retval)
1584                         goto errout;
1585                 pb.changed = 0;
1586
1587 #ifdef RESIZE2FS_DEBUG
1588                 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
1589                         printf("Inode moved %u->%u\n", ino, new_inode);
1590 #endif
1591                 if (!rfs->imap) {
1592                         retval = ext2fs_create_extent_table(&rfs->imap, 0);
1593                         if (retval)
1594                                 goto errout;
1595                 }
1596                 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
1597
1598 remap_blocks:
1599                 if (pb.changed)
1600                         retval = ext2fs_write_inode_full(rfs->old_fs,
1601                                                          new_inode,
1602                                                          inode, inode_size);
1603                 if (retval)
1604                         goto errout;
1605
1606                 /*
1607                  * Update inodes to point to new blocks; schedule directory
1608                  * blocks for inode remapping.  Need to write out dir blocks
1609                  * with new inode numbers if we have metadata_csum enabled.
1610                  */
1611                 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
1612                     (rfs->bmap || pb.is_dir)) {
1613                         pb.ino = new_inode;
1614                         pb.old_ino = ino;
1615                         pb.has_extents = inode->i_flags & EXT4_EXTENTS_FL;
1616                         rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1617                         retval = ext2fs_block_iterate3(rfs->old_fs,
1618                                                        new_inode, 0, block_buf,
1619                                                        process_block, &pb);
1620                         rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1621                         if (retval)
1622                                 goto errout;
1623                         if (pb.error) {
1624                                 retval = pb.error;
1625                                 goto errout;
1626                         }
1627                 }
1628         }
1629         io_channel_flush(rfs->old_fs->io);
1630
1631 errout:
1632         ext2fs_blocks_count_set(rfs->old_fs->super, orig_size);
1633         if (rfs->bmap) {
1634                 ext2fs_free_extent_table(rfs->bmap);
1635                 rfs->bmap = 0;
1636         }
1637         if (scan)
1638                 ext2fs_close_inode_scan(scan);
1639         if (block_buf)
1640                 ext2fs_free_mem(&block_buf);
1641         free(inode);
1642         return retval;
1643 }
1644
1645 /* --------------------------------------------------------------------
1646  *
1647  * Resize processing, phase 4.
1648  *
1649  * --------------------------------------------------------------------
1650  */
1651
1652 struct istruct {
1653         ext2_resize_t rfs;
1654         errcode_t       err;
1655         unsigned int    max_dirs;
1656         unsigned int    num;
1657 };
1658
1659 static int check_and_change_inodes(ext2_ino_t dir,
1660                                    int entry EXT2FS_ATTR((unused)),
1661                                    struct ext2_dir_entry *dirent, int offset,
1662                                    int  blocksize EXT2FS_ATTR((unused)),
1663                                    char *buf EXT2FS_ATTR((unused)),
1664                                    void *priv_data)
1665 {
1666         struct istruct *is = (struct istruct *) priv_data;
1667         struct ext2_inode       inode;
1668         ext2_ino_t              new_inode;
1669         errcode_t               retval;
1670         int                     ret = 0;
1671
1672         if (is->rfs->progress && offset == 0) {
1673                 io_channel_flush(is->rfs->old_fs->io);
1674                 is->err = (is->rfs->progress)(is->rfs,
1675                                               E2_RSZ_INODE_REF_UPD_PASS,
1676                                               ++is->num, is->max_dirs);
1677                 if (is->err)
1678                         return DIRENT_ABORT;
1679         }
1680
1681         /*
1682          * If we have checksums enabled and the inode wasn't present in the
1683          * old fs, then we must rewrite all dir blocks with new checksums.
1684          */
1685         if (EXT2_HAS_RO_COMPAT_FEATURE(is->rfs->old_fs->super,
1686                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1687             !ext2fs_test_inode_bitmap2(is->rfs->old_fs->inode_map, dir))
1688                 ret |= DIRENT_CHANGED;
1689
1690         if (!dirent->inode)
1691                 return ret;
1692
1693         new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
1694
1695         if (!new_inode)
1696                 return ret;
1697 #ifdef RESIZE2FS_DEBUG
1698         if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
1699                 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
1700                        dir, ext2fs_dirent_name_len(dirent), dirent->name,
1701                        dirent->inode, new_inode);
1702 #endif
1703
1704         dirent->inode = new_inode;
1705
1706         /* Update the directory mtime and ctime */
1707         retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
1708         if (retval == 0) {
1709                 inode.i_mtime = inode.i_ctime = time(0);
1710                 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
1711                 if (is->err)
1712                         return ret | DIRENT_ABORT;
1713         }
1714
1715         return ret | DIRENT_CHANGED;
1716 }
1717
1718 static errcode_t inode_ref_fix(ext2_resize_t rfs)
1719 {
1720         errcode_t               retval;
1721         struct istruct          is;
1722
1723         if (!rfs->imap)
1724                 return 0;
1725
1726         /*
1727          * Now, we iterate over all of the directories to update the
1728          * inode references
1729          */
1730         is.num = 0;
1731         is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
1732         is.rfs = rfs;
1733         is.err = 0;
1734
1735         if (rfs->progress) {
1736                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1737                                          0, is.max_dirs);
1738                 if (retval)
1739                         goto errout;
1740         }
1741
1742         rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1743         retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
1744                                            DIRENT_FLAG_INCLUDE_EMPTY, 0,
1745                                            check_and_change_inodes, &is);
1746         rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1747         if (retval)
1748                 goto errout;
1749         if (is.err) {
1750                 retval = is.err;
1751                 goto errout;
1752         }
1753
1754         if (rfs->progress && (is.num < is.max_dirs))
1755                 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
1756                                 is.max_dirs, is.max_dirs);
1757
1758 errout:
1759         ext2fs_free_extent_table(rfs->imap);
1760         rfs->imap = 0;
1761         return retval;
1762 }
1763
1764
1765 /* --------------------------------------------------------------------
1766  *
1767  * Resize processing, phase 5.
1768  *
1769  * In this phase we actually move the inode table around, and then
1770  * update the summary statistics.  This is scary, since aborting here
1771  * will potentially scramble the filesystem.  (We are moving the
1772  * inode tables around in place, and so the potential for lost data,
1773  * or at the very least scrambling the mapping between filenames and
1774  * inode numbers is very high in case of a power failure here.)
1775  * --------------------------------------------------------------------
1776  */
1777
1778
1779 /*
1780  * A very scary routine --- this one moves the inode table around!!!
1781  *
1782  * After this you have to use the rfs->new_fs file handle to read and
1783  * write inodes.
1784  */
1785 static errcode_t move_itables(ext2_resize_t rfs)
1786 {
1787         int             n, num, size;
1788         long long       diff;
1789         dgrp_t          i, max_groups;
1790         ext2_filsys     fs = rfs->new_fs;
1791         char            *cp;
1792         blk64_t         old_blk, new_blk, blk;
1793         errcode_t       retval;
1794         int             j, to_move, moved;
1795
1796         max_groups = fs->group_desc_count;
1797         if (max_groups > rfs->old_fs->group_desc_count)
1798                 max_groups = rfs->old_fs->group_desc_count;
1799
1800         size = fs->blocksize * fs->inode_blocks_per_group;
1801         if (!rfs->itable_buf) {
1802                 retval = ext2fs_get_mem(size, &rfs->itable_buf);
1803                 if (retval)
1804                         return retval;
1805         }
1806
1807         /*
1808          * Figure out how many inode tables we need to move
1809          */
1810         to_move = moved = 0;
1811         for (i=0; i < max_groups; i++)
1812                 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
1813                     ext2fs_inode_table_loc(fs, i))
1814                         to_move++;
1815
1816         if (to_move == 0)
1817                 return 0;
1818
1819         if (rfs->progress) {
1820                 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1821                                        0, to_move);
1822                 if (retval)
1823                         goto errout;
1824         }
1825
1826         rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1827
1828         for (i=0; i < max_groups; i++) {
1829                 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
1830                 new_blk = ext2fs_inode_table_loc(fs, i);
1831                 diff = new_blk - old_blk;
1832
1833 #ifdef RESIZE2FS_DEBUG
1834                 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1835                         printf("Itable move group %d block %llu->%llu (diff %lld)\n",
1836                                i, old_blk, new_blk, diff);
1837 #endif
1838
1839                 if (!diff)
1840                         continue;
1841
1842                 retval = io_channel_read_blk64(fs->io, old_blk,
1843                                                fs->inode_blocks_per_group,
1844                                                rfs->itable_buf);
1845                 if (retval)
1846                         goto errout;
1847                 /*
1848                  * The end of the inode table segment often contains
1849                  * all zeros, and we're often only moving the inode
1850                  * table down a block or two.  If so, we can optimize
1851                  * things by not rewriting blocks that we know to be zero
1852                  * already.
1853                  */
1854                 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
1855                         if (*cp)
1856                                 break;
1857                 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
1858 #ifdef RESIZE2FS_DEBUG
1859                 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1860                         printf("%d blocks of zeros...\n", n);
1861 #endif
1862                 num = fs->inode_blocks_per_group;
1863                 if (n > diff)
1864                         num -= n;
1865
1866                 retval = io_channel_write_blk64(fs->io, new_blk,
1867                                                 num, rfs->itable_buf);
1868                 if (retval) {
1869                         io_channel_write_blk64(fs->io, old_blk,
1870                                                num, rfs->itable_buf);
1871                         goto errout;
1872                 }
1873                 if (n > diff) {
1874                         retval = io_channel_write_blk64(fs->io,
1875                               old_blk + fs->inode_blocks_per_group,
1876                               diff, (rfs->itable_buf +
1877                                      (fs->inode_blocks_per_group - diff) *
1878                                      fs->blocksize));
1879                         if (retval)
1880                                 goto errout;
1881                 }
1882
1883                 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1884                      j < fs->inode_blocks_per_group ; j++, blk++)
1885                         ext2fs_block_alloc_stats2(fs, blk, -1);
1886
1887                 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
1888                 ext2fs_group_desc_csum_set(rfs->old_fs, i);
1889                 ext2fs_mark_super_dirty(rfs->old_fs);
1890                 ext2fs_flush(rfs->old_fs);
1891
1892                 if (rfs->progress) {
1893                         retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
1894                                                ++moved, to_move);
1895                         if (retval)
1896                                 goto errout;
1897                 }
1898         }
1899         mark_table_blocks(fs, fs->block_map);
1900         ext2fs_flush(fs);
1901 #ifdef RESIZE2FS_DEBUG
1902         if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
1903                 printf("Inode table move finished.\n");
1904 #endif
1905         return 0;
1906
1907 errout:
1908         return retval;
1909 }
1910
1911 /*
1912  * Fix the resize inode
1913  */
1914 static errcode_t fix_resize_inode(ext2_filsys fs)
1915 {
1916         struct ext2_inode       inode;
1917         errcode_t               retval;
1918         char                    *block_buf = NULL;
1919
1920         if (!(fs->super->s_feature_compat &
1921               EXT2_FEATURE_COMPAT_RESIZE_INODE))
1922                 return 0;
1923
1924         retval = ext2fs_get_mem(fs->blocksize, &block_buf);
1925         if (retval) goto errout;
1926
1927         retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
1928         if (retval) goto errout;
1929
1930         ext2fs_iblk_set(fs, &inode, 1);
1931
1932         retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
1933         if (retval) goto errout;
1934
1935         if (!inode.i_block[EXT2_DIND_BLOCK]) {
1936                 /*
1937                  * Avoid zeroing out block #0; that's rude.  This
1938                  * should never happen anyway since the filesystem
1939                  * should be fsck'ed and we assume it is consistent.
1940                  */
1941                 fprintf(stderr, "%s",
1942                         _("Should never happen: resize inode corrupt!\n"));
1943                 exit(1);
1944         }
1945
1946         memset(block_buf, 0, fs->blocksize);
1947
1948         retval = io_channel_write_blk64(fs->io, inode.i_block[EXT2_DIND_BLOCK],
1949                                         1, block_buf);
1950         if (retval) goto errout;
1951
1952         retval = ext2fs_create_resize_inode(fs);
1953         if (retval)
1954                 goto errout;
1955
1956 errout:
1957         if (block_buf)
1958                 ext2fs_free_mem(&block_buf);
1959         return retval;
1960 }
1961
1962 /*
1963  * Finally, recalculate the summary information
1964  */
1965 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1966 {
1967         blk64_t         blk;
1968         ext2_ino_t      ino;
1969         unsigned int    group = 0;
1970         unsigned int    count = 0;
1971         blk64_t         total_blocks_free = 0;
1972         int             total_inodes_free = 0;
1973         int             group_free = 0;
1974         int             uninit = 0;
1975         blk64_t         super_blk, old_desc_blk, new_desc_blk;
1976         int             old_desc_blocks;
1977
1978         /*
1979          * First calculate the block statistics
1980          */
1981         uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
1982         ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
1983                                   &new_desc_blk, 0);
1984         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
1985                 old_desc_blocks = fs->super->s_first_meta_bg;
1986         else
1987                 old_desc_blocks = fs->desc_blocks +
1988                         fs->super->s_reserved_gdt_blocks;
1989         for (blk = B2C(fs->super->s_first_data_block);
1990              blk < ext2fs_blocks_count(fs->super);
1991              blk += EXT2FS_CLUSTER_RATIO(fs)) {
1992                 if ((uninit &&
1993                      !(EQ_CLSTR(blk, super_blk) ||
1994                        ((old_desc_blk && old_desc_blocks &&
1995                          GE_CLSTR(blk, old_desc_blk) &&
1996                          LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
1997                        ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
1998                        EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
1999                        EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
2000                        ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
2001                          LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
2002                                   + fs->inode_blocks_per_group))))) ||
2003                     (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
2004                         group_free++;
2005                         total_blocks_free++;
2006                 }
2007                 count++;
2008                 if ((count == fs->super->s_clusters_per_group) ||
2009                     EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
2010                         ext2fs_bg_free_blocks_count_set(fs, group, group_free);
2011                         ext2fs_group_desc_csum_set(fs, group);
2012                         group++;
2013                         if (group >= fs->group_desc_count)
2014                                 break;
2015                         count = 0;
2016                         group_free = 0;
2017                         uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2018                         ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
2019                                                   &old_desc_blk,
2020                                                   &new_desc_blk, 0);
2021                         if (fs->super->s_feature_incompat &
2022                             EXT2_FEATURE_INCOMPAT_META_BG)
2023                                 old_desc_blocks = fs->super->s_first_meta_bg;
2024                         else
2025                                 old_desc_blocks = fs->desc_blocks +
2026                                         fs->super->s_reserved_gdt_blocks;
2027                 }
2028         }
2029         total_blocks_free = C2B(total_blocks_free);
2030         ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
2031
2032         /*
2033          * Next, calculate the inode statistics
2034          */
2035         group_free = 0;
2036         count = 0;
2037         group = 0;
2038
2039         /* Protect loop from wrap-around if s_inodes_count maxed */
2040         uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2041         for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2042                 if (uninit ||
2043                     !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2044                         group_free++;
2045                         total_inodes_free++;
2046                 }
2047                 count++;
2048                 if ((count == fs->super->s_inodes_per_group) ||
2049                     (ino == fs->super->s_inodes_count)) {
2050                         ext2fs_bg_free_inodes_count_set(fs, group, group_free);
2051                         ext2fs_group_desc_csum_set(fs, group);
2052                         group++;
2053                         if (group >= fs->group_desc_count)
2054                                 break;
2055                         count = 0;
2056                         group_free = 0;
2057                         uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2058                 }
2059         }
2060         fs->super->s_free_inodes_count = total_inodes_free;
2061         ext2fs_mark_super_dirty(fs);
2062         return 0;
2063 }
2064
2065 /*
2066  *  Journal may have been relocated; update the backup journal blocks
2067  *  in the superblock.
2068  */
2069 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2070 {
2071         errcode_t         retval;
2072         struct ext2_inode inode;
2073
2074         if (!(fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
2075                 return 0;
2076
2077         /* External journal? Nothing to do. */
2078         if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2079                 return 0;
2080
2081         retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2082         if (retval)
2083                 return retval;
2084         memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
2085         fs->super->s_jnl_blocks[15] = inode.i_size_high;
2086         fs->super->s_jnl_blocks[16] = inode.i_size;
2087         fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2088         ext2fs_mark_super_dirty(fs);
2089         return 0;
2090 }
2091
2092 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2093                                int old_desc_blocks)
2094 {
2095         blk64_t super_blk, old_desc_blk, new_desc_blk;
2096         int overhead;
2097
2098         /* inode table blocks plus allocation bitmaps */
2099         overhead = fs->inode_blocks_per_group + 2;
2100
2101         ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2102                                   &old_desc_blk, &new_desc_blk, 0);
2103         if ((grp == 0) || super_blk)
2104                 overhead++;
2105         if (old_desc_blk)
2106                 overhead += old_desc_blocks;
2107         else if (new_desc_blk)
2108                 overhead++;
2109         return overhead;
2110 }
2111
2112
2113 /*
2114  * calcluate the minimum number of blocks the given fs can be resized to
2115  */
2116 blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
2117 {
2118         ext2_ino_t inode_count;
2119         dgrp_t groups;
2120         blk64_t blks_needed, data_blocks;
2121         blk64_t grp, data_needed, last_start;
2122         blk64_t overhead = 0;
2123         int old_desc_blocks;
2124         int extra_groups = 0;
2125         int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2126
2127         /*
2128          * first figure out how many group descriptors we need to
2129          * handle the number of inodes we have
2130          */
2131         inode_count = fs->super->s_inodes_count -
2132                 fs->super->s_free_inodes_count;
2133         blks_needed = ext2fs_div_ceil(inode_count,
2134                                       fs->super->s_inodes_per_group) *
2135                 EXT2_BLOCKS_PER_GROUP(fs->super);
2136         groups = ext2fs_div64_ceil(blks_needed,
2137                                    EXT2_BLOCKS_PER_GROUP(fs->super));
2138 #ifdef RESIZE2FS_DEBUG
2139         if (flags & RESIZE_DEBUG_MIN_CALC)
2140                 printf("fs has %d inodes, %d groups required.\n",
2141                        inode_count, groups);
2142 #endif
2143
2144         /*
2145          * number of old-style block group descriptor blocks
2146          */
2147         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
2148                 old_desc_blocks = fs->super->s_first_meta_bg;
2149         else
2150                 old_desc_blocks = fs->desc_blocks +
2151                         fs->super->s_reserved_gdt_blocks;
2152
2153         /* calculate how many blocks are needed for data */
2154         data_needed = ext2fs_blocks_count(fs->super) -
2155                 ext2fs_free_blocks_count(fs->super);
2156
2157         for (grp = 0; grp < fs->group_desc_count; grp++)
2158                 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2159 #ifdef RESIZE2FS_DEBUG
2160         if (flags & RESIZE_DEBUG_MIN_CALC)
2161                 printf("fs requires %llu data blocks.\n", data_needed);
2162 #endif
2163
2164         /*
2165          * For ext4 we need to allow for up to a flex_bg worth of
2166          * inode tables of slack space so the resize operation can be
2167          * guaranteed to finish.
2168          */
2169         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2170                 extra_groups = flexbg_size - (groups & (flexbg_size - 1));
2171                 data_needed += fs->inode_blocks_per_group * extra_groups;
2172                 extra_groups = groups % flexbg_size;
2173         }
2174
2175         /*
2176          * figure out how many data blocks we have given the number of groups
2177          * we need for our inodes
2178          */
2179         data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
2180         last_start = 0;
2181         for (grp = 0; grp < groups; grp++) {
2182                 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
2183
2184                 /*
2185                  * we want to keep track of how much data we can store in
2186                  * the groups leading up to the last group so we can determine
2187                  * how big the last group needs to be
2188                  */
2189                 if (grp != (groups - 1))
2190                         last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
2191                                 overhead;
2192
2193                 data_blocks -= overhead;
2194         }
2195 #ifdef RESIZE2FS_DEBUG
2196         if (flags & RESIZE_DEBUG_MIN_CALC)
2197                 printf("With %d group(s), we have %llu blocks available.\n",
2198                        groups, data_blocks);
2199 #endif
2200
2201         /*
2202          * if we need more group descriptors in order to accomodate our data
2203          * then we need to add them here
2204          */
2205         while (data_needed > data_blocks) {
2206                 blk64_t remainder = data_needed - data_blocks;
2207                 dgrp_t extra_grps;
2208
2209                 /* figure out how many more groups we need for the data */
2210                 extra_grps = ext2fs_div64_ceil(remainder,
2211                                                EXT2_BLOCKS_PER_GROUP(fs->super));
2212
2213                 data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
2214
2215                 /* ok we have to account for the last group */
2216                 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2217                 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
2218
2219                 for (grp = groups; grp < groups+extra_grps; grp++) {
2220                         overhead = calc_group_overhead(fs, grp,
2221                                                        old_desc_blocks);
2222
2223                         /*
2224                          * again, we need to see how much data we cram into
2225                          * all of the groups leading up to the last group
2226                          */
2227                         if (grp != (groups + extra_grps - 1))
2228                                 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
2229                                         - overhead;
2230
2231                         data_blocks -= overhead;
2232                 }
2233
2234                 groups += extra_grps;
2235                 extra_groups += extra_grps;
2236                 if (fs->super->s_feature_incompat
2237                         & EXT4_FEATURE_INCOMPAT_FLEX_BG
2238                     && extra_groups > flexbg_size) {
2239                         /*
2240                          * For ext4 we need to allow for up to a flex_bg worth
2241                          * of inode tables of slack space so the resize
2242                          * operation can be guaranteed to finish.
2243                          */
2244                         extra_groups = flexbg_size -
2245                                                 (groups & (flexbg_size - 1));
2246                         data_needed += (fs->inode_blocks_per_group *
2247                                         extra_groups);
2248                         extra_groups = groups % flexbg_size;
2249                 }
2250 #ifdef RESIZE2FS_DEBUG
2251                 if (flags & RESIZE_DEBUG_MIN_CALC)
2252                         printf("Added %d extra group(s), "
2253                                "data_needed %llu, data_blocks %llu, "
2254                                "last_start %llu\n",
2255                                extra_grps, data_needed, data_blocks,
2256                                last_start);
2257 #endif
2258         }
2259
2260         /* now for the fun voodoo */
2261         overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
2262 #ifdef RESIZE2FS_DEBUG
2263         if (flags & RESIZE_DEBUG_MIN_CALC)
2264                 printf("Last group's overhead is %llu\n", overhead);
2265 #endif
2266
2267         /*
2268          * if this is the case then the last group is going to have data in it
2269          * so we need to adjust the size of the last group accordingly
2270          */
2271         if (last_start < data_needed) {
2272                 blk64_t remainder = data_needed - last_start;
2273
2274 #ifdef RESIZE2FS_DEBUG
2275                 if (flags & RESIZE_DEBUG_MIN_CALC)
2276                         printf("Need %llu data blocks in last group\n",
2277                                remainder);
2278 #endif
2279                 /*
2280                  * 50 is a magic number that mkfs/resize uses to see if its
2281                  * even worth making/resizing the fs.  basically you need to
2282                  * have at least 50 blocks in addition to the blocks needed
2283                  * for the metadata in the last group
2284                  */
2285                 if (remainder > 50)
2286                         overhead += remainder;
2287                 else
2288                         overhead += 50;
2289         } else
2290                 overhead += 50;
2291
2292         overhead += fs->super->s_first_data_block;
2293 #ifdef RESIZE2FS_DEBUG
2294         if (flags & RESIZE_DEBUG_MIN_CALC)
2295                 printf("Final size of last group is %lld\n", overhead);
2296 #endif
2297
2298         /*
2299          * since our last group doesn't have to be BLOCKS_PER_GROUP large, we
2300          * only do groups-1, and then add the number of blocks needed to
2301          * handle the group descriptor metadata+data that we need
2302          */
2303         blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
2304         blks_needed += overhead;
2305
2306         /*
2307          * Make sure blks_needed covers the end of the inode table in
2308          * the last block group.
2309          */
2310         overhead = ext2fs_inode_table_loc(fs, groups-1) +
2311                 fs->inode_blocks_per_group;
2312         if (blks_needed < overhead)
2313                 blks_needed = overhead;
2314
2315 #ifdef RESIZE2FS_DEBUG
2316         if (flags & RESIZE_DEBUG_MIN_CALC)
2317                 printf("Estimated blocks needed: %llu\n", blks_needed);
2318 #endif
2319
2320         /*
2321          * If at this point we've already added up more "needed" than
2322          * the current size, just return current size as minimum.
2323          */
2324         if (blks_needed >= ext2fs_blocks_count(fs->super))
2325                 return ext2fs_blocks_count(fs->super);
2326         /*
2327          * We need to reserve a few extra blocks if extents are
2328          * enabled, in case we need to grow the extent tree.  The more
2329          * we shrink the file system, the more space we need.
2330          */
2331         if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
2332                 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
2333                                        blks_needed)/500;
2334 #ifdef RESIZE2FS_DEBUG
2335                 if (flags & RESIZE_DEBUG_MIN_CALC)
2336                         printf("Extents safety margin: %llu\n", safe_margin);
2337 #endif
2338                 blks_needed += safe_margin;
2339         }
2340
2341         return blks_needed;
2342 }