summaryrefslogtreecommitdiff
path: root/mkfs.c
blob: ffc6af183c0c9d5edeed0455b67fe367e8d6d239 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#define _XOPEN_SOURCE 500
#ifndef __CHECKER__
#include <sys/ioctl.h>
#include <sys/mount.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <linux/fs.h>
#include "kerncompat.h"
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"

#ifdef __CHECKER__
#define BLKGETSIZE64 0
static inline int ioctl(int fd, int define, u64 *size) { return 0; }
#endif

static int __make_root_dir(struct btrfs_trans_handle *trans,
			   struct btrfs_root *root, u64 objectid)
{
	int ret;
	char buf[8];
	struct btrfs_key inode_map;
	struct btrfs_inode_item inode_item;

	buf[0] = '.';
	buf[1] = '.';

	inode_map.objectid = objectid;
	btrfs_set_key_type(&inode_map, BTRFS_INODE_ITEM_KEY);
	inode_map.offset = 0;

	memset(&inode_item, 0, sizeof(inode_item));
	btrfs_set_inode_generation(&inode_item, root->fs_info->generation);
	btrfs_set_inode_size(&inode_item, 6);
	btrfs_set_inode_nlink(&inode_item, 1);
	btrfs_set_inode_nblocks(&inode_item, 1);
	btrfs_set_inode_mode(&inode_item, S_IFDIR | 0555);

	if (root->fs_info->tree_root == root)
		btrfs_set_super_root_dir(root->fs_info->disk_super, objectid);

	ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
	if (ret)
		goto error;
	ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
				    &inode_map, BTRFS_FT_DIR);
	if (ret)
		goto error;
	ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
				    &inode_map, BTRFS_FT_DIR);
	if (ret)
		goto error;
	btrfs_set_root_dirid(&root->root_item, objectid);
	ret = 0;
error:
	return ret;
}

static int make_block_groups(struct btrfs_trans_handle *trans,
			     struct btrfs_root *root)
{
	u64 group_size;
	u64 total_bytes;
	u64 cur_start;
	int ret;
	u64 nr = 0;
	struct btrfs_block_group_cache *cache;
	struct cache_tree *bg_cache = &root->fs_info->block_group_cache;

	root = root->fs_info->extent_root;

	/* first we bootstrap the things into cache */
	group_size = BTRFS_BLOCK_GROUP_SIZE;
	cache = malloc(sizeof(*cache));
	cache->key.objectid = 0;
	cache->key.offset = group_size;
	cache->cache.start = 0;
	cache->cache.size = group_size;
	btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);

	memset(&cache->item, 0, sizeof(cache->item));
	btrfs_set_block_group_used(&cache->item,
			   btrfs_super_bytes_used(root->fs_info->disk_super));
	ret = insert_existing_cache_extent(bg_cache, &cache->cache);
	BUG_ON(ret);

	total_bytes = btrfs_super_total_bytes(root->fs_info->disk_super);
	cur_start = group_size;
	while(cur_start < total_bytes) {
		cache = malloc(sizeof(*cache));
		cache->key.objectid = cur_start;
		cache->key.offset = group_size;
		cache->cache.start = cur_start;
		cache->cache.size = group_size;
		btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
		memset(&cache->item, 0, sizeof(cache->item));
		if (nr % 3)
			cache->item.flags |= BTRFS_BLOCK_GROUP_DATA;

		ret = insert_existing_cache_extent(bg_cache, &cache->cache);
		BUG_ON(ret);
		cur_start += group_size;
		nr++;
	}
	/* then insert all the items */
	cur_start = 0;
	while(cur_start < total_bytes) {
		struct cache_extent *ce;
		ce = find_first_cache_extent(bg_cache, cur_start);
		BUG_ON(!ce);
		cache = container_of(ce, struct btrfs_block_group_cache,
					cache);
		ret = btrfs_insert_block_group(trans, root, &cache->key,
					       &cache->item);
		BUG_ON(ret);
		cur_start += group_size;
	}
	return 0;
}

static int make_root_dir(int fd) {
	struct btrfs_root *root;
	struct btrfs_super_block super;
	struct btrfs_trans_handle *trans;
	int ret;
	struct btrfs_key location;

	root = open_ctree_fd(fd, &super);

	if (!root) {
		fprintf(stderr, "ctree init failed\n");
		return -1;
	}
	trans = btrfs_start_transaction(root, 1);
	ret = make_block_groups(trans, root);
	ret = __make_root_dir(trans, root->fs_info->tree_root,
			      BTRFS_ROOT_TREE_DIR_OBJECTID);
	if (ret)
		goto err;
	ret = __make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
	if (ret)
		goto err;
	memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
	location.offset = (u64)-1;
	ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
			"default", 7,
			btrfs_super_root_dir(root->fs_info->disk_super),
			&location, BTRFS_FT_DIR);
	if (ret)
		goto err;
	btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
	ret = close_ctree(root, &super);
