Whamcloud - gitweb
resize2fs: add support for resizing filesystems with ea_inode feature
[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 static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size);
60 static errcode_t move_bg_metadata(ext2_resize_t rfs);
61 static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs);
62
63 /*
64  * Some helper functions to check if a block is in a metadata area
65  */
66 static inline int is_block_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
67 {
68         return blk == ext2fs_block_bitmap_loc(fs, grp);
69 }
70
71 static inline int is_inode_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
72 {
73         return blk == ext2fs_inode_bitmap_loc(fs, grp);
74 }
75
76 static int is_inode_tb(ext2_filsys fs, unsigned int grp, blk64_t blk)
77 {
78         return blk >= ext2fs_inode_table_loc(fs, grp) &&
79                blk < (ext2fs_inode_table_loc(fs, grp) +
80                       fs->inode_blocks_per_group);
81 }
82
83 /* Some bigalloc helper macros which are more succint... */
84 #define B2C(x)  EXT2FS_B2C(fs, (x))
85 #define C2B(x)  EXT2FS_C2B(fs, (x))
86 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
87 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
88 #define LT_CLSTR(x, y) (B2C(x) <  B2C(y))
89 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
90 #define GT_CLSTR(x, y) (B2C(x) >  B2C(y))
91
92 static int lazy_itable_init;
93
94 /*
95  * This is the top-level routine which does the dirty deed....
96  */
97 errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
98             errcode_t (*progress)(ext2_resize_t rfs, int pass,
99                                           unsigned long cur,
100                                           unsigned long max_val))
101 {
102         ext2_resize_t   rfs;
103         errcode_t       retval;
104         struct resource_track   rtrack, overall_track;
105
106         /*
107          * Create the data structure
108          */
109         retval = ext2fs_get_mem(sizeof(struct ext2_resize_struct), &rfs);
110         if (retval)
111                 return retval;
112
113         memset(rfs, 0, sizeof(struct ext2_resize_struct));
114         fs->priv_data = rfs;
115         rfs->old_fs = fs;
116         rfs->flags = flags;
117         rfs->itable_buf  = 0;
118         rfs->progress = progress;
119
120         init_resource_track(&overall_track, "overall resize2fs", fs->io);
121         init_resource_track(&rtrack, "read_bitmaps", fs->io);
122         retval = ext2fs_read_bitmaps(fs);
123         if (retval)
124                 goto errout;
125         print_resource_track(rfs, &rtrack, fs->io);
126
127         fs->super->s_state |= EXT2_ERROR_FS;
128         ext2fs_mark_super_dirty(fs);
129         ext2fs_flush(fs);
130
131         init_resource_track(&rtrack, "fix_uninit_block_bitmaps 1", fs->io);
132         fix_uninit_block_bitmaps(fs);
133         print_resource_track(rfs, &rtrack, fs->io);
134         retval = ext2fs_dup_handle(fs, &rfs->new_fs);
135         if (retval)
136                 goto errout;
137
138         init_resource_track(&rtrack, "resize_group_descriptors", fs->io);
139         retval = resize_group_descriptors(rfs, *new_size);
140         if (retval)
141                 goto errout;
142         print_resource_track(rfs, &rtrack, fs->io);
143
144         init_resource_track(&rtrack, "move_bg_metadata", fs->io);
145         retval = move_bg_metadata(rfs);
146         if (retval)
147                 goto errout;
148         print_resource_track(rfs, &rtrack, fs->io);
149
150         init_resource_track(&rtrack, "zero_high_bits_in_metadata", fs->io);
151         retval = zero_high_bits_in_inodes(rfs);
152         if (retval)
153                 goto errout;
154         print_resource_track(rfs, &rtrack, fs->io);
155
156         init_resource_track(&rtrack, "adjust_superblock", fs->io);
157         retval = adjust_superblock(rfs, *new_size);
158         if (retval)
159                 goto errout;
160         print_resource_track(rfs, &rtrack, fs->io);
161
162         init_resource_track(&rtrack, "fix_uninit_block_bitmaps 2", fs->io);
163         fix_uninit_block_bitmaps(rfs->new_fs);
164         print_resource_track(rfs, &rtrack, fs->io);
165         /* Clear the block bitmap uninit flag for the last block group */
166         ext2fs_bg_flags_clear(rfs->new_fs, rfs->new_fs->group_desc_count - 1,
167                              EXT2_BG_BLOCK_UNINIT);
168
169         *new_size = ext2fs_blocks_count(rfs->new_fs->super);
170
171         init_resource_track(&rtrack, "blocks_to_move", fs->io);
172         retval = blocks_to_move(rfs);
173         if (retval)
174                 goto errout;
175         print_resource_track(rfs, &rtrack, fs->io);
176
177 #ifdef RESIZE2FS_DEBUG
178         if (rfs->flags & RESIZE_DEBUG_BMOVE)
179                 printf("Number of free blocks: %llu/%llu, Needed: %llu\n",
180                        ext2fs_free_blocks_count(rfs->old_fs->super),
181                        ext2fs_free_blocks_count(rfs->new_fs->super),
182                        rfs->needed_blocks);
183 #endif
184
185         init_resource_track(&rtrack, "block_mover", fs->io);
186         retval = block_mover(rfs);
187         if (retval)
188                 goto errout;
189         print_resource_track(rfs, &rtrack, fs->io);
190
191         init_resource_track(&rtrack, "inode_scan_and_fix", fs->io);
192         retval = inode_scan_and_fix(rfs);
193         if (retval)
194                 goto errout;
195         print_resource_track(rfs, &rtrack, fs->io);
196
197         init_resource_track(&rtrack, "inode_ref_fix", fs->io);
198         retval = inode_ref_fix(rfs);
199         if (retval)
200                 goto errout;
201         print_resource_track(rfs, &rtrack, fs->io);
202
203         init_resource_track(&rtrack, "move_itables", fs->io);
204         retval = move_itables(rfs);
205         if (retval)
206                 goto errout;
207         print_resource_track(rfs, &rtrack, fs->io);
208
209         retval = clear_sparse_super2_last_group(rfs);
210         if (retval)
211                 goto errout;
212
213         init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
214         retval = ext2fs_calculate_summary_stats(rfs->new_fs);
215         if (retval)
216                 goto errout;
217         print_resource_track(rfs, &rtrack, fs->io);
218
219         init_resource_track(&rtrack, "fix_resize_inode", fs->io);
220         retval = fix_resize_inode(rfs->new_fs);
221         if (retval)
222                 goto errout;
223         print_resource_track(rfs, &rtrack, fs->io);
224
225         init_resource_track(&rtrack, "fix_sb_journal_backup", fs->io);
226         retval = fix_sb_journal_backup(rfs->new_fs);
227         if (retval)
228                 goto errout;
229         print_resource_track(rfs, &rtrack, fs->io);
230
231         retval = ext2fs_set_gdt_csum(rfs->new_fs);
232         if (retval)
233                 goto errout;
234
235         rfs->new_fs->super->s_state &= ~EXT2_ERROR_FS;
236         rfs->new_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
237
238         print_resource_track(rfs, &overall_track, fs->io);
239         retval = ext2fs_close_free(&rfs->new_fs);
240         if (retval)
241                 goto errout;
242
243         rfs->flags = flags;
244
245         ext2fs_free(rfs->old_fs);
246         rfs->old_fs = NULL;
247         if (rfs->itable_buf)
248                 ext2fs_free_mem(&rfs->itable_buf);
249         if (rfs->reserve_blocks)
250                 ext2fs_free_block_bitmap(rfs->reserve_blocks);
251         if (rfs->move_blocks)
252                 ext2fs_free_block_bitmap(rfs->move_blocks);
253         ext2fs_free_mem(&rfs);
254
255         return 0;
256
257 errout:
258         if (rfs->new_fs) {
259                 ext2fs_free(rfs->new_fs);
260                 rfs->new_fs = NULL;
261         }
262         if (rfs->itable_buf)
263                 ext2fs_free_mem(&rfs->itable_buf);
264         ext2fs_free_mem(&rfs);
265         return retval;
266 }
267
268 /* Keep the size of the group descriptor region constant */
269 static void adjust_reserved_gdt_blocks(ext2_filsys old_fs, ext2_filsys fs)
270 {
271         if (ext2fs_has_feature_resize_inode(fs->super) &&
272             (old_fs->desc_blocks != fs->desc_blocks)) {
273                 int new;
274
275                 new = ((int) fs->super->s_reserved_gdt_blocks) +
276                         (old_fs->desc_blocks - fs->desc_blocks);
277                 if (new < 0)
278                         new = 0;
279                 if (new > (int) fs->blocksize/4)
280                         new = fs->blocksize/4;
281                 fs->super->s_reserved_gdt_blocks = new;
282         }
283 }
284
285 /* Toggle 64bit mode */
286 static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size)
287 {
288         void *o, *n, *new_group_desc;
289         dgrp_t i;
290         int copy_size;
291         errcode_t retval;
292
293         if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
294                 return 0;
295
296         if (new_size != ext2fs_blocks_count(rfs->new_fs->super) ||
297             ext2fs_blocks_count(rfs->new_fs->super) >= (1ULL << 32) ||
298             (rfs->flags & RESIZE_DISABLE_64BIT &&
299              rfs->flags & RESIZE_ENABLE_64BIT))
300                 return EXT2_ET_INVALID_ARGUMENT;
301
302         if (rfs->flags & RESIZE_DISABLE_64BIT) {
303                 ext2fs_clear_feature_64bit(rfs->new_fs->super);
304                 rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE;
305         } else if (rfs->flags & RESIZE_ENABLE_64BIT) {
306                 ext2fs_set_feature_64bit(rfs->new_fs->super);
307                 rfs->new_fs->super->s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
308         }
309
310         if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
311             EXT2_DESC_SIZE(rfs->new_fs->super))
312                 return 0;
313
314         o = rfs->new_fs->group_desc;
315         rfs->new_fs->desc_blocks = ext2fs_div_ceil(
316                         rfs->old_fs->group_desc_count,
317                         EXT2_DESC_PER_BLOCK(rfs->new_fs->super));
318         retval = ext2fs_get_arrayzero(rfs->new_fs->desc_blocks,
319                                       rfs->old_fs->blocksize, &new_group_desc);
320         if (retval)
321                 return retval;
322
323         n = new_group_desc;
324
325         if (EXT2_DESC_SIZE(rfs->old_fs->super) <=
326             EXT2_DESC_SIZE(rfs->new_fs->super))
327                 copy_size = EXT2_DESC_SIZE(rfs->old_fs->super);
328         else
329                 copy_size = EXT2_DESC_SIZE(rfs->new_fs->super);
330         for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
331                 memcpy(n, o, copy_size);
332                 n = (char *)n + EXT2_DESC_SIZE(rfs->new_fs->super);
333                 o = (char *)o + EXT2_DESC_SIZE(rfs->old_fs->super);
334         }
335
336         ext2fs_free_mem(&rfs->new_fs->group_desc);
337         rfs->new_fs->group_desc = new_group_desc;
338
339         for (i = 0; i < rfs->old_fs->group_desc_count; i++)
340                 ext2fs_group_desc_csum_set(rfs->new_fs, i);
341
342         adjust_reserved_gdt_blocks(rfs->old_fs, rfs->new_fs);
343
344         return 0;
345 }
346
347 /* Move bitmaps/inode tables out of the way. */
348 static errcode_t move_bg_metadata(ext2_resize_t rfs)
349 {
350         dgrp_t i;
351         blk64_t b, c, d, old_desc_blocks, new_desc_blocks, j;
352         ext2fs_block_bitmap old_map, new_map;
353         int old, new;
354         errcode_t retval;
355         int cluster_ratio;
356
357         if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
358                 return 0;
359
360         retval = ext2fs_allocate_block_bitmap(rfs->old_fs, "oldfs", &old_map);
361         if (retval)
362                 return retval;
363
364         retval = ext2fs_allocate_block_bitmap(rfs->new_fs, "newfs", &new_map);
365         if (retval)
366                 goto out;
367
368         if (ext2fs_has_feature_meta_bg(rfs->old_fs->super)) {
369                 old_desc_blocks = rfs->old_fs->super->s_first_meta_bg;
370                 new_desc_blocks = rfs->new_fs->super->s_first_meta_bg;
371         } else {
372                 old_desc_blocks = rfs->old_fs->desc_blocks +
373                                 rfs->old_fs->super->s_reserved_gdt_blocks;
374                 new_desc_blocks = rfs->new_fs->desc_blocks +
375                                 rfs->new_fs->super->s_reserved_gdt_blocks;
376         }
377
378         /* Construct bitmaps of super/descriptor blocks in old and new fs */
379         for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
380                 retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, i, &b, &c, &d,
381                                                    NULL);
382                 if (retval)
383                         goto out;
384                 if (b)
385                         ext2fs_mark_block_bitmap2(old_map, b);
386                 for (j = 0; c != 0 && j < old_desc_blocks; j++)
387                         ext2fs_mark_block_bitmap2(old_map, c + j);
388                 if (d)
389                         ext2fs_mark_block_bitmap2(old_map, d);
390
391                 retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, i, &b, &c, &d,
392                                                    NULL);
393                 if (retval)
394                         goto out;
395                 if (b)
396                         ext2fs_mark_block_bitmap2(new_map, b);
397                 for (j = 0; c != 0 && j < new_desc_blocks; j++)
398                         ext2fs_mark_block_bitmap2(new_map, c + j);
399                 if (d)
400                         ext2fs_mark_block_bitmap2(new_map, d);
401         }
402
403         cluster_ratio = EXT2FS_CLUSTER_RATIO(rfs->new_fs);
404
405         /* Find changes in block allocations for bg metadata */
406         for (b = EXT2FS_B2C(rfs->old_fs,
407                             rfs->old_fs->super->s_first_data_block);
408              b < ext2fs_blocks_count(rfs->new_fs->super);
409              b += cluster_ratio) {
410                 old = ext2fs_test_block_bitmap2(old_map, b);
411                 new = ext2fs_test_block_bitmap2(new_map, b);
412
413                 if (old && !new) {
414                         /* mark old_map, unmark new_map */
415                         if (cluster_ratio == 1)
416                                 ext2fs_unmark_block_bitmap2(
417                                                 rfs->new_fs->block_map, b);
418                 } else if (!old && new)
419                         ; /* unmark old_map, mark new_map */
420                 else {
421                         ext2fs_unmark_block_bitmap2(old_map, b);
422                         ext2fs_unmark_block_bitmap2(new_map, b);
423                 }
424         }
425
426         /*
427          * new_map now shows blocks that have been newly allocated.
428          * old_map now shows blocks that have been newly freed.
429          */
430
431         /*
432          * Move any conflicting bitmaps and inode tables.  Ensure that we
433          * don't try to free clusters associated with bitmaps or tables.
434          */
435         for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
436                 b = ext2fs_block_bitmap_loc(rfs->new_fs, i);
437                 if (ext2fs_test_block_bitmap2(new_map, b))
438                         ext2fs_block_bitmap_loc_set(rfs->new_fs, i, 0);
439                 else if (ext2fs_test_block_bitmap2(old_map, b))
440                         ext2fs_unmark_block_bitmap2(old_map, b);
441
442                 b = ext2fs_inode_bitmap_loc(rfs->new_fs, i);
443                 if (ext2fs_test_block_bitmap2(new_map, b))
444                         ext2fs_inode_bitmap_loc_set(rfs->new_fs, i, 0);
445                 else if (ext2fs_test_block_bitmap2(old_map, b))
446                         ext2fs_unmark_block_bitmap2(old_map, b);
447
448                 c = ext2fs_inode_table_loc(rfs->new_fs, i);
449                 for (b = 0;
450                      b < rfs->new_fs->inode_blocks_per_group;
451                      b++) {
452                         if (ext2fs_test_block_bitmap2(new_map, b + c))
453                                 ext2fs_inode_table_loc_set(rfs->new_fs, i, 0);
454                         else if (ext2fs_test_block_bitmap2(old_map, b + c))
455                                 ext2fs_unmark_block_bitmap2(old_map, b + c);
456                 }
457         }
458
459         /* Free unused clusters */
460         for (b = 0;
461              cluster_ratio > 1 && b < ext2fs_blocks_count(rfs->new_fs->super);
462              b += cluster_ratio)
463                 if (ext2fs_test_block_bitmap2(old_map, b))
464                         ext2fs_unmark_block_bitmap2(rfs->new_fs->block_map, b);
465 out:
466         if (old_map)
467                 ext2fs_free_block_bitmap(old_map);
468         if (new_map)
469                 ext2fs_free_block_bitmap(new_map);
470         return retval;
471 }
472
473 /* Zero out the high bits of extent fields */
474 static errcode_t zero_high_bits_in_extents(ext2_filsys fs, ext2_ino_t ino,
475                                  struct ext2_inode *inode)
476 {
477         ext2_extent_handle_t    handle;
478         struct ext2fs_extent    extent;
479         int                     op = EXT2_EXTENT_ROOT;
480         errcode_t               errcode;
481
482         if (!(inode->i_flags & EXT4_EXTENTS_FL))
483                 return 0;
484
485         errcode = ext2fs_extent_open(fs, ino, &handle);
486         if (errcode)
487                 return errcode;
488
489         while (1) {
490                 errcode = ext2fs_extent_get(handle, op, &extent);
491                 if (errcode)
492                         break;
493
494                 op = EXT2_EXTENT_NEXT_SIB;
495
496                 if (extent.e_pblk > (1ULL << 32)) {
497                         extent.e_pblk &= (1ULL << 32) - 1;
498                         errcode = ext2fs_extent_replace(handle, 0, &extent);
499                         if (errcode)
500                                 break;
501                 }
502         }
503
504         /* Ok if we run off the end */
505         if (errcode == EXT2_ET_EXTENT_NO_NEXT)
506                 errcode = 0;
507         ext2fs_extent_free(handle);
508         return errcode;
509 }
510
511 /* Zero out the high bits of inodes. */
512 static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs)
513 {
514         ext2_filsys     fs = rfs->old_fs;
515         int length = EXT2_INODE_SIZE(fs->super);
516         struct ext2_inode *inode = NULL;
517         ext2_inode_scan scan = NULL;
518         errcode_t       retval;
519         ext2_ino_t      ino;
520
521         if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
522                 return 0;
523
524         if (fs->super->s_creator_os == EXT2_OS_HURD)
525                 return 0;
526
527         retval = ext2fs_open_inode_scan(fs, 0, &scan);
528         if (retval)
529                 return retval;
530
531         retval = ext2fs_get_mem(length, &inode);
532         if (retval)
533                 goto out;
534
535         do {
536                 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
537                 if (retval)
538                         goto out;
539                 if (!ino)
540                         break;
541                 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino))
542                         continue;
543
544                 /*
545                  * Here's how we deal with high block number fields:
546                  *
547                  *  - i_size_high has been been written out with i_size_lo
548                  *    since the ext2 days, so no conversion is needed.
549                  *
550                  *  - i_blocks_hi is guarded by both the huge_file feature and
551                  *    inode flags and has always been written out with
552                  *    i_blocks_lo if the feature is set.  The field is only
553                  *    ever read if both feature and inode flag are set, so
554                  *    we don't need to zero it now.
555                  *
556                  *  - i_file_acl_high can be uninitialized, so zero it if
557                  *    it isn't already.
558                  */
559                 if (inode->osd2.linux2.l_i_file_acl_high) {
560                         inode->osd2.linux2.l_i_file_acl_high = 0;
561                         retval = ext2fs_write_inode_full(fs, ino, inode,
562                                                          length);
563                         if (retval)
564                                 goto out;
565                 }
566
567                 retval = zero_high_bits_in_extents(fs, ino, inode);
568                 if (retval)
569                         goto out;
570         } while (ino);
571
572 out:
573         if (inode)
574                 ext2fs_free_mem(&inode);
575         if (scan)
576                 ext2fs_close_inode_scan(scan);
577         return retval;
578 }
579
580 /*
581  * Clean up the bitmaps for unitialized bitmaps
582  */
583 static void fix_uninit_block_bitmaps(ext2_filsys fs)
584 {
585         blk64_t         blk, lblk;
586         dgrp_t          g;
587         unsigned int    i;
588
589         if (!ext2fs_has_group_desc_csum(fs))
590                 return;
591
592         for (g=0; g < fs->group_desc_count; g++) {
593                 if (!(ext2fs_bg_flags_test(fs, g, EXT2_BG_BLOCK_UNINIT)))
594                         continue;
595
596                 blk = ext2fs_group_first_block2(fs, g);
597                 lblk = ext2fs_group_last_block2(fs, g);
598                 ext2fs_unmark_block_bitmap_range2(fs->block_map, blk,
599                                                   lblk - blk + 1);
600
601                 ext2fs_reserve_super_and_bgd(fs, g, fs->block_map);
602                 ext2fs_mark_block_bitmap2(fs->block_map,
603                                           ext2fs_block_bitmap_loc(fs, g));
604                 ext2fs_mark_block_bitmap2(fs->block_map,
605                                           ext2fs_inode_bitmap_loc(fs, g));
606                 for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
607                      i < fs->inode_blocks_per_group;
608                      i++, blk++)
609                         ext2fs_mark_block_bitmap2(fs->block_map, blk);
610         }
611 }
612
613 /* --------------------------------------------------------------------
614  *
615  * Resize processing, phase 1.
616  *
617  * In this phase we adjust the in-memory superblock information, and
618  * initialize any new parts of the inode table.  The new parts of the
619  * inode table are created in virgin disk space, so we can abort here
620  * without any side effects.
621  * --------------------------------------------------------------------
622  */
623
624 /*
625  * If the group descriptor's bitmap and inode table blocks are valid,
626  * release them in the new filesystem data structure, and mark them as
627  * reserved so the old inode table blocks don't get overwritten.
628  */
629 static errcode_t free_gdp_blocks(ext2_filsys fs,
630                                  ext2fs_block_bitmap reserve_blocks,
631                                  ext2_filsys old_fs,
632                                  dgrp_t group)
633 {
634         blk64_t         blk;
635         unsigned int    j;
636         dgrp_t          i;
637         ext2fs_block_bitmap bg_map = NULL;
638         errcode_t       retval = 0;
639         dgrp_t          count = old_fs->group_desc_count - fs->group_desc_count;
640
641         /* If bigalloc, don't free metadata living in the same cluster */
642         if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
643                 retval = ext2fs_allocate_block_bitmap(fs, "bgdata", &bg_map);
644                 if (retval)
645                         goto out;
646
647                 retval = mark_table_blocks(fs, bg_map);
648                 if (retval)
649                         goto out;
650         }
651
652         for (i = group; i < group + count; i++) {
653                 blk = ext2fs_block_bitmap_loc(old_fs, i);
654                 if (blk &&
655                     (blk < ext2fs_blocks_count(fs->super)) &&
656                     !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
657                         ext2fs_block_alloc_stats2(fs, blk, -1);
658                         ext2fs_mark_block_bitmap2(reserve_blocks, blk);
659                 }
660
661                 blk = ext2fs_inode_bitmap_loc(old_fs, i);
662                 if (blk &&
663                     (blk < ext2fs_blocks_count(fs->super)) &&
664                     !(bg_map && ext2fs_test_block_bitmap2(bg_map, blk))) {
665                         ext2fs_block_alloc_stats2(fs, blk, -1);
666                         ext2fs_mark_block_bitmap2(reserve_blocks, blk);
667                 }
668
669                 blk = ext2fs_inode_table_loc(old_fs, i);
670                 for (j = 0;
671                      j < fs->inode_blocks_per_group; j++, blk++) {
672                         if (blk >= ext2fs_blocks_count(fs->super) ||
673                             (bg_map && ext2fs_test_block_bitmap2(bg_map, blk)))
674                                 continue;
675                         ext2fs_block_alloc_stats2(fs, blk, -1);
676                         ext2fs_mark_block_bitmap2(reserve_blocks, blk);
677                 }
678         }
679
680 out:
681         if (bg_map)
682                 ext2fs_free_block_bitmap(bg_map);
683         return retval;
684 }
685
686 /*
687  * This routine is shared by the online and offline resize routines.
688  * All of the information which is adjusted in memory is done here.
689  */
690 errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
691                          ext2fs_block_bitmap reserve_blocks, blk64_t new_size)
692 {
693         errcode_t       retval;
694         blk64_t         overhead = 0;
695         blk64_t         rem;
696         blk64_t         blk, group_block;
697         blk64_t         real_end;
698         blk64_t         old_numblocks, numblocks, adjblocks;
699         unsigned long   i, j, old_desc_blocks;
700         unsigned int    meta_bg, meta_bg_size;
701         int             has_super, csum_flag, has_bg;
702         unsigned long long new_inodes;  /* u64 to check for overflow */
703         double          percent;
704
705         ext2fs_blocks_count_set(fs->super, new_size);
706
707 retry:
708         fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
709                                        fs->super->s_first_data_block,
710                                        EXT2_BLOCKS_PER_GROUP(fs->super));
711         if (fs->group_desc_count == 0)
712                 return EXT2_ET_TOOSMALL;
713         fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
714                                           EXT2_DESC_PER_BLOCK(fs->super));
715
716         /*
717          * Overhead is the number of bookkeeping blocks per group.  It
718          * includes the superblock backup, the group descriptor
719          * backups, the inode bitmap, the block bitmap, and the inode
720          * table.
721          */
722         overhead = (int) (2 + fs->inode_blocks_per_group);
723
724         has_bg = 0;
725         if (ext2fs_has_feature_sparse_super2(fs->super)) {
726                 /*
727                  * We have to do this manually since
728                  * super->s_backup_bgs hasn't been set up yet.
729                  */
730                 if (fs->group_desc_count == 2)
731                         has_bg = fs->super->s_backup_bgs[0] != 0;
732                 else
733                         has_bg = fs->super->s_backup_bgs[1] != 0;
734         } else
735                 has_bg = ext2fs_bg_has_super(fs, fs->group_desc_count - 1);
736         if (has_bg)
737                 overhead += 1 + fs->desc_blocks +
738                         fs->super->s_reserved_gdt_blocks;
739
740         /*
741          * See if the last group is big enough to support the
742          * necessary data structures.  If not, we need to get rid of
743          * it.
744          */
745         rem = (ext2fs_blocks_count(fs->super) - fs->super->s_first_data_block) %
746                 fs->super->s_blocks_per_group;
747         if ((fs->group_desc_count == 1) && rem && (rem < overhead))
748                 return EXT2_ET_TOOSMALL;
749         if ((fs->group_desc_count > 1) && rem && (rem < overhead+50)) {
750                 ext2fs_blocks_count_set(fs->super,
751                                         ext2fs_blocks_count(fs->super) - rem);
752                 goto retry;
753         }
754         /*
755          * Adjust the number of inodes
756          */
757         new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
758         if (new_inodes > ~0U) {
759                 fprintf(stderr, _("inodes (%llu) must be less than %u"),
760                                    new_inodes, ~0U);
761                 return EXT2_ET_TOO_MANY_INODES;
762         }
763         fs->super->s_inodes_count = fs->super->s_inodes_per_group *
764                 fs->group_desc_count;
765
766         /*
767          * Adjust the number of free blocks
768          */
769         blk = ext2fs_blocks_count(old_fs->super);
770         if (blk > ext2fs_blocks_count(fs->super))
771                 ext2fs_free_blocks_count_set(fs->super, 
772                         ext2fs_free_blocks_count(fs->super) -
773                         (blk - ext2fs_blocks_count(fs->super)));
774         else
775                 ext2fs_free_blocks_count_set(fs->super, 
776                         ext2fs_free_blocks_count(fs->super) +
777                         (ext2fs_blocks_count(fs->super) - blk));
778
779         /*
780          * Adjust the number of reserved blocks
781          */
782         percent = (ext2fs_r_blocks_count(old_fs->super) * 100.0) /
783                 ext2fs_blocks_count(old_fs->super);
784         ext2fs_r_blocks_count_set(fs->super,
785                                   (percent * ext2fs_blocks_count(fs->super) /
786                                    100.0));
787
788         /*
789          * Adjust the bitmaps for size
790          */
791         retval = ext2fs_resize_inode_bitmap2(fs->super->s_inodes_count,
792                                             fs->super->s_inodes_count,
793                                             fs->inode_map);
794         if (retval) goto errout;
795
796         real_end = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count) - 1 +
797                 fs->super->s_first_data_block;
798         retval = ext2fs_resize_block_bitmap2(new_size - 1,
799                                              real_end, fs->block_map);
800         if (retval) goto errout;
801
802         /*
803          * If we are growing the file system, also grow the size of
804          * the reserve_blocks bitmap
805          */
806         if (reserve_blocks && new_size > ext2fs_blocks_count(old_fs->super)) {
807                 retval = ext2fs_resize_block_bitmap2(new_size - 1,
808                                                      real_end, reserve_blocks);
809                 if (retval) goto errout;
810         }
811
812         /*
813          * Reallocate the group descriptors as necessary.
814          */
815         if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super) &&
816             old_fs->desc_blocks != fs->desc_blocks) {
817                 retval = ext2fs_resize_mem(old_fs->desc_blocks *
818                                            fs->blocksize,
819                                            fs->desc_blocks * fs->blocksize,
820                                            &fs->group_desc);
821                 if (retval)
822                         goto errout;
823                 if (fs->desc_blocks > old_fs->desc_blocks)
824                         memset((char *) fs->group_desc +
825                                (old_fs->desc_blocks * fs->blocksize), 0,
826                                (fs->desc_blocks - old_fs->desc_blocks) *
827                                fs->blocksize);
828         }
829
830         /*
831          * If the resize_inode feature is set, and we are changing the
832          * number of descriptor blocks, then adjust
833          * s_reserved_gdt_blocks if possible to avoid needing to move
834          * the inode table either now or in the future.
835          *
836          * Note: If we're converting to 64bit mode, we did this earlier.
837          */
838         if (EXT2_DESC_SIZE(old_fs->super) == EXT2_DESC_SIZE(fs->super))
839                 adjust_reserved_gdt_blocks(old_fs, fs);
840
841         if (ext2fs_has_feature_meta_bg(fs->super) &&
842             (fs->super->s_first_meta_bg > fs->desc_blocks)) {
843                 ext2fs_clear_feature_meta_bg(fs->super);
844                 fs->super->s_first_meta_bg = 0;
845         }
846
847         /*
848          * Update the location of the backup superblocks if the
849          * sparse_super2 feature is enabled.
850          */
851         if (ext2fs_has_feature_sparse_super2(fs->super)) {
852                 dgrp_t last_bg = fs->group_desc_count - 1;
853                 dgrp_t old_last_bg = old_fs->group_desc_count - 1;
854
855                 if (last_bg > old_last_bg) {
856                         if (old_fs->group_desc_count == 1)
857                                 fs->super->s_backup_bgs[0] = 1;
858                         if ((old_fs->group_desc_count < 3 &&
859                              fs->group_desc_count > 2) ||
860                             fs->super->s_backup_bgs[1])
861                                 fs->super->s_backup_bgs[1] = last_bg;
862                 } else if (last_bg < old_last_bg) {
863                         if (fs->super->s_backup_bgs[0] > last_bg)
864                                 fs->super->s_backup_bgs[0] = 0;
865                         if (fs->super->s_backup_bgs[1] > last_bg)
866                                 fs->super->s_backup_bgs[1] = 0;
867                         if (last_bg > 1 &&
868                             old_fs->super->s_backup_bgs[1] == old_last_bg)
869                                 fs->super->s_backup_bgs[1] = last_bg;
870                 }
871         }
872
873         /*
874          * If we are shrinking the number of block groups, we're done
875          * and can exit now.
876          */
877         if (old_fs->group_desc_count > fs->group_desc_count) {
878                 /*
879                  * Check the block groups that we are chopping off
880                  * and free any blocks associated with their metadata
881                  */
882                 retval = free_gdp_blocks(fs, reserve_blocks, old_fs,
883                                          fs->group_desc_count);
884                 goto errout;
885         }
886
887         /*
888          * Fix the count of the last (old) block group
889          */
890         old_numblocks = (ext2fs_blocks_count(old_fs->super) -
891                          old_fs->super->s_first_data_block) %
892                                  old_fs->super->s_blocks_per_group;
893         if (!old_numblocks)
894                 old_numblocks = old_fs->super->s_blocks_per_group;
895         if (old_fs->group_desc_count == fs->group_desc_count) {
896                 numblocks = (ext2fs_blocks_count(fs->super) -
897                              fs->super->s_first_data_block) %
898                         fs->super->s_blocks_per_group;
899                 if (!numblocks)
900                         numblocks = fs->super->s_blocks_per_group;
901         } else
902                 numblocks = fs->super->s_blocks_per_group;
903         i = old_fs->group_desc_count - 1;
904         ext2fs_bg_free_blocks_count_set(fs, i, ext2fs_bg_free_blocks_count(fs, i) + (numblocks - old_numblocks));
905         ext2fs_group_desc_csum_set(fs, i);
906
907         /*
908          * If the number of block groups is staying the same, we're
909          * done and can exit now.  (If the number block groups is
910          * shrinking, we had exited earlier.)
911          */
912         if (old_fs->group_desc_count >= fs->group_desc_count) {
913                 retval = 0;
914                 goto errout;
915         }
916
917         /*
918          * Initialize the new block group descriptors
919          */
920         group_block = ext2fs_group_first_block2(fs,
921                                                 old_fs->group_desc_count);
922         csum_flag = ext2fs_has_group_desc_csum(fs);
923         if (!getenv("RESIZE2FS_FORCE_ITABLE_INIT") &&
924             access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
925                 lazy_itable_init = 1;
926         if (ext2fs_has_feature_meta_bg(fs->super))
927                 old_desc_blocks = fs->super->s_first_meta_bg;
928         else
929                 old_desc_blocks = fs->desc_blocks +
930                         fs->super->s_reserved_gdt_blocks;
931
932         /*
933          * If we changed the number of block_group descriptor blocks,
934          * we need to make sure they are all marked as reserved in the
935          * file systems's block allocation map.
936          */
937         for (i = 0; i < old_fs->group_desc_count; i++)
938                 ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
939
940         for (i = old_fs->group_desc_count;
941              i < fs->group_desc_count; i++) {
942                 memset(ext2fs_group_desc(fs, fs->group_desc, i), 0,
943                        sizeof(struct ext2_group_desc));
944                 adjblocks = 0;
945
946                 ext2fs_bg_flags_zap(fs, i);
947                 if (csum_flag) {
948                         ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
949                         if (!lazy_itable_init)
950                                 ext2fs_bg_flags_set(fs, i,
951                                                     EXT2_BG_INODE_ZEROED);
952                         ext2fs_bg_itable_unused_set(fs, i,
953                                         fs->super->s_inodes_per_group);
954                 }
955
956                 numblocks = ext2fs_group_blocks_count(fs, i);
957                 if ((i < fs->group_desc_count - 1) && csum_flag)
958                         ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
959
960                 has_super = ext2fs_bg_has_super(fs, i);
961                 if (has_super) {
962                         ext2fs_block_alloc_stats2(fs, group_block, +1);
963                         adjblocks++;
964                 }
965                 meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
966                 meta_bg = i / meta_bg_size;
967                 if (!ext2fs_has_feature_meta_bg(fs->super) ||
968                     (meta_bg < fs->super->s_first_meta_bg)) {
969                         if (has_super) {
970                                 for (j=0; j < old_desc_blocks; j++)
971                                         ext2fs_block_alloc_stats2(fs,
972                                                  group_block + 1 + j, +1);
973                                 adjblocks += old_desc_blocks;
974                         }
975                 } else {
976                         if (has_super)
977                                 has_super = 1;
978                         if (((i % meta_bg_size) == 0) ||
979                             ((i % meta_bg_size) == 1) ||
980                             ((i % meta_bg_size) == (meta_bg_size-1)))
981                                 ext2fs_block_alloc_stats2(fs,
982                                                  group_block + has_super, +1);
983                 }
984
985                 adjblocks += 2 + fs->inode_blocks_per_group;
986
987                 numblocks -= adjblocks;
988                 ext2fs_free_blocks_count_set(fs->super,
989                              ext2fs_free_blocks_count(fs->super) - adjblocks);
990                 fs->super->s_free_inodes_count +=
991                         fs->super->s_inodes_per_group;
992                 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
993                 ext2fs_bg_free_inodes_count_set(fs, i,
994                                                 fs->super->s_inodes_per_group);
995                 ext2fs_bg_used_dirs_count_set(fs, i, 0);
996                 ext2fs_group_desc_csum_set(fs, i);
997
998                 retval = ext2fs_allocate_group_table(fs, i, 0);
999                 if (retval) goto errout;
1000
1001                 group_block += fs->super->s_blocks_per_group;
1002         }
1003         retval = 0;
1004
1005         /*
1006          * Mark all of the metadata blocks as reserved so they won't
1007          * get allocated by the call to ext2fs_allocate_group_table()
1008          * in blocks_to_move(), where we allocate new blocks to
1009          * replace those allocation bitmap and inode table blocks
1010          * which have to get relocated to make space for an increased
1011          * number of the block group descriptors.
1012          */
1013         if (reserve_blocks)
1014                 mark_table_blocks(fs, reserve_blocks);
1015
1016 errout:
1017         return (retval);
1018 }
1019
1020 /*
1021  * This routine adjusts the superblock and other data structures, both
1022  * in disk as well as in memory...
1023  */
1024 static errcode_t adjust_superblock(ext2_resize_t rfs, blk64_t new_size)
1025 {
1026         ext2_filsys     fs = rfs->new_fs;
1027         int             adj = 0;
1028         errcode_t       retval;
1029         blk64_t         group_block;
1030         unsigned long   i;
1031         unsigned long   max_group;
1032
1033         ext2fs_mark_super_dirty(fs);
1034         ext2fs_mark_bb_dirty(fs);
1035         ext2fs_mark_ib_dirty(fs);
1036
1037         retval = ext2fs_allocate_block_bitmap(fs, _("reserved blocks"),
1038                                               &rfs->reserve_blocks);
1039         if (retval)
1040                 return retval;
1041
1042         retval = adjust_fs_info(fs, rfs->old_fs, rfs->reserve_blocks, new_size);
1043         if (retval)
1044                 goto errout;
1045
1046         /*
1047          * Check to make sure there are enough inodes
1048          */
1049         if ((rfs->old_fs->super->s_inodes_count -
1050              rfs->old_fs->super->s_free_inodes_count) >
1051             rfs->new_fs->super->s_inodes_count) {
1052                 retval = ENOSPC;
1053                 goto errout;
1054         }
1055
1056         /*
1057          * If we are shrinking the number block groups, we're done and
1058          * can exit now.
1059          */
1060         if (rfs->old_fs->group_desc_count > fs->group_desc_count) {
1061                 retval = 0;
1062                 goto errout;
1063         }
1064
1065         /*
1066          * If the number of block groups is staying the same, we're
1067          * done and can exit now.  (If the number block groups is
1068          * shrinking, we had exited earlier.)
1069          */
1070         if (rfs->old_fs->group_desc_count >= fs->group_desc_count) {
1071                 retval = 0;
1072                 goto errout;
1073         }
1074
1075         /*
1076          * If we are using uninit_bg (aka GDT_CSUM) and the kernel
1077          * supports lazy inode initialization, we can skip
1078          * initializing the inode table.
1079          */
1080         if (lazy_itable_init && ext2fs_has_group_desc_csum(fs)) {
1081                 retval = 0;
1082                 goto errout;
1083         }
1084
1085         /*
1086          * Initialize the inode table
1087          */
1088         retval = ext2fs_get_array(fs->blocksize, fs->inode_blocks_per_group,
1089                                 &rfs->itable_buf);
1090         if (retval)
1091                 goto errout;
1092
1093         memset(rfs->itable_buf, 0, fs->blocksize * fs->inode_blocks_per_group);
1094         group_block = ext2fs_group_first_block2(fs,
1095                                                 rfs->old_fs->group_desc_count);
1096         adj = rfs->old_fs->group_desc_count;
1097         max_group = fs->group_desc_count - adj;
1098         if (rfs->progress) {
1099                 retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
1100                                        0, max_group);
1101                 if (retval)
1102                         goto errout;
1103         }
1104         for (i = rfs->old_fs->group_desc_count;
1105              i < fs->group_desc_count; i++) {
1106                 /*
1107                  * Write out the new inode table
1108                  */
1109                 retval = ext2fs_zero_blocks2(fs, ext2fs_inode_table_loc(fs, i),
1110                                              fs->inode_blocks_per_group, NULL,
1111                                              NULL);
1112                 if (retval)
1113                         goto errout;
1114
1115                 io_channel_flush(fs->io);
1116                 if (rfs->progress) {
1117                         retval = rfs->progress(rfs, E2_RSZ_EXTEND_ITABLE_PASS,
1118                                                i - adj + 1, max_group);
1119                         if (retval)
1120                                 goto errout;
1121                 }
1122                 group_block += fs->super->s_blocks_per_group;
1123         }
1124         io_channel_flush(fs->io);
1125         retval = 0;
1126
1127 errout:
1128         return retval;
1129 }
1130
1131 /* --------------------------------------------------------------------
1132  *
1133  * Resize processing, phase 2.
1134  *
1135  * In this phase we adjust determine which blocks need to be moved, in
1136  * blocks_to_move().  We then copy the blocks to their ultimate new
1137  * destinations using block_mover().  Since we are copying blocks to
1138  * their new locations, again during this pass we can abort without
1139  * any problems.
1140  * --------------------------------------------------------------------
1141  */
1142
1143 /*
1144  * This helper function creates a block bitmap with all of the
1145  * filesystem meta-data blocks.
1146  */
1147 static errcode_t mark_table_blocks(ext2_filsys fs,
1148                                    ext2fs_block_bitmap bmap)
1149 {
1150         dgrp_t                  i;
1151         blk64_t                 blk;
1152
1153         for (i = 0; i < fs->group_desc_count; i++) {
1154                 ext2fs_reserve_super_and_bgd(fs, i, bmap);
1155
1156                 /*
1157                  * Mark the blocks used for the inode table
1158                  */
1159                 blk = ext2fs_inode_table_loc(fs, i);
1160                 if (blk)
1161                         ext2fs_mark_block_bitmap_range2(bmap, blk,
1162                                                 fs->inode_blocks_per_group);
1163
1164                 /*
1165                  * Mark block used for the block bitmap
1166                  */
1167                 blk = ext2fs_block_bitmap_loc(fs, i);
1168                 if (blk)
1169                         ext2fs_mark_block_bitmap2(bmap, blk);
1170
1171                 /*
1172                  * Mark block used for the inode bitmap
1173                  */
1174                 blk = ext2fs_inode_bitmap_loc(fs, i);
1175                 if (blk)
1176                         ext2fs_mark_block_bitmap2(bmap, blk);
1177         }
1178         return 0;
1179 }
1180
1181 /*
1182  * This function checks to see if a particular block (either a
1183  * superblock or a block group descriptor) overlaps with an inode or
1184  * block bitmap block, or with the inode table.
1185  */
1186 static void mark_fs_metablock(ext2_resize_t rfs,
1187                               ext2fs_block_bitmap meta_bmap,
1188                               int group, blk64_t blk)
1189 {
1190         ext2_filsys fs = rfs->new_fs;
1191
1192         ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1193         ext2fs_block_alloc_stats2(fs, blk, +1);
1194
1195         /*
1196          * Check to see if we overlap with the inode or block bitmap,
1197          * or the inode tables.  If not, and the block is in use, then
1198          * mark it as a block to be moved.
1199          */
1200         if (is_block_bm(fs, group, blk)) {
1201                 ext2fs_block_bitmap_loc_set(fs, group, 0);
1202                 rfs->needed_blocks++;
1203                 return;
1204         }
1205         if (is_inode_bm(fs, group, blk)) {
1206                 ext2fs_inode_bitmap_loc_set(fs, group, 0);
1207                 rfs->needed_blocks++;
1208                 return;
1209         }
1210         if (is_inode_tb(fs, group, blk)) {
1211                 ext2fs_inode_table_loc_set(fs, group, 0);
1212                 rfs->needed_blocks++;
1213                 return;
1214         }
1215         if (ext2fs_has_feature_flex_bg(fs->super)) {
1216                 dgrp_t i;
1217
1218                 for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
1219                         if (is_block_bm(fs, i, blk)) {
1220                                 ext2fs_block_bitmap_loc_set(fs, i, 0);
1221                                 rfs->needed_blocks++;
1222                                 return;
1223                         }
1224                         if (is_inode_bm(fs, i, blk)) {
1225                                 ext2fs_inode_bitmap_loc_set(fs, i, 0);
1226                                 rfs->needed_blocks++;
1227                                 return;
1228                         }
1229                         if (is_inode_tb(fs, i, blk)) {
1230                                 ext2fs_inode_table_loc_set(fs, i, 0);
1231                                 rfs->needed_blocks++;
1232                                 return;
1233                         }
1234                 }
1235         }
1236
1237         if (ext2fs_has_group_desc_csum(fs) &&
1238             (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))) {
1239                 /*
1240                  * If the block bitmap is uninitialized, which means
1241                  * nothing other than standard metadata in use.
1242                  */
1243                 return;
1244         } else if (ext2fs_test_block_bitmap2(rfs->old_fs->block_map, blk) &&
1245                    !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
1246                 ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
1247                 rfs->needed_blocks++;
1248         }
1249 }
1250
1251
1252 /*
1253  * This routine marks and unmarks reserved blocks in the new block
1254  * bitmap.  It also determines which blocks need to be moved and
1255  * places this information into the move_blocks bitmap.
1256  */
1257 static errcode_t blocks_to_move(ext2_resize_t rfs)
1258 {
1259         unsigned int    j;
1260         int             has_super;
1261         dgrp_t          i, max_groups, g;
1262         blk64_t         blk, group_blk;
1263         blk64_t         old_blocks, new_blocks, group_end, cluster_freed;
1264         blk64_t         new_size;
1265         unsigned int    meta_bg, meta_bg_size;
1266         errcode_t       retval;
1267         ext2_filsys     fs, old_fs;
1268         ext2fs_block_bitmap     meta_bmap, new_meta_bmap = NULL;
1269         int             flex_bg;
1270
1271         fs = rfs->new_fs;
1272         old_fs = rfs->old_fs;
1273         if (ext2fs_blocks_count(old_fs->super) > ext2fs_blocks_count(fs->super))
1274                 fs = rfs->old_fs;
1275
1276         retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1277                                               &rfs->move_blocks);
1278         if (retval)
1279                 return retval;
1280
1281         retval = ext2fs_allocate_block_bitmap(fs, _("meta-data blocks"),
1282                                               &meta_bmap);
1283         if (retval)
1284                 return retval;
1285
1286         retval = mark_table_blocks(old_fs, meta_bmap);
1287         if (retval)
1288                 return retval;
1289
1290         fs = rfs->new_fs;
1291
1292         /*
1293          * If we're shrinking the filesystem, we need to move any
1294          * group's metadata blocks (either allocation bitmaps or the
1295          * inode table) which are beyond the end of the new
1296          * filesystem.
1297          */
1298         new_size = ext2fs_blocks_count(fs->super);
1299         if (new_size < ext2fs_blocks_count(old_fs->super)) {
1300                 for (g = 0; g < fs->group_desc_count; g++) {
1301                         int realloc = 0;
1302                         /*
1303                          * ext2fs_allocate_group_table will re-allocate any
1304                          * metadata blocks whose location is set to zero.
1305                          */
1306                         if (ext2fs_block_bitmap_loc(fs, g) >= new_size) {
1307                                 ext2fs_block_bitmap_loc_set(fs, g, 0);
1308                                 realloc = 1;
1309                         }
1310                         if (ext2fs_inode_bitmap_loc(fs, g) >= new_size) {
1311                                 ext2fs_inode_bitmap_loc_set(fs, g, 0);
1312                                 realloc = 1;
1313                         }
1314                         if ((ext2fs_inode_table_loc(fs, g) +
1315                              fs->inode_blocks_per_group) > new_size) {
1316                                 ext2fs_inode_table_loc_set(fs, g, 0);
1317                                 realloc = 1;
1318                         }
1319
1320                         if (realloc) {
1321                                 retval = ext2fs_allocate_group_table(fs, g, 0);
1322                                 if (retval)
1323                                         return retval;
1324                         }
1325                 }
1326         }
1327
1328         /*
1329          * If we're shrinking the filesystem, we need to move all of
1330          * the blocks that don't fit any more
1331          */
1332         for (blk = ext2fs_blocks_count(fs->super);
1333              blk < ext2fs_blocks_count(old_fs->super); blk++) {
1334                 g = ext2fs_group_of_blk2(fs, blk);
1335                 if (ext2fs_has_group_desc_csum(fs) &&
1336                     ext2fs_bg_flags_test(old_fs, g, EXT2_BG_BLOCK_UNINIT)) {
1337                         /*
1338                          * The block bitmap is uninitialized, so skip
1339                          * to the next block group.
1340                          */
1341                         blk = ext2fs_group_first_block2(fs, g+1) - 1;
1342                         continue;
1343                 }
1344                 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1345                     !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
1346                         ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
1347                         rfs->needed_blocks++;
1348                 }
1349                 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1350         }
1351
1352         if (ext2fs_has_feature_meta_bg(old_fs->super))
1353                 old_blocks = old_fs->super->s_first_meta_bg;
1354         else
1355                 old_blocks = old_fs->desc_blocks +
1356                         old_fs->super->s_reserved_gdt_blocks;
1357         if (ext2fs_has_feature_meta_bg(fs->super))
1358                 new_blocks = fs->super->s_first_meta_bg;
1359         else
1360                 new_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
1361
1362         retval = reserve_sparse_super2_last_group(rfs, meta_bmap);
1363         if (retval)
1364                 goto errout;
1365
1366         if (EXT2_DESC_SIZE(rfs->old_fs->super) ==
1367             EXT2_DESC_SIZE(rfs->new_fs->super) &&
1368             old_blocks == new_blocks) {
1369                 retval = 0;
1370                 goto errout;
1371         }
1372
1373         max_groups = fs->group_desc_count;
1374         if (max_groups > old_fs->group_desc_count)
1375                 max_groups = old_fs->group_desc_count;
1376         group_blk = old_fs->super->s_first_data_block;
1377         /*
1378          * If we're reducing the number of descriptor blocks, this
1379          * makes life easy.  :-)   We just have to mark some extra
1380          * blocks as free.
1381          */
1382         if (old_blocks > new_blocks) {
1383                 if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
1384                         retval = ext2fs_allocate_block_bitmap(fs,
1385                                                         _("new meta blocks"),
1386                                                         &new_meta_bmap);
1387                         if (retval)
1388                                 goto errout;
1389
1390                         retval = mark_table_blocks(fs, new_meta_bmap);
1391                         if (retval)
1392                                 goto errout;
1393                 }
1394
1395                 for (i = 0; i < max_groups; i++) {
1396                         if (!ext2fs_bg_has_super(old_fs, i)) {
1397                                 group_blk += fs->super->s_blocks_per_group;
1398                                 continue;
1399                         }
1400                         group_end = group_blk + 1 + old_blocks;
1401                         for (blk = group_blk + 1 + new_blocks;
1402                              blk < group_end;) {
1403                                 if (new_meta_bmap == NULL ||
1404                                     !ext2fs_test_block_bitmap2(new_meta_bmap,
1405                                                                blk)) {
1406                                         cluster_freed =
1407                                                 EXT2FS_CLUSTER_RATIO(fs) -
1408                                                 (blk &
1409                                                  EXT2FS_CLUSTER_MASK(fs));
1410                                         if (cluster_freed > group_end - blk)
1411                                                 cluster_freed = group_end - blk;
1412                                         ext2fs_block_alloc_stats2(fs, blk, -1);
1413                                         blk += EXT2FS_CLUSTER_RATIO(fs);
1414                                         rfs->needed_blocks -= cluster_freed;
1415                                         continue;
1416                                 }
1417                                 rfs->needed_blocks--;
1418                                 blk++;
1419                         }
1420                         group_blk += fs->super->s_blocks_per_group;
1421                 }
1422                 retval = 0;
1423                 goto errout;
1424         }
1425         /*
1426          * If we're increasing the number of descriptor blocks, life
1427          * gets interesting....
1428          */
1429         meta_bg_size = EXT2_DESC_PER_BLOCK(fs->super);
1430         flex_bg = ext2fs_has_feature_flex_bg(fs->super);
1431         /* first reserve all of the existing fs meta blocks */
1432         for (i = 0; i < max_groups; i++) {
1433                 has_super = ext2fs_bg_has_super(fs, i);
1434                 if (has_super)
1435                         mark_fs_metablock(rfs, meta_bmap, i, group_blk);
1436
1437                 meta_bg = i / meta_bg_size;
1438                 if (!ext2fs_has_feature_meta_bg(fs->super) ||
1439                     (meta_bg < fs->super->s_first_meta_bg)) {
1440                         if (has_super) {
1441                                 for (blk = group_blk+1;
1442                                      blk < group_blk + 1 + new_blocks; blk++)
1443                                         mark_fs_metablock(rfs, meta_bmap,
1444                                                           i, blk);
1445                         }
1446                 } else {
1447                         if (has_super)
1448                                 has_super = 1;
1449                         if (((i % meta_bg_size) == 0) ||
1450                             ((i % meta_bg_size) == 1) ||
1451                             ((i % meta_bg_size) == (meta_bg_size-1)))
1452                                 mark_fs_metablock(rfs, meta_bmap, i,
1453                                                   group_blk + has_super);
1454                 }
1455
1456                 /*
1457                  * Reserve the existing meta blocks that we know
1458                  * aren't to be moved.
1459                  *
1460                  * For flex_bg file systems, in order to avoid
1461                  * overwriting fs metadata (especially inode table
1462                  * blocks) belonging to a different block group when
1463                  * we are relocating the inode tables, we need to
1464                  * reserve all existing fs metadata blocks.
1465                  */
1466                 if (ext2fs_block_bitmap_loc(fs, i))
1467                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1468                                  ext2fs_block_bitmap_loc(fs, i));
1469                 else if (flex_bg && i < old_fs->group_desc_count)
1470                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1471                                  ext2fs_block_bitmap_loc(old_fs, i));
1472
1473                 if (ext2fs_inode_bitmap_loc(fs, i))
1474                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1475                                  ext2fs_inode_bitmap_loc(fs, i));
1476                 else if (flex_bg && i < old_fs->group_desc_count)
1477                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks,
1478                                  ext2fs_inode_bitmap_loc(old_fs, i));
1479
1480                 if (ext2fs_inode_table_loc(fs, i))
1481                         ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1482                                         ext2fs_inode_table_loc(fs, i),
1483                                         fs->inode_blocks_per_group);
1484                 else if (flex_bg && i < old_fs->group_desc_count)
1485                         ext2fs_mark_block_bitmap_range2(rfs->reserve_blocks,
1486                                         ext2fs_inode_table_loc(old_fs, i),
1487                                         old_fs->inode_blocks_per_group);
1488
1489                 group_blk += rfs->new_fs->super->s_blocks_per_group;
1490         }
1491
1492         /* Allocate the missing data structures */
1493         for (i = 0; i < max_groups; i++) {
1494                 if (ext2fs_inode_table_loc(fs, i) &&
1495                     ext2fs_inode_bitmap_loc(fs, i) &&
1496                     ext2fs_block_bitmap_loc(fs, i))
1497                         continue;
1498
1499                 retval = ext2fs_allocate_group_table(fs, i,
1500                                                      rfs->reserve_blocks);
1501                 if (retval)
1502                         goto errout;
1503
1504                 /*
1505                  * For those structures that have changed, we need to
1506                  * do bookkeepping.
1507                  */
1508                 if (ext2fs_block_bitmap_loc(old_fs, i) !=
1509                     (blk = ext2fs_block_bitmap_loc(fs, i))) {
1510                         ext2fs_block_alloc_stats2(fs, blk, +1);
1511                         if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1512                             !ext2fs_test_block_bitmap2(meta_bmap, blk))
1513                                 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1514                                                          blk);
1515                 }
1516                 if (ext2fs_inode_bitmap_loc(old_fs, i) !=
1517                     (blk = ext2fs_inode_bitmap_loc(fs, i))) {
1518                         ext2fs_block_alloc_stats2(fs, blk, +1);
1519                         if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1520                             !ext2fs_test_block_bitmap2(meta_bmap, blk))
1521                                 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1522                                                          blk);
1523                 }
1524
1525                 /*
1526                  * The inode table, if we need to relocate it, is
1527                  * handled specially.  We have to reserve the blocks
1528                  * for both the old and the new inode table, since we
1529                  * can't have the inode table be destroyed during the
1530                  * block relocation phase.
1531                  */
1532                 if (ext2fs_inode_table_loc(fs, i) == ext2fs_inode_table_loc(old_fs, i))
1533                         continue;       /* inode table not moved */
1534
1535                 rfs->needed_blocks += fs->inode_blocks_per_group;
1536
1537                 /*
1538                  * Mark the new inode table as in use in the new block
1539                  * allocation bitmap, and move any blocks that might
1540                  * be necessary.
1541                  */
1542                 for (blk = ext2fs_inode_table_loc(fs, i), j=0;
1543                      j < fs->inode_blocks_per_group ; j++, blk++) {
1544                         ext2fs_block_alloc_stats2(fs, blk, +1);
1545                         if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
1546                             !ext2fs_test_block_bitmap2(meta_bmap, blk))
1547                                 ext2fs_mark_block_bitmap2(rfs->move_blocks,
1548                                                          blk);
1549                 }
1550
1551                 /*
1552                  * Make sure the old inode table is reserved in the
1553                  * block reservation bitmap.
1554                  */
1555                 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
1556                      j < fs->inode_blocks_per_group ; j++, blk++)
1557                         ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
1558         }
1559         retval = 0;
1560
1561 errout:
1562         if (new_meta_bmap)
1563                 ext2fs_free_block_bitmap(new_meta_bmap);
1564         if (meta_bmap)
1565                 ext2fs_free_block_bitmap(meta_bmap);
1566
1567         return retval;
1568 }
1569
1570 /*
1571  * This helper function tries to allocate a new block.  We try to
1572  * avoid hitting the original group descriptor blocks at least at
1573  * first, since we want to make it possible to recover from a badly
1574  * aborted resize operation as much as possible.
1575  *
1576  * In the future, I may further modify this routine to balance out
1577  * where we get the new blocks across the various block groups.
1578  * Ideally we would allocate blocks that corresponded with the block
1579  * group of the containing inode, and keep contiguous blocks
1580  * together.  However, this very difficult to do efficiently, since we
1581  * don't have the necessary information up front.
1582  */
1583
1584 #define AVOID_OLD       1
1585 #define DESPERATION     2
1586
1587 static void init_block_alloc(ext2_resize_t rfs)
1588 {
1589         rfs->alloc_state = AVOID_OLD;
1590         rfs->new_blk = rfs->new_fs->super->s_first_data_block;
1591 #if 0
1592         /* HACK for testing */
1593         if (ext2fs_blocks_count(rfs->new_fs->super) >
1594             ext2fs_blocks_count(rfs->old_fs->super))
1595                 rfs->new_blk = ext2fs_blocks_count(rfs->old_fs->super);
1596 #endif
1597 }
1598
1599 static blk64_t get_new_block(ext2_resize_t rfs)
1600 {
1601         ext2_filsys     fs = rfs->new_fs;
1602
1603         while (1) {
1604                 if (rfs->new_blk >= ext2fs_blocks_count(fs->super)) {
1605                         if (rfs->alloc_state == DESPERATION)
1606                                 return 0;
1607
1608 #ifdef RESIZE2FS_DEBUG
1609                         if (rfs->flags & RESIZE_DEBUG_BMOVE)
1610                                 printf("Going into desperation mode "
1611                                        "for block allocations\n");
1612 #endif
1613                         rfs->alloc_state = DESPERATION;
1614                         rfs->new_blk = fs->super->s_first_data_block;
1615                         continue;
1616                 }
1617                 if (ext2fs_test_block_bitmap2(fs->block_map, rfs->new_blk) ||
1618                     ext2fs_test_block_bitmap2(rfs->reserve_blocks,
1619                                              rfs->new_blk) ||
1620                     ((rfs->alloc_state == AVOID_OLD) &&
1621                      (rfs->new_blk < ext2fs_blocks_count(rfs->old_fs->super)) &&
1622                      ext2fs_test_block_bitmap2(rfs->old_fs->block_map,
1623                                               rfs->new_blk))) {
1624                         rfs->new_blk++;
1625                         continue;
1626                 }
1627                 return rfs->new_blk;
1628         }
1629 }
1630
1631 static errcode_t resize2fs_get_alloc_block(ext2_filsys fs,
1632                                            blk64_t goal EXT2FS_ATTR((unused)),
1633                                            blk64_t *ret)
1634 {
1635         ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
1636         blk64_t blk;
1637         int group;
1638
1639         blk = get_new_block(rfs);
1640         if (!blk)
1641                 return ENOSPC;
1642
1643 #ifdef RESIZE2FS_DEBUG
1644         if (rfs->flags & 0xF)
1645                 printf("get_alloc_block allocating %llu\n", blk);
1646 #endif
1647
1648         ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
1649         ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
1650
1651         group = ext2fs_group_of_blk2(rfs->old_fs, blk);
1652         ext2fs_clear_block_uninit(rfs->old_fs, group);
1653         group = ext2fs_group_of_blk2(rfs->new_fs, blk);
1654         ext2fs_clear_block_uninit(rfs->new_fs, group);
1655
1656         *ret = (blk64_t) blk;
1657         return 0;
1658 }
1659
1660 static errcode_t block_mover(ext2_resize_t rfs)
1661 {
1662         blk64_t                 blk, old_blk, new_blk;
1663         ext2_filsys             fs = rfs->new_fs;
1664         ext2_filsys             old_fs = rfs->old_fs;
1665         errcode_t               retval;
1666         __u64                   c, size;
1667         int                     to_move, moved;
1668         ext2_badblocks_list     badblock_list = 0;
1669         int                     bb_modified = 0;
1670
1671         fs->get_alloc_block = resize2fs_get_alloc_block;
1672         old_fs->get_alloc_block = resize2fs_get_alloc_block;
1673
1674         retval = ext2fs_read_bb_inode(old_fs, &badblock_list);
1675         if (retval)
1676                 return retval;
1677
1678         new_blk = fs->super->s_first_data_block;
1679         if (!rfs->itable_buf) {
1680                 retval = ext2fs_get_array(fs->blocksize,
1681                                         fs->inode_blocks_per_group,
1682                                         &rfs->itable_buf);
1683                 if (retval)
1684                         return retval;
1685         }
1686         retval = ext2fs_create_extent_table(&rfs->bmap, 0);
1687         if (retval)
1688                 return retval;
1689
1690         /*
1691          * The first step is to figure out where all of the blocks
1692          * will go.
1693          */
1694         to_move = moved = 0;
1695         init_block_alloc(rfs);
1696         for (blk = B2C(old_fs->super->s_first_data_block);
1697              blk < ext2fs_blocks_count(old_fs->super);
1698              blk += EXT2FS_CLUSTER_RATIO(fs)) {
1699                 if (!ext2fs_test_block_bitmap2(old_fs->block_map, blk))
1700                         continue;
1701                 if (!ext2fs_test_block_bitmap2(rfs->move_blocks, blk))
1702                         continue;
1703                 if (ext2fs_badblocks_list_test(badblock_list, blk)) {
1704                         ext2fs_badblocks_list_del(badblock_list, blk);
1705                         bb_modified++;
1706                         continue;
1707                 }
1708
1709                 new_blk = get_new_block(rfs);
1710                 if (!new_blk) {
1711                         retval = ENOSPC;
1712                         goto errout;
1713                 }
1714                 ext2fs_block_alloc_stats2(fs, new_blk, +1);
1715                 ext2fs_add_extent_entry(rfs->bmap, B2C(blk), B2C(new_blk));
1716                 to_move++;
1717         }
1718
1719         if (to_move == 0) {
1720                 if (rfs->bmap) {
1721                         ext2fs_free_extent_table(rfs->bmap);
1722                         rfs->bmap = 0;
1723                 }
1724                 retval = 0;
1725                 goto errout;
1726         }
1727
1728         /*
1729          * Step two is to actually move the blocks
1730          */
1731         retval =  ext2fs_iterate_extent(rfs->bmap, 0, 0, 0);
1732         if (retval) goto errout;
1733
1734         if (rfs->progress) {
1735                 retval = (rfs->progress)(rfs, E2_RSZ_BLOCK_RELOC_PASS,
1736                                          0, to_move);
1737                 if (retval)
1738                         goto errout;
1739         }
1740         while (1) {
1741                 retval = ext2fs_iterate_extent(rfs->bmap, &old_blk, &new_blk, &size);
1742                 if (retval) goto errout;
1743                 if (!size)
1744                         break;
1745                 old_blk = C2B(old_blk);
1746                 new_blk = C2B(new_blk);
1747                 size = C2B(size);
1748 #ifdef RESIZE2FS_DEBUG
1749                 if (rfs->flags & RESIZE_DEBUG_BMOVE)
1750                         printf("Moving %llu blocks %llu->%llu\n",
1751                                size, old_blk, new_blk);
1752 #endif
1753                 do {
1754                         c = size;
1755                         if (c > fs->inode_blocks_per_group)
1756                                 c = fs->inode_blocks_per_group;
1757                         retval = io_channel_read_blk64(fs->io, old_blk, c,
1758                                                        rfs->itable_buf);
1759                         if (retval) goto errout;
1760                         retval = io_channel_write_blk64(fs->io, new_blk, c,
1761                                                         rfs->itable_buf);
1762                         if (retval) goto errout;
1763                         size -= c;
1764                         new_blk += c;
1765                         old_blk += c;
1766                         moved += c;
1767                         if (rfs->progress) {
1768                                 io_channel_flush(fs->io);
1769                                 retval = (rfs->progress)(rfs,
1770                                                 E2_RSZ_BLOCK_RELOC_PASS,
1771                                                 moved, to_move);
1772                                 if (retval)
1773                                         goto errout;
1774                         }
1775                 } while (size > 0);
1776                 io_channel_flush(fs->io);
1777         }
1778
1779 errout:
1780         if (badblock_list) {
1781                 if (!retval && bb_modified)
1782                         retval = ext2fs_update_bb_inode(old_fs,
1783                                                         badblock_list);
1784                 ext2fs_badblocks_list_free(badblock_list);
1785         }
1786         return retval;
1787 }
1788
1789
1790 /* --------------------------------------------------------------------
1791  *
1792  * Resize processing, phase 3
1793  *
1794  * --------------------------------------------------------------------
1795  */
1796
1797
1798 /*
1799  * The extent translation table is stored in clusters so we need to
1800  * take special care when mapping a source block number to its
1801  * destination block number.
1802  */
1803 static __u64 extent_translate(ext2_filsys fs, ext2_extent extent, __u64 old_loc)
1804 {
1805         __u64 new_block = C2B(ext2fs_extent_translate(extent, B2C(old_loc)));
1806
1807         if (new_block != 0)
1808                 new_block += old_loc & (EXT2FS_CLUSTER_RATIO(fs) - 1);
1809         return new_block;
1810 }
1811
1812 struct process_block_struct {
1813         ext2_resize_t           rfs;
1814         ext2_ino_t              ino;
1815         ext2_ino_t              old_ino;
1816         struct ext2_inode *     inode;
1817         errcode_t               error;
1818         int                     is_dir;
1819         int                     changed;
1820         int                     has_extents;
1821 };
1822
1823 static int process_block(ext2_filsys fs, blk64_t        *block_nr,
1824                          e2_blkcnt_t blockcnt,
1825                          blk64_t ref_block EXT2FS_ATTR((unused)),
1826                          int ref_offset EXT2FS_ATTR((unused)), void *priv_data)
1827 {
1828         struct process_block_struct *pb;
1829         errcode_t       retval;
1830         blk64_t         block, new_block;
1831         int             ret = 0;
1832
1833         pb = (struct process_block_struct *) priv_data;
1834         block = *block_nr;
1835         if (pb->rfs->bmap) {
1836                 new_block = extent_translate(fs, pb->rfs->bmap, block);
1837                 if (new_block) {
1838                         *block_nr = new_block;
1839                         ret |= BLOCK_CHANGED;
1840                         pb->changed = 1;
1841 #ifdef RESIZE2FS_DEBUG
1842                         if (pb->rfs->flags & RESIZE_DEBUG_BMOVE)
1843                                 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
1844                                        pb->old_ino, blockcnt, block,
1845                                        new_block);
1846 #endif
1847                         block = new_block;
1848                 }
1849         }
1850
1851         if (pb->is_dir) {
1852                 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
1853                                                block, (int) blockcnt);
1854                 if (retval) {
1855                         pb->error = retval;
1856                         ret |= BLOCK_ABORT;
1857                 }
1858         }
1859         return ret;
1860 }
1861
1862 /*
1863  * Progress callback
1864  */
1865 static errcode_t progress_callback(ext2_filsys fs,
1866                                    ext2_inode_scan scan EXT2FS_ATTR((unused)),
1867                                    dgrp_t group, void * priv_data)
1868 {
1869         ext2_resize_t rfs = (ext2_resize_t) priv_data;
1870         errcode_t               retval;
1871
1872         /*
1873          * This check is to protect against old ext2 libraries.  It
1874          * shouldn't be needed against new libraries.
1875          */
1876         if ((group+1) == 0)
1877                 return 0;
1878
1879         if (rfs->progress) {
1880                 io_channel_flush(fs->io);
1881                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
1882                                          group+1, fs->group_desc_count);
1883                 if (retval)
1884                         return retval;
1885         }
1886
1887         return 0;
1888 }
1889
1890 static errcode_t migrate_ea_block(ext2_resize_t rfs, ext2_ino_t ino,
1891                                   struct ext2_inode *inode, int *changed)
1892 {
1893         char *buf = NULL;
1894         blk64_t new_block;
1895         errcode_t err = 0;
1896
1897         /* No EA block or no remapping?  Quit early. */
1898         if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 || !rfs->bmap)
1899                 return 0;
1900         new_block = extent_translate(rfs->old_fs, rfs->bmap,
1901                 ext2fs_file_acl_block(rfs->old_fs, inode));
1902         if (new_block == 0)
1903                 return 0;
1904
1905         /* Set the new ACL block */
1906         ext2fs_file_acl_block_set(rfs->old_fs, inode, new_block);
1907
1908         /* Update checksum */
1909         if (ext2fs_has_feature_metadata_csum(rfs->new_fs->super)) {
1910                 err = ext2fs_get_mem(rfs->old_fs->blocksize, &buf);
1911                 if (err)
1912                         return err;
1913                 rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1914                 err = ext2fs_read_ext_attr3(rfs->old_fs, new_block, buf, ino);
1915                 rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1916                 if (err)
1917                         goto out;
1918                 err = ext2fs_write_ext_attr3(rfs->old_fs, new_block, buf, ino);
1919                 if (err)
1920                         goto out;
1921         }
1922         *changed = 1;
1923
1924 out:
1925         ext2fs_free_mem(&buf);
1926         return err;
1927 }
1928
1929 /* Rewrite extents */
1930 static errcode_t rewrite_extents(ext2_filsys fs, ext2_ino_t ino)
1931 {
1932         ext2_extent_handle_t    handle;
1933         struct ext2fs_extent    extent;
1934         errcode_t               errcode;
1935         struct ext2_extent_info info;
1936
1937         errcode = ext2fs_extent_open(fs, ino, &handle);
1938         if (errcode)
1939                 return errcode;
1940
1941         errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
1942         if (errcode)
1943                 goto out;
1944
1945         do {
1946                 errcode = ext2fs_extent_get_info(handle, &info);
1947                 if (errcode)
1948                         break;
1949
1950                 /*
1951                  * If this is the first extent in an extent block that we
1952                  * haven't visited, rewrite the extent to force the ETB
1953                  * checksum to be rewritten.
1954                  */
1955                 if (info.curr_entry == 1 && info.curr_level != 0 &&
1956                     !(extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)) {
1957                         errcode = ext2fs_extent_replace(handle, 0, &extent);
1958                         if (errcode)
1959                                 break;
1960                 }
1961
1962                 /* Skip to the end of a block of leaf nodes */
1963                 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
1964                         errcode = ext2fs_extent_get(handle,
1965                                                     EXT2_EXTENT_LAST_SIB,
1966                                                     &extent);
1967                         if (errcode)
1968                                 break;
1969                 }
1970
1971                 errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
1972         } while (errcode == 0);
1973
1974 out:
1975         /* Ok if we run off the end */
1976         if (errcode == EXT2_ET_EXTENT_NO_NEXT)
1977                 errcode = 0;
1978         ext2fs_extent_free(handle);
1979         return errcode;
1980 }
1981
1982 static void quiet_com_err_proc(const char *whoami EXT2FS_ATTR((unused)),
1983                                errcode_t code EXT2FS_ATTR((unused)),
1984                                const char *fmt EXT2FS_ATTR((unused)),
1985                                va_list args EXT2FS_ATTR((unused)))
1986 {
1987 }
1988
1989 static int fix_ea_entries(ext2_extent imap, struct ext2_ext_attr_entry *entry,
1990                           struct ext2_ext_attr_entry *end, ext2_ino_t last_ino)
1991 {
1992         int modified = 0;
1993         ext2_ino_t new_ino;
1994         errcode_t retval;
1995
1996         while (entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry)) {
1997                 if (entry->e_value_inum > last_ino) {
1998                         new_ino = ext2fs_extent_translate(imap,
1999                                                           entry->e_value_inum);
2000                         entry->e_value_inum = new_ino;
2001                         modified = 1;
2002                 }
2003                 entry = EXT2_EXT_ATTR_NEXT(entry);
2004         }
2005         return modified;
2006 }
2007
2008 static int fix_ea_ibody_entries(ext2_extent imap,
2009                                 struct ext2_inode_large *inode, int inode_size,
2010                                 ext2_ino_t last_ino)
2011 {
2012         struct ext2_ext_attr_entry *start, *end;
2013         __u32 *ea_magic;
2014
2015         if (inode->i_extra_isize == 0)
2016                 return 0;
2017
2018         ea_magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
2019                                 inode->i_extra_isize);
2020         if (*ea_magic != EXT2_EXT_ATTR_MAGIC)
2021                 return 0;
2022
2023         start = (struct ext2_ext_attr_entry *)(ea_magic + 1);
2024         end = (struct ext2_ext_attr_entry *)((char *)inode + inode_size);
2025
2026         return fix_ea_entries(imap, start, end, last_ino);
2027 }
2028
2029 static int fix_ea_block_entries(ext2_extent imap, char *block_buf,
2030                                 unsigned int blocksize, ext2_ino_t last_ino)
2031 {
2032         struct ext2_ext_attr_header *header;
2033         struct ext2_ext_attr_entry *start, *end;
2034
2035         header = (struct ext2_ext_attr_header *)block_buf;
2036         start = (struct ext2_ext_attr_entry *)(header+1);
2037         end = (struct ext2_ext_attr_entry *)(block_buf + blocksize);
2038
2039         return fix_ea_entries(imap, start, end, last_ino);
2040 }
2041
2042 /* A simple LRU cache to check recently processed blocks. */
2043 struct blk_cache {
2044         int cursor;
2045         blk64_t blks[4];
2046 };
2047
2048 #define BLK_IN_CACHE(b,c) ((b) == (c).blks[0] || (b) == (c).blks[1] || \
2049                            (b) == (c).blks[2] || (b) == (c).blks[3])
2050 #define BLK_ADD_CACHE(b,c) {                    \
2051         (c).blks[(c).cursor] = (b);             \
2052         (c).cursor = ((c).cursor + 1) % 4;      \
2053 }
2054
2055 static errcode_t fix_ea_inode_refs(ext2_resize_t rfs, struct ext2_inode *inode,
2056                                    char *block_buf, ext2_ino_t last_ino)
2057 {
2058         ext2_filsys     fs = rfs->new_fs;
2059         ext2_inode_scan scan = NULL;
2060         ext2_ino_t      ino;
2061         int             inode_size = EXT2_INODE_SIZE(fs->super);
2062         blk64_t         blk;
2063         int             modified;
2064         struct blk_cache blk_cache = { 0 };
2065         struct ext2_ext_attr_header *header;
2066         errcode_t               retval;
2067
2068         header = (struct ext2_ext_attr_header *)block_buf;
2069
2070         retval = ext2fs_open_inode_scan(fs, 0, &scan);
2071         if (retval)
2072                 goto out;
2073
2074         while (1) {
2075                 retval = ext2fs_get_next_inode_full(scan, &ino, inode,
2076                                                     inode_size);
2077                 if (retval)
2078                         goto out;
2079                 if (!ino)
2080                         break;
2081
2082                 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
2083                         continue; /* inode not in use */
2084
2085                 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
2086                         modified = fix_ea_ibody_entries(rfs->imap,
2087                                         (struct ext2_inode_large *)inode,
2088                                         inode_size, last_ino);
2089                         if (modified) {
2090                                 retval = ext2fs_write_inode_full(fs, ino, inode,
2091                                                                  inode_size);
2092                                 if (retval)
2093                                         goto out;
2094                         }
2095                 }
2096
2097                 blk = ext2fs_file_acl_block(fs, inode);
2098                 if (blk && !BLK_IN_CACHE(blk, blk_cache)) {
2099                         retval = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
2100                         if (retval)
2101                                 goto out;
2102
2103                         modified = fix_ea_block_entries(rfs->imap, block_buf,
2104                                                         fs->blocksize,
2105                                                         last_ino);
2106                         if (modified) {
2107                                 retval = ext2fs_write_ext_attr3(fs, blk,
2108                                                                 block_buf, ino);
2109                                 if (retval)
2110                                         goto out;
2111                                 /*
2112                                  * If refcount is greater than 1, we might see
2113                                  * the same block referenced by other inodes
2114                                  * later.
2115                                  */
2116                                 if (header->h_refcount > 1)
2117                                         BLK_ADD_CACHE(blk, blk_cache);
2118                         }
2119                 }
2120         }
2121         retval = 0;
2122 out:
2123         if (scan)
2124                 ext2fs_close_inode_scan(scan);
2125         return retval;
2126
2127 }
2128 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
2129 {
2130         struct process_block_struct     pb;
2131         ext2_ino_t              ino, new_inode;
2132         struct ext2_inode       *inode = NULL;
2133         ext2_inode_scan         scan = NULL;
2134         errcode_t               retval;
2135         char                    *block_buf = 0;
2136         ext2_ino_t              start_to_move;
2137         int                     inode_size;
2138         int                     update_ea_inode_refs = 0;
2139
2140         if ((rfs->old_fs->group_desc_count <=
2141              rfs->new_fs->group_desc_count) &&
2142             !rfs->bmap)
2143                 return 0;
2144
2145         set_com_err_hook(quiet_com_err_proc);
2146
2147         retval = ext2fs_open_inode_scan(rfs->old_fs, 0, &scan);
2148         if (retval) goto errout;
2149
2150         retval = ext2fs_init_dblist(rfs->old_fs, 0);
2151         if (retval) goto errout;
2152         retval = ext2fs_get_array(rfs->old_fs->blocksize, 3, &block_buf);
2153         if (retval) goto errout;
2154
2155         start_to_move = (rfs->new_fs->group_desc_count *
2156                          rfs->new_fs->super->s_inodes_per_group);
2157
2158         if (rfs->progress) {
2159                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_SCAN_PASS,
2160                                          0, rfs->old_fs->group_desc_count);
2161                 if (retval)
2162                         goto errout;
2163         }
2164         ext2fs_set_inode_callback(scan, progress_callback, (void *) rfs);
2165         pb.rfs = rfs;
2166         pb.inode = inode;
2167         pb.error = 0;
2168         new_inode = EXT2_FIRST_INODE(rfs->new_fs->super);
2169         inode_size = EXT2_INODE_SIZE(rfs->new_fs->super);
2170         inode = malloc(inode_size);
2171         if (!inode) {
2172                 retval = ENOMEM;
2173                 goto errout;
2174         }
2175         /*
2176          * First, copy all of the inodes that need to be moved
2177          * elsewhere in the inode table
2178          */
2179         while (1) {
2180                 retval = ext2fs_get_next_inode_full(scan, &ino, inode, inode_size);
2181                 if (retval) goto errout;
2182                 if (!ino)
2183                         break;
2184
2185                 if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
2186                         continue; /* inode not in use */
2187
2188                 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2189                 pb.changed = 0;
2190
2191                 /* Remap EA block */
2192                 retval = migrate_ea_block(rfs, ino, inode, &pb.changed);
2193                 if (retval)
2194                         goto errout;
2195
2196                 new_inode = ino;
2197                 if (ino <= start_to_move)
2198                         goto remap_blocks; /* Don't need to move inode. */
2199
2200                 /*
2201                  * Find a new inode.  Now that extents and directory blocks
2202                  * are tied to the inode number through the checksum, we must
2203                  * set up the new inode before we start rewriting blocks.
2204                  */
2205                 retval = ext2fs_new_inode(rfs->new_fs, 0, 0, 0, &new_inode);
2206                 if (retval)
2207                         goto errout;
2208
2209                 ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
2210                                           pb.is_dir);
2211                 /*
2212                  * i_ctime field in xattr inodes contain a portion of the ref
2213                  * count, do not overwrite.
2214                  */
2215                 if (inode->i_flags & EXT4_EA_INODE_FL)
2216                         update_ea_inode_refs = 1;
2217                 else
2218                         inode->i_ctime = time(0);
2219
2220                 retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
2221                                                 inode, inode_size);
2222                 if (retval)
2223                         goto errout;
2224                 pb.changed = 0;
2225
2226 #ifdef RESIZE2FS_DEBUG
2227                 if (rfs->flags & RESIZE_DEBUG_INODEMAP)
2228                         printf("Inode moved %u->%u\n", ino, new_inode);
2229 #endif
2230                 if (!rfs->imap) {
2231                         retval = ext2fs_create_extent_table(&rfs->imap, 0);
2232                         if (retval)
2233                                 goto errout;
2234                 }
2235                 ext2fs_add_extent_entry(rfs->imap, ino, new_inode);
2236
2237 remap_blocks:
2238                 if (pb.changed)
2239                         retval = ext2fs_write_inode_full(rfs->old_fs,
2240                                                          new_inode,
2241                                                          inode, inode_size);
2242                 if (retval)
2243                         goto errout;
2244
2245                 /* Rewrite extent block checksums with new inode number */
2246                 if (ext2fs_has_feature_metadata_csum(rfs->old_fs->super) &&
2247                     (inode->i_flags & EXT4_EXTENTS_FL)) {
2248                         rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2249                         retval = rewrite_extents(rfs->old_fs, new_inode);
2250                         rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
2251                         if (retval)
2252                                 goto errout;
2253                 }
2254
2255                 /*
2256                  * Update inodes to point to new blocks; schedule directory
2257                  * blocks for inode remapping.  Need to write out dir blocks
2258                  * with new inode numbers if we have metadata_csum enabled.
2259                  */
2260                 if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
2261                     (rfs->bmap || pb.is_dir)) {
2262                         pb.ino = new_inode;
2263                         pb.old_ino = ino;
2264                         pb.has_extents = inode->i_flags & EXT4_EXTENTS_FL;
2265                         rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2266                         retval = ext2fs_block_iterate3(rfs->old_fs,
2267                                                        new_inode, 0, block_buf,
2268                                                        process_block, &pb);
2269                         rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
2270                         if (retval)
2271                                 goto errout;
2272                         if (pb.error) {
2273                                 retval = pb.error;
2274                                 goto errout;
2275                         }
2276                 } else if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2277                            (rfs->bmap || pb.is_dir)) {
2278                         /* inline data dir; update it too */
2279                         retval = ext2fs_add_dir_block2(rfs->old_fs->dblist,
2280                                                        new_inode, 0, 0);
2281                         if (retval)
2282                                 goto errout;
2283                 }
2284         }
2285
2286         if (update_ea_inode_refs &&
2287             ext2fs_has_feature_ea_inode(rfs->new_fs->super)) {
2288                 retval = fix_ea_inode_refs(rfs, inode, block_buf,
2289                                            start_to_move);
2290                 if (retval)
2291                         goto errout;
2292         }
2293         io_channel_flush(rfs->old_fs->io);
2294
2295 errout:
2296         reset_com_err_hook();
2297         if (rfs->bmap) {
2298                 ext2fs_free_extent_table(rfs->bmap);
2299                 rfs->bmap = 0;
2300         }
2301         if (scan)
2302                 ext2fs_close_inode_scan(scan);
2303         if (block_buf)
2304                 ext2fs_free_mem(&block_buf);
2305         free(inode);
2306         return retval;
2307 }
2308
2309 /* --------------------------------------------------------------------
2310  *
2311  * Resize processing, phase 4.
2312  *
2313  * --------------------------------------------------------------------
2314  */
2315
2316 struct istruct {
2317         ext2_resize_t rfs;
2318         errcode_t       err;
2319         unsigned int    max_dirs;
2320         unsigned int    num;
2321 };
2322
2323 static int check_and_change_inodes(ext2_ino_t dir,
2324                                    int entry EXT2FS_ATTR((unused)),
2325                                    struct ext2_dir_entry *dirent, int offset,
2326                                    int  blocksize EXT2FS_ATTR((unused)),
2327                                    char *buf EXT2FS_ATTR((unused)),
2328                                    void *priv_data)
2329 {
2330         struct istruct *is = (struct istruct *) priv_data;
2331         struct ext2_inode       inode;
2332         ext2_ino_t              new_inode;
2333         errcode_t               retval;
2334         int                     ret = 0;
2335
2336         if (is->rfs->progress && offset == 0) {
2337                 io_channel_flush(is->rfs->old_fs->io);
2338                 is->err = (is->rfs->progress)(is->rfs,
2339                                               E2_RSZ_INODE_REF_UPD_PASS,
2340                                               ++is->num, is->max_dirs);
2341                 if (is->err)
2342                         return DIRENT_ABORT;
2343         }
2344
2345         /*
2346          * If we have checksums enabled and the inode wasn't present in the
2347          * old fs, then we must rewrite all dir blocks with new checksums.
2348          */
2349         if (ext2fs_has_feature_metadata_csum(is->rfs->old_fs->super) &&
2350             !ext2fs_test_inode_bitmap2(is->rfs->old_fs->inode_map, dir))
2351                 ret |= DIRENT_CHANGED;
2352
2353         if (!dirent->inode)
2354                 return ret;
2355
2356         new_inode = ext2fs_extent_translate(is->rfs->imap, dirent->inode);
2357
2358         if (!new_inode)
2359                 return ret;
2360 #ifdef RESIZE2FS_DEBUG
2361         if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
2362                 printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
2363                        dir, ext2fs_dirent_name_len(dirent), dirent->name,
2364                        dirent->inode, new_inode);
2365 #endif
2366
2367         dirent->inode = new_inode;
2368
2369         /* Update the directory mtime and ctime */
2370         retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
2371         if (retval == 0) {
2372                 inode.i_mtime = inode.i_ctime = time(0);
2373                 is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
2374                 if (is->err)
2375                         return ret | DIRENT_ABORT;
2376         }
2377
2378         return ret | DIRENT_CHANGED;
2379 }
2380
2381 static errcode_t inode_ref_fix(ext2_resize_t rfs)
2382 {
2383         errcode_t               retval;
2384         struct istruct          is;
2385
2386         if (!rfs->imap)
2387                 return 0;
2388
2389         /*
2390          * Now, we iterate over all of the directories to update the
2391          * inode references
2392          */
2393         is.num = 0;
2394         is.max_dirs = ext2fs_dblist_count2(rfs->old_fs->dblist);
2395         is.rfs = rfs;
2396         is.err = 0;
2397
2398         if (rfs->progress) {
2399                 retval = (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
2400                                          0, is.max_dirs);
2401                 if (retval)
2402                         goto errout;
2403         }
2404
2405         rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2406         retval = ext2fs_dblist_dir_iterate(rfs->old_fs->dblist,
2407                                            DIRENT_FLAG_INCLUDE_EMPTY, 0,
2408                                            check_and_change_inodes, &is);
2409         rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
2410         if (retval)
2411                 goto errout;
2412         if (is.err) {
2413                 retval = is.err;
2414                 goto errout;
2415         }
2416
2417         if (rfs->progress && (is.num < is.max_dirs))
2418                 (rfs->progress)(rfs, E2_RSZ_INODE_REF_UPD_PASS,
2419                                 is.max_dirs, is.max_dirs);
2420
2421 errout:
2422         ext2fs_free_extent_table(rfs->imap);
2423         rfs->imap = 0;
2424         return retval;
2425 }
2426
2427
2428 /* --------------------------------------------------------------------
2429  *
2430  * Resize processing, phase 5.
2431  *
2432  * In this phase we actually move the inode table around, and then
2433  * update the summary statistics.  This is scary, since aborting here
2434  * will potentially scramble the filesystem.  (We are moving the
2435  * inode tables around in place, and so the potential for lost data,
2436  * or at the very least scrambling the mapping between filenames and
2437  * inode numbers is very high in case of a power failure here.)
2438  * --------------------------------------------------------------------
2439  */
2440
2441
2442 /*
2443  * A very scary routine --- this one moves the inode table around!!!
2444  *
2445  * After this you have to use the rfs->new_fs file handle to read and
2446  * write inodes.
2447  */
2448 static errcode_t move_itables(ext2_resize_t rfs)
2449 {
2450         int             n, num, size;
2451         long long       diff;
2452         dgrp_t          i, max_groups;
2453         ext2_filsys     fs = rfs->new_fs;
2454         char            *cp;
2455         blk64_t         old_blk, new_blk, blk, cluster_freed;
2456         errcode_t       retval;
2457         int             to_move, moved;
2458         unsigned int    j;
2459         ext2fs_block_bitmap     new_bmap = NULL;
2460
2461         max_groups = fs->group_desc_count;
2462         if (max_groups > rfs->old_fs->group_desc_count)
2463                 max_groups = rfs->old_fs->group_desc_count;
2464
2465         size = fs->blocksize * fs->inode_blocks_per_group;
2466         if (!rfs->itable_buf) {
2467                 retval = ext2fs_get_mem(size, &rfs->itable_buf);
2468                 if (retval)
2469                         return retval;
2470         }
2471
2472         if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
2473                 retval = ext2fs_allocate_block_bitmap(fs, _("new meta blocks"),
2474                                                       &new_bmap);
2475                 if (retval)
2476                         return retval;
2477
2478                 retval = mark_table_blocks(fs, new_bmap);
2479                 if (retval)
2480                         goto errout;
2481         }
2482
2483         /*
2484          * Figure out how many inode tables we need to move
2485          */
2486         to_move = moved = 0;
2487         for (i=0; i < max_groups; i++)
2488                 if (ext2fs_inode_table_loc(rfs->old_fs, i) !=
2489                     ext2fs_inode_table_loc(fs, i))
2490                         to_move++;
2491
2492         if (to_move == 0) {
2493                 retval = 0;
2494                 goto errout;
2495         }
2496
2497         if (rfs->progress) {
2498                 retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2499                                        0, to_move);
2500                 if (retval)
2501                         goto errout;
2502         }
2503
2504         rfs->old_fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
2505
2506         for (i=0; i < max_groups; i++) {
2507                 old_blk = ext2fs_inode_table_loc(rfs->old_fs, i);
2508                 new_blk = ext2fs_inode_table_loc(fs, i);
2509                 diff = new_blk - old_blk;
2510
2511 #ifdef RESIZE2FS_DEBUG
2512                 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2513                         printf("Itable move group %d block %llu->%llu (diff %lld)\n",
2514                                i, old_blk, new_blk, diff);
2515 #endif
2516
2517                 if (!diff)
2518                         continue;
2519                 if (diff < 0)
2520                         diff = 0;
2521
2522                 retval = io_channel_read_blk64(fs->io, old_blk,
2523                                                fs->inode_blocks_per_group,
2524                                                rfs->itable_buf);
2525                 if (retval)
2526                         goto errout;
2527                 /*
2528                  * The end of the inode table segment often contains
2529                  * all zeros, and we're often only moving the inode
2530                  * table down a block or two.  If so, we can optimize
2531                  * things by not rewriting blocks that we know to be zero
2532                  * already.
2533                  */
2534                 for (cp = rfs->itable_buf+size-1, n=0; n < size; n++, cp--)
2535                         if (*cp)
2536                                 break;
2537                 n = n >> EXT2_BLOCK_SIZE_BITS(fs->super);
2538 #ifdef RESIZE2FS_DEBUG
2539                 if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2540                         printf("%d blocks of zeros...\n", n);
2541 #endif
2542                 num = fs->inode_blocks_per_group;
2543                 if (n > diff)
2544                         num -= n;
2545
2546                 retval = io_channel_write_blk64(fs->io, new_blk,
2547                                                 num, rfs->itable_buf);
2548                 if (retval) {
2549                         io_channel_write_blk64(fs->io, old_blk,
2550                                                num, rfs->itable_buf);
2551                         goto errout;
2552                 }
2553                 if (n > diff) {
2554                         retval = io_channel_write_blk64(fs->io,
2555                               old_blk + fs->inode_blocks_per_group,
2556                               diff, (rfs->itable_buf +
2557                                      (fs->inode_blocks_per_group - diff) *
2558                                      fs->blocksize));
2559                         if (retval)
2560                                 goto errout;
2561                 }
2562
2563                 for (blk = ext2fs_inode_table_loc(rfs->old_fs, i), j=0;
2564                      j < fs->inode_blocks_per_group;) {
2565                         if (new_bmap == NULL ||
2566                             !ext2fs_test_block_bitmap2(new_bmap, blk)) {
2567                                 ext2fs_block_alloc_stats2(fs, blk, -1);
2568                                 cluster_freed = EXT2FS_CLUSTER_RATIO(fs) -
2569                                                 (blk & EXT2FS_CLUSTER_MASK(fs));
2570                                 blk += cluster_freed;
2571                                 j += cluster_freed;
2572                                 continue;
2573                         }
2574                         blk++;
2575                         j++;
2576                 }
2577
2578                 ext2fs_inode_table_loc_set(rfs->old_fs, i, new_blk);
2579                 ext2fs_group_desc_csum_set(rfs->old_fs, i);
2580                 ext2fs_mark_super_dirty(rfs->old_fs);
2581                 ext2fs_flush(rfs->old_fs);
2582
2583                 if (rfs->progress) {
2584                         retval = rfs->progress(rfs, E2_RSZ_MOVE_ITABLE_PASS,
2585                                                ++moved, to_move);
2586                         if (retval)
2587                                 goto errout;
2588                 }
2589         }
2590         mark_table_blocks(fs, fs->block_map);
2591         ext2fs_flush(fs);
2592 #ifdef RESIZE2FS_DEBUG
2593         if (rfs->flags & RESIZE_DEBUG_ITABLEMOVE)
2594                 printf("Inode table move finished.\n");
2595 #endif
2596         retval = 0;
2597
2598 errout:
2599         if (new_bmap)
2600                 ext2fs_free_block_bitmap(new_bmap);
2601         return retval;
2602 }
2603
2604 /*
2605  * This function is used when expanding a file system.  It frees the
2606  * superblock and block group descriptor blocks from the block group
2607  * which is no longer the last block group.
2608  */
2609 static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs)
2610 {
2611         ext2_filsys     fs = rfs->new_fs;
2612         ext2_filsys     old_fs = rfs->old_fs;
2613         errcode_t       retval;
2614         dgrp_t          old_last_bg = rfs->old_fs->group_desc_count - 1;
2615         dgrp_t          last_bg = fs->group_desc_count - 1;
2616         blk64_t         sb, old_desc;
2617         blk_t           num;
2618
2619         if (!ext2fs_has_feature_sparse_super2(fs->super))
2620                 return 0;
2621
2622         if (last_bg <= old_last_bg)
2623                 return 0;
2624
2625         if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2626             fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2627                 return 0;
2628
2629         if (old_fs->super->s_backup_bgs[0] != old_last_bg &&
2630             old_fs->super->s_backup_bgs[1] != old_last_bg)
2631                 return 0;
2632
2633         if (fs->super->s_backup_bgs[0] == old_last_bg ||
2634             fs->super->s_backup_bgs[1] == old_last_bg)
2635                 return 0;
2636
2637         if (old_last_bg == 0)
2638                 return 0;
2639
2640         retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg,
2641                                            &sb, &old_desc, NULL, &num);
2642         if (retval)
2643                 return retval;
2644
2645         if (sb)
2646                 ext2fs_unmark_block_bitmap2(fs->block_map, sb);
2647         if (old_desc)
2648                 ext2fs_unmark_block_bitmap_range2(fs->block_map, old_desc, num);
2649         return 0;
2650 }
2651
2652 /*
2653  * This function is used when shrinking a file system.  We need to
2654  * utilize blocks from what will be the new last block group for the
2655  * backup superblock and block group descriptor blocks.
2656  * Unfortunately, those blocks may be used by other files or fs
2657  * metadata blocks.  We need to mark them as being in use.
2658  */
2659 static errcode_t reserve_sparse_super2_last_group(ext2_resize_t rfs,
2660                                                  ext2fs_block_bitmap meta_bmap)
2661 {
2662         ext2_filsys     fs = rfs->new_fs;
2663         ext2_filsys     old_fs = rfs->old_fs;
2664         errcode_t       retval;
2665         dgrp_t          old_last_bg = rfs->old_fs->group_desc_count - 1;
2666         dgrp_t          last_bg = fs->group_desc_count - 1;
2667         dgrp_t          g;
2668         blk64_t         blk, sb, old_desc;
2669         blk_t           i, num;
2670         int             realloc = 0;
2671
2672         if (!ext2fs_has_feature_sparse_super2(fs->super))
2673                 return 0;
2674
2675         if (last_bg >= old_last_bg)
2676                 return 0;
2677
2678         if (fs->super->s_backup_bgs[0] == old_fs->super->s_backup_bgs[0] &&
2679             fs->super->s_backup_bgs[1] == old_fs->super->s_backup_bgs[1])
2680                 return 0;
2681
2682         if (fs->super->s_backup_bgs[0] != last_bg &&
2683             fs->super->s_backup_bgs[1] != last_bg)
2684                 return 0;
2685
2686         if (old_fs->super->s_backup_bgs[0] == last_bg ||
2687             old_fs->super->s_backup_bgs[1] == last_bg)
2688                 return 0;
2689
2690         retval = ext2fs_super_and_bgd_loc2(rfs->new_fs, last_bg,
2691                                            &sb, &old_desc, NULL, &num);
2692         if (retval)
2693                 return retval;
2694
2695         if (last_bg && !sb) {
2696                 fputs(_("Should never happen!  No sb in last super_sparse bg?\n"),
2697                       stderr);
2698                 exit(1);
2699         }
2700         if (old_desc && old_desc != sb+1) {
2701                 fputs(_("Should never happen!  Unexpected old_desc in "
2702                         "super_sparse bg?\n"),
2703                       stderr);
2704                 exit(1);
2705         }
2706         num = (old_desc) ? num : 1;
2707
2708         /* Reserve the backup blocks */
2709         ext2fs_mark_block_bitmap_range2(fs->block_map, sb, num);
2710
2711         for (g = 0; g < fs->group_desc_count; g++) {
2712                 blk64_t mb;
2713
2714                 mb = ext2fs_block_bitmap_loc(fs, g);
2715                 if ((mb >= sb) && (mb < sb + num)) {
2716                         ext2fs_block_bitmap_loc_set(fs, g, 0);
2717                         realloc = 1;
2718                 }
2719                 mb = ext2fs_inode_bitmap_loc(fs, g);
2720                 if ((mb >= sb) && (mb < sb + num)) {
2721                         ext2fs_inode_bitmap_loc_set(fs, g, 0);
2722                         realloc = 1;
2723                 }
2724                 mb = ext2fs_inode_table_loc(fs, g);
2725                 if ((mb < sb + num) &&
2726                     (sb < mb + fs->inode_blocks_per_group)) {
2727                         ext2fs_inode_table_loc_set(fs, g, 0);
2728                         realloc = 1;
2729                 }
2730                 if (realloc) {
2731                         retval = ext2fs_allocate_group_table(fs, g, 0);
2732                         if (retval)
2733                                 return retval;
2734                 }
2735         }
2736
2737         for (blk = sb, i = 0; i < num; blk++, i++) {
2738                 if (ext2fs_test_block_bitmap2(old_fs->block_map, blk) &&
2739                     !ext2fs_test_block_bitmap2(meta_bmap, blk)) {
2740                         ext2fs_mark_block_bitmap2(rfs->move_blocks, blk);
2741                         rfs->needed_blocks++;
2742                 }
2743                 ext2fs_mark_block_bitmap2(rfs->reserve_blocks, blk);
2744         }
2745         return 0;
2746 }
2747
2748 /*
2749  * Fix the resize inode
2750  */
2751 static errcode_t fix_resize_inode(ext2_filsys fs)
2752 {
2753         struct ext2_inode       inode;
2754         errcode_t               retval;
2755
2756         if (!ext2fs_has_feature_resize_inode(fs->super))
2757                 return 0;
2758
2759         retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
2760         if (retval) goto errout;
2761
2762         ext2fs_iblk_set(fs, &inode, 1);
2763
2764         retval = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
2765         if (retval) goto errout;
2766
2767         if (!inode.i_block[EXT2_DIND_BLOCK]) {
2768                 /*
2769                  * Avoid zeroing out block #0; that's rude.  This
2770                  * should never happen anyway since the filesystem
2771                  * should be fsck'ed and we assume it is consistent.
2772                  */
2773                 fprintf(stderr, "%s",
2774                         _("Should never happen: resize inode corrupt!\n"));
2775                 exit(1);
2776         }
2777
2778         retval = ext2fs_zero_blocks2(fs, inode.i_block[EXT2_DIND_BLOCK], 1,
2779                                      NULL, NULL);
2780         if (retval)
2781                 goto errout;
2782
2783         retval = ext2fs_create_resize_inode(fs);
2784         if (retval)
2785                 goto errout;
2786
2787 errout:
2788         return retval;
2789 }
2790
2791 /*
2792  * Finally, recalculate the summary information
2793  */
2794 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2795 {
2796         blk64_t         blk;
2797         ext2_ino_t      ino;
2798         unsigned int    group = 0;
2799         unsigned int    count = 0;
2800         blk64_t         total_blocks_free = 0;
2801         int             total_inodes_free = 0;
2802         int             group_free = 0;
2803         int             uninit = 0;
2804         blk64_t         super_blk, old_desc_blk, new_desc_blk;
2805         int             old_desc_blocks;
2806
2807         /*
2808          * First calculate the block statistics
2809          */
2810         uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2811         ext2fs_super_and_bgd_loc2(fs, group, &super_blk, &old_desc_blk,
2812                                   &new_desc_blk, 0);
2813         if (ext2fs_has_feature_meta_bg(fs->super))
2814                 old_desc_blocks = fs->super->s_first_meta_bg;
2815         else
2816                 old_desc_blocks = fs->desc_blocks +
2817                         fs->super->s_reserved_gdt_blocks;
2818         for (blk = B2C(fs->super->s_first_data_block);
2819              blk < ext2fs_blocks_count(fs->super);
2820              blk += EXT2FS_CLUSTER_RATIO(fs)) {
2821                 if ((uninit &&
2822                      !(EQ_CLSTR(blk, super_blk) ||
2823                        ((old_desc_blk && old_desc_blocks &&
2824                          GE_CLSTR(blk, old_desc_blk) &&
2825                          LT_CLSTR(blk, old_desc_blk + old_desc_blocks))) ||
2826                        ((new_desc_blk && EQ_CLSTR(blk, new_desc_blk))) ||
2827                        EQ_CLSTR(blk, ext2fs_block_bitmap_loc(fs, group)) ||
2828                        EQ_CLSTR(blk, ext2fs_inode_bitmap_loc(fs, group)) ||
2829                        ((GE_CLSTR(blk, ext2fs_inode_table_loc(fs, group)) &&
2830                          LT_CLSTR(blk, ext2fs_inode_table_loc(fs, group)
2831                                   + fs->inode_blocks_per_group))))) ||
2832                     (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk))) {
2833                         group_free++;
2834                         total_blocks_free++;
2835                 }
2836                 count++;
2837                 if ((count == fs->super->s_clusters_per_group) ||
2838                     EQ_CLSTR(blk, ext2fs_blocks_count(fs->super)-1)) {
2839                         ext2fs_bg_free_blocks_count_set(fs, group, group_free);
2840                         ext2fs_group_desc_csum_set(fs, group);
2841                         group++;
2842                         if (group >= fs->group_desc_count)
2843                                 break;
2844                         count = 0;
2845                         group_free = 0;
2846                         uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT);
2847                         ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
2848                                                   &old_desc_blk,
2849                                                   &new_desc_blk, 0);
2850                         if (ext2fs_has_feature_meta_bg(fs->super))
2851                                 old_desc_blocks = fs->super->s_first_meta_bg;
2852                         else
2853                                 old_desc_blocks = fs->desc_blocks +
2854                                         fs->super->s_reserved_gdt_blocks;
2855                 }
2856         }
2857         total_blocks_free = C2B(total_blocks_free);
2858         ext2fs_free_blocks_count_set(fs->super, total_blocks_free);
2859
2860         /*
2861          * Next, calculate the inode statistics
2862          */
2863         group_free = 0;
2864         count = 0;
2865         group = 0;
2866
2867         /* Protect loop from wrap-around if s_inodes_count maxed */
2868         uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2869         for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2870                 if (uninit ||
2871                     !ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2872                         group_free++;
2873                         total_inodes_free++;
2874                 }
2875                 count++;
2876                 if ((count == fs->super->s_inodes_per_group) ||
2877                     (ino == fs->super->s_inodes_count)) {
2878                         ext2fs_bg_free_inodes_count_set(fs, group, group_free);
2879                         ext2fs_group_desc_csum_set(fs, group);
2880                         group++;
2881                         if (group >= fs->group_desc_count)
2882                                 break;
2883                         count = 0;
2884                         group_free = 0;
2885                         uninit = ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT);
2886                 }
2887         }
2888         fs->super->s_free_inodes_count = total_inodes_free;
2889         ext2fs_mark_super_dirty(fs);
2890         return 0;
2891 }
2892
2893 /*
2894  *  Journal may have been relocated; update the backup journal blocks
2895  *  in the superblock.
2896  */
2897 static errcode_t fix_sb_journal_backup(ext2_filsys fs)
2898 {
2899         errcode_t         retval;
2900         struct ext2_inode inode;
2901
2902         if (!ext2fs_has_feature_journal(fs->super))
2903                 return 0;
2904
2905         /* External journal? Nothing to do. */
2906         if (fs->super->s_journal_dev && !fs->super->s_journal_inum)
2907                 return 0;
2908
2909         retval = ext2fs_read_inode(fs, fs->super->s_journal_inum, &inode);
2910         if (retval)
2911                 return retval;
2912         memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
2913         fs->super->s_jnl_blocks[15] = inode.i_size_high;
2914         fs->super->s_jnl_blocks[16] = inode.i_size;
2915         fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
2916         ext2fs_mark_super_dirty(fs);
2917         return 0;
2918 }
2919
2920 static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
2921                                int old_desc_blocks)
2922 {
2923         blk64_t super_blk, old_desc_blk, new_desc_blk;
2924         int overhead;
2925
2926         /* inode table blocks plus allocation bitmaps */
2927         overhead = fs->inode_blocks_per_group + 2;
2928
2929         ext2fs_super_and_bgd_loc2(fs, grp, &super_blk,
2930                                   &old_desc_blk, &new_desc_blk, 0);
2931         if ((grp == 0) || super_blk)
2932                 overhead++;
2933         if (old_desc_blk)
2934                 overhead += old_desc_blocks;
2935         else if (new_desc_blk)
2936                 overhead++;
2937         return overhead;
2938 }
2939
2940
2941 /*
2942  * calcluate the minimum number of blocks the given fs can be resized to
2943  */
2944 blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
2945 {
2946         ext2_ino_t inode_count;
2947         dgrp_t groups, flex_groups;
2948         blk64_t blks_needed, data_blocks;
2949         blk64_t grp, data_needed, last_start;
2950         blk64_t overhead = 0;
2951         int old_desc_blocks;
2952         int flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2953
2954         /*
2955          * first figure out how many group descriptors we need to
2956          * handle the number of inodes we have
2957          */
2958         inode_count = fs->super->s_inodes_count -
2959                 fs->super->s_free_inodes_count;
2960         blks_needed = ext2fs_div_ceil(inode_count,
2961                                       fs->super->s_inodes_per_group) *
2962                 (blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super);
2963         groups = ext2fs_div64_ceil(blks_needed,
2964                                    EXT2_BLOCKS_PER_GROUP(fs->super));
2965 #ifdef RESIZE2FS_DEBUG
2966         if (flags & RESIZE_DEBUG_MIN_CALC)
2967                 printf("fs has %d inodes, %d groups required.\n",
2968                        inode_count, groups);
2969 #endif
2970
2971         /*
2972          * number of old-style block group descriptor blocks
2973          */
2974         if (ext2fs_has_feature_meta_bg(fs->super))
2975                 old_desc_blocks = fs->super->s_first_meta_bg;
2976         else
2977                 old_desc_blocks = fs->desc_blocks +
2978                         fs->super->s_reserved_gdt_blocks;
2979
2980         /* calculate how many blocks are needed for data */
2981         data_needed = ext2fs_blocks_count(fs->super) -
2982                 ext2fs_free_blocks_count(fs->super);
2983
2984         for (grp = 0; grp < fs->group_desc_count; grp++)
2985                 data_needed -= calc_group_overhead(fs, grp, old_desc_blocks);
2986 #ifdef RESIZE2FS_DEBUG
2987         if (flags & RESIZE_DEBUG_MIN_CALC)
2988                 printf("fs requires %llu data blocks.\n", data_needed);
2989 #endif
2990
2991         /*
2992          * For ext4 we need to allow for up to a flex_bg worth of
2993          * inode tables of slack space so the resize operation can be
2994          * guaranteed to finish.
2995          */
2996         flex_groups = groups;
2997         if (ext2fs_has_feature_flex_bg(fs->super)) {
2998                 dgrp_t remainder = groups & (flexbg_size - 1);
2999
3000                 flex_groups += flexbg_size - remainder;
3001                 if (flex_groups > fs->group_desc_count)
3002                         flex_groups = fs->group_desc_count;
3003         }
3004
3005         /*
3006          * figure out how many data blocks we have given the number of groups
3007          * we need for our inodes
3008          */
3009         data_blocks = EXT2_GROUPS_TO_BLOCKS(fs->super, groups);
3010         last_start = 0;
3011         for (grp = 0; grp < flex_groups; grp++) {
3012                 overhead = calc_group_overhead(fs, grp, old_desc_blocks);
3013
3014                 /*
3015                  * we want to keep track of how much data we can store in
3016                  * the groups leading up to the last group so we can determine
3017                  * how big the last group needs to be
3018                  */
3019                 if (grp < (groups - 1))
3020                         last_start += EXT2_BLOCKS_PER_GROUP(fs->super) -
3021                                 overhead;
3022
3023                 if (data_blocks > overhead)
3024                         data_blocks -= overhead;
3025                 else
3026                         data_blocks = 0;
3027         }
3028 #ifdef RESIZE2FS_DEBUG
3029         if (flags & RESIZE_DEBUG_MIN_CALC)
3030                 printf("With %d group(s), we have %llu blocks available.\n",
3031                        groups, data_blocks);
3032 #endif
3033
3034         /*
3035          * if we need more group descriptors in order to accomodate our data
3036          * then we need to add them here
3037          */
3038         blks_needed = data_needed;
3039         while (blks_needed > data_blocks) {
3040                 blk64_t remainder = blks_needed - data_blocks;
3041                 dgrp_t extra_grps;
3042
3043                 /* figure out how many more groups we need for the data */
3044                 extra_grps = ext2fs_div64_ceil(remainder,
3045                                                EXT2_BLOCKS_PER_GROUP(fs->super));
3046
3047                 data_blocks += EXT2_GROUPS_TO_BLOCKS(fs->super, extra_grps);
3048
3049                 /* ok we have to account for the last group */
3050                 overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
3051                 last_start += EXT2_BLOCKS_PER_GROUP(fs->super) - overhead;
3052
3053                 grp = flex_groups;
3054                 groups += extra_grps;
3055                 if (!ext2fs_has_feature_flex_bg(fs->super))
3056                         flex_groups = groups;
3057                 else if (groups > flex_groups) {
3058                         dgrp_t r = groups & (flexbg_size - 1);
3059
3060                         flex_groups = groups + flexbg_size - r;
3061                         if (flex_groups > fs->group_desc_count)
3062                                 flex_groups = fs->group_desc_count;
3063                 }
3064
3065                 for (; grp < flex_groups; grp++) {
3066                         overhead = calc_group_overhead(fs, grp,
3067                                                        old_desc_blocks);
3068
3069                         /*
3070                          * again, we need to see how much data we cram into
3071                          * all of the groups leading up to the last group
3072                          */
3073                         if (grp < groups - 1)
3074                                 last_start += EXT2_BLOCKS_PER_GROUP(fs->super)
3075                                         - overhead;
3076
3077                         data_blocks -= overhead;
3078                 }
3079
3080 #ifdef RESIZE2FS_DEBUG
3081                 if (flags & RESIZE_DEBUG_MIN_CALC)
3082                         printf("Added %d extra group(s), "
3083                                "blks_needed %llu, data_blocks %llu, "
3084                                "last_start %llu\n", extra_grps, blks_needed,
3085                                data_blocks, last_start);
3086 #endif
3087         }
3088
3089         /* now for the fun voodoo */
3090         grp = groups - 1;
3091         if (ext2fs_has_feature_flex_bg(fs->super) &&
3092             (grp & ~(flexbg_size - 1)) == 0)
3093                 grp = grp & ~(flexbg_size - 1);
3094         overhead = 0;
3095         for (; grp < flex_groups; grp++)
3096                 overhead += calc_group_overhead(fs, grp, old_desc_blocks);
3097
3098 #ifdef RESIZE2FS_DEBUG
3099         if (flags & RESIZE_DEBUG_MIN_CALC)
3100                 printf("Last group's overhead is %llu\n", overhead);
3101 #endif
3102
3103         /*
3104          * if this is the case then the last group is going to have data in it
3105          * so we need to adjust the size of the last group accordingly
3106          */
3107         if (last_start < blks_needed) {
3108                 blk64_t remainder = blks_needed - last_start;
3109
3110 #ifdef RESIZE2FS_DEBUG
3111                 if (flags & RESIZE_DEBUG_MIN_CALC)
3112                         printf("Need %llu data blocks in last group\n",
3113                                remainder);
3114 #endif
3115                 /*
3116                  * 50 is a magic number that mkfs/resize uses to see if its
3117                  * even worth making/resizing the fs.  basically you need to
3118                  * have at least 50 blocks in addition to the blocks needed
3119                  * for the metadata in the last group
3120                  */
3121                 if (remainder > 50)
3122                         overhead += remainder;
3123                 else
3124                         overhead += 50;
3125         } else
3126                 overhead += 50;
3127
3128         overhead += fs->super->s_first_data_block;
3129 #ifdef RESIZE2FS_DEBUG
3130         if (flags & RESIZE_DEBUG_MIN_CALC)
3131                 printf("Final size of last group is %lld\n", overhead);
3132 #endif
3133
3134         /* Add extra slack for bigalloc file systems */
3135         if (EXT2FS_CLUSTER_RATIO(fs) > 1)
3136                 overhead += EXT2FS_CLUSTER_RATIO(fs) * 2;
3137
3138         /*
3139          * since our last group doesn't have to be BLOCKS_PER_GROUP
3140          * large, we only do groups-1, and then add the number of
3141          * blocks needed to handle the group descriptor metadata+data
3142          * that we need
3143          */
3144         blks_needed = EXT2_GROUPS_TO_BLOCKS(fs->super, groups - 1);
3145         blks_needed += overhead;
3146
3147         /*
3148          * Make sure blks_needed covers the end of the inode table in
3149          * the last block group.
3150          */
3151         overhead = ext2fs_inode_table_loc(fs, groups-1) +
3152                 fs->inode_blocks_per_group;
3153         if (blks_needed < overhead)
3154                 blks_needed = overhead;
3155
3156 #ifdef RESIZE2FS_DEBUG
3157         if (flags & RESIZE_DEBUG_MIN_CALC)
3158                 printf("Estimated blocks needed: %llu\n", blks_needed);
3159 #endif
3160
3161         /*
3162          * If at this point we've already added up more "needed" than
3163          * the current size, just return current size as minimum.
3164          */
3165         if (blks_needed >= ext2fs_blocks_count(fs->super))
3166                 return ext2fs_blocks_count(fs->super);
3167         /*
3168          * We need to reserve a few extra blocks if extents are
3169          * enabled, in case we need to grow the extent tree.  The more
3170          * we shrink the file system, the more space we need.
3171          *
3172          * The absolute worst case is every single data block is in
3173          * the part of the file system that needs to be evacuated,
3174          * with each data block needs to be in its own extent, and
3175          * with each inode needing at least one extent block.
3176          */
3177         if (ext2fs_has_feature_extents(fs->super)) {
3178                 blk64_t safe_margin = (ext2fs_blocks_count(fs->super) -
3179                                        blks_needed)/500;
3180                 unsigned int exts_per_blk = (fs->blocksize /
3181                                              sizeof(struct ext3_extent)) - 1;
3182                 blk64_t worst_case = ((data_needed + exts_per_blk - 1) /
3183                                       exts_per_blk);
3184
3185                 if (worst_case < inode_count)
3186                         worst_case = inode_count;
3187
3188                 if (safe_margin > worst_case)
3189                         safe_margin = worst_case;
3190
3191 #ifdef RESIZE2FS_DEBUG
3192                 if (flags & RESIZE_DEBUG_MIN_CALC)
3193                         printf("Extents safety margin: %llu\n", safe_margin);
3194 #endif
3195                 blks_needed += safe_margin;
3196         }
3197
3198         return blks_needed;
3199 }