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>
25 #include "mkfs/common.h"
27 static u64 reference_root_table
[] = {
28 [1] = BTRFS_ROOT_TREE_OBJECTID
,
29 [2] = BTRFS_EXTENT_TREE_OBJECTID
,
30 [3] = BTRFS_CHUNK_TREE_OBJECTID
,
31 [4] = BTRFS_DEV_TREE_OBJECTID
,
32 [5] = BTRFS_FS_TREE_OBJECTID
,
33 [6] = BTRFS_CSUM_TREE_OBJECTID
,
37 * @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
39 * The superblock signature is not valid, denotes a partially created
40 * filesystem, needs to be finalized.
42 int make_btrfs(int fd
, struct btrfs_mkfs_config
*cfg
)
44 struct btrfs_super_block super
;
45 struct extent_buffer
*buf
;
46 struct btrfs_root_item root_item
;
47 struct btrfs_disk_key disk_key
;
48 struct btrfs_extent_item
*extent_item
;
49 struct btrfs_inode_item
*inode_item
;
50 struct btrfs_chunk
*chunk
;
51 struct btrfs_dev_item
*dev_item
;
52 struct btrfs_dev_extent
*dev_extent
;
53 u8 chunk_tree_uuid
[BTRFS_UUID_SIZE
];
63 int skinny_metadata
= !!(cfg
->features
&
64 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA
);
67 buf
= malloc(sizeof(*buf
) + max(cfg
->sectorsize
, cfg
->nodesize
));
71 first_free
= BTRFS_SUPER_INFO_OFFSET
+ cfg
->sectorsize
* 2 - 1;
72 first_free
&= ~((u64
)cfg
->sectorsize
- 1);
74 memset(&super
, 0, sizeof(super
));
76 num_bytes
= (cfg
->num_bytes
/ cfg
->sectorsize
) * cfg
->sectorsize
;
78 if (uuid_parse(cfg
->fs_uuid
, super
.fsid
) != 0) {
79 error("cannot not parse UUID: %s", cfg
->fs_uuid
);
83 if (!test_uuid_unique(cfg
->fs_uuid
)) {
84 error("non-unique UUID: %s", cfg
->fs_uuid
);
89 uuid_generate(super
.fsid
);
90 uuid_unparse(super
.fsid
, cfg
->fs_uuid
);
92 uuid_generate(super
.dev_item
.uuid
);
93 uuid_generate(chunk_tree_uuid
);
95 cfg
->blocks
[0] = BTRFS_SUPER_INFO_OFFSET
;
96 for (i
= 1; i
< 7; i
++) {
97 cfg
->blocks
[i
] = BTRFS_SUPER_INFO_OFFSET
+ 1024 * 1024 +
101 btrfs_set_super_bytenr(&super
, cfg
->blocks
[0]);
102 btrfs_set_super_num_devices(&super
, 1);
103 btrfs_set_super_magic(&super
, BTRFS_MAGIC_PARTIAL
);
104 btrfs_set_super_generation(&super
, 1);
105 btrfs_set_super_root(&super
, cfg
->blocks
[1]);
106 btrfs_set_super_chunk_root(&super
, cfg
->blocks
[3]);
107 btrfs_set_super_total_bytes(&super
, num_bytes
);
108 btrfs_set_super_bytes_used(&super
, 6 * cfg
->nodesize
);
109 btrfs_set_super_sectorsize(&super
, cfg
->sectorsize
);
110 btrfs_set_super_leafsize(&super
, cfg
->nodesize
);
111 btrfs_set_super_nodesize(&super
, cfg
->nodesize
);
112 btrfs_set_super_stripesize(&super
, cfg
->stripesize
);
113 btrfs_set_super_csum_type(&super
, BTRFS_CSUM_TYPE_CRC32
);
114 btrfs_set_super_chunk_root_generation(&super
, 1);
115 btrfs_set_super_cache_generation(&super
, -1);
116 btrfs_set_super_incompat_flags(&super
, cfg
->features
);
118 __strncpy_null(super
.label
, cfg
->label
, BTRFS_LABEL_SIZE
- 1);
120 /* create the tree of root objects */
121 memset(buf
->data
, 0, cfg
->nodesize
);
122 buf
->len
= cfg
->nodesize
;
123 btrfs_set_header_bytenr(buf
, cfg
->blocks
[1]);
124 btrfs_set_header_nritems(buf
, 4);
125 btrfs_set_header_generation(buf
, 1);
126 btrfs_set_header_backref_rev(buf
, BTRFS_MIXED_BACKREF_REV
);
127 btrfs_set_header_owner(buf
, BTRFS_ROOT_TREE_OBJECTID
);
128 write_extent_buffer(buf
, super
.fsid
, btrfs_header_fsid(),
131 write_extent_buffer(buf
, chunk_tree_uuid
,
132 btrfs_header_chunk_tree_uuid(buf
),
135 /* create the items for the root tree */
136 memset(&root_item
, 0, sizeof(root_item
));
137 inode_item
= &root_item
.inode
;
138 btrfs_set_stack_inode_generation(inode_item
, 1);
139 btrfs_set_stack_inode_size(inode_item
, 3);
140 btrfs_set_stack_inode_nlink(inode_item
, 1);
141 btrfs_set_stack_inode_nbytes(inode_item
, cfg
->nodesize
);
142 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
143 btrfs_set_root_refs(&root_item
, 1);
144 btrfs_set_root_used(&root_item
, cfg
->nodesize
);
145 btrfs_set_root_generation(&root_item
, 1);
147 memset(&disk_key
, 0, sizeof(disk_key
));
148 btrfs_set_disk_key_type(&disk_key
, BTRFS_ROOT_ITEM_KEY
);
149 btrfs_set_disk_key_offset(&disk_key
, 0);
152 itemoff
= __BTRFS_LEAF_DATA_SIZE(cfg
->nodesize
) - sizeof(root_item
);
153 btrfs_set_root_bytenr(&root_item
, cfg
->blocks
[2]);
154 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_EXTENT_TREE_OBJECTID
);
155 btrfs_set_item_key(buf
, &disk_key
, nritems
);
156 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
157 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
159 write_extent_buffer(buf
, &root_item
, btrfs_item_ptr_offset(buf
,
160 nritems
), sizeof(root_item
));
163 itemoff
= itemoff
- sizeof(root_item
);
164 btrfs_set_root_bytenr(&root_item
, cfg
->blocks
[4]);
165 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_DEV_TREE_OBJECTID
);
166 btrfs_set_item_key(buf
, &disk_key
, nritems
);
167 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
168 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
170 write_extent_buffer(buf
, &root_item
,
171 btrfs_item_ptr_offset(buf
, nritems
),
175 itemoff
= itemoff
- sizeof(root_item
);
176 btrfs_set_root_bytenr(&root_item
, cfg
->blocks
[5]);
177 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_FS_TREE_OBJECTID
);
178 btrfs_set_item_key(buf
, &disk_key
, nritems
);
179 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
180 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
182 write_extent_buffer(buf
, &root_item
,
183 btrfs_item_ptr_offset(buf
, nritems
),
187 itemoff
= itemoff
- sizeof(root_item
);
188 btrfs_set_root_bytenr(&root_item
, cfg
->blocks
[6]);
189 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_CSUM_TREE_OBJECTID
);
190 btrfs_set_item_key(buf
, &disk_key
, nritems
);
191 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
192 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
194 write_extent_buffer(buf
, &root_item
,
195 btrfs_item_ptr_offset(buf
, nritems
),
200 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
201 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[1]);
202 if (ret
!= cfg
->nodesize
) {
203 ret
= (ret
< 0 ? -errno
: -EIO
);
207 /* create the items for the extent tree */
208 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
209 cfg
->nodesize
- sizeof(struct btrfs_header
));
211 itemoff
= __BTRFS_LEAF_DATA_SIZE(cfg
->nodesize
);
212 for (i
= 1; i
< 7; i
++) {
213 item_size
= sizeof(struct btrfs_extent_item
);
214 if (!skinny_metadata
)
215 item_size
+= sizeof(struct btrfs_tree_block_info
);
217 if (cfg
->blocks
[i
] < first_free
) {
218 error("block[%d] below first free: %llu < %llu",
219 i
, (unsigned long long)cfg
->blocks
[i
],
220 (unsigned long long)first_free
);
224 if (cfg
->blocks
[i
] < cfg
->blocks
[i
- 1]) {
225 error("blocks %d and %d in reverse order: %llu < %llu",
227 (unsigned long long)cfg
->blocks
[i
],
228 (unsigned long long)cfg
->blocks
[i
- 1]);
233 /* create extent item */
234 itemoff
-= item_size
;
235 btrfs_set_disk_key_objectid(&disk_key
, cfg
->blocks
[i
]);
236 if (skinny_metadata
) {
237 btrfs_set_disk_key_type(&disk_key
,
238 BTRFS_METADATA_ITEM_KEY
);
239 btrfs_set_disk_key_offset(&disk_key
, 0);
241 btrfs_set_disk_key_type(&disk_key
,
242 BTRFS_EXTENT_ITEM_KEY
);
243 btrfs_set_disk_key_offset(&disk_key
, cfg
->nodesize
);
245 btrfs_set_item_key(buf
, &disk_key
, nritems
);
246 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
),
248 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
250 extent_item
= btrfs_item_ptr(buf
, nritems
,
251 struct btrfs_extent_item
);
252 btrfs_set_extent_refs(buf
, extent_item
, 1);
253 btrfs_set_extent_generation(buf
, extent_item
, 1);
254 btrfs_set_extent_flags(buf
, extent_item
,
255 BTRFS_EXTENT_FLAG_TREE_BLOCK
);
258 /* create extent ref */
259 ref_root
= reference_root_table
[i
];
260 btrfs_set_disk_key_objectid(&disk_key
, cfg
->blocks
[i
]);
261 btrfs_set_disk_key_offset(&disk_key
, ref_root
);
262 btrfs_set_disk_key_type(&disk_key
, BTRFS_TREE_BLOCK_REF_KEY
);
263 btrfs_set_item_key(buf
, &disk_key
, nritems
);
264 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
),
266 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), 0);
269 btrfs_set_header_bytenr(buf
, cfg
->blocks
[2]);
270 btrfs_set_header_owner(buf
, BTRFS_EXTENT_TREE_OBJECTID
);
271 btrfs_set_header_nritems(buf
, nritems
);
272 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
273 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[2]);
274 if (ret
!= cfg
->nodesize
) {
275 ret
= (ret
< 0 ? -errno
: -EIO
);
279 /* create the chunk tree */
280 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
281 cfg
->nodesize
- sizeof(struct btrfs_header
));
283 item_size
= sizeof(*dev_item
);
284 itemoff
= __BTRFS_LEAF_DATA_SIZE(cfg
->nodesize
) - item_size
;
286 /* first device 1 (there is no device 0) */
287 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_DEV_ITEMS_OBJECTID
);
288 btrfs_set_disk_key_offset(&disk_key
, 1);
289 btrfs_set_disk_key_type(&disk_key
, BTRFS_DEV_ITEM_KEY
);
290 btrfs_set_item_key(buf
, &disk_key
, nritems
);
291 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
292 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), item_size
);
294 dev_item
= btrfs_item_ptr(buf
, nritems
, struct btrfs_dev_item
);
295 btrfs_set_device_id(buf
, dev_item
, 1);
296 btrfs_set_device_generation(buf
, dev_item
, 0);
297 btrfs_set_device_total_bytes(buf
, dev_item
, num_bytes
);
298 btrfs_set_device_bytes_used(buf
, dev_item
,
299 BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
300 btrfs_set_device_io_align(buf
, dev_item
, cfg
->sectorsize
);
301 btrfs_set_device_io_width(buf
, dev_item
, cfg
->sectorsize
);
302 btrfs_set_device_sector_size(buf
, dev_item
, cfg
->sectorsize
);
303 btrfs_set_device_type(buf
, dev_item
, 0);
305 write_extent_buffer(buf
, super
.dev_item
.uuid
,
306 (unsigned long)btrfs_device_uuid(dev_item
),
308 write_extent_buffer(buf
, super
.fsid
,
309 (unsigned long)btrfs_device_fsid(dev_item
),
311 read_extent_buffer(buf
, &super
.dev_item
, (unsigned long)dev_item
,
315 item_size
= btrfs_chunk_item_size(1);
316 itemoff
= itemoff
- item_size
;
318 /* then we have chunk 0 */
319 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
320 btrfs_set_disk_key_offset(&disk_key
, 0);
321 btrfs_set_disk_key_type(&disk_key
, BTRFS_CHUNK_ITEM_KEY
);
322 btrfs_set_item_key(buf
, &disk_key
, nritems
);
323 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
324 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), item_size
);
326 chunk
= btrfs_item_ptr(buf
, nritems
, struct btrfs_chunk
);
327 btrfs_set_chunk_length(buf
, chunk
, BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
328 btrfs_set_chunk_owner(buf
, chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
329 btrfs_set_chunk_stripe_len(buf
, chunk
, BTRFS_STRIPE_LEN
);
330 btrfs_set_chunk_type(buf
, chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
331 btrfs_set_chunk_io_align(buf
, chunk
, cfg
->sectorsize
);
332 btrfs_set_chunk_io_width(buf
, chunk
, cfg
->sectorsize
);
333 btrfs_set_chunk_sector_size(buf
, chunk
, cfg
->sectorsize
);
334 btrfs_set_chunk_num_stripes(buf
, chunk
, 1);
335 btrfs_set_stripe_devid_nr(buf
, chunk
, 0, 1);
336 btrfs_set_stripe_offset_nr(buf
, chunk
, 0, 0);
339 write_extent_buffer(buf
, super
.dev_item
.uuid
,
340 (unsigned long)btrfs_stripe_dev_uuid(&chunk
->stripe
),
343 /* copy the key for the chunk to the system array */
344 ptr
= super
.sys_chunk_array
;
345 array_size
= sizeof(disk_key
);
347 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
348 ptr
+= sizeof(disk_key
);
350 /* copy the chunk to the system array */
351 read_extent_buffer(buf
, ptr
, (unsigned long)chunk
, item_size
);
352 array_size
+= item_size
;
354 btrfs_set_super_sys_array_size(&super
, array_size
);
356 btrfs_set_header_bytenr(buf
, cfg
->blocks
[3]);
357 btrfs_set_header_owner(buf
, BTRFS_CHUNK_TREE_OBJECTID
);
358 btrfs_set_header_nritems(buf
, nritems
);
359 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
360 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[3]);
361 if (ret
!= cfg
->nodesize
) {
362 ret
= (ret
< 0 ? -errno
: -EIO
);
366 /* create the device tree */
367 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
368 cfg
->nodesize
- sizeof(struct btrfs_header
));
370 itemoff
= __BTRFS_LEAF_DATA_SIZE(cfg
->nodesize
) -
371 sizeof(struct btrfs_dev_extent
);
373 btrfs_set_disk_key_objectid(&disk_key
, 1);
374 btrfs_set_disk_key_offset(&disk_key
, 0);
375 btrfs_set_disk_key_type(&disk_key
, BTRFS_DEV_EXTENT_KEY
);
376 btrfs_set_item_key(buf
, &disk_key
, nritems
);
377 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
378 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
379 sizeof(struct btrfs_dev_extent
));
380 dev_extent
= btrfs_item_ptr(buf
, nritems
, struct btrfs_dev_extent
);
381 btrfs_set_dev_extent_chunk_tree(buf
, dev_extent
,
382 BTRFS_CHUNK_TREE_OBJECTID
);
383 btrfs_set_dev_extent_chunk_objectid(buf
, dev_extent
,
384 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
385 btrfs_set_dev_extent_chunk_offset(buf
, dev_extent
, 0);
387 write_extent_buffer(buf
, chunk_tree_uuid
,
388 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent
),
391 btrfs_set_dev_extent_length(buf
, dev_extent
,
392 BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
395 btrfs_set_header_bytenr(buf
, cfg
->blocks
[4]);
396 btrfs_set_header_owner(buf
, BTRFS_DEV_TREE_OBJECTID
);
397 btrfs_set_header_nritems(buf
, nritems
);
398 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
399 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[4]);
400 if (ret
!= cfg
->nodesize
) {
401 ret
= (ret
< 0 ? -errno
: -EIO
);
405 /* create the FS root */
406 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
407 cfg
->nodesize
- sizeof(struct btrfs_header
));
408 btrfs_set_header_bytenr(buf
, cfg
->blocks
[5]);
409 btrfs_set_header_owner(buf
, BTRFS_FS_TREE_OBJECTID
);
410 btrfs_set_header_nritems(buf
, 0);
411 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
412 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[5]);
413 if (ret
!= cfg
->nodesize
) {
414 ret
= (ret
< 0 ? -errno
: -EIO
);
417 /* finally create the csum root */
418 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
419 cfg
->nodesize
- sizeof(struct btrfs_header
));
420 btrfs_set_header_bytenr(buf
, cfg
->blocks
[6]);
421 btrfs_set_header_owner(buf
, BTRFS_CSUM_TREE_OBJECTID
);
422 btrfs_set_header_nritems(buf
, 0);
423 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
424 ret
= pwrite(fd
, buf
->data
, cfg
->nodesize
, cfg
->blocks
[6]);
425 if (ret
!= cfg
->nodesize
) {
426 ret
= (ret
< 0 ? -errno
: -EIO
);
430 /* and write out the super block */
431 memset(buf
->data
, 0, BTRFS_SUPER_INFO_SIZE
);
432 memcpy(buf
->data
, &super
, sizeof(super
));
433 buf
->len
= BTRFS_SUPER_INFO_SIZE
;
434 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
435 ret
= pwrite(fd
, buf
->data
, BTRFS_SUPER_INFO_SIZE
, cfg
->blocks
[0]);
436 if (ret
!= BTRFS_SUPER_INFO_SIZE
) {
437 ret
= (ret
< 0 ? -errno
: -EIO
);
448 u64
btrfs_min_dev_size(u32 nodesize
)
450 return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE
+
451 btrfs_min_global_blk_rsv_size(nodesize
));
455 * Btrfs minimum size calculation is complicated, it should include at least:
456 * 1. system group size
457 * 2. minimum global block reserve
458 * 3. metadata used at mkfs
459 * 4. space reservation to create uuid for first mount.
460 * Also, raid factor should also be taken into consideration.
461 * To avoid the overkill calculation, (system group + global block rsv) * 2
462 * for *EACH* device should be good enough.
464 u64
btrfs_min_global_blk_rsv_size(u32 nodesize
)
466 return (u64
)nodesize
<< 10;
469 #define isoctal(c) (((c) & ~7) == '0')
471 static inline void translate(char *f
, char *t
)
475 isoctal(f
[1]) && isoctal(f
[2]) && isoctal(f
[3])) {
476 *t
++ = 64*(f
[1] & 7) + 8*(f
[2] & 7) + (f
[3] & 7);
486 * Checks if the swap device.
487 * Returns 1 if swap device, < 0 on error or 0 if not swap device.
489 static int is_swap_device(const char *file
)
500 if (stat(file
, &st_buf
) < 0)
502 if (S_ISBLK(st_buf
.st_mode
))
503 dev
= st_buf
.st_rdev
;
504 else if (S_ISREG(st_buf
.st_mode
)) {
510 if ((f
= fopen("/proc/swaps", "r")) == NULL
)
513 /* skip the first line */
514 if (fgets(tmp
, sizeof(tmp
), f
) == NULL
)
517 while (fgets(tmp
, sizeof(tmp
), f
) != NULL
) {
518 if ((cp
= strchr(tmp
, ' ')) != NULL
)
520 if ((cp
= strchr(tmp
, '\t')) != NULL
)
523 if (stat(buf
, &st_buf
) != 0)
525 if (S_ISBLK(st_buf
.st_mode
)) {
526 if (dev
== st_buf
.st_rdev
) {
530 } else if (S_ISREG(st_buf
.st_mode
)) {
531 if (dev
== st_buf
.st_dev
&& ino
== st_buf
.st_ino
) {
545 * Check for existing filesystem or partition table on device.
547 * 1 for existing fs or partition
548 * 0 for nothing found
549 * -1 for internal error
551 static int check_overwrite(const char *device
)
554 blkid_probe pr
= NULL
;
558 if (!device
|| !*device
)
561 ret
= -1; /* will reset on success of all setup calls */
563 pr
= blkid_new_probe_from_filename(device
);
567 size
= blkid_probe_get_size(pr
);
571 /* nothing to overwrite on a 0-length device */
577 ret
= blkid_probe_enable_partitions(pr
, 1);
581 ret
= blkid_do_fullprobe(pr
);
586 * Blkid returns 1 for nothing found and 0 when it finds a signature,
587 * but we want the exact opposite, so reverse the return value here.
589 * In addition print some useful diagnostics about what actually is
597 if (!blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
)) {
599 "%s appears to contain an existing "
600 "filesystem (%s).\n", device
, type
);
601 } else if (!blkid_probe_lookup_value(pr
, "PTTYPE", &type
, NULL
)) {
603 "%s appears to contain a partition "
604 "table (%s).\n", device
, type
);
607 "%s appears to contain something weird "
608 "according to blkid\n", device
);
614 blkid_free_probe(pr
);
617 "probe of %s failed, cannot detect "
618 "existing filesystem.\n", device
);
623 * Check if a device is suitable for btrfs
625 * 1: something is wrong, an error is printed
628 int test_dev_for_mkfs(const char *file
, int force_overwrite
)
633 ret
= is_swap_device(file
);
635 error("checking status of %s: %s", file
, strerror(-ret
));
639 error("%s is a swap device", file
);
642 if (!force_overwrite
) {
643 if (check_overwrite(file
)) {
644 error("use the -f option to force overwrite of %s",
649 ret
= check_mounted(file
);
651 error("cannot check mount status of %s: %s", file
,
656 error("%s is mounted", file
);
659 /* check if the device is busy */
660 fd
= open(file
, O_RDWR
|O_EXCL
);
662 error("unable to open %s: %s", file
, strerror(errno
));
665 if (fstat(fd
, &st
)) {
666 error("unable to stat %s: %s", file
, strerror(errno
));
670 if (!S_ISBLK(st
.st_mode
)) {
671 error("%s is not a block device", file
);
679 int is_vol_small(const char *file
)
686 fd
= open(file
, O_RDONLY
);
689 if (fstat(fd
, &st
) < 0) {
694 size
= btrfs_device_size(fd
, &st
);
699 if (size
< BTRFS_MKFS_SMALL_VOLUME_SIZE
) {
708 int test_minimum_size(const char *file
, u32 nodesize
)
713 fd
= open(file
, O_RDONLY
);
716 if (stat(file
, &statbuf
) < 0) {
720 if (btrfs_device_size(fd
, &statbuf
) < btrfs_min_dev_size(nodesize
)) {