Commit Graph

1337 Commits (2df733facd99261a4c462f76f72e143e803487d0)

Author SHA1 Message Date
Theodore Ts'o f0996c12d5 Merge branch 'maint' into next
Conflicts:
	lib/ext2fs/alloc.c
	misc/mke2fs.c
2014-01-30 19:05:16 -05:00
Theodore Ts'o c714d02482 Fix up the Makefiles dependencies in lib/ext2fs and lib/quota
Also use angle brackets for the #include of dirpaths.h to avoid the
need to manually massage the Makefile.in for the util directory.  This
is needed because we have to create a fake dirpaths.h file in the util
directory.  The fake dirpaths.h file is rquired to break the circular
dependency caused by util/subst creating dirpaths.h, while
util/subst.c is including config.h, which includes dirpaths.h.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-30 18:48:23 -05:00
Theodore Ts'o 65c6c3e06f Add support for new compat feature "sparse_super2"
In practice, it is **extremely** rare for users to try to use more
than the first backup superblock located at the beginning of block
group #1.  (i.e., at block number 32768 for file systems with a 4k
block size).  This new compat feature restricts the backup superblock
to block group #1 and the last block group in the file system.

Aside from reducing the overhead of the file system by a small number
of blocks, by eliminating the rest of the backup superblocks, it
allows us to have a much more flexible metadata layout.  For example,
we can force all of the allocation bitmaps and inode table blocks to
the beginning of the disk, which allows most of the disk to be
exclusively used for contiguous data blocks.

This simplifies taking advantage of certain HDD specific features,
such as Shingled Magnetic Recording (aka Shingled Drives), and the
TCG's OPAL Storage Specification where having a simple mapping between
LBA block ranges and the data blocks used by the file system can make
life much simpler.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-30 13:58:18 -05:00
Theodore Ts'o 0fe6e3ae7b libext2fs: add new function ext2fs_add_journal_inode2()
This new function has an parameter which allows the caller to specify
the location of the journal.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-30 13:58:17 -05:00
Theodore Ts'o 3550f722e9 libext2fs: factor out get_midpoint_journal_block() in mkjournal.c
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-30 13:58:17 -05:00
Theodore Ts'o 424de6e8e3 libext2fs: optimize ext2fs_new_block2()
If there are hundreds of thousands of blocks which are in use before
the first free block, it is much, MUCH faster to use
ext2fs_find_first_zero_block_bitmap2() instead of searching the
allocation bitmap bit by bit.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-20 22:16:16 -05:00
Theodore Ts'o fccdbac394 libext2fs: optimize ext2fs_allocate_group_table()
By using ext2fs_mark_block_bitmap_range2 and/or
ext2fs_block_alloc_stats_range(), we can significantly speed up the
time needed by mke2fs to allocate the inode table.

For example, the CPU time needed to run the command "mke2fs -t ext4
/tmp/foo.img 32T" (where tmpfs was mounted on /tmp) was decreased from
21.7 CPU seconds down to under 1.7 seconds.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-20 22:16:16 -05:00
Theodore Ts'o 5c41451e16 libext2fs: add ext2fs_block_alloc_stats_range()
This function is more efficient than using ext2fs_block_alloc_stats2()
for each block in a range.  The efficiencies come from being able to
set a block range in the block bitmap at once, and from being update
the block group descriptors once per block group.  Especially now that
we are checksuming the block group descriptors, and we are using red
black trees for the allocation bitmaps, these changes can make a huge
difference in the CPU time used by mke2fs when creating very large
file systems.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-20 22:13:14 -05:00
Theodore Ts'o dd9aa132bf libext2fs: further clean up and rename check_block_uninit
Commit 8e44eb64bb (libext2fs: mark group data blocks when loading
block bitmap) simplified check_block_uninit since we are now
initializing the bitmap when it is loaded from disk.  It left some
variables which were being set but never used, however.  In addition,
since we only need check_block_uninit() to clear the block bitmap's
uninit flag, rename it to clear_block_uninit(), and only call it once
we have found a free block in ext2fs_new_blocks2().

This cleans up the code some and optimizes things if we need to search
multiple block groups trying to find a free block.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Cc: Darrick J. Wong <darrick.wong@oracle.com>
2014-01-20 01:00:05 -05:00
Theodore Ts'o 3203cd9332 libext2fs: optimize find_first_{zero,set}() for red-black tree based bitmaps
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-20 01:00:05 -05:00
Theodore Ts'o 14717832dd libext2fs: optimize find_first_set() for bitarray-based bitmaps
Basically just a trivial adaption of the find_first_zero() function
for bitarray-based bitmaps.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-20 00:59:49 -05:00
Theodore Ts'o d954fa407b libext2fs: build tst_bitmaps with rep invariants checking enabled
When building tst_bitmaps, enable #define DEBUG_RB, so we are
always testing the sanity of the in-memory representation of the
bitmap when using red-black trees as part of a "make check" run.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-19 19:19:26 -05:00
Theodore Ts'o 3602197449 libext2fs: clean up generic handling of ext2fs_find_first_{set,zero}_*()
Move the error checking into the the generic bitmap code, and add
support for bitmaps with cluster_bits set.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-19 19:19:17 -05:00
Theodore Ts'o a2acec75c0 libext2fs: fix off-by-one bug in ext2fs_extent_insert()
When inserting the first extent into an empty inode, the
ext2fs_extent_insert() leaves path->left set to 1 instead of 0.  Since
path->curr is pointing at the last (only) extent in the file,
path->left should be 0.

This is mostly harmless, and gets corrected fairly quickly if the
calling applicaton jumps to a different part of the extent tree ---
for example, by calling ext2fs_extent_goto(), or calling
ext2fs_extent_get with the flags argument set to EXT2_EXTENT_ROOT.
Which is why we hadn't noticed this problem until now.

However, if you insert four extents using ext2fs_extent_insert, the
fourth insert will end up copying too many bytes in the i_block[]
array, since path->left is one larger than it should be.  This results
in the inode fields i_generation, i_file_acl, and i_size_high getting
zeroed out.

This problem can be replicated as follows:

% cp /dev/null /tmp/foo.img
% mke2fs -F -t ext4 /tmp/foo.img 100
% debugfs -w /tmp/foo.img
debugfs: write /dev/null foo
debugfs: set_inode_field foo i_size_hi 1
debugfs: stat foo
 <----- note that the inode's size is 4294967296
debugfs: extent_open foo
debugfs (extent ino 12): insert --after 0 1 100
debugfs (extent ino 12): insert --after 1 1 101
debugfs (extent ino 12): insert --after 2 1 102
debugfs (extent ino 12): insert --after 3 1 103
debugfs (extent ino 12): extent_close
debugfs: stat foo
 <----- note that the inode's size is now 0
debugfs: quit

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-19 19:18:48 -05:00
Theodore Ts'o dff0b6a333 libext2fs: add ext2fs_find_first_set_{block,inode}_bitmap2()
Add functions which try to find the first set block or inode in a
bitmap.  This is useful when trying to allocate a range of blocks
efficiently.

Like the find_first_zero family of functions, provide a generic O(N)
search function which will be used if there is no optimized version
provided by the red-black tree or bitarray functions.

Also, expand the test cases for ext2fs_find_first_zero_*() functions.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-13 14:02:06 -05:00
Darrick J. Wong d7c64cdc96 libext2fs: no need to clear BLOCK_UNINIT during ext2fs_reserve_super_and_bgd
Since the beginning of the uninit_bg feature, the kernel[1] and
e2fsck[2] have always been careful to detect the presence of the
BLOCK_UNINIT flag, and compute a block bitmap with any group metadata
blocks marked in that bitmap.  With that in mind, I think it's safe to
say that this is a design feature of uninit_bg.

Now that we've trained libext2fs to have this same behavior whenever
it's loading a block bitmap, we no longer need to unset BLOCK_UNINIT
for a group that contains only its own group metadata -- kernel,
e2fsck, and e2fsprogs will handle this correctly.

[1] kernel git 717d50e4971b81b96c0199c91cdf0039a8cb181a
    "Ext4: Uninitialized Block Groups"
[2] e2fsprogs git f5fa20078b
    "Add support for EXT2_FEATURE_COMPAT_LAZY_BG"

Reported-by: Akira Fujita <a-fujita@rs.jp.nec.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-01-11 14:15:51 -05:00
Darrick J. Wong 8e44eb64bb 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 f5fa20078b
    "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 14:04:48 -05:00
Darrick J. Wong d36b957b34 libext2fs: don't always read backup group descriptors on a 1k-block meta_bg fs
On a filesystem with 1K blocks and meta_bg enabled, opening a
filesystem with automatic superblock detection tries to compensate for
the fact that the superblock lives in block 1.  However, the method by
which this is done is later misinterpreted to mean "read the backup
group descriptors", which is not what we want in this case.

Therefore, in ext2fs_open3() separate the 'group zero' adjustment into
its own variable so that we don't get fed backup group descriptors
when we try to load meta_bg group descriptors.

Furthermore, enhance ext2fs_descriptor_block_loc2() to perform its own
group zero correction.  The other caller of this function neglects to
do any group-zero correction of their own, so this fixes them too.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-01-11 13:58:15 -05:00
Darrick J. Wong 6ff02832a0 libext2fs: detect correct superblock adjustments when loading backup groups
If ext2fs_descriptor_block_loc2() is called with a meta_bg filesystem
and group_block is not the normal value, the function will return the
location of the backup group descriptor block in the next block group.
Unfortunately, it fails to account for the possibility that the backup
group contains a backup superblock but the regular superblock does
not.  This is the case with block groups 48-49 on a meta_bg fs with 1k
blocks; in this case, libext2fs will fail to open the filesystem.

Therefore, teach the function to adjust for superblocks in the backup
group, if necessary.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-01-11 13:55:20 -05:00
Theodore Ts'o 81eca9afc0 Merge branch 'maint' into next 2014-01-10 17:44:17 -05:00
Theodore Ts'o 253a96500a Save and restore umask around calls to mkstemp()
The races would be hard to exploit, but let's close them off.

Addresses-Coverity-Id: #709504
Addresses-Coverity-Id: #709505
Addresses-Coverity-Id: #709506

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-09 15:18:44 -05:00
Theodore Ts'o bc82f39437 Merge branch 'maint' into next
Conflicts:
	e2fsck/pass1.c
	lib/ext2fs/csum.c
2014-01-09 12:25:37 -05:00
Theodore Ts'o 1fb1a498ff libext2fs: fix memory leaks on error paths in ext2fs_create_icount_tdb
Addresses-Coverity-Id: #1138575

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-07 09:49:10 -05:00
Theodore Ts'o 2641bf8991 libext2fs: remove redundant code in rb_print_stats()
Addresses-Coverity-Id: #709550

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-07 09:10:24 -05:00
Theodore Ts'o d887b93701 libext2fs: make ext2fs_group_desc_csum return 0 if meta_csum not enabled
Addresses-Coverity-Id: #1147784

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-07 09:10:24 -05:00
Theodore Ts'o d2dce124dd Merge branch 'maint' into next
Conflicts:
	debian/changelog
	e2fsck/Makefile.in
2014-01-06 06:58:58 -05:00
Theodore Ts'o ece2d5884b libext2fs: fix potential memory leak in qcow2_write_raw_image()
Addresses-Coverity-ID: #1049179
Addresses-Coverity-ID: #1049180

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-05 22:58:16 -05:00
Theodore Ts'o 795c02def3 Use consistent e2fsprogs-standard types
Some different types such as u_int16_t and __uint32_t have snuck into
e2fsprogs.  These types are not guaranteed by any standard, and they
are not provided by dietlibc.  Convert them to __u16, __u32,
etc. since these are guaranteed to be provided by e2fsprogs' build.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-05 22:58:16 -05:00
Theodore Ts'o 6c59a665da configure: fix --with-diet-libc
Newer versions of autoconf pull in AC_PROG_GCC as part of
AC_CANONICAL_HOST.  So we need check for WITH_DIET_LIBC earlier in
configure.in.

Also, e2fsprogs now needs functions which are found in diet libc's
compat library.  So add support for autoconf's LIBS function, and
automatically set libs to include -lcompat.

Finally, disable compiling e4defrag by deault if --with-diet-libc is
specified because the program has too many glibc dependencies.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-01-05 22:58:16 -05:00
Theodore Ts'o 2ffccc82c4 Merge branch 'maint' into next
Conflicts:
	lib/ext2fs/csum.c
