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