From patchwork Wed Oct 1 23:22:09 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 5016461 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 40CD1BEEA6 for ; Thu, 2 Oct 2014 07:17:39 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5AA652021A for ; Thu, 2 Oct 2014 07:17:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 12B3C2026F for ; Thu, 2 Oct 2014 07:17:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751646AbaJBHR0 (ORCPT ); Thu, 2 Oct 2014 03:17:26 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:24225 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751031AbaJBHRU (ORCPT ); Thu, 2 Oct 2014 03:17:20 -0400 Received: from ucsinet22.oracle.com (ucsinet22.oracle.com [156.151.31.94]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id s927HGTh031594 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 2 Oct 2014 07:17:17 GMT Received: from aserz7022.oracle.com (aserz7022.oracle.com [141.146.126.231]) by ucsinet22.oracle.com (8.14.5+Sun/8.14.5) with ESMTP id s927HEm2029620 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 2 Oct 2014 07:17:15 GMT Received: from abhmp0015.oracle.com (abhmp0015.oracle.com [141.146.116.21]) by aserz7022.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s927HEII026266; Thu, 2 Oct 2014 07:17:14 GMT Received: from OL.sg.oracle.com (/10.186.101.34) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 02 Oct 2014 00:17:13 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org, dsterba@suse.cz Subject: [PATCH v2] btrfs-progs: force overwrite should wipe stale SB Date: Thu, 2 Oct 2014 07:22:09 +0800 Message-Id: <1412205729-20938-1-git-send-email-anand.jain@oracle.com> X-Mailer: git-send-email 2.0.0.153.g79dcccc In-Reply-To: <20141001112606.GO11436@twin.jikos.cz> References: <20141001112606.GO11436@twin.jikos.cz> X-Source-IP: ucsinet22.oracle.com [156.151.31.94] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00, DATE_IN_PAST_06_12, 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 (I am unable to reproduce the issue, tried to go back with progs versions but still the same. So as of now this code remains untested, suggest to wait till we have a reproducible test case). Here is a test case which says it all.. mkfs.xfs -f $DEV mkfs.btrfs -f $DEV mount $DEV $MNT mount: /dev/vdiskc: more filesystems detected. This should not happen, use -t to explicitly specify the filesystem type or use wipefs(8) to clean up the device. mount: you must specify the filesystem type with this patch btrfs_prepare_device() also wipes old FS if any, btrfs_prepare_device() is called after we have verified that user has provided -f option. v2: to satisfy the backward compatibility issue, replace blkid_do_wipe() with local wipe function. Signed-off-by: Anand Jain Signed-off-by: David Sterba --- utils.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/utils.c b/utils.c index fb78dd6..5f21737 100644 --- a/utils.c +++ b/utils.c @@ -680,6 +680,42 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, return 0; } +static void btrfs_wipe_existing_sb(int fd) +{ + const char *off = NULL; + size_t len = 0; + loff_t offset; + char buf[BUFSIZ]; + int rc = 0; + blkid_probe pr = NULL; + + pr = blkid_new_probe(); + if (!pr) + return; + + if (blkid_probe_set_device(pr, fd, 0, 0)) + goto out; + + rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL); + if (!rc) + rc = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len); + + if (rc || len == 0 || off == NULL) + goto out; + + offset = strtoll(off, NULL, 10); + if (len > sizeof(buf)) + len = sizeof(buf); + + memset(buf, 0, len); + rc = pwrite(fd, buf, len, offset); + fsync(fd); + +out: + blkid_free_probe(pr); + return; +} + int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, u64 max_block_count, int *mixed, int discard) { @@ -731,6 +767,8 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, return 1; } + btrfs_wipe_existing_sb(fd); + *block_count_ret = block_count; return 0; }