2013-12-26 00:30:56 -05:00
Theodore Ts'o aa2c743331 e2image: only skip writing zero blocks when writing to a new file
The e2image progam was originally intended to create image files.
However, some people have started using e2image to copy a file system
from one block device to another, since it is more efficient than
using dd because it only copies the blocks which are in use.  If we
are doing this, however, we must not skip writing blocks which are all
zero in the source device, since they may not be zero in the
destination device.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Cc: Phillip Susi <psusi@ubuntu.com>
2013-12-26 00:21:08 -05:00
Theodore Ts'o b3c2a63813 libext2fs: checksum bg descriptors larger than 64 bytes
Currently the ext4 block group descriptor is 64 bytes.  In case we
need to support larger block group descriptors in the future, teach
ext2fs_group_desc_csum() to checksum parts of the block group
descriptors that libext2fs doesn't yet understand.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-24 23:19:07 -05:00
Andreas Dilger 2bc3041754 debugfs, e2fsck: fix s_desc_size handling
The s_desc_size in the superblock specifies the group descriptor
size in bytes, but in various places the EXT4_FEATURE_INCOMPAT_64BIT
flag implies that the descriptor size is EXT2_MIN_DESC_SIZE_64BIT
(64 bytes) instead of checking the actual size.  In other places,
the s_desc_size field is used without checking for INCOMPAT_64BIT.

In the case of ext2fs_group_desc() the s_desc_size was being ignored,
and assumed to be sizeof(struct ext4_group_desc), which would result
in garbage for any but the first group descriptor.  Similarly, in
ext2fs_group_desc_csum() and print_csum() they assumed that the
maximum group descriptor size was sizeof(struct ext4_group_desc).
Fix these functions to use the actual superblock s_desc_size if
INCOMPAT_64BIT.

Conversely, in ext2fs_swap_group_desc2() s_desc_size was used
without checking for INCOMPAT_64BIT being set.

The e2fsprogs behaviour is different than that of the kernel,
which always checks INCOMPAT_64BIT, and only uses s_desc_size to
determine the offset of group descriptors and what range of bytes
to checksum.

Allow specifying the s_desc_size field at mke2fs time with the
"-E desc_size=NNN" option.  Allow a power-of-two s_desc_size
value up to s_blocksize if INCOMPAT_64BIT is specified.  This
is not expected to be used by regular users at this time, so it
is not currently documented in the mke2fs usage or man page.

Add m_desc_size_128, f_desc_size_128, and f_desc_bad test cases to
verify mke2fs and e2fsck handling of larger group descriptor sizes.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-24 22:50:19 -05:00
Andreas Dilger 11d1116a7c e2fsck: verify s_desc_size is power-of-two value
Add a LOG2_CHECK mode for check_super_value() so that it is easy
to verify values that are supposed to be power-of-two values
(s_desc_size and s_inode_size so far).  In ext2fs_check_desc()
also check for a power-of-two s_desc_size.

Print out s_desc_size in debugfs "stats" and dumpe2fs output, if
it is non-zero.

It turns out that the s_desc_size validation in check_super_block()
is not currently used by e2fsck, because the group descriptors are
verified earlier by ext2fs_check_desc(), and even without an
explicit check of s_desc_size the group descriptors fail to align
correctly on disk.  It makes sense to keep the check_super_block()
regardless, in case the code changes at some point in the future.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-23 16:03:46 -05:00
Theodore Ts'o f404167dda Clean up sparse warnings
Mostly by adding static and removing excess extern qualifiers.  Also
convert a few remaining non-ANSI function declarations to ANSI.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-16 18:56:36 -05:00
Theodore Ts'o e4681bca17 Merge branch 'maint' into next
Conflicts:
	debugfs/debugfs.8.in
2013-12-16 01:35:56 -05:00
Kazuya Mio fbabd5c44c mke2fs: disallow creating FS on a loop mounted file with no option
When /etc/mtab is a symlink of /proc/mounts, mke2fs without -FF option
can create a filesystem on the image file that is mounted.
According to mke2fs man page, we should specify -FF option in this case.

This patch protects filesystem from unintended mke2fs caused by human error.

How to reproduce:
  # mke2fs -t ext4 -Fq fs.img
  # mount -o loop fs.img /mnt/mp1
  # mke2fs -t ext4 -Fq fs.img && echo "mke2fs success"
  mke2fs success

Signed-off-by: Kazuya Mio <k-mio@sx.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-16 00:48:54 -05:00
Darrick J. Wong 230272c15a libext2fs: don't update the summary counts when doing implied cluster allocation
When we're appending a block to a directory file or the journal file,
and the new block is part of a cluster that has already been allocated
to the file (implied cluster allocation), don't update the bitmap or
the summary counts because that was performed when the cluster was
allocated.

Reviewed-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-15 23:50:54 -05:00
Darrick J. Wong 8439775425 libext2fs: only punch complete clusters
When bigalloc is enabled, using ext2fs_block_alloc_stats2() to free
any block in a cluster has the effect of freeing the entire cluster.
This is problematic if a caller instructs us to punch, say, blocks
12-15 of a 16-block cluster, because blocks 0-11 now point to a "free"
cluster.

The naive way to solve this problem is to see if any of the other
blocks in this logical cluster map to a physical cluster.  If so, then
we know that the cluster is still in use and it mustn't be freed.
Otherwise, we are punching the last mapped block in this cluster, so
we can free the cluster.

The implementation given only does the rigorous checks for the partial
clusters at the beginning and end of the punching range.

Reviewed-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-15 23:50:06 -05:00
Andreas Dilger eaf4403561 ext2fs,blkid: delete unused LIST_HEAD() macro
The LIST_HEAD() macro conflicts with the <sys/queue.h> declaration
of the same name.  Delete the unused LIST_HEAD() macro from the
libext2fs and libblkid headers to avoid compiler warnings.  It can
be replaced by INIT_LIST_HEAD() or LIST_HEAD_INIT() if needed.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-15 23:26:39 -05:00
Andreas Dilger 269da3b806 ext2fs: quiet compiler warnings
Include ext2fsP.h in fileio.c for ext2fs_file_block_offset_too_big()
declaration.  Fix up the declaration to mark it extern in the header.

Include <strings.h> header for strcasecmp() in tune2fs.c if available,
as described in the strcasecmp(3) man page, instead of doing this
indirectly by declaring _BSD_SOURCE and getting it from <string.h>.

If CONFIG_QUOTA is undefined, parse_quota_opts() is unused in
tune2fs.c so #ifdef it out.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-15 23:26:31 -05:00
Andreas Dilger 3a941bef3b build: use long long for __u64 by default
Don't print a verbose configure error in parse-types.h if
<asm/types.h> missing and __[SU]*_TYPEDEF is unset.  This is
always the case for non-Linux builds.

The printf formatting strings all use "%llu" for printing 64-bit
values and this it produces a large number of warnings if __u64
is defined as "unsigned long".  If __U64_TYPEDEF isn't set use
"unsigned long long" for __u64 in ext2-types.h and blkid-types.h
by default instead of using "unsigned long".

Fix a few places where "%d" or "%u" or "%Lu" were used to print a
64-bit value, by converting them to use "%lld" or "%llu" instead.

Fix a few places where "%lu" was used to print .tv_usec, by casting
the variable to "(long)" since .tv_usec is "int" on some systems.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-15 22:10:09 -05:00
Theodore Ts'o 2b24aadee8 Merge branch 'maint' into next
Conflicts:
	misc/tune2fs.c
2013-12-14 20:56:29 -05:00
Darrick J. Wong 4c6fd9c20a libext2fs: clamp block-map punch range end to 2^32 blocks
In the ^extent case, passing ~0ULL as the 'end' parameter to
ext2fs_punch() causes the (end - start + 1) calculation to overflow to
zero.  Since the old-style mapped block files cannot have more than
2^32 blocks, just clamp it to ~0U.

This fixes a regression in t_quota_2off with the patch "libext2fs: use
ext2fs_punch() to truncate quota file" applied.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-14 19:46:53 -05:00
Darrick J. Wong 8385ad596f libext2fs: tweak inline data error wording
Tweak the wording to be a little less ambiguous, since 'block' can be
a noun or a verb.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 23:33:03 -05:00
Theodore Ts'o 2d3df8dc7f Merge branch 'maint' into next
Conflicts:
	lib/ext2fs/newdir.c
2013-12-12 15:39:14 -05:00
Darrick J. Wong 299cc61755 libext2fs: zero block contents past EOF when setting size
When we set the file size, find the block containing EOF, and zero
everything in that block past EOF so that we can't return stale data
if we ever use fallocate or truncate to lengthen the file.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:32:25 -05:00
Darrick J. Wong 042a0f52ec libext2fs: don't error out when punching a totally sparse file
If we're asked to punch a file with no data blocks mapped to it and a
non-zero length, we don't need to do any work in ext2fs_punch_extent()
and can return success.  Unfortunately, the extent_get() function
returns "no current node" because it (correctly) failed to find any
extents, which is bubbled up to callers.  Since no extents being found
is not an error in this corner case, fix up ext2fs_punch_extent() to
return 0 to callers.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:25:42 -05:00
Darrick J. Wong dc9673abcf libext2fs: fix punching extents when there are no left extents
When deleting an entire extent, we cannot always slip to the previous
leaf extent because there might not /be/ a previous extent.
Attempting to correct for that error by asking for the 'current' leaf
extent also doesn't work, because the failed attempt to change to the
previous extent leaves us with no current extent.

Fix this problem by recording the lblk of the next extent before
deleting the current extent and _goto()ing to the next extent after
the deletion.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:23:54 -05:00
Darrick J. Wong 8a86bbb197 libext2fs: fail fileio write if we can't allocate a block
If we're using ext2fs_file_write() to write to a hole in a file,
ensure that we can actually allocate the block before updating i_size.
In other words, don't update i_size and don't return success if we hit
an error while allocating space.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:19:48 -05:00
Darrick J. Wong 13b48a0ae1 libext2fs: fix a broken close() test
Zero is a valid file descriptor, so close it.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:17:59 -05:00
Darrick J. Wong 6a26b38ac4 libext2fs: fix memory leaks (on error paths)
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:16:59 -05:00
Darrick J. Wong 8d74ab767d libext2fs: check return values
Fix up a few places where we ignore return values.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:14:52 -05:00
Darrick J. Wong 85b8f7affa debugfs: don't leak mmp_s memory (on error path)
ext2fs_free_mem() takes a pointer to a pointer, similar to
ext2fs_get_mem().  Improve the documentation, and fix debugfs.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 12:57:27 -05:00
Darrick J. Wong 56fa488796 libext2fs: fileio should use 64bit io routines
When reading or writing file blocks, use the IO manager routines that
can handle 64bit block numbers.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 12:50:07 -05:00
Darrick J. Wong 62f17f3603 libext2fs: make symlinks safe for 64bit blocks and extents
If we have to create a big symlink (i.e. one that doesn't fit into
i_block[]), we are not 64bit block safe and the namei code does not
handle extents at all.  Fix both.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 12:48:39 -05:00
Darrick J. Wong ba0230f885 libext2fs: fix "a" vs "an" in the error catalog
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 12:42:32 -05:00
Darrick J. Wong 1e7451493e libext2fs: don't allow ridiculously large logical block numbers
Forbid clients from trying to map logical block numbers that are
larger than the lblk->pblk data structures are capable of handling.
While we're at it, don't let clients set the file size to a number
that's beyond what can be mapped.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 12:40:58 -05:00
Darrick J. Wong 3b6c0938ec libext2fs: fix tests that set LARGE_FILE
For each site where we test for a large file (> 2GB) and set the
LARGE_FILE feature, use a helper function to make the size test
consistent with the test that's in e2fsck.  This fixes the fsck
complaints when we try to create a 2GB journal (not so hard with 64k
block size) and fixes the incorrect test in fileio.c.

Reviewed-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 12:08:48 -05:00
Darrick J. Wong 341bc5e368 libext2fs: don't overflow when punching indirect blocks with large blocks
On a FS with a rather large blockize (> 4K), the old block map
structure can construct a fat enough "tree" (or whatever we call that
lopsided thing) that (at least in theory) one could create mappings
for logical blocks higher than 32 bits.  In practice this doesn't
happen, but the 'max' and 'iter' variables that the punch helpers use
will overflow because the BLOCK_SIZE_BITS shifts are too large to fit
a 32-bit variable.  The current variable declarations also cause punch
to fail on TIND-mapped blocks even if the file is < 16T.  So enlarge
the fields to fit.