err:
	return ret;
}

int mkfs(int fd, char *pathname, u64 num_bytes, u32 nodesize, u32 leafsize,
	 u32 sectorsize, u32 stripesize)
{
	struct btrfs_super_block super;
	struct btrfs_leaf *empty_leaf;
	struct btrfs_root_item root_item;
	struct btrfs_item item;
	struct btrfs_extent_item extent_item;
	struct btrfs_inode_item *inode_item;
	char *block;
	int ret;
	u32 itemoff;
	u32 start_block = BTRFS_SUPER_INFO_OFFSET;
	u32 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize;

	btrfs_set_super_generation(&super, 1);
	btrfs_set_super_bytenr(&super, start_block);
	btrfs_set_super_root_level(&super, 0);
	btrfs_set_super_root(&super, first_free);
	strcpy((char *)(&super.magic), BTRFS_MAGIC);

printf("blocksize is %d\n", leafsize);
	btrfs_set_super_sectorsize(&super, sectorsize);
	btrfs_set_super_leafsize(&super, leafsize);
	btrfs_set_super_nodesize(&super, nodesize);
	btrfs_set_super_stripesize(&super, stripesize);

	num_bytes = (num_bytes / sectorsize) * sectorsize;
	btrfs_set_super_total_bytes(&super, num_bytes);
	btrfs_set_super_bytes_used(&super, start_block + 3 * leafsize +
				   sectorsize);
	uuid_generate(super.fsid);

	block = malloc(sectorsize);
	memset(block, 0, sectorsize);
	BUG_ON(sizeof(super) > sectorsize);
	memcpy(block, &super, sizeof(super));
	ret = pwrite(fd, block, sectorsize, BTRFS_SUPER_INFO_OFFSET);
	BUG_ON(ret != sectorsize);

	/* create the tree of root objects */
	empty_leaf = malloc(leafsize);
	memset(empty_leaf, 0, leafsize);
	btrfs_set_header_bytenr(&empty_leaf->header, first_free);
	btrfs_set_header_nritems(&empty_leaf->header, 2);
	btrfs_set_header_generation(&empty_leaf->header, 1);
	btrfs_set_header_owner(&empty_leaf->header, BTRFS_ROOT_TREE_OBJECTID);
	memcpy(empty_leaf->header.fsid, super.fsid,
	       sizeof(empty_leaf->header.fsid));

	/* create the items for the root tree */
	inode_item = &root_item.inode;
	memset(inode_item, 0, sizeof(*inode_item));
	btrfs_set_inode_generation(inode_item, 1);
	btrfs_set_inode_size(inode_item, 3);
	btrfs_set_inode_nlink(inode_item, 1);
	btrfs_set_inode_nblocks(inode_item, 1);
	btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);

	// memset(&root_item, 0, sizeof(root_item));
	btrfs_set_root_dirid(&root_item, 0);
	btrfs_set_root_refs(&root_item, 1);
	btrfs_set_disk_key_offset(&item.key, 0);
	btrfs_set_item_size(&item, sizeof(root_item));
	btrfs_set_disk_key_type(&item.key, BTRFS_ROOT_ITEM_KEY);

	itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
	btrfs_set_root_bytenr(&root_item, first_free + leafsize);
	root_item.level = 0;
	btrfs_set_item_offset(&item, itemoff);
	btrfs_set_disk_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID);
	memcpy(empty_leaf->items, &item, sizeof(item));
	memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
		&root_item, sizeof(root_item));

	btrfs_set_root_bytenr(&root_item, first_free + leafsize * 2);
	btrfs_set_root_bytes_used(&root_item, 1);
	itemoff = itemoff - sizeof(root_item);
	btrfs_set_item_offset(&item, itemoff);
	btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
	memcpy(empty_leaf->items + 1, &item, sizeof(item));
	memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
		&root_item, sizeof(root_item));
	ret = pwrite(fd, empty_leaf, leafsize, first_free);

	/* create the items for the extent tree */
	btrfs_set_header_bytenr(&empty_leaf->header, first_free + leafsize);
	btrfs_set_header_nritems(&empty_leaf->header, 4);

	/* item1, reserve blocks 0-16 */
	btrfs_set_disk_key_objectid(&item.key, 0);
	btrfs_set_disk_key_offset(&item.key, first_free);
	btrfs_set_disk_key_type(&item.key, 0);
	btrfs_set_disk_key_type(&item.key, BTRFS_EXTENT_ITEM_KEY);
	itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
			sizeof(struct btrfs_extent_item);
	btrfs_set_item_offset(&item, itemoff);
	btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
	btrfs_set_extent_refs(&extent_item, 1);
	memcpy(empty_leaf->items, &item, sizeof(item));
	memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
		&extent_item, btrfs_item_size(&item));

	/* item2, give block 17 to the root */
	btrfs_set_disk_key_objectid(&item.key, first_free);
	btrfs_set_disk_key_offset(&item.key, leafsize);
	itemoff = itemoff - sizeof(struct btrfs_extent_item);
	btrfs_set_item_offset(&item, itemoff);
	memcpy(empty_leaf->items + 1, &item, sizeof(item));
	memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
		&extent_item, btrfs_item_size(&item));

	/* item3, give block 18 to the extent root */
	btrfs_set_disk_key_objectid(&item.key, first_free + leafsize);
	btrfs_set_disk_key_offset(&item.key, leafsize);
	itemoff = itemoff - sizeof(struct btrfs_extent_item);
	btrfs_set_item_offset(&item, itemoff);
	memcpy(empty_leaf->items + 2, &item, sizeof(item));
	memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
		&extent_item, btrfs_item_size(&item));

	/* item4, give block 19 to the FS root */
	btrfs_set_disk_key_objectid(&item.key, first_free + leafsize * 2);
	btrfs_set_disk_key_offset(&item.key, leafsize);
	itemoff = itemoff - sizeof(struct btrfs_extent_item);
	btrfs_set_item_offset(&item, itemoff);
	memcpy(empty_leaf->items + 3, &item, sizeof(item));
	memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
		&extent_item, btrfs_item_size(&item));
	ret = pwrite(fd, empty_leaf, leafsize, first_free + leafsize);
	if (ret != leafsize)
		return -1;

	/* finally create the FS root */
	btrfs_set_header_bytenr(&empty_leaf->header, first_free + leafsize * 2);
	btrfs_set_header_nritems(&empty_leaf->header, 0);
	ret = pwrite(fd, empty_leaf, leafsize, first_free + leafsize * 2);
	if (ret != leafsize)
		return -1;
	return 0;
}

