From nobody Fri Apr 28 01:29:56 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q6w5J4mGYz47kny; Fri, 28 Apr 2023 01:29:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q6w5J497mz3rnt; Fri, 28 Apr 2023 01:29:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682645396; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=evnxdOEJKy+j5Pz1CS7sm9gnJ4/r68Sp+DnYsjpLUm0=; b=OqLemzHy05eq5+Sw/LrD2Tbov8Hr1N3oOvdZl4GkDyRJR2xRekDTIGAhETldTISgGS+5dp QhkAePjoqjn28SFSXfCM5BmmiJOrZwgJ1edxlp6lb98PnNr8sY0jOAyDptXGyetK7QtWgb +EaNrihOvrFyZWx4fPo04s6+LiDhHX4mV7aG++URozNfV2A0FUVu/znZlcbee+HLcLlsvT kqQJ0v6HtLgdnyVfzFAD2se9pFFs8UbL8cHe9GQpJhJaUVbsD708wB/zw5x5YHFzHmr87O VfogTe+EmAQ9P/tczi0Afbehqnvbhv0RdJ8um+Jd6MOAhaZEFnROah9rLxqdkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682645396; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=evnxdOEJKy+j5Pz1CS7sm9gnJ4/r68Sp+DnYsjpLUm0=; b=qmUimgnTYGIiESPbpjAab9vABG3RmKftIdFCQP0vquBz/9clsXqXGL9k6H4TnDAICTMmJk oaWkS9Rw8i83T7i53cTTa/BzDGjya+jsiRFt5kgW6T5Asin9rKpDvBe0N6xFosFw2dYlh1 +wvTm8ul41C42CNzg8cswKKICoVNlMNil8D1Tix9QCQsLwj5SR5C+89BgU++CbG7rXYn6m Mo8Q0Xt3cypmnjUMlintqN/LzPMuJgob5K6z9vtLr3/r4RYhIBUAS6meCO9bt2mbUpfEa7 b30cFg9kqHDyHRKSMI6MNRdrflRoB6/kiEFJXGii9JRfwNnGn10tAuZujiWK6A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1682645396; a=rsa-sha256; cv=none; b=RfFmZy773RvSZztxm3Gjr6RyXTl3tKN76Twe8og/rm2vgQwC80N0BI61jh/iRIctGSsFcz M9B+V4czWfesgZG1KUw7WJ5ZE4ApHDQdlbFpW+NCOVmeWxKzIM1SfBkoJlEEWt2eBY8+Rh 8Ox74jsr4WsukQX5MajMJvOqcTDuxgvOeqQcvhnDO8vQ04zdogvFDToKMZ52GILT9AT7BC jIokOApnQusOU5bK2ok/7B0ZGKGCBadLxN8iJu7uXHFSA5xT8tY0mt7ol0k6Sbe+TSzFJE mWlqhkNnRG1bI/Xtx+1odzVeIDWNNP6lZDp8rLX6EpkPv1ehe98kYut3UbQ0Yw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Q6w5J3FhbzgXF; Fri, 28 Apr 2023 01:29:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 33S1TuVo075195; Fri, 28 Apr 2023 01:29:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33S1TuWU075194; Fri, 28 Apr 2023 01:29:56 GMT (envelope-from git) Date: Fri, 28 Apr 2023 01:29:56 GMT Message-Id: <202304280129.33S1TuWU075194@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 6450e7bbad0c - main - vfs: Fix "emptydir" mount option List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6450e7bbad0c68176f28b51773a3af5d6022c7dd Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=6450e7bbad0c68176f28b51773a3af5d6022c7dd commit 6450e7bbad0c68176f28b51773a3af5d6022c7dd Author: Olivier Certner AuthorDate: 2023-04-22 16:07:07 +0000 Commit: Konstantin Belousov CommitDate: 2023-04-28 01:27:54 +0000 vfs: Fix "emptydir" mount option Fix vfs_emptydir(). It would consider directories containing directories with name of the form 'X.' (X being any authorized byte) as empty. Also, it would cause VOP_READDIR() to return an error on directories containing enough whiteouts. While here, use a more decently sized buffer as done elsewhere. Remove ad-hoc iteration on the directory's content and instead use the newly exported vn_dir_next_dirent() function (this is what fixes the second problem mentioned above). PR: 270988 Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D39775 --- sys/kern/vfs_subr.c | 115 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 48 deletions(-) diff --git a/sys/kern/vfs_subr.c b/sys/kern/vfs_subr.c index eac6fa0ddb6a..3c783f7326d0 100644 --- a/sys/kern/vfs_subr.c +++ b/sys/kern/vfs_subr.c @@ -6391,65 +6391,84 @@ filt_vfsvnode(struct knote *kn, long hint) int vfs_emptydir(struct vnode *vp) { - struct uio uio; - struct iovec iov; - struct dirent *dirent, *dp, *endp; - int error, eof; - - error = 0; - eof = 0; + struct thread *const td = curthread; + char *dirbuf; + size_t dirbuflen, len; + off_t off; + int eofflag, error; + struct dirent *dp; + struct vattr va; ASSERT_VOP_LOCKED(vp, "vfs_emptydir"); VNPASS(vp->v_type == VDIR, vp); - dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK); - iov.iov_base = dirent; - iov.iov_len = sizeof(struct dirent); - - uio.uio_iov = &iov; - uio.uio_iovcnt = 1; - uio.uio_offset = 0; - uio.uio_resid = sizeof(struct dirent); - uio.uio_segflg = UIO_SYSSPACE; - uio.uio_rw = UIO_READ; - uio.uio_td = curthread; - - while (eof == 0 && error == 0) { - error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof, - NULL, NULL); + error = VOP_GETATTR(vp, &va, td->td_ucred); + if (error != 0) + return (error); + + dirbuflen = max(DEV_BSIZE, GENERIC_MAXDIRSIZ); + if (dirbuflen < va.va_blocksize) + dirbuflen = va.va_blocksize; + dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK); + + len = 0; + off = 0; + eofflag = 0; + + for (;;) { + error = vn_dir_next_dirent(vp, td, dirbuf, dirbuflen, + &dp, &len, &off, &eofflag); if (error != 0) - break; - endp = (void *)((uint8_t *)dirent + - sizeof(struct dirent) - uio.uio_resid); - for (dp = dirent; dp < endp; - dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) { - if (dp->d_type == DT_WHT) - continue; - if (dp->d_namlen == 0) - continue; - if (dp->d_type != DT_DIR && - dp->d_type != DT_UNKNOWN) { - error = ENOTEMPTY; - break; - } - if (dp->d_namlen > 2) { - error = ENOTEMPTY; - break; - } - if (dp->d_namlen == 1 && - dp->d_name[0] != '.') { + goto end; + + if (len == 0) { + /* EOF */ + error = 0; + goto end; + } + + /* + * Skip whiteouts. Unionfs operates on filesystems only and not + * on hierarchies, so these whiteouts would be shadowed on the + * system hierarchy but not for a union using the filesystem of + * their directories as the upper layer. Additionally, unionfs + * currently transparently exposes union-specific metadata of + * its upper layer, meaning that whiteouts can be seen through + * the union view in empty directories. Taking into account + * these whiteouts would then prevent mounting another + * filesystem on such effectively empty directories. + */ + if (dp->d_type == DT_WHT) + continue; + + /* + * Any file in the directory which is not '.' or '..' indicates + * the directory is not empty. + */ + switch (dp->d_namlen) { + case 2: + if (dp->d_name[1] != '.') { + /* Can't be '..' (nor '.') */ error = ENOTEMPTY; - break; + goto end; } - if (dp->d_namlen == 2 && - dp->d_name[1] != '.') { + /* FALLTHROUGH */ + case 1: + if (dp->d_name[0] != '.') { + /* Can't be '..' nor '.' */ error = ENOTEMPTY; - break; + goto end; } - uio.uio_resid = sizeof(struct dirent); + break; + + default: + error = ENOTEMPTY; + goto end; } } - free(dirent, M_TEMP); + +end: + free(dirbuf, M_TEMP); return (error); }