From patchwork Wed Oct 21 14:15:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 40800 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3963B3857022; Wed, 21 Oct 2020 14:15:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3963B3857022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1603289757; bh=k87iBi3edRIxG+dOjfP7/nZKftIYy42px0Aq4c6T8os=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ijvLpRcd+RfMvL3jiWhkHmEI+VCzd9hdMGgFb4cKd+tnycCfp29kVuj8BCCUzXCZV ijvNDJlh9vmGRQcnHq3MczzWvoTBI4bKntmQp+SDh9QdoQZIddVVrg83zvn4g2Sofv 8cRpzgJkqKgvPE+j7/HTBCRHllYAjiy0R+5QXwmg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x741.google.com (mail-qk1-x741.google.com [IPv6:2607:f8b0:4864:20::741]) by sourceware.org (Postfix) with ESMTPS id 442B03857022 for ; Wed, 21 Oct 2020 14:15:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 442B03857022 Received: by mail-qk1-x741.google.com with SMTP id v200so2641890qka.0 for ; Wed, 21 Oct 2020 07:15:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=k87iBi3edRIxG+dOjfP7/nZKftIYy42px0Aq4c6T8os=; b=WCMjetbKObJW/yZv78+TEHxiSiNCxoYbhYr5nauSpgRlaZlKe+6fNyEMjbTbX5apvu ykw/e9R47zX3XqP9EF8M7+e74GuTzpcvdYVx00l40nIWeuvk7BoC4+hPYTyyIKC9Z+l5 Y/qA4xbpPo+WAS9e5ployyCo8FaRDEXm+lFHzBDO/hVAW/+lL1Hsy0a+JiQQo2er8xUW ONK2YcaxHYthG3TlAJjpSncewbuR19L0Pd/dvBXtMv/UYnYb4jTsPM+dQvhevM3252Nv wS9H2fOR0EQr6rfzKwujuVKZqbr+Q4Hilnv0jlO8lciPeRJwXveYPUFnqk2kz4J5/cVT r+gw== X-Gm-Message-State: AOAM533WpiX5v3Hgkz0ZoI5V+lInNFzoTdIBNb5gJhsT/za30lhfawGy UMiREVNndNNteDBUBp9kYLHw2v36Rt4Qpw== X-Google-Smtp-Source: ABdhPJxoi00CHxOrXkJO0UfEFPD8nBhGh4e8577ghZQVIEARTobxcfGKinhxmV4TgBNhLqfcAweW8A== X-Received: by 2002:a37:9045:: with SMTP id s66mr3488704qkd.223.1603289753507; Wed, 21 Oct 2020 07:15:53 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id e17sm1187952qts.54.2020.10.21.07.15.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Oct 2020 07:15:53 -0700 (PDT) To: libc-alpha@sourceware.org, Florian Weimer Subject: [PATCH v3 2/7] linux: Use getdents64 on non-LFS readdir Date: Wed, 21 Oct 2020 11:15:37 -0300 Message-Id: <20201021141542.2003377-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201021141542.2003377-1-adhemerval.zanella@linaro.org> References: <20201021141542.2003377-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: James Clarke , John Paul Adrian Glaubitz Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The opendir allocates a translation buffer to be used to return the non-LFS readdir entry. The obtained dirent64 struct is translated to the temporary buffer on each readdir call. Entries that overflow d_off/d_ino and the buffer reallocation failure (in case of large d_name) are ignored. Checked on x86_64-linux-gnu and i686-linux-gnu. --- sysdeps/unix/sysv/linux/closedir.c | 4 ++ sysdeps/unix/sysv/linux/dirstream.h | 5 ++ sysdeps/unix/sysv/linux/opendir.c | 21 +++++++ sysdeps/unix/sysv/linux/readdir.c | 97 +++++++++++++++++++++-------- 4 files changed, 101 insertions(+), 26 deletions(-) diff --git a/sysdeps/unix/sysv/linux/closedir.c b/sysdeps/unix/sysv/linux/closedir.c index ccc19eefcd..d67b590d5f 100644 --- a/sysdeps/unix/sysv/linux/closedir.c +++ b/sysdeps/unix/sysv/linux/closedir.c @@ -47,6 +47,10 @@ __closedir (DIR *dirp) __libc_lock_fini (dirp->lock); #endif +#if !_DIRENT_MATCHES_DIRENT64 + free (dirp->tbuffer); +#endif + free ((void *) dirp); return __close_nocancel (fd); diff --git a/sysdeps/unix/sysv/linux/dirstream.h b/sysdeps/unix/sysv/linux/dirstream.h index a3ea2b7197..da9315d353 100644 --- a/sysdeps/unix/sysv/linux/dirstream.h +++ b/sysdeps/unix/sysv/linux/dirstream.h @@ -41,6 +41,11 @@ struct __dirstream int errcode; /* Delayed error code. */ +#if !defined __OFF_T_MATCHES_OFF64_T || !defined __INO_T_MATCHES_INO64_T + char *tbuffer; /* Translation buffer for non-LFS calls. */ + size_t tbuffer_size; /* Size of translation buffer. */ +#endif + /* Directory block. We must make sure that this block starts at an address that is aligned adequately enough to store dirent entries. Using the alignment of "void *" is not diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c index 2198224588..329575e809 100644 --- a/sysdeps/unix/sysv/linux/opendir.c +++ b/sysdeps/unix/sysv/linux/opendir.c @@ -119,6 +119,27 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp) return NULL; } +#if !_DIRENT_MATCHES_DIRENT64 + /* Allocates a translation buffer to use as the returned 'struct direct' + for non-LFS 'readdir' calls. + + The initial NAME_MAX size should handle most cases, while readdir might + expand the buffer if required. */ + enum + { + tbuffer_size = sizeof (struct dirent) + NAME_MAX + 1 + }; + dirp->tbuffer = malloc (tbuffer_size); + if (dirp->tbuffer == NULL) + { + free (dirp); + if (close_fd) + __close_nocancel_nostatus (fd); + return NULL; + } + dirp->tbuffer_size = tbuffer_size; +#endif + dirp->fd = fd; #if IS_IN (libc) __libc_lock_init (dirp->lock); diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c index 1a9820e475..9af4561a8c 100644 --- a/sysdeps/unix/sysv/linux/readdir.c +++ b/sysdeps/unix/sysv/linux/readdir.c @@ -21,42 +21,87 @@ #if !_DIRENT_MATCHES_DIRENT64 #include +/* Translate the DP64 entry to the non-LFS one in the translation buffer + at dirstream DS. Return true is the translation was possible or + false if either an internal fields can be represented in the non-LFS + entry or if the translation can not be resized. */ +static bool +dirstream_entry (struct __dirstream *ds, const struct dirent64 *dp64) +{ + off_t d_off = dp64->d_off; + if (d_off != dp64->d_off) + return false; + ino_t d_ino = dp64->d_ino; + if (d_ino != dp64->d_ino) + return false; + + /* Expand the translation buffer to hold the new name size. */ + size_t new_reclen = sizeof (struct dirent) + + dp64->d_reclen - offsetof (struct dirent64, d_name); + if (new_reclen > ds->tbuffer_size) + { + char *newbuffer = realloc (ds->tbuffer, new_reclen); + if (newbuffer == NULL) + return false; + ds->tbuffer = newbuffer; + ds->tbuffer_size = new_reclen; + } + + struct dirent *dp = (struct dirent *) ds->tbuffer; + + dp->d_off = d_off; + dp->d_ino = d_ino; + dp->d_reclen = new_reclen; + dp->d_type = dp64->d_type; + memcpy (dp->d_name, dp64->d_name, + dp64->d_reclen - offsetof (struct dirent64, d_name)); + + return true; +} + /* Read a directory entry from DIRP. */ struct dirent * __readdir_unlocked (DIR *dirp) { const int saved_errno = errno; - if (dirp->offset >= dirp->size) + while (1) { - /* We've emptied out our buffer. Refill it. */ - ssize_t bytes = __getdents (dirp->fd, dirp->data, dirp->allocation); - if (bytes <= 0) + if (dirp->offset >= dirp->size) { - /* On some systems getdents fails with ENOENT when the - open directory has been rmdir'd already. POSIX.1 - requires that we treat this condition like normal EOF. */ - if (bytes < 0 && errno == ENOENT) - bytes = 0; - - /* Don't modifiy errno when reaching EOF. */ - if (bytes == 0) - __set_errno (saved_errno); - return NULL; + /* We've emptied out our buffer. Refill it. */ + ssize_t bytes = __getdents64 (dirp->fd, dirp->data, + dirp->allocation); + if (bytes <= 0) + { + /* On some systems getdents fails with ENOENT when the + open directory has been rmdir'd already. POSIX.1 + requires that we treat this condition like normal EOF. */ + if (bytes < 0 && errno == ENOENT) + bytes = 0; + + /* Don't modifiy errno when reaching EOF. */ + if (bytes == 0) + __set_errno (saved_errno); + return NULL; + } + dirp->size = bytes; + + /* Reset the offset into the buffer. */ + dirp->offset = 0; + } + + struct dirent64 *dp64 = (struct dirent64 *) &dirp->data[dirp->offset]; + dirp->offset += dp64->d_reclen; + + /* Skip entries which might overflow d_off/d_ino or if the translation + buffer can't be resized. */ + if (dirstream_entry (dirp, dp64)) + { + dirp->filepos = dp64->d_off; + return (struct dirent *) dirp->tbuffer; } - dirp->size = bytes; - - /* Reset the offset into the buffer. */ - dirp->offset = 0; } - - struct dirent *dp = (struct dirent *) &dirp->data[dirp->offset]; - - dirp->offset += dp->d_reclen; - - dirp->filepos = dp->d_off; - - return dp; } struct dirent *