Yes, this is an obscure corner case, but it seems a little silly if we
can't punch a file's block 300,000,000 on a 64k-block filesystem.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 12:06:07 -05:00
Theodore Ts'o ef429f5968 Merge branch 'maint' into next 2013-12-04 00:23:19 -05:00
Andreas Dilger 2fe2d408a4 mmp: fix 64-bit handling of s_mmp_block
Fix the checking of s_mmp_block in e2fsck_pass1() and
ext2fs_mmp_read() to handle the high 32 bits of s_blocks_count.
Remove redundant check of s_mmp_block in do_dump_mmp() right before
ext2fs_mmp_read() is called.

Also fix s_blocks_count_hi in check_backup_super_block(), since it
cannot use the ext2fs_blocks_count() helper easily.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-03 20:22:16 -05:00
Darrick J. Wong 17abb05f51 libext2fs: fix function declarations to match definition
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-03 14:24:53 -05:00
Theodore Ts'o 2fae17697a Merge branch 'maint' into next
Conflicts:
	e2fsck/pass2.c
	e2fsck/pass3.c
2013-12-03 00:24:39 -05:00
Eric Whitney 144f4e8ca5 libext2fs: fix printf conversion spec in tst_iscan.c
A recent patch to fix blk_t to blk64_t assignment mismatches in
e2fsprogs (commit 4dbfd79d14) created
a printf conversion spec / argument type mismatch in tst_iscan.c.
Fix this to avoid truncation of the printed value and to silence
a compiler warning seen when "make check" is run.

Signed-off-by: Eric Whitney <enwlinux@gmail.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-02 23:37:08 -05:00
Theodore Ts'o 15a668c4b5 libext2fs: add explicit casts to bitops.h
Add some explicit casts to silence some -Wconversion noise.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-02 22:24:12 -05:00
Theodore Ts'o b70483742b libext2fs: add explicit casts to ext2fs.h
Add some explicit casts to silence some -Wconversion noise.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-02 22:04:25 -05:00
Theodore Ts'o 4962203093 libext2fs: fix some memory leaks with image file handling
These memory leaks were discovered by using "valgrind
--leak-check=full" while running "e2image -I bar.img foo.e2i"

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-02 14:52:31 -05:00
Kit Westneat 8b3c52fb81 libext2fs: set the fs block size to new_io in ext2fs_rewrite_to_io()
e2image manually opens a new IO channel, and then sets the file system
to use this new IO channel via ext2fs_rewrite+to_io().  We need to
make sure the IO channel is set to the file system's block size to
avoid some nasty buffer overruns.

[ Modified by tytso to use io_channel_set_blksize() ]

Signed-off-by: Kit Westneat <kwestneat@ddn.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Andreas Dilger <andreas.dilger@intel.com>
2013-12-02 14:49:41 -05:00
Theodore Ts'o 7e0337ed74 Merge branch 'maint' into next 2013-10-23 20:17:35 -04:00
Darrick J. Wong d87f198ca3 libext2fs: reject 64bit badblocks numbers
Don't accept block numbers larger than 2^32 for the badblocks list,
and don't run badblocks on them either.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2013-10-23 19:56:21 -04:00
Darrick J. Wong d36e027f9c libext2fs: stop iterating dirents when done linking
When we've succesfully linked an inode into a directory, we can stop
iterating the directory.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2013-10-23 19:29:20 -04:00
Theodore Ts'o a7f4c6353f libext2fs: handle inline_data in block iterator by returning an error code
An inode with inline data has no data blocks, so we can not iterate
over such an inode.  Return an error code which indicates this fact;
callers can use this to determine whether or not the inode has inline
data, and then call some routine to iterate over the directory intries
in the line data or read the inline data, as appropriate.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-13 21:43:41 -04:00
Zheng Liu 00716339da libext2fs: add INLINE_DATA into EXT2_LIB_SOFTSUPP_INCOMPAT
EXT4_FEATURE_INCOMPAT_INLINE_DATA flag is added into
EXT2_LIB_SOFTSUPP_INCOMPAT due to we still need to take a long time to
test inline_data feature.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2013-10-12 23:20:38 -04:00
Darrick J. Wong 8a9a7fd366 libext2fs: add space for checksum when unconverting a hashed directory block
The ext2fs_link function has the unfortunate habit of converting
hashed directories into unhashed directories.  It doesn't notice that
it's slicing and dicing directory entries from a former dx_{root,node}
block, and therefore doesn't write a protective dirent into the end of
the block to store the checksum.  Teach it to do this.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-12 23:16:10 -04:00
Darrick J. Wong a2dd2e13c6 Define an error code for block bitmap checksum failures
Apparently libext2fs didn't have an error code defined for block
bitmap checksum errors, so add one.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
2013-10-12 23:11:31 -04:00
Theodore Ts'o e337e7fad8 Merge branch 'maint' into next
Conflicts:
	e2fsck/problem.c
	e2fsck/rehash.c
	e2fsck/super.c
2013-10-12 22:26:28 -04:00
Darrick J. Wong 832cb612f8 e2fsprogs: add (optional) sparse checking to the build
Run sparse against source files when building e2fsprogs with 'make C=1'.  If
instead C=2, it configures basic ext2 types for bitwise checking with sparse,
which can help find the (many many) spots where conversion errors are
(possibly) happening.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-11 23:12:40 -04:00
Darrick J. Wong 57bde59a06 libext2fs: openfs() musn't allow bigalloc without EXT2_FLAGS_64BITS
Currently, only the new 64-bit bitmap implementation supports the
block<->cluster conversions that bigalloc requires.  Therefore, if we
have a bigalloc filesystem, require EXT2_FLAGS_64BITS be passed in to
ext2fs_open().  This does not mean that bigalloc file systems have to
be 64-bits; just that the userspace utilities have to be able to use
the new 64-bit capable library functions.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
2013-10-11 21:36:00 -04:00
Darrick J. Wong 2a091427bc libext2fs: search all possible blocks in implied_cluster_alloc()
implied_cluster_alloc() is written such that if the the user passes in
a logical block that is the zeroth block in a logical cluster (lblk %
cluster_ratio == 0), then it will assume that there is no physical
cluster mapped to any other part of the logical cluster.

This is not true if we happen to be allocating logical blocks in
reverse order.  Therefore, search the whole cluster, except for the
lblk that we passed in.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-08 12:08:49 -04:00
Darrick J. Wong 4605a35d1e libext2fs: fix off-by-one error in file truncation
When told to truncate a file, ext2fs_file_set_size2() should start with
the first block past the end of the file.  The current calculation
jumps one more block ahead, with the result that it fails to hack off
the last block.  Adding blocksize-1 and dividing is sufficient to find
the last block.

Reviewed-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-08 11:51:26 -04:00
Niu Yawei 470ca046b1 libext2fs: update i_size in ext2fs_file_write()
ext2fs_file_write() needs to update i_size on successful write,
otherwise, ext2fs_file_read() in same open/close cycle will not
be able to read the just written data.

This fixes a bug which results in the the problem of quotacheck
triggered on 'tune2fs -O quota' failed to write back multiple
users/groups accounting information.

Signed-off-by: Niu Yawei <yawei.niu@intel.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-08 11:36:52 -04:00
Darrick J. Wong a25487cbaf e2fsprogs: fix inode and block relocation functions to use blk64_t
The inode and block relocation functions aren't currently compiled in
(so we don't need to worry about breaking ABI compatibility).  They
were originally intended for use by resize2fs, but we never ended up
using them, so (wisely) they weren't ever included in libext2fs as an
exported interface (they're not even compiled by the Makefile).

Fix them so that in case we ever use them, so that in places where raw
data types (int, long, etc.) stood in for blk_t and blk64_t.  Also fix
some sites where we should probably be using blk64_t.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-07 09:57:43 -04:00
Darrick J. Wong 4dbfd79d14 e2fsprogs: fix blk_t <- blk64_t assignment mismatches
Fix all the places where we should be using a blk64_t instead of a
blk_t.  These fixes are more severe because 64bit values could be
truncated silently.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-07 09:51:48 -04:00
Darrick J. Wong 581646b94f libext2fs: ind_punch() must not stop examining blocks prematurely
When we're iterating the main loop in ind_punch(), "offset" tracks how
far we've progressed into the block map, "start" tells us where to
start punching, and "count" tells us how many blocks we are to punch
after "start".  Therefore, we would like to break out of the loop once
the "offset" that we're looking at has progressed past the end of the
punch range.  Unfortunately, if start !=0, the if-break clause in the
loop causes us to break out of the loop early.

Therefore, change the breakout test to terminate the loop at the
correct time.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-07 09:51:35 -04:00
Darrick J. Wong 4ee4ad80dc libext2fs: allow callers to punch a single block
The range of blocks to punch is treated as an inclusive range on both
ends, i.e. if start=1 and end=2, both blocks 1 and 2 are punched out.
Thus, start == end means that the caller wishes to punch a single
block.  Remove the check that prevents us from punching a single
block.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-07 09:51:20 -04:00
Darrick J. Wong 5d494038ee libext2fs: rewind extent pointer when totally deleting an extent
During a punch operation, if we decide to delete an extent out of the
extent tree, the subsequent extents are moved on top of the current
extent (that is to say, they're memmmove'd down one slot).  Therefore
it is not correct to advance to the next leaf because that means we
miss half the extents in the range!  Rereading the current pointer
should be fine.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-10-07 09:35:25 -04:00
Darrick J. Wong 580d8a0933 libext2fs: fix a minor grammatical error in the error catalog
'an block' should be 'a block'.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2013-10-07 09:20:28 -04:00
Darrick J. Wong 92a7b0d2e3 libext2fs: set the large_file feature flag when setting i_size > 2GB
If someone tries to write a file that is larger than 2GB, we need to
set the large_file feature flag to affirm that i_size_hi can hold
meaningful contents.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2013-10-07 09:14:11 -04:00
Darrick J. Wong 7320cb7106 libext2fs: only link an inode into a directory once
The ext2fs_link helper function link_proc does not check the value of
ls->done, which means that if the function finds multiple empty spaces
that will fit the new directory entry, it will create a directory
entry in each of the spaces.  Instead of doing that, check the done
value and don't do anything more if we've already added the directory
entry.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2013-10-07 09:13:59 -04:00
Theodore Ts'o fc9f162eea Merge branch 'maint' into next 2013-09-09 20:49:49 -04:00
Zheng Liu 8ab395524b libext2fs: fix a coding style for EXT2_NO_MTAB_FILE
When we define an error in lib/ext2fs/ext2_err.et.in, we will always use
EXT2_ET_* prefix for a new error.  But EXT2_NO_MTAB_FILE doesn't obey
this rule.  So fix it.

Signed-off-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-09-09 10:50:11 -04:00
Eric Sandeen a88c2fb508 resize2fs: fix interior extent node corruption
If we have an extent tree like this (from debuge2fs's "ex" command):

Level Entries       Logical            Physical Length Flags
...
 2/ 2  60/ 63 13096 - 13117 650024 - 650045     22
 2/ 2  61/ 63 13134 - 13142 650062 - 650070      9
 2/ 2  62/ 63 13193 - 13194 650121 - 650122      2
 2/ 2  63/ 63 13227 - 13227 650155 - 650155      1 A)
 1/ 2   4/ 14 13228 - 17108 655367            3881 B)
 2/ 2   1/117 13228 - 13251 650156 - 650179     24 C)
 2/ 2   2/117 13275 - 13287 650203 - 650215     13
 2/ 2   3/117 13348 - 13353 650276 - 650281      6
...

and we resize the fs in such a way that all of those blocks must
be moved down, we do them one at a time.  Eventually we move 1-block
extent A) to a lower block, and then follow it with the other
blocks in the next logical offsets from extent C) in the next
interior node B).

The userspace extent code tries to merge, so when it finds that
logical 13228 can be merged with logical 13227 into a single extent,
it does.  And so on, all through extent C), up to block 13250 (why
not 13251?  [1]), and eventually move the node block as well.
So we end up with this when all the blocks are moved post-resize:

Level Entries       Logical            Physical Length Flags
...
 2/ 2 120/122 13193 - 13193  33220 -  33220      1
 2/ 2 121/122 13194 - 13194  33221 -  33221      1
 2/ 2 122/122 13227 - 13250  33222 -  33245     24 D)
 1/ 2   5/ 19 13228 - 17108  34676            3881 E) ***
 2/ 2   1/222 13251 - 13251  33246 -  33246      1 F)
 2/ 2   2/222 13275 - 13286  33247 -  33258     12
...

All those adjacent blocks got moved into extent D), which is nice -
but the next interior node E) was never updated to reflect its new
starting point - it says the leaf extents beneath it start at 13228,
when in fact they start at 13251.

