diff options
Diffstat (limited to 'fs/btrfs/mkfs.c')
-rw-r--r-- | fs/btrfs/mkfs.c | 95 |
1 files changed, 94 insertions, 1 deletions
diff --git a/fs/btrfs/mkfs.c b/fs/btrfs/mkfs.c index 1cac5ab114dd..f7efc8a5fb1a 100644 --- a/fs/btrfs/mkfs.c +++ b/fs/btrfs/mkfs.c | |||
@@ -1,4 +1,8 @@ | |||
1 | #define _XOPEN_SOURCE 500 | 1 | #define _XOPEN_SOURCE 500 |
2 | #ifndef __CHECKER__ | ||
3 | #include <sys/ioctl.h> | ||
4 | #include <sys/mount.h> | ||
5 | #endif | ||
2 | #include <stdio.h> | 6 | #include <stdio.h> |
3 | #include <stdlib.h> | 7 | #include <stdlib.h> |
4 | #include <sys/types.h> | 8 | #include <sys/types.h> |
@@ -10,6 +14,17 @@ | |||
10 | #include "ctree.h" | 14 | #include "ctree.h" |
11 | #include "disk-io.h" | 15 | #include "disk-io.h" |
12 | 16 | ||
17 | #ifdef __CHECKER__ | ||
18 | #define BLKGETSIZE64 0 | ||
19 | static inline int ioctl(int fd, int define, u64 *size) { return 0; } | ||
20 | #endif | ||
21 | |||
22 | #if 0 | ||
23 | #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64) | ||
24 | # define BLKGETSIZE64 _IOR(0x12, 114, __u64) | ||
25 | #endif | ||
26 | #endif | ||
27 | |||
13 | int mkfs(int fd, u64 num_blocks, u32 blocksize) | 28 | int mkfs(int fd, u64 num_blocks, u32 blocksize) |
14 | { | 29 | { |
15 | struct btrfs_super_block super; | 30 | struct btrfs_super_block super; |
@@ -27,7 +42,7 @@ int mkfs(int fd, u64 num_blocks, u32 blocksize) | |||
27 | strcpy((char *)(&super.magic), BTRFS_MAGIC); | 42 | strcpy((char *)(&super.magic), BTRFS_MAGIC); |
28 | btrfs_set_super_blocksize(&super, blocksize); | 43 | btrfs_set_super_blocksize(&super, blocksize); |
29 | btrfs_set_super_total_blocks(&super, num_blocks); | 44 | btrfs_set_super_total_blocks(&super, num_blocks); |
30 | btrfs_set_super_blocks_used(&super, 0); | 45 | btrfs_set_super_blocks_used(&super, start_block + 5); |
31 | 46 | ||
32 | block = malloc(blocksize); | 47 | block = malloc(blocksize); |
33 | memset(block, 0, blocksize); | 48 | memset(block, 0, blocksize); |
@@ -160,3 +175,81 @@ int mkfs(int fd, u64 num_blocks, u32 blocksize) | |||
160 | return -1; | 175 | return -1; |
161 | return 0; | 176 | return 0; |
162 | } | 177 | } |
178 | |||
179 | u64 device_size(int fd, struct stat *st) | ||
180 | { | ||
181 | u64 size; | ||
182 | if (S_ISREG(st->st_mode)) { | ||
183 | return st->st_size; | ||
184 | } | ||
185 | if (!S_ISBLK(st->st_mode)) { | ||
186 | return 0; | ||
187 | } | ||
188 | if (ioctl(fd, BLKGETSIZE64, &size) >= 0) { | ||
189 | return size; | ||
190 | } | ||
191 | return 0; | ||
192 | } | ||
193 | |||
194 | int main(int ac, char **av) | ||
195 | { | ||
196 | char *file; | ||
197 | u64 block_count = 0; | ||
198 | int fd; | ||
199 | struct stat st; | ||
200 | int ret; | ||
201 | int i; | ||
202 | char *buf = malloc(4096); | ||
203 | if (ac >= 2) { | ||
204 | file = av[1]; | ||
205 | if (ac == 3) { | ||
206 | block_count = atoi(av[2]); | ||
207 | if (!block_count) { | ||
208 | fprintf(stderr, "error finding block count\n"); | ||
209 | exit(1); | ||
210 | } | ||
211 | } | ||
212 | } else { | ||
213 | fprintf(stderr, "usage: mkfs.btrfs file [block count]\n"); | ||
214 | exit(1); | ||
215 | } | ||
216 | fd = open(file, O_RDWR); | ||
217 | if (fd < 0) { | ||
218 | fprintf(stderr, "unable to open %s\n", file); | ||
219 | exit(1); | ||
220 | } | ||
221 | ret = fstat(fd, &st); | ||
222 | if (ret < 0) { | ||
223 | fprintf(stderr, "unable to stat %s\n", file); | ||
224 | exit(1); | ||
225 | } | ||
226 | if (block_count == 0) { | ||
227 | block_count = device_size(fd, &st); | ||
228 | if (block_count == 0) { | ||
229 | fprintf(stderr, "unable to find %s size\n", file); | ||
230 | exit(1); | ||
231 | } | ||
232 | } | ||
233 | block_count /= 4096; | ||
234 | if (block_count < 256) { | ||
235 | fprintf(stderr, "device %s is too small\n", file); | ||
236 | exit(1); | ||
237 | } | ||
238 | memset(buf, 0, 4096); | ||
239 | for(i = 0; i < 6; i++) { | ||
240 | ret = write(fd, buf, 4096); | ||
241 | if (ret != 4096) { | ||
242 | fprintf(stderr, "unable to zero fill device\n"); | ||
243 | exit(1); | ||
244 | } | ||
245 | } | ||
246 | ret = mkfs(fd, block_count, 4096); | ||
247 | if (ret) { | ||
248 | fprintf(stderr, "error during mkfs %d\n", ret); | ||
249 | exit(1); | ||
250 | } | ||
251 | printf("fs created on %s blocksize %d blocks %Lu\n", | ||
252 | file, 4096, block_count); | ||
253 | return 0; | ||
254 | } | ||
255 | |||