linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: force overwrite should wipe stale SB
@ 2014-09-05 15:51 Anand Jain
  2014-09-15 16:15 ` David Sterba
  2014-10-01 11:26 ` David Sterba
  0 siblings, 2 replies; 7+ messages in thread
From: Anand Jain @ 2014-09-05 15:51 UTC (permalink / raw)
  To: linux-btrfs

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 <type> 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.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 utils.c |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/utils.c b/utils.c
index 0064587..8b0c751 100644
--- a/utils.c
+++ b/utils.c
@@ -678,6 +678,35 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
 	return 0;
 }
 
+static void wipe_existing_fs(int fd)
+{
+	blkid_probe     pr = NULL;
+
+	pr = blkid_new_probe();
+	if (!pr)
+		return;
+
+	if (blkid_probe_set_device(pr, fd, 0, 0))
+		goto out;
+
+	blkid_probe_enable_superblocks(pr, 1);
+	blkid_probe_set_superblocks_flags(pr,
+				BLKID_SUBLKS_MAGIC |
+				BLKID_SUBLKS_TYPE |
+				BLKID_SUBLKS_USAGE |
+				BLKID_SUBLKS_LABEL |
+				BLKID_SUBLKS_UUID);
+
+	blkid_probe_enable_partitions(pr, 1);
+
+	while (blkid_do_probe(pr) == 0)
+		blkid_do_wipe(pr, 0);
+
+	fsync(fd);
+out:
+	blkid_free_probe(pr);
+}
+
 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
 			   u64 max_block_count, int *mixed, int discard)
 {
@@ -729,6 +758,8 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
 		return 1;
 	}
 
+	wipe_existing_fs(fd);
+
 	*block_count_ret = block_count;
 	return 0;
 }
-- 
1.7.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] btrfs-progs: force overwrite should wipe stale SB
  2014-09-05 15:51 [PATCH] btrfs-progs: force overwrite should wipe stale SB Anand Jain
@ 2014-09-15 16:15 ` David Sterba
  2014-10-01 11:26 ` David Sterba
  1 sibling, 0 replies; 7+ messages in thread
From: David Sterba @ 2014-09-15 16:15 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Fri, Sep 05, 2014 at 11:51:24PM +0800, Anand Jain wrote:
> 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 <type> 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.

Good catch.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] btrfs-progs: force overwrite should wipe stale SB
  2014-09-05 15:51 [PATCH] btrfs-progs: force overwrite should wipe stale SB Anand Jain
  2014-09-15 16:15 ` David Sterba
@ 2014-10-01 11:26 ` David Sterba
  2014-10-01 11:45   ` Anand Jain
  2014-10-01 23:22   ` [PATCH v2] " Anand Jain
  1 sibling, 2 replies; 7+ messages in thread
