Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / mkfs / common.c
blob0ace262b0e5f7498db9a90a02cf878882ad1d779
1 /*
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.
17 #include <unistd.h>
18 #include <uuid/uuid.h>
19 #include <blkid/blkid.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include "ctree.h"
23 #include "disk-io.h"
24 #include "volumes.h"
25 #include "utils.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;
43 u32 nritems = 0;
44 u32 itemoff;
45 int ret = 0;
46 int blk;
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)
72 continue;
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),
80 sizeof(root_item));
81 if (blk == MKFS_FS_TREE) {
82 time_t now = time(NULL);
84 uuid_generate(uuid);
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);
88 } else {
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),
96 sizeof(root_item));
97 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);
109 return ret;
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:
119 * Device extent:
120 * 0 1M 5M ......
121 * | Reserved | dev extent for SYS chunk |
123 * And chunk mapping will be:
124 * Chunk mapping:
125 * 0 1M 5M
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];
143 u8 *ptr;
144 int i;
145 int ret;
146 u32 itemoff;
147 u32 nritems = 0;
148 u64 first_free;
149 u64 ref_root;
150 u32 array_size;
151 u32 item_size;
152 int skinny_metadata = !!(cfg->features &
153 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
154 u64 num_bytes;
156 buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
157 if (!buf)
158 return -ENOMEM;
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;
166 if (*cfg->fs_uuid) {
167 if (uuid_parse(cfg->fs_uuid, super.fsid) != 0) {
168 error("cannot not parse UUID: %s", cfg->fs_uuid);
169 ret = -EINVAL;
170 goto out;
172 if (!test_uuid_unique(cfg->fs_uuid)) {
173 error("non-unique UUID: %s", cfg->fs_uuid);
174 ret = -EBUSY;
175 goto out;
177 } else {
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);
206 if (cfg->label)
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(),
218 BTRFS_FSID_SIZE);
220 write_extent_buffer(buf, chunk_tree_uuid,
221 btrfs_header_chunk_tree_uuid(buf),
222 BTRFS_UUID_SIZE);
224 ret = btrfs_create_tree_root(fd, cfg, buf);
225 if (ret < 0)
226 goto out;
228 /* create the items for the extent tree */
229 memset(buf->data + sizeof(struct btrfs_header), 0,
230 cfg->nodesize - sizeof(struct btrfs_header));
231 nritems = 0;
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);
242 ret = -EINVAL;
243 goto out;
245 if (cfg->blocks[i] < cfg->blocks[i - 1]) {
246 error("blocks %d and %d in reverse order: %llu < %llu",
247 i, i - 1,
248 (unsigned long long)cfg->blocks[i],
249 (unsigned long long)cfg->blocks[i - 1]);
250 ret = -EINVAL;
251 goto out;
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);
261 } else {
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),
268 itemoff);
269 btrfs_set_item_size(buf, btrfs_item_nr(nritems),
270 item_size);
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);
277 nritems++;
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),
286 itemoff);
287 btrfs_set_item_size(buf, btrfs_item_nr(nritems), 0);
288 nritems++;
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);
297 goto out;
300 /* create the chunk tree */
301 memset(buf->data + sizeof(struct btrfs_header), 0,
302 cfg->nodesize - sizeof(struct btrfs_header));
303 nritems = 0;
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),
328 BTRFS_UUID_SIZE);
329 write_extent_buffer(buf, super.fsid,
330 (unsigned long)btrfs_device_fsid(dev_item),
331 BTRFS_UUID_SIZE);
332 read_extent_buffer(buf, &super.dev_item, (unsigned long)dev_item,
333 sizeof(*dev_item));
335 nritems++;
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);
359 nritems++;
361 write_extent_buffer(buf, super.dev_item.uuid,
362 (unsigned long)btrfs_stripe_dev_uuid(&chunk->stripe),
363 BTRFS_UUID_SIZE);
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;
375 ptr += 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);
385 goto out;
388 /* create the device tree */
389 memset(buf->data + sizeof(struct btrfs_header), 0,
390 cfg->nodesize - sizeof(struct btrfs_header));
391 nritems = 0;
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),
412 BTRFS_UUID_SIZE);
414 btrfs_set_dev_extent_length(buf, dev_extent,
415 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
416 nritems++;
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);
425 goto out;
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);
438 goto out;
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);
450 goto out;
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);
462 goto out;
465 ret = 0;
467 out:
468 free(buf);
469 return ret;
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,
488 u64 data_profile)
490 u64 reserved = 0;
491 u64 meta_size;
492 u64 data_size;
494 if (mixed)
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
510 * to 64M.
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;
532 else
533 meta_size = SZ_8M + SZ_8M;
534 /* For DUP/metadata, 2 stripes on one disk */
535 if (meta_profile & BTRFS_BLOCK_GROUP_DUP)
536 meta_size *= 2;
537 reserved += meta_size;
539 if (data_profile & BTRFS_BLOCK_GROUP_PROFILE_MASK)
540 data_size = SZ_64M;
541 else
542 data_size = SZ_8M;
543 /* For DUP/data, 2 stripes on one disk */
544 if (data_profile & BTRFS_BLOCK_GROUP_DUP)
545 data_size *= 2;
546 reserved += data_size;
548 return reserved;
551 #define isoctal(c) (((c) & ~7) == '0')
553 static inline void translate(char *f, char *t)
555 while (*f != '\0') {
556 if (*f == '\\' &&
557 isoctal(f[1]) && isoctal(f[2]) && isoctal(f[3])) {
558 *t++ = 64*(f[1] & 7) + 8*(f[2] & 7) + (f[3] & 7);
559 f += 4;
560 } else
561 *t++ = *f++;
563 *t = '\0';
564 return;
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)
573 FILE *f;
574 struct stat st_buf;
575 dev_t dev;
576 ino_t ino = 0;
577 char tmp[PATH_MAX];
578 char buf[PATH_MAX];
579 char *cp;
580 int ret = 0;
582 if (stat(file, &st_buf) < 0)
583 return -errno;
584 if (S_ISBLK(st_buf.st_mode))
585 dev = st_buf.st_rdev;
586 else if (S_ISREG(st_buf.st_mode)) {
587 dev = st_buf.st_dev;
588 ino = st_buf.st_ino;
589 } else
590 return 0;
592 if ((f = fopen("/proc/swaps", "r")) == NULL)
593 return 0;
595 /* skip the first line */
596 if (fgets(tmp, sizeof(tmp), f) == NULL)
597 goto out;
599 while (fgets(tmp, sizeof(tmp), f) != NULL) {
600 if ((cp = strchr(tmp, ' ')) != NULL)
601 *cp = '\0';
602 if ((cp = strchr(tmp, '\t')) != NULL)
603 *cp = '\0';
604 translate(tmp, buf);
605 if (stat(buf, &st_buf) != 0)
606 continue;
607 if (S_ISBLK(st_buf.st_mode)) {
608 if (dev == st_buf.st_rdev) {
609 ret = 1;
610 break;
612 } else if (S_ISREG(st_buf.st_mode)) {
613 if (dev == st_buf.st_dev && ino == st_buf.st_ino) {
614 ret = 1;
615 break;
620 out:
621 fclose(f);
623 return ret;
627 * Check for existing filesystem or partition table on device.
628 * Returns:
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)
635 const char *type;
636 blkid_probe pr = NULL;
637 int ret;
638 blkid_loff_t size;
640 if (!device || !*device)
641 return 0;
643 ret = -1; /* will reset on success of all setup calls */
645 pr = blkid_new_probe_from_filename(device);
646 if (!pr)
647 goto out;
649 size = blkid_probe_get_size(pr);
650 if (size < 0)
651 goto out;
653 /* nothing to overwrite on a 0-length device */
654 if (size == 0) {
655 ret = 0;
656 goto out;
659 ret = blkid_probe_enable_partitions(pr, 1);
660 if (ret < 0)
661 goto out;
663 ret = blkid_do_fullprobe(pr);
664 if (ret < 0)
665 goto out;
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
672 * on the device.
674 if (ret) {
675 ret = 0;
676 goto out;
679 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
680 fprintf(stderr,
681 "%s appears to contain an existing "
682 "filesystem (%s).\n", device, type);
683 } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
684 fprintf(stderr,
685 "%s appears to contain a partition "
686 "table (%s).\n", device, type);
687 } else {
688 fprintf(stderr,
689 "%s appears to contain something weird "
690 "according to blkid\n", device);
692 ret = 1;
694 out:
695 if (pr)
696 blkid_free_probe(pr);
697 if (ret == -1)
698 fprintf(stderr,
699 "probe of %s failed, cannot detect "
700 "existing filesystem.\n", device);
701 return ret;
705 * Check if a device is suitable for btrfs
706 * returns:
707 * 1: something is wrong, an error is printed
708 * 0: all is fine
710 int test_dev_for_mkfs(const char *file, int force_overwrite)
712 int ret, fd;
713 struct stat st;
715 ret = is_swap_device(file);
716 if (ret < 0) {
717 error("checking status of %s: %s", file, strerror(-ret));
718 return 1;
720 if (ret == 1) {
721 error("%s is a swap device", file);
722 return 1;
724 ret = test_status_for_mkfs(file, force_overwrite);
725 if (ret)
726 return 1;
727 /* check if the device is busy */
728 fd = open(file, O_RDWR|O_EXCL);
729 if (fd < 0) {
730 error("unable to open %s: %m", file);
731 return 1;
733 if (fstat(fd, &st)) {
734 error("unable to stat %s: %m", file);
735 close(fd);
736 return 1;
738 if (!S_ISBLK(st.st_mode)) {
739 error("%s is not a block device", file);
740 close(fd);
741 return 1;
743 close(fd);
744 return 0;
748 * check if the file (device) is formatted or mounted
750 int test_status_for_mkfs(const char *file, bool force_overwrite)
752 int ret;
754 if (!force_overwrite) {
755 if (check_overwrite(file)) {
756 error("use the -f option to force overwrite of %s",
757 file);
758 return 1;
761 ret = check_mounted(file);
762 if (ret < 0) {
763 error("cannot check mount status of %s: %s", file,
764 strerror(-ret));
765 return 1;
767 if (ret == 1) {
768 error("%s is mounted", file);
769 return 1;
772 return 0;
775 int is_vol_small(const char *file)
777 int fd = -1;
778 int e;
779 struct stat st;
780 u64 size;
782 fd = open(file, O_RDONLY);
783 if (fd < 0)
784 return -errno;
785 if (fstat(fd, &st) < 0) {
786 e = -errno;
787 close(fd);
788 return e;
790 size = btrfs_device_size(fd, &st);
791 if (size == 0) {
792 close(fd);
793 return -1;
795 if (size < BTRFS_MKFS_SMALL_VOLUME_SIZE) {
796 close(fd);
797 return 1;
798 } else {
799 close(fd);
800 return 0;
804 int test_minimum_size(const char *file, u64 min_dev_size)
806 int fd;
807 struct stat statbuf;
809 fd = open(file, O_RDONLY);
810 if (fd < 0)
811 return -errno;
812 if (stat(file, &statbuf) < 0) {
813 close(fd);
814 return -errno;
816 if (btrfs_device_size(fd, &statbuf) < min_dev_size) {
817 close(fd);
818 return 1;
820 close(fd);
821 return 0;