So as we move blocks one by one out of original extent C) above, we
need to keep updating C)'s parent node B) for a proper starting point.
fix_parents() does this.

Once the tree is corrupted like this, more corruption can
ensue post-resize, because we traverse the tree by interior nodes,
relying on their start block to know where we are in the tree.
If it gets off, we'll end up inserting blocks into the wrong part
of the tree, etc.

I have a testcase using fsx to create a complex extent tree which
is then moved during resize; it hit this corruption quite easily,
and with this fix, it succeeds.

Note the first hunk in the commit is for going the other way,
moving the last block of an extent to the extent after it; this
needs the same sort of fix-up, although I haven't seen it in
practice.

[1] We leave the last block because a single-block extent is its
own case, and there is no merging code in that case.  \o/

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-09-09 10:47:25 -04:00
Darrick J. Wong 816d2c9cf7 libext2fs: ext2fs_dup_handle should not alias MMP buffers
It turns out that resize2fs uses ext2fs_dup_handle to duplicate fs handles.  If
MMP is enabled, this causes both handles to share MMP buffers, which is bad
news when it comes time to free both handles.  Change the code to (we hope) fix
this.  This prevents resize2fs from failing with a double-free error when
handed a MMP filesystem.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-09-09 10:39:33 -04:00
Theodore Ts'o 38cc555a5f Merge branch 'maint' into next 2013-07-08 12:14:45 -04:00
Theodore Ts'o 42b61c50e5 libext2fs, tests: allow /etc/mtab file to be missing
The environment variable EXT2FS_NO_MTAB_OK will suppress the error
code EXT2_NO_MTAB_FILE when the /etc/mtab file can not be found.  This
allows the e2fsprogs regression test suite to be run in chroots which
might not have an /etc/mtab file.

By default will still want to complain if the /etc/mtab file is
missing, since we really don't want to discourage distributions and
purveyors of embedded systems from running without an /etc/mtab file.
But if it's missing it only results in a missing sanity check that
might cause file system corruption if the file system is mounted when
programs such as e2fsck, tune2fs, or resize2fs is running, so there is
no potential security problems that might result if this environment
variable is set inappropriately.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-07-08 12:08:44 -04:00
Theodore Ts'o 93061ea0f4 Merge branch 'maint' into next
Conflicts:
	RELEASE-NOTES
	version.h
2013-06-20 23:20:51 -04:00
Theodore Ts'o b49d67ddd8 Use ext2fs_cpu_to_be32() instead of cpu_to_be32() in kernel-jbd.h
Avoid compatibility problems by using the byte swapping functions
defined by e2fsprogs, instead of the ones defined in the system header
files.  We use them everywhere else, so we should use them in
kernel-jbd.h too.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-06-16 18:58:40 -04:00
Theodore Ts'o 8b9eb12807 Merge branch 'maint' into next
Conflicts:
	RELEASE-NOTES
	e2fsck/Makefile.in
	lib/config.h.in
	version.h
2013-06-16 18:54:01 -04:00
Theodore Ts'o 1ad3174af5 Try to use secure_getenv() in preference to __secure_getenv()
If secure_getenv() use it in preference to __secure_getenv().
Starting with (e)glibc version 2.17, secure_getenv() exists, while
__secure_getenv() only works with shared library links (where it is a
weak symbol), but not for static links with /lib/libc.a

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-06-16 14:34:59 -04:00
Theodore Ts'o 4718395120 libext2fs: optimize ext2fs_bg_has_super()
Reduce the CPU time needed when checking whether a block group has a
sparse superblock.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-06-15 18:29:52 -04:00
Theodore Ts'o 3ac420165c libext2fs: remove lib/ext2fs/sparse.c
This file was never getting compiled, and there is no user of
ext2fs_list_backups() in the e2fsprogs sources.  So remove it as a
clean up.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-06-15 18:28:58 -04:00
Jan Kara 70f4632b62 libext2fs: provide functions to safely access name_len and file_type
Accessing name_len (and file_type) in ext4_dir_entry structure is
somewhat problematic because on big endian architecture we need to now
whether we are really dealing with ext4_dir_entry (which has u16
name_len which needs byte swapping) or ext4_dir_entry_2 (which has u8
name_len which must not be byte swapped).

Currently the code is somewhat surprising and name_len is always
treated as u16 and byte swapped (flag EXT2_DIRBLOCK_V2_STRUCT isn't
ever used) and then masking of name_len is used to access real
name_len or file_type. Doing things this way in applications using
libext2fs is unexpected to say the least (more natural is to type
struct ext4_dir_entry * to struct ext4_dir_entry_2 * but that gives
wrong results on big endian architectures. So provide helper functions
that give endian-safe access to these fields. Also convert users in
e2fsprogs to use these functions.

Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2013-06-08 17:03:05 -04:00
Theodore Ts'o 894eaf8506 libext2fs: fix gcc -Wall nits
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-05-19 22:12:36 -04:00
Theodore Ts'o 581ecb6d88 Merge branch 'maint' into next
Conflicts:
	e2fsck/pass1b.c
	e2fsck/rehash.c
	lib/ext2fs/crc32c.c
	lib/ext2fs/gen_bitmap64.c
	misc/tune2fs.c
2013-05-19 21:45:16 -04:00
Theodore Ts'o e48bf256e9 libext2fs: fix gcc -Wall warnings
Primarily signed vs unsigned and const warnings.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-05-19 18:50:10 -04:00
Theodore Ts'o 07bcd90f3d Merge branch 'maint' into next 2013-04-22 00:07:08 -04:00
Theodore Ts'o 572ef60b89 libext2fs: only use override function when reading an 128 byte inode
The ext2fs_read_inode_full() function should not use fs->read_inode()
if the caller has requested more than the base 128 byte inode
structure and the inode size is greater than 128 bytes.  Otherwise the
caller won't get all of the bytes that they were asking for, since
there's no way for the fs->read_inode override function can know what
the size of the buffer passed to ext2fs_read_inode_full().

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-04-21 23:53:26 -04:00
Tomas Racek 8034d9fa0f e2image: fix flag settings in e2image.h
This prevents from SIGSEGV when -s options is used.

Signed-off-by: Tomas Racek <tracek@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
2013-04-21 23:49:04 -04:00
Theodore Ts'o 8c236c62c9 Merge branch 'maint' into next 2013-03-13 16:17:34 -04:00
Jan Kara 5084088215 libext2fs: Provide prototype for ext2fs_symlink()
New function ext2fs_symlink() doesn't have a prototype in ext2fs.h and
thus debugfs compilation gives warning:

debugfs.c:2219:2: warning: implicit declaration of function 'ext2fs_symlink'

Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2013-02-28 20:12:30 -05:00
Philipp Thomas 0d89e5acea Fix warnings about functions not returning a value
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-27 22:41:50 -05:00
Andreas Dilger 1b8c4c1b45 build: quiet build warnings for "gcc -Wall"
Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-27 22:31:03 -05:00
Andreas Dilger b55705e0ba build: quiet build warnings for "gcc -Wall"
Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-27 22:29:01 -05:00
Phillip Susi 274d46e1d3 libext2fs: fix ext2fs_llseek on i386
ext2fs_llseek() was using lseek instead of lseek64.  The
only time it would use lseek64 is if passed an offset that
overflowed 32 bits.  This works for SEEK_SET, but not
SEEK_CUR, which can apply a small offset to move the file
pointer past the 32 bit limit.

The code has been changed to instead try lseek64 first, and
fall back to lseek if that fails.  It also was doing a
runtime check of the size of off_t.  This has been moved to
compile time.

This fixes a problem which would cause e2image when built for
x86-32 to bomb out when used with large file systems.

Signed-off-by: Phillip Susi <psusi@ubuntu.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-24 23:04:01 -05:00
Theodore Ts'o fca8b1b241 Merge branch 'maint' into next
Conflicts:
	RELEASE-NOTES
	version.h
2013-01-21 22:06:16 -05:00
Theodore Ts'o 3e1816b8cc libext2fs: teach the ext2fs_*_block_bitmap_range2() about clusters
The ext2fs_{mark,unmark,test}_block_bitmap2() functions understand
about clusters, and will take block numbers and convert them to
clusters before checking the bitmap.  The
ext2fs_*_block_bitmap_range2() functions did not do this, which made
them inconsistent.  Fortunately, nothing has depended on this
incorrect behavior, and in fact most of the usage of these functions
have only recently been added, and only for optimizations that were
only enabled for non-bigalloc file systems.

So this is a change in previously exported functions, but (a) it
doesn't change the behavior at all for non-bigalloc file systems, and
(b) the change is more likely to fix bugs for bigalloc file systems.
For example, this change fixes a problem with resize2fs and bigalloc
file systems.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-19 23:21:11 -05:00
Theodore Ts'o 7b68fa38b8 Merge branch 'maint' into next
Conflicts:
	RELEASE-NOTES
	lib/ext2fs/ext2_err.et.in
	version.h
2013-01-16 14:14:09 -05:00
Darren Hart f01c1a6bce libext2fs: add the ext2fs_symlink() function
Creating symlinks is a complex affair when accounting for slowlinks.

Create a new function, ext2fs_symlink(), modeled after ext2fs_mkdir().
Like ext2fs_mkdir(), ext2fs_symlink() takes on the task of allocating a
new inode and block (for slowlinks), setting up sane default values in
the inode, copying the target path to either the inode (for fastlinks)
or to the first block (for slowlinks), and accounting for the inode and
block stats.  Disallow link targets longer than blocksize as the Linux
kernel prevents this.

It does not attempt to expand the parent directory, instead returning
EXT2_ET_DIR_NO_SPACE and leaving it to the caller to expand just as
ext2fs_mkdir() does.  Ideally, I think both of these functions should
make a single attempt to expand the directory.

[ Fixed a few bugs discovered when creating a test case for ext2fs_symlink() ]

Signed-off-by: Darren Hart <dvhart@infradead.org>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Cc: "Darrick J. Wong" <darrick.wong@oracle.com>
Cc: Andreas Dilger <adilger@dilger.ca>
2013-01-16 14:09:17 -05:00
Theodore Ts'o 53f2a1eaf0 libext2fs: add error codes from 1.43.x development branch
To maintain the error codes numbering, we need to pull in the changes
from the 1.43.x development branch for the libext2's error table.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-16 14:07:25 -05:00
Theodore Ts'o dd4f565d5a libext2fs: avoid 32-bit overflow in ext2fs_initialize with a 512M cluster size
If the user attemps to create a 512MB cluster, we need to adjust the
defaults to avoid a 32-bit overflow of s_blocks_per_group.  Also check
to make sure that the caller of ext2fs_initialize() has not given a
value of s_clusters_per_group that would result in an overflow of
s_blocks_per_group.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Zheng Liu <wenqing.lz@taobao.com>
2013-01-15 14:08:18 -05:00
Theodore Ts'o df5e35fd63 Merge branch 'maint' into next
Conflicts:
	misc/mke2fs.c
2013-01-08 21:14:25 -05:00
Theodore Ts'o f0257d8891 libext2fs: ext2fs_open2() should not set ret_fs after a MMP failure
The addition of MMP code was added in the wrong place, so ret_fs could
get set (and EXT2_FLAG_NOFREE_ON_ERROR was cleared as well, which
could confuse e2fsck which depends on this flag being cleared if
ext2fs_open2() succeeded.)

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-08 20:47:11 -05:00
Theodore Ts'o 827f45243a Merge branch 'maint' into next
Conflicts:
	resize/resize2fs.c
2013-01-05 10:15:04 -05:00
Theodore Ts'o 027b0577d4 Fix 32-bit overflow problems: dgrp_t * s_blocks_per_group
There are a number of places where we multiply a dgrp_t with
s_blocks_per_group expecting that we will get a blk64_t.  This
requires a cast, or using the convenience function
ext2fs_group_first_block2().

This audit was suggested by Eric Sandeen.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
2013-01-05 10:14:11 -05:00
Theodore Ts'o a0ba54ec00 resize2fs: use [un]mark_block_range bitmap functions to reduce CPU usage
Use ext2fs_[un]mark_block_range2() functions to reduce the CPU
overhead of resizing large file systems by 45%, primarily by
reducing the time spent in fix_uninit_block_bitmaps().

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-03 09:03:53 -05:00
Theodore Ts'o b9b5e43ea8 Merge branch 'maint' into next
Conflicts:
	RELEASE-NOTES
	debian/changelog
	lib/ext2fs/crc32c.c
	lib/ext2fs/rw_bitmaps.c
	resize/resize2fs.c
	version.h
2013-01-01 18:50:26 -05:00
Theodore Ts'o e3507739e4 Fix gcc -Wall nits
This fixes the last set of gcc -Wall complaints.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-01-01 13:28:27 -05:00
Andreas Dilger 00eb0eee0a build: quiet some "gcc -Wall" compiler warnings
Quiet a number of simple compiler warnings:
- pointers not initialized by ext2fs_get_mem()
- return without value in non-void function
- dereferencing type-punned pointers
- unused variables

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-12-29 17:39:45 -05:00
Andreas Dilger 1d6fd6d0c3 misc: cleanup unused variables on MacOS
Clean up unused variables found by GCC on MacOS.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-12-29 17:28:37 -05:00
Andreas Dilger f797cf3e37 tests: don't consider "make check" a compiler error
In a number of places, the output format from "make check" is
incorrectly interpreted as compiler warning output (triggered by
the presence of colons and parenthesis in the output).  Convert
these lines to similar output that does not trigger false build
warnings.

In the case of the tst_uuid.c program, the "ctime()" output was
difficult to change, but in fact it is better to actually compare
the time-based UUID against wallclock time instead of just printing
the formatted time as a string, so this test is improved.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-12-29 17:13:24 -05:00
Theodore Ts'o ab83252174 Merge branch 'maint' into next
Conflicts:
	e2fsck/problem.c
	e2fsck/problem.h
2012-12-24 10:50:20 -05:00
Theodore Ts'o 068136c31e libext2fs: link the test programs with static libraries
Force the use of the static libraries when linking the test program so
that "make check" works when the shared libraries have not been
installed, and so that we test against the version of the libraries in
the source tree.

Reported-by: g.esp@free.fr
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-12-24 10:49:45 -05:00
Theodore Ts'o bee7b67ce9 debugfs: add the ability to manipulate the extent tree directly
This commit adds the functionality which had previously only been in
the tst_extents command to debugfs.  The debugfs command extent_open
will open extent tree of a particular inode, and enables a series of
commands which will allow the user to interact with the extent tree
directly.  Once the extent tree is closed via extent_open(), these
additional commands will be disabled again.

This commit exports two new functions from lib/ext2fs/extent.c which
had previously been statically defined: ext2fs_extent_node_split() and
ext2fs_extent_goto2().

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-12-24 10:49:41 -05:00
Theodore Ts'o eacf6a0878 libext2fs: ext2fs_extents_fix_parents() should not modify the handle location
Previously, ext2fs_extent_fix_parents() would only avoid modifying the
cursor location associated with the extent handle the cursor was
pointed at a leaf node in the extent tree.  This is because it saved
the starting logical block number of the current extent, but not the
"level" of the extent (where level 0 is the leaf node, level 1 is the
interior node which points at blocks containing leaf nodes, etc.)

Fix ext2fs_extent_fix_parents() so it is guaranteed to not change the
current extent in the handle even if the current extent is not at the
bottom of the tree.

Also add a fix_extent command to the tst_extents program to make it
easier to test this function.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-12-20 18:27:33 -05:00
Eric Sandeen 789bd401c3 e2fsck: fix incorrect interior node logical start values
An index node's logical start (ei_block) should
match the logical start of the first node (index
or leaf) below it.  If we find a node whose start
does not match its parent, fix all of its parents
accordingly.

If it finds such a problem, we'll see:

Pass 1: Checking inodes, blocks, and sizes
Interior extent node level 0 of inode 274258:
Logical start 3666 does not match logical start 4093 at next level.  Fix<y>?

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-12-20 14:07:56 -05:00
Theodore Ts'o 0a7baccf61 Merge branch 'maint' into next 2012-12-17 09:58:50 -05:00
Mikael Pettersson dbe5c43b2e libext2fs: remove m68k-specific bitops code
The current m68k code was buggy for multiple reasons; first the bfset,
et. al commands interpret the bit number as a signed number, not an
unsigned number.  Secondly, there were missing memory clobbers.  Since
there is no real benefit in using explicit asm's at this point (gcc is
smart enough to optimize the generic C code to use the set/clear/test
bit m68k instruction) fix this bug by removing the m68k specific asm
versions of these functions.

