Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[debugfs] cmd link hard link count error #19

Closed
time-river opened this issue Jul 16, 2019 · 1 comment

Comments

@time-river
Copy link

commented Jul 16, 2019

I tried to use debugfs to operate my ext4 fs img in CentOS 7(e2fsprogs: 1.42.9, glibc: 2.17), it seems that there is a bug in debugfs -R "link ..." command.

Apparence:
After cmd debugfs -R "link ...", the hard link count is no increase, then $ du would duplicate the same file resulting in the wrong output of statistics.

I read the source code, find that operation will directly write struct ext2_inode member i_links_count without increasing.

I also find the Debian 10(e2fsprogs: 1.44.5, glibc: 2.28) has the same question.

The following is the operation log (hide some useless output), steps show after debugfs -R "link ...", $ df has no change but $ du not. the hard link count is the same as before. However, $ln command will increase the count, and all of the hard link show 2 reference:

root@river-CentOS-7 /h/m/img# mkfs.ext4 ext4.img 
mke2fs 1.42.9 (28-Dec-2013)
ext4.img is not a block special device.
Proceed anyway? (y,n) y
Discarding device blocks: done                            
warning: 257 blocks unused.

Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
49248 inodes, 196608 blocks
9830 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=201326592
6 block groups
32768 blocks per group, 32768 fragments per group
8208 inodes per group
Superblock backups stored on blocks: 
  32768, 98304, 163840

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

root@river-CentOS-7 /h/m/img# losetup --show -f -P /home/me/img/ext4.img
/dev/loop0
me@river-CentOS-7 ~/img> sudo mount /dev/loop0 /mnt/
me@river-CentOS-7 ~/img> sudo chown -R me:me /mnt
me@river-CentOS-7 ~/img> cd /mnt/
me@river-CentOS-7 /mnt> ls -ali
total 20
 2 drwxr-xr-x.  3 me   me    4096 Jul 14 23:47 ./
64 dr-xr-xr-x. 18 root root   238 Jul 10 03:21 ../
11 drwx------.  2 me   me   16384 Jul 14 23:47 lost+found/
me@river-CentOS-7 /mnt> df --output=source,fstype,itotal,iused,iavail,ipcent,size,used,avail,pcent,file,target -B 4KiB 
Filesystem              Type       Inodes  IUsed    IFree IUse% 4K-blocks    Used   Avail Use% File Mounted on
/dev/loop0              ext4        49248     11    49237    1%    189414     386  175266   1% -    /mnt
me@river-CentOS-7 /mnt> du --block-size=4KiB -a
4 ./lost+found
5 .
me@river-CentOS-7 /mnt> echo '1234' > test.txt
me@river-CentOS-7 /mnt> df --output=source,fstype,itotal,iused,iavail,ipcent,size,used,avail,pcent,file,target -B 4KiB 
Filesystem              Type       Inodes  IUsed    IFree IUse% 4K-blocks    Used   Avail Use% File Mounted on
/dev/loop0              ext4        49248     12    49236    1%    189414     387  175265   1% -    /mnt
me@river-CentOS-7 /mnt> du --block-size=4KiB -a
1 ./test.txt
4 ./lost+found
6 .
me@river-CentOS-7 /mnt> sudo -s
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
root@river-CentOS-7 /mnt# ls -ali
total 24
 2 drwxr-xr-x.  3 me   me    4096 Jul 14 23:48 ./
64 dr-xr-xr-x. 18 root root   238 Jul 10 03:21 ../
11 drwx------.  2 me   me   16384 Jul 14 23:47 lost+found/
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 test.txt
root@river-CentOS-7 /mnt# debugfs -w /dev/loop0 -R  'link test.txt /1.txt'
debugfs 1.42.9 (28-Dec-2013)
root@river-CentOS-7 /mnt# ls -ali
total 28
 2 drwxr-xr-x.  3 me   me    4096 Jul 14 23:48 ./
64 dr-xr-xr-x. 18 root root   238 Jul 10 03:21 ../
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 1.txt
11 drwx------.  2 me   me   16384 Jul 14 23:47 lost+found/
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 test.txt
root@river-CentOS-7 /mnt# du --block-size=4KiB -a
1 ./test.txt
1 ./1.txt
4 ./lost+found
7 .
root@river-CentOS-7 /mnt# df --output=source,fstype,itotal,iused,iavail,ipcent,size,used,avail,pcent,file,target --block-size=4KiB
Filesystem              Type       Inodes  IUsed    IFree IUse% 4K-blocks    Used   Avail Use% File Mounted on
/dev/loop0              ext4        49248     12    49236    1%    189414     387  175265   1% -    /mnt
root@river-CentOS-7 /mnt# debugfs -w /dev/loop0 -R  'link test.txt /11.txt'
debugfs 1.42.9 (28-Dec-2013)
root@river-CentOS-7 /mnt# ls -ali
total 32
 2 drwxr-xr-x.  3 me   me    4096 Jul 14 23:48 ./
