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