Tested on m68k-linux with e2fsprogs-1.42.6 and gcc-4.6.3 as before.
All tests pass and the debug output looks sane.

I compared the e2fsck binaries from the previous build with this
one.  They had identical .text sizes, and almost the same number
of bit field instructions (obviously compiler-generated), so this
change should have no serious performance implications.

Signed-off-by: Mikael Pettersson <mikpe@it.uu.se>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Cc: Andreas Schwab <schwab@linux-m68k.org>
2012-12-17 09:46:33 -05:00
Theodore Ts'o 4e0bb5eb74 libext2fs: fix memory and fd leak in error path of unix_open()
Fix a potential memory leak reported by Li Xi.  In addition, there
were possible error cases where the file descriptor would not be
properly closed, so fix those as well while we're at it.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reported-by:  Li Xi <pkuelelixi@gmail.com>
2012-12-16 20:14:20 -05:00
Gregoire Pichon 1564150d31 mke2fs: fix handling of mmp_update_interval option
Make sure the s_mmp_update_interval super block field is set
from the file system parameters block which is passed into the
ext2fs_initialize() function.

Addresses-Lustre-Bug: LU-1888

Signed-off-by: Gregoire Pichon <gregoire.pichon@bull.net>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-12-15 22:36:37 -05:00
Theodore Ts'o 603e5ebc8b libext2fs: allocate separate memory regions for each inode in the cache
The changes to support metadata checksum allocated a single large
array for all of the inodes in the inode cache.  This is slightly more
efficient, but given that the inode cache is small (only 4 inodes) it
doesn't really have that much benefit.  The problem with doing things
this way is that the memory overruns, such as the one fixed in commit
43c4910371, do not get detected by valgrind.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-11-29 20:40:21 -05:00
Eric Whitney 43c4910371 libext2fs: fix inode cache overruns
An inode cache slot will be overrun if a caller to ext2fs_read_inode_full()
or ext2fs_write_inode_full() attempts to read or write a full sized 156
byte inode when the target filesystem contains 128 byte inodes.  Limit the
copied inode to the smaller of the target filesystem's or the caller's
requested inode size.

Signed-off-by: Eric Whitney <enwlinux@gmail.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-11-29 19:59:41 -05:00
Theodore Ts'o ccea20f62d Merge branch 'maint' into next
Conflicts:
	misc/tune2fs.c
2012-11-29 19:54:46 -05:00
Theodore Ts'o b65ccfc7a4 libext2fs: optimize rb_get_bmap_range() for mostly allocated bmaps
This optimizies the CPU utilization of the rb_get_bmap_range()
function when most of the bitmap is allocated.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
2012-11-28 19:01:51 -05:00
Theodore Ts'o c3f9641eaf libext2fs: optimize rb_get_bmap_range()
This simplifies the rb_get_bmap_range() function and speeds it up for
the case where most of the bitmap is zero.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
2012-11-28 19:01:51 -05:00
Theodore Ts'o c6b006ea6c libext2fs: add ext2fs_bitcount() function
This function efficiently counts the number of bits in a block of
memory.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
2012-11-28 19:01:43 -05:00
Theodore Ts'o fc8ea52033 libext2fs: optimize rb_set_bmap_range()
This speeds up reading bitmaps from disk for very large (and full)
disks by significant amounts (i.e., up to two CPU minutes for a 4T
file system).

Addresses-Google-Bug: #7534813

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
2012-11-27 12:37:34 -05:00
Andreas Dilger 3e554cc116 ext2fs, blkid: localize environment-specific variables
Restructure the ext2fs_get_device_size() and blkid_get_dev_size()
code to localize the variables used for different device probing
methods.  This at least reduces the #ifdef mess to only one part
of the code for each method, and avoids "unused variable" compiler
warnings added when variables are declared without being #ifdef'd.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-11-26 09:34:20 -05:00
Andreas Dilger ab00fdac7c libext2fs: change mmp.c to LGPL to match other files
Change the license of the mmp.c file to LGPL to match the license
of other files in the libext2fs library.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-11-26 09:32:52 -05:00
Theodore Ts'o 41bf599391 debugfs: teach the htree and ls commands to show directory checksums
In addition, make the directory interator more robust in the case
where the file system has the metadata checksum feature enabled, but
the directory checksum is not present in a directory block.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-10-14 06:35:26 -04:00
Theodore Ts'o 3a4fd4c84d Merge branch 'maint' into next
Conflicts:
	lib/ext2fs/blkmap64_rb.c
2012-10-11 07:38:01 -04:00
Theodore Ts'o fb129bba73 libext2fs: further optimize rb_test_bit
Profiling shows that rb_test_bit() is now calling ext2fs_rb_next() a
lot, and this function is now the hot spot when running e2freefrag.
If we cache the results of ext2fs_rb_next(), we can eliminate those
extra calls, which further speeds up both e2freefrag and e2fsck by
reducing the amount of CPU time spent in userspace.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-10-11 06:30:35 -04:00
Theodore Ts'o 0bcba36f3f libext2fs: remove pointless indirection in rbtree bitmaps
The code was previously allocating a single 4 or 8 byte pointer for
the rcursor and wcursor fields in the ext2fs_rb_private structure;
this added two extra memory allocations (which could fail), and extra
indirections, for no good reason.  Removing the extra indirection also
makes the code more readable, so it's all upside and no downside.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
2012-10-11 06:30:34 -04:00
Theodore Ts'o 547a59a821 libext2fs: optimize rb_test_bit
Optimize testing for a bit in an rbtree-based bitmap for the case
where the calling application is scanning through the bitmap
sequentially.  Previously, we did this for a set of bits which were
inside an allocated extent, but we did not optimize the case where
there was a large number of bits after an allocated extents which were
not in use.

             1111111111111110000000000000000000
             ^ optimized    ^not optimized

In my tests of a roughly half-filled file system, the run time of
e2freefrag was halved, and the cpu time spent in userspace was during
e2fsck's pass 5 was reduced by a factor of 30%.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Reviewed-by: Lukas Czerner <lczerner@redhat.com>
2012-10-11 06:30:16 -04:00
Theodore Ts'o 2f2a048b1d Merge branch 'maint' into next 2012-09-22 21:35:46 -04:00
Theodore Ts'o c498cb11d3 mke2fs: fix progress suppression to make regression tests reliable
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-09-22 21:26:48 -04:00
Theodore Ts'o ae67aaac0e libext2fs: always define i_checksum_lo
Always #define i_checksum_lo, since otherwise e2fsprogs won't compile
on non-Linux platforms.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-09-22 09:02:52 -04:00
Theodore Ts'o 43c67c4f73 Merge branch 'maint' into next
Conflicts:
	debian/changelog
	version.h
2012-09-21 12:55:45 -04:00
Theodore Ts'o b2f5ce5516 mke2fs: throttle allocating groups progress as well
Throttle updates for the "Allocating Groups" progress updates to once
a second as well.  We now do this throttling in libext2fs, so we don't
have to do this for each of mke2fs's progress updates, and because the
updates from ext2fs_allocate_tables() come from within libext2fs
anyway.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-09-21 12:50:04 -04:00
Theodore Ts'o 0e7e7c8ef8 Merge branch 'maint' into next
Conflicts:
	lib/ext2fs/ext2fs.h
2012-09-09 22:52:26 -04:00
Theodore Ts'o daf57eaaae ext2fs.h: move ext2fs_init_csum_seed() outside of EXT2_CUSTOM_MEMORY_ROUTINES
The function ext2fs_init_csum_seed() has nothing to do with the
ext2fs_get_mem()/ext2fs_get_memzero()/ext2fs_get_array()/ext2fs_get_arrayzero()
functions.  (This define is there so that on platforms where we need
to use the standard C functions, they can be replaced --- this is
primarily needed when trying to compile libext2fs for strange,
non-quite-standards-compliant platforms, such as Windows.)

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-09-09 21:43:08 -04:00
Theodore Ts'o ab3f5c5aad Allow e2fsprogs to be built using the clang (LLVM) frontend
Since clang uses C99 semantics by default, the main changes required
to allow clang to build e2fsprogs was to add support the C99 inline
semantics, while still allowing us to be built when the legacy (but
still default for gcc) GNU C89 inline semantics are in force.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-09-09 21:35:39 -04:00
Akira Fujita 321649c9f4 mke2fs: recalculate the reserved blocks when the last BG is dropped
mke2fs -m option can set reserved blocks ratio up to 50%.  But if the
last block group is not big enough to support the necessary data
structures, it gets dropped, we have to recalculate the number of
reserved blocks so that the reserved blocks matches the requested
percentage.

