extern char *optarg;
extern int optind;
#endif
+#if defined(HAVE_EXT2_IOCTLS) && !defined(DEBUGFS)
+# include <sys/ioctl.h>
+# include <sys/types.h>
+# include <sys/stat.h>
+# include <fcntl.h>
+# include <limits.h>
+#endif
#include "ext2fs/ext2_fs.h"
#include "ext2fs/ext2fs.h"
#include "e2freefrag.h"
+#if defined(HAVE_EXT2_IOCTLS) && !defined(DEBUGFS)
+# ifdef HAVE_LINUX_FSMAP_H
+# include <linux/fsmap.h>
+# endif
+# include "fsmap.h"
+#endif
+
static void usage(const char *prog)
{
fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
update_chunk_stats(info, last_chunk_size);
}
-static errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info,
- FILE *f)
+#if defined(HAVE_EXT2_IOCTLS) && !defined(DEBUGFS)
+# define FSMAP_EXTENTS 1024
+static int scan_online(ext2_filsys fs, struct chunk_info *info)
+{
+ struct fsmap_head *fsmap;
+ struct fsmap *extent;
+ struct fsmap *p;
+ char mntpoint[PATH_MAX + 1];
+ errcode_t retval;
+ int mount_flags;
+ int fd;
+ int ret;
+ int i;
+
+ /* Try to open the mountpoint for a live query. */
+ retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
+ mntpoint, PATH_MAX);
+ if (retval) {
+ com_err(fs->device_name, retval, "while checking mount status");
+ return 0;
+ }
+ if (!mount_flags & EXT2_MF_MOUNTED)
+ return 0;
+ fd = open(mntpoint, O_RDONLY);
+ if (fd < 0) {
+ com_err(mntpoint, errno, "while opening mount point");
+ return 0;
+ }
+
+ fsmap = malloc(fsmap_sizeof(FSMAP_EXTENTS));
+ if (!fsmap) {
+ com_err(fs->device_name, errno, "while allocating memory");
+ return 0;
+ }
+
+ memset(fsmap, 0, sizeof(*fsmap));
+ fsmap->fmh_count = FSMAP_EXTENTS;
+ fsmap->fmh_keys[1].fmr_device = UINT_MAX;
+ fsmap->fmh_keys[1].fmr_physical = ULLONG_MAX;
+ fsmap->fmh_keys[1].fmr_owner = ULLONG_MAX;
+ fsmap->fmh_keys[1].fmr_offset = ULLONG_MAX;
+ fsmap->fmh_keys[1].fmr_flags = UINT_MAX;
+
+ /* Fill the extent histogram with live data */
+ while (1) {
+ ret = ioctl(fd, FS_IOC_GETFSMAP, fsmap);
+ if (ret < 0) {
+ com_err(fs->device_name, errno, "while calling fsmap");
+ free(fsmap);
+ return 0;
+ }
+
+ /* No more extents to map, exit */
+ if (!fsmap->fmh_entries)
+ break;
+
+ for (i = 0, extent = fsmap->fmh_recs;
+ i < fsmap->fmh_entries;
+ i++, extent++) {
+ if (!(extent->fmr_flags & FMR_OF_SPECIAL_OWNER) ||
+ extent->fmr_owner != FMR_OWN_FREE)
+ continue;
+ update_chunk_stats(info,
+ extent->fmr_length / fs->blocksize);
+ }
+
+ p = &fsmap->fmh_recs[fsmap->fmh_entries - 1];
+ if (p->fmr_flags & FMR_OF_LAST)
+ break;
+ fsmap_advance(fsmap);
+ }
+
+ return 1;
+}
+#else
+# define scan_online(fs, info) (0)
+#endif /* HAVE_EXT2_IOCTLS */
+
+static errcode_t scan_offline(ext2_filsys fs, struct chunk_info *info)
+{
+ errcode_t retval;
+
+ retval = ext2fs_read_block_bitmap(fs);
+ if (retval)
+ return retval;
+ scan_block_bitmap(fs, info);
+ return 0;
+}
+
+static errcode_t dump_chunk_info(ext2_filsys fs, struct chunk_info *info,
+ FILE *f)
{
unsigned long total_chunks;
const char *unitp = "KMGTPEZY";
unsigned long start = 0, end;
int i, retval = 0;
- scan_block_bitmap(fs, info);
-
- fprintf(f, "Total blocks: %llu\nFree blocks: %u (%0.1f%%)\n",
- ext2fs_blocks_count(fs->super), fs->super->s_free_blocks_count,
- (double)fs->super->s_free_blocks_count * 100 /
+ fprintf(f, "Total blocks: %llu\nFree blocks: %llu (%0.1f%%)\n",
+ ext2fs_blocks_count(fs->super),
+ ext2fs_free_blocks_count(fs->super),
+ (double)ext2fs_free_blocks_count(fs->super) * 100 /
ext2fs_blocks_count(fs->super));
if (info->chunkbytes) {
info->histogram.fc_chunks[i],
info->histogram.fc_blocks[i],
(double)info->histogram.fc_blocks[i] * 100 /
- fs->super->s_free_blocks_count);
+ ext2fs_free_blocks_count(fs->super));
}
start = end;
if (start == 1<<10) {
static void close_device(char *device_name, ext2_filsys fs)
{
- int retval = ext2fs_close(fs);
+ int retval = ext2fs_close_free(&fs);
if (retval)
com_err(device_name, retval, "while closing the filesystem.\n");
fprintf(f, "Device: %s\n", fs->device_name);
fprintf(f, "Blocksize: %u bytes\n", fs->blocksize);
- retval = ext2fs_read_block_bitmap(fs);
+ init_chunk_info(fs, chunk_info);
+ if (!scan_online(fs, chunk_info)) {
+ init_chunk_info(fs, chunk_info);
+ retval = scan_offline(fs, chunk_info);
+ }
if (retval) {
com_err(fs->device_name, retval, "while reading block bitmap");
close_device(fs->device_name, fs);
exit(1);
}
- init_chunk_info(fs, chunk_info);
-
- retval = get_chunk_info(fs, chunk_info, f);
+ retval = dump_chunk_info(fs, chunk_info, f);
if (retval) {
- com_err(fs->device_name, retval, "while collecting chunk info");
+ com_err(fs->device_name, retval, "while dumping chunk info");
close_device(fs->device_name, fs);
exit(1);
}
#ifdef DEBUGFS
if (check_fs_open(argv[0]))
return;
+ reset_getopt();
#else
char *device_name;