[Toybox] [PATCH] df, stat: query mount stats lazily instead of eagerly in xgetmountlist
Thiébaud Weksteen
tweek at google.com
Mon Aug 24 04:42:28 UTC 2026
On Sat, Aug 22, 2026 at 10:28 AM Rob Landley <rob at landley.net> wrote:
>
> On 8/20/26 00:07, Thiébaud Weksteen wrote:
> > Previously, xgetmountlist(0) eagerly invoked stat() and statvfs() on
> > every mount point parsed from /proc/mounts. When a user ran `df <path>`
> > (e.g., `df /tmp`), df would probe every mounted filesystem in the system
> > before evaluating the target argument. It can be an issue if some mounts
> > on the system are slow, unreachable, or backed by stalled network
> > (NFS/CIFS) or user-space (FUSE) filesystems. If the caller does not have
> > permission to access these mount points (e.g., because of the SELinux
> > policy), this may also trigger spurious log messages.
> >
> > This patch decouples mount table parsing from filesystem inspection.
> > xgetmountlist() is now a pure in-memory parser without side-effects from
> > stat/statvfs syscalls.
> >
> > For df, when path arguments are supplied, df calls stat() and statvfs()
> > directly on the target argument(s) and resolves the mount entry in
> > memory via device ID or longest mount path prefix. When run without
> > arguments, df queries active mounts lazily.
> >
> > For stat, the mount entries are lazily queried only when formatting
> > mount points (%m).
>
> All good in theory...
>
> $ diffstat diff.eml
> df.c | 27 +++++++++++++++++++++++----
> 1 file changed, 23 insertions(+), 4 deletions(-)
>
> Hmmm...
>
> I very vaguely recall years ago doing something like O_NONBLOCK on the
> open() of the device so eject.c wouldn't spin up the disk before opening
> the tray. It also avoided triggering automounts.
>
> $ man 2 open | grep -m1 -A4 O_NONBLOCK
> O_NONBLOCK or O_NDELAY
> When possible, the file is opened in nonblocking mode. Neither
> the open() nor any subsequent I/O operations on the file de‐
> scriptor which is returned will cause the calling process to
> wait.
>
> > Signed-off-by: Thiébaud Weksteen <tweek at google.com>
> > ---
> > lib/portability.c | 14 +----------
> > toys/other/stat.c | 10 +++++---
> > toys/posix/df.c | 64 +++++++++++++++++++++++++++++++++++++++++++----
> > 3 files changed, 67 insertions(+), 21 deletions(-)
> >
> > diff --git a/lib/portability.c b/lib/portability.c
> > index b048dca1..ae01a839 100644
> > --- a/lib/portability.c
> > +++ b/lib/portability.c
> > @@ -51,7 +51,7 @@ void xgetrandom(void *buf, unsigned buflen)
> > close(fd);
> > }
> >
> > -// Get list of mounted filesystems, including stat and statvfs info.
> > +// Get list of mounted filesystems.
> > // Returns a reversed list, which is good for finding overmounts and such.
> >
> > #if defined(__APPLE__) || defined(__IsBSD__)
> > @@ -78,11 +78,6 @@ struct mtab_list *xgetmountlist(char *path)
> > strlen(me->f_mntonname) + strlen(me->f_mntfromname) + strlen("") + 4);
> > dlist_add_nomalloc((void *)&mtlist, (void *)mt);
> >
> > - // Collect details about mounted filesystem.
> > - // Don't report errors, just leave data zeroed.
> > - stat(me->f_mntonname, &(mt->stat));
> > - statvfs(me->f_mntonname, &(mt->statvfs));
> > -
> > // Remember information from struct statfs.
> > mt->dir = stpcpy(mt->type, me->f_fstypename)+1;
> > mt->device = stpcpy(mt->dir, me->f_mntonname)+1;
> > @@ -146,13 +141,6 @@ struct mtab_list *xgetmountlist(char *path)
> > strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4);
> > dlist_add_nomalloc((void *)&mtlist, (void *)mt);
> >
> > - // Collect details about mounted filesystem
> > - // Don't report errors, just leave data zeroed
> > - if (!path) {
> > - stat(me->mnt_dir, &(mt->stat));
> > - statvfs(me->mnt_dir, &(mt->statvfs));
> > - }
>
> If the scan isn't populating them, why weren't mt->stat and mt->statvfs
> removed from the list structure?
>
> > // Remember information from /proc/mounts
> > mt->dir = stpcpy(mt->type, me->mnt_type)+1;
> > mt->device = stpcpy(mt->dir, me->mnt_dir)+1;
> > diff --git a/toys/other/stat.c b/toys/other/stat.c
> > index eaf46bf4..30c517aa 100644
> > --- a/toys/other/stat.c
> > +++ b/toys/other/stat.c
> > @@ -108,9 +108,13 @@ static void print_stat(char type)
> > dev_t dev = stat->st_rdev ? stat->st_rdev : stat->st_dev;
> >
> > // This mount point could exist multiple times, so show oldest.
> > - for (dlist_terminate(mt); mt; mt = mt->next) if (mt->stat.st_dev == dev) {
> > - strout(mt->dir);
> > - break;
> > + for (dlist_terminate(mt); mt; mt = mt->next) {
> > + // Using fstatat as stat(2) conflict with the local variable.
>
> Then I should probably fix the local variable...
>
> > + if (!mt->stat.st_dev) fstatat(AT_FDCWD, mt->dir, &mt->stat, 0);
> > + if (mt->stat.st_dev == dev) {
> > + strout(mt->dir);
> > + break;
> > + }
> > }
>
> If this is the only/immediate consumer why put it in the list variable
> instead of just having a local? (Is the re-stat expensive once the
> mount's been spun up and its superblock presumably cached in the kernel?)
>
> > llist_traverse(mt, free);
> > } else if (type == 'N') {
> > diff --git a/toys/posix/df.c b/toys/posix/df.c
> > index a771f4e0..debac745 100644
> > --- a/toys/posix/df.c
> > +++ b/toys/posix/df.c
> > @@ -161,21 +161,75 @@ void df_main(void)
> > } else {
> > // Find and display this filesystem. Use _last_ hit in case of
> > // overmounts (which is first hit in the reversed list).
> > - for (mt = mtend, mt2 = 0; mt; mt = mt->prev) {
> > - if (!mt2 && st.st_dev == mt->stat.st_dev) mt2 = mt;
> > - if (st.st_rdev && (st.st_rdev == mt->stat.st_dev)) break;
> > + struct mtab_list *best_mt = 0, *dev_mt = 0;
> > + char *ca = xabspath(*next, 0);
> > + int best_len = -1;
> > +
> > + for (mt = mtend; mt; mt = mt->prev) {
> > + int dlen = strlen(mt->dir);
> > +
> > + if (st.st_rdev && (!strcmp(mt->device, *next) ||
> > + (ca && !strcmp(mt->device, ca))))
> > + {
> > + dev_mt = mt;
> > + break;
> > + }
> > + if (dlen > best_len && ca) {
> > + if (!strcmp(mt->dir, "/") || (!strncmp(ca, mt->dir, dlen) &&
> > + (ca[dlen] == '/' || !ca[dlen])))
> > + {
> > + best_len = dlen;
> > + best_mt = mt;
> > + }
> > + }
>
> This is WAY more complicated than what was there before. And has
> replaced integer comparisons (which should be unique) with multiple
> string comparisons (which can be overmounts of different filesystems),
> and I've never been 100% sure my hand-rolled abspath() produces the same
> results as the kernel in all cases (it _should_, but the kernel gets
> WEIRD when you traverse --bind mounts and such), and you're special
> casing "/" for some reason.
>
> Did we make sure we're de-escaping the kernel's escape sequences for
> names with spaces and such in them?
>
> $ mkdir 'potato salad'
> $ sudo mount -t tmpfs blah 'potato salad'
> $ grep potato /proc/mounts
> blah /home/landley/toybox/toybox/potato\040salad tmpfs
> rw,relatime,inode64 0 0
>
> I _think_ the existing lib/ plumbing is doing that right, but it wasn't
> depending on it before. As soon as you stick directory string
> comparisons in there it gets funky...
>
> > }
> > - show_mt(mt ? : mt2, measuring);
> > +
> > + // If device node didn't match by name, try matching by device ID
> > + if (st.st_rdev && !dev_mt) {
> > + for (mt = mtend; mt; mt = mt->prev) {
> > + if (!mt->stat.st_dev) stat(mt->dir, &mt->stat);
> > + if (st.st_rdev == mt->stat.st_dev) {
> > + dev_mt = mt;
> > + break;
> > + }
> > + }
> > + }
>
> And then you have a fallback to device ID anyway, and we still aren't done.
>
> Right, if I plug in my USB dvd reader, will that be a sufficiently slow
> spin up that I can reproduce the problem you're seeing? (Not
> pathological, just detectable. I want a test case...)
>
> > + if (dev_mt) best_mt = dev_mt;
> > + free(ca);
> > +
> > + if (!best_mt) best_mt = mtstart;
> > + if (st.st_rdev && !best_mt->stat.st_dev)
> > + stat(best_mt->dir, &best_mt->stat);
> > + else if (!st.st_rdev) best_mt->stat.st_dev = st.st_dev;
> > + // We may have called statvfs already (measuring=1),
> > + // check f_frsize to detect.
> > + if (!best_mt->statvfs.f_frsize) {
> > + memset(&best_mt->statvfs, 0, sizeof(best_mt->statvfs));
> > + statvfs(best_mt->dir, &best_mt->statvfs);
> > + }
> > + show_mt(best_mt, measuring);
>
> Again, once you've done a stat() once recently it should theoretically
> be cached in the kernel, so doing it again shouldn't be expensive the
> second time? (Or am I wrong about that? And in theory O_NONBLOCK should
> give you "what we've got" without triggering blocking actions, so we may
> need better -ENOIDEA error handling but that's true if you have a
> network timeout on NFS, scuffed DVD, flash partition that's worn through
> its supply of remap blocks...)
>
> And then there's still more:
>
> > }
> > }
> > if (!measuring--) break;
> > print_header();
> > }
> > } else {
> > + for (mt = mtstart; mt; mt = mt->next) {
> > + if (TT.t) {
> > + struct arg_list *al;
> > +
> > + for (al = TT.t; al; al = al->next)
> > + if (!strcmp(mt->type, al->arg)) break;
> > + if (!al) continue;
> > + }
> > + stat(mt->dir, &mt->stat);
> > + statvfs(mt->dir, &mt->statvfs);
> > + }
> > +
> > // Loop through mount list to filter out overmounts.
> > for (mt = mtend; mt; mt = mt->prev) {
>
> You added a loop over mtstart that wasn't there before, which... fills
> in (some of) the stats that were previously done by the lib
> implementation. Again using strcmp(), except this time you didn't
> abspath(al->arg) so I think you're comparing what the filesystem gave us
> against a symlink and/or relative path from the command line?
>
> > for (mt3 = mt, mt2 = mt->prev; mt2; mt2 = mt2->prev) {
> > - if (mt->stat.st_dev == mt2->stat.st_dev) {
> > + if (mt->stat.st_dev && mt->stat.st_dev == mt2->stat.st_dev) {
> > // For --bind mounts, show earliest mount
> > if (!strcmp(mt->device, mt2->device)) {
> > mt3->stat.st_dev = 0;
>
> I need to come back and look at this when I have more brain, to reverse
> engineer what specifically you're trying to accomplish. Your new
> implementation has noticeably more surface area than the previous one.
>
> I'd really like to find out of the O_NONBLOCK trick can at least give us
> the subset of info about the mount to identify "is this filesystem the
> same one as this other filesystem". Because in THEORY anything that's
> actually mounted needs to know which st_dev is mounted there (whether or
> not it's spun up or the pipe to the server is open), and if all we're
> trying to do is go "is this dev the same as that dev", we can
> nonblockingly stat() one path and nonblockingly stat() the other path
> and go "yes, they're the same".
>
> And then open it AGAIN O_NONBLOCK if we need to force it to spin up if
> we need things like current free block counts...
>
> Rob
Thanks for the review Rob. Yeah, I should have known after that 3rd
extra loop that this was a rabbit hole. Thanks for looking at the
approach though.
The main issue we observe is these extra verbose logs from SELinux.
Basically, in our tests we are validating the space available on the
device /data directory (`df /data`), which triggers many denial logs
when trying to access other mount points.
I extrapolated the issue for any slow/blocking device, but you're
right, it can be handled with O_NONBLOCK.
More information about the Toybox
mailing list