It also avoids a problem where if the user specifies a reserved blocks
of 50%, and after the last partial block group was dropped, if the
number of reserved blocks is greater than 50%, e2fsck will complain.

Steps to reproduce:

1. Create a FS which has the overhead for the last BG
   and specify 50 % for reserved blocks ratio
 # mke2fs -m 50 -t ext4 DEV 1025M

 mke2fs 1.42.5 (29-Jul-2012)
 warning: 256 blocks unused.

 Filesystem label=
 OS type: Linux
 Block size=4096 (log=2)
 Fragment size=4096 (log=2)
 Stride=0 blocks, Stripe width=0 blocks
 656640 inodes, 2621440 blocks
 1310848 blocks (50.00%) reserved for the super user
 ~~~~~~~ <-- Reserved blocks exceed 50% of FS blocks count!

2. e2fsck outputs filesystem corruption
 # e2fsck DEV

 e2fsck 1.42.5 (29-Jul-2012)
 Corruption found in superblock.  (r_blocks_count = 1310848).

 The superblock could not be read or does not describe a correct ext2
 filesystem.  If the device is valid and it really contains an ext2
 filesystem (and not swap or ufs or something else), then the superblock
 is corrupt, and you might try running e2fsck with an alternate superblock:
     e2fsck -b 32768 <device>

Signed-off-by: Akira Fujita <a-fujita@rs.jp.ne.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-09-07 00:09:19 -04:00
Zheng Liu bf5dd0aec4 libe2p/libext2fs: add EXT4_INLINE_DATA_FL flag
[ Also teach libe2p's print_flags() function to display this flag so
  that lsattr will allow us to see whether a file has inline data or not.
  --tytso ]

Signed-off-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-08-07 14:06:47 -04:00
Theodore Ts'o 948461584a libext2fs: rename INCOMPAT_INLINEDATA to INCOMPAT_INLINE_DATA
This is what the patches from Zhen Liu uses, so let's make this change
now to keep things easier.  INCOMPAT_INLINE_DATA also looks better
IMHO.  :-)

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-08-07 13:53:22 -04:00
Theodore Ts'o 2aac47776f libext2fs: refactor the quota feature flag in the supported flags mask
Handle EXT4_FEATURE_RO_COMPAT_QUOTA the same way we handle INCOMPAT
features, so we don't have to have two definitions for
EXT2_LIB_FEATURE_RO_COMPAT_SUPP depending on whether or not
CONFIG_QUOTA is enabled or not.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-08-07 13:46:13 -04:00
Theodore Ts'o 2d913dbcdf Merge branch 'maint' into next 2012-08-06 21:09:49 -04:00
Theodore Ts'o d5aa6a82b3 Put ELF_OTHER_LIBS in the right place for the linker
Commit a7c17431b9 attempted to fix a problem where the system
libraries might get used instead of local libraries for things like
-lcom_err.  It tried to accomplish this by moving $(ELF_OTHER_LIBS) to
before $(LDFLAGS).

Unfortunately, this was the wrong fix; $(ELF_OTHER_LIBS) *MUST* be
after the object files, or the linker might not pull in the necessary
library and not include it into the DT_NEEDED section of the shared
library.  The proper fix is to add a -L$(LIB) before $(LDFLAGS), and
then remove the -L option from all of the ELF_OTHER_LIBS definitions
in the library Makefiles.

Addresses-Sourceforge-Bug: #3554345

Cc: Olivier Blin <olivier.blin@softathome.com>
Reported-by:  Mike Frysinger <vapier@gentoo.org>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-08-06 12:30:49 -04:00
Theodore Ts'o 97fac35094 libext2fs: when checking the inode's checksum, allow an all-zero inode
When the kernel writes an inode where all of the other inodes in in
the inode table (itable) block are unused, it skips reading the itable
block from disk, and instead uses an all zeros block.  This can cause
e2fsck to complain when it iterates over the inodes using
ext2fs_get_next_inode() since the inode apparently has an invalid
checksum.  Normally the inode won't be returned at all if it is at the
end of the block group's part of the inode table, thanks to the
bg_itable_unused field.  But it's possible for this situation to
happen earlier in the inode table block.

Fix this by changing ext2fs_inode_csum_verify() to allow the inode to
be all zero's; if the checksum fails, and the inode is all zero's,
treat it as a valid checksum.

Reported-by: Tao Ma <boyu.tm@taobao.com>
Reported-by: Zheng Liu <wenqing.lz@taobao.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-08-02 20:47:46 -04:00
Darrick J. Wong 033c338490 libext2fs: optimize the CRC32c implementation
The crc32c implementation in the kernel has been refactored a bit to
reduce the amount of code that needs to be maintained, and to speed up
tune2fs/e2fsck on PowerPC by 5-10%.  Port the crc32c changes over, and
provide a crc32_be so that we can remove the duplicate functionality
from e2fsck.  Also drop crc32c_be and crc32_le since neither got used.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:46 -04:00
Darrick J. Wong 3e752286b9 libext2fs: enable support for the metadata checksumming feature
Add metadata checksumming to the list of supported features.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:46 -04:00
Darrick J. Wong d920038068 e2fsck: verify data block checksums when recovering journal
Check the data block checksums when recovering the journal.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:46 -04:00
Darrick J. Wong 2556373a33 libext2fs: change on-disk journal layout to support metadata checksumming
Define flags and change journal structure definitions to support v2 journal
checksumming.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:45 -04:00
Darrick J. Wong a9620d8b30 libext2fs: add checksum to MMP block
Calculate and verify a checksum of the MMP block.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:45 -04:00
Darrick J. Wong 5b58dc2304 libext2fs: block group checksum should use metadata_csum algorithm
Change the block group algorithm to use the same algorithm as the rest
of the metadata_csum.  This mostly involves providing a helper
function to tell if group descriptors should have checksums set or
verified, and modifying the gdt checksum code to use the correct
algorithm.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:45 -04:00
Darrick J. Wong 1eedf3e52c libext2fs: record the checksum algorithm in use in the superblock
Record the type of checksum algorithm we're using for metadata in the
superblock, in case we ever want/need to change the algorithm.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:44 -04:00
Darrick J. Wong d4ca3e40ca libext2fs: calculate and verify superblock checksums
Calculate and verify the superblock checksums.  Each copy of the
superblock records the number of the group it's in and the FS UUID, so
we can simply checksum the whole block.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:44 -04:00
Darrick J. Wong 39f5659ae3 libext2fs: verify and calculate extended attribute block checksums
Calculate and verify the checksum for separate (i.e. not in the inode)
extended attribute blocks; the checksum lives in the header.

[ Merged in change from Tao so that we always use the fs checksum seed
  for the xattr blocks. ]

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Tao Ma <boyu.mt@taobao.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 20:47:44 -04:00
Darrick J. Wong 81683c6a32 libext2fs: add checksums to the end of directory leaf nodes
Introduce small structures for recording directory tree checksums, and
some API changes to support writing out directory blocks with
checksums.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-08-02 17:27:43 -04:00
Darrick J. Wong dc96de09d7 libext2fs: add dx_root/dx_node checksum calculation and verification helpers
Verify and calculate checksums of htree internal node blocks.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 19:22:04 -04:00
Darrick J. Wong 41275efeee libext2fs: verify and calculate extent tree block checksums
Verify and calculate extent tree block checksums when processing
filesystems.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 19:14:04 -04:00
Darrick J. Wong e7dc95c677 libext2fs: create the block bitmap checksum
Calculate and verify the block bitmap checksum.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 19:04:04 -04:00
Darrick J. Wong a80ea34023 libext2fs: create the inode bitmap checksum
Provide a field in the block group descriptor to store inode bitmap
checksum, and some helper functions to calculate and verify it.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 18:56:04 -04:00
Darrick J. Wong 37d82b6a95 libext2fs: add inode checksum support
This patch adds the ability for the libext2fs functions to read and
write the inode checksum.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 18:46:04 -04:00
Darrick J. Wong 4ffbe3afa9 libext2fs: precompute FS UUID checksum seed
Precompute the FS UUID checksum seed that is used for all metadata
checksumming operations and store it in ext2_filsys.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 18:44:04 -04:00
Darrick J. Wong 5d914d721a libext2fs: change ext4 on-disk layout to support metadata checksumming
Define flags and extend ext4 structure definitions to support metadata
checksumming.  Ted Ts'o covered many of these fields in an earlier
patch, but there are more required changes to the disk layout.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 17:44:15 -04:00
Darrick J. Wong 91db7e206d libext2fs: read and write full size inodes
Change libext2fs to read and write full-size inodes in preparation for
the metadata checksumming patchset, which will require this.  Due to
ABI compatibility requirements, this change must be hidden from client
programs.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 17:42:15 -04:00
Theodore Ts'o 8718cb66c8 libext2fs: enforce the block group descriptor size in ext2fs_open()
Since various parts of the library depend on the value of s_desc_size,
check to make sure it is the correct, expected value based on the file
system features.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-07-30 17:40:15 -04:00
Theodore Ts'o f3ce48a4b6 libext2fs: remove debugging printf from ext2fs_group_desc_csum
This reduces the number of C library symbols needed by boot loader
systems such as yaboot.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-07-30 17:39:25 -04:00
Theodore Ts'o dfe74c5c67 libext2fs: call numeric_progress functions through a operations struct
Instead of calling ext2fs_numeric_progress_*() directly from closefs.c
and alloc_tables.c, call it via a operations structure which is only
initialized by the one program (mke2fs) which needs it.

This reduces the number of C library symbols needed by boot loader
systems such as yaboot.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-07-30 17:19:19 -04:00
Theodore Ts'o bc0b1a899a libext2fs: move ext2fs_get_num_dirs to its own file
This reduces the number of C library symbols needed by boot loader
systems such as yaboot.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-07-30 17:19:15 -04:00
Theodore Ts'o c4111cd7ea libext2fs: use strcpy()/strcat() instead of sprintf() in bmap functions
This simplifies the number of C library symbols needed by boot loader
systems such as yaboot.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-07-30 15:54:12 -04:00
Theodore Ts'o 1b6de47f65 libext2fs: use abort() instead of perror()/exit()
This simplifies the number of C library symbols needed by boot loader
systems such as yaboot.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-07-30 15:46:26 -04:00
Tony Breeds 567e7a36ee libext2fs: only define ba_print_stats() when needed
If ENABLE_BMAP_STATS isn't defined make ba_print_stats() do nothing.

Signed-off-by: Tony Breeds <tony@bakeyournoodle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 14:51:59 -04:00
Tony Breeds 1625bf42d6 Add support so that all bmap statistics via configure options
Signed-off-by: Tony Breeds <tony@bakeyournoodle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 14:50:53 -04:00
Tony Breeds d6a4bcb517 Make Multi Mount Protection (MMP) optional at configure time.
Add --{en,dis}able-mmp options for configure, default to enabled.
Also make tools fail gracefully in the event of encoutering a filesystem
with MMP enabled when the tools were compiled with --disable-mmp

Signed-off-by: Tony Breeds <tony@bakeyournoodle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-30 14:50:30 -04:00
Tony Breeds 119f1ad8c9 ext2fs.h: Change how we deal with optional incompat features.
Currently EXT2_LIB_FEATURE_INCOMPAT_SUPP is #defined twice once with
EXT2_FEATURE_INCOMPAT_COMPRESSION and once without depending on the
state of ENABLE_COMPRESSION

Change this to use an intermediate symbol so that the definition of
EXT2_LIB_FEATURE_INCOMPAT_SUPP doesn't change as other optional fetures
are added.

Signed-off-by: Tony Breeds <tony@bakeyournoodle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-29 20:52:00 -04:00
Theodore Ts'o 3aa8f60aae libext2fs: fix uninit block calculation when inodes_per_block < first_ino
The following commands:

	dd if=/dev/zero of=/tmp/foo count=1 ibs=$(( 256 * 1024 * 1024 ))
	mke2fs -N 256 -t ext4 /tmp/foo

... will cause mke2fs to write until it fills the device.  The cause
for this is that the explicit request for 256 inodes causes the number
of inodes per block group to be 8.  The ext2fs_initialize() function
assumed that all of the reserved inodes would be in the first block
group, which is not true in this case.  This caused the number of
uninitialized inodes in the first block group to be negative, which
then resulted in mke2fs trying to zero out a very large number of
blocks.  Oops.

Addresses-Sourceforge-Bug: #3528892