u64 device_size(int fd, struct stat *st)
{
	u64 size;
	if (S_ISREG(st->st_mode)) {
		return st->st_size;
	}
	if (!S_ISBLK(st->st_mode)) {
		return 0;
	}
	if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
		return size;
	}
	return 0;
}

static void print_usage(void)
{
	fprintf(stderr, "usage: mkfs.btrfs [ -l leafsize ] [ -n nodesize] dev [ blocks ]\n");
	exit(1);
}

int main(int ac, char **av)
{
	char *file;
	u64 block_count = 0;
	int fd;
	struct stat st;
	int ret;
	int i;
	u32 leafsize = 8 * 1024;
	u32 sectorsize = 4096;
	u32 nodesize = 8 * 1024;
	u32 stripesize = 4096;
	char *buf = malloc(sectorsize);
	char *realpath_name;

	while(1) {
		int c;
		c = getopt(ac, av, "l:n:s:");
		if (c < 0)
			break;
		switch(c) {
			case 'l':
				leafsize = atol(optarg);
				break;
			case 'n':
				nodesize = atol(optarg);
				break;
			case 's':
				stripesize = atol(optarg);
				break;
			default:
				print_usage();
		}
	}
	if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
		fprintf(stderr, "Illegal leafsize %u\n", leafsize);
		exit(1);
	}
	if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
		fprintf(stderr, "Illegal nodesize %u\n", nodesize);
		exit(1);
	}
	ac = ac - optind;
	if (ac >= 1) {
		file = av[optind];
		if (ac == 2) {
			block_count = atol(av[optind + 1]);
			if (!block_count) {
				fprintf(stderr, "error finding block count\n");
				exit(1);
			}
		}
	} else {
		print_usage();
	}
	fd = open(file, O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "unable to open %s\n", file);
		exit(1);
	}
	ret = fstat(fd, &st);
	if (ret < 0) {
		fprintf(stderr, "unable to stat %s\n", file);
		exit(1);
	}
	if (block_count == 0) {
		block_count = device_size(fd, &st);
		if (block_count == 0) {
			fprintf(stderr, "unable to find %s size\n", file);
			exit(1);
		}
		block_count /= sectorsize;
	}
	if (block_count < 256) {
		fprintf(stderr, "device %s is too small\n", file);
		exit(1);
	}
	block_count = block_count * sectorsize;
	memset(buf, 0, sectorsize);
	for(i = 0; i < 64; i++) {
		ret = write(fd, buf, sectorsize);
		if (ret != sectorsize) {
			fprintf(stderr, "unable to zero fill device\n");
			exit(1);
		}
	}
	realpath_name = realpath(file, NULL);
	ret = mkfs(fd, realpath_name, block_count, nodesize, leafsize,
		   sectorsize, stripesize);
	if (ret) {
		fprintf(stderr, "error during mkfs %d\n", ret);
		exit(1);
	}
	ret = make_root_dir(fd);
	if (ret) {
		fprintf(stderr, "failed to setup the root directory\n");
		exit(1);
	}
	printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
	       file, nodesize, leafsize, sectorsize,
	       (unsigned long long)block_count);
	return 0;
}