1997-04-26 17:34:30 +04:00
|
|
|
/*
|
|
|
|
* rw_bitmaps.c --- routines to read and write the inode and block bitmaps.
|
|
|
|
*
|
1997-04-29 20:17:09 +04:00
|
|
|
* Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o.
|
|
|
|
*
|
|
|
|
* %Begin-Header%
|
2010-05-18 05:31:56 +04:00
|
|
|
* This file may be redistributed under the terms of the GNU Library
|
|
|
|
* General Public License, version 2.
|
1997-04-29 20:17:09 +04:00
|
|
|
* %End-Header%
|
1997-04-26 17:34:30 +04:00
|
|
|
*/
|
|
|
|
|
2011-09-19 01:34:37 +04:00
|
|
|
#include "config.h"
|
1997-04-26 17:34:30 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1997-08-11 03:07:40 +04:00
|
|
|
#if HAVE_UNISTD_H
|
1997-04-26 17:34:30 +04:00
|
|
|
#include <unistd.h>
|
1997-08-11 03:07:40 +04:00
|
|
|
#endif
|
1997-04-26 17:34:30 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
1997-10-20 04:52:43 +04:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
1997-04-26 17:34:30 +04:00
|
|
|
#include <sys/stat.h>
|
1997-10-20 04:52:43 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
1997-04-26 17:34:30 +04:00
|
|
|
#include <sys/types.h>
|
1997-10-20 04:52:43 +04:00
|
|
|
#endif
|
1997-04-26 17:34:30 +04:00
|
|
|
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
#include "ext2_fs.h"
|
1997-04-26 17:34:30 +04:00
|
|
|
#include "ext2fs.h"
|
2001-05-03 08:02:29 +04:00
|
|
|
#include "e2image.h"
|
1997-04-26 17:34:30 +04:00
|
|
|
|
2006-04-23 20:43:40 +04:00
|
|
|
static errcode_t write_bitmaps(ext2_filsys fs, int do_inode, int do_block)
|
1997-04-26 17:34:30 +04:00
|
|
|
{
|
1998-09-03 05:22:57 +04:00
|
|
|
dgrp_t i;
|
2003-12-07 09:28:50 +03:00
|
|
|
unsigned int j;
|
2006-04-23 20:43:40 +04:00
|
|
|
int block_nbytes, inode_nbytes;
|
2003-12-07 09:28:50 +03:00
|
|
|
unsigned int nbits;
|
1997-04-26 17:34:30 +04:00
|
|
|
errcode_t retval;
|
2011-09-17 00:49:34 +04:00
|
|
|
char *block_buf = NULL, *inode_buf = NULL;
|
2012-08-03 04:47:45 +04:00
|
|
|
int csum_flag;
|
Convert to use block group accessor functions
Convert direct accesses to use the following block group accessor
functions: ext2fs_block_bitmap_loc(), ext2fs_inode_bitmap_loc(),
ext2fs_inode_table_loc(), ext2fs_bg_itable_unused(),
ext2fs_block_bitmap_loc_set(), ext2fs_inode_bitmap_loc_set(),
ext2fs_inode_table_loc_set(), ext2fs_bg_free_inodes_count(),
ext2fs_ext2fs_bg_used_dirs_count(), ext2fs_bg_free_inodes_count_set(),
ext2fs_bg_free_blocks_count_set(), ext2fs_bg_used_dirs_count_set()
Signed-off-by: Valerie Aurora Henson <vaurora@redhat.com>
Signed-off-by: Nick Dokos <nicholas.dokos@hp.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-10-26 04:43:47 +03:00
|
|
|
blk64_t blk;
|
2011-06-05 04:24:36 +04:00
|
|
|
blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
|
2007-07-23 12:32:48 +04:00
|
|
|
ext2_ino_t ino_itr = 1;
|
1997-04-26 17:34:30 +04:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
|
|
|
|
|
|
|
|
if (!(fs->flags & EXT2_FLAG_RW))
|
|
|
|
return EXT2_ET_RO_FILSYS;
|
2008-04-21 07:33:34 +04:00
|
|
|
|
2012-08-03 04:47:45 +04:00
|
|
|
csum_flag = ext2fs_has_group_desc_csum(fs);
|
2008-04-21 07:33:34 +04:00
|
|
|
|
2006-04-23 20:43:40 +04:00
|
|
|
inode_nbytes = block_nbytes = 0;
|
|
|
|
if (do_block) {
|
2011-06-05 04:05:22 +04:00
|
|
|
block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
|
2012-05-07 22:41:49 +04:00
|
|
|
retval = io_channel_alloc_buf(fs->io, 0, &block_buf);
|
2006-04-23 20:43:40 +04:00
|
|
|
if (retval)
|
2011-09-17 00:49:34 +04:00
|
|
|
goto errout;
|
2006-04-23 20:43:40 +04:00
|
|
|
memset(block_buf, 0xff, fs->blocksize);
|
|
|
|
}
|
|
|
|
if (do_inode) {
|
2008-08-28 07:07:54 +04:00
|
|
|
inode_nbytes = (size_t)
|
2006-04-23 20:43:40 +04:00
|
|
|
((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
|
2012-05-07 22:41:49 +04:00
|
|
|
retval = io_channel_alloc_buf(fs->io, 0, &inode_buf);
|
2006-04-23 20:43:40 +04:00
|
|
|
if (retval)
|
2011-09-17 00:49:34 +04:00
|
|
|
goto errout;
|
2006-04-23 20:43:40 +04:00
|
|
|
memset(inode_buf, 0xff, fs->blocksize);
|
|
|
|
}
|
|
|
|
|
1997-04-26 17:34:30 +04:00
|
|
|
for (i = 0; i < fs->group_desc_count; i++) {
|
2007-07-23 12:32:48 +04:00
|
|
|
if (!do_block)
|
2006-04-23 20:43:40 +04:00
|
|
|
goto skip_block_bitmap;
|
2006-05-09 04:17:26 +04:00
|
|
|
|
2009-10-26 04:42:12 +03:00
|
|
|
if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)
|
2009-09-08 05:15:12 +04:00
|
|
|
)
|
2006-05-09 04:17:26 +04:00
|
|
|
goto skip_this_block_bitmap;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2009-08-05 08:27:10 +04:00
|
|
|
retval = ext2fs_get_block_bitmap_range2(fs->block_map,
|
2007-07-23 12:32:48 +04:00
|
|
|
blk_itr, block_nbytes << 3, block_buf);
|
|
|
|
if (retval)
|
2011-09-17 00:49:34 +04:00
|
|
|
goto errout;
|
2007-07-23 12:32:48 +04:00
|
|
|
|
1997-04-26 17:34:30 +04:00
|
|
|
if (i == fs->group_desc_count - 1) {
|
|
|
|
/* Force bitmap padding for the last group */
|
2011-06-05 04:05:22 +04:00
|
|
|
nbits = EXT2FS_NUM_B2C(fs,
|
2011-06-05 04:24:36 +04:00
|
|
|
((ext2fs_blocks_count(fs->super)
|
2009-09-08 04:46:34 +04:00
|
|
|
- (__u64) fs->super->s_first_data_block)
|
2011-06-05 04:24:36 +04:00
|
|
|
% (__u64) EXT2_BLOCKS_PER_GROUP(fs->super)));
|
1997-04-26 17:34:30 +04:00
|
|
|
if (nbits)
|
|
|
|
for (j = nbits; j < fs->blocksize * 8; j++)
|
2006-04-23 20:43:40 +04:00
|
|
|
ext2fs_set_bit(j, block_buf);
|
1997-04-26 17:34:30 +04:00
|
|
|
}
|
2012-07-31 03:04:04 +04:00
|
|
|
|
|
|
|
retval = ext2fs_block_bitmap_csum_set(fs, i, block_buf,
|
|
|
|
block_nbytes);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
ext2fs_group_desc_csum_set(fs, i);
|
|
|
|
|
Convert to use block group accessor functions
Convert direct accesses to use the following block group accessor
functions: ext2fs_block_bitmap_loc(), ext2fs_inode_bitmap_loc(),
ext2fs_inode_table_loc(), ext2fs_bg_itable_unused(),
ext2fs_block_bitmap_loc_set(), ext2fs_inode_bitmap_loc_set(),
ext2fs_inode_table_loc_set(), ext2fs_bg_free_inodes_count(),
ext2fs_ext2fs_bg_used_dirs_count(), ext2fs_bg_free_inodes_count_set(),
ext2fs_bg_free_blocks_count_set(), ext2fs_bg_used_dirs_count_set()
Signed-off-by: Valerie Aurora Henson <vaurora@redhat.com>
Signed-off-by: Nick Dokos <nicholas.dokos@hp.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-10-26 04:43:47 +03:00
|
|
|
blk = ext2fs_block_bitmap_loc(fs, i);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (blk) {
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_write_blk64(fs->io, blk, 1,
|
|
|
|
block_buf);
|
2011-09-17 00:49:34 +04:00
|
|
|
if (retval) {
|
|
|
|
retval = EXT2_ET_BLOCK_BITMAP_WRITE;
|
|
|
|
goto errout;
|
|
|
|
}
|
1997-04-29 20:17:09 +04:00
|
|
|
}
|
2006-05-09 04:17:26 +04:00
|
|
|
skip_this_block_bitmap:
|
2007-07-23 12:32:48 +04:00
|
|
|
blk_itr += block_nbytes << 3;
|
2006-04-23 20:43:40 +04:00
|
|
|
skip_block_bitmap:
|
|
|
|
|
2007-07-23 12:32:48 +04:00
|
|
|
if (!do_inode)
|
2006-04-23 20:43:40 +04:00
|
|
|
continue;
|
|
|
|
|
2009-10-26 04:42:12 +03:00
|
|
|
if (csum_flag && ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)
|
2009-09-08 05:15:12 +04:00
|
|
|
)
|
2006-05-09 04:17:26 +04:00
|
|
|
goto skip_this_inode_bitmap;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2009-08-05 08:27:10 +04:00
|
|
|
retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
|
2007-07-23 12:32:48 +04:00
|
|
|
ino_itr, inode_nbytes << 3, inode_buf);
|
|
|
|
if (retval)
|
2011-09-17 00:49:34 +04:00
|
|
|
goto errout;
|
2007-07-23 12:32:48 +04:00
|
|
|
|
2012-07-31 02:56:04 +04:00
|
|
|
retval = ext2fs_inode_bitmap_csum_set(fs, i, inode_buf,
|
|
|
|
inode_nbytes);
|
|
|
|
if (retval)
|
|
|
|
goto errout;
|
|
|
|
ext2fs_group_desc_csum_set(fs, i);
|
|
|
|
|
Convert to use block group accessor functions
Convert direct accesses to use the following block group accessor
functions: ext2fs_block_bitmap_loc(), ext2fs_inode_bitmap_loc(),
ext2fs_inode_table_loc(), ext2fs_bg_itable_unused(),
ext2fs_block_bitmap_loc_set(), ext2fs_inode_bitmap_loc_set(),
ext2fs_inode_table_loc_set(), ext2fs_bg_free_inodes_count(),
ext2fs_ext2fs_bg_used_dirs_count(), ext2fs_bg_free_inodes_count_set(),
ext2fs_bg_free_blocks_count_set(), ext2fs_bg_used_dirs_count_set()
Signed-off-by: Valerie Aurora Henson <vaurora@redhat.com>
Signed-off-by: Nick Dokos <nicholas.dokos@hp.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-10-26 04:43:47 +03:00
|
|
|
blk = ext2fs_inode_bitmap_loc(fs, i);
|
2006-04-23 20:43:40 +04:00
|
|
|
if (blk) {
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_write_blk64(fs->io, blk, 1,
|
2006-04-23 20:43:40 +04:00
|
|
|
inode_buf);
|
2011-09-17 00:49:34 +04:00
|
|
|
if (retval) {
|
|
|
|
retval = EXT2_ET_INODE_BITMAP_WRITE;
|
|
|
|
goto errout;
|
|
|
|
}
|
2006-04-23 20:43:40 +04:00
|
|
|
}
|
2006-05-09 04:17:26 +04:00
|
|
|
skip_this_inode_bitmap:
|
2007-07-23 12:32:48 +04:00
|
|
|
ino_itr += inode_nbytes << 3;
|
2006-04-23 20:43:40 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
if (do_block) {
|
|
|
|
fs->flags &= ~EXT2_FLAG_BB_DIRTY;
|
|
|
|
ext2fs_free_mem(&block_buf);
|
|
|
|
}
|
|
|
|
if (do_inode) {
|
|
|
|
fs->flags &= ~EXT2_FLAG_IB_DIRTY;
|
|
|
|
ext2fs_free_mem(&inode_buf);
|
1997-04-26 17:34:30 +04:00
|
|
|
}
|
|
|
|
return 0;
|
2011-09-17 00:49:34 +04:00
|
|
|
errout:
|
|
|
|
if (inode_buf)
|
|
|
|
ext2fs_free_mem(&inode_buf);
|
|
|
|
if (block_buf)
|
|
|
|
ext2fs_free_mem(&block_buf);
|
|
|
|
return retval;
|
1997-04-26 17:34:30 +04:00
|
|
|
}
|
|
|
|
|
libext2fs: mark group data blocks when loading block bitmap
The kernel[1] and e2fsck[2] both react to a BLOCK_UNINIT group by
calculating the block bitmap that's needed to show all the group
blocks for that group (if any) and using that. However, when reading
bitmaps from disk, libext2fs simply imports a block of zeroes into the
bitmap, without bothering to check for group blocks. This erroneous
behavior results in the filesystem having a block bitmap that does not
accurately reflect disk contents, and worse yet makes it seem as
though superblocks, group descriptors, bitmaps, and inode tables are
"free" space on disk.
So, fix the block bitmap loading routines to calculate the correct
block bitmap for all groups and load it into the main fs block bitmap.
This also fixes bogus debugfs output such as:
Group 1: (Blocks 8193-16384) [INODE_UNINIT, BLOCK_UNINIT]
Checksum 0x1310, unused inodes 512
Backup superblock at 8193, Group descriptors at 8194-8217
Reserved GDT blocks at 8218-8473
Block bitmap at 283 (bg #0 + 282), Inode bitmap at 299 (bg #0 + 298)
Inode table at 442-569 (bg #0 + 441)
7911 free blocks, 512 free inodes, 0 directories, 512 unused inodes
Free blocks: 8193-16384
Free inodes: 513-1024
Notice how the "free blocks" range includes the backup sb & GDT area
and doesn't match the free block count.
Worse yet, debugfs' testb command will report those group descriptor
blocks as not being in use unless the user also instructs debugfs to
find a free block first. That is a rather surprising result:
debugfs: testb 8194
Block 8194 not in use
debugfs: ffb 1 16380
Free blocks found: 16380
debugfs: testb 8194
Block 8194 marked in use
Also, remove the part of check_block_uninit() that "fixes" the bitmap
since we're doing that at bitmap load time now.
[1] kernel git 717d50e4971b81b96c0199c91cdf0039a8cb181a
"Ext4: Uninitialized Block Groups"
[2] e2fsprogs git f5fa20078bfc05b554294fe9c5505375d7913e8c
"Add support for EXT2_FEATURE_COMPAT_LAZY_BG"
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-01-11 23:04:48 +04:00
|
|
|
static errcode_t mark_uninit_bg_group_blocks(ext2_filsys fs)
|
|
|
|
{
|
|
|
|
dgrp_t i;
|
|
|
|
blk64_t blk;
|
|
|
|
ext2fs_block_bitmap bmap = fs->block_map;
|
|
|
|
|
|
|
|
for (i = 0; i < fs->group_desc_count; i++) {
|
|
|
|
if (!ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ext2fs_reserve_super_and_bgd(fs, i, bmap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark the blocks used for the inode table
|
|
|
|
*/
|
|
|
|
blk = ext2fs_inode_table_loc(fs, i);
|
|
|
|
if (blk)
|
|
|
|
ext2fs_mark_block_bitmap_range2(bmap, blk,
|
|
|
|
fs->inode_blocks_per_group);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark block used for the block bitmap
|
|
|
|
*/
|
|
|
|
blk = ext2fs_block_bitmap_loc(fs, i);
|
|
|
|
if (blk)
|
|
|
|
ext2fs_mark_block_bitmap2(bmap, blk);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark block used for the inode bitmap
|
|
|
|
*/
|
|
|
|
blk = ext2fs_inode_bitmap_loc(fs, i);
|
|
|
|
if (blk)
|
|
|
|
ext2fs_mark_block_bitmap2(bmap, blk);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-04-26 17:34:30 +04:00
|
|
|
static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
|
|
|
|
{
|
1998-09-03 05:22:57 +04:00
|
|
|
dgrp_t i;
|
1997-04-26 17:58:21 +04:00
|
|
|
char *block_bitmap = 0, *inode_bitmap = 0;
|
1997-04-26 17:34:30 +04:00
|
|
|
char *buf;
|
|
|
|
errcode_t retval;
|
2011-06-05 04:05:22 +04:00
|
|
|
int block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
|
2008-08-22 01:56:44 +04:00
|
|
|
int inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
|
2012-08-03 04:47:45 +04:00
|
|
|
int csum_flag;
|
2007-07-23 12:32:48 +04:00
|
|
|
unsigned int cnt;
|
Convert to use block group accessor functions
Convert direct accesses to use the following block group accessor
functions: ext2fs_block_bitmap_loc(), ext2fs_inode_bitmap_loc(),
ext2fs_inode_table_loc(), ext2fs_bg_itable_unused(),
ext2fs_block_bitmap_loc_set(), ext2fs_inode_bitmap_loc_set(),
ext2fs_inode_table_loc_set(), ext2fs_bg_free_inodes_count(),
ext2fs_ext2fs_bg_used_dirs_count(), ext2fs_bg_free_inodes_count_set(),
ext2fs_bg_free_blocks_count_set(), ext2fs_bg_used_dirs_count_set()
Signed-off-by: Valerie Aurora Henson <vaurora@redhat.com>
Signed-off-by: Nick Dokos <nicholas.dokos@hp.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-10-26 04:43:47 +03:00
|
|
|
blk64_t blk;
|
2011-06-05 04:24:36 +04:00
|
|
|
blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
|
Convert to use block group accessor functions
Convert direct accesses to use the following block group accessor
functions: ext2fs_block_bitmap_loc(), ext2fs_inode_bitmap_loc(),
ext2fs_inode_table_loc(), ext2fs_bg_itable_unused(),
ext2fs_block_bitmap_loc_set(), ext2fs_inode_bitmap_loc_set(),
ext2fs_inode_table_loc_set(), ext2fs_bg_free_inodes_count(),
ext2fs_ext2fs_bg_used_dirs_count(), ext2fs_bg_free_inodes_count_set(),
ext2fs_bg_free_blocks_count_set(), ext2fs_bg_used_dirs_count_set()
Signed-off-by: Valerie Aurora Henson <vaurora@redhat.com>
Signed-off-by: Nick Dokos <nicholas.dokos@hp.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-10-26 04:43:47 +03:00
|
|
|
blk64_t blk_cnt;
|
2007-07-23 12:32:48 +04:00
|
|
|
ext2_ino_t ino_itr = 1;
|
|
|
|
ext2_ino_t ino_cnt;
|
1997-04-26 17:34:30 +04:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
|
|
|
|
|
2013-05-20 02:50:10 +04:00
|
|
|
if ((block_nbytes > (int) fs->blocksize) ||
|
|
|
|
(inode_nbytes > (int) fs->blocksize))
|
2012-05-07 22:30:31 +04:00
|
|
|
return EXT2_ET_CORRUPT_SUPERBLOCK;
|
|
|
|
|
1997-04-26 17:34:30 +04:00
|
|
|
fs->write_bitmaps = ext2fs_write_bitmaps;
|
|
|
|
|
2012-08-03 04:47:45 +04:00
|
|
|
csum_flag = ext2fs_has_group_desc_csum(fs);
|
2006-05-09 04:17:26 +04:00
|
|
|
|
2003-08-01 17:41:07 +04:00
|
|
|
retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
|
1997-10-26 06:41:24 +03:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
1997-04-26 17:34:30 +04:00
|
|
|
if (do_block) {
|
|
|
|
if (fs->block_map)
|
|
|
|
ext2fs_free_block_bitmap(fs->block_map);
|
2008-08-22 18:37:18 +04:00
|
|
|
strcpy(buf, "block bitmap for ");
|
|
|
|
strcat(buf, fs->device_name);
|
1997-04-26 17:34:30 +04:00
|
|
|
retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
|
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
2012-05-07 22:41:49 +04:00
|
|
|
retval = io_channel_alloc_buf(fs->io, 0, &block_bitmap);
|
2007-07-23 12:32:48 +04:00
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
|
|
|
} else
|
|
|
|
block_nbytes = 0;
|
1997-04-26 17:34:30 +04:00
|
|
|
if (do_inode) {
|
|
|
|
if (fs->inode_map)
|
|
|
|
ext2fs_free_inode_bitmap(fs->inode_map);
|
2008-08-22 18:37:18 +04:00
|
|
|
strcpy(buf, "inode bitmap for ");
|
|
|
|
strcat(buf, fs->device_name);
|
1997-04-26 17:34:30 +04:00
|
|
|
retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
|
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
2012-05-07 22:41:49 +04:00
|
|
|
retval = io_channel_alloc_buf(fs->io, 0, &inode_bitmap);
|
2007-07-23 12:32:48 +04:00
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
|
|
|
} else
|
|
|
|
inode_nbytes = 0;
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&buf);
|
1997-04-26 17:34:30 +04:00
|
|
|
|
2001-05-03 08:02:29 +04:00
|
|
|
if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
|
2007-07-23 12:32:48 +04:00
|
|
|
blk = (fs->image_header->offset_inodemap / fs->blocksize);
|
|
|
|
ino_cnt = fs->super->s_inodes_count;
|
|
|
|
while (inode_nbytes > 0) {
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_read_blk64(fs->image_io, blk++,
|
2007-07-23 12:32:48 +04:00
|
|
|
1, inode_bitmap);
|
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
|
|
|
cnt = fs->blocksize << 3;
|
|
|
|
if (cnt > ino_cnt)
|
|
|
|
cnt = ino_cnt;
|
2009-08-05 08:27:10 +04:00
|
|
|
retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
|
2007-07-23 12:32:48 +04:00
|
|
|
ino_itr, cnt, inode_bitmap);
|
2001-05-03 08:02:29 +04:00
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
2007-07-23 12:32:48 +04:00
|
|
|
ino_itr += fs->blocksize << 3;
|
|
|
|
ino_cnt -= fs->blocksize << 3;
|
|
|
|
inode_nbytes -= fs->blocksize;
|
2001-05-03 08:02:29 +04:00
|
|
|
}
|
2007-07-23 12:32:48 +04:00
|
|
|
blk = (fs->image_header->offset_blockmap /
|
|
|
|
fs->blocksize);
|
2014-07-26 15:40:36 +04:00
|
|
|
blk_cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super,
|
|
|
|
fs->group_desc_count);
|
2007-07-23 12:32:48 +04:00
|
|
|
while (block_nbytes > 0) {
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_read_blk64(fs->image_io, blk++,
|
2007-07-23 12:32:48 +04:00
|
|
|
1, block_bitmap);
|
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
|
|
|
cnt = fs->blocksize << 3;
|
|
|
|
if (cnt > blk_cnt)
|
|
|
|
cnt = blk_cnt;
|
2009-08-05 08:27:10 +04:00
|
|
|
retval = ext2fs_set_block_bitmap_range2(fs->block_map,
|
2007-07-23 12:32:48 +04:00
|
|
|
blk_itr, cnt, block_bitmap);
|
2001-05-03 08:02:29 +04:00
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
2007-07-23 12:32:48 +04:00
|
|
|
blk_itr += fs->blocksize << 3;
|
|
|
|
blk_cnt -= fs->blocksize << 3;
|
|
|
|
block_nbytes -= fs->blocksize;
|
2001-05-03 08:02:29 +04:00
|
|
|
}
|
2007-07-23 12:32:48 +04:00
|
|
|
goto success_cleanup;
|
2001-05-03 08:02:29 +04:00
|
|
|
}
|
|
|
|
|
1997-04-26 17:34:30 +04:00
|
|
|
for (i = 0; i < fs->group_desc_count; i++) {
|
1997-04-26 17:58:21 +04:00
|
|
|
if (block_bitmap) {
|
Convert to use block group accessor functions
Convert direct accesses to use the following block group accessor
functions: ext2fs_block_bitmap_loc(), ext2fs_inode_bitmap_loc(),
ext2fs_inode_table_loc(), ext2fs_bg_itable_unused(),
ext2fs_block_bitmap_loc_set(), ext2fs_inode_bitmap_loc_set(),
ext2fs_inode_table_loc_set(), ext2fs_bg_free_inodes_count(),
ext2fs_ext2fs_bg_used_dirs_count(), ext2fs_bg_free_inodes_count_set(),
ext2fs_bg_free_blocks_count_set(), ext2fs_bg_used_dirs_count_set()
Signed-off-by: Valerie Aurora Henson <vaurora@redhat.com>
Signed-off-by: Nick Dokos <nicholas.dokos@hp.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-10-26 04:43:47 +03:00
|
|
|
blk = ext2fs_block_bitmap_loc(fs, i);
|
2009-09-08 05:15:12 +04:00
|
|
|
if (csum_flag &&
|
2009-10-26 04:42:12 +03:00
|
|
|
ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
|
2007-10-22 06:03:25 +04:00
|
|
|
ext2fs_group_desc_csum_verify(fs, i))
|
2006-05-09 04:17:26 +04:00
|
|
|
blk = 0;
|
1997-04-29 20:17:09 +04:00
|
|
|
if (blk) {
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_read_blk64(fs->io, blk,
|
2012-05-07 22:30:31 +04:00
|
|
|
1, block_bitmap);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (retval) {
|
|
|
|
retval = EXT2_ET_BLOCK_BITMAP_READ;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-07-31 03:04:04 +04:00
|
|
|
/* verify block bitmap checksum */
|
|
|
|
if (!(fs->flags &
|
|
|
|
EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
|
|
|
|
!ext2fs_block_bitmap_csum_verify(fs, i,
|
|
|
|
block_bitmap, block_nbytes)) {
|
|
|
|
retval =
|
2013-10-13 07:11:25 +04:00
|
|
|
EXT2_ET_BLOCK_BITMAP_CSUM_INVALID;
|
2012-07-31 03:04:04 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
1997-04-29 20:17:09 +04:00
|
|
|
} else
|
2009-08-10 06:55:55 +04:00
|
|
|
memset(block_bitmap, 0, block_nbytes);
|
2007-07-23 12:32:48 +04:00
|
|
|
cnt = block_nbytes << 3;
|
2009-08-05 08:27:10 +04:00
|
|
|
retval = ext2fs_set_block_bitmap_range2(fs->block_map,
|
2007-07-23 12:32:48 +04:00
|
|
|
blk_itr, cnt, block_bitmap);
|
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
|
|
|
blk_itr += block_nbytes << 3;
|
1997-04-26 17:34:30 +04:00
|
|
|
}
|
1997-04-26 17:58:21 +04:00
|
|
|
if (inode_bitmap) {
|
Convert to use block group accessor functions
Convert direct accesses to use the following block group accessor
functions: ext2fs_block_bitmap_loc(), ext2fs_inode_bitmap_loc(),
ext2fs_inode_table_loc(), ext2fs_bg_itable_unused(),
ext2fs_block_bitmap_loc_set(), ext2fs_inode_bitmap_loc_set(),
ext2fs_inode_table_loc_set(), ext2fs_bg_free_inodes_count(),
ext2fs_ext2fs_bg_used_dirs_count(), ext2fs_bg_free_inodes_count_set(),
ext2fs_bg_free_blocks_count_set(), ext2fs_bg_used_dirs_count_set()
Signed-off-by: Valerie Aurora Henson <vaurora@redhat.com>
Signed-off-by: Nick Dokos <nicholas.dokos@hp.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-10-26 04:43:47 +03:00
|
|
|
blk = ext2fs_inode_bitmap_loc(fs, i);
|
2009-09-08 05:15:12 +04:00
|
|
|
if (csum_flag &&
|
2009-10-26 04:42:12 +03:00
|
|
|
ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) &&
|
2007-10-22 06:03:25 +04:00
|
|
|
ext2fs_group_desc_csum_verify(fs, i))
|
2006-05-09 04:17:26 +04:00
|
|
|
blk = 0;
|
1997-04-29 20:17:09 +04:00
|
|
|
if (blk) {
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_read_blk64(fs->io, blk,
|
2012-05-07 22:30:31 +04:00
|
|
|
1, inode_bitmap);
|
1997-04-29 20:17:09 +04:00
|
|
|
if (retval) {
|
|
|
|
retval = EXT2_ET_INODE_BITMAP_READ;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-07-31 02:56:04 +04:00
|
|
|
|
|
|
|
/* verify inode bitmap checksum */
|
|
|
|
if (!(fs->flags &
|
|
|
|
EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
|
|
|
|
!ext2fs_inode_bitmap_csum_verify(fs, i,
|
|
|
|
inode_bitmap, inode_nbytes)) {
|
|
|
|
retval =
|
|
|
|
EXT2_ET_INODE_BITMAP_CSUM_INVALID;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
1997-04-29 20:17:09 +04:00
|
|
|
} else
|
2009-08-10 06:55:55 +04:00
|
|
|
memset(inode_bitmap, 0, inode_nbytes);
|
2007-07-23 12:32:48 +04:00
|
|
|
cnt = inode_nbytes << 3;
|
2009-08-05 08:27:10 +04:00
|
|
|
retval = ext2fs_set_inode_bitmap_range2(fs->inode_map,
|
2007-07-23 12:32:48 +04:00
|
|
|
ino_itr, cnt, inode_bitmap);
|
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
|
|
|
ino_itr += inode_nbytes << 3;
|
1997-04-26 17:34:30 +04:00
|
|
|
}
|
|
|
|
}
|
libext2fs: mark group data blocks when loading block bitmap
The kernel[1] and e2fsck[2] both react to a BLOCK_UNINIT group by
calculating the block bitmap that's needed to show all the group
blocks for that group (if any) and using that. However, when reading
bitmaps from disk, libext2fs simply imports a block of zeroes into the
bitmap, without bothering to check for group blocks. This erroneous
behavior results in the filesystem having a block bitmap that does not
accurately reflect disk contents, and worse yet makes it seem as
though superblocks, group descriptors, bitmaps, and inode tables are
"free" space on disk.
So, fix the block bitmap loading routines to calculate the correct
block bitmap for all groups and load it into the main fs block bitmap.
This also fixes bogus debugfs output such as:
Group 1: (Blocks 8193-16384) [INODE_UNINIT, BLOCK_UNINIT]
Checksum 0x1310, unused inodes 512
Backup superblock at 8193, Group descriptors at 8194-8217
Reserved GDT blocks at 8218-8473
Block bitmap at 283 (bg #0 + 282), Inode bitmap at 299 (bg #0 + 298)
Inode table at 442-569 (bg #0 + 441)
7911 free blocks, 512 free inodes, 0 directories, 512 unused inodes
Free blocks: 8193-16384
Free inodes: 513-1024
Notice how the "free blocks" range includes the backup sb & GDT area
and doesn't match the free block count.
Worse yet, debugfs' testb command will report those group descriptor
blocks as not being in use unless the user also instructs debugfs to
find a free block first. That is a rather surprising result:
debugfs: testb 8194
Block 8194 not in use
debugfs: ffb 1 16380
Free blocks found: 16380
debugfs: testb 8194
Block 8194 marked in use
Also, remove the part of check_block_uninit() that "fixes" the bitmap
since we're doing that at bitmap load time now.
[1] kernel git 717d50e4971b81b96c0199c91cdf0039a8cb181a
"Ext4: Uninitialized Block Groups"
[2] e2fsprogs git f5fa20078bfc05b554294fe9c5505375d7913e8c
"Add support for EXT2_FEATURE_COMPAT_LAZY_BG"
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-01-11 23:04:48 +04:00
|
|
|
|
|
|
|
/* Mark group blocks for any BLOCK_UNINIT groups */
|
|
|
|
if (do_block) {
|
|
|
|
retval = mark_uninit_bg_group_blocks(fs);
|
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2007-07-23 12:32:48 +04:00
|
|
|
success_cleanup:
|
|
|
|
if (inode_bitmap)
|
|
|
|
ext2fs_free_mem(&inode_bitmap);
|
|
|
|
if (block_bitmap)
|
|
|
|
ext2fs_free_mem(&block_bitmap);
|
1997-04-26 17:34:30 +04:00
|
|
|
return 0;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-04-26 17:34:30 +04:00
|
|
|
cleanup:
|
|
|
|
if (do_block) {
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&fs->block_map);
|
1997-04-26 17:34:30 +04:00
|
|
|
fs->block_map = 0;
|
|
|
|
}
|
|
|
|
if (do_inode) {
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&fs->inode_map);
|
1997-04-26 17:34:30 +04:00
|
|
|
fs->inode_map = 0;
|
|
|
|
}
|
2007-07-23 12:32:48 +04:00
|
|
|
if (inode_bitmap)
|
|
|
|
ext2fs_free_mem(&inode_bitmap);
|
|
|
|
if (block_bitmap)
|
|
|
|
ext2fs_free_mem(&block_bitmap);
|
1997-04-26 17:34:30 +04:00
|
|
|
if (buf)
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&buf);
|
1997-04-26 17:34:30 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2006-04-23 20:43:40 +04:00
|
|
|
errcode_t ext2fs_read_inode_bitmap(ext2_filsys fs)
|
1997-04-26 17:34:30 +04:00
|
|
|
{
|
|
|
|
return read_bitmaps(fs, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
|
|
|
|
{
|
|
|
|
return read_bitmaps(fs, 0, 1);
|
|
|
|
}
|
|
|
|
|
2006-04-23 20:43:40 +04:00
|
|
|
errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
|
1997-04-26 17:34:30 +04:00
|
|
|
{
|
2006-04-23 20:43:40 +04:00
|
|
|
return write_bitmaps(fs, 1, 0);
|
|
|
|
}
|
1997-04-26 17:34:30 +04:00
|
|
|
|
2006-04-23 20:43:40 +04:00
|
|
|
errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
|
|
|
|
{
|
|
|
|
return write_bitmaps(fs, 0, 1);
|
|
|
|
}
|
1997-04-26 17:34:30 +04:00
|
|
|
|
2006-04-23 20:43:40 +04:00
|
|
|
errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
|
|
|
|
{
|
1998-02-17 01:00:37 +03:00
|
|
|
if (fs->inode_map && fs->block_map)
|
|
|
|
return 0;
|
|
|
|
|
1997-04-26 17:34:30 +04:00
|
|
|
return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
|
|
|
|
{
|
2006-04-23 20:43:40 +04:00
|
|
|
int do_inode = fs->inode_map && ext2fs_test_ib_dirty(fs);
|
|
|
|
int do_block = fs->block_map && ext2fs_test_bb_dirty(fs);
|
1997-04-26 17:34:30 +04:00
|
|
|
|
2006-04-23 20:43:40 +04:00
|
|
|
if (!do_inode && !do_block)
|
|
|
|
return 0;
|
1997-04-26 17:34:30 +04:00
|
|
|
|
2006-04-23 20:43:40 +04:00
|
|
|
return write_bitmaps(fs, do_inode, do_block);
|
|
|
|
}
|