From patchwork Tue May 20 06:31:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 4207401 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 02C899F32A for ; Tue, 20 May 2014 06:29:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 39284201C7 for ; Tue, 20 May 2014 06:29:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4C46D201C0 for ; Tue, 20 May 2014 06:29:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751506AbaETG3v (ORCPT ); Tue, 20 May 2014 02:29:51 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:41729 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750879AbaETG3u (ORCPT ); Tue, 20 May 2014 02:29:50 -0400 Received: from acsinet22.oracle.com (acsinet22.oracle.com [141.146.126.238]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id s4K6T4Nq027590 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 20 May 2014 06:29:04 GMT Received: from aserz7022.oracle.com (aserz7022.oracle.com [141.146.126.231]) by acsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s4K6T2la014698 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 20 May 2014 06:29:03 GMT Received: from abhmp0020.oracle.com (abhmp0020.oracle.com [141.146.116.26]) by aserz7022.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s4K6T2E8014694; Tue, 20 May 2014 06:29:02 GMT Received: from wish.sg.oracle.com (/10.186.101.18) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 19 May 2014 23:29:01 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: brendan@swiftspirit.co.za Subject: [PATCH 1/4 RESEND V3] Btrfs: all super blocks of the replaced disk must be scratched Date: Tue, 20 May 2014 14:31:57 +0800 Message-Id: <1400567517-9437-1-git-send-email-anand.jain@oracle.com> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1400519022-5540-1-git-send-email-anand.jain@oracle.com> References: <1400519022-5540-1-git-send-email-anand.jain@oracle.com> X-Source-IP: acsinet22.oracle.com [141.146.126.238] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Anand Jain In a normal scenario when sys-admin replaces a disk, the expectation is that btrfs will release the disk completely. However the below test case gives a wrong impression that replaced disk is still is in use. $ btrfs rep start /dev/sde /dev/sdg4 /btrfs $ mkfs.btrfs /dev/sde /dev/sde appears to contain an existing filesystem (btrfs). Error: Use the -f option to force overwrite. Reviewed-by: David Sterba Signed-off-by: Anand Jain --- v2->v3: fix commit typo error. thanks Brendan v1->v2: add missing signed-off fs/btrfs/volumes.c | 33 +++++++++++++++++++++++++-------- 1 files changed, 25 insertions(+), 8 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index b4660c4..19e68f7 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -6280,16 +6280,33 @@ int btrfs_scratch_superblock(struct btrfs_device *device) { struct buffer_head *bh; struct btrfs_super_block *disk_super; + int i; + u64 bytenr; - bh = btrfs_read_dev_super(device->bdev); - if (!bh) - return -EINVAL; - disk_super = (struct btrfs_super_block *)bh->b_data; + for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { + bytenr = btrfs_sb_offset(i); + if (bytenr + BTRFS_SUPER_INFO_SIZE >= + i_size_read(device->bdev->bd_inode)) + break; - memset(&disk_super->magic, 0, sizeof(disk_super->magic)); - set_buffer_dirty(bh); - sync_dirty_buffer(bh); - brelse(bh); + bh = __bread(device->bdev, bytenr / 4096, + BTRFS_SUPER_INFO_SIZE); + if (!bh) + continue; + + disk_super = (struct btrfs_super_block *)bh->b_data; + if (btrfs_super_bytenr(disk_super) != bytenr || + btrfs_super_magic(disk_super) != BTRFS_MAGIC) { + brelse(bh); + continue; + } + + memset(&disk_super->magic, 0, sizeof(disk_super->magic)); + + set_buffer_dirty(bh); + sync_dirty_buffer(bh); + brelse(bh); + } return 0; }