From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Subject: [PATCH 02/10] linux: Simplify opendir buffer allocation
Date: Fri, 17 Apr 2020 10:22:01 -0300 [thread overview]
Message-ID: <20200417132209.22065-2-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20200417132209.22065-1-adhemerval.zanella@linaro.org>
THe fallback allocation is removed, so the possible size constraint
should be analized just once; __alloc_dir assumes that 'statp'
argument is non-null, and the max_buffer_size move to close its
used.
Checked on x86_64-linux-gnu and i686-linux-gnu.
---
include/dirent.h | 3 +-
sysdeps/unix/sysv/linux/opendir.c | 52 +++++++++++--------------------
2 files changed, 21 insertions(+), 34 deletions(-)
diff --git a/include/dirent.h b/include/dirent.h
index 2b1cdcf8bd..fdf4c4a2f1 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -48,7 +48,8 @@ extern int __versionsort64 (const struct dirent64 **a,
const struct dirent64 **b)
__attribute_pure__;
extern DIR *__alloc_dir (int fd, bool close_fd, int flags,
- const struct stat64 *statp) attribute_hidden;
+ const struct stat64 *statp)
+ __nonnull (4) attribute_hidden;
extern __typeof (rewinddir) __rewinddir;
extern __typeof (seekdir) __seekdir;
extern __typeof (dirfd) __dirfd;
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index c6ab79246c..765c8104b3 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -23,12 +23,6 @@
#include <not-cancel.h>
-/* The st_blksize value of the directory is used as a hint for the
- size of the buffer which receives struct dirent values from the
- kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
- file system provides a bogus value. */
-#define MAX_DIR_BUFFER_SIZE 1048576U
-
enum {
opendir_oflags = O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE|O_CLOEXEC
};
@@ -100,38 +94,30 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp)
file descriptor. */
if (!close_fd
&& __glibc_unlikely (__fcntl64_nocancel (fd, F_SETFD, FD_CLOEXEC) < 0))
- goto lose;
-
- const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : 4 * BUFSIZ);
- const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : BUFSIZ);
- size_t allocation = default_allocation;
-#ifdef _STATBUF_ST_BLKSIZE
+ return NULL;
+
+ /* The st_blksize value of the directory is used as a hint for the
+ size of the buffer which receives struct dirent values from the
+ kernel. st_blksize is limited to max_buffer_size, in case the
+ file system provides a bogus value. */
+ enum { max_buffer_size = 1U << 20 };
+
+ const size_t allocation_size = 4 * BUFSIZ;
+ _Static_assert (allocation_size >= sizeof (struct dirent64),
+ "allocation_size < sizeof (struct dirent64)");
+
/* Increase allocation if requested, but not if the value appears to
- be bogus. */
- if (statp != NULL)
- allocation = MIN (MAX ((size_t) statp->st_blksize, default_allocation),
- MAX_DIR_BUFFER_SIZE);
-#endif
+ be bogus. It will be between 32Kb (for blocksizes smaller than BUFSIZ)
+ up to 1Mb. */
+ size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
+ max_buffer_size);
DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
if (dirp == NULL)
{
- allocation = small_allocation;
- dirp = (DIR *) malloc (sizeof (DIR) + allocation);
-
- if (dirp == NULL)
- lose:
- {
- if (close_fd)
- {
- int save_errno = errno;
- __close_nocancel_nostatus (fd);
- __set_errno (save_errno);
- }
- return NULL;
- }
+ if (close_fd)
+ __close_nocancel_nostatus (fd);
+ return NULL;
}
dirp->fd = fd;
--
2.17.1
next prev parent reply other threads:[~2020-04-17 13:22 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-17 13:22 [PATCH 01/10] linux: Move posix dir implementations to Linux Adhemerval Zanella
2020-04-17 13:22 ` Adhemerval Zanella [this message]
2020-04-21 10:28 ` [PATCH 02/10] linux: Simplify opendir buffer allocation Florian Weimer
2020-04-23 21:27 ` Rafal Luzynski
2020-04-29 17:09 ` Adhemerval Zanella
2020-04-23 21:39 ` Adhemerval Zanella
2020-04-24 10:11 ` Florian Weimer
2020-04-24 12:08 ` Adhemerval Zanella
2020-04-24 13:08 ` Florian Weimer
2020-04-17 13:22 ` [PATCH 03/10] linux: Add __readdir_unlocked Adhemerval Zanella
2020-04-21 10:41 ` Florian Weimer
2020-04-21 12:03 ` Adhemerval Zanella
2020-04-21 12:16 ` Florian Weimer
2020-04-21 13:00 ` Adhemerval Zanella
2020-05-27 16:38 ` Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 04/10] linux: Use internal DIR locks when accessing filepos on telldir Adhemerval Zanella
2020-04-21 10:33 ` Florian Weimer
2020-04-17 13:22 ` [PATCH 05/10] linux: Use getdents64 on non-LFS readdir Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 06/10] linux: Set internal DIR filepos as off64_t [BZ #23960, BZ #24050] Adhemerval Zanella
2020-04-20 15:01 ` Andreas Schwab
2020-04-20 15:02 ` Florian Weimer
2020-04-20 15:06 ` Andreas Schwab
2020-04-21 12:04 ` Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 07/10] linux: Add __readdir64_unlocked Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 08/10] linux: Add __old_readdir64_unlocked Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 09/10] linux: Use getdents64 on readdir64 compat implementation Adhemerval Zanella
2020-04-17 13:22 ` [PATCH 10/10] dirent: Deprecate getdirentries Adhemerval Zanella
2020-04-22 10:10 ` Florian Weimer
2020-04-20 14:53 ` [PATCH 01/10] linux: Move posix dir implementations to Linux Andreas Schwab
2020-04-21 10:15 ` Florian Weimer
2020-04-21 11:51 ` Adhemerval Zanella
2020-05-27 16:35 ` Adhemerval Zanella
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200417132209.22065-2-adhemerval.zanella@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).