summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/btrfs-tools/btrfs-tools/upstream-tmp/0015-btrfs-progs-add-discard-support-to-mkfs.patch
blob: 7a93b5314f409ea6497adaea603ae87f41b768d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Upstream-Status: Inappropriate [Backport]
From e6bd18d8938986c997c45f0ea95b221d4edec095 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <hch@lst.de>
Date: Thu, 21 Apr 2011 16:24:07 -0400
Subject: [PATCH 15/15] 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>
---
 btrfs_cmds.c |   21 +++++++++++++++++----
 utils.c      |   21 +++++++++++++++++++++
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/btrfs_cmds.c b/btrfs_cmds.c
index 6de73f4..32f6b25 100644
--- a/btrfs_cmds.c
+++ b/btrfs_cmds.c
@@ -732,13 +732,26 @@ int do_add_volume(int nargs, char **args)
 		return 12;
 	}
 
-	for(i=1 ; i < (nargs-1) ; i++ ){
+	for (i = 1; i < (nargs-1); i++ ){
 		struct btrfs_ioctl_vol_args ioctl_args;
 		int	devfd, res;
 		u64 dev_block_count = 0;
 		struct stat st;
 		int mixed = 0;
 
+		res = check_mounted(args[i]);
+		if (res < 0) {
+			fprintf(stderr, "error checking %s mount status\n",
+				args[i]);
+			ret++;
+			continue;
+		}
+		if (res == 1) {
+			fprintf(stderr, "%s is mounted\n", args[i]);
+			ret++;
+			continue;
+		}
+
 		devfd = open(args[i], O_RDWR);
 		if (!devfd) {
 			fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
@@ -746,8 +759,8 @@ int do_add_volume(int nargs, char **args)
 			ret++;
 			continue;
 		}
-		ret = fstat(devfd, &st);
-		if (ret) {
+		res = fstat(devfd, &st);
+		if (res) {
 			fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
 			close(devfd);
 			ret++;
@@ -781,7 +794,7 @@ int do_add_volume(int nargs, char **args)
 	}
 
 	close(fdmnt);
-	if( ret)
+	if (ret)
 		return ret+20;
 	else
 		return 0;
diff --git a/utils.c b/utils.c
index 13373c9..17e5afe 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);
-- 
1.7.2.3