Blob Blame History Raw
From: Christoph Hellwig <hch@lst.de>
Date: Tue, 13 Oct 2009 22:28:52 +0000 (+0200)
Subject: mkfs: add discard support
X-Git-Url: http://git.kernel.org/?p=fs%2Fxfs%2Fxfsprogs-dev.git;a=commitdiff_plain;h=ad136b3382919e93cc692b54f735fad8b35e88fe

mkfs: add discard support

Call the BLKDISCARD ioctl to mark the whole disk as unused before creating
a new filesystem.  This will allow SSDs, Arrays with thin provisioning support
and virtual machines to make smarter allocation decisions.

Add a new -K option to prevent mkfs from discarding blocks to aid
trouble-shooting or specialized requirements.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Eric Sandeen <sandeen@sandeen.net>
Reviewed-by: Andi Kleen <andi@firstfloor.org>
---

From: Christoph Hellwig <hch@lst.de>
Date: Fri, 20 Nov 2009 09:55:15 +0000 (+0100)
Subject: xfsprogs: stop using off64_t in platform headers
X-Git-Url: http://git.kernel.org/?p=fs%2Fxfs%2Fxfsprogs-dev.git;a=commitdiff_plain;h=f96aff0c250ae8c70f50d5becb592723eb064f5d

xfsprogs: stop using off64_t in platform headers

Using off64_t may require special headers or compiler flags that aren't
always available, e.g. in the configure check in xfstests.  Rever to a plain
uint64_t to make apps compile as before.

While we're at it also rename the second argument of platform_discard_blocks
from end to len as that's what the BLKDISCARD ioctl excepts - we currently
always discard the whole device so it doesn't matter in practice.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Eric Sandeen <sandeen@sandeen.net>
---

Index: xfsprogs-3.0.3/include/darwin.h
===================================================================
--- xfsprogs-3.0.3.orig/include/darwin.h
+++ xfsprogs-3.0.3/include/darwin.h
@@ -154,4 +154,10 @@ typedef unsigned char	uchar_t;
 
 #define HAVE_FID	1
 
+static __inline__ int
+platform_discard_blocks(int fd, uint64_t start, uint64_t len)
+{
+	return 0;
+}
+
 #endif	/* __XFS_DARWIN_H__ */
Index: xfsprogs-3.0.3/include/freebsd.h
===================================================================
--- xfsprogs-3.0.3.orig/include/freebsd.h
+++ xfsprogs-3.0.3/include/freebsd.h
@@ -139,4 +139,10 @@ static __inline__ void platform_uuid_cop
 	memcpy(dst, src, sizeof(uuid_t));
 }
 
+static __inline__ int
+platform_discard_blocks(int fd, uint64_t start, uint64_t len)
+{
+	return 0;
+}
+
 #endif	/* __XFS_FREEBSD_H__ */
Index: xfsprogs-3.0.3/include/irix.h
===================================================================
--- xfsprogs-3.0.3.orig/include/irix.h
+++ xfsprogs-3.0.3/include/irix.h
@@ -337,6 +337,12 @@ static __inline__ void platform_uuid_cop
 	memcpy(dst, src, sizeof(uuid_t));
 }
 
+static __inline__ int
+platform_discard_blocks(int fd, uint64_t start, uint64_t len)
+{
+	return 0;
+}
+
 static __inline__ char * strsep(char **s, const char *ct)
 {
 	char *sbegin = *s, *end;
Index: xfsprogs-3.0.3/include/linux.h
===================================================================
--- xfsprogs-3.0.3.orig/include/linux.h
+++ xfsprogs-3.0.3/include/linux.h
@@ -93,6 +93,20 @@ static __inline__ void platform_uuid_cop
 	uuid_copy(*dst, *src);
 }
 
+#ifndef BLKDISCARD
+#define BLKDISCARD	_IO(0x12,119)
+#endif
+
+static __inline__ int
+platform_discard_blocks(int fd, uint64_t start, uint64_t len)
+{
+	__uint64_t range[2] = { start, len };
+
+	if (ioctl(fd, BLKDISCARD, &range) < 0)
+		return errno;
+	return 0;
+}
+
 #if (__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ <= 1))
 # define constpp	const char * const *
 #else
Index: xfsprogs-3.0.3/man/man8/mkfs.xfs.8
===================================================================
--- xfsprogs-3.0.3.orig/man/man8/mkfs.xfs.8
+++ xfsprogs-3.0.3/man/man8/mkfs.xfs.8
@@ -36,6 +36,8 @@ mkfs.xfs \- construct an XFS filesystem
 .I label
 ] [
 .B \-N
+] [
+.B \-K
 ]
 .I device
 .SH DESCRIPTION
@@ -714,6 +716,9 @@ manual entries for additional informatio
 .B \-N
 Causes the file system parameters to be printed out without really
 creating the file system.
+.TP
+.B \-K
+Do not attempt to discard blocks at mkfs time.
 .SH SEE ALSO
 .BR xfs (5),
 .BR mkfs (8),
Index: xfsprogs-3.0.3/mkfs/xfs_mkfs.c
===================================================================
--- xfsprogs-3.0.3.orig/mkfs/xfs_mkfs.c
+++ xfsprogs-3.0.3/mkfs/xfs_mkfs.c
@@ -604,6 +604,20 @@ done:
 	free(buf);
 }
 
+static void
+discard_blocks(dev_t dev, __uint64_t nsectors)
+{
+	int fd;
+
+	/*
+	 * We intentionally ignore errors from the discard ioctl.  It is
+	 * not necessary for the mkfs functionality but just an optimization.
+	 */
+	fd = libxfs_device_to_fd(dev);
+	if (fd > 0)
+		platform_discard_blocks(fd, 0, nsectors << 9);
+}
+
 int
 main(
 	int			argc,
@@ -680,6 +694,7 @@ main(
 	int			nvflag;
 	int			nci;
 	int			Nflag;
+	int			discard = 1;
 	char			*p;
 	char			*protofile;
 	char			*protostring;
@@ -740,7 +755,7 @@ main(
 	xi.isdirect = LIBXFS_DIRECT;
 	xi.isreadonly = LIBXFS_EXCLUSIVELY;
 
-	while ((c = getopt(argc, argv, "b:d:i:l:L:n:Np:qr:s:CfV")) != EOF) {
+	while ((c = getopt(argc, argv, "b:d:i:l:L:n:KNp:qr:s:CfV")) != EOF) {
 		switch (c) {
 		case 'C':
 		case 'f':
@@ -1256,6 +1271,9 @@ main(
 		case 'N':
 			Nflag = 1;
 			break;
+		case 'K':
+			discard = 0;
+			break;
 		case 'p':
 			if (protofile)
 				respec('p', NULL, 0);
@@ -1644,6 +1662,14 @@ main(
 		}
 	}
 
+	if (discard) {
+		discard_blocks(xi.ddev, xi.dsize);
+		if (xi.rtdev)
+			discard_blocks(xi.rtdev, xi.rtsize);
+		if (xi.logdev && xi.logdev != xi.ddev)
+			discard_blocks(xi.logdev, xi.logBBsize);
+	}
+
 	if (!liflag && !ldflag)
 		loginternal = xi.logdev == 0;
 	if (xi.logname)