Subject: stat shows wrong, non-existing device number

From: Joachim Wagner <[email-masked]>
Date: Wed, 12 Jan 2022 11:13:21 +0000

Issue: The device number shown in `stat` output does not match the device 
numbers of any of the devices used by the filesystem.

On my system:

$ stat --version | head -n 1 
stat (GNU coreutils) 8.32

$ cat /etc/os-release | fgrep -i pretty
PRETTY_NAME="openSUSE Leap 15.3"

$ ls -l /dev/nvme0n1p6 /dev/nvme1n1p3
brw-rw---- 1 root disk 259,  6 Jan 12 09:29 /dev/nvme0n1p6
brw-rw---- 1 root disk 259, 12 Jan 12 09:29 /dev/nvme1n1p3

$ fgrep home /etc/crypttab 
cr_home0   /dev/disk/by-id/nvme-RETRACTED1-part6
cr_home1   /dev/disk/by-id/nvme-RETRACTED2-part3

$ ls -l /dev/mapper/cr_home?
lrwxrwxrwx 1 root root 7 Jan 12 09:29 /dev/mapper/cr_home0 -> ../dm-6
lrwxrwxrwx 1 root root 7 Jan 12 09:30 /dev/mapper/cr_home1 -> ../dm-5

$ ls -l /dev/dm-[56]
brw-rw---- 1 root disk 254, 5 Jan 12 09:30 /dev/dm-5
brw-rw---- 1 root disk 254, 6 Jan 12 09:29 /dev/dm-6

$ fgrep home /etc/fstab 
/dev/mapper/cr_home0                       /home        btrfs  defaults              
0  0

$ history | fgrep cr_home | tail -n 3 | head -n 2
  108  2022-01-05 17:01:22 mkfs.btrfs /dev/mapper/cr_home0
  141  2022-01-05 17:16:49 btrfs device add /dev/mapper/cr_home1 /home

$ touch /home/test

$ stat /home/test | fgrep -i device
Device: 3ch/60d Inode: 2143351     Links: 1

$ ls -l /dev | grep "^b" | fgrep 60 | wc -l
0

Expected behaviour: Device should be one (or more) of 

fe06h/65030d  <-- most expected as this is the primary btrfs device
fe05h/65029d  <-- the 2nd device (/dev/cr_home1)
10306h/66310d  <-- backing device for cr_home0 (/dev/nvme0n1p6)
1030ch/66316d  <-- backing device for cr_home1 (/dev/nvme1n1p3)
10300h/66304d  <-- SSD 1 (/dev/nvme0n1)
10309h/66313d  <-- SSD 2 (/dev/nvme1n1)

Speculation: Searching the mailing list for stat + major on https://
www.gnu.org/software/coreutils/ shows results indicating that stat uses only 8 
bit for major and minor. However, since kernel 2.6.0 according to https://
stackoverflow.com/questions/14833467/maximum-values-of-major-and-minor-
numbers-in-linux , the major has 12 bit and the minor 20 bit. Output above 
shows a major with at least 9 bits that may confuse `stat` if it doesn't stay 
with the immediate device number of the top level dm-crypt device.