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
;
47 u8 uuid
[BTRFS_UUID_SIZE
];
49 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
50 cfg
->nodesize
- sizeof(struct btrfs_header
));
51 memset(&root_item
, 0, sizeof(root_item
));
52 memset(&disk_key
, 0, sizeof(disk_key
));
54 /* create the items for the root tree */
55 inode_item
= &root_item
.inode
;
56 btrfs_set_stack_inode_generation(inode_item
, 1);
57 btrfs_set_stack_inode_size(inode_item
, 3);
58 btrfs_set_stack_inode_nlink(inode_item
, 1);
59 btrfs_set_stack_inode_nbytes(inode_item
, cfg
->nodesize
);
60 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
61 btrfs_set_root_refs(&root_item
, 1);
62 btrfs_set_root_used(&root_item
, cfg
->nodesize
);
63 btrfs_set_root_generation(&root_item
, 1);
65 btrfs_set_disk_key_type(&disk_key
, BTRFS_ROOT_ITEM_KEY
);
66 btrfs_set_disk_key_offset(&disk_key
, 0);
67 itemoff
= __BTRFS_LEAF_DATA_SIZE(cfg
->nodesize
) - sizeof(root_item
);
69 for (blk
= 0; blk
< MKFS_BLOCK_COUNT
; blk
++) {
70 if (blk
== MKFS_SUPER_BLOCK
|| blk
== MKFS_ROOT_TREE
71 || blk
== MKFS_CHUNK_TREE
)
74 btrfs_set_root_bytenr(&root_item
, cfg
->blocks
[blk
]);
75 btrfs_set_disk_key_objectid(&disk_key
,
76 reference_root_table
[blk
]);
77 btrfs_set_item_key(buf
, &disk_key
, nritems
);
78 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
79 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
81 if (blk
== MKFS_FS_TREE
) {
82 time_t now
= time(NULL
);
85 memcpy(root_item
.uuid
, uuid
, BTRFS_UUID_SIZE
);
86 btrfs_set_stack_timespec_sec(&root_item
.otime
, now
);
87 btrfs_set_stack_timespec_sec(&root_item
.ctime
, now
);
89 memset(uuid
, 0, BTRFS_UUID_SIZE
);
90 memcpy(root_item
.uuid
, uuid
, BTRFS_UUID_SIZE
);
91 btrfs_set_stack_timespec_sec(&root_item
.otime
, 0);
92 btrfs_set_stack_timespec_sec(&root_item
.ctime
, 0);
94 write_extent_buffer(buf
, &root_item
,
95 btrfs_item_ptr_offset(buf
, nritems
),
98 itemoff
-= sizeof(root_item
);
101 /* generate checksum */
102 csum_tree_block_size(buf
, btrfs_csum_sizes
[BTRFS_CSUM_TYPE_CRC32
], 0);
104 /* write back root tree */
105 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[MKFS_ROOT_TREE
]);
106 if (ret
!= cfg
->nodesize
)
107 return (ret
< 0 ? -errno
: -EIO
);
113 * @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
115 * The superblock signature is not valid, denotes a partially created
116 * filesystem, needs to be finalized.
118 * The temporary fs will have the following chunk layout:
121 * | Reserved | dev extent for SYS chunk |
123 * And chunk mapping will be:
126 * | | System chunk, 1:1 mapped |
128 * That's to say, there will only be *ONE* system chunk, mapped to
129 * [1M, 5M) physical offset.
130 * And the only chunk is also in logical address [1M, 5M), containing
131 * all essential tree blocks.
133 int make_btrfs(int fd
, struct btrfs_mkfs_config
*cfg
)
135 struct btrfs_super_block super
;
136 struct extent_buffer
*buf
;
137 struct btrfs_disk_key disk_key
;
138 struct btrfs_extent_item
*extent_item
;
139 struct btrfs_chunk
*chunk
;
140 struct btrfs_dev_item
*dev_item
;
141 struct btrfs_dev_extent
*dev_extent
;
142 u8 chunk_tree_uuid
[BTRFS_UUID_SIZE
];
152 int skinny_metadata
= !!(cfg
->features
&
153 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA
);
156 buf
= malloc(sizeof(*buf
) + max(cfg
->sectorsize
, cfg
->nodesize
));
160 first_free
= BTRFS_SUPER_INFO_OFFSET
+ cfg
->sectorsize
* 2 - 1;
161 first_free
&= ~((u64
)cfg
->sectorsize
- 1);
163 memset(&super
, 0, sizeof(super
));
165 num_bytes
= (cfg
->num_bytes
/ cfg
->sectorsize
) * cfg
->sectorsize
;
167 if (uuid_parse(cfg
->fs_uuid
, super
.fsid
) != 0) {
168 error("cannot not parse UUID: %s", cfg
->fs_uuid
);
172 if (!test_uuid_unique(cfg
->fs_uuid
)) {
173 error("non-unique UUID: %s", cfg
->fs_uuid
);
178 uuid_generate(super
.fsid
);
179 uuid_unparse(super
.fsid
, cfg
->fs_uuid
);
181 uuid_generate(super
.dev_item
.uuid
);
182 uuid_generate(chunk_tree_uuid
);
184 cfg
->blocks
[MKFS_SUPER_BLOCK
] = BTRFS_SUPER_INFO_OFFSET
;
185 for (i
= 1; i
< MKFS_BLOCK_COUNT
; i
++) {
186 cfg
->blocks
[i
] = BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
+
187 cfg
->nodesize
* (i
- 1);
190 btrfs_set_super_bytenr(&super
, cfg
->blocks
[MKFS_SUPER_BLOCK
]);
191 btrfs_set_super_num_devices(&super
, 1);
192 btrfs_set_super_magic(&super
, BTRFS_MAGIC_TEMPORARY
);
193 btrfs_set_super_generation(&super
, 1);
194 btrfs_set_super_root(&super
, cfg
->blocks
[MKFS_ROOT_TREE
]);
195 btrfs_set_super_chunk_root(&super
, cfg
->blocks
[MKFS_CHUNK_TREE
]);
196 btrfs_set_super_total_bytes(&super
, num_bytes
);
197 btrfs_set_super_bytes_used(&super
, 6 * cfg
->nodesize
);
198 btrfs_set_super_sectorsize(&super
, cfg
->sectorsize
);
199 super
.__unused_leafsize
= cpu_to_le32(cfg
->nodesize
);
200 btrfs_set_super_nodesize(&super
, cfg
->nodesize
);
201 btrfs_set_super_stripesize(&super
, cfg
->stripesize
);
202 btrfs_set_super_csum_type(&super
, BTRFS_CSUM_TYPE_CRC32
);
203 btrfs_set_super_chunk_root_generation(&super
, 1);
204 btrfs_set_super_cache_generation(&super
, -1);
205 btrfs_set_super_incompat_flags(&super
, cfg
->features
);
207 __strncpy_null(super
.label
, cfg
->label
, BTRFS_LABEL_SIZE
- 1);
209 /* create the tree of root objects */
210 memset(buf
->data
, 0, cfg
->nodesize
);
211 buf
->len
= cfg
->nodesize
;
212 btrfs_set_header_bytenr(buf
, cfg
->blocks
[MKFS_ROOT_TREE
]);
213 btrfs_set_header_nritems(buf
, 4);
214 btrfs_set_header_generation(buf
, 1);
215 btrfs_set_header_backref_rev(buf
, BTRFS_MIXED_BACKREF_REV
);
216 btrfs_set_header_owner(buf
, BTRFS_ROOT_TREE_OBJECTID
);
217 write_extent_buffer(buf
, super
.fsid
, btrfs_header_fsid(),
220 write_extent_buffer(buf
, chunk_tree_uuid
,
221 btrfs_header_chunk_tree_uuid(buf
),
224 ret
= btrfs_create_tree_root(fd
, cfg
, buf
);
228 /* create the items for the extent tree */
229 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
230 cfg
->nodesize
- sizeof(struct btrfs_header
));
232 itemoff
= __BTRFS_LEAF_DATA_SIZE(cfg
->nodesize
);
233 for (i
= 1; i
< MKFS_BLOCK_COUNT
; i
++) {
234 item_size
= sizeof(struct btrfs_extent_item
);
235 if (!skinny_metadata
)
236 item_size
+= sizeof(struct btrfs_tree_block_info
);
238 if (cfg
->blocks
[i
] < first_free
) {
239 error("block[%d] below first free: %llu < %llu",
240 i
, (unsigned long long)cfg
->blocks
[i
],
241 (unsigned long long)first_free
);
245 if (cfg
->blocks
[i
] < cfg
->blocks
[i
- 1]) {
246 error("blocks %d and %d in reverse order: %llu < %llu",
248 (unsigned long long)cfg
->blocks
[i
],
249 (unsigned long long)cfg
->blocks
[i
- 1]);
254 /* create extent item */
255 itemoff
-= item_size
;
256 btrfs_set_disk_key_objectid(&disk_key
, cfg
->blocks
[i
]);
257 if (skinny_metadata
) {
258 btrfs_set_disk_key_type(&disk_key
,
259 BTRFS_METADATA_ITEM_KEY
);
260 btrfs_set_disk_key_offset(&disk_key
, 0);
262 btrfs_set_disk_key_type(&disk_key
,
263 BTRFS_EXTENT_ITEM_KEY
);
264 btrfs_set_disk_key_offset(&disk_key
, cfg
->nodesize
);
266 btrfs_set_item_key(buf
, &disk_key
, nritems
);
267 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
),
269 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
271 extent_item
= btrfs_item_ptr(buf
, nritems
,
272 struct btrfs_extent_item
);
273 btrfs_set_extent_refs(buf
, extent_item
, 1);
274 btrfs_set_extent_generation(buf
, extent_item
, 1);
275 btrfs_set_extent_flags(buf
, extent_item
,
276 BTRFS_EXTENT_FLAG_TREE_BLOCK
);
279 /* create extent ref */
280 ref_root
= reference_root_table
[i
];
281 btrfs_set_disk_key_objectid(&disk_key
, cfg
->blocks
[i
]);
282 btrfs_set_disk_key_offset(&disk_key
, ref_root
);
283 btrfs_set_disk_key_type(&disk_key
, BTRFS_TREE_BLOCK_REF_KEY
);
284 btrfs_set_item_key(buf
, &disk_key
, nritems
);
285 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
),
287 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), 0);
290 btrfs_set_header_bytenr(buf
, cfg
->blocks
[MKFS_EXTENT_TREE
]);
291 btrfs_set_header_owner(buf
, BTRFS_EXTENT_TREE_OBJECTID
);
292 btrfs_set_header_nritems(buf
, nritems
);
293 csum_tree_block_size(buf
, btrfs_csum_sizes
[BTRFS_CSUM_TYPE_CRC32
], 0);
294 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[MKFS_EXTENT_TREE
]);
295 if (ret
!= cfg
->nodesize
) {
296 ret
= (ret
< 0 ? -errno
: -EIO
);
300 /* create the chunk tree */
301 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
302 cfg
->nodesize
- sizeof(struct btrfs_header
));
304 item_size
= sizeof(*dev_item
);
305 itemoff
= __BTRFS_LEAF_DATA_SIZE(cfg
->nodesize
) - item_size
;
307 /* first device 1 (there is no device 0) */
308 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_DEV_ITEMS_OBJECTID
);
309 btrfs_set_disk_key_offset(&disk_key
, 1);
310 btrfs_set_disk_key_type(&disk_key
, BTRFS_DEV_ITEM_KEY
);
311 btrfs_set_item_key(buf
, &disk_key
, nritems
);
312 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
313 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), item_size
);
315 dev_item
= btrfs_item_ptr(buf
, nritems
, struct btrfs_dev_item
);
316 btrfs_set_device_id(buf
, dev_item
, 1);
317 btrfs_set_device_generation(buf
, dev_item
, 0);
318 btrfs_set_device_total_bytes(buf
, dev_item
, num_bytes
);
319 btrfs_set_device_bytes_used(buf
, dev_item
,
320 BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
321 btrfs_set_device_io_align(buf
, dev_item
, cfg
->sectorsize
);
322 btrfs_set_device_io_width(buf
, dev_item
, cfg
->sectorsize
);
323 btrfs_set_device_sector_size(buf
, dev_item
, cfg
->sectorsize
);
324 btrfs_set_device_type(buf
, dev_item
, 0);
326 write_extent_buffer(buf
, super
.dev_item
.uuid
,
327 (unsigned long)btrfs_device_uuid(dev_item
),
329 write_extent_buffer(buf
, super
.fsid
,
330 (unsigned long)btrfs_device_fsid(dev_item
),
332 read_extent_buffer(buf
, &super
.dev_item
, (unsigned long)dev_item
,
336 item_size
= btrfs_chunk_item_size(1);
337 itemoff
= itemoff
- item_size
;
339 /* then we have chunk 0 */
340 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
341 btrfs_set_disk_key_offset(&disk_key
, BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
);
342 btrfs_set_disk_key_type(&disk_key
, BTRFS_CHUNK_ITEM_KEY
);
343 btrfs_set_item_key(buf
, &disk_key
, nritems
);
344 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
345 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), item_size
);
347 chunk
= btrfs_item_ptr(buf
, nritems
, struct btrfs_chunk
);
348 btrfs_set_chunk_length(buf
, chunk
, BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
349 btrfs_set_chunk_owner(buf
, chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
350 btrfs_set_chunk_stripe_len(buf
, chunk
, BTRFS_STRIPE_LEN
);
351 btrfs_set_chunk_type(buf
, chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
352 btrfs_set_chunk_io_align(buf
, chunk
, cfg
->sectorsize
);
353 btrfs_set_chunk_io_width(buf
, chunk
, cfg
->sectorsize
);
354 btrfs_set_chunk_sector_size(buf
, chunk
, cfg
->sectorsize
);
355 btrfs_set_chunk_num_stripes(buf
, chunk
, 1);
356 btrfs_set_stripe_devid_nr(buf
, chunk
, 0, 1);
357 btrfs_set_stripe_offset_nr(buf
, chunk
, 0,
358 BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
);
361 write_extent_buffer(buf
, super
.dev_item
.uuid
,
362 (unsigned long)btrfs_stripe_dev_uuid(&chunk
->stripe
),
365 /* copy the key for the chunk to the system array */
366 ptr
= super
.sys_chunk_array
;
367 array_size
= sizeof(disk_key
);
369 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
370 ptr
+= sizeof(disk_key
);
372 /* copy the chunk to the system array */
373 read_extent_buffer(buf
, ptr
, (unsigned long)chunk
, item_size
);
374 array_size
+= item_size
;
376 btrfs_set_super_sys_array_size(&super
, array_size
);
378 btrfs_set_header_bytenr(buf
, cfg
->blocks
[MKFS_CHUNK_TREE
]);
379 btrfs_set_header_owner(buf
, BTRFS_CHUNK_TREE_OBJECTID
);
380 btrfs_set_header_nritems(buf
, nritems
);
381 csum_tree_block_size(buf
, btrfs_csum_sizes
[BTRFS_CSUM_TYPE_CRC32
], 0);
382 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[MKFS_CHUNK_TREE
]);
383 if (ret
!= cfg
->nodesize
) {
384 ret
= (ret
< 0 ? -errno
: -EIO
);
388 /* create the device tree */
389 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
390 cfg
->nodesize
- sizeof(struct btrfs_header
));
392 itemoff
= __BTRFS_LEAF_DATA_SIZE(cfg
->nodesize
) -
393 sizeof(struct btrfs_dev_extent
);
395 btrfs_set_disk_key_objectid(&disk_key
, 1);
396 btrfs_set_disk_key_offset(&disk_key
, BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
);
397 btrfs_set_disk_key_type(&disk_key
, BTRFS_DEV_EXTENT_KEY
);
398 btrfs_set_item_key(buf
, &disk_key
, nritems
);
399 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
400 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
401 sizeof(struct btrfs_dev_extent
));
402 dev_extent
= btrfs_item_ptr(buf
, nritems
, struct btrfs_dev_extent
);
403 btrfs_set_dev_extent_chunk_tree(buf
, dev_extent
,
404 BTRFS_CHUNK_TREE_OBJECTID
);
405 btrfs_set_dev_extent_chunk_objectid(buf
, dev_extent
,
406 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
407 btrfs_set_dev_extent_chunk_offset(buf
, dev_extent
,
408 BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
);
410 write_extent_buffer(buf
, chunk_tree_uuid
,
411 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent
),
414 btrfs_set_dev_extent_length(buf
, dev_extent
,
415 BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
418 btrfs_set_header_bytenr(buf
, cfg
->blocks
[MKFS_DEV_TREE
]);
419 btrfs_set_header_owner(buf
, BTRFS_DEV_TREE_OBJECTID
);
420 btrfs_set_header_nritems(buf
, nritems
);
421 csum_tree_block_size(buf
, btrfs_csum_sizes
[BTRFS_CSUM_TYPE_CRC32
], 0);
422 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[MKFS_DEV_TREE
]);
423 if (ret
!= cfg
->nodesize
) {
424 ret
= (ret
< 0 ? -errno
: -EIO
);
428 /* create the FS root */
429 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
430 cfg
->nodesize
- sizeof(struct btrfs_header
));
431 btrfs_set_header_bytenr(buf
, cfg
->blocks
[MKFS_FS_TREE
]);
432 btrfs_set_header_owner(buf
, BTRFS_FS_TREE_OBJECTID
);
433 btrfs_set_header_nritems(buf
, 0);
434 csum_tree_block_size(buf
, btrfs_csum_sizes
[BTRFS_CSUM_TYPE_CRC32
], 0);
435 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[MKFS_FS_TREE
]);
436 if (ret
!= cfg
->nodesize
) {
437 ret
= (ret
< 0 ? -errno
: -EIO
);
440 /* finally create the csum root */
441 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
442 cfg
->nodesize
- sizeof(struct btrfs_header
));
443 btrfs_set_header_bytenr(buf
, cfg
->blocks
[MKFS_CSUM_TREE
]);
444 btrfs_set_header_owner(buf
, BTRFS_CSUM_TREE_OBJECTID
);
445 btrfs_set_header_nritems(buf
, 0);
446 csum_tree_block_size(buf
, btrfs_csum_sizes
[BTRFS_CSUM_TYPE_CRC32
], 0);
447 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[MKFS_CSUM_TREE
]);
448 if (ret
!= cfg
->nodesize
) {
449 ret
= (ret
< 0 ? -errno
: -EIO
);
453 /* and write out the super block */
454 memset(buf
->data
, 0, BTRFS_SUPER_INFO_SIZE
);
455 memcpy(buf
->data
, &super
, sizeof(super
));
456 buf
->len
= BTRFS_SUPER_INFO_SIZE
;
457 csum_tree_block_size(buf
, btrfs_csum_sizes
[BTRFS_CSUM_TYPE_CRC32
], 0);
458 ret
= pwrite(fd
, buf
->data
, BTRFS_SUPER_INFO_SIZE
,
459 cfg
->blocks
[MKFS_SUPER_BLOCK
]);
460 if (ret
!= BTRFS_SUPER_INFO_SIZE
) {
461 ret
= (ret
< 0 ? -errno
: -EIO
);
473 * Btrfs minimum size calculation is complicated, it should include at least:
474 * 1. system group size
475 * 2. minimum global block reserve
476 * 3. metadata used at mkfs
477 * 4. space reservation to create uuid for first mount.
478 * Also, raid factor should also be taken into consideration.
479 * To avoid the overkill calculation, (system group + global block rsv) * 2
480 * for *EACH* device should be good enough.
482 static u64
btrfs_min_global_blk_rsv_size(u32 nodesize
)
484 return (u64
)nodesize
<< 10;
487 u64
btrfs_min_dev_size(u32 nodesize
, int mixed
, u64 meta_profile
,
495 return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE
+
496 btrfs_min_global_blk_rsv_size(nodesize
));
499 * Minimal size calculation is complex due to several factors:
500 * 0) Reserved 1M range.
502 * 1) Temporary chunk reuse
503 * If specified chunk profile is SINGLE, we can reuse
504 * temporary chunks, no need to allocate new chunks.
506 * 2) Different minimal chunk size for different profiles:
507 * For initial sys chunk, chunk size is fixed to 4M.
508 * For single profile, minimal chunk size is 8M for all.
509 * For other profiles, minimal chunk and stripe size ranges from 8M
512 * To calculate it a little easier, here we assume we don't reuse any
513 * temporary chunk, and calculate the size completely by ourselves.
515 * Temporary chunks sizes are always fixed:
516 * One initial sys chunk, one SINGLE meta, and one SINGLE data.
517 * The latter two are all 8M, according to @calc_size of
518 * btrfs_alloc_chunk().
520 reserved
+= BTRFS_BLOCK_RESERVED_1M_FOR_SUPER
+
521 BTRFS_MKFS_SYSTEM_GROUP_SIZE
+ SZ_8M
* 2;
524 * For real chunks, we need to select different sizes:
525 * For SINGLE, it's still fixed to 8M (@calc_size).
526 * For other profiles, refer to max(@min_stripe_size, @calc_size).
528 * And use the stripe size to calculate its physical used space.
530 if (meta_profile
& BTRFS_BLOCK_GROUP_PROFILE_MASK
)
531 meta_size
= SZ_8M
+ SZ_32M
;
533 meta_size
= SZ_8M
+ SZ_8M
;
534 /* For DUP/metadata, 2 stripes on one disk */
535 if (meta_profile
& BTRFS_BLOCK_GROUP_DUP
)
537 reserved
+= meta_size
;
539 if (data_profile
& BTRFS_BLOCK_GROUP_PROFILE_MASK
)
543 /* For DUP/data, 2 stripes on one disk */
544 if (data_profile
& BTRFS_BLOCK_GROUP_DUP
)
546 reserved
+= data_size
;
551 #define isoctal(c) (((c) & ~7) == '0')
553 static inline void translate(char *f
, char *t
)
557 isoctal(f
[1]) && isoctal(f
[2]) && isoctal(f
[3])) {
558 *t
++ = 64*(f
[1] & 7) + 8*(f
[2] & 7) + (f
[3] & 7);
568 * Checks if the swap device.
569 * Returns 1 if swap device, < 0 on error or 0 if not swap device.
571 static int is_swap_device(const char *file
)
582 if (stat(file
, &st_buf
) < 0)
584 if (S_ISBLK(st_buf
.st_mode
))
585 dev
= st_buf
.st_rdev
;
586 else if (S_ISREG(st_buf
.st_mode
)) {
592 if ((f
= fopen("/proc/swaps", "r")) == NULL
)
595 /* skip the first line */
596 if (fgets(tmp
, sizeof(tmp
), f
) == NULL
)
599 while (fgets(tmp
, sizeof(tmp
), f
) != NULL
) {
600 if ((cp
= strchr(tmp
, ' ')) != NULL
)
602 if ((cp
= strchr(tmp
, '\t')) != NULL
)
605 if (stat(buf
, &st_buf
) != 0)
607 if (S_ISBLK(st_buf
.st_mode
)) {
608 if (dev
== st_buf
.st_rdev
) {
612 } else if (S_ISREG(st_buf
.st_mode
)) {
613 if (dev
== st_buf
.st_dev
&& ino
== st_buf
.st_ino
) {
627 * Check for existing filesystem or partition table on device.
629 * 1 for existing fs or partition
630 * 0 for nothing found
631 * -1 for internal error
633 static int check_overwrite(const char *device
)
636 blkid_probe pr
= NULL
;
640 if (!device
|| !*device
)
643 ret
= -1; /* will reset on success of all setup calls */
645 pr
= blkid_new_probe_from_filename(device
);
649 size
= blkid_probe_get_size(pr
);
653 /* nothing to overwrite on a 0-length device */
659 ret
= blkid_probe_enable_partitions(pr
, 1);
663 ret
= blkid_do_fullprobe(pr
);
668 * Blkid returns 1 for nothing found and 0 when it finds a signature,
669 * but we want the exact opposite, so reverse the return value here.
671 * In addition print some useful diagnostics about what actually is
679 if (!blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
)) {
681 "%s appears to contain an existing "
682 "filesystem (%s).\n", device
, type
);
683 } else if (!blkid_probe_lookup_value(pr
, "PTTYPE", &type
, NULL
)) {
685 "%s appears to contain a partition "
686 "table (%s).\n", device
, type
);
689 "%s appears to contain something weird "
690 "according to blkid\n", device
);
696 blkid_free_probe(pr
);
699 "probe of %s failed, cannot detect "
700 "existing filesystem.\n", device
);
705 * Check if a device is suitable for btrfs
707 * 1: something is wrong, an error is printed
710 int test_dev_for_mkfs(const char *file
, int force_overwrite
)
715 ret
= is_swap_device(file
);
717 error("checking status of %s: %s", file
, strerror(-ret
));
721 error("%s is a swap device", file
);
724 ret
= test_status_for_mkfs(file
, force_overwrite
);
727 /* check if the device is busy */
728 fd
= open(file
, O_RDWR
|O_EXCL
);
730 error("unable to open %s: %m", file
);
733 if (fstat(fd
, &st
)) {
734 error("unable to stat %s: %m", file
);
738 if (!S_ISBLK(st
.st_mode
)) {
739 error("%s is not a block device", file
);
748 * check if the file (device) is formatted or mounted
750 int test_status_for_mkfs(const char *file
, bool force_overwrite
)
754 if (!force_overwrite
) {
755 if (check_overwrite(file
)) {
756 error("use the -f option to force overwrite of %s",
761 ret
= check_mounted(file
);
763 error("cannot check mount status of %s: %s", file
,
768 error("%s is mounted", file
);
775 int is_vol_small(const char *file
)
782 fd
= open(file
, O_RDONLY
);
785 if (fstat(fd
, &st
) < 0) {
790 size
= btrfs_device_size(fd
, &st
);
795 if (size
< BTRFS_MKFS_SMALL_VOLUME_SIZE
) {
804 int test_minimum_size(const char *file
, u64 min_dev_size
)
809 fd
= open(file
, O_RDONLY
);
812 if (stat(file
, &statbuf
) < 0) {
816 if (btrfs_device_size(fd
, &statbuf
) < min_dev_size
) {