[Toybox] [PATCH] df, stat: query mount stats lazily instead of eagerly in xgetmountlist

Thiébaud Weksteen tweek at google.com
Thu Aug 20 05:07:23 UTC 2026


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).

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));
-    }
-
     // 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.
+      if (!mt->stat.st_dev) fstatat(AT_FDCWD, mt->dir, &mt->stat, 0);
+      if (mt->stat.st_dev == dev) {
+        strout(mt->dir);
+        break;
+      }
     }
     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;
+              }
+            }
           }
-          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;
+              }
+            }
+          }
+          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);
         }
       }
       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) {
       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;
-- 
2.55.0.737.g08866a6d13-goog



More information about the Toybox mailing list