1 /*
2 * initialize.c --- initialize a filesystem handle given superblock
3 * parameters. Used by mke2fs when initializing a filesystem.
4 *
5 * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
10 * %End-Header%
11 */
12
13 #include "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <fcntl.h>
20 #include <time.h>
21 #if HAVE_SYS_STAT_H
22 #include <sys/stat.h>
23 #endif
24 #if HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27
28 #include "ext2_fs.h"
29 #include "ext2fs.h"
30
31 #ifndef O_BINARY
32 #define O_BINARY 0
33 #endif
34
35 #if defined(__linux__) && defined(EXT2_OS_LINUX)
36 #define CREATOR_OS EXT2_OS_LINUX
37 #else
38 #if defined(__GNU__) && defined(EXT2_OS_HURD)
39 #define CREATOR_OS EXT2_OS_HURD
40 #else
41 #if defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD)
42 #define CREATOR_OS EXT2_OS_FREEBSD
43 #else
44 #if defined(LITES) && defined(EXT2_OS_LITES)
45 #define CREATOR_OS EXT2_OS_LITES
46 #else
47 #define CREATOR_OS EXT2_OS_LINUX /* by default */
48 #endif /* defined(LITES) && defined(EXT2_OS_LITES) */
49 #endif /* defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD) */
50 #endif /* defined(__GNU__) && defined(EXT2_OS_HURD) */
51 #endif /* defined(__linux__) && defined(EXT2_OS_LINUX) */
52
53 /*
54 * Calculate the number of GDT blocks to reserve for online filesystem growth.
55 * The absolute maximum number of GDT blocks we can reserve is determined by
56 * the number of block pointers that can fit into a single block.
57 */
calc_reserved_gdt_blocks(ext2_filsys fs)58 static unsigned int calc_reserved_gdt_blocks(ext2_filsys fs)
59 {
60 struct ext2_super_block *sb = fs->super;
61 unsigned long bpg = sb->s_blocks_per_group;
62 unsigned int gdpb = EXT2_DESC_PER_BLOCK(sb);
63 unsigned long max_blocks = 0xffffffff;
64 unsigned long rsv_groups;
65 unsigned int rsv_gdb;
66
67 /* We set it at 1024x the current filesystem size, or
68 * the upper block count limit (2^32), whichever is lower.
69 */
70 if (ext2fs_blocks_count(sb) < max_blocks / 1024)
71 max_blocks = ext2fs_blocks_count(sb) * 1024;
72 /*
73 * ext2fs_div64_ceil() is unnecessary because max_blocks is
74 * max _GDT_ blocks, which is limited to 32 bits.
75 */
76 rsv_groups = ext2fs_div_ceil(max_blocks - sb->s_first_data_block, bpg);
77 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) - fs->desc_blocks;
78 if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb))
79 rsv_gdb = EXT2_ADDR_PER_BLOCK(sb);
80 #ifdef RES_GDT_DEBUG
81 printf("max_blocks %lu, rsv_groups = %lu, rsv_gdb = %u\n",
82 max_blocks, rsv_groups, rsv_gdb);
83 #endif
84
85 return rsv_gdb;
86 }
87
ext2fs_initialize(const char * name,int flags,struct ext2_super_block * param,io_manager manager,ext2_filsys * ret_fs)88 errcode_t ext2fs_initialize(const char *name, int flags,
89 struct ext2_super_block *param,
90 io_manager manager, ext2_filsys *ret_fs)
91 {
92 ext2_filsys fs;
93 errcode_t retval;
94 struct ext2_super_block *super;
95 unsigned int rem;
96 unsigned int overhead = 0;
97 unsigned int ipg;
98 dgrp_t i;
99 blk64_t free_blocks;
100 blk_t numblocks;
101 int rsv_gdt;
102 int csum_flag;
103 int bigalloc_flag;
104 int io_flags;
105 int has_bg;
106 unsigned reserved_inos;
107 char *buf = 0;
108 char c;
109 double reserved_ratio;
110 char *time_env;
111
112 if (!param || !ext2fs_blocks_count(param))
113 return EXT2_ET_INVALID_ARGUMENT;
114
115 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
116 if (retval)
117 return retval;
118
119 memset(fs, 0, sizeof(struct struct_ext2_filsys));
120 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
121 fs->flags = flags | EXT2_FLAG_RW;
122 fs->umask = 022;
123 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
124 #ifdef WORDS_BIGENDIAN
125 fs->flags |= EXT2_FLAG_SWAP_BYTES;
126 #endif
127
128 time_env = getenv("E2FSPROGS_FAKE_TIME");
129 if (time_env)
130 fs->now = strtoul(time_env, NULL, 0);
131
132 io_flags = IO_FLAG_RW;
133 if (flags & EXT2_FLAG_EXCLUSIVE)
134 io_flags |= IO_FLAG_EXCLUSIVE;
135 if (flags & EXT2_FLAG_DIRECT_IO)
136 io_flags |= IO_FLAG_DIRECT_IO;
137 io_flags |= O_BINARY;
138 retval = manager->open(name, io_flags, &fs->io);
139 if (retval)
140 goto cleanup;
141 fs->image_io = fs->io;
142 fs->io->app_data = fs;
143 retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
144 if (retval)
145 goto cleanup;
146
147 strcpy(fs->device_name, name);
148 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super);
149 if (retval)
150 goto cleanup;
151 fs->super = super;
152
153 memset(super, 0, SUPERBLOCK_SIZE);
154
155 #define set_field(field, default) (super->field = param->field ? \
156 param->field : (default))
157 #define assign_field(field) (super->field = param->field)
158
159 super->s_magic = EXT2_SUPER_MAGIC;
160 super->s_state = EXT2_VALID_FS;
161
162 bigalloc_flag = ext2fs_has_feature_bigalloc(param);
163
164 assign_field(s_log_block_size);
165
166 if (bigalloc_flag) {
167 set_field(s_log_cluster_size, super->s_log_block_size+4);
168 if (super->s_log_block_size > super->s_log_cluster_size) {
169 retval = EXT2_ET_INVALID_ARGUMENT;
170 goto cleanup;
171 }
172 } else
173 super->s_log_cluster_size = super->s_log_block_size;
174
175 set_field(s_first_data_block, super->s_log_cluster_size ? 0 : 1);
176 set_field(s_max_mnt_count, 0);
177 set_field(s_errors, EXT2_ERRORS_DEFAULT);
178 set_field(s_feature_compat, 0);
179 set_field(s_feature_incompat, 0);
180 set_field(s_feature_ro_compat, 0);
181 set_field(s_default_mount_opts, 0);
182 set_field(s_first_meta_bg, 0);
183 set_field(s_raid_stride, 0); /* default stride size: 0 */
184 set_field(s_raid_stripe_width, 0); /* default stripe width: 0 */
185 set_field(s_log_groups_per_flex, 0);
186 set_field(s_flags, 0);
187 assign_field(s_backup_bgs[0]);
188 assign_field(s_backup_bgs[1]);
189
190 assign_field(s_encoding);
191 assign_field(s_encoding_flags);
192
193 if (ext2fs_has_feature_casefold(param))
194 fs->encoding = ext2fs_load_nls_table(param->s_encoding);
195
196 if (super->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
197 retval = EXT2_ET_UNSUPP_FEATURE;
198 goto cleanup;
199 }
200 if (super->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
201 retval = EXT2_ET_RO_UNSUPP_FEATURE;
202 goto cleanup;
203 }
204
205 set_field(s_rev_level, EXT2_GOOD_OLD_REV);
206 if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
207 set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
208 set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
209 if (super->s_inode_size >= sizeof(struct ext2_inode_large)) {
210 int extra_isize = sizeof(struct ext2_inode_large) -
211 EXT2_GOOD_OLD_INODE_SIZE;
212 set_field(s_min_extra_isize, extra_isize);
213 set_field(s_want_extra_isize, extra_isize);
214 }
215 } else {
216 super->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
217 super->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
218 }
219
220 set_field(s_checkinterval, 0);
221 super->s_mkfs_time = super->s_lastcheck = fs->now ? fs->now : time(NULL);
222
223 super->s_creator_os = CREATOR_OS;
224
225 fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(super);
226 fs->cluster_ratio_bits = super->s_log_cluster_size -
227 super->s_log_block_size;
228
229 if (bigalloc_flag) {
230 unsigned long long bpg;
231
232 if (param->s_blocks_per_group &&
233 param->s_clusters_per_group &&
234 ((param->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs)) !=
235 param->s_blocks_per_group)) {
236 retval = EXT2_ET_INVALID_ARGUMENT;
237 goto cleanup;
238 }
239 if (param->s_clusters_per_group)
240 assign_field(s_clusters_per_group);
241 else if (param->s_blocks_per_group)
242 super->s_clusters_per_group =
243 param->s_blocks_per_group /
244 EXT2FS_CLUSTER_RATIO(fs);
245 else if (super->s_log_cluster_size + 15 < 32)
246 super->s_clusters_per_group = fs->blocksize * 8;
247 else
248 super->s_clusters_per_group = (fs->blocksize - 1) * 8;
249 if (super->s_clusters_per_group > EXT2_MAX_CLUSTERS_PER_GROUP(super))
250 super->s_clusters_per_group = EXT2_MAX_CLUSTERS_PER_GROUP(super);
251 bpg = EXT2FS_C2B(fs,
252 (unsigned long long) super->s_clusters_per_group);
253 if (bpg >= (((unsigned long long) 1) << 32)) {
254 retval = EXT2_ET_INVALID_ARGUMENT;
255 goto cleanup;
256 }
257 super->s_blocks_per_group = bpg;
258 } else {
259 set_field(s_blocks_per_group, fs->blocksize * 8);
260 if (super->s_blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(super))
261 super->s_blocks_per_group = EXT2_MAX_BLOCKS_PER_GROUP(super);
262 super->s_clusters_per_group = super->s_blocks_per_group;
263 }
264
265 ext2fs_blocks_count_set(super, ext2fs_blocks_count(param) &
266 ~((blk64_t) EXT2FS_CLUSTER_MASK(fs)));
267 ext2fs_r_blocks_count_set(super, ext2fs_r_blocks_count(param));
268 if (ext2fs_r_blocks_count(super) >= ext2fs_blocks_count(param)) {
269 retval = EXT2_ET_INVALID_ARGUMENT;
270 goto cleanup;
271 }
272
273 set_field(s_mmp_update_interval, 0);
274
275 /*
276 * If we're creating an external journal device, we don't need
277 * to bother with the rest.
278 */
279 if (ext2fs_has_feature_journal_dev(super)) {
280 fs->group_desc_count = 0;
281 ext2fs_mark_super_dirty(fs);
282 *ret_fs = fs;
283 return 0;
284 }
285
286 retry:
287 fs->group_desc_count = (dgrp_t) ext2fs_div64_ceil(
288 ext2fs_blocks_count(super) - super->s_first_data_block,
289 EXT2_BLOCKS_PER_GROUP(super));
290 if (fs->group_desc_count == 0) {
291 retval = EXT2_ET_TOOSMALL;
292 goto cleanup;
293 }
294
295 set_field(s_desc_size,
296 ext2fs_has_feature_64bit(super) ?
297 EXT2_MIN_DESC_SIZE_64BIT : 0);
298
299 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
300 EXT2_DESC_PER_BLOCK(super));
301
302 i = fs->blocksize >= 4096 ? 1 : 4096 / fs->blocksize;
303
304 if (ext2fs_has_feature_64bit(super) &&
305 (ext2fs_blocks_count(super) / i) >= (1ULL << 32))
306 set_field(s_inodes_count, ~0U);
307 else
308 set_field(s_inodes_count, ext2fs_blocks_count(super) / i);
309
310 /*
311 * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
312 * that we have enough inodes for the filesystem(!)
313 */
314 if (super->s_inodes_count < EXT2_FIRST_INODE(super)+1)
315 super->s_inodes_count = EXT2_FIRST_INODE(super)+1;
316
317 /*
318 * There should be at least as many inodes as the user
319 * requested. Figure out how many inodes per group that
320 * should be. But make sure that we don't allocate more than
321 * one bitmap's worth of inodes each group.
322 */
323 ipg = ext2fs_div_ceil(super->s_inodes_count, fs->group_desc_count);
324 if (ipg > fs->blocksize * 8) {
325 if (!bigalloc_flag && super->s_blocks_per_group >= 256) {
326 /* Try again with slightly different parameters */
327 super->s_blocks_per_group -= 8;
328 ext2fs_blocks_count_set(super,
329 ext2fs_blocks_count(param));
330 super->s_clusters_per_group = super->s_blocks_per_group;
331 goto retry;
332 } else {
333 retval = EXT2_ET_TOO_MANY_INODES;
334 goto cleanup;
335 }
336 }
337
338 if (ipg > (unsigned) EXT2_MAX_INODES_PER_GROUP(super))
339 ipg = EXT2_MAX_INODES_PER_GROUP(super);
340
341 ipg_retry:
342 super->s_inodes_per_group = ipg;
343
344 /*
345 * Make sure the number of inodes per group completely fills
346 * the inode table blocks in the descriptor. If not, add some
347 * additional inodes/group. Waste not, want not...
348 */
349 fs->inode_blocks_per_group = (((super->s_inodes_per_group *
350 EXT2_INODE_SIZE(super)) +
351 EXT2_BLOCK_SIZE(super) - 1) /
352 EXT2_BLOCK_SIZE(super));
353 super->s_inodes_per_group = ((fs->inode_blocks_per_group *
354 EXT2_BLOCK_SIZE(super)) /
355 EXT2_INODE_SIZE(super));
356 /*
357 * Finally, make sure the number of inodes per group is a
358 * multiple of 8. This is needed to simplify the bitmap
359 * splicing code.
360 */
361 if (super->s_inodes_per_group < 8)
362 super->s_inodes_per_group = 8;
363 super->s_inodes_per_group &= ~7;
364 fs->inode_blocks_per_group = (((super->s_inodes_per_group *
365 EXT2_INODE_SIZE(super)) +
366 EXT2_BLOCK_SIZE(super) - 1) /
367 EXT2_BLOCK_SIZE(super));
368
369 /*
370 * adjust inode count to reflect the adjusted inodes_per_group
371 */
372 if ((__u64)super->s_inodes_per_group * fs->group_desc_count > ~0U) {
373 ipg--;
374 goto ipg_retry;
375 }
376 super->s_inodes_count = super->s_inodes_per_group *
377 fs->group_desc_count;
378 super->s_free_inodes_count = super->s_inodes_count;
379
380 /*
381 * check the number of reserved group descriptor table blocks
382 */
383 if (ext2fs_has_feature_resize_inode(super))
384 rsv_gdt = calc_reserved_gdt_blocks(fs);
385 else
386 rsv_gdt = 0;
387 set_field(s_reserved_gdt_blocks, rsv_gdt);
388 if (super->s_reserved_gdt_blocks > EXT2_ADDR_PER_BLOCK(super)) {
389 retval = EXT2_ET_RES_GDT_BLOCKS;
390 goto cleanup;
391 }
392 /* Enable meta_bg if we'd lose more than 3/4 of a BG to GDT blocks. */
393 if (super->s_reserved_gdt_blocks + fs->desc_blocks >
394 super->s_blocks_per_group * 3 / 4) {
395 ext2fs_set_feature_meta_bg(fs->super);
396 ext2fs_clear_feature_resize_inode(fs->super);
397 set_field(s_reserved_gdt_blocks, 0);
398 }
399
400 /*
401 * Calculate the maximum number of bookkeeping blocks per
402 * group. It includes the superblock, the block group
403 * descriptors, the block bitmap, the inode bitmap, the inode
404 * table, and the reserved gdt blocks.
405 */
406 overhead = (int) (3 + fs->inode_blocks_per_group +
407 super->s_reserved_gdt_blocks);
408
409 if (ext2fs_has_feature_meta_bg(fs->super))
410 overhead++;
411 else
412 overhead += fs->desc_blocks;
413
414 /* This can only happen if the user requested too many inodes */
415 if (overhead > super->s_blocks_per_group) {
416 retval = EXT2_ET_TOO_MANY_INODES;
417 goto cleanup;
418 }
419
420 /*
421 * See if the last group is big enough to support the
422 * necessary data structures. If not, we need to get rid of
423 * it. We need to recalculate the overhead for the last block
424 * group, since it might or might not have a superblock
425 * backup.
426 */
427 overhead = (int) (2 + fs->inode_blocks_per_group);
428 has_bg = 0;
429 if (ext2fs_has_feature_sparse_super2(super)) {
430 /*
431 * We have to do this manually since
432 * super->s_backup_bgs hasn't been set up yet.
433 */
434 if (fs->group_desc_count == 2)
435 has_bg = param->s_backup_bgs[0] != 0;
436 else
437 has_bg = param->s_backup_bgs[1] != 0;
438 } else
439 has_bg = ext2fs_bg_has_super(fs, fs->group_desc_count - 1);
440 if (has_bg)
441 overhead += 1 + fs->desc_blocks + super->s_reserved_gdt_blocks;
442 rem = ((ext2fs_blocks_count(super) - super->s_first_data_block) %
443 super->s_blocks_per_group);
444 if ((fs->group_desc_count == 1) && rem && (rem < overhead)) {
445 retval = EXT2_ET_TOOSMALL;
446 goto cleanup;
447 }
448 if (rem && (rem < overhead+50)) {
449 ext2fs_blocks_count_set(super, ext2fs_blocks_count(super) -
450 rem);
451 /*
452 * If blocks count is changed, we need to recalculate
453 * reserved blocks count not to exceed 50%.
454 */
455 reserved_ratio = 100.0 * ext2fs_r_blocks_count(param) /
456 ext2fs_blocks_count(param);
457 ext2fs_r_blocks_count_set(super, reserved_ratio *
458 ext2fs_blocks_count(super) / 100.0);
459
460 goto retry;
461 }
462
463 /*
464 * At this point we know how big the filesystem will be. So
465 * we can do any and all allocations that depend on the block
466 * count.
467 */
468
469 /* Set up the locations of the backup superblocks */
470 if (ext2fs_has_feature_sparse_super2(super)) {
471 if (super->s_backup_bgs[0] >= fs->group_desc_count)
472 super->s_backup_bgs[0] = fs->group_desc_count - 1;
473 if (super->s_backup_bgs[1] >= fs->group_desc_count)
474 super->s_backup_bgs[1] = fs->group_desc_count - 1;
475 if (super->s_backup_bgs[0] == super->s_backup_bgs[1])
476 super->s_backup_bgs[1] = 0;
477 if (super->s_backup_bgs[0] > super->s_backup_bgs[1]) {
478 __u32 t = super->s_backup_bgs[0];
479 super->s_backup_bgs[0] = super->s_backup_bgs[1];
480 super->s_backup_bgs[1] = t;
481 }
482 }
483
484 retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
485 if (retval)
486 goto cleanup;
487
488 strcpy(buf, "block bitmap for ");
489 strcat(buf, fs->device_name);
490 retval = ext2fs_allocate_subcluster_bitmap(fs, buf, &fs->block_map);
491 if (retval)
492 goto cleanup;
493
494 strcpy(buf, "inode bitmap for ");
495 strcat(buf, fs->device_name);
496 retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
497 if (retval)
498 goto cleanup;
499
500 ext2fs_free_mem(&buf);
501
502 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
503 &fs->group_desc);
504 if (retval)
505 goto cleanup;
506
507 memset(fs->group_desc, 0, (size_t) fs->desc_blocks * fs->blocksize);
508
509 /*
510 * Reserve the superblock and group descriptors for each
511 * group, and fill in the correct group statistics for group.
512 * Note that although the block bitmap, inode bitmap, and
513 * inode table have not been allocated (and in fact won't be
514 * by this routine), they are accounted for nevertheless.
515 *
516 * If FLEX_BG meta-data grouping is used, only account for the
517 * superblock and group descriptors (the inode tables and
518 * bitmaps will be accounted for when allocated).
519 */
520 free_blocks = 0;
521 csum_flag = ext2fs_has_group_desc_csum(fs);
522 reserved_inos = super->s_first_ino;
523 for (i = 0; i < fs->group_desc_count; i++) {
524 /*
525 * Don't set the BLOCK_UNINIT group for the last group
526 * because the block bitmap needs to be padded.
527 */
528 if (csum_flag) {
529 if (i != fs->group_desc_count - 1)
530 ext2fs_bg_flags_set(fs, i,
531 EXT2_BG_BLOCK_UNINIT);
532 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
533 numblocks = super->s_inodes_per_group;
534 if (reserved_inos) {
535 if (numblocks > reserved_inos) {
536 numblocks -= reserved_inos;
537 reserved_inos = 0;
538 } else {
539 reserved_inos -= numblocks;
540 numblocks = 0;
541 }
542 }
543 ext2fs_bg_itable_unused_set(fs, i, numblocks);
544 }
545 numblocks = ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
546 if (fs->super->s_log_groups_per_flex)
547 numblocks += 2 + fs->inode_blocks_per_group;
548
549 free_blocks += numblocks;
550 ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
551 ext2fs_bg_free_inodes_count_set(fs, i, fs->super->s_inodes_per_group);
552 ext2fs_bg_used_dirs_count_set(fs, i, 0);
553 ext2fs_group_desc_csum_set(fs, i);
554 }
555 free_blocks &= ~EXT2FS_CLUSTER_MASK(fs);
556 ext2fs_free_blocks_count_set(super, free_blocks);
557
558 c = (char) 255;
559 if (((int) c) == -1) {
560 super->s_flags |= EXT2_FLAGS_SIGNED_HASH;
561 } else {
562 super->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
563 }
564
565 ext2fs_mark_super_dirty(fs);
566 ext2fs_mark_bb_dirty(fs);
567 ext2fs_mark_ib_dirty(fs);
568
569 io_channel_set_blksize(fs->io, fs->blocksize);
570
571 *ret_fs = fs;
572 return 0;
573 cleanup:
574 free(buf);
575 ext2fs_free(fs);
576 return retval;
577 }
578
ext2fs_calculate_summary_stats(ext2_filsys fs,int super_only)579 errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs, int super_only)
580 {
581 blk64_t blk;
582 ext2_ino_t ino;
583 unsigned int group = 0;
584 unsigned int count = 0;
585 int total_free = 0;
586 int group_free = 0;
587 int last_allocated = 0;
588 int uninit;
589
590 /*
591 * First calculate the block statistics
592 */
593 uninit = 1;
594 for (blk = fs->super->s_first_data_block;
595 blk < ext2fs_blocks_count(fs->super); blk++) {
596 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
597 group_free++;
598 total_free++;
599 } else {
600 uninit = 0;
601 }
602 count++;
603 if ((count == fs->super->s_blocks_per_group) ||
604 (blk == ext2fs_blocks_count(fs->super)-1)) {
605 ext2fs_bg_free_blocks_count_set(fs, group,
606 group_free);
607 if (!super_only) {
608 if (uninit && blk !=
609 ext2fs_blocks_count(fs->super) - 1)
610 ext2fs_bg_flags_set(fs, group,
611 EXT2_BG_BLOCK_UNINIT);
612 else
613 ext2fs_bg_flags_clear(fs, group,
614 EXT2_BG_BLOCK_UNINIT);
615 }
616 count = 0;
617 group_free = 0;
618 uninit = 1;
619 group++;
620 }
621 }
622 total_free = EXT2FS_C2B(fs, total_free);
623 ext2fs_free_blocks_count_set(fs->super, total_free);
624
625 /*
626 * Next, calculate the inode statistics
627 */
628 group_free = 0;
629 total_free = 0;
630 last_allocated = 0;
631 count = 0;
632 group = 0;
633
634 /* Protect loop from wrap-around if s_inodes_count maxed */
635 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
636 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
637 group_free++;
638 total_free++;
639 } else {
640 last_allocated = ino;
641 }
642 count++;
643 if ((count == fs->super->s_inodes_per_group) ||
644 (ino == fs->super->s_inodes_count)) {
645 if (!super_only) {
646 if (last_allocated) {
647 ext2fs_bg_flags_clear(fs, group,
648 EXT2_BG_INODE_UNINIT);
649 ext2fs_bg_itable_unused_set(fs, group,
650 fs->super->s_inodes_per_group -
651 (last_allocated %
652 fs->super->s_inodes_per_group));
653 } else {
654 ext2fs_bg_flags_set(fs, group,
655 EXT2_BG_INODE_UNINIT);
656 ext2fs_bg_itable_unused_set(fs, group,
657 0);
658 }
659 ext2fs_bg_free_inodes_count_set(fs, group,
660 group_free);
661 }
662 group++;
663 count = 0;
664 group_free = 0;
665 last_allocated = 0;
666 }
667 }
668 fs->super->s_free_inodes_count = total_free;
669 ext2fs_mark_super_dirty(fs);
670 return 0;
671 }
672