From nobody Sat Feb 7 08:59:10 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1595266424; cv=none; d=zohomail.com; s=zohoarc; b=PAaf3ZkESJwgDA75Z3ZXIwjtxLyPSctogzc2d2/Y5aQLrA2Chg1FyNu6yN1eYZe8bXaygaoogiShYEJzO4P99lGhpmmbZNCnNdLQu52oma9RLSLzLkTNbsgmT2BvpUXkyBPplcHbid11Jkxc8Jo61hF1FjlWZXoVcVtkIyUTDqM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595266424; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=efbkjVnkCO4s0Oekw9m8N1nJWWG1H3VDQc1dt63po/M=; b=Ovxzy8g/+lYJeWd5uexPfQCxtJ8Yjl4mY6JDf3UoE6bym2wezXApyFI5sVp+CBQMZjWti9IMSwaqT25UEm4VQ5m7nbds4mXXTA+hP/rUNg79e7Bn0ipy+hkMQQVIwJPePkbzw2iolAOMleqyJM6tVurT2tg04TVfqQEpKz88lZI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1595266424270778.1922089666065; Mon, 20 Jul 2020 10:33:44 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-400-NjkgrNykO0u_Z3WPnqs1dA-1; Mon, 20 Jul 2020 13:33:40 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0851210059AA; Mon, 20 Jul 2020 17:33:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DA3F960F8D; Mon, 20 Jul 2020 17:33:34 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id AA7681809563; Mon, 20 Jul 2020 17:33:34 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 06KHXSbN020443 for ; Mon, 20 Jul 2020 13:33:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6E7A02B6E2; Mon, 20 Jul 2020 17:33:28 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E56F19C66; Mon, 20 Jul 2020 17:33:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595266423; h=from:from:sender:sender: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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=efbkjVnkCO4s0Oekw9m8N1nJWWG1H3VDQc1dt63po/M=; b=BGOZU+Fe+5nfCzhvBBNzb16GZhso+2jumoMEmUWqEr3Pmhie/F/SOGVLq66NJh/vt+8a6u le8hszzvKUtyPW4oLI71B4VgaFqa4Oim5LRtlsP7ua245Wp8V1bTV+weUNvkx7p323TjhP BX5peH7rJ7yVVZsN3QCkDSB5XZIz4rI= X-MC-Unique: NjkgrNykO0u_Z3WPnqs1dA-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 1/4] util: add a helper method for controlling the COW flag on btrfs Date: Mon, 20 Jul 2020 18:33:19 +0100 Message-Id: <20200720173322.839717-2-berrange@redhat.com> In-Reply-To: <20200720173322.839717-1-berrange@redhat.com> References: <20200720173322.839717-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) btrfs defaults to performing copy-on-write for files. This is often undesirable for VM images, so we need to be able to control whether this behaviour is used. The virFileSetCOW() will allow for this. We use a tristate, since out of the box, we want the default behaviour attempt to disable cow, but only on btrfs, silently do nothing on non-btrfs. If someone explicitly asks to disable/enable cow, then we want to raise a hard error on non-btrfs. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/libvirt_private.syms | 1 + src/util/virfile.c | 76 ++++++++++++++++++++++++++++++++++++++++ src/util/virfile.h | 3 ++ 3 files changed, 80 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 73b72c9e10..eea31a736d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2125,6 +2125,7 @@ virFileRewrite; virFileRewriteStr; virFileSanitizePath; virFileSetACLs; +virFileSetCOW; virFileSetupDev; virFileSetXAttr; virFileTouch; diff --git a/src/util/virfile.c b/src/util/virfile.c index 213acdbcaa..5b169b3d11 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -71,6 +71,7 @@ # endif # include # include +# include #endif =20 #if HAVE_LIBATTR @@ -4504,3 +4505,78 @@ virFileDataSync(int fd) return fdatasync(fd); #endif } + + +int +virFileSetCOW(const char *path, + virTristateBool state) +{ +#if __linux__ + int val =3D 0; + struct statfs buf; + VIR_AUTOCLOSE fd =3D -1; + + VIR_DEBUG("Setting COW flag on '%s' to '%s'", + path, virTristateBoolTypeToString(state)); + + fd =3D open(path, O_RDONLY|O_NONBLOCK|O_LARGEFILE); + if (fd < 0) { + virReportSystemError(errno, _("unable to open '%s'"), + path); + return -1; + } + + if (fstatfs(fd, &buf) < 0) { + virReportSystemError(errno, _("unable query filesystem type on '%s= '"), + path); + return -1; + } + + if (buf.f_type !=3D BTRFS_SUPER_MAGIC) { + if (state =3D=3D VIR_TRISTATE_BOOL_ABSENT) { + virReportSystemError(ENOSYS, + _("unable to control COW flag on '%s', no= t btrfs"), + path); + return -1; + } + return 0; + } + + if (ioctl(fd, FS_IOC_GETFLAGS, &val) < 0) { + virReportSystemError(errno, _("unable get directory flags on '%s'"= ), + path); + return -1; + } + + VIR_DEBUG("Current flags on '%s' are 0x%x", path, val); + if (state =3D=3D VIR_TRISTATE_BOOL_YES) { + val &=3D ~FS_NOCOW_FL; + } else { + val |=3D FS_NOCOW_FL; + } + + VIR_DEBUG("New flags on '%s' will be 0x%x", path, val); + if (ioctl(fd, FS_IOC_SETFLAGS, &val) < 0) { + int saved_err =3D errno; + VIR_DEBUG("Failed to set flags on '%s': %s", path, g_strerror(save= d_err)); + if (state !=3D VIR_TRISTATE_BOOL_ABSENT) { + virReportSystemError(saved_err, + _("unable control COW flag on '%s'"), + path); + return -1; + } else { + VIR_DEBUG("Ignoring failure to set COW"); + } + } + + return 0; +#else /* ! __linux__ */ + if (state !=3D VIR_TRISTATE_BOOL_ABSENT) { + virReportSystemError(ENOSYS, + _("Unable to set copy-on-write state on '%s' = to '%s'"), + path, virTristateBoolTypeToString(state)); + return -1; + } + return 0; +#endif /* ! __linux__ */ +} diff --git a/src/util/virfile.h b/src/util/virfile.h index 7a92364a5c..cb0e586a7d 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -374,3 +374,6 @@ int virFileRemoveXAttr(const char *path, G_GNUC_NO_INLINE; =20 int virFileDataSync(int fd); + +int virFileSetCOW(const char *path, + virTristateBool state); --=20 2.26.2