lsfd: extend nodev table to decode "btrfs" on SOURCE column
When filling SOURCE column, lsfd decodes the name of the device where the
file object is. If the file object is sourced from a file system, lsfd
fills the column with the file system's name.
As #2349 and #2308, if the file system is btrfs, lsfd couldn't decode
the name correctly. This change and its preceding changes fix this bug.
"devnum offset" causes the trouble. On btrfs, the device number
reported by stat syscall and proc fs are different.
For the details of "devnum offset", see "Mechanism behind the
devnum offset". About the way to fix it, see "How to adjust the output of lsfd".
Without this change:
$ ./lsfd -Q '(ASSOC == "exe")' -p $$
COMMAND PID USER ASSOC XMODE TYPE SOURCE MNTID INODE NAME
zsh 19318 yamato exe ------ REG 0:38 0 589767 /usr/bin/zsh
With this change:
$ ./lsfd -Q '(ASSOC == "exe")' -p $$
COMMAND PID USER ASSOC XMODE TYPE SOURCE MNTID INODE NAME
zsh 19318 yamato exe ------ REG btrfs 0 589767 /usr/bin/zsh
Mechanisum behind the devnum offset
-----------------------------------
Both stat command and the inotify field in fdinfo refer to an inode.
filename_lookup(https://elixir.bootlin.com/linux/v6.2.9/source/fs/namei.c#L2495)
is the function getting the inode for a given file
name. filename_lookup returns a struct path. Via path->detnry->inode,
the caller of filename_lookup can get the inode.
stat command calls statx system call. statx calls filename_lookup
eventually.
inotify_add_watch system call takes a file name. The inotify_add_watch
calls the filename_lookup eventually for getting the inode for the
file name. The inode number that inotify_add_watch gets via
filename_lookup is printed in the inotify field in fdinfo.
The device number, the subject of this issue, can be obtained via
path->detnry->inode->i_sb->s_dev. Both the stat command and the
inotify field in fdinfo use the filename_lookup for getting path. If
they use the same function, why don't the device numbers match? I
monitored the device numbers obtained via
path->detnry->inode->i_sb->s_dev by inserting a systemtap probe to
filename_lookup. I saw the numbers matched.
However, the number monitored via systemtap did not match the number
printed by the stat command. statx system call doesn't use
path->detnry->inode->i_sb->s_dev , the value obtained via
filename_lookup, directly. statx calls vfs_statx. vfs_statx calls
vfs_getattr after calling the filename_lookup for filling struct
kstat. vfs_getattr calls inode->i_op->getattr, a file system specific
method for filling struct kstat if it is available. btrfs has an
implementation for the method,
btrfs_getattr(https://elixir.bootlin.com/linux/v6.2.9/source/fs/btrfs/inode.c#L9007):
stat->dev = BTRFS_I(inode)->root->anon_dev;
The dev member is overwritten with btrfs specific value.
How to adjust the output of lsfd
--------------------------------
lsfd already reads mountinfo files.
1. Get the "rawnum" and mount point
The device numbers in a mountinfo file are raw; btrfs is not
considered. Let's call the number "rawnum" here. When reading the
mountinfo file, lsfd can know the mount points of btrfs.
By calling "stat" system call for the mount point getting in the
step 1, lsdf can know the device number the btrfs customizes with
its getattr method. Let's call the device number "cookednum".