linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dave Chinner <david@fromorbit.com>
To: linux-xfs@vger.kernel.org
Subject: [PATCH 16/42] mkfs: factor out device preparation
Date: Wed, 30 Aug 2017 09:50:26 +1000	[thread overview]
Message-ID: <20170829235052.21050-17-david@fromorbit.com> (raw)
In-Reply-To: <20170829235052.21050-1-david@fromorbit.com>

From: Dave Chinner <dchinner@redhat.com>

Prior to formating the device(s), we have to take several steps to
prepare them and check that they are appropriate for the formatting
that is about to take place. Pull all this into a single function
that is run before mounting the libxfs infrastructure.

Signed-Off-By: Dave Chinner <dchinner@redhat.com>
---
 mkfs/xfs_mkfs.c | 167 ++++++++++++++++++++++++++++++++------------------------
 1 file changed, 96 insertions(+), 71 deletions(-)

diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
index 0e115bf3cf4d..2438e50d3f33 100644
--- a/mkfs/xfs_mkfs.c
+++ b/mkfs/xfs_mkfs.c
@@ -1993,6 +1993,97 @@ setup_superblock(
 	}
 }
 
+/*
+ * Sanitise the data and log devices and prepare them so libxfs can mount the
+ * device successfully. Also check we can access the rt device if configured.
+ */
+static void
+prepare_devices(
+	struct mkfs_params	*cfg,
+	struct libxfs_xinit	*xi,
+	struct xfs_mount	*mp,
+	struct xfs_sb		*sbp,
+	bool			clear_stale)
+{
+	struct xfs_buf		*buf;
+	int			whack_blks = BTOBB(WHACK_SIZE);
+
+	/*
+	 * If there's an old XFS filesystem on the device with enough intact
+	 * information that we can parse the superblock, there's enough
+	 * information on disk to confuse a future xfs_repair call. To avoid
+	 * this, whack all the old secondary superblocks that we can find.
+	 */
+	if (clear_stale)
+		zero_old_xfs_structures(xi, sbp);
+
+	/*
+	 * If the data device is a file, grow out the file to its final size if
+	 * needed so that the reads for the end of the device in the mount code
+	 * will succeed.
+	 */
+	if (xi->disfile &&
+	    xi->dsize * xi->dbsize < cfg->dblocks * cfg->blocksize) {
+		if (ftruncate(xi->dfd, cfg->dblocks * cfg->blocksize) < 0) {
+			fprintf(stderr,
+				_("%s: Growing the data section failed\n"),
+				progname);
+			exit(1);
+		}
+
+		/* update size to be able to whack blocks correctly */
+		xi->dsize = BTOBB(cfg->dblocks * cfg->blocksize);
+	}
+
+	/*
+	 * Zero out the end to obliterate any old MD RAID (or other) metadata at
+	 * the end of the device.  (MD sb is ~64k from the end, take out a wider
+	 * swath to be sure)
+	 */
+	buf = libxfs_getbuf(mp->m_ddev_targp, (xi->dsize - whack_blks),
+			    whack_blks);
+	memset(XFS_BUF_PTR(buf), 0, WHACK_SIZE);
+	libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
+	libxfs_purgebuf(buf);
+
+	/*
+	 * Now zero out the beginning of the device, to obliterate any old
+	 * filesystem signatures out there.  This should take care of
+	 * swap (somewhere around the page size), jfs (32k),
+	 * ext[2,3] and reiserfs (64k) - and hopefully all else.
+	 */
+	buf = libxfs_getbuf(mp->m_ddev_targp, 0, whack_blks);
+	memset(XFS_BUF_PTR(buf), 0, WHACK_SIZE);
+	libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
+	libxfs_purgebuf(buf);
+
+	/* OK, now write the superblock... */
+	buf = libxfs_getbuf(mp->m_ddev_targp, XFS_SB_DADDR, XFS_FSS_TO_BB(mp, 1));
+	buf->b_ops = &xfs_sb_buf_ops;
+	memset(XFS_BUF_PTR(buf), 0, cfg->sectorsize);
+	libxfs_sb_to_disk((void *)XFS_BUF_PTR(buf), sbp);
+	libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
+	libxfs_purgebuf(buf);
+
+	/* ...and zero the log.... */
+	libxfs_log_clear(mp->m_logdev_targp, NULL,
+			 XFS_FSB_TO_DADDR(mp, cfg->logstart),
+			 (xfs_extlen_t)XFS_FSB_TO_BB(mp, cfg->logblocks),
+			 &sbp->sb_uuid, cfg->sb_feat.log_version,
+			 sbp->sb_logsunit, XLOG_FMT, XLOG_INIT_CYCLE, false);
+
+	/* finally, check we can write the last block in the realtime area */
+	if (mp->m_rtdev_targp->dev && cfg->rtblocks > 0) {
+		buf = libxfs_getbuf(mp->m_rtdev_targp,
+				    XFS_FSB_TO_BB(mp, cfg->rtblocks - 1LL),
+				    BTOBB(cfg->blocksize));
+		memset(XFS_BUF_PTR(buf), 0, cfg->blocksize);
+		libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
+		libxfs_purgebuf(buf);
+	}
+
+}
+
 int
 main(
 	int			argc,
@@ -3174,65 +3265,17 @@ _("size %s specified for log subvolume is too large, maximum is %lld blocks\n"),
 	 */
 	setup_superblock(&cfg, mp, sbp);
 
-	if (force_overwrite)
-		zero_old_xfs_structures(&xi, sbp);
-
 	/*
-	 * Zero out the beginning of the device, to obliterate any old
-	 * filesystem signatures out there.  This should take care of
-	 * swap (somewhere around the page size), jfs (32k),
-	 * ext[2,3] and reiserfs (64k) - and hopefully all else.
+	 * we need the libxfs buffer cache from here on in.
 	 */
 	libxfs_buftarg_init(mp, xi.ddev, xi.logdev, xi.rtdev);
-	buf = libxfs_getbuf(mp->m_ddev_targp, 0, BTOBB(WHACK_SIZE));
-	memset(XFS_BUF_PTR(buf), 0, WHACK_SIZE);
-	libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
-	libxfs_purgebuf(buf);
-
-	/* OK, now write the superblock */
-	buf = libxfs_getbuf(mp->m_ddev_targp, XFS_SB_DADDR, XFS_FSS_TO_BB(mp, 1));
-	buf->b_ops = &xfs_sb_buf_ops;
-	memset(XFS_BUF_PTR(buf), 0, sectorsize);
-	libxfs_sb_to_disk((void *)XFS_BUF_PTR(buf), sbp);
-	libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
-	libxfs_purgebuf(buf);
-
-	/*
-	 * If the data area is a file, then grow it out to its final size
-	 * if needed so that the reads for the end of the device in the mount
-	 * code will succeed.
-	 */
-	if (xi.disfile && xi.dsize * xi.dbsize < dblocks * blocksize) {
-		if (ftruncate(xi.dfd, dblocks * blocksize) < 0) {
-			fprintf(stderr,
-				_("%s: Growing the data section failed\n"),
-				progname);
-			exit(1);
-		}
-	}
-
-	/*
-	 * Zero out the end of the device, to obliterate any
-	 * old MD RAID (or other) metadata at the end of the device.
-	 * (MD sb is ~64k from the end, take out a wider swath to be sure)
-	 */
-	if (!xi.disfile) {
-		buf = libxfs_getbuf(mp->m_ddev_targp,
-				    (xi.dsize - BTOBB(WHACK_SIZE)),
-				    BTOBB(WHACK_SIZE));
-		memset(XFS_BUF_PTR(buf), 0, WHACK_SIZE);
-		libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
-		libxfs_purgebuf(buf);
-	}
 
 	/*
-	 * Zero the log....
+	 * Before we mount the filesystem we need to make sure the devices have
+	 * enough of the filesystem structure on them that allows libxfs to
+	 * mount.
 	 */
-	libxfs_log_clear(mp->m_logdev_targp, NULL,
-		XFS_FSB_TO_DADDR(mp, logstart),
-		(xfs_extlen_t)XFS_FSB_TO_BB(mp, logblocks),
-		&sbp->sb_uuid, sb_feat.log_version, lsunit, XLOG_FMT, XLOG_INIT_CYCLE, false);
-
+	prepare_devices(&cfg, &xi, mp, sbp, force_overwrite);
 	mp = libxfs_mount(mp, sbp, xi.ddev, xi.logdev, xi.rtdev, 0);
 	if (mp == NULL) {
 		fprintf(stderr, _("%s: filesystem failed to initialize\n"),
@@ -3576,24 +3619,6 @@ _("size %s specified for log subvolume is too large, maximum is %lld blocks\n"),
 	}
 
 	/*
-	 * Touch last block, make fs the right size if it's a file.
-	 */
-	buf = libxfs_getbuf(mp->m_ddev_targp,
-		(xfs_daddr_t)XFS_FSB_TO_BB(mp, dblocks - 1LL), bsize);
-	memset(XFS_BUF_PTR(buf), 0, blocksize);
-	libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
-
-	/*
-	 * Make sure we can write the last block in the realtime area.
-	 */
-	if (mp->m_rtdev_targp->dev && rtblocks > 0) {
-		buf = libxfs_getbuf(mp->m_rtdev_targp,
-				XFS_FSB_TO_BB(mp, rtblocks - 1LL), bsize);
-		memset(XFS_BUF_PTR(buf), 0, blocksize);
-		libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
-	}
-
-	/*
 	 * BNO, CNT free block list
 	 */
 	for (agno = 0; agno < agcount; agno++) {
-- 
2.13.3


  parent reply	other threads:[~2017-08-29 23:51 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-29 23:50 [PATCH 00/42] mkfs: factor the crap out of the code Dave Chinner
2017-08-29 23:50 ` [PATCH 01/42] mkfs: can't specify sector size of internal log Dave Chinner
2017-08-29 23:50 ` [PATCH 02/42] mkfs: make subopt table const Dave Chinner
2017-08-29 23:50 ` [PATCH 03/42] mkfs: introduce a structure to hold CLI options Dave Chinner
2017-08-29 23:50 ` [PATCH 04/42] mkfs: add generic subopt parsing table Dave Chinner
2017-08-29 23:50 ` [PATCH 05/42] mkfs: factor block subopts parser Dave Chinner
2017-08-29 23:50 ` [PATCH 06/42] mkfs: factor data " Dave Chinner
2017-08-29 23:50 ` [PATCH 07/42] mkfs: factor inode " Dave Chinner
2017-08-29 23:50 ` [PATCH 08/42] mkfs: factor log " Dave Chinner
2017-08-29 23:50 ` [PATCH 09/42] mkfs: factor meta " Dave Chinner
2017-08-29 23:50 ` [PATCH 10/42] mkfs: factor naming " Dave Chinner
2017-08-29 23:50 ` [PATCH 11/42] mkfs: factor rt " Dave Chinner
2017-08-29 23:50 ` [PATCH 12/42] mkfs: factor sector " Dave Chinner
2017-08-29 23:50 ` [PATCH 13/42] mkfs: Introduce mkfs configuration structure Dave Chinner
2017-08-29 23:50 ` [PATCH 14/42] mkfs: factor printing of mkfs config Dave Chinner
2017-08-29 23:50 ` [PATCH 15/42] mkfs: factor in memory superblock setup Dave Chinner
2017-08-29 23:50 ` Dave Chinner [this message]
2017-08-29 23:50 ` [PATCH 17/42] mkfs: factor writing AG headers Dave Chinner
2017-08-29 23:50 ` [PATCH 18/42] mkfs: factor secondary superblock updates Dave Chinner
2017-08-29 23:50 ` [PATCH 19/42] mkfs: introduce default configuration structure Dave Chinner
2017-08-29 23:50 ` [PATCH 20/42] mkfs: rename top level CLI parameters Dave Chinner
2017-08-29 23:50 ` [PATCH 21/42] mkfs: factor sectorsize validation Dave Chinner
2017-08-29 23:50 ` [PATCH 22/42] mkfs: factor blocksize validation Dave Chinner
2017-08-29 23:50 ` [PATCH 23/42] mkfs: factor log sector size validation Dave Chinner
2017-08-29 23:50 ` [PATCH 24/42] mkfs: factor superblock feature validation Dave Chinner
2017-08-29 23:50 ` [PATCH 25/42] mkfs: factor directory blocksize validation Dave Chinner
2017-08-29 23:50 ` [PATCH 26/42] mkfs: factor inode size validation Dave Chinner
2017-08-29 23:50 ` [PATCH 27/42] mkfs: factor out device size calculations Dave Chinner
2017-08-29 23:50 ` [PATCH 28/42] mkfs: fix hidden parameter in DTOBT() Dave Chinner
2017-08-29 23:50 ` [PATCH 29/42] mkfs: factor rtdev extent size validation Dave Chinner
2017-08-29 23:50 ` [PATCH 30/42] mkfs: rework stripe calculations Dave Chinner
2017-08-29 23:50 ` [PATCH 31/42] mkfs: factor device opening Dave Chinner
2017-08-29 23:50 ` [PATCH 32/42] mkfs: factor data device validation Dave Chinner
2017-08-29 23:50 ` [PATCH 33/42] mkfs: factor log " Dave Chinner
2017-08-29 23:50 ` [PATCH 34/42] mkfs: factor rt " Dave Chinner
2017-08-29 23:50 ` [PATCH 35/42] mkfs: factor AG geometry calculations Dave Chinner
2017-08-29 23:50 ` [PATCH 36/42] mkfs: factor AG alignment Dave Chinner
2017-08-30 23:44   ` Dave Chinner
2017-08-29 23:50 ` [PATCH 37/42] mkfs: rework imaxpct calculation Dave Chinner
2017-08-29 23:50 ` [PATCH 38/42] mkfs: factor initial mount setup Dave Chinner
2017-08-29 23:50 ` [PATCH 39/42] mkfs: factor log size calculations Dave Chinner
2017-09-05  5:23   ` Dave Chinner
2017-08-29 23:50 ` [PATCH 40/42] mkfs: cleanup redundant temporary code Dave Chinner
2017-08-29 23:50 ` [PATCH 41/42] mkfs: move error functions Dave Chinner
2017-08-29 23:50 ` [PATCH 42/42] mkfs: tidy up definitions Dave Chinner
2017-08-30  1:23 ` [PATCH 00/42] mkfs: factor the crap out of the code Darrick J. Wong
2017-08-30  1:57   ` Dave Chinner
2017-08-30  4:16 ` Luis R. Rodriguez
2017-08-30  5:44   ` Dave Chinner
2017-08-30 22:10     ` Luis R. Rodriguez
2017-08-30 23:22       ` Dave Chinner
2017-08-31  0:05         ` Luis R. Rodriguez
2017-08-31 16:23     ` Jan Tulak
2017-08-30  7:44 ` Martin Steigerwald
2017-09-04 12:31 ` Chandan Rajendra
2017-09-04 15:34   ` Eric Sandeen
2017-09-04 22:40   ` Dave Chinner
2017-09-07 10:31 ` Chandan Rajendra
2017-09-07 23:38   ` Dave Chinner
2017-09-09 10:24 ` Chandan Rajendra
2017-09-15  9:42 ` Jan Tulak
2017-09-16 11:29   ` Dave Chinner
2017-10-24  3:00 ` Eric Sandeen
2017-10-25  0:59   ` Dave Chinner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170829235052.21050-17-david@fromorbit.com \
    --to=david@fromorbit.com \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).