From: David Sterba @ 2014-10-01 11:26 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Fri, Sep 05, 2014 at 11:51:24PM +0800, Anand Jain wrote:
> +static void wipe_existing_fs(int fd)
> +{
...
> +	blkid_probe_enable_partitions(pr, 1);
> +
> +	while (blkid_do_probe(pr) == 0)
> +		blkid_do_wipe(pr, 0);

Reported by a user on IRC, I verified it on one of my old test boxes.
The function is missing in older versions of blkid and causes comilation
errors:

    [CC]     utils.o
utils.c: In function ‘wipe_existing_fs’:
utils.c:705:3: warning: implicit declaration of function ‘blkid_do_wipe

utils.o: In function `wipe_existing_fs':
/labs/dsterba/btrfs-progs/utils.c:705: undefined reference to `blkid_do_wipe'
collect2: ld returned 1 exit status

blkid_do_wipe is from Novembe 2011, so nothing really new, but I'd still
like to keep the backward compatibility of btrfs-progs.  The
implementation of blkid_do_wipe uses some internal data so it's not
possible to simply copy the implementation. Maybe wiping the first
megabyte is enough.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] btrfs-progs: force overwrite should wipe stale SB
  2014-10-01 11:26 ` David Sterba
@ 2014-10-01 11:45   ` Anand Jain
  2014-10-01 23:22   ` [PATCH v2] " Anand Jain
  1 sibling, 0 replies; 7+ messages in thread
From: Anand Jain @ 2014-10-01 11:45 UTC (permalink / raw)
  To: dsterba, linux-btrfs



On 10/01/2014 07:26 PM, David Sterba wrote:
> On Fri, Sep 05, 2014 at 11:51:24PM +0800, Anand Jain wrote:
>> +static void wipe_existing_fs(int fd)
>> +{
> ...
>> +	blkid_probe_enable_partitions(pr, 1);
>> +
>> +	while (blkid_do_probe(pr) == 0)
>> +		blkid_do_wipe(pr, 0);
>
> Reported by a user on IRC, I verified it on one of my old test boxes.
> The function is missing in older versions of blkid and causes comilation
> errors:
>
>      [CC]     utils.o
> utils.c: In function ‘wipe_existing_fs’:
> utils.c:705:3: warning: implicit declaration of function ‘blkid_do_wipe
>
> utils.o: In function `wipe_existing_fs':
> /labs/dsterba/btrfs-progs/utils.c:705: undefined reference to `blkid_do_wipe'
> collect2: ld returned 1 exit status
 >
> blkid_do_wipe is from Novembe 2011, so nothing really new, but I'd still
> like to keep the backward compatibility of btrfs-progs.  The

> implementation of blkid_do_wipe uses some internal data so it's not
> possible to simply copy the implementation. Maybe wiping the first
> megabyte is enough.

  for the sake of backward compatible.. the only choice I see as of now
  yes to write our own wipe. will send one.

Anand


> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2] btrfs-progs: force overwrite should wipe stale SB
  2014-10-01 11:26 ` David Sterba
  2014-10-01 11:45   ` Anand Jain
@ 2014-10-01 23:22   ` Anand Jain
  2014-10-10  7:39     ` David Sterba
  1 sibling, 1 reply; 7+ messages in thread
From: Anand Jain @ 2014-10-01 23:22 UTC (permalink / raw)
  To: linux-btrfs, dsterba

(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 <type> 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 <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
---
 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;
 }
-- 
2.0.0.153.g79dcccc


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] btrfs-progs: force overwrite should wipe stale SB
  2014-10-01 23:22   ` [PATCH v2] " Anand Jain
@ 2014-10-10  7:39     ` David Sterba
  2014-10-10  7:47       ` Anand Jain
  0 siblings, 1 reply; 7+ messages in thread
From: David Sterba @ 2014-10-10  7:39 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs, dsterba

On Thu, Oct 02, 2014 at 07:22:09AM +0800, Anand Jain wrote:
> (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 <type> 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.

Thank you, works for me, added to 3.17 queue.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] btrfs-progs: force overwrite should wipe stale SB
  2014-10-10  7:39     ` David Sterba
@ 2014-10-10  7:47       ` Anand Jain
  0 siblings, 0 replies; 7+ messages in thread
From: Anand Jain @ 2014-10-10  7:47 UTC (permalink / raw)
  To: dsterba, linux-btrfs



  Thanks.
Anand

On 10/10/2014 03:39 PM, David Sterba wrote:
> On Thu, Oct 02, 2014 at 07:22:09AM +0800, Anand Jain wrote:
>> (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 <type> 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.
>
> Thank you, works for me, added to 3.17 queue.
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-10-10  7:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-05 15:51 [PATCH] btrfs-progs: force overwrite should wipe stale SB Anand Jain
2014-09-15 16:15 ` David Sterba
2014-10-01 11:26 ` David Sterba
2014-10-01 11:45   ` Anand Jain
2014-10-01 23:22   ` [PATCH v2] " Anand Jain
2014-10-10  7:39     ` David Sterba
2014-10-10  7:47       ` Anand Jain

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).