64 dr-xr-xr-x. 18 root root   238 Jul 10 03:21 ../
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 11.txt
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 1.txt
11 drwx------.  2 me   me   16384 Jul 14 23:47 lost+found/
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 test.txt
root@river-CentOS-7 /mnt# du --block-size=4KiB -a
1 ./test.txt
1 ./1.txt
4 ./lost+found
1 ./11.txt
8 .
root@river-CentOS-7 /mnt# df --output=source,fstype,itotal,iused,iavail,ipcent,size,used,avail,pcent,file,target --block-size=4KiB
Filesystem              Type       Inodes  IUsed    IFree IUse% 4K-blocks    Used   Avail Use% File Mounted on
/dev/loop0              ext4        49248     12    49236    1%    189414     387  175265   1% -    /mnt
root@river-CentOS-7 /mnt# debugfs -w /dev/loop0 -R  'link /test.txt /111.txt'
debugfs 1.42.9 (28-Dec-2013)
root@river-CentOS-7 /mnt# ls -ali
total 36
 2 drwxr-xr-x.  3 me   me    4096 Jul 14 23:48 ./
64 dr-xr-xr-x. 18 root root   238 Jul 10 03:21 ../
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 111.txt
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 11.txt
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 1.txt
11 drwx------.  2 me   me   16384 Jul 14 23:47 lost+found/
12 -rw-r--r--.  1 me   me       5 Jul 14 23:48 test.txt
root@river-CentOS-7 /mnt# du --block-size=4KiB -a
1 ./test.txt
1 ./111.txt
1 ./1.txt
4 ./lost+found
1 ./11.txt
9 .
root@river-CentOS-7 /mnt# df --output=source,fstype,itotal,iused,iavail,ipcent,size,used,avail,pcent,file,target --block-size=4KiB
Filesystem              Type       Inodes  IUsed    IFree IUse% 4K-blocks    Used   Avail Use% File Mounted on
/dev/loop0              ext4        49248     12    49236    1%    189414     387  175265   1% -    /mnt
root@river-CentOS-7 /mnt# ln test.txt ln-cmd-1.txt
root@river-CentOS-7 /mnt# ls -ali
total 40
 2 drwxr-xr-x.  3 me   me    4096 Jul 14 23:52 ./
64 dr-xr-xr-x. 18 root root   238 Jul 10 03:21 ../
12 -rw-r--r--.  2 me   me       5 Jul 14 23:48 111.txt
12 -rw-r--r--.  2 me   me       5 Jul 14 23:48 11.txt
12 -rw-r--r--.  2 me   me       5 Jul 14 23:48 1.txt
12 -rw-r--r--.  2 me   me       5 Jul 14 23:48 ln-cmd-1.txt
11 drwx------.  2 me   me   16384 Jul 14 23:47 lost+found/
12 -rw-r--r--.  2 me   me       5 Jul 14 23:48 test.txt
root@river-CentOS-7 /mnt# du --block-size=4KiB -a
1 ./test.txt
4 ./lost+found
6 .
root@river-CentOS-7 /mnt# df --output=source,fstype,itotal,iused,iavail,ipcent,size,used,avail,pcent,file,target --block-size=4KiB
Filesystem              Type       Inodes  IUsed    IFree IUse% 4K-blocks    Used   Avail Use% File Mounted on
/dev/loop0              ext4        49248     12    49236    1%    189414     387  175265   1% -    /mnt
@tytso

This comment has been minimized.

Copy link
Owner

commented Jul 16, 2019

Debugfs is intended to be a low-level tool; it's used to create test file systems for e2fsprogs' regression tests. As such, among other things, it's used to create corrupt file systems for e2fsck to fix. In fact, if you were to look at the debugfs's Fine Manual, it's explicitly documented to work this way:

   ln filespec dest_file
          Create a link named dest_file which is a hard link to filespec.  Note this does not ad‐
          just the inode reference counts.

Changing things at this point would break shell scripts which use debugfs, including those that are used in e2fsprogs' regression test suite. If you want a more user friendly way of modifying a file system image, I'd suggest fuse2fs (included in more modern versions of e2fsprogs) or the e2tools package.

@tytso tytso closed this Jul 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.