From patchwork Wed Jun 17 18:29:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 1311389 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20170209 header.b=Ayi6+6g2; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=nrdgJrvY; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (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) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49nDC771kcz9sR4 for ; Thu, 18 Jun 2020 04:30:27 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:Mime-Version:Date:To:From:Message-ID:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=HeJZpV0HcuOXJWvyLvuNaNvEeCWY/JGYDtSck7O6f7o=; b=Ayi 6+6g2RPJZpILH+Wi+OCtRMJ48rd44UbT8mBxJ6sf2P7/l7ugnXWqbRLxdtBSxV2XFtG0bC2PInk22 xg8gDiX86arb3goa77CAbKDByVswork4yFuDHbb3znom1HS1/wVTmpWpiBmTu0tpQuuqrL/AA2Xal AhbNN0IjhxQe4AWm9A2GCWPwpPz72gN/K4UE34wE1Trw2KGpAzZk9kMCR5j4L8ALdnpPP8R0GipQn YzQJOeeDr1hLKpGDbVfWcvKB6yiYOuxJKe+rLgsIG/aAtccMng/doBhE+MyKRnSSGoRZOlpBqTxuq qvUVoLx+o3XimNbXpHP+dboWD2vId4w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jlcoy-0002Bf-T5; Wed, 17 Jun 2020 18:30:00 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jlcox-0002BZ-L7 for openwrt-devel@bombadil.infradead.org; Wed, 17 Jun 2020 18:29:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Mime-Version:Content-Type:Date:To:From: Subject:Message-ID:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=CCrEQHamx7Z7YxAqErg61fXrFtjPJDFhy5CO/IU1ppo=; b=nrdgJrvYf5H6zxKsRV27vQresY hg+AIXfagWNwdAZnb32EDYAlJJxf6aS9IuSoZGMr+r15MN1wyjkPESot1m6jd+VrcepEozX1HH4Qo CuF578ZtnaGMeD7BO3v2ynUprgjTqiRP8AbmsagwBIOMGEvfiF2939ynS6qyXUmjglmyFMvaxkjER D2hRa/sFbZWVTSqwkRgbCCKUc0kin7l5tXUI3jyulHfk9sL1PZSmb7bAmBIvRG+P1enUWE8ufCJUF YHIx8ttzak0GVE8o9xQ29sidA2VlK4x18olpMsaRrJdvAu4UKLbBkmfO7J3uim7ONH7kv18x10HzI s0XBRf6Q==; Received: from 54-240-197-232.amazon.com ([54.240.197.232] helo=u3832b3a9db3152.ant.amazon.com) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jlcov-00045C-T0 for openwrt-devel@lists.openwrt.org; Wed, 17 Jun 2020 18:29:58 +0000 Message-ID: <7fa3102e5272a9bf2b3453a24f1fd741b59b59c0.camel@infradead.org> From: David Woodhouse To: openwrt-devel Date: Wed, 17 Jun 2020 19:29:56 +0100 X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.2 Mime-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by merlin.infradead.org. See http://www.infradead.org/rpr.html Subject: [OpenWrt-Devel] [PATCH][fstools] Use autoclear for overlay loopback device X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org During a sysupgrade on a block-based device, the partition table might get updated. The partitions have to be completely unused by the time partx is invoked, or it fails thus: partx: /dev/mmcblk1: error deleting partition 3 partx: /dev/mmcblk1: error adding partition 3 That's cosmetic in some cases, but in others where the old root partition overlaps with the new partition where the config is stored during the reboot, it causes a sysugprade failure (resulting in the backup being lost and a completely clean system image). Although we carefully unmount the root and overlay file systems, the problem is that the loopback device used for the overlay isn't being torn down, and it still has a refcount on the root block partition (in the above case, /dev/mmcblk1p3). Installing losetup and adding 'losetup -D' to the switch_to_ramfs() function makes it work nicely. But the better option that doesn't add a new dependency is to use the autoclear flag when setting up the loop device, so it goes away automatically when the overlay file system is unmounted. To make that work sanely, we have to *not* close the fd right after configuring it — or it'll go away immediately. We could store the fd in the volume struct and either add destructor method or close it after performing the mount… but honestly it just seems simpler and saner to "leak" the fd in the knowledge that it'll get closed when the process exits in a few milliseconds anyway. We can revisit that if anyone really feels strongly about it. Dissent is best expressed in 'diff -up' form. Signed-off-by: David Woodhouse diff --git a/libfstools/rootdisk.c b/libfstools/rootdisk.c index 5a6dcb9..dc861a9 100644 --- a/libfstools/rootdisk.c +++ b/libfstools/rootdisk.c @@ -231,12 +231,19 @@ static int rootdisk_create_loop(struct rootdev_volume *p) snprintf((char *) info.lo_file_name, sizeof(info.lo_file_name), "%s", rootdev); info.lo_offset = p->offset; + info.lo_flags |= LO_FLAGS_AUTOCLEAR; if (ioctl(fd, LOOP_SET_STATUS64, &info) != 0) { ioctl(fd, LOOP_CLR_FD, 0); continue; } + /* + * Don't close fd. Leave it open until this process exits, to avoid + * the autoclear from happening too soon. + */ + fd = -1; + ret = 0; break; }