Reported-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-07-29 13:34:01 -04:00
Theodore Ts'o c1986ecb6f Fix blhc (Build Log Hardening Check) warnings
The Build Log Hardening Check is a debian tool which scans the output
of a package build making sure that the security hardening flags are
used when compiling and linking all of binaries in a package.

For the most part we were passing CFLAGS, CPPFLAGS, and LDFLAGS down
to the compiler and link commands, but there there were one or two
exceptions.  In addition, there where a few places in "make install"
where the V=1 option was not being honored, which triggered blhc
warnings since it couldn't analyze those commands.

The e2fsck.static was the only binary that was not getting built and
packaged with the hardening flags, but I've fixed all of the blhc
warnings so in the future it will be obvious if we regress.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-06-13 16:06:44 -04:00
Theodore Ts'o 0796e66085 lsattr, chattr: add support for btrfs's No_COW flag
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-06-12 17:30:10 -04:00
Theodore Ts'o beb388a473 libext2fs: fix block iterator for extents when truncating inodes
When e2fsck uses the block iterator to release the blocks in an
extent-mapped inode, when the last block in an extent is removed, the
current extent has been removed and the extent cursor is now pointing
at the next inode.  But the block iterator code doesn't know that.  So
when it tries to go the next extent, it will end up skipping an
extent, and so the inode will be incompletely truncated.

The fix is to go to the next extent before calling the callback
function for the current extent.  This way, regardless of whether the
current extent gets removed, the extent cursor is still pointing at
the right place.

Reported-by: Andreas Dilger <adilger@whamcloud.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-06-12 01:34:10 -04:00
Aditya Kali e0e2c7db4d libext2fs: fix i_blocks for extent leaf create/delete with bigalloc
When libext2fs allocates/deletes an extent leaf, the i_blocks
value is incremented/decremented by fs->blocksize / 512. This
is incorrect in case of bigalloc. The correct way here is to
use cluster_size / 512.

The problem is seen if we try to create a large inode using
libext2fs (say using ext2fs_block_iterate3()) on a bigalloc
filesystem. fsck catches this and complains.

Signed-off-by: Aditya Kali <adityakali@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-06-10 23:52:11 -04:00
Andreas Dilger 182acd17be libext2fs: don't inline ext2fs_open_file() and ext2fs_stat()
The creation of inline wrappers ext2fs_open_file() and ext2fs_stat()
in commit c859cb1de0 in ext2fs.h caused
difficulties with the use of headers, since the headers for open64()
and stat64() may already be included (and skip the declaration of the
64-bit variants) before ext2fs.h is ever read.  There is no real way
to solve the missing prototypes and resulting compiler warnings inside
ext2fs.h.

Since ext2fs_open_file() and ext2fs_stat() are not performance
critical operations, they do not need to be inline functions at all,
and the needed function headers can be handled properly in one file.

Similarly, posix_memalloc() was having difficulties with headers, and
was being defined in ext2fs.h, but it is now only being used by a
single file, so move the required header there.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-05-28 10:54:12 -04:00
Eric Sandeen 918eeb32e9 libext2fs: fix rbtree backend for extent lengths greater than 2^32
For a completely full filesystem with more than 2^32 blocks, the
rbtree bitmap backend can assemble an extent of used blocks which is
longer than 2^32.  If it does, it will overflow ->count, and corrupt
the rbtree for the bitmaps.

Discovered by completely filling a 32T filesystem using fallocate, and
then observing debugfs, dumpe2fs, and e2fsck all behaving badly.

(Note that filling with only 31 x 1T files did not show the problem,
because freespace was fragmented enough that there was no sufficiently
long range of used blocks.)

Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-05-27 22:14:29 -04:00
Theodore Ts'o ae06582867 Change pkg-config files so that both <et/com_err.h> and <com_err.h> work
Change the include path in the Cflags field so that #include
<lib/foo.h> and <foo.h> will work.  We had originally used a C flags
which allowed <foo.h> to work, but many applications (especially those
not using pkg-config) had been using the <lob/foo.h> formulation which
didn't require an explicit -I{$includedir} option to the C compiler.

If those applications then converted over to pkg-config, and the
e2fsprogs libraries were installed with a prefix other than /usr, so
that the header files were in some directory such as
/usr/local/include, a program that used #include <lib/foo.h> would
fail to compile.

So change the pkg-config files to include both -I{$includedir} and
-I{$includir}/lib.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-05-14 12:45:54 -04:00
Theodore Ts'o 40024fdbaf libext2fs: fix bug in unix_io corrupted > 16TB file systems on 32-bit systems
The code was assuming that "unsigned long" was 64-bit, which of course
it isn't on 32-bit systems.  This caused blocks to get written to the
wrong place.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-05-14 10:55:09 -04:00
Theodore Ts'o 660b4c3b3f Reserve the codepoints for the INCOMPAT features LARGEDATA and INLINEDATA
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-05-11 18:39:27 -04:00
Theodore Ts'o d71520751e Support systems without posix_memalign() and memalign()
MacOS 10.5 doesn't have posix_memalign() nor memalign(), but it does
have valloc().  The Android SDK would like to be built on MacOS 10.5,
so I've added support for a good-enough emulation of memalign()'s
functionality using valloc(), with an explicit test to make sure
valloc() is returning a pointer which is sufficiently aligned given
the requested alignment.  This won't work if you try to operate on a
file system with a 16k blocksize using an e2fsprogs built on MacOS
10.5 system, but it is good enough for the common case of 4k
blocksize file systems, and we will let the memory allocation fail in
the alignment is not good enough.

I've also added a unit test for ext2fs_get_memalign() so we can be
sure it's working as expected.  I've tested the code paths with
HAVE_POSIX_MEMALIGN defined, HAVE_POSIX_MEMALIGN undefined, and
HAVE_POSIX_MEMALIGN and HAVE_MEMALIGN undefined on an x86 Linux
system, and so I know the valloc() code path works OK.  The simplistic
(and less safe) patch at:

https://trac.macports.org/attachment/ticket/33692/patch-lib-ext2fs-inline.c.diff

Shows that using valloc() apparently works OK for MacOS 10.5 (but if
it doesn't the unit test will catch a problem).

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-05-11 18:21:08 -04:00
Theodore Ts'o fd1c5a0622 libext2fs: factor out I/O buffer allocation
Create a new function, io_channel_alloc_buf() which allocates I/O
buffers with appropriate alignment if we are using direct I/O.  The
original code was sometimes using a larger alignment factor than
necessary, and would always request an aligned memory buffer even when
it was not necessary since the block device was not opened with
O_DIRECT.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-05-07 14:41:49 -04:00
Theodore Ts'o 07d120848d libext2fs: make read_bitmaps() more efficient when using direct I/O
Read in a full block for each allocation bitmap, to avoid using a
kernel bounce buffer when using direct I/O.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-05-07 14:30:31 -04:00
Theodore Ts'o dd0a2679dd libext2fs: refactor Direct I/O alignment requirement calculations
Create a new function, ext2fs_get_dio_alignment(), which returns the
alignment requirements for direct I/O.  This way we can factor out the
code from MMP and the Unix I/O manager.  The two modules weren't
consistently calculating the alignment factors, and in particular MMP
would sometimes use a larger alignment factor than was strictly
necessary.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-05-07 13:25:44 -04:00
Theodore Ts'o 0a05b90330 libext2fs: move the alignment field from unix_io to the io_manager
The align field which indicated the required data alignment of data
buffers was stored in a field specific to the unix_io manager.  Move
it to the top-level io_channel structure so it can be better
generalized.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-05-07 12:56:07 -04:00
Theodore Ts'o 46f100707c libext2fs: improve testing coverage of tst_bitmaps
Improve the test coverage of tst_bitmaps by:

   (a) adding the ability to test the legacy (32-bit) bitmap code
   (b) adding tests for ext2fs_find_first_zero_inode_bitmap2() and
       ext2fs_find_first_zero_block_bitmap2()

The recent regressions caused by the addition (and use) of
ext2fs_find_first_zero_inode_bitmap2() would have been caught if we
had added these tests first.  (Another object lesson in why unit tests
are critically important!)

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-04-09 14:29:47 -04:00
Theodore Ts'o 38792ca78b libext2fs: use correct types in ext2fs_find_first_zero_block_bitmap2()
Fortunately nothing was using this inline function, so we'll just fix
the types in its function signature, which were nonsensical (this was
caused by a cut-and-paste error).

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-04-09 14:29:47 -04:00
Theodore Ts'o 664c332612 libext2fs: add 32-bit compat code for ext2fs_find_first_zero_generic_bmap()
The lack of 32-bit support was causing febootstrap to crash since it
wasn't passing EXT2_FLAG_64BITS when opening the file system, so we
were still using the legacy bitmaps.

Also add support for bigalloc bitmap into the ffz functions.

Addresses-Red-Hat-Bugzilla: #808421

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-04-09 14:29:13 -04:00
Theodore Ts'o b24efa2187 Don't assume that the presence of mntent.h means that setmntent() exists
Change autoconf to test for setmntent() and use that to decide whether
to use getmntent() and setmntent(), since some systems don't have
setmntent() but they do have the mntent.h header file.

Also, remove the includes of mntent.h from e2fsck and mke2fs and other
places where it is not needed.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-04-05 15:31:09 -07:00
Theodore Ts'o 304e11c2c1 mke2fs: don't fail creating the journal if /etc/mtab is missing
The ext2fs_add_journal_inode() function calls
ext2fs_check_mount_point(), which can fail if /etc/mtab is missing.
This causes mke2fs to fail in the middle of the file system format
process; mke2fs calls ext2fs_check_mount_point() already (and has
appropriate fallbacks that calls fails), so add a flag so that mke2fs
can request ext2fs_add_journal_inode() to skip trying to call
e2fsck_check_mount_point().

Addresses-Sourceforge-Bug: #3509398

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-04-05 12:30:02 -07:00
Theodore Ts'o e64e6761aa Fix gcc -Wall nitpicks
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-04-05 12:13:05 -07:00
Theodore Ts'o 70d7281dcb Update Release Notes, Changelogs, version.h, etc. for 1.42.2 release
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-03-27 16:20:46 -07:00
Theodore Ts'o 3a6db9bbc8 libext2fs: fix ext2fs_get_memalign when posix_memalign() doesn't exist
Reported by: Gianluigi Tiesi <sherpya@netfarm.it>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-03-26 15:37:28 -07:00
Sami Liedes 11f359f766 libext2fs: implement fast find_first_zero() for bitarray bitmaps.
With this change the CPU time needed to shrink a 100G filesystem drops
to 0.8% of the original (17 CPU seconds instead of 2057).

Signed-off-by: Sami Liedes <sami.liedes@iki.fi>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-03-26 10:36:24 -07:00
Theodore Ts'o 538eb8784a libext2fs: update dirty flags if the uninit flags are cleared
Update the block group descriptor checksum and mark the superblock and
allocation bitmaps as dirty in check_inode_uninit() and
check_block_uninit().

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-03-26 10:36:02 -07:00
Sami Liedes c1a1e7fc24 libext2fs: Implement ext2fs_find_first_zero_generic_bmap().
This function searches a bitmap for the first zero bit within a range.
It checks if there is a bitmap backend specific implementation
available (if the relevant field in bitmap_ops is non-NULL). If not,
it uses a generic and slow method by repeatedly calling test_bmap() in
a loop. Also change ext2fs_new_inode() to use this new function.

This change in itself does not result in a large speedup, rather it
refactors the code in preparation for the introduction of a faster
find_first_zero() for bitarray based bitmaps.

Signed-off-by: Sami Liedes <sami.liedes@iki.fi>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-03-25 18:55:14 -04:00
Sami Liedes 5f7c04972f libext2fs: document EXT2_FLAG_64BITS in ext2fs_open2()
From 5c7302de65d689ad84427649aab4d361b21270df Mon Sep 17 00:00:00 2001
From: Sami Liedes <sami.liedes@iki.fi>
Date: Sat, 10 Mar 2012 22:25:55 +0200
Subject: [PATCH] libext2fs: Document EXT2_FLAG_64BITS in ext2fs_open2().

Signed-off-by: Sami Liedes <sami.liedes@iki.fi>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-03-22 19:48:57 -04:00
Sami Liedes 75556776d3 libext2fs: move a modulo operation out of a hot loop.
Filesystem shrinking in particular is a heavy user of this loop in
ext2fs_new_inode(). This change makes resize2fs use 24% less CPU time
for shrinking a 100G filesystem.

Signed-off-by: Sami Liedes <sami.liedes@iki.fi>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-03-22 19:43:48 -04:00
Lukas Czerner 010dc7b90d e2fsck: remove EXT4_EOFBLOCKS_FL flag handling
We've decided to remove EOFBLOCKS_FL from the ext4 file system entirely,
because it is not actually very useful and it is causing more problems
than it solves. We're going to remove it from e2fsprogs first and then
after the new e2fsprogs version is common enough we can remove the
kernel part as well.

This commit changes e2fsck to not check for EOFBLOCKS_FL. Instead we
simply search for initialized extents past the i_size as this should not
happen. Uninitialized extents can be past the i_size as we can do
fallocate with KEEP_SIZE flag.

Also remove the EXT4_EOFBLOCKS_FL from lib/ext2fs/ext2_fs.h since it is
no longer needed.

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-03-22 19:42:11 -04:00
Theodore Ts'o 37c8db7b20 mke2fs: add option to use direct I/O (mke2fs -D)
This can be useful when using mke2fs on loaded servers, since
otherwise mke2fs can dirty a huge amount of memory very quickly,
leading to other applications not being happy at all.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-03-22 16:00:49 -04:00
Andreas Dilger 963d0f1a94 tests: add field sizes to inode/super struct tests
In addition to validating the ordering of fields within the inode
and superblock structures, also validate the field sizes.  Otherwise
it is possible to incorrectly change the size of one of these fields
without getting any kind of error from these tests.  Failures would
only show up later in the test image checks if the field that is
changed is before another in-use field.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-03-19 23:54:03 -04:00
Theodore Ts'o 87141781aa dumpe2fs: print the expected block group checksum if it is incorrect
This helps to diagnose problems caused by incorrect block group
checksums.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-03-15 12:13:25 -04:00
Mike Frysinger 800766ee4a libext2fs: check for fallocate symbol before using it
If we have newer kernel headers which define FALLOC_FL_PUNCH_HOLE, but we
are on an older glibc which lacks fallocate, we end up trying to use the
func anyways.  Check the ifdef that autoconf already set up for us.

Reported-by: Ortwin Glueck <odi@odi.ch>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-03-06 20:21:39 -05:00
Andreas Dilger 7270fbe7fa build: fix compile warnings on OSX
Clean up some compile warnings related to fstat64(), which is
verbosely deprecated on OSX.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-02-27 01:28:23 -05:00
Mike Frysinger 997a0cf34e libext2fs: include config.h for HAVE_xxx defines
We check HAVE_UNISTD_H but haven't included config.h yet, so we end up
hitting warnings about missing prototypes for close/read/etc... funcs.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-02-27 01:12:53 -05:00
Mike Frysinger b96365f8c2 libext2fs: include sys/time.h for gettimeofday()
Building on my glibc-2.15 system hits a warning:
	gen_bitmap64.c: In function 'ext2fs_alloc_generic_bmap':
	gen_bitmap64.c:127:2: warning: implicit declaration of function
		'gettimeofday' [-Wimplicit-function-declaration]

Include sys/time.h if it's available for the prototype.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-02-27 01:12:53 -05:00
Theodore Ts'o d9a5d37535 libext2fs: support O_DIRECT functionality for Mac OS X
Darwin uses fcntl(fd, F_NOCACHE, 1) instead of the O_DIRECT flag.

Addresses-SourceForge-Bug: #3140289

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-02-20 20:45:52 -05:00
Theodore Ts'o 48b8910820 libext2fs: don't mark the superblock as dirty if the fs was opened r/o
If the file system is read/only opened with a backup superblock, and
the file system has uninit_bg enabled, the super block must not be
marked as dirty; otherwise, ext2fs_close() will call ext2fs_flush(),
which will fail, since the file descriptor for the block device was
opened read/only, and then the file descriptor won't actually be
closed.

This is normally not a problem since most of the time the program will
exit shortly after calling ext2fs_close(), and many programs don't
bother checking the error return from ext2fs_close(), especially if
the file system was opened read/only.

A big exception to this is e2fsck, since it opens and close the file
systems during its startup, and to make matters worse, registers an
error handler which will noisly complain about the failed writes
caused by ext2fs_flush().

Fix this by not marking the superblock as dirty if the file system was
opened read/only.  The changes to the block group descriptors to clear
the uninit bits will still happen, so that e2fsck -n will properly
scan the whole file system.  However, those changes will get dropped
when the file system handle is closed.

Addresses-SourceForge-Bug: #3444351

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-02-20 20:27:58 -05:00
Theodore Ts'o 991211f676 libext2fs, libe2p: Reserve RO_COMPAT_REPLICA feature
The replica is a feature which stores multiple copies of the key
metadata blocks so a single block failure in failure-prone media
(read: certain types of flash storage) doesn't take out the entire
file system.

Discussion on the upstream list proved not to be very positive on this
feature; the arguments were that it added complexity that wasn't
warrented, since common practice in industry is to insist on reliable
media, and if media is unreliable, you're kind of toast anyway (unless
the file system is being used as the back-end store of a cluster file
system where checksuming and data replication is happening above the
local disk file system level).  So, this feature is being developed
out of tree.

