From patchwork Thu Feb 28 05:08:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jianhong.Yin" X-Patchwork-Id: 10832529 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1A93D1399 for ; Thu, 28 Feb 2019 05:08:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0434C28A0E for ; Thu, 28 Feb 2019 05:08:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E7AAB288ED; Thu, 28 Feb 2019 05:08:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,FREEMAIL_FROM, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4481028623 for ; Thu, 28 Feb 2019 05:08:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725899AbfB1FIy (ORCPT ); Thu, 28 Feb 2019 00:08:54 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48426 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731103AbfB1FIy (ORCPT ); Thu, 28 Feb 2019 00:08:54 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C92AF30BDBF8; Thu, 28 Feb 2019 05:08:53 +0000 (UTC) Received: from dhcp-12-115.nay.redhat.com (dhcp-12-115.nay.redhat.com [10.66.12.115]) by smtp.corp.redhat.com (Postfix) with ESMTP id B7AB66BF9A; Thu, 28 Feb 2019 05:08:49 +0000 (UTC) From: "Jianhong.Yin" To: steved@redhat.com Cc: linux-nfs@vger.kernel.org, jiyin@redhat.com, nfs-team@redhat.com, "Jianhong.Yin" Subject: [PATCH] mount.nfs: fix ignore EBUSY problem in special scenario Date: Thu, 28 Feb 2019 13:08:42 +0800 Message-Id: <20190228050842.15827-1-yin-jianhong@163.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Thu, 28 Feb 2019 05:08:53 +0000 (UTC) Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP now mount.nfs ignore EBUSY when the $dir is a mountpoint, it's not correct in specail scenario for example: $dir is a mountpoint of other filesystem(let's say tmpfs) so we must be sure: current nfs mounting has mounted on $dir, then ignore the EBUSY for user. - if (errno == EBUSY && is_mountpoint(mi->node)) + if (errno == EBUSY && is_mountpoint(mi->node) && has_mounted_on(mi->spec, mi->node)) reproducer: ''' mkdir -p /expdir /mnt/nfsmp /mnt/tmpfs echo "/expdir *(rw,no_root_squash)" >/etc/exports service nfs restart mount -t tmpfs tmpfs /mnt/tmpfs mount -osharecache localhost:/expdir /mnt/nfsmp #success mount -osharecache localhost:/expdir /mnt/nfsmp #mounted already, return 0(pass) mount -osharecache localhost:/expdir /mnt/tmpfs #success mount -osharecache localhost:/expdir /mnt/tmpfs #mounted already, return 0(pass) umount /mnt/nfsmp umount /mnt/tmpfs mount -ocontext=system_u:object_r:xferlog_t:s0,sharecache localhost:/expdir /mnt/nfsmp mount -ocontext=system_u:object_r:user_home_dir_t:s0,sharecache localhost:/expdir /mnt/tmpfs [[ $? = 0 ]] && echo "***test fail: hasn't mounted, should return fail ***" umount /mnt/nfsmp ''' Signed-off-by: Jianhong Yin --- support/include/misc.h | 1 + support/misc/mountpoint.c | 26 +++++++++++++++++++++++++- utils/mount/stropts.c | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/support/include/misc.h b/support/include/misc.h index 06e2a0c..bd02bfc 100644 --- a/support/include/misc.h +++ b/support/include/misc.h @@ -19,6 +19,7 @@ char *generic_make_pathname(const char *, const char *); _Bool generic_setup_basedir(const char *, const char *, char *, const size_t); extern int is_mountpoint(char *path); +extern int has_mounted_on(const char *fsname, const char *dir); /* size of the file pointer buffers for rpc procfs files */ #define RPC_CHAN_BUF_SIZE 32768 diff --git a/support/misc/mountpoint.c b/support/misc/mountpoint.c index 9f9ce44..7f8a89f 100644 --- a/support/misc/mountpoint.c +++ b/support/misc/mountpoint.c @@ -3,9 +3,11 @@ * check if a given path is a mountpoint */ +#include #include -#include "xcommon.h" +#include #include +#include "xcommon.h" #include "misc.h" int @@ -38,3 +40,25 @@ is_mountpoint(char *path) free(dotdot); return rv; } + +int +has_mounted_on(const char *fsname, const char *dir) +{ + struct mntent *ent; + FILE *fp; + int ret = 0; + + fp = setmntent("/proc/mounts", "r"); + if (fp == NULL) { + perror("[unlikely] setmntent(3) fail"); + exit(1); + } + while (NULL != (ent = getmntent(fp))) { + if (!strcmp(ent->mnt_fsname, fsname) && !strcmp(ent->mnt_dir, dir)) { + ret = 1; + break; + } + } + endmntent(fp); + return ret; +} diff --git a/utils/mount/stropts.c b/utils/mount/stropts.c index a093926..bffb74a 100644 --- a/utils/mount/stropts.c +++ b/utils/mount/stropts.c @@ -1081,7 +1081,7 @@ static int nfsmount_fg(struct nfsmount_info *mi) return EX_SUCCESS; #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" - if (errno == EBUSY && is_mountpoint(mi->node)) { + if (errno == EBUSY && is_mountpoint(mi->node) && has_mounted_on(mi->spec, mi->node)) { #pragma GCC diagnostic warning "-Wdiscarded-qualifiers" /* * EBUSY can happen when mounting a filesystem that