From nobody Fri May 05 06:38:57 2023 X-Original-To: dev-commits-src-branches@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 4QCLcd6J3kz49qL2; Fri, 5 May 2023 06:38:57 +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 4QCLcd5hk6z47BK; Fri, 5 May 2023 06:38:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1683268737; 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=fIyK6vrBXrfifZKNrOI0uLEL2UXyKhje5jyPfejge3U=; b=eX1APAE2CUQq3It59qsY0fUik9x49SkLJSAVcWezpn8ZloOE8wfzPhWE1XtT6wlHHlaUbT 0RvbvkLkliJyPG3QeUnza+5kf/Uxo+L8jbywKVrh35KM43bYNRTnd4YToVyjTrRQVVha0S DsZvUw9m86/lFyYq/5rAz8xfwJOcNjEt9s8X62Hvt4AlX6cPMrBaxLfMiNgBu+aT+jKNhi fI6OpPo8w3FLmWD32vlgpVKWa+GSvnjQTfVoploipyRnyTejPQt7S/hQUhWy5Rk0feXTSk JHLtdZKbeobLcmWu93ucCvSKj1ce8IP5qHmCjHO66oKvI2W+Pr6FakMT5EnwSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1683268737; 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=fIyK6vrBXrfifZKNrOI0uLEL2UXyKhje5jyPfejge3U=; b=iikOnIMhgzE3Keffo5gdLHNGE7cuzqbLPoulkYvpwNdL38AYv+LLT1CHy70Fl3rEba6E4h AARLq0VF6KhJW0tGAn7ouMDlSkuAU0VDbjEwHZ9UQOf4IeC3mWLpBCsN1fBWq4tWQcyHRi tTvSeCDVJjFWPnZh33ZurYktXPWrKwtf48IoIUuIWMAjE7grp5LvsynHavn9hoT/2O0KZ1 K7IGYbO4PVX5vdAkUNK1N7hfMAcSOJ72kQcB8xyt8wlbwKnB1x+vgdqGUulKwn4oWVYZkn y6zhae2xs9DG1QAgltXBUW1flIpbm8UlkmHpUQxYUFUjp2epLeflBQrZ/03dSw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1683268737; a=rsa-sha256; cv=none; b=Cl2HimlcE8ivKHxROsyIS3hP4+UDY0MdVPys2A1F+dyehMvf/J87o8628pw1VbkcZp8aaP PRMIM5QhupEdcAvX83u82jF2tNaSmHAxN/wItlITOc3+gaXHBXaQrWeYrD9peKsjZJb+wp RBv8dBL7N55cD6zCCVSqn6VjinmDYtV25Ey8d67C2cJ1QcBaORbt6laPHiobk+czTd5t2I LEIjlc6Aseqw5rI/esyVhcG+86lu/v25z6kEplIWYK07W1GuqtucGcV4OTkITuBb022sWo KEGcjMjx34nFDbNcgu7xqcrw7NSaUE9snZdVLbpSGVpqcUifHWO0s318lXE2/w== 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 4QCLcd4mmNzklF; Fri, 5 May 2023 06:38:57 +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 3456cvZN039515; Fri, 5 May 2023 06:38:57 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 3456cvNM039514; Fri, 5 May 2023 06:38:57 GMT (envelope-from git) Date: Fri, 5 May 2023 06:38:57 GMT Message-Id: <202305050638.3456cvNM039514@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 6a53a4ff6537 - stable/13 - vfs: Fix "emptydir" mount option List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 6a53a4ff653722488379cedf4326deabbfccd749 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=6a53a4ff653722488379cedf4326deabbfccd749 commit 6a53a4ff653722488379cedf4326deabbfccd749 Author: Olivier Certner AuthorDate: 2023-04-22 16:07:07 +0000 Commit: Konstantin Belousov CommitDate: 2023-05-05 06:20:58 +0000 vfs: Fix "emptydir" mount option PR: 270988 (cherry picked from commit 6450e7bbad0c68176f28b51773a3af5d6022c7dd) --- 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 ff5b4822316f..dbedc16f555b 100644 --- a/sys/kern/vfs_subr.c +++ b/sys/kern/vfs_subr.c @@ -6452,65 +6452,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"); VNASSERT(vp->v_type == VDIR, vp, ("vp is not a directory")); - 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); }