[Bug 277804] stat(2) on UFS volumes reports wrong st_dev (devid) when run through Linux compat layer

From: <bugzilla-noreply_at_freebsd.org>
Date: Tue, 19 Mar 2024 08:46:28 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=277804

            Bug ID: 277804
           Summary: stat(2) on UFS volumes reports wrong st_dev (devid)
                    when run through Linux compat layer
           Product: Base System
           Version: 15.0-CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Some People
          Priority: ---
         Component: bin
          Assignee: bugs@FreeBSD.org
          Reporter: michaelo@FreeBSD.org

This is a upstream report of
https://lists.gnu.org/archive/html/bug-coreutils/2024-03/msg00045.html.

Broken down we have the following problem:

Consider this simple C program:
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <unistd.h>
> #include <stdio.h>
> #include <string.h>
> 
> int main(int argc, char *argv[]) {
>         char *fs = argv[1];
>         struct stat sb;
>         if (!stat(fs, &sb)) {
>             int major = major(sb.st_dev);
>             int minor = minor(sb.st_dev);
>             printf("%s:%ld, major: %d, minor: %d\n", fs, sb.st_dev, major, minor);
>         }
>         return 0;
> }

Compile it once on FreeBSD and Linux and run both on FreeBSD with the following
UFS volumes mounted (not reproducible with ZFS or NFS mounts):
> $ df -t ufs
> Filesystem            1K-blocks     Used    Avail Capacity  Mounted on
> /dev/da0p2              2031132   629168  1239476    34%    /
> /dev/da0p4              4053308   245928  3483116     7%    /tmp
> /dev/da0p5              4053308   864504  2864540    23%    /var
> /dev/da0p6              8106716   798660  6659520    11%    /var/tmp
> /dev/da0p7             12180252  7877268  3328564    70%    /usr
> /dev/gvinum/local      24360604 16555848  5855908    74%    /usr/local
> /dev/gvinum/ports      48741436 23930092 20912032    53%    /usr/ports
> /dev/gvinum/obj        20307196 15515764  3166860    83%    /usr/obj
> /dev/gvinum/pgsql      32487548  5266684 24621864    18%    /usr/local/pgsql
> /dev/gvinum/nexus2     32487548 13744292 16144256    46%    /var/nexus2
> /dev/gvinum/svn         8106716  5183236  2274944    69%    /var/svn
> /dev/gvinum/osipovmi   30450780 21861900  6152820    78%    /var/osipovmi
> /dev/gvinum/poudriere  64995324 37001360 22794340    62%    /var/poudriere
> /dev/gvinum/compat      4053308   984876  2744168    26%    /compat
> /dev/gvinum/bastille   81249212 59453292 15295984    80%    /usr/local/bastille

FreeBSD executable:
> $ df  -t ufs | cut -f 6 -w | sed 1d | xargs -I% ./stat-freebsd %
> /:108, major: 0, minor: 108
> /tmp:110, major: 0, minor: 110
> /var:111, major: 0, minor: 111
> /var/tmp:112, major: 0, minor: 112
> /usr:113, major: 0, minor: 113
> /usr/local:161, major: 0, minor: 161
> /usr/ports:142, major: 0, minor: 142
> /usr/obj:141, major: 0, minor: 141
> /usr/local/pgsql:140, major: 0, minor: 140
> /var/nexus2:162, major: 0, minor: 162
> /var/svn:163, major: 0, minor: 163
> /var/osipovmi:164, major: 0, minor: 164
> /var/poudriere:166, major: 0, minor: 166
> /compat:165, major: 0, minor: 165
> /usr/local/bastille:139, major: 0, minor: 139

now the Linux executable:

> $ file stat-linux
> stat-linux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=4f0f500b4e20d2a43770db4afa7e94c834d8ccca, not stripped
> $ df  -t ufs | cut -f 6 -w | sed 1d | xargs -I% ./stat-linux %
> /:108, major: 0, minor: 108
> /tmp:165, major: 0, minor: 165
> /var:165, major: 0, minor: 165
> /var/tmp:112, major: 0, minor: 112
> /usr:165, major: 0, minor: 165
> /usr/local:161, major: 0, minor: 161
> /usr/ports:142, major: 0, minor: 142
> /usr/obj:141, major: 0, minor: 141
> /usr/local/pgsql:140, major: 0, minor: 140
> /var/nexus2:162, major: 0, minor: 162
> /var/svn:163, major: 0, minor: 163
> /var/osipovmi:164, major: 0, minor: 164
> /var/poudriere:166, major: 0, minor: 166
> /compat:165, major: 0, minor: 165
> /usr/local/bastille:139, major: 0, minor: 139

So if you use df(1) from /compat/linux those incorrectly FS are omitted:
> $ /compat/linux/usr/bin/linux-df -a -B 512 -P  -t ufs -T
> Dateisystem           Typ  512-Blöcke   Benutzt Verfügbar Kapazität Eingehängt auf
> /dev/da0p2            ufs     4062264   1258336   2478952       34% /
> /dev/da0p4            ufs     8106616   1969752   5488336       27% /tmp
> /dev/da0p5            -             -         -         -         - /var
> /dev/da0p6            ufs    16213432   1597320  13319040       11% /var/tmp
> /dev/da0p7            -             -         -         -         - /usr
> /dev/gvinum/local     ufs    48721208  33111696  11711816       74% /usr/local
> /dev/gvinum/ports     ufs    97482872  47860184  41824064       54% /usr/ports
> /dev/gvinum/obj       ufs    40614392  31031528   6333720       84% /usr/obj
> /dev/gvinum/pgsql     ufs    64975096  10533368  49243728       18% /usr/local/pgsql
> /dev/gvinum/nexus2    ufs    64975096  27488584  32288512       46% /var/nexus2
> /dev/gvinum/svn       ufs    16213432  10366472   4549888       70% /var/svn
> /dev/gvinum/osipovmi  ufs    60901560  43723800  12305640       79% /var/osipovmi
> /dev/gvinum/poudriere ufs   129990648  74002720  45588680       62% /var/poudriere
> /dev/gvinum/compat    -             -         -         -         - /compat
> /dev/gvinum/bastille  ufs   162498424 118906584  30591968       80% /usr/local/bastille

This can be reproduced with 13-STABLE:
FreeBSD deblndw011x.ad001.siemens.net 13.3-STABLE FreeBSD 13.3-STABLE 77814c959
GENERIC amd64
and on 15-STABLE:
FreeBSD deblndw013x5v.ad001.siemens.net 15.0-CURRENT FreeBSD 15.0-CURRENT #0
main-n268454-9097284b98be: Thu Feb 22 03:00:34 UTC 2024    
root@releng3.nyi.freebsd.org:/usr/obj/usr/src/amd64.amd64/sys/GENERIC amd64

I assume the bug is somewhere here:
*
https://github.com/freebsd/freebsd-src/blob/e29be07861173f87b2dd46db1d0c7cbcf72d4ad0/sys/compat/linux/linux_stats.c#L707
* https://github.com/freebsd/freebsd-src/blob/main/sys/compat/linux/linux.h#L55

-- 
You are receiving this mail because:
You are the assignee for the bug.