2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
18 #include <uuid/uuid.h>
19 #include <blkid/blkid.h>
26 #include "mkfs/common.h"
28 static u64 reference_root_table[] = {
29 [1] = BTRFS_ROOT_TREE_OBJECTID,
30 [2] = BTRFS_EXTENT_TREE_OBJECTID,
31 [3] = BTRFS_CHUNK_TREE_OBJECTID,
32 [4] = BTRFS_DEV_TREE_OBJECTID,
33 [5] = BTRFS_FS_TREE_OBJECTID,
34 [6] = BTRFS_CSUM_TREE_OBJECTID,
37 static int btrfs_create_tree_root(int fd, struct btrfs_mkfs_config *cfg,
38 struct extent_buffer *buf)
40 struct btrfs_root_item root_item;
41 struct btrfs_inode_item *inode_item;
42 struct btrfs_disk_key disk_key;
48 memset(buf->data + sizeof(struct btrfs_header), 0,
49 cfg->nodesize - sizeof(struct btrfs_header));
50 memset(&root_item, 0, sizeof(root_item));
51 memset(&disk_key, 0, sizeof(disk_key));
53 /* create the items for the root tree */
54 inode_item = &root_item.inode;
55 btrfs_set_stack_inode_generation(inode_item, 1);
56 btrfs_set_stack_inode_size(inode_item, 3);
57 btrfs_set_stack_inode_nlink(inode_item, 1);
58 btrfs_set_stack_inode_nbytes(inode_item, cfg->nodesize);
59 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
60 btrfs_set_root_refs(&root_item, 1);
61 btrfs_set_root_used(&root_item, cfg->nodesize);
62 btrfs_set_root_generation(&root_item, 1);
64 btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
65 btrfs_set_disk_key_offset(&disk_key, 0);
66 itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - sizeof(root_item);
68 for (blk = 0; blk < MKFS_BLOCK_COUNT; blk++) {
69 if (blk == MKFS_SUPER_BLOCK || blk == MKFS_ROOT_TREE
70 || blk == MKFS_CHUNK_TREE)
73 btrfs_set_root_bytenr(&root_item, cfg->blocks[blk]);
74 btrfs_set_disk_key_objectid(&disk_key,
75 reference_root_table[blk]);
76 btrfs_set_item_key(buf, &disk_key, nritems);
77 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
78 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
80 write_extent_buffer(buf, &root_item,
81 btrfs_item_ptr_offset(buf, nritems),
84 itemoff -= sizeof(root_item);
87 /* generate checksum */
88 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
90 /* write back root tree */
91 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_ROOT_TREE]);
92 if (ret != cfg->nodesize)
93 return (ret < 0 ? -errno : -EIO);
99 * @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
101 * The superblock signature is not valid, denotes a partially created
102 * filesystem, needs to be finalized.
104 int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
106 struct btrfs_super_block super;
107 struct extent_buffer *buf;
108 struct btrfs_disk_key disk_key;
109 struct btrfs_extent_item *extent_item;
110 struct btrfs_chunk *chunk;
111 struct btrfs_dev_item *dev_item;
112 struct btrfs_dev_extent *dev_extent;
113 u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
123 int skinny_metadata = !!(cfg->features &
124 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
127 buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
131 first_free = BTRFS_SUPER_INFO_OFFSET + cfg->sectorsize * 2 - 1;
132 first_free &= ~((u64)cfg->sectorsize - 1);
134 memset(&super, 0, sizeof(super));
136 num_bytes = (cfg->num_bytes / cfg->sectorsize) * cfg->sectorsize;
138 if (uuid_parse(cfg->fs_uuid, super.fsid) != 0) {
139 error("cannot not parse UUID: %s", cfg->fs_uuid);
143 if (!test_uuid_unique(cfg->fs_uuid)) {
144 error("non-unique UUID: %s", cfg->fs_uuid);
149 uuid_generate(super.fsid);
150 uuid_unparse(super.fsid, cfg->fs_uuid);
152 uuid_generate(super.dev_item.uuid);
153 uuid_generate(chunk_tree_uuid);
155 cfg->blocks[MKFS_SUPER_BLOCK] = BTRFS_SUPER_INFO_OFFSET;
156 for (i = 1; i < MKFS_BLOCK_COUNT; i++) {
157 cfg->blocks[i] = BTRFS_SUPER_INFO_OFFSET + SZ_1M +
161 btrfs_set_super_bytenr(&super, cfg->blocks[MKFS_SUPER_BLOCK]);
162 btrfs_set_super_num_devices(&super, 1);
163 btrfs_set_super_magic(&super, BTRFS_MAGIC_PARTIAL);
164 btrfs_set_super_generation(&super, 1);
165 btrfs_set_super_root(&super, cfg->blocks[MKFS_ROOT_TREE]);
166 btrfs_set_super_chunk_root(&super, cfg->blocks[MKFS_CHUNK_TREE]);
167 btrfs_set_super_total_bytes(&super, num_bytes);
168 btrfs_set_super_bytes_used(&super, 6 * cfg->nodesize);
169 btrfs_set_super_sectorsize(&super, cfg->sectorsize);
170 super.__unused_leafsize = cpu_to_le32(cfg->nodesize);
171 btrfs_set_super_nodesize(&super, cfg->nodesize);
172 btrfs_set_super_stripesize(&super, cfg->stripesize);
173 btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
174 btrfs_set_super_chunk_root_generation(&super, 1);
175 btrfs_set_super_cache_generation(&super, -1);
176 btrfs_set_super_incompat_flags(&super, cfg->features);
178 __strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE - 1);
180 /* create the tree of root objects */
181 memset(buf->data, 0, cfg->nodesize);
182 buf->len = cfg->nodesize;
183 btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_ROOT_TREE]);
184 btrfs_set_header_nritems(buf, 4);
185 btrfs_set_header_generation(buf, 1);
186 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
187 btrfs_set_header_owner(buf, BTRFS_ROOT_TREE_OBJECTID);
188 write_extent_buffer(buf, super.fsid, btrfs_header_fsid(),
191 write_extent_buffer(buf, chunk_tree_uuid,
192 btrfs_header_chunk_tree_uuid(buf),
195 ret = btrfs_create_tree_root(fd, cfg, buf);
199 /* create the items for the extent tree */
200 memset(buf->data + sizeof(struct btrfs_header), 0,
201 cfg->nodesize - sizeof(struct btrfs_header));
203 itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize);
204 for (i = 1; i < MKFS_BLOCK_COUNT; i++) {
205 item_size = sizeof(struct btrfs_extent_item);
206 if (!skinny_metadata)
207 item_size += sizeof(struct btrfs_tree_block_info);
209 if (cfg->blocks[i] < first_free) {
210 error("block[%d] below first free: %llu < %llu",
211 i, (unsigned long long)cfg->blocks[i],
212 (unsigned long long)first_free);
216 if (cfg->blocks[i] < cfg->blocks[i - 1]) {
217 error("blocks %d and %d in reverse order: %llu < %llu",
219 (unsigned long long)cfg->blocks[i],
220 (unsigned long long)cfg->blocks[i - 1]);
225 /* create extent item */
226 itemoff -= item_size;
227 btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[i]);
228 if (skinny_metadata) {
229 btrfs_set_disk_key_type(&disk_key,
230 BTRFS_METADATA_ITEM_KEY);
231 btrfs_set_disk_key_offset(&disk_key, 0);
233 btrfs_set_disk_key_type(&disk_key,
234 BTRFS_EXTENT_ITEM_KEY);
235 btrfs_set_disk_key_offset(&disk_key, cfg->nodesize);
237 btrfs_set_item_key(buf, &disk_key, nritems);
238 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
240 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
242 extent_item = btrfs_item_ptr(buf, nritems,
243 struct btrfs_extent_item);
244 btrfs_set_extent_refs(buf, extent_item, 1);
245 btrfs_set_extent_generation(buf, extent_item, 1);
246 btrfs_set_extent_flags(buf, extent_item,
247 BTRFS_EXTENT_FLAG_TREE_BLOCK);
250 /* create extent ref */
251 ref_root = reference_root_table[i];
252 btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[i]);
253 btrfs_set_disk_key_offset(&disk_key, ref_root);
254 btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
255 btrfs_set_item_key(buf, &disk_key, nritems);
256 btrfs_set_item_offset(buf, btrfs_item_nr(nritems),
258 btrfs_set_item_size(buf, btrfs_item_nr(nritems), 0);
261 btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_EXTENT_TREE]);
262 btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
263 btrfs_set_header_nritems(buf, nritems);
264 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
265 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_EXTENT_TREE]);
266 if (ret != cfg->nodesize) {
267 ret = (ret < 0 ? -errno : -EIO);
271 /* create the chunk tree */
272 memset(buf->data + sizeof(struct btrfs_header), 0,
273 cfg->nodesize - sizeof(struct btrfs_header));
275 item_size = sizeof(*dev_item);
276 itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - item_size;
278 /* first device 1 (there is no device 0) */
279 btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_ITEMS_OBJECTID);
280 btrfs_set_disk_key_offset(&disk_key, 1);
281 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_ITEM_KEY);
282 btrfs_set_item_key(buf, &disk_key, nritems);
283 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
284 btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
286 dev_item = btrfs_item_ptr(buf, nritems, struct btrfs_dev_item);
287 btrfs_set_device_id(buf, dev_item, 1);
288 btrfs_set_device_generation(buf, dev_item, 0);
289 btrfs_set_device_total_bytes(buf, dev_item, num_bytes);
290 btrfs_set_device_bytes_used(buf, dev_item,
291 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
292 btrfs_set_device_io_align(buf, dev_item, cfg->sectorsize);
293 btrfs_set_device_io_width(buf, dev_item, cfg->sectorsize);
294 btrfs_set_device_sector_size(buf, dev_item, cfg->sectorsize);
295 btrfs_set_device_type(buf, dev_item, 0);
297 write_extent_buffer(buf, super.dev_item.uuid,
298 (unsigned long)btrfs_device_uuid(dev_item),
300 write_extent_buffer(buf, super.fsid,
301 (unsigned long)btrfs_device_fsid(dev_item),
303 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
307 item_size = btrfs_chunk_item_size(1);
308 itemoff = itemoff - item_size;
310 /* then we have chunk 0 */
311 btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
312 btrfs_set_disk_key_offset(&disk_key, 0);
313 btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
314 btrfs_set_item_key(buf, &disk_key, nritems);
315 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
316 btrfs_set_item_size(buf, btrfs_item_nr(nritems), item_size);
318 chunk = btrfs_item_ptr(buf, nritems, struct btrfs_chunk);
319 btrfs_set_chunk_length(buf, chunk, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
320 btrfs_set_chunk_owner(buf, chunk, BTRFS_EXTENT_TREE_OBJECTID);
321 btrfs_set_chunk_stripe_len(buf, chunk, BTRFS_STRIPE_LEN);
322 btrfs_set_chunk_type(buf, chunk, BTRFS_BLOCK_GROUP_SYSTEM);
323 btrfs_set_chunk_io_align(buf, chunk, cfg->sectorsize);
324 btrfs_set_chunk_io_width(buf, chunk, cfg->sectorsize);
325 btrfs_set_chunk_sector_size(buf, chunk, cfg->sectorsize);
326 btrfs_set_chunk_num_stripes(buf, chunk, 1);
327 btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
328 btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
331 write_extent_buffer(buf, super.dev_item.uuid,
332 (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
335 /* copy the key for the chunk to the system array */
336 ptr = super.sys_chunk_array;
337 array_size = sizeof(disk_key);
339 memcpy(ptr, &disk_key, sizeof(disk_key));
340 ptr += sizeof(disk_key);
342 /* copy the chunk to the system array */
343 read_extent_buffer(buf, ptr, (unsigned long)chunk, item_size);
344 array_size += item_size;
346 btrfs_set_super_sys_array_size(&super, array_size);
348 btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CHUNK_TREE]);
349 btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
350 btrfs_set_header_nritems(buf, nritems);
351 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
352 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CHUNK_TREE]);
353 if (ret != cfg->nodesize) {
354 ret = (ret < 0 ? -errno : -EIO);
358 /* create the device tree */
359 memset(buf->data + sizeof(struct btrfs_header), 0,
360 cfg->nodesize - sizeof(struct btrfs_header));
362 itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) -
363 sizeof(struct btrfs_dev_extent);
365 btrfs_set_disk_key_objectid(&disk_key, 1);
366 btrfs_set_disk_key_offset(&disk_key, 0);
367 btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
368 btrfs_set_item_key(buf, &disk_key, nritems);
369 btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
370 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
371 sizeof(struct btrfs_dev_extent));
372 dev_extent = btrfs_item_ptr(buf, nritems, struct btrfs_dev_extent);
373 btrfs_set_dev_extent_chunk_tree(buf, dev_extent,
374 BTRFS_CHUNK_TREE_OBJECTID);
375 btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
376 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
377 btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
379 write_extent_buffer(buf, chunk_tree_uuid,
380 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
383 btrfs_set_dev_extent_length(buf, dev_extent,
384 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
387 btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_DEV_TREE]);
388 btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
389 btrfs_set_header_nritems(buf, nritems);
390 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
391 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_DEV_TREE]);
392 if (ret != cfg->nodesize) {
393 ret = (ret < 0 ? -errno : -EIO);
397 /* create the FS root */
398 memset(buf->data + sizeof(struct btrfs_header), 0,
399 cfg->nodesize - sizeof(struct btrfs_header));
400 btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_FS_TREE]);
401 btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
402 btrfs_set_header_nritems(buf, 0);
403 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
404 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_FS_TREE]);
405 if (ret != cfg->nodesize) {
406 ret = (ret < 0 ? -errno : -EIO);
409 /* finally create the csum root */
410 memset(buf->data + sizeof(struct btrfs_header), 0,
411 cfg->nodesize - sizeof(struct btrfs_header));
412 btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CSUM_TREE]);
413 btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
414 btrfs_set_header_nritems(buf, 0);
415 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
416 ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CSUM_TREE]);
417 if (ret != cfg->nodesize) {
418 ret = (ret < 0 ? -errno : -EIO);
422 /* and write out the super block */
423 memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
424 memcpy(buf->data, &super, sizeof(super));
425 buf->len = BTRFS_SUPER_INFO_SIZE;
426 csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
427 ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
428 cfg->blocks[MKFS_SUPER_BLOCK]);
429 if (ret != BTRFS_SUPER_INFO_SIZE) {
430 ret = (ret < 0 ? -errno : -EIO);
441 u64 btrfs_min_dev_size(u32 nodesize)
443 return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE +
444 btrfs_min_global_blk_rsv_size(nodesize));
448 * Btrfs minimum size calculation is complicated, it should include at least:
449 * 1. system group size
450 * 2. minimum global block reserve
451 * 3. metadata used at mkfs
452 * 4. space reservation to create uuid for first mount.
453 * Also, raid factor should also be taken into consideration.
454 * To avoid the overkill calculation, (system group + global block rsv) * 2
455 * for *EACH* device should be good enough.
457 u64 btrfs_min_global_blk_rsv_size(u32 nodesize)
459 return (u64)nodesize << 10;
462 #define isoctal(c) (((c) & ~7) == '0')
464 static inline void translate(char *f, char *t)
468 isoctal(f[1]) && isoctal(f[2]) && isoctal(f[3])) {
469 *t++ = 64*(f[1] & 7) + 8*(f[2] & 7) + (f[3] & 7);
479 * Checks if the swap device.
480 * Returns 1 if swap device, < 0 on error or 0 if not swap device.
482 static int is_swap_device(const char *file)
493 if (stat(file, &st_buf) < 0)
495 if (S_ISBLK(st_buf.st_mode))
496 dev = st_buf.st_rdev;
497 else if (S_ISREG(st_buf.st_mode)) {
503 if ((f = fopen("/proc/swaps", "r")) == NULL)
506 /* skip the first line */
507 if (fgets(tmp, sizeof(tmp), f) == NULL)
510 while (fgets(tmp, sizeof(tmp), f) != NULL) {
511 if ((cp = strchr(tmp, ' ')) != NULL)
513 if ((cp = strchr(tmp, '\t')) != NULL)
516 if (stat(buf, &st_buf) != 0)
518 if (S_ISBLK(st_buf.st_mode)) {
519 if (dev == st_buf.st_rdev) {
523 } else if (S_ISREG(st_buf.st_mode)) {
524 if (dev == st_buf.st_dev && ino == st_buf.st_ino) {
538 * Check for existing filesystem or partition table on device.
540 * 1 for existing fs or partition
541 * 0 for nothing found
542 * -1 for internal error
544 static int check_overwrite(const char *device)
547 blkid_probe pr = NULL;
551 if (!device || !*device)
554 ret = -1; /* will reset on success of all setup calls */
556 pr = blkid_new_probe_from_filename(device);
560 size = blkid_probe_get_size(pr);
564 /* nothing to overwrite on a 0-length device */
570 ret = blkid_probe_enable_partitions(pr, 1);
574 ret = blkid_do_fullprobe(pr);
579 * Blkid returns 1 for nothing found and 0 when it finds a signature,
580 * but we want the exact opposite, so reverse the return value here.
582 * In addition print some useful diagnostics about what actually is
590 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
592 "%s appears to contain an existing "
593 "filesystem (%s).\n", device, type);
594 } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
596 "%s appears to contain a partition "
597 "table (%s).\n", device, type);
600 "%s appears to contain something weird "
601 "according to blkid\n", device);
607 blkid_free_probe(pr);
610 "probe of %s failed, cannot detect "
611 "existing filesystem.\n", device);
616 * Check if a device is suitable for btrfs
618 * 1: something is wrong, an error is printed
621 int test_dev_for_mkfs(const char *file, int force_overwrite)
626 ret = is_swap_device(file);
628 error("checking status of %s: %s", file, strerror(-ret));
632 error("%s is a swap device", file);
635 if (!force_overwrite) {
636 if (check_overwrite(file)) {
637 error("use the -f option to force overwrite of %s",
642 ret = check_mounted(file);
644 error("cannot check mount status of %s: %s", file,
649 error("%s is mounted", file);
652 /* check if the device is busy */
653 fd = open(file, O_RDWR|O_EXCL);
655 error("unable to open %s: %s", file, strerror(errno));
658 if (fstat(fd, &st)) {
659 error("unable to stat %s: %s", file, strerror(errno));
663 if (!S_ISBLK(st.st_mode)) {
664 error("%s is not a block device", file);
672 int is_vol_small(const char *file)
679 fd = open(file, O_RDONLY);
682 if (fstat(fd, &st) < 0) {
687 size = btrfs_device_size(fd, &st);
692 if (size < BTRFS_MKFS_SMALL_VOLUME_SIZE) {
701 int test_minimum_size(const char *file, u64 min_dev_size)
706 fd = open(file, O_RDONLY);
709 if (stat(file, &statbuf) < 0) {
713 if (btrfs_device_size(fd, &statbuf) < min_dev_size) {