We reserve the code points so that other people won't accidentally
step on them.  Since it's not upstream, it's a soft reservation, but
it's not like we have any shortage of RO_COMPAT features.  We are a
bit more tight on reserved inodes, but EXT2_BOOT_LOADER_INO and
EXT2_UNDEL_DIR_INO are not currently used anywhere, and
EXT2_EXCLUDE_INO is a reservation for another out-of-tree feature.
There are no features currently being discussed which require a
reserved inode, but if a need were to arise, we can claw back code
point reservations that were never used or not in tree, as those will
always be considered lower priority than in-tree features.

Cc: Aditya Kali <adityakali@google.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-02-17 15:28:21 -05:00
Darrick J. Wong 3ae682bb0a libext2fs: Add ext2fs_file_get_inode_num call (for fuse2fs)
Add a function to return the inode number of an open file.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-02-17 15:28:21 -05:00
Theodore Ts'o a204236eaf libext2fs: fix BLOCK_ABORT handling in the block iterator for extents
When processing files that contain extents, the block iterator
functions were not properly handling the BLOCK_ABORT bit.  This could
cause problems such as ext2fs_link() adding a directory entry multiple
times.

Thanks to Darrick Wong <djwong@us.ibm.com> for reporting this.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-02-17 14:32:17 -05:00
Darrick J. Wong 9e89dbbd46 libext2fs: when truncating file, punch out blocks at end
Currently, ext2fs_file_set_size2 punches out data blocks between the
end of the file and infinity when truncate_block <= old_truncate
(i.e. when you've made the file longer).  This is not a useful
behavior, particularly since it *fails* to punch out the data blocks
when the file is shortened (i.e. truncate_block < old_truncate).  This
seems to be the result of the test being backwards, so fix the code to
punch only when the file is getting shorter.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-02-17 14:32:10 -05:00
Andreas Dilger 454ac9d56a libext2fs: quiet spurious group checksum errors
If we have to read the backup group descriptor checksums, the UNINIT
flags are cleared to ensure that all of the inodes in the filesystem
are scanned.  However, the code that reset the UNINIT flags did not
reset the group checksum, and this produced many spurious error
messages in e2fsck.

Group descriptor 0 checksum is invalid.  FIXED.
Group descriptor 1 checksum is invalid.  FIXED.
:
:

Recompute checksums after modifying group descriptors to avoid these
error messages.  Remove expected error messages in f_illitable_flexbg.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-02-07 21:11:21 -05:00
Theodore Ts'o 1f572d1f88 libext2fs: display partial path if fs corrupted in ext2fs_get_pathname()
The function ext2fs_get_pathname() used to return EXT2_ET_NO_DIRECTORY
if one of the directories in an inode's pathname is not a directory.
This is not very useful in an emergency, when the file system is
corrupted.  This commit will cause ext2fs_get_pathname() to return a
partial pathname, which should help system administrators trying to
use debugfs to investigate a corrupted file system.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2012-01-17 15:38:31 -05:00
Mike Frysinger 24d364fc43 libext2fs: change getpagesize to sysconf
Newer versions of glibc no longer export the getpagesize() prototype when
using recent versions of POSIX (_XOPEN_SOURCE).  So building tdb.c gives
use implicit function declaration warnings.  Fix the issue by using the
portable sysconf() function which returns the same answer.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-01-09 21:19:48 -05:00
Lukas Czerner 9288e3be66 libext2fs: add bitmap statistics
This feature is especially useful for better understanding how e2fsprogs
tools (mainly e2fsck) treats bitmaps and what bitmap backend can be most
suitable for particular bitmap. Backend itself (if implemented) can
provide statistics of its own as well.

[ Changed to provide basic statistics when enabled with the
  E2FSPROGS_BITMAPS_STATS environment variable -- tytso]

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-12-18 01:12:44 -05:00
Theodore Ts'o 4a61d17c7c libext2fs: adjust the description when copying a bitmap
Label the copy of a bitmap as "copy of ..." so that the bitmap's
description is more descriptive.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-12-18 01:12:44 -05:00
Theodore Ts'o 830b44f438 e2fsck: use different bitmap types as appropriate
Now that we have multiple backend implementations of the bitmap code,
this commit teaches e2fsck to use either the most appropriate backend
for each use case.

Since we don't know for sure if we will get it all right, the default
choices can be overridden via e2fsck.conf.  The various definitions
are shown here, with the current defaults (which may change as we add
more bitmap implementations and as learn what works better).

; EXT2FS_BAMP64_BITARRAY is 1
; EXT2FS_BMAP64_RBTREE is 2
; EXT2FS_BMAP64_AUTODIR is 3
[bitmaps]
	inode_used_map = 2	; pass1
	inode_dir_map = 3	; pass1
	inode_reg_map = 2	; pass1
	block_found_map = 2	; pass1
	inode_bad_map = 2	; pass1
	inode_imagic_map = 2	; pass1
	block_dup_map = 2	; pass1
	block_ea_map = 2	; pass1
	inode_link_info = 2	; pass1
	inode_dup_map = 2	; pass1b
	inode_done_map = 3	; pass3
	inode_loop_detect = 3	; pass3
	fs_bitmaps = 2

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-12-18 01:12:44 -05:00
Theodore Ts'o 0ff7bf3007 libext2fs: use the rbtree bitmap by default when initializing a file system
This change causes the max resident memory of mke2fs, as reported by
/usr/bin/time, to drop from 9296k to 5328k when formatting a 25
gig volume.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-12-18 01:12:44 -05:00
Theodore Ts'o d182831a15 libext2fs: add pseudo bitmap backend type EXT2FS_BMAP64_AUTODIR
This backend type will automatically switch between the bitarray and
the rbtree backend based on the number of directories in the file
system.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-12-18 01:12:43 -05:00
Lukas Czerner c1359d9195 libext2fs: add a bitmap implementation using rbtree's
For a long time we had a bitarray backend for storing filesystem
metadata bitmaps, however today this approach might hit its limits with
todays huge data storage devices, because of its memory utilization.

Bitarrays stores bitmaps as ..well, as bitmaps. But this is in most
cases highly unefficient because we need to allocate memory even for the
big parts of bitmaps we will never use, resulting in high memory
utilization especially for huge filesystem, when bitmaps might occupy
gigabytes of space.

This commit adds another backend to store bitmaps. It is based on
rbtrees and it stores just used extents of bitmaps. It means that it can
be more memory efficient in most cases.

I have done some limited benchmarking and it shows that rbtree backend
consumes approx 65% less memory that bitarray on 312GB filesystem aged
with Impression (default config). This number may grow significantly
with the filesystem size, but also it may be a lot lower (even negative)
if the inodes are very fragmented (need more benchmarking).

This commit itself does not enable the use of rbtree backend.

[ Simplified the code by avoiding unneeded memory allocation and
  deallocation of del_ext.  In addition, fixed a bug discovered by the
  tst_bitmaps tests: rb_unamrk_bmap() must return true if the bit was
  previously set in bitmap, and zero otherwise -- tytso ]

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-12-18 01:12:40 -05:00
Lukas Czerner 5db745a283 libext2fs: add rbtree library
This commit adds rbtree library into e2fsprogs so it can be used for
various internal data structures. The rbtree implementation is ripped of
kernel rbtree implementation with small changes needed for it to work
outside kernel.

[ I prefixed the exported symbols and interface with ext2fs_ to keep
  avoid pulluting the namespace exported by the libext2fs shared
  library.  -- tytso ]

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-12-18 01:00:44 -05:00
Theodore Ts'o b8ad88b3e7 libext2fs: add tests for the bitmap functions
These tests allow us to be sure that the new bitmap backends are
correctly implemented.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-12-18 01:00:38 -05:00