Remove extent back refs in batches, and avoid duplicate searches
[btrfs-progs-unstable/devel.git] / mkfs.c
blobc3a84c03b0b4bccea017cbd9e83b53fd7555500d
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #define _XOPEN_SOURCE 500
20 #ifndef __CHECKER__
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
23 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <uuid/uuid.h>
31 #include <linux/fs.h>
32 #include <ctype.h>
33 #include "kerncompat.h"
34 #include "ctree.h"
35 #include "disk-io.h"
36 #include "transaction.h"
37 #include "utils.h"
39 #ifdef __CHECKER__
40 #define BLKGETSIZE64 0
41 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
42 #endif
44 static u64 parse_size(char *s)
46 int len = strlen(s);
47 char c;
48 u64 mult = 1;
50 if (!isdigit(s[len - 1])) {
51 c = tolower(s[len - 1]);
52 switch (c) {
53 case 'g':
54 mult *= 1024;
55 case 'm':
56 mult *= 1024;
57 case 'k':
58 mult *= 1024;
59 case 'b':
60 break;
61 default:
62 fprintf(stderr, "Unknown size descriptor %c\n", c);
63 exit(1);
65 s[len - 1] = '\0';
67 return atol(s) * mult;
70 static int zero_blocks(int fd, off_t start, size_t len)
72 char *buf = malloc(len);
73 int ret = 0;
74 ssize_t written;
76 if (!buf)
77 return -ENOMEM;
78 memset(buf, 0, len);
79 written = pwrite(fd, buf, len, start);
80 if (written != len)
81 ret = -EIO;
82 free(buf);
83 return ret;
86 static int zero_dev_start(int fd)
88 off_t start = 0;
89 size_t len = 2 * 1024 * 1024;
91 #ifdef __sparc__
92 /* don't overwrite the disk labels on sparc */
93 start = 1024;
94 len -= 1024;
95 #endif
96 return zero_blocks(fd, start, len);
99 static int zero_dev_end(int fd, u64 dev_size)
101 size_t len = 2 * 1024 * 1024;
102 off_t start = dev_size - len;
104 return zero_blocks(fd, start, len);
107 static int make_root_dir(int fd) {
108 struct btrfs_root *root;
109 struct btrfs_trans_handle *trans;
110 struct btrfs_key location;
111 int ret;
113 root = open_ctree_fd(fd, 0);
115 if (!root) {
116 fprintf(stderr, "ctree init failed\n");
117 return -1;
119 trans = btrfs_start_transaction(root, 1);
120 ret = btrfs_make_block_groups(trans, root);
121 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
122 BTRFS_ROOT_TREE_DIR_OBJECTID);
123 if (ret)
124 goto err;
125 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
126 if (ret)
127 goto err;
128 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
129 location.offset = (u64)-1;
130 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
131 "default", 7,
132 btrfs_super_root_dir(&root->fs_info->super_copy),
133 &location, BTRFS_FT_DIR);
134 if (ret)
135 goto err;
137 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
138 "default", 7, location.objectid,
139 BTRFS_ROOT_TREE_DIR_OBJECTID);
140 if (ret)
141 goto err;
143 btrfs_commit_transaction(trans, root);
144 ret = close_ctree(root);
145 err:
146 return ret;
149 u64 device_size(int fd, struct stat *st)
151 u64 size;
152 if (S_ISREG(st->st_mode)) {
153 return st->st_size;
155 if (!S_ISBLK(st->st_mode)) {
156 return 0;
158 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
159 return size;
161 return 0;
164 static void print_usage(void)
166 fprintf(stderr, "usage: mkfs.btrfs [ -l leafsize ] [ -n nodesize] dev [ blocks ]\n");
167 exit(1);
170 int main(int ac, char **av)
172 char *file;
173 u64 block_count = 0;
174 int fd;
175 struct stat st;
176 int ret;
177 int i;
178 u32 leafsize = 16 * 1024;
179 u32 sectorsize = 4096;
180 u32 nodesize = 16 * 1024;
181 u32 stripesize = 4096;
182 u64 blocks[4];
183 int zero_end = 0;
185 while(1) {
186 int c;
187 c = getopt(ac, av, "l:n:s:");
188 if (c < 0)
189 break;
190 switch(c) {
191 case 'l':
192 leafsize = parse_size(optarg);
193 break;
194 case 'n':
195 nodesize = parse_size(optarg);
196 break;
197 case 's':
198 stripesize = parse_size(optarg);
199 break;
200 default:
201 print_usage();
204 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
205 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
206 exit(1);
208 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
209 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
210 exit(1);
212 ac = ac - optind;
213 if (ac >= 1) {
214 file = av[optind];
215 if (ac == 2) {
216 block_count = parse_size(av[optind + 1]);
217 if (!block_count) {
218 fprintf(stderr, "error finding block count\n");
219 exit(1);
222 } else {
223 print_usage();
225 fd = open(file, O_RDWR);
226 if (fd < 0) {
227 fprintf(stderr, "unable to open %s\n", file);
228 exit(1);
230 ret = fstat(fd, &st);
231 if (ret < 0) {
232 fprintf(stderr, "unable to stat %s\n", file);
233 exit(1);
235 if (block_count == 0) {
236 block_count = device_size(fd, &st);
237 if (block_count == 0) {
238 fprintf(stderr, "unable to find %s size\n", file);
239 exit(1);
241 zero_end = 1;
243 block_count /= sectorsize;
244 block_count *= sectorsize;
246 if (block_count < 256 * 1024 * 1024) {
247 fprintf(stderr, "device %s is too small\n", file);
248 exit(1);
250 ret = zero_dev_start(fd);
251 if (ret) {
252 fprintf(stderr, "failed to zero device start %d\n", ret);
253 exit(1);
256 if (zero_end) {
257 ret = zero_dev_end(fd, block_count);
258 if (ret) {
259 fprintf(stderr, "failed to zero device end %d\n", ret);
260 exit(1);
264 for (i = 0; i < 4; i++)
265 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
267 ret = make_btrfs(fd, blocks, block_count, nodesize, leafsize,
268 sectorsize, stripesize);
269 if (ret) {
270 fprintf(stderr, "error during mkfs %d\n", ret);
271 exit(1);
273 ret = make_root_dir(fd);
274 if (ret) {
275 fprintf(stderr, "failed to setup the root directory\n");
276 exit(1);
278 printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
279 file, nodesize, leafsize, sectorsize,
280 (unsigned long long)block_count);
281 return 0;