Commit Graph

29 Commits (d530271fb39cbf7fddf9fea55b4e4cf74142a98b)

Author SHA1 Message Date
Theodore Ts'o 7719e69a5c e4defrag: fix build failure if libc doesn't have sync_file_range()
The posix_fadvise() to hint to the system that the file can be removed
from memory will probably not work well without the sync_file_range(2)
call, but e4defrag should still fundamentally work, and this will
allow e4defrag to compile if the C library doesn't happen this system
call exposed.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2015-09-05 22:18:29 -04:00
Darrick J. Wong 63cd76d6ac misc: fix Coverity bugs
Fix Coverity bugs 1297093, 1297096, 1297489, 1297491, 1297493,
1297506, 1297507, 1297514, 1297516, and 1297517.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2015-05-16 18:32:33 -04:00
Darrick J. Wong c7c539e8fd e4defrag: backwards-allocated files should be defragmented too
Currently, e4defrag avoids increasing file fragmentation by comparing
the number of runs of physical extents of both the original and the
donor files.  Unfortunately, there is a bug in the routine that counts
physical extents, since it doesn't look at the logical block offsets
of the extents.  Therefore, a file whose blocks were allocated in
reverse order will be seen as only having one big physical extent, and
therefore will not be defragmented.

Fix the counting routine to consider logical extent offset so that we
defragment backwards-allocated files.  This could be problematic if we
ever gain the ability to lay out logically sparse extents in a
physically contiguous manner, but presumably one wouldn't call defrag
on such a file.

Reported-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-07-22 12:40:56 -04:00
Lukas Czerner 47fee2ef6a e2fsprogs: introduce ext2fs_close_free() helper
Currently there are many uses of ext2fs_close() which might be wrong.
First of all ext2fs_close() does not set the ext2_filsys pointer to NULL
so the caller is responsible for clearing it, however there are some
cases there we do not do it.

Second of all very small number of users of ext2fs_close() actually
check the return value. If there is a problem in ext2fs_close() it will
not even free the ext2_filsys structure, but majority of users expect it
to do so.

To fix both problems this commit introduces a new helper
ext2fs_close_free() which will not only check for the return value and
free the ext2_filsys structure if the call to ext2fs_close2() failed,
but it will also set the ext2_filsys pointer to NULL.

Replace every use of ext2fs_close() in e2fsprogs tools with
ext2fs_close_free() - there is no real reason to keep using
ext2fs_close().

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
2014-07-05 21:06:48 -04:00
Theodore Ts'o aa73432f71 e4defrag: fix error reporting when ext2fs_open fails
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-04-10 23:16:34 -04:00
Jon Ernst 4a77520a98 e4defrag: open fs with EXT2_FLAG_64BITS flag
This allows e4defrag to work with 64-bit and bigalloc file systems.

Signed-off-by: Jon Ernst <jonernst07@gmail.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2014-04-10 23:06:35 -04:00
Baruch Siach 58229aaf85 e4defrag: remove local sync_file_range and fallocate
The locally defined versions of both sync_file_range and fallocate are broken
on 32bit systems. On these systems two 32bit registers are needed for each
64bit parameter. Also, sync_file_range on MIPS32 needs a dummy parameters
after the fd parameter. Just leave all these subtleties to the C library.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-02-18 20:02:49 -05:00
Baruch Siach 9a39389185 e4defrag: choose the best available posix_fadvise variant
Use posix_fadvise64() when available.  This allows 64bit offsets on
32bit systems.

