04161d
From b9f9ed14bda93ecb407129b69e6476813c250046 Mon Sep 17 00:00:00 2001
04161d
From: Paul Eggert <eggert@cs.ucla.edu>
04161d
Date: Wed, 15 Apr 2020 20:50:32 -0700
04161d
Subject: [PATCH] fts: remove NOSTAT_LEAF_OPTIMIZATION
04161d
MIME-Version: 1.0
04161d
Content-Type: text/plain; charset=UTF-8
04161d
Content-Transfer-Encoding: 8bit
04161d
04161d
It caused ‘find’ and ‘du’ to dump core, and it was useful
04161d
only for obsolescent Linux filesystems anyway.  Problem reported in:
04161d
https://lists.gnu.org/r/bug-gnulib/2020-04/msg00068.html
04161d
Quite possibly there is still a serious underlying fts bug with
04161d
tight-loop-check and mutating file systems, but if so this patch
04161d
should cause the bug to be triggered less often.
04161d
* lib/fts.c (enum leaf_optimization): Remove
04161d
NOSTAT_LEAF_OPTIMIZATION, as it’s problematic.
04161d
(S_MAGIC_REISERFS, S_MAGIC_XFS): Remove; no longer needed.
04161d
(leaf_optimization): Remove special cases for ReiserFS and XFS.
04161d
(fts_read): Remove NOSTAT_LEAF_OPTIMIZATION code.
04161d
* lib/fts_.h (struct _ftsent.fts_n_dirs_remaining):
04161d
Remove.  All uses removed.
04161d
04161d
Upstream-commit: 47bf2cf3184027c1eb9c1dfeea5c5b8b2d69710d
04161d
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
04161d
---
04161d
 lib/fts.c  | 56 ++++++++-------------------------------------------
04161d
 lib/fts_.h |  5 -----
04161d
 2 files changed, 8 insertions(+), 53 deletions(-)
04161d
04161d
diff --git a/lib/fts.c b/lib/fts.c
04161d
index 1093ce5..dfe3fef 100644
04161d
--- a/lib/fts.c
04161d
+++ b/lib/fts.c
04161d
@@ -445,7 +445,6 @@ fts_open (char * const *argv,
04161d
                 if ((parent = fts_alloc(sp, "", 0)) == NULL)
04161d
                         goto mem2;
04161d
                 parent->fts_level = FTS_ROOTPARENTLEVEL;
04161d
-                parent->fts_n_dirs_remaining = -1;
04161d
           }
04161d
 
04161d
         /* The classic fts implementation would call fts_stat with
04161d
@@ -634,9 +633,8 @@ fts_close (FTS *sp)
04161d
 }
04161d
 
04161d
 /* Minimum link count of a traditional Unix directory.  When leaf
04161d
-   optimization is OK and MIN_DIR_NLINK <= st_nlink, then st_nlink is
04161d
-   an upper bound on the number of subdirectories (counting "." and
04161d
-   "..").  */
04161d
+   optimization is OK and a directory's st_nlink == MIN_DIR_NLINK,
04161d
+   then the directory has no subdirectories.  */
04161d
 enum { MIN_DIR_NLINK = 2 };
04161d
 
04161d
 /* Whether leaf optimization is OK for a directory.  */
04161d
@@ -645,12 +643,8 @@ enum leaf_optimization
04161d
     /* st_nlink is not reliable for this directory's subdirectories.  */
04161d
     NO_LEAF_OPTIMIZATION,
04161d
 
04161d
-    /* Leaf optimization is OK, but is not useful for avoiding stat calls.  */
04161d
-    OK_LEAF_OPTIMIZATION,
04161d
-
04161d
-    /* Leaf optimization is not only OK: it is useful for avoiding
04161d
-       stat calls, because dirent.d_type does not work.  */
04161d
-    NOSTAT_LEAF_OPTIMIZATION
04161d
+    /* st_nlink == 2 means the directory lacks subdirectories.  */
04161d
+    OK_LEAF_OPTIMIZATION
04161d
   };
04161d
 
04161d
 #if (defined __linux__ || defined __ANDROID__) \
04161d
@@ -663,9 +657,7 @@ enum leaf_optimization
04161d
 # define S_MAGIC_CIFS 0xFF534D42
04161d
 # define S_MAGIC_NFS 0x6969
04161d
 # define S_MAGIC_PROC 0x9FA0
04161d
-# define S_MAGIC_REISERFS 0x52654973
04161d
 # define S_MAGIC_TMPFS 0x1021994
04161d
-# define S_MAGIC_XFS 0x58465342
04161d
 
04161d
 # ifdef HAVE___FSWORD_T
04161d
 typedef __fsword_t fsword;
04161d
@@ -786,23 +778,15 @@ dirent_inode_sort_may_be_useful (FTSENT const *p, int dir_fd)
04161d
 }
