From nobody Thu Feb 2 14:46:41 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 4P71ns2Rx6z3fK13; Thu, 2 Feb 2023 14:46:41 +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 4P71ns1cMhz4l1M; Thu, 2 Feb 2023 14:46:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675349201; 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=jDcPxhk6rvqWw09tEcmCA0MJgrZUWdeFutvgjIrhRYI=; b=hqAGGMZEwpTUVA83moGHNbzB43kUhvvU1SFCPaUn1O2BnJ2kaMNPt4bQmQl0YlzwjsKUUY yeAt6jA3qgfRMs1XNRfhpMgClIbDxwqL4lADSSux3CmHsNAoixZaFtInoipj01oBzvF7SO T3dSuBWibeR4bejTpjOwyHue3l4AZrm8UAadFkd4WZ/txO0YlzAIJcjk7c0W8rnp7pNLZ4 kRKuoWT8AMqkra3SJEVTl4Cx6haG0QGieDYoAv5jFNLrLPoufhU1TbLkw+Q2BmWtrt8cf7 GULBArIW0r4ULpVakPtguiF4EOvxqtGNRiSJZieiY9vyz4pLhGxeHuA+Ib7ReQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675349201; 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=jDcPxhk6rvqWw09tEcmCA0MJgrZUWdeFutvgjIrhRYI=; b=vZjnBs3xlA/qy7dyrohwD5DaCWn+3g6SWMbFZB13a5IJXYSm0OM16EOjsvc12keQRSikX7 KUHJ4Z/Pd8O0mIZdzf0pM8ntC7dfi4iqfDAOVTmZpZ5VXD6kN/y0uFwdu8Dy+2PxlEIY11 pah0bkXaQSFT5ZbLMBLcjVA+E8fLpj099x8ZKeMVDaTk0LApMzYwGgjGDWkYBxGa+2UsOp ZyyveCBwdGmQUoL6Fz3upTPCfcLb8K7hTPJ/CAsRfBGJruk7gbjpOry+9y0iGZeqS34NGP CbbVDrG5otIdw6Cnqiv3r1pr5vq1YhiR3bIbwvvYNnq0tBlQ82GSHiKjUZP/YQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1675349201; a=rsa-sha256; cv=none; b=MU2z7yji3N59E3eiKig5dexGXzWBtd61p2UHTrjKV9BUHHNnJr7K6YhoYBvMVfuDbZapHE zjNQd00T3PtGas8afjzx0KNbzNfx+CYKXV6wwzi7BX+/8naCtnMAPR5d3w1DqiUhFmK4/d 10Bask+2zb87WqDDsaq3AgCwNvMPRbQoRAbYZhw8CDTI8A9lrC4PxpOhINhMO8Pi4OqRSI PwpdNxZ7qGOiTPA71eMX6TtYSFlexr5eZdG5dlVOzHXw8MuhDqEGgrV+j0MzrD/HZC6pmT M/hf/aEGDhkmIhIpM9PH1pfoIRwodAABw8vu2dOcqYmz3+IhTl5CC95bGc/bMw== 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 4P71ns0fYZzy4n; Thu, 2 Feb 2023 14:46:41 +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 312Ekfpn077241; Thu, 2 Feb 2023 14:46:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 312EkfiB077240; Thu, 2 Feb 2023 14:46:41 GMT (envelope-from git) Date: Thu, 2 Feb 2023 14:46:41 GMT Message-Id: <202302021446.312EkfiB077240@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Dag-Erling=20Sm=C3=B8rgrav?= Subject: git: 6c85042afcbb - main - cp: Simplify the common case. 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: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6c85042afcbbf4cd0fb7e7c03226c7249081e690 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=6c85042afcbbf4cd0fb7e7c03226c7249081e690 commit 6c85042afcbbf4cd0fb7e7c03226c7249081e690 Author: Dag-Erling Smørgrav AuthorDate: 2023-02-01 20:06:28 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-02-02 14:46:04 +0000 cp: Simplify the common case. * The allocated buffer is only used in the fallback case, so move it there. The argument for passing it in from the caller was that if malloc(3) were to fail, we'd want it to fail before we started copying anything, but firstly, it was already not in the right place to ensure that, and secondly, malloc(3) never fails (except in very contrived circumstances, such as an unreasonable RLIMIT_AS or RLIMIT_DATA). * Remove the mmap(2) option. It is almost never beneficial, especially when the alternative is copy_file_range(2), and it adds needless complexity and indentation. MFC after: 1 week Sponsored by: Klara, Inc. Reviewed by: rmacklem, mav Differential Revision: https://reviews.freebsd.org/D38291 --- bin/cp/Makefile | 2 +- bin/cp/utils.c | 122 +++++++++++++++----------------------------------------- 2 files changed, 34 insertions(+), 90 deletions(-) diff --git a/bin/cp/Makefile b/bin/cp/Makefile index 0c4c7b5eff53..a73ee5447769 100644 --- a/bin/cp/Makefile +++ b/bin/cp/Makefile @@ -6,7 +6,7 @@ PACKAGE=runtime PROG= cp SRCS= cp.c utils.c -CFLAGS+= -DVM_AND_BUFFER_CACHE_SYNCHRONIZED -D_ACL_PRIVATE +CFLAGS+= -D_ACL_PRIVATE HAS_TESTS= SUBDIR.${MK_TESTS}= tests diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 07de0495ba9e..a3a498714f43 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -41,9 +41,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED -#include -#endif #include #include @@ -75,11 +72,22 @@ __FBSDID("$FreeBSD$"); #define BUFSIZE_SMALL (MAXPHYS) static ssize_t -copy_fallback(int from_fd, int to_fd, char *buf, size_t bufsize) +copy_fallback(int from_fd, int to_fd) { + static char *buf = NULL; + static size_t bufsize; ssize_t rcount, wresid, wcount = 0; char *bufp; + if (buf == NULL) { + if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD) + bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8); + else + bufsize = BUFSIZE_SMALL; + buf = malloc(bufsize); + if (buf == NULL) + err(1, "Not enough memory"); + } rcount = read(from_fd, buf, bufsize); if (rcount <= 0) return (rcount); @@ -96,16 +104,10 @@ copy_fallback(int from_fd, int to_fd, char *buf, size_t bufsize) int copy_file(const FTSENT *entp, int dne) { - static char *buf = NULL; - static size_t bufsize; struct stat *fs; ssize_t wcount; off_t wtotal; int ch, checkch, from_fd, rval, to_fd; -#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED - size_t wresid; - char *bufp, *p; -#endif int use_copy_file_range = 1; from_fd = to_fd = -1; @@ -174,89 +176,31 @@ copy_file(const FTSENT *entp, int dne) rval = 0; if (!lflag && !sflag) { - /* - * Mmap and write if less than 8M (the limit is so we don't - * totally trash memory on big files. This is really a minor - * hack, but it wins some CPU back. - * Some filesystems, such as smbnetfs, don't support mmap, - * so this is a best-effort attempt. - */ -#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED - if (S_ISREG(fs->st_mode) && fs->st_size > 0 && - fs->st_size <= 8 * 1024 * 1024 && - (p = mmap(NULL, (size_t)fs->st_size, PROT_READ, - MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) { - wtotal = 0; - for (bufp = p, wresid = fs->st_size; ; - bufp += wcount, wresid -= (size_t)wcount) { - wcount = write(to_fd, bufp, wresid); - if (wcount <= 0) - break; - wtotal += wcount; - if (info) { - info = 0; - (void)fprintf(stderr, - "%s -> %s %3d%%\n", - entp->fts_path, to.p_path, - cp_pct(wtotal, fs->st_size)); + wtotal = 0; + do { + if (use_copy_file_range) { + wcount = copy_file_range(from_fd, NULL, + to_fd, NULL, SSIZE_MAX, 0); + if (wcount < 0 && errno == EINVAL) { + /* Prob a non-seekable FD */ + use_copy_file_range = 0; } - if (wcount >= (ssize_t)wresid) - break; } - if (wcount != (ssize_t)wresid) { - warn("%s", to.p_path); - rval = 1; + if (!use_copy_file_range) { + wcount = copy_fallback(from_fd, to_fd); } - /* Some systems don't unmap on close(2). */ - if (munmap(p, fs->st_size) < 0) { - warn("%s", entp->fts_path); - rval = 1; - } - } else -#endif - { - if (buf == NULL) { - /* - * Note that buf and bufsize are static. If - * malloc() fails, it will fail at the start - * and not copy only some files. - */ - if (sysconf(_SC_PHYS_PAGES) > - PHYSPAGES_THRESHOLD) - bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8); - else - bufsize = BUFSIZE_SMALL; - buf = malloc(bufsize); - if (buf == NULL) - err(1, "Not enough memory"); - } - wtotal = 0; - do { - if (use_copy_file_range) { - wcount = copy_file_range(from_fd, NULL, - to_fd, NULL, SSIZE_MAX, 0); - if (wcount < 0 && errno == EINVAL) { - /* Prob a non-seekable FD */ - use_copy_file_range = 0; - } - } - if (!use_copy_file_range) { - wcount = copy_fallback(from_fd, to_fd, - buf, bufsize); - } - wtotal += wcount; - if (info) { - info = 0; - (void)fprintf(stderr, - "%s -> %s %3d%%\n", - entp->fts_path, to.p_path, - cp_pct(wtotal, fs->st_size)); - } - } while (wcount > 0); - if (wcount < 0) { - warn("%s", entp->fts_path); - rval = 1; + wtotal += wcount; + if (info) { + info = 0; + (void)fprintf(stderr, + "%s -> %s %3d%%\n", + entp->fts_path, to.p_path, + cp_pct(wtotal, fs->st_size)); } + } while (wcount > 0); + if (wcount < 0) { + warn("%s", entp->fts_path); + rval = 1; } } else if (lflag) { if (link(entp->fts_path, to.p_path)) {