From nobody Tue Jun 11 00:42:05 2024 X-Original-To: dev-commits-src-main@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 4Vyqcs3wzGz5NG8K; Tue, 11 Jun 2024 00:42:05 +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 4Vyqcs3LTdz4gvM; Tue, 11 Jun 2024 00:42:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1718066525; 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=5XWFV3eTLygesr5Ef65UtKkqdJertJNz8LE9gLwulYg=; b=CoTSL3XCuvgLtlHfL53/2o3mfYqEQ3SWmHfBUeeRWfMj+MZk0rcArKX70+fY/fAIX0RduH wxfoMd7l9agBPHN3AAbEBlHhz4SBvbEKOEC0riJ7WXzaY5TT6j3U4C8WKvz7yZDdoIlzEm MmUTOlZr4c4rP79HTiEDq9MYeFMd0OW7SGK//OG4tc22tKyYmUai2RAZMWJWTsHmxNJsZc WUo17mcDGy2r1X+0t6khTYd6IccLZIER+0cFYgo7tJnLj4F0RLJeduSEUL82lgz9uNqp8s 3M4FtlWgNUzIleXp2Mf8K7rQ1eCIFu7O4A2LuTfjN0mA6xaeHGGVQjneOfZivg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1718066525; a=rsa-sha256; cv=none; b=SjT1ObU8U2k2S7ImfiqsBiYCGHJa6wafpbWTNCfnUZXN7CFQ3eBLRHZTTEMx7L99p0zopC 4ilSgBUoUisLgJ/qlvXrILCxJdwtAvbpCJ2IrVmQIFi+qDxlWzip+AI+tW2kDIPGRkkUtU 8TnAzstHhAkY/1gUlrAHEeBGA9mczNr97IIY188EAf2sWjj/vIAgg0ekCUldubkbRwl4B6 jJidfdRb8RH8UHLZpar2OiTMoiuJb7aEwBZUXdZhvuUhEVJP3hoo02YZnvf9XOrQSwWpjB jIp4f13aPbf29n5DDt4nTDns+BFHfKim/Qcx0k/eFfHkjXcnsEQTGi7QDnCazg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1718066525; 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=5XWFV3eTLygesr5Ef65UtKkqdJertJNz8LE9gLwulYg=; b=rbgJehzDbECDv7+MhHHV1/OBXfgEHC6ruGiV7pQGV6CEnZY5bwoXAakqz+n5gTbjs4+Cy1 AlyMOGmCfpi0pxyJAB011bd7TzZMzPUYR802GUi5tpqDJ1qn2tHLGKO8f4wraKP1/qudUe TIv6A0D7yxfPE6RGbN8SNGilo/YCnP+ZRV+fN9H3YVjdW9Ftlpp8hkut/gj6ClSjCvLLUQ gvHJM8mhPwI6cYkzOOllt34WfDBzxm0pjT+zi3XF8PTsW2g0EiZP6i0XWXolX1wnyIiyj9 Jm+USlvKIMX4ZAvMr10Za6BwBPcw5MAotTZYOh5Hh3zxm+3OhzrV6NavzNCS+Q== 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 4Vyqcs2xs8znhp; Tue, 11 Jun 2024 00:42:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 45B0g5gp082655; Tue, 11 Jun 2024 00:42:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 45B0g5b2082652; Tue, 11 Jun 2024 00:42:05 GMT (envelope-from git) Date: Tue, 11 Jun 2024 00:42:05 GMT Message-Id: <202406110042.45B0g5b2082652@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ryan Libby Subject: git: a2fda816eb05 - main - virstor: write large maps in chunks during label List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rlibby X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a2fda816eb054d5873be223ef2461741dfcc253c Auto-Submitted: auto-generated The branch main has been updated by rlibby: URL: https://cgit.FreeBSD.org/src/commit/?id=a2fda816eb054d5873be223ef2461741dfcc253c commit a2fda816eb054d5873be223ef2461741dfcc253c Author: Ryan Libby AuthorDate: 2024-06-11 00:36:20 +0000 Commit: Ryan Libby CommitDate: 2024-06-11 00:36:20 +0000 virstor: write large maps in chunks during label During the initial label of a virstor device, write out the allocation map in chunks if it is large (> 1 MB) in order to avoid large mallocs. Even though the kernel virstor geom may still do a large malloc to represent the allocation map, this may still be useful to avoid a ulimit. Reviewed by: markj Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D45517 --- lib/geom/virstor/geom_virstor.c | 59 ++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/geom/virstor/geom_virstor.c b/lib/geom/virstor/geom_virstor.c index 131ece0107c7..6a7dfb27fe43 100644 --- a/lib/geom/virstor/geom_virstor.c +++ b/lib/geom/virstor/geom_virstor.c @@ -157,8 +157,7 @@ virstor_label(struct gctl_req *req) char param[32]; int hardcode, nargs, error; struct virstor_map_entry *map; - size_t total_chunks; /* We'll run out of memory if - this needs to be bigger. */ + size_t total_chunks, write_max_map_entries; unsigned int map_chunks; /* Chunks needed by the map (map size). */ size_t map_size; /* In bytes. */ ssize_t written; @@ -325,28 +324,56 @@ virstor_label(struct gctl_req *req) sprintf(param, "%s%s", _PATH_DEV, name); fd = open(param, O_RDWR); } - if (fd < 0) + if (fd < 0) { gctl_error(req, "Cannot open provider %s to write map", name); + return; + } - /* Do it with calloc because there might be a need to set up chunk flags - * in the future */ - map = calloc(total_chunks, sizeof(*map)); + /* + * Initialize and write the map. Don't malloc the whole map at once, + * in case it's large. Use calloc because there might be a need to set + * up chunk flags in the future. + */ + write_max_map_entries = 1024 * 1024 / sizeof(*map); + if (write_max_map_entries > total_chunks) + write_max_map_entries = total_chunks; + map = calloc(write_max_map_entries, sizeof(*map)); if (map == NULL) { gctl_error(req, "Out of memory (need %zu bytes for allocation map)", - map_size); + write_max_map_entries * sizeof(*map)); + close(fd); + return; } - - written = pwrite(fd, map, map_size, 0); - free(map); - if ((size_t)written != map_size) { - if (verbose) { - fprintf(stderr, "\nTried to write %zu, written %zd (%s)\n", - map_size, written, strerror(errno)); + for (size_t chunk = 0; chunk < total_chunks; + chunk += write_max_map_entries) { + size_t bytes_to_write, entries_to_write; + + entries_to_write = total_chunks - chunk; + if (entries_to_write > write_max_map_entries) + entries_to_write = write_max_map_entries; + bytes_to_write = entries_to_write * sizeof(*map); + for (size_t off = 0; off < bytes_to_write; off += written) { + written = write(fd, ((char *)map) + off, + bytes_to_write - off); + if (written < 0) { + if (verbose) { + fprintf(stderr, + "\nError writing map at offset " + "%zu of %zu: %s\n", + chunk * sizeof(*map) + off, + map_size, strerror(errno)); + } + gctl_error(req, + "Error writing out allocation map!"); + free(map); + close(fd); + return; + } } - gctl_error(req, "Error writing out allocation map!"); - return; } + free(map); + map = NULL; close (fd); if (verbose)