From owner-svn-src-all@freebsd.org Mon Apr 8 17:36:26 2019 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E31B21562DA1; Mon, 8 Apr 2019 17:36:25 +0000 (UTC) (envelope-from kevans@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 8397082FF3; Mon, 8 Apr 2019 17:36:25 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BF616269E6; Mon, 8 Apr 2019 17:36:24 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x38HaOq6027099; Mon, 8 Apr 2019 17:36:24 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x38HaORe027098; Mon, 8 Apr 2019 17:36:24 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201904081736.x38HaORe027098@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Mon, 8 Apr 2019 17:36:24 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r346033 - stable/12/lib/libbe X-SVN-Group: stable-12 X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: stable/12/lib/libbe X-SVN-Commit-Revision: 346033 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 8397082FF3 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.97 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_SHORT(-0.98)[-0.976,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_LONG(-1.00)[-1.000,0] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Apr 2019 17:36:26 -0000 Author: kevans Date: Mon Apr 8 17:36:24 2019 New Revision: 346033 URL: https://svnweb.freebsd.org/changeset/base/346033 Log: MFC r345848: libbe(3): Add a serial to the generated snapshot names To use bectl in an example, when one creates a new boot environment with either `bectl create ` or `bectl create -e `, libbe will take a snapshot of the original boot environment to clone. Previously, this used %F-%T date format as the snapshot name, but this has some limitations- attempting to create multiple boot environments in quick succession may collide if done within the same second. Tack a serial onto it to reduce the chances of a collision... we could still collide if multiple processes/threads are creating boot environments at the same time, but this is likely not a big concern as this has only been reported as occurring in freebsd-ci setup. Modified: stable/12/lib/libbe/be.c Directory Properties: stable/12/ (props changed) Modified: stable/12/lib/libbe/be.c ============================================================================== --- stable/12/lib/libbe/be.c Mon Apr 8 15:52:13 2019 (r346032) +++ stable/12/lib/libbe/be.c Mon Apr 8 17:36:24 2019 (r346033) @@ -51,6 +51,9 @@ static int be_create_child_noent(libbe_handle_t *lbh, static int be_create_child_cloned(libbe_handle_t *lbh, const char *active); #endif +/* Arbitrary... should tune */ +#define BE_SNAP_SERIAL_MAX 1024 + /* * Iterator function for locating the rootfs amongst the children of the * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *. @@ -252,13 +255,32 @@ be_destroy(libbe_handle_t *lbh, const char *name, int } +static void +be_setup_snapshot_name(libbe_handle_t *lbh, char *buf, size_t buflen) +{ + time_t rawtime; + int len, serial; + + time(&rawtime); + len = strlen(buf); + len += strftime(buf + len, buflen - len, "@%F-%T", localtime(&rawtime)); + /* No room for serial... caller will do its best */ + if (buflen - len < 2) + return; + + for (serial = 0; serial < BE_SNAP_SERIAL_MAX; ++serial) { + snprintf(buf + len, buflen - len, "-%d", serial); + if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) + return; + } +} + int be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name, bool recursive, char *result) { char buf[BE_MAXPATHLEN]; - time_t rawtime; - int len, err; + int err; be_root_concat(lbh, source, buf); @@ -276,10 +298,8 @@ be_snapshot(libbe_handle_t *lbh, const char *source, c snprintf(result, BE_MAXPATHLEN, "%s@%s", source, snap_name); } else { - time(&rawtime); - len = strlen(buf); - strftime(buf + len, sizeof(buf) - len, - "@%F-%T", localtime(&rawtime)); + be_setup_snapshot_name(lbh, buf, sizeof(buf)); + if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1, sizeof(buf)) >= sizeof(buf)) return (set_error(lbh, BE_ERR_INVALIDNAME));