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
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
26 #include <sys/types.h>
30 #include <uuid/uuid.h>
31 #include "radix-tree.h"
33 #include "kerncompat.h"
36 #include "transaction.h"
39 #define BLKGETSIZE64 0
40 static inline int ioctl(int fd
, int define
, u64
*size
) { return 0; }
43 static int __make_root_dir(struct btrfs_trans_handle
*trans
,
44 struct btrfs_root
*root
, u64 objectid
)
48 struct btrfs_key inode_map
;
49 struct btrfs_inode_item inode_item
;
54 inode_map
.objectid
= objectid
;
56 btrfs_set_key_type(&inode_map
, BTRFS_INODE_ITEM_KEY
);
59 memset(&inode_item
, 0, sizeof(inode_item
));
60 btrfs_set_inode_generation(&inode_item
, root
->fs_info
->generation
);
61 btrfs_set_inode_size(&inode_item
, 6);
62 btrfs_set_inode_nlink(&inode_item
, 1);
63 btrfs_set_inode_nblocks(&inode_item
, 1);
64 btrfs_set_inode_mode(&inode_item
, S_IFDIR
| 0555);
66 if (root
->fs_info
->tree_root
== root
)
67 btrfs_set_super_root_dir(root
->fs_info
->disk_super
, objectid
);
69 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
72 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
73 &inode_map
, BTRFS_FT_DIR
);
76 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
77 &inode_map
, BTRFS_FT_DIR
);
80 btrfs_set_root_dirid(&root
->root_item
, objectid
);
86 static int make_block_groups(struct btrfs_trans_handle
*trans
,
87 struct btrfs_root
*root
)
89 u64 group_size_blocks
;
94 struct btrfs_block_group_cache
*cache
;
96 root
= root
->fs_info
->extent_root
;
97 /* first we bootstrap the things into cache */
98 group_size_blocks
= BTRFS_BLOCK_GROUP_SIZE
/ root
->blocksize
;
99 cache
= malloc(sizeof(*cache
));
100 cache
->key
.objectid
= 0;
101 cache
->key
.offset
= group_size_blocks
;
102 cache
->key
.flags
= 0;
103 btrfs_set_key_type(&cache
->key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
104 memset(&cache
->item
, 0, sizeof(cache
->item
));
105 btrfs_set_block_group_used(&cache
->item
,
106 btrfs_super_blocks_used(root
->fs_info
->disk_super
));
107 ret
= radix_tree_insert(&root
->fs_info
->block_group_radix
,
108 group_size_blocks
- 1, (void *)cache
);
111 total_blocks
= btrfs_super_total_blocks(root
->fs_info
->disk_super
);
112 cur_start
= group_size_blocks
;
113 while(cur_start
< total_blocks
) {
114 cache
= malloc(sizeof(*cache
));
115 cache
->key
.objectid
= cur_start
;
116 cache
->key
.offset
= group_size_blocks
;
117 cache
->key
.flags
= 0;
118 btrfs_set_key_type(&cache
->key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
119 memset(&cache
->item
, 0, sizeof(cache
->item
));
121 cache
->item
.flags
|= BTRFS_BLOCK_GROUP_DATA
;
123 ret
= radix_tree_insert(&root
->fs_info
->block_group_radix
,
124 cur_start
+ group_size_blocks
- 1,
127 cur_start
+= group_size_blocks
;
130 /* then insert all the items */
132 while(cur_start
< total_blocks
) {
133 cache
= radix_tree_lookup(&root
->fs_info
->block_group_radix
,
134 cur_start
+ group_size_blocks
- 1);
136 ret
= btrfs_insert_block_group(trans
, root
, &cache
->key
,
139 cur_start
+= group_size_blocks
;
144 static int make_root_dir(int fd
) {
145 struct btrfs_root
*root
;
146 struct btrfs_super_block super
;
147 struct btrfs_trans_handle
*trans
;
149 struct btrfs_key location
;
151 root
= open_ctree_fd(fd
, &super
);
154 fprintf(stderr
, "ctree init failed\n");
157 trans
= btrfs_start_transaction(root
, 1);
158 ret
= make_block_groups(trans
, root
);
159 ret
= __make_root_dir(trans
, root
->fs_info
->tree_root
,
160 BTRFS_ROOT_TREE_DIR_OBJECTID
);
163 ret
= __make_root_dir(trans
, root
, BTRFS_FIRST_FREE_OBJECTID
);
166 memcpy(&location
, &root
->fs_info
->fs_root
->root_key
, sizeof(location
));
167 location
.offset
= (u64
)-1;
168 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
169 "default", strlen("default"),
170 btrfs_super_root_dir(root
->fs_info
->disk_super
),
171 &location
, BTRFS_FT_DIR
);
174 btrfs_commit_transaction(trans
, root
, root
->fs_info
->disk_super
);
175 ret
= close_ctree(root
, &super
);
180 int mkfs(int fd
, char *pathname
, u64 num_blocks
, u32 blocksize
)
182 struct btrfs_super_block super
;
183 struct btrfs_leaf
*empty_leaf
;
184 struct btrfs_root_item root_item
;
185 struct btrfs_item item
;
186 struct btrfs_extent_item extent_item
;
187 struct btrfs_inode_item
*inode_item
;
191 u32 start_block
= BTRFS_SUPER_INFO_OFFSET
/ blocksize
;
193 btrfs_set_super_generation(&super
, 1);
194 btrfs_set_super_blocknr(&super
, start_block
);
195 btrfs_set_super_root(&super
, start_block
+ 1);
196 strcpy((char *)(&super
.magic
), BTRFS_MAGIC
);
197 btrfs_set_super_blocksize(&super
, blocksize
);
198 btrfs_set_super_total_blocks(&super
, num_blocks
);
199 btrfs_set_super_blocks_used(&super
, start_block
+ 4);
200 uuid_generate(super
.fsid
);
202 block
= malloc(blocksize
);
203 memset(block
, 0, blocksize
);
204 BUG_ON(sizeof(super
) > blocksize
);
205 memcpy(block
, &super
, sizeof(super
));
206 ret
= pwrite(fd
, block
, blocksize
, BTRFS_SUPER_INFO_OFFSET
);
207 BUG_ON(ret
!= blocksize
);
209 /* create the tree of root objects */
210 empty_leaf
= malloc(blocksize
);
211 memset(empty_leaf
, 0, blocksize
);
212 btrfs_set_header_blocknr(&empty_leaf
->header
, start_block
+ 1);
213 btrfs_set_header_nritems(&empty_leaf
->header
, 2);
214 btrfs_set_header_generation(&empty_leaf
->header
, 0);
215 btrfs_set_header_owner(&empty_leaf
->header
, BTRFS_ROOT_TREE_OBJECTID
);
216 memcpy(empty_leaf
->header
.fsid
, super
.fsid
,
217 sizeof(empty_leaf
->header
.fsid
));
219 /* create the items for the root tree */
220 inode_item
= &root_item
.inode
;
221 memset(inode_item
, 0, sizeof(*inode_item
));
222 btrfs_set_inode_generation(inode_item
, 1);
223 btrfs_set_inode_size(inode_item
, 3);
224 btrfs_set_inode_nlink(inode_item
, 1);
225 btrfs_set_inode_nblocks(inode_item
, 1);
226 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
228 btrfs_set_root_dirid(&root_item
, 0);
229 btrfs_set_root_refs(&root_item
, 1);
230 btrfs_set_disk_key_offset(&item
.key
, 0);
231 btrfs_set_disk_key_flags(&item
.key
, 0);
232 btrfs_set_item_size(&item
, sizeof(root_item
));
233 btrfs_set_disk_key_type(&item
.key
, BTRFS_ROOT_ITEM_KEY
);
235 itemoff
= __BTRFS_LEAF_DATA_SIZE(blocksize
) - sizeof(root_item
);
236 btrfs_set_root_blocknr(&root_item
, start_block
+ 2);
237 btrfs_set_item_offset(&item
, itemoff
);
238 btrfs_set_disk_key_objectid(&item
.key
, BTRFS_EXTENT_TREE_OBJECTID
);
239 memcpy(empty_leaf
->items
, &item
, sizeof(item
));
240 memcpy(btrfs_leaf_data(empty_leaf
) + itemoff
,
241 &root_item
, sizeof(root_item
));
243 btrfs_set_root_blocknr(&root_item
, start_block
+ 3);
244 itemoff
= itemoff
- sizeof(root_item
);
245 btrfs_set_item_offset(&item
, itemoff
);
246 btrfs_set_disk_key_objectid(&item
.key
, BTRFS_FS_TREE_OBJECTID
);
247 memcpy(empty_leaf
->items
+ 1, &item
, sizeof(item
));
248 memcpy(btrfs_leaf_data(empty_leaf
) + itemoff
,
249 &root_item
, sizeof(root_item
));
250 ret
= pwrite(fd
, empty_leaf
, blocksize
, (start_block
+ 1) * blocksize
);
252 /* create the items for the extent tree */
253 btrfs_set_header_blocknr(&empty_leaf
->header
, start_block
+ 2);
254 btrfs_set_header_nritems(&empty_leaf
->header
, 4);
256 /* item1, reserve blocks 0-16 */
257 btrfs_set_disk_key_objectid(&item
.key
, 0);
258 btrfs_set_disk_key_offset(&item
.key
, start_block
+ 1);
259 btrfs_set_disk_key_flags(&item
.key
, 0);
260 btrfs_set_disk_key_type(&item
.key
, BTRFS_EXTENT_ITEM_KEY
);
261 itemoff
= __BTRFS_LEAF_DATA_SIZE(blocksize
) -
262 sizeof(struct btrfs_extent_item
);
263 btrfs_set_item_offset(&item
, itemoff
);
264 btrfs_set_item_size(&item
, sizeof(struct btrfs_extent_item
));
265 btrfs_set_extent_refs(&extent_item
, 1);
266 btrfs_set_extent_owner(&extent_item
, BTRFS_ROOT_TREE_OBJECTID
);
267 memcpy(empty_leaf
->items
, &item
, sizeof(item
));
268 memcpy(btrfs_leaf_data(empty_leaf
) + btrfs_item_offset(&item
),
269 &extent_item
, btrfs_item_size(&item
));
271 /* item2, give block 17 to the root */
272 btrfs_set_disk_key_objectid(&item
.key
, start_block
+ 1);
273 btrfs_set_disk_key_offset(&item
.key
, 1);
274 itemoff
= itemoff
- sizeof(struct btrfs_extent_item
);
275 btrfs_set_item_offset(&item
, itemoff
);
276 memcpy(empty_leaf
->items
+ 1, &item
, sizeof(item
));
277 memcpy(btrfs_leaf_data(empty_leaf
) + btrfs_item_offset(&item
),
278 &extent_item
, btrfs_item_size(&item
));
280 /* item3, give block 18 to the extent root */
281 btrfs_set_disk_key_objectid(&item
.key
, start_block
+ 2);
282 btrfs_set_disk_key_offset(&item
.key
, 1);
283 itemoff
= itemoff
- sizeof(struct btrfs_extent_item
);
284 btrfs_set_item_offset(&item
, itemoff
);
285 memcpy(empty_leaf
->items
+ 2, &item
, sizeof(item
));
286 memcpy(btrfs_leaf_data(empty_leaf
) + btrfs_item_offset(&item
),
287 &extent_item
, btrfs_item_size(&item
));
289 /* item4, give block 19 to the FS root */
290 btrfs_set_disk_key_objectid(&item
.key
, start_block
+ 3);
291 btrfs_set_disk_key_offset(&item
.key
, 1);
292 itemoff
= itemoff
- sizeof(struct btrfs_extent_item
);
293 btrfs_set_item_offset(&item
, itemoff
);
294 memcpy(empty_leaf
->items
+ 3, &item
, sizeof(item
));
295 memcpy(btrfs_leaf_data(empty_leaf
) + btrfs_item_offset(&item
),
296 &extent_item
, btrfs_item_size(&item
));
297 ret
= pwrite(fd
, empty_leaf
, blocksize
, (start_block
+ 2) * blocksize
);
298 if (ret
!= blocksize
)
301 /* finally create the FS root */
302 btrfs_set_header_blocknr(&empty_leaf
->header
, start_block
+ 3);
303 btrfs_set_header_nritems(&empty_leaf
->header
, 0);
304 ret
= pwrite(fd
, empty_leaf
, blocksize
, (start_block
+ 3) * blocksize
);
305 if (ret
!= blocksize
)
310 u64
device_size(int fd
, struct stat
*st
)
313 if (S_ISREG(st
->st_mode
)) {
316 if (!S_ISBLK(st
->st_mode
)) {
319 if (ioctl(fd
, BLKGETSIZE64
, &size
) >= 0) {
324 int main(int ac
, char **av
)
332 char *buf
= malloc(4096);
340 block_count
= atoi(av
[2]);
342 fprintf(stderr
, "error finding block count\n");
347 fprintf(stderr
, "usage: mkfs.btrfs file [block count]\n");
350 fd
= open(file
, O_RDWR
);
352 fprintf(stderr
, "unable to open %s\n", file
);
355 ret
= fstat(fd
, &st
);
357 fprintf(stderr
, "unable to stat %s\n", file
);
360 if (block_count
== 0) {
361 block_count
= device_size(fd
, &st
);
362 if (block_count
== 0) {
363 fprintf(stderr
, "unable to find %s size\n", file
);
368 if (block_count
< 256) {
369 fprintf(stderr
, "device %s is too small\n", file
);
372 memset(buf
, 0, 4096);
373 for(i
= 0; i
< 64; i
++) {
374 ret
= write(fd
, buf
, 4096);
376 fprintf(stderr
, "unable to zero fill device\n");
380 realpath_name
= realpath(file
, NULL
);
381 ret
= mkfs(fd
, realpath_name
, block_count
, 4096);
383 fprintf(stderr
, "error during mkfs %d\n", ret
);
386 ret
= make_root_dir(fd
);
388 fprintf(stderr
, "failed to setup the root directory\n");
391 printf("fs created on %s blocksize %d blocks %llu\n",
392 file
, 4096, (unsigned long long)block_count
);