Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3426519ybf; Tue, 3 Mar 2020 05:54:46 -0800 (PST) X-Google-Smtp-Source: ADFU+vuBA1T/m/+P49Xj9s1F6tW8KIKO9T91Bo6lVvz1hclaVjVvGT9R1mgzGf1PbQy/MsRnHZXT X-Received: by 2002:aca:7283:: with SMTP id p125mr2580671oic.1.1583243685968; Tue, 03 Mar 2020 05:54:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583243685; cv=none; d=google.com; s=arc-20160816; b=CWuc7bAdxxqhLfh1etxGPHP8JAgfm9ADUkwzbag+YTUkj2abdEP4/A+CJyjrNfKj7f 1k4Q4lm4fYGSqyNnkW9PATyXvRwcRdhHGucOev3JIC3/L4FwHJZY8R4xARz9xaXyOcdg Al6qgRKKzdBxgA6QE92VwR5WmbU+iR5ZbJaXaGXlPpIdZW0YiQ4gYzQmlpllrIxFsWNP igTKeq+XbYLx3DzXgrDVGOxoCcJhYHNCtRSU+cVPoxbg1Pq5IWxSa+96MZIENdArVl2X v6FIrj9jQN7hpD2QOs7T1mMIgsrUPCieZsPHdDmY6wqYu6zIsX5EqQpATtrcvEr//5Wd wTvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ou45CDmoGgxQtOQjQhYGAj4mIV8n4ICScH2/4oWCOoM=; b=Mql1dT3X1ojX7nezMDO7dnIdaRYE1ZW3h8Y4FJIlpqZb0yGrexhw0KuTzhguQOmvAL VChTQqolnu7SvR2+0rhxzIUHg8z3VEdh65PzomTyZnVeNBxMSCFvb6cJs0H9YqOAfWWB OaGIWO5njA7gXuazaVh1o6CVAt7NihXXM4ydnV96FQfn3yO98fLRL7yYDosjcP+WV7zm mXewM6W+IlA11jOdmteBrjZeMrOwOp9KFB334A5skKBZjcxfyHPpzzcn9LjJU4TuNHsJ rujqPwSxOQyt8x1nmRf6YuzwLB7s1tnGGbGDMruW4LRAadGL6ejMyLaVOdTu/+8Zln7G ZG3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="AinVYJM/"; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r6si8089139otp.220.2020.03.03.05.54.28; Tue, 03 Mar 2020 05:54:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="AinVYJM/"; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727862AbgCCNx5 (ORCPT + 99 others); Tue, 3 Mar 2020 08:53:57 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:44251 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727585AbgCCNx5 (ORCPT ); Tue, 3 Mar 2020 08:53:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583243636; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ou45CDmoGgxQtOQjQhYGAj4mIV8n4ICScH2/4oWCOoM=; b=AinVYJM/FHHUx6ZRJb76aqlmKMuDuEDKGx1csvsbW2dZk0CIU1Lq6LDc+u2H8RrOCGgUql UdJcCLpgpB24sz2mOoTNxRpB/i6pAkDC6nKXqLd1lcVygNVYVmHUnbpKDWXRjyvhF2kOXY 2LC8UHYASXoUDtB+dY3kyGNXtvseCxk= 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-295-fWPXsB2lMbyekBSw3drjMA-1; Tue, 03 Mar 2020 08:53:54 -0500 X-MC-Unique: fWPXsB2lMbyekBSw3drjMA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7E5FC13E4; Tue, 3 Mar 2020 13:53:53 +0000 (UTC) Received: from localhost.localdomain (ovpn-205-84.brq.redhat.com [10.40.205.84]) by smtp.corp.redhat.com (Postfix) with ESMTP id A2BFD5C1D8; Tue, 3 Mar 2020 13:53:49 +0000 (UTC) From: Lukas Czerner To: linux-ext4@vger.kernel.org Cc: tytso@mit.edu, Zdenek Kabelac , Karel Zak , Carlos Maiolino Subject: [PATCH v2] libext2fs/ismounted.c: check open(O_EXCL) before mntent file Date: Tue, 3 Mar 2020 14:53:48 +0100 Message-Id: <20200303135348.20827-1-lczerner@redhat.com> In-Reply-To: <20200225143445.13182-1-lczerner@redhat.com> References: <20200225143445.13182-1-lczerner@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Content-Transfer-Encoding: quoted-printable Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Currently the ext2fs_check_mount_point() will use the open(O_EXCL) check on linux after all the other checks are done. However it is not necessary to check mntent if open(O_EXCL) succeeds because it means that the device is not mounted. Moreover the commit ea4d53b7 introduced a regression where a following set of commands fails: vgcreate mygroup /dev/sda lvcreate -L 1G -n lvol0 mygroup mkfs.ext4 /dev/mygroup/lvol0 mount /dev/mygroup/lvol0 /mnt lvrename /dev/mygroup/lvol0 /dev/mygroup/lvol1 lvcreate -L 1G -n lvol0 mygroup mkfs.ext4 /dev/mygroup/lvol0 <<<--- This fails It fails because it thinks that /dev/mygroup/lvol0 is mounted because the device name in /proc/mounts is not updated following the lvrename. Move the open(O_EXCL) check before the mntent check and return immediatelly if the device is not busy. Fixes: ea4d53b7 ("libext2fs/ismounted.c: check device id in advance to sk= ip false device names") Signed-off-by: Lukas Czerner Reported-by: Zdenek Kabelac Reported-by: Karel Zak Reviewed-by: Carlos Maiolino --- lib/ext2fs/ismounted.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/ext2fs/ismounted.c b/lib/ext2fs/ismounted.c index c0215692..46d330d9 100644 --- a/lib/ext2fs/ismounted.c +++ b/lib/ext2fs/ismounted.c @@ -352,6 +352,7 @@ errcode_t ext2fs_check_mount_point(const char *device= , int *mount_flags, char *mtpt, int mtlen) { errcode_t retval =3D 0; + int busy =3D 0; =20 if (getenv("EXT2FS_PRETEND_RO_MOUNT")) { *mount_flags =3D EXT2_MF_MOUNTED | EXT2_MF_READONLY; @@ -366,6 +367,30 @@ errcode_t ext2fs_check_mount_point(const char *devic= e, int *mount_flags, return 0; } =20 +#ifdef __linux__ /* This only works on Linux 2.6+ systems */ + { + struct stat st_buf; + + if (stat(device, &st_buf) =3D=3D 0 && + ext2fsP_is_disk_device(st_buf.st_mode)) { + int fd =3D open(device, O_RDONLY | O_EXCL); + + if (fd >=3D 0) { + /* + * The device is not busy so it's + * definitelly not mounted. No need to + * to perform any more checks. + */ + close(fd); + *mount_flags =3D 0; + return 0; + } else if (errno =3D=3D EBUSY) { + busy =3D 1; + } + } + } +#endif + if (is_swap_device(device)) { *mount_flags =3D EXT2_MF_MOUNTED | EXT2_MF_SWAP; strncpy(mtpt, "", mtlen); @@ -386,21 +411,8 @@ errcode_t ext2fs_check_mount_point(const char *devic= e, int *mount_flags, if (retval) return retval; =20 -#ifdef __linux__ /* This only works on Linux 2.6+ systems */ - { - struct stat st_buf; - - if (stat(device, &st_buf) =3D=3D 0 && - ext2fsP_is_disk_device(st_buf.st_mode)) { - int fd =3D open(device, O_RDONLY | O_EXCL); - - if (fd >=3D 0) - close(fd); - else if (errno =3D=3D EBUSY) - *mount_flags |=3D EXT2_MF_BUSY; - } - } -#endif + if (busy) + *mount_flags |=3D EXT2_MF_BUSY; =20 return 0; } --=20 2.21.1