summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorChristoph Hellwig <hch@lst.de>2011-04-21 16:24:07 -0400
committerChris Mason <chris.mason@oracle.com>2011-10-25 09:18:32 -0400
commit43a06f07c7b64bc4cabfbbf2397fb17b666b155f (patch)
tree3a0bf4c3f5b63bb7fbdc6b810d710e3da7946a0e /utils.c
parentb4d668ee6bcf93f097507df6d76fd0fca7d3888a (diff)
btrfs-progs: add discard support to mkfs
Discard the whole device before starting to create the filesystem structures. Modelled after similar support in mkfs.xfs. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 13373c99..17e5afe2 100644
--- a/utils.c
+++ b/utils.c
@@ -50,6 +50,20 @@
static inline int ioctl(int fd, int define, u64 *size) { return 0; }
#endif
+#ifndef BLKDISCARD
+#define BLKDISCARD _IO(0x12,119)
+#endif
+
+static int
+discard_blocks(int fd, u64 start, u64 len)
+{
+ u64 range[2] = { start, len };
+
+ if (ioctl(fd, BLKDISCARD, &range) < 0)
+ return errno;
+ return 0;
+}
+
static u64 reference_root_table[] = {
[1] = BTRFS_ROOT_TREE_OBJECTID,
[2] = BTRFS_EXTENT_TREE_OBJECTID,
@@ -537,6 +551,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
*mixed = 1;
}
+
+ /*
+ * We intentionally ignore errors from the discard ioctl. It is
+ * not necessary for the mkfs functionality but just an optimization.
+ */
+ discard_blocks(fd, 0, block_count);
+
ret = zero_dev_start(fd);
if (ret) {
fprintf(stderr, "failed to zero device start %d\n", ret);