From owner-svn-src-stable@freebsd.org Fri Aug 4 08:27:35 2017 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id BEACDDCD09C; Fri, 4 Aug 2017 08:27:35 +0000 (UTC) (envelope-from kib@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 mx1.freebsd.org (Postfix) with ESMTPS id 8BC7D83A91; Fri, 4 Aug 2017 08:27:35 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v748RYiN020450; Fri, 4 Aug 2017 08:27:34 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v748RYQV020449; Fri, 4 Aug 2017 08:27:34 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201708040827.v748RYQV020449@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Fri, 4 Aug 2017 08:27:34 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r322048 - stable/10/sys/ufs/ffs X-SVN-Group: stable-10 X-SVN-Commit-Author: kib X-SVN-Commit-Paths: stable/10/sys/ufs/ffs X-SVN-Commit-Revision: 322048 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Aug 2017 08:27:35 -0000 Author: kib Date: Fri Aug 4 08:27:34 2017 New Revision: 322048 URL: https://svnweb.freebsd.org/changeset/base/322048 Log: MFC r321349: Improve publication of the newly allocated snapdata. Modified: stable/10/sys/ufs/ffs/ffs_snapshot.c Directory Properties: stable/10/ (props changed) Modified: stable/10/sys/ufs/ffs/ffs_snapshot.c ============================================================================== --- stable/10/sys/ufs/ffs/ffs_snapshot.c Fri Aug 4 08:26:19 2017 (r322047) +++ stable/10/sys/ufs/ffs/ffs_snapshot.c Fri Aug 4 08:27:34 2017 (r322048) @@ -2643,8 +2643,8 @@ try_free_snapdata(struct vnode *devvp) static struct snapdata * ffs_snapdata_acquire(struct vnode *devvp) { - struct snapdata *nsn; - struct snapdata *sn; + struct snapdata *nsn, *sn; + int error; /* * Allocate a free snapdata. This is done before acquiring the @@ -2652,23 +2652,37 @@ ffs_snapdata_acquire(struct vnode *devvp) * held. */ nsn = ffs_snapdata_alloc(); - /* - * If there snapshots already exist on this filesystem grab a - * reference to the shared lock. Otherwise this is the first - * snapshot on this filesystem and we need to use our - * pre-allocated snapdata. - */ - VI_LOCK(devvp); - if (devvp->v_rdev->si_snapdata == NULL) { - devvp->v_rdev->si_snapdata = nsn; - nsn = NULL; + + for (;;) { + VI_LOCK(devvp); + sn = devvp->v_rdev->si_snapdata; + if (sn == NULL) { + /* + * This is the first snapshot on this + * filesystem and we use our pre-allocated + * snapdata. Publish sn with the sn_lock + * owned by us, to avoid the race. + */ + error = lockmgr(&nsn->sn_lock, LK_EXCLUSIVE | + LK_NOWAIT, NULL); + if (error != 0) + panic("leaked sn, lockmgr error %d", error); + sn = devvp->v_rdev->si_snapdata = nsn; + VI_UNLOCK(devvp); + nsn = NULL; + break; + } + + /* + * There is a snapshots which already exists on this + * filesystem, grab a reference to the common lock. + */ + error = lockmgr(&sn->sn_lock, LK_INTERLOCK | + LK_EXCLUSIVE | LK_SLEEPFAIL, VI_MTX(devvp)); + if (error == 0) + break; } - sn = devvp->v_rdev->si_snapdata; - /* - * Acquire the snapshot lock. - */ - lockmgr(&sn->sn_lock, - LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, VI_MTX(devvp)); + /* * Free any unused snapdata. */