04161d
 
04161d
 /* Given an FTS entry P for a directory with descriptor DIR_FD,
04161d
-   return true if it is both useful and valid to apply leaf optimization.
04161d
-   The optimization is useful only for file systems that lack usable
04161d
-   dirent.d_type info.  The optimization is valid if an st_nlink value
04161d
-   of at least MIN_DIR_NLINK is an upper bound on the number of
04161d
-   subdirectories of D, counting "." and ".."  as subdirectories.
04161d
+   return whether it is valid to apply leaf optimization.
04161d
+   The optimization is valid if a directory's st_nlink value equal
04161d
+   to MIN_DIR_NLINK means the directory has no subdirectories.
04161d
    DIR_FD is negative if unavailable.  */
04161d
 static enum leaf_optimization
04161d
 leaf_optimization (FTSENT const *p, int dir_fd)
04161d
 {
04161d
   switch (filesystem_type (p, dir_fd))
04161d
     {
04161d
-      /* List here the file system types that may lack usable dirent.d_type
04161d
-         info, yet for which the optimization does apply.  */
04161d
-    case S_MAGIC_REISERFS:
04161d
-    case S_MAGIC_XFS: /* XFS lacked it until 2013-08-22 commit.  */
04161d
-      return NOSTAT_LEAF_OPTIMIZATION;
04161d
-
04161d
     case 0:
04161d
       /* Leaf optimization is unsafe if the file system type is unknown.  */
04161d
       FALLTHROUGH;
04161d
@@ -1027,26 +1011,7 @@ check_for_dir:
04161d
                 if (p->fts_info == FTS_NSOK)
04161d
                   {
04161d
                     if (p->fts_statp->st_size == FTS_STAT_REQUIRED)
04161d
-                      {
04161d
-                        FTSENT *parent = p->fts_parent;
04161d
-                        if (parent->fts_n_dirs_remaining == 0
04161d
-                            && ISSET(FTS_NOSTAT)
04161d
-                            && ISSET(FTS_PHYSICAL)
04161d
-                            && (leaf_optimization (parent, sp->fts_cwd_fd)
04161d
-                                == NOSTAT_LEAF_OPTIMIZATION))
04161d
-                          {
04161d
-                            /* nothing more needed */
04161d
-                          }
04161d
-                        else
04161d
-                          {
04161d
-                            p->fts_info = fts_stat(sp, p, false);
04161d
-                            if (S_ISDIR(p->fts_statp->st_mode)
04161d
-                                && p->fts_level != FTS_ROOTLEVEL
04161d
-                                && 0 < parent->fts_n_dirs_remaining
04161d
-                                && parent->fts_n_dirs_remaining != (nlink_t) -1)
04161d
-                                  parent->fts_n_dirs_remaining--;
04161d
-                          }
04161d
-                      }
04161d
+                      p->fts_info = fts_stat(sp, p, false);
04161d
                     else
04161d
                       fts_assert (p->fts_statp->st_size == FTS_NO_STAT_REQUIRED);
04161d
                   }
04161d
@@ -1830,11 +1795,6 @@ err:            memset(sbp, 0, sizeof(struct stat));
04161d
         }
04161d
 
04161d
         if (S_ISDIR(sbp->st_mode)) {
04161d
-                p->fts_n_dirs_remaining
04161d
-                  = ((sbp->st_nlink < MIN_DIR_NLINK
04161d
-                      || p->fts_level <= FTS_ROOTLEVEL)
04161d
-                     ? -1
04161d
-                     : sbp->st_nlink - (ISSET (FTS_SEEDOT) ? 0 : MIN_DIR_NLINK));
04161d
                 if (ISDOT(p->fts_name)) {
04161d
                         /* Command-line "." and ".." are real directories. */
04161d
                         return (p->fts_level == FTS_ROOTLEVEL ? FTS_D : FTS_DOT);
04161d
diff --git a/lib/fts_.h b/lib/fts_.h
04161d
index d40a116..2e76cc4 100644
04161d
--- a/lib/fts_.h
04161d
+++ b/lib/fts_.h
04161d
@@ -227,11 +227,6 @@ typedef struct _ftsent {
04161d
 
04161d
         size_t fts_namelen;             /* strlen(fts_name) */
04161d
 
04161d
-        /* If not (nlink_t) -1, an upper bound on the number of
04161d
-           remaining subdirectories of interest.  If this becomes
04161d
-           zero, some work can be avoided.  */
04161d
-        nlink_t fts_n_dirs_remaining;
04161d
-
04161d
 # define FTS_D           1              /* preorder directory */
04161d
 # define FTS_DC          2              /* directory that causes cycles */
04161d
 # define FTS_DEFAULT     3              /* none of the above */
04161d
-- 
04161d
2.21.1
04161d