[ Modified by tytso to try to use fadvise64() as well, and to remove
  the attempt to call the syscall directly, since because and
  complexities caused by required dummy arguments on some
  architectures, it's not worth the hair.  ]

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2014-01-05 22:55:10 -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
Darrick J. Wong b1667425f8 e4defrag: defensively check results of sysconf(_SC_PAGESIZE)
sysconf(_SC_PAGESIZE) will probably never return an error, but just in
case it does, we shouldn't pass what looks like a huge number to
sync_file_range() and posix_fadvise().

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:08:49 -05:00
Darrick J. Wong e661d28151 e4defrag: don't crash if umounts the filesystem races with us
If someone umounts the filesystem between statfs64() and the getmntent()
iteration, we can exit the loop having never set mnt_type, and strcmp
can crash.  Fix the potential NULL dereference.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2013-12-12 13:07:34 -05:00
Eric Sandeen 032eafee30 e4defrag: handle device symlinks
Device nodes are commonly accessed via symlinks, i.e.

lrwxrwxrwx. 1 root root 7 Jul 19 13:01 /dev/mapper/testvg-testlv -> ../dm-0

Today, e4defrag on such a device will fail:

File is not regular file
 "/dev/mapper/testvg-testlv"

due to it being a link, and e4defrag on the link target does as well:

Filesystem is not mounted

due to the target not being found in /etc/mtab.

Fix this by checking whether the symlink target is a block device
and if so, using that device in main(), and also changing get_mount_point()
to search for a matching device number, not device name.

Addresses-Red-Hat-Bugzilla: #707209

Reported-by: Peter Hjalmarsson <xake@rymdraket.net>
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2012-07-28 17:53:48 -04:00
Theodore Ts'o d1154eb460 Shorten compile commands run by the build system
The DEFS line in MCONFIG had gotten so long that it exceeded 4k, and
this was starting to cause some tools heartburn.  It also made "make
V=1" almost useless, since trying to following the individual commands
run by make was lost in the noise of all of the defines.

So fix this by putting the configure-generated defines in lib/config.h
and the directory pathnames to lib/dirpaths.h.

In addition, clean up some vestigal defines in configure.in and in the
Makefiles to further shorten the cc command lines.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2011-09-18 17:34:37 -04:00
Eric Sandeen 25726554ca e4defrag: Check error return of sysconf()
In theory sysconf() can fail, so check for an error return.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2011-09-16 19:55:55 -04:00
Kazuya Mio 9ac557a1df e4defrag: Use libext2fs to get the correct superblock information
Currently, e4defrag always does byte-swapping when it gets superblock
information, so the calculation of the best extents count is not
correct on little endian machine. This doesn't cause data corruption,
but it may confuse users by showing the wrong extent count.  To solve
this problem, we use ext2fs_open() instead of get_superblock_info()
that is the original function.

Signed-off-by: Kazuya Mio <k-mio@sx.jp.nec.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2010-12-24 16:56:24 -05:00
Kazuya Mio b07f014fac e4defrag: fix segfault when e4defrag races with unlink/truncate
If a file gets deleted or truncated while e4defrag is trying to
operate on it, it's possible for it seg fault.

Addresses-Red-Hat-Bugzilla: #641926

Reported-by: Michal Piotrowski <mkkp4x4@gmail.com>
Signed-off-by: Kazuya Mio <k-mio@sx.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2010-12-24 14:43:13 -05:00
Theodore Ts'o 30c0529d27 e4defrag: Fix the overflow in e4defrag with > 2GB files
The fallocate() interface on 32-bit machines is defined to use off_t,
not loff_t (even though the system call interface is 64-bit clean).
This causes e4defrag to fail on files greater than 2GB.  Fix this by
trying to use fallocate64(), and using the hard-coded syscall if it
does not exist.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2010-12-16 22:53:34 -05:00
Kazuya Mio 4baacad703 e4defrag: avoid unsuccessful return for an non-privileged user
If non-privileged user runs e4defrag, e4defrag returns an exit status
of 1 despite its success. This patch fixes this problem.

Signed-off-by: Kazuya Mio <k-mio@sx.jp.nec.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2010-12-13 09:59:07 -05:00
Kazuya Mio 02808f7d9a e4defrag: fix file blocks calculation
e4defrag uses st_blocks (struct stat) to calculate file blocks. However,
st_blocks also has meta data blocks in addition to file blocks. So, we
calculate file blocks by sum of the extent length.

Signed-off-by: Kazuya Mio <k-mio@sx.jp.nec.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2010-12-13 09:59:06 -05:00
Kazuya Mio 94d26c267d e4defrag: output size per extent by -c option
e4defrag with -c option outputs "ratio" that means the levels of
fragmentation. However, it's difficult for users to understand, so we will
use size per extent instead of ratio.

Before:
# e4defrag -c /mnt/mp1/file
<File>                                         now/best          ratio
/mnt/mp1/file                                    6/1             0.00%

 Total/best extents                             6/1
 Fragmentation ratio                            0.00%
 Fragmentation score                            0.04
 [0-30 no problem: 31-55 a little bit fragmented: 55- needs defrag]
 This file(/mnt/mp1/file) does not need defragmentation.
 Done.

After:
# e4defrag -c /mnt/mp1/file
<File>                                         now/best       size/ext
/mnt/mp1/file                                    6/1          16666 KB

 Total/best extents                             6/1
 Average size per extent                        16666 KB
 Fragmentation score                            0
 [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag]
 This file (/mnt/mp1/file) does not need defragmentation.
 Done.

Signed-off-by: Kazuya Mio <k-mio@sx.jp.nec.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2010-12-13 09:57:26 -05:00
Peng Tao ae09b934a7 e4defrag: return more specific error message on ioctl failure
Currently e4defrag relies on the EXT4_IOC_MOVE_EXT ioctl to perform online
defragmentation. However, this iotcl kernel patch is not available before
2.6.30-rc1. e4defrag shall fail without obvious reasons on systems running
older kernels. The patch adds more detailed error message addressing this
issue and prompts users with the minimal kernel version that is needed to
run e4defrag.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2010-12-13 09:44:57 -05:00
Eric Sandeen 43eb2ad477 e4defrag: skip "rootfs" entry when checking for ext4 filesystem
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2010-12-13 09:29:19 -05:00
Darrick J. Wong 90b9dce3e5 e4defrag: open the file to be defragged in read/write mode
Akira Fujita merged a patch into 2.6.33 that adds a requirement that a
file being defragged must be opened with read and write access, so
e2fsprogs needs to satisfy that.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2010-12-13 09:23:45 -05:00
Kyle McMartin a9e55a1191 e4defrag: fix ppc build
ppc glibc seems to be missing sync_file_range, so we fell back
to the local define, and there ppc differs as well, so the
build was failing.

Thanks to Kyle for the patch w/ the tidy solution.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2009-09-07 17:00:33 -04:00
Theodore Ts'o 88fca201ac Move declaration of FS_IOC_FIEMAP ioctl to fiemap.h
This helps e4defrag compile on systems where the system header files
don't yet define FS_IOC_FIEMAP.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-08-22 13:16:14 -04:00
Eric Sandeen 4f1d0e8608 e4defrag: remove a lot of cruft
e4defrag.c had a lot of stuff copied into it from other
places, redefinitions of existing interfaces, etc.

We should be able to remove most of this, as the tool only
works on recent kernels anyway, we should just pick up
definitions from recent kernel headers whenever possible.

I've left the local definitions of fallocate, fadvise
(changed to posix_fadvise) and sync_file_range, and
wrapped them in #ifdef configure-time tests - though
really it seems like only fallocate should be necessary
by now, and perhaps the others can be dropped.

We still need some Makefile work so that it won't try to
build e4defrag if the right pieces aren't there (and
if the local definitions won't work...)

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-07-27 10:53:46 -04:00
Kazuya Mio ae50746004 e4defrag: Fix compile warnings
When compile e2fsprogs git tree with gcc-wall option, we get some warnings about
e4defrag. This patch fixes them.

Signed-off-by: Kazuya Mio <k-mio@sx.jp.nec.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2009-07-11 14:52:21 -04:00
Theodore Ts'o 1d21f4bd8d e4defrag: Fix a core dump when trying to defrag a file with no blocks
Signed-off-by Akira Fujita <a-fujita@rs.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-07-09 15:03:39 -04:00
Theodore Ts'o 4836459a3f Add e4defrag to the misc directory
To make it easier to maintain changes and fixes to the e4defrag
program, check it into the e2fsprogs source tree.

Signed-off-by: Akira Fujita <a-fujita@rs.jp.nec.com>
Signed-off-by: Takashi Sato <t-sato@yk.jp.nec.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
2009-07-09 14:56:43 -04:00