]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
mkfs: add discard support
authorChristoph Hellwig <hch@lst.de>
Tue, 13 Oct 2009 22:28:52 +0000 (00:28 +0200)
committerhch@lst.de <Christoph Hellwig>
Tue, 13 Oct 2009 22:28:52 +0000 (00:28 +0200)
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>
include/darwin.h
include/freebsd.h
include/irix.h
include/linux.h
man/man8/mkfs.xfs.8
mkfs/xfs_mkfs.c

index 25cd61841cb8163bf0729947316addb6ed2299c2..69c03227753063133540ddf31fadc8dec3de780f 100644 (file)
@@ -154,4 +154,10 @@ typedef unsigned char      uchar_t;
 
 #define HAVE_FID       1
 
+static __inline__ int
+platform_discard_blocks(int fd, off64_t start, off64_t end)
+{
+       return 0;
+}
+
 #endif /* __XFS_DARWIN_H__ */
index 967ca25f238575d937b1f12ad41740b8764cbd90..7089dd97ca9d5ba77f4b709a6a7b92116ecfb847 100644 (file)
@@ -139,4 +139,10 @@ static __inline__ void platform_uuid_copy(uuid_t *dst, uuid_t *src)
        memcpy(dst, src, sizeof(uuid_t));
 }
 
+static __inline__ int
+platform_discard_blocks(int fd, off64_t start, off64_t end)
+{
+       return 0;
+}
+
 #endif /* __XFS_FREEBSD_H__ */
index a7debd3e991603cd46d6dcfd27dbe125abe6daa6..f75193939acfa41f21e5a6251b64fc8011dbd49c 100644 (file)
@@ -337,6 +337,12 @@ static __inline__ void platform_uuid_copy(uuid_t *dst, uuid_t *src)
        memcpy(dst, src, sizeof(uuid_t));
 }
 
+static __inline__ int
+platform_discard_blocks(int fd, off64_t start, off64_t end)
+{
+       return 0;
+}
+
 static __inline__ char * strsep(char **s, const char *ct)
 {
        char *sbegin = *s, *end;
index 1bb5866d6ee2c45da5852653cba75bc638d5c4cc..0486485137d642ca371d95e3e71814092976bc90 100644 (file)
@@ -93,6 +93,20 @@ static __inline__ void platform_uuid_copy(uuid_t *dst, uuid_t *src)
        uuid_copy(*dst, *src);
 }
 
+#ifndef BLKDISCARD
+#define BLKDISCARD     _IO(0x12,119)
+#endif
+
+static __inline__ int
+platform_discard_blocks(int fd, off64_t start, off64_t end)
+{
+       __uint64_t range[2] = { start, end };
+
+       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 74a98db385a3b7c1e9a6c189e6c85ff2fc0ca433..40ea1bd707d6fcaf47facf67bcff41087aa2d054 100644 (file)
@@ -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 information.
 .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 75ad20c5297f4b2a7453fb9b3c6640ac863705d9..bd92bc00fdbe6afa68f2ba1c66836c0275278c24 100644 (file)
@@ -736,6 +736,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,
@@ -812,6 +826,7 @@ main(
        int                     nvflag;
        int                     nci;
        int                     Nflag;
+       int                     discard = 1;
        char                    *p;
        char                    *protofile;
        char                    *protostring;
@@ -870,7 +885,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':
@@ -1386,6 +1401,9 @@ main(
                case 'N':
                        Nflag = 1;
                        break;
+               case 'K':
+                       discard = 0;
+                       break;
                case 'p':
                        if (protofile)
                                respec('p', NULL, 0);
@@ -1773,6 +1791,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)