2012-09-07 20:12:50

by Mike Fleetwood

[permalink] [raw]
Subject: How to query ext2/3/4 total fs size while mounted?

Hi,

Is there a way I can/should code GParted (GNOME Partition Editor) to
query a mounted ext2/3/4 file system to get the total file system size?

Unfortunately the f_blocks value from the statvfs() system call has
space occupied by inode tables, allocation bitmaps, etc. subtracted as
overhead and I need the total file system size. This is so that GParted
can inform users when the file system doesn't fill the partition and
recommend resizing the FS to fix it.

I really want some way to get the the total file system size from
statvfs(). The mount option minixdf isn't really appropriate as GParted
shouldn't be remounting file systems adding mount options to query the
file system size.

Is it safe to read the super block off disk using ext2fs_open() or by
running dumpe2fs for a mounted file system? What if the file system has
just been resized? Are there any other methods for querying the total
file system size?

Thanks,
Mike


In this example I want to get the 194560 blocks figure while mounted,
not f_blocks=188403.

# sfdisk -s /dev/sda10
194560
# mkfs.ext4 /dev/sda10
mke2fs 1.41.12 (17-May-2010)
...
48768 inodes, 194560 blocks
...
# dumpe2fs -h /dev/sda10 | grep 'Block count:'
dumpe2fs 1.41.12 (17-May-2010)
Block count: 194560
# mount /dev/sda10 /mnt/0
# strace -e statfs64 stat -f /mnt/0
...
statfs64("/mnt/0", 84, {f_type="EXT2_SUPER_MAGIC", f_bsize=1024,
f_blocks=188403, f_bfree=182757, f_bavail=173029, f_files=48768,
f_ffree=48757, f_fsid={-1366779076, -459235834}, f_namelen=255,
f_frsize=1024}) = 0
File: "/mnt/0"
ID: ae889b3ce4a09e06 Namelen: 255 Type: ext2/ext3
Block size: 1024 Fundamental block size: 1024
Blocks: Total: 188403 Free: 182757 Available: 173029
Inodes: Total: 48768 Free: 48757


2012-09-08 00:39:31

by Theodore Ts'o

[permalink] [raw]
Subject: Re: How to query ext2/3/4 total fs size while mounted?

On Fri, Sep 07, 2012 at 09:12:49PM +0100, Mike Fleetwood wrote:
>
> Is it safe to read the super block off disk using ext2fs_open() or by
> running dumpe2fs for a mounted file system? What if the file system has
> just been resized? Are there any other methods for querying the total
> file system size?

Yes, it would be safe to read the superblock off a mounted disk.
There is always going to be a race if someone is resizing the file
system as you read the file system, but that's true for the statfs
system call as well.

There's no way that parted can stop someone else from kicking off a
online resize between the time you display information about the
partition table and their contents and the user types in another
command.

Well, you could freeze the file system, but if another process is
writing huge amounts of data at the time, freezing the file system for
long periods of time could cause the system to run into significant
memory problems (since it can't clean memory pages so they can be
dropped to make room for more modified pages, leading to OOM kills).

Probably the best thing you can do, if this is really important to
you, is to snapshot the state of the file systems, then when the user
enters a command, in quick succession, freeze the file system, double
check to make sure nothing has changed, and if it hasn't, update the
partition table (this is most important if you are shrinking the
partition), and then unfreeze the file system.

Cheers,

- Ted

2012-09-10 10:39:40

by Mike Fleetwood

[permalink] [raw]
Subject: Re: How to query ext2/3/4 total fs size while mounted?

On 8 September 2012 01:39, Theodore Ts'o <[email protected]> wrote:
> On Fri, Sep 07, 2012 at 09:12:49PM +0100, Mike Fleetwood wrote:
>>
>> Is it safe to read the super block off disk using ext2fs_open() or by
>> running dumpe2fs for a mounted file system? What if the file system has
>> just been resized? Are there any other methods for querying the total
>> file system size?
>
> Yes, it would be safe to read the superblock off a mounted disk.
> There is always going to be a race if someone is resizing the file
> system as you read the file system, but that's true for the statfs
> system call as well.

It would also be easiest if GParted can use the free blocks from the on
disk super block too, rather than calling statvfs(). How up to date
will the on disk free blocks figure be?

My testing using a slowly allocating space workload finds ext2 writes
the super block every 6 seconds keeping the blocks free figure up to
date. However for ext3 & ext4 the super block doesn't change. I guess
because all the meta data updates are journalled and can be replayed on
crash.

Freezing and unfreezing ext3/4 does get the blocks free figure in the
super block updated, but GParted shouldn't be freezing and unfreezing a
file system just to get the free space. I guess that GParted should
just use statvfs() to query free blocks.

Thanks,
Mike