Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Dec 2011 14:43:07 +0100
From:      Robert Millan <rmh@freebsd.org>
To:        freebsd-fs@freebsd.org, dougb@freebsd.org
Cc:        Adrian Chadd <adrian@freebsd.org>
Subject:   [PATCH] Wipe other file systems when creating new UFS
Message-ID:  <20111208134307.GA5266@thorin>

next in thread | raw e-mail | index | archive | help

--pvezYHf7grwyp3Bc
Content-Type: multipart/mixed; boundary="UugvWAfsgieZRqgk"
Content-Disposition: inline


--UugvWAfsgieZRqgk
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable


Hi,

This patch resolves a problem reported to Debian BTS [1] which also affects
FreeBSD.  The problem is that if you create a ZFS file system in a partition
and later decide you'd rather use UFS and overwrite it, remnants of ZFS
uberblocks are left at the beginning and end of the partition.  This can
lead to disaster (data loss) if "zpool import" is attempted, as it would
destroy UFS data structures.

newfs(8) currently erases remnants of UFS if they exist.  I think it should
go further and erase first and last 512 kiB of the partition.

[1] http://bugs.debian.org/635272

--=20
Robert Millan

--UugvWAfsgieZRqgk
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment; filename="wipe_other_fs.diff"
Content-Transfer-Encoding: quoted-printable

Index: sbin/newfs/mkfs.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- sbin/newfs/mkfs.c	(revision 228348)
+++ sbin/newfs/mkfs.c	(working copy)
@@ -113,6 +113,7 @@
 mkfs(struct partition *pp, char *fsys)
 {
 	int fragsperinode, optimalfpg, origdensity, minfpg, lastminfpg;
+	off_t mediasize;
 	long i, j, csfrags;
 	uint cg;
 	time_t utime;
@@ -505,24 +506,23 @@
 		    sblock.fs_size * sblock.fs_fsize - sblock.fs_sblockloc);
 	}
 	/*
-	 * Wipe out old UFS1 superblock(s) if necessary.
+	 * Wipe out other file systems. For now we erase first and last 512 kiB
+	 * (this works for at least UFS1 and ZFS).
 	 */
-	if (!Nflag && Oflag !=3D 1) {
-		i =3D bread(&disk, part_ofs + SBLOCK_UFS1 / disk.d_bsize, chdummy, SBLOC=
KSIZE);
-		if (i =3D=3D -1)
-			err(1, "can't read old UFS1 superblock: %s", disk.d_error);
+	if (!Eflag && !Nflag) {
+		mediasize =3D get_block_device_size(disk.d_fd);
=20
-		if (fsdummy.fs_magic =3D=3D FS_UFS1_MAGIC) {
-			fsdummy.fs_magic =3D 0;
-			bwrite(&disk, part_ofs + SBLOCK_UFS1 / disk.d_bsize,
-			    chdummy, SBLOCKSIZE);
-			for (cg =3D 0; cg < fsdummy.fs_ncg; cg++) {
-				if (fsbtodb(&fsdummy, cgsblock(&fsdummy, cg)) > fssize)
-					break;
-				bwrite(&disk, part_ofs + fsbtodb(&fsdummy,
-				  cgsblock(&fsdummy, cg)), chdummy, SBLOCKSIZE);
-			}
-		}
+		printf("Erasing sectors [%jd...%jd]\n",
+		    sblock.fs_sblockloc / disk.d_bsize,
+		    fsbtodb(&sblock, sblock.fs_size) - 1);
+		berase(&disk, sblock.fs_sblockloc / disk.d_bsize,
+		    1024 * disk.d_bsize - sblock.fs_sblockloc);
+
+		printf("Erasing sectors [%jd...%jd]\n",
+		    (mediasize - 1024 * 512) / disk.d_bsize,
+		    (mediasize / disk.d_bsize) - 1);
+		berase(&disk, (mediasize - 1024 * 512) / disk.d_bsize,
+		    1024 * 512);
 	}
 	if (!Nflag)
 		do_sbwrite(&disk);

--UugvWAfsgieZRqgk--

--pvezYHf7grwyp3Bc
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/kFreeBSD)

iQIcBAEBCAAGBQJO4L7pAAoJELd1onhloKnOSckP/2naJNqSpTOE84U+Ng/HcgW7
OtrYHlcTGVaeb+dV/MsFPiTW3g+w8A3+r1UsU8E9hrJGjZ27OQfI5dPZa9xgtRow
GAWLpmc0Hh5tvEGcCSEPhJX/03DzejzkYEPE9hIorp2ovoviaR+UpROi+RaH3ode
37OorAMp47xDXBHHe5BC2oOooRHCRrZMHaJFtoHXesykK0ehUmYZEYzeWIyKdtnd
piZ54+veQvnTBROYrUNlY0DrDH9zuROLF7Pzhh5sx/8YSvrzjnJrqozO0pP2+hbs
1176skKzAGG5HOl5xxSS5Q5zf3y0Zdvi/+N5bdM918jhB2UH6d9UfrOSKNjNQzpH
RdJ0ACA/7CW7kdgSa6yYjKlpkLpp3gAIJ8VicLoJF7RYV1MhZK4YDsfx3NcDHF2k
pa5qCMAjQwPmO0iQHjA6oRnuy6/KNJj+quI0e+G+XAtq/pOHb+RwChDLJYjq6tw0
nGSGgRKWveOTbN94t2AxrPlh1IRLcO7po/p+T2d4RojqEt8/U8yEO/J2An1gETEx
8Mtyt0BtyDx1d3v/U6Ko8Gp8Qly4WiJ9KaO0fnTN+DTFXgAplJ0oXGxAAjZw0nfY
4BQPCEfejMpGMsNwTARh+w9usDxrdZ0FPK3c1Jf01gsDaMnNHB2cWZaDHCti0iia
CEYYqBB3aNWfPx1KobHN
=SO4B
-----END PGP SIGNATURE-----

--pvezYHf7grwyp3Bc--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20111208134307.GA5266>