2 * Copyright (C) 2007 Oracle. All rights reserved.
3 * Copyright (C) 2008 Morey Roof. All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 021110-1307, USA.
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
25 #include <sys/types.h>
27 #include <uuid/uuid.h>
32 #include <linux/loop.h>
33 #include <linux/major.h>
34 #include <linux/kdev_t.h>
36 #include <blkid/blkid.h>
39 #include "kerncompat.h"
40 #include "radix-tree.h"
43 #include "transaction.h"
50 #define BLKDISCARD _IO(0x12,119)
53 static int btrfs_scan_done
= 0;
55 static char argv0_buf
[ARGV0_BUF_SIZE
] = "btrfs";
57 void fixup_argv0(char **argv
, const char *token
)
59 int len
= strlen(argv0_buf
);
61 snprintf(argv0_buf
+ len
, sizeof(argv0_buf
) - len
, " %s", token
);
65 void set_argv0(char **argv
)
67 strncpy(argv0_buf
, argv
[0], sizeof(argv0_buf
));
68 argv0_buf
[sizeof(argv0_buf
) - 1] = 0;
71 int check_argc_exact(int nargs
, int expected
)
74 fprintf(stderr
, "%s: too few arguments\n", argv0_buf
);
76 fprintf(stderr
, "%s: too many arguments\n", argv0_buf
);
78 return nargs
!= expected
;
81 int check_argc_min(int nargs
, int expected
)
83 if (nargs
< expected
) {
84 fprintf(stderr
, "%s: too few arguments\n", argv0_buf
);
91 int check_argc_max(int nargs
, int expected
)
93 if (nargs
> expected
) {
94 fprintf(stderr
, "%s: too many arguments\n", argv0_buf
);
103 * Discard the given range in one go
105 static int discard_range(int fd
, u64 start
, u64 len
)
107 u64 range
[2] = { start
, len
};
109 if (ioctl(fd
, BLKDISCARD
, &range
) < 0)
115 * Discard blocks in the given range in 1G chunks, the process is interruptible
117 static int discard_blocks(int fd
, u64 start
, u64 len
)
121 u64 chunk_size
= min_t(u64
, len
, 1*1024*1024*1024);
124 ret
= discard_range(fd
, start
, chunk_size
);
134 static u64 reference_root_table
[] = {
135 [1] = BTRFS_ROOT_TREE_OBJECTID
,
136 [2] = BTRFS_EXTENT_TREE_OBJECTID
,
137 [3] = BTRFS_CHUNK_TREE_OBJECTID
,
138 [4] = BTRFS_DEV_TREE_OBJECTID
,
139 [5] = BTRFS_FS_TREE_OBJECTID
,
140 [6] = BTRFS_CSUM_TREE_OBJECTID
,
143 int test_uuid_unique(char *fs_uuid
)
146 blkid_dev_iterate iter
= NULL
;
147 blkid_dev dev
= NULL
;
148 blkid_cache cache
= NULL
;
150 if (blkid_get_cache(&cache
, 0) < 0) {
151 printf("ERROR: lblkid cache get failed\n");
154 blkid_probe_all(cache
);
155 iter
= blkid_dev_iterate_begin(cache
);
156 blkid_dev_set_search(iter
, "UUID", fs_uuid
);
158 while (blkid_dev_next(iter
, &dev
) == 0) {
159 dev
= blkid_verify(cache
, dev
);
166 blkid_dev_iterate_end(iter
);
167 blkid_put_cache(cache
);
172 int make_btrfs(int fd
, const char *device
, const char *label
, char *fs_uuid
,
173 u64 blocks
[7], u64 num_bytes
, u32 nodesize
,
174 u32 sectorsize
, u32 stripesize
, u64 features
)
176 struct btrfs_super_block super
;
177 struct extent_buffer
*buf
= NULL
;
178 struct btrfs_root_item root_item
;
179 struct btrfs_disk_key disk_key
;
180 struct btrfs_extent_item
*extent_item
;
181 struct btrfs_inode_item
*inode_item
;
182 struct btrfs_chunk
*chunk
;
183 struct btrfs_dev_item
*dev_item
;
184 struct btrfs_dev_extent
*dev_extent
;
185 u8 chunk_tree_uuid
[BTRFS_UUID_SIZE
];
195 int skinny_metadata
= !!(features
&
196 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA
);
198 first_free
= BTRFS_SUPER_INFO_OFFSET
+ sectorsize
* 2 - 1;
199 first_free
&= ~((u64
)sectorsize
- 1);
201 memset(&super
, 0, sizeof(super
));
203 num_bytes
= (num_bytes
/ sectorsize
) * sectorsize
;
205 if (uuid_parse(fs_uuid
, super
.fsid
) != 0) {
206 fprintf(stderr
, "could not parse UUID: %s\n", fs_uuid
);
210 if (!test_uuid_unique(fs_uuid
)) {
211 fprintf(stderr
, "non-unique UUID: %s\n", fs_uuid
);
216 uuid_generate(super
.fsid
);
218 uuid_generate(super
.dev_item
.uuid
);
219 uuid_generate(chunk_tree_uuid
);
221 btrfs_set_super_bytenr(&super
, blocks
[0]);
222 btrfs_set_super_num_devices(&super
, 1);
223 btrfs_set_super_magic(&super
, BTRFS_MAGIC
);
224 btrfs_set_super_generation(&super
, 1);
225 btrfs_set_super_root(&super
, blocks
[1]);
226 btrfs_set_super_chunk_root(&super
, blocks
[3]);
227 btrfs_set_super_total_bytes(&super
, num_bytes
);
228 btrfs_set_super_bytes_used(&super
, 6 * nodesize
);
229 btrfs_set_super_sectorsize(&super
, sectorsize
);
230 btrfs_set_super_leafsize(&super
, nodesize
);
231 btrfs_set_super_nodesize(&super
, nodesize
);
232 btrfs_set_super_stripesize(&super
, stripesize
);
233 btrfs_set_super_csum_type(&super
, BTRFS_CSUM_TYPE_CRC32
);
234 btrfs_set_super_chunk_root_generation(&super
, 1);
235 btrfs_set_super_cache_generation(&super
, -1);
236 btrfs_set_super_incompat_flags(&super
, features
);
238 strncpy(super
.label
, label
, BTRFS_LABEL_SIZE
- 1);
240 buf
= malloc(sizeof(*buf
) + max(sectorsize
, nodesize
));
242 /* create the tree of root objects */
243 memset(buf
->data
, 0, nodesize
);
245 btrfs_set_header_bytenr(buf
, blocks
[1]);
246 btrfs_set_header_nritems(buf
, 4);
247 btrfs_set_header_generation(buf
, 1);
248 btrfs_set_header_backref_rev(buf
, BTRFS_MIXED_BACKREF_REV
);
249 btrfs_set_header_owner(buf
, BTRFS_ROOT_TREE_OBJECTID
);
250 write_extent_buffer(buf
, super
.fsid
, btrfs_header_fsid(),
253 write_extent_buffer(buf
, chunk_tree_uuid
,
254 btrfs_header_chunk_tree_uuid(buf
),
257 /* create the items for the root tree */
258 memset(&root_item
, 0, sizeof(root_item
));
259 inode_item
= &root_item
.inode
;
260 btrfs_set_stack_inode_generation(inode_item
, 1);
261 btrfs_set_stack_inode_size(inode_item
, 3);
262 btrfs_set_stack_inode_nlink(inode_item
, 1);
263 btrfs_set_stack_inode_nbytes(inode_item
, nodesize
);
264 btrfs_set_stack_inode_mode(inode_item
, S_IFDIR
| 0755);
265 btrfs_set_root_refs(&root_item
, 1);
266 btrfs_set_root_used(&root_item
, nodesize
);
267 btrfs_set_root_generation(&root_item
, 1);
269 memset(&disk_key
, 0, sizeof(disk_key
));
270 btrfs_set_disk_key_type(&disk_key
, BTRFS_ROOT_ITEM_KEY
);
271 btrfs_set_disk_key_offset(&disk_key
, 0);
274 itemoff
= __BTRFS_LEAF_DATA_SIZE(nodesize
) - sizeof(root_item
);
275 btrfs_set_root_bytenr(&root_item
, blocks
[2]);
276 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_EXTENT_TREE_OBJECTID
);
277 btrfs_set_item_key(buf
, &disk_key
, nritems
);
278 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
279 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
281 write_extent_buffer(buf
, &root_item
, btrfs_item_ptr_offset(buf
,
282 nritems
), sizeof(root_item
));
285 itemoff
= itemoff
- sizeof(root_item
);
286 btrfs_set_root_bytenr(&root_item
, blocks
[4]);
287 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_DEV_TREE_OBJECTID
);
288 btrfs_set_item_key(buf
, &disk_key
, nritems
);
289 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
290 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
292 write_extent_buffer(buf
, &root_item
,
293 btrfs_item_ptr_offset(buf
, nritems
),
297 itemoff
= itemoff
- sizeof(root_item
);
298 btrfs_set_root_bytenr(&root_item
, blocks
[5]);
299 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_FS_TREE_OBJECTID
);
300 btrfs_set_item_key(buf
, &disk_key
, nritems
);
301 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
302 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
304 write_extent_buffer(buf
, &root_item
,
305 btrfs_item_ptr_offset(buf
, nritems
),
309 itemoff
= itemoff
- sizeof(root_item
);
310 btrfs_set_root_bytenr(&root_item
, blocks
[6]);
311 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_CSUM_TREE_OBJECTID
);
312 btrfs_set_item_key(buf
, &disk_key
, nritems
);
313 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
314 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
316 write_extent_buffer(buf
, &root_item
,
317 btrfs_item_ptr_offset(buf
, nritems
),
322 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
323 ret
= pwrite(fd
, buf
->data
, nodesize
, blocks
[1]);
324 if (ret
!= nodesize
) {
325 ret
= (ret
< 0 ? -errno
: -EIO
);
329 /* create the items for the extent tree */
330 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
331 nodesize
- sizeof(struct btrfs_header
));
333 itemoff
= __BTRFS_LEAF_DATA_SIZE(nodesize
);
334 for (i
= 1; i
< 7; i
++) {
335 item_size
= sizeof(struct btrfs_extent_item
);
336 if (!skinny_metadata
)
337 item_size
+= sizeof(struct btrfs_tree_block_info
);
339 BUG_ON(blocks
[i
] < first_free
);
340 BUG_ON(blocks
[i
] < blocks
[i
- 1]);
342 /* create extent item */
343 itemoff
-= item_size
;
344 btrfs_set_disk_key_objectid(&disk_key
, blocks
[i
]);
345 if (skinny_metadata
) {
346 btrfs_set_disk_key_type(&disk_key
,
347 BTRFS_METADATA_ITEM_KEY
);
348 btrfs_set_disk_key_offset(&disk_key
, 0);
350 btrfs_set_disk_key_type(&disk_key
,
351 BTRFS_EXTENT_ITEM_KEY
);
352 btrfs_set_disk_key_offset(&disk_key
, nodesize
);
354 btrfs_set_item_key(buf
, &disk_key
, nritems
);
355 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
),
357 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
359 extent_item
= btrfs_item_ptr(buf
, nritems
,
360 struct btrfs_extent_item
);
361 btrfs_set_extent_refs(buf
, extent_item
, 1);
362 btrfs_set_extent_generation(buf
, extent_item
, 1);
363 btrfs_set_extent_flags(buf
, extent_item
,
364 BTRFS_EXTENT_FLAG_TREE_BLOCK
);
367 /* create extent ref */
368 ref_root
= reference_root_table
[i
];
369 btrfs_set_disk_key_objectid(&disk_key
, blocks
[i
]);
370 btrfs_set_disk_key_offset(&disk_key
, ref_root
);
371 btrfs_set_disk_key_type(&disk_key
, BTRFS_TREE_BLOCK_REF_KEY
);
372 btrfs_set_item_key(buf
, &disk_key
, nritems
);
373 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
),
375 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), 0);
378 btrfs_set_header_bytenr(buf
, blocks
[2]);
379 btrfs_set_header_owner(buf
, BTRFS_EXTENT_TREE_OBJECTID
);
380 btrfs_set_header_nritems(buf
, nritems
);
381 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
382 ret
= pwrite(fd
, buf
->data
, nodesize
, blocks
[2]);
383 if (ret
!= nodesize
) {
384 ret
= (ret
< 0 ? -errno
: -EIO
);
388 /* create the chunk tree */
389 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
390 nodesize
- sizeof(struct btrfs_header
));
392 item_size
= sizeof(*dev_item
);
393 itemoff
= __BTRFS_LEAF_DATA_SIZE(nodesize
) - item_size
;
395 /* first device 1 (there is no device 0) */
396 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_DEV_ITEMS_OBJECTID
);
397 btrfs_set_disk_key_offset(&disk_key
, 1);
398 btrfs_set_disk_key_type(&disk_key
, BTRFS_DEV_ITEM_KEY
);
399 btrfs_set_item_key(buf
, &disk_key
, nritems
);
400 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
401 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), item_size
);
403 dev_item
= btrfs_item_ptr(buf
, nritems
, struct btrfs_dev_item
);
404 btrfs_set_device_id(buf
, dev_item
, 1);
405 btrfs_set_device_generation(buf
, dev_item
, 0);
406 btrfs_set_device_total_bytes(buf
, dev_item
, num_bytes
);
407 btrfs_set_device_bytes_used(buf
, dev_item
,
408 BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
409 btrfs_set_device_io_align(buf
, dev_item
, sectorsize
);
410 btrfs_set_device_io_width(buf
, dev_item
, sectorsize
);
411 btrfs_set_device_sector_size(buf
, dev_item
, sectorsize
);
412 btrfs_set_device_type(buf
, dev_item
, 0);
414 write_extent_buffer(buf
, super
.dev_item
.uuid
,
415 (unsigned long)btrfs_device_uuid(dev_item
),
417 write_extent_buffer(buf
, super
.fsid
,
418 (unsigned long)btrfs_device_fsid(dev_item
),
420 read_extent_buffer(buf
, &super
.dev_item
, (unsigned long)dev_item
,
424 item_size
= btrfs_chunk_item_size(1);
425 itemoff
= itemoff
- item_size
;
427 /* then we have chunk 0 */
428 btrfs_set_disk_key_objectid(&disk_key
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
429 btrfs_set_disk_key_offset(&disk_key
, 0);
430 btrfs_set_disk_key_type(&disk_key
, BTRFS_CHUNK_ITEM_KEY
);
431 btrfs_set_item_key(buf
, &disk_key
, nritems
);
432 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
433 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
), item_size
);
435 chunk
= btrfs_item_ptr(buf
, nritems
, struct btrfs_chunk
);
436 btrfs_set_chunk_length(buf
, chunk
, BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
437 btrfs_set_chunk_owner(buf
, chunk
, BTRFS_EXTENT_TREE_OBJECTID
);
438 btrfs_set_chunk_stripe_len(buf
, chunk
, 64 * 1024);
439 btrfs_set_chunk_type(buf
, chunk
, BTRFS_BLOCK_GROUP_SYSTEM
);
440 btrfs_set_chunk_io_align(buf
, chunk
, sectorsize
);
441 btrfs_set_chunk_io_width(buf
, chunk
, sectorsize
);
442 btrfs_set_chunk_sector_size(buf
, chunk
, sectorsize
);
443 btrfs_set_chunk_num_stripes(buf
, chunk
, 1);
444 btrfs_set_stripe_devid_nr(buf
, chunk
, 0, 1);
445 btrfs_set_stripe_offset_nr(buf
, chunk
, 0, 0);
448 write_extent_buffer(buf
, super
.dev_item
.uuid
,
449 (unsigned long)btrfs_stripe_dev_uuid(&chunk
->stripe
),
452 /* copy the key for the chunk to the system array */
453 ptr
= super
.sys_chunk_array
;
454 array_size
= sizeof(disk_key
);
456 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
457 ptr
+= sizeof(disk_key
);
459 /* copy the chunk to the system array */
460 read_extent_buffer(buf
, ptr
, (unsigned long)chunk
, item_size
);
461 array_size
+= item_size
;
463 btrfs_set_super_sys_array_size(&super
, array_size
);
465 btrfs_set_header_bytenr(buf
, blocks
[3]);
466 btrfs_set_header_owner(buf
, BTRFS_CHUNK_TREE_OBJECTID
);
467 btrfs_set_header_nritems(buf
, nritems
);
468 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
469 ret
= pwrite(fd
, buf
->data
, nodesize
, blocks
[3]);
470 if (ret
!= nodesize
) {
471 ret
= (ret
< 0 ? -errno
: -EIO
);
475 /* create the device tree */
476 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
477 nodesize
- sizeof(struct btrfs_header
));
479 itemoff
= __BTRFS_LEAF_DATA_SIZE(nodesize
) -
480 sizeof(struct btrfs_dev_extent
);
482 btrfs_set_disk_key_objectid(&disk_key
, 1);
483 btrfs_set_disk_key_offset(&disk_key
, 0);
484 btrfs_set_disk_key_type(&disk_key
, BTRFS_DEV_EXTENT_KEY
);
485 btrfs_set_item_key(buf
, &disk_key
, nritems
);
486 btrfs_set_item_offset(buf
, btrfs_item_nr(nritems
), itemoff
);
487 btrfs_set_item_size(buf
, btrfs_item_nr(nritems
),
488 sizeof(struct btrfs_dev_extent
));
489 dev_extent
= btrfs_item_ptr(buf
, nritems
, struct btrfs_dev_extent
);
490 btrfs_set_dev_extent_chunk_tree(buf
, dev_extent
,
491 BTRFS_CHUNK_TREE_OBJECTID
);
492 btrfs_set_dev_extent_chunk_objectid(buf
, dev_extent
,
493 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
494 btrfs_set_dev_extent_chunk_offset(buf
, dev_extent
, 0);
496 write_extent_buffer(buf
, chunk_tree_uuid
,
497 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent
),
500 btrfs_set_dev_extent_length(buf
, dev_extent
,
501 BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
504 btrfs_set_header_bytenr(buf
, blocks
[4]);
505 btrfs_set_header_owner(buf
, BTRFS_DEV_TREE_OBJECTID
);
506 btrfs_set_header_nritems(buf
, nritems
);
507 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
508 ret
= pwrite(fd
, buf
->data
, nodesize
, blocks
[4]);
509 if (ret
!= nodesize
) {
510 ret
= (ret
< 0 ? -errno
: -EIO
);
514 /* create the FS root */
515 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
516 nodesize
- sizeof(struct btrfs_header
));
517 btrfs_set_header_bytenr(buf
, blocks
[5]);
518 btrfs_set_header_owner(buf
, BTRFS_FS_TREE_OBJECTID
);
519 btrfs_set_header_nritems(buf
, 0);
520 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
521 ret
= pwrite(fd
, buf
->data
, nodesize
, blocks
[5]);
522 if (ret
!= nodesize
) {
523 ret
= (ret
< 0 ? -errno
: -EIO
);
526 /* finally create the csum root */
527 memset(buf
->data
+ sizeof(struct btrfs_header
), 0,
528 nodesize
- sizeof(struct btrfs_header
));
529 btrfs_set_header_bytenr(buf
, blocks
[6]);
530 btrfs_set_header_owner(buf
, BTRFS_CSUM_TREE_OBJECTID
);
531 btrfs_set_header_nritems(buf
, 0);
532 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
533 ret
= pwrite(fd
, buf
->data
, nodesize
, blocks
[6]);
534 if (ret
!= nodesize
) {
535 ret
= (ret
< 0 ? -errno
: -EIO
);
539 /* and write out the super block */
540 BUG_ON(sizeof(super
) > sectorsize
);
541 memset(buf
->data
, 0, sectorsize
);
542 memcpy(buf
->data
, &super
, sizeof(super
));
543 buf
->len
= sectorsize
;
544 csum_tree_block_size(buf
, BTRFS_CRC32_SIZE
, 0);
545 ret
= pwrite(fd
, buf
->data
, sectorsize
, blocks
[0]);
546 if (ret
!= sectorsize
) {
547 ret
= (ret
< 0 ? -errno
: -EIO
);
558 static const struct btrfs_fs_feature
{
562 } mkfs_features
[] = {
563 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
,
564 "mixed data and metadata block groups" },
565 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF
,
566 "increased hardlink limit per file to 65536" },
567 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56
,
568 "raid56 extended format" },
569 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA
,
570 "reduced-size metadata extent refs" },
571 { "no-holes", BTRFS_FEATURE_INCOMPAT_NO_HOLES
,
572 "no explicit hole extents for files" },
573 /* Keep this one last */
574 { "list-all", BTRFS_FEATURE_LIST_ALL
, NULL
}
577 static int parse_one_fs_feature(const char *name
, u64
*flags
)
582 for (i
= 0; i
< ARRAY_SIZE(mkfs_features
); i
++) {
583 if (name
[0] == '^' &&
584 !strcmp(mkfs_features
[i
].name
, name
+ 1)) {
585 *flags
&= ~ mkfs_features
[i
].flag
;
587 } else if (!strcmp(mkfs_features
[i
].name
, name
)) {
588 *flags
|= mkfs_features
[i
].flag
;
596 void btrfs_parse_features_to_string(char *buf
, u64 flags
)
602 for (i
= 0; i
< ARRAY_SIZE(mkfs_features
); i
++) {
603 if (flags
& mkfs_features
[i
].flag
) {
606 strcat(buf
, mkfs_features
[i
].name
);
611 void btrfs_process_fs_features(u64 flags
)
615 for (i
= 0; i
< ARRAY_SIZE(mkfs_features
); i
++) {
616 if (flags
& mkfs_features
[i
].flag
) {
617 printf("Turning ON incompat feature '%s': %s\n",
618 mkfs_features
[i
].name
,
619 mkfs_features
[i
].desc
);
624 void btrfs_list_all_fs_features(u64 mask_disallowed
)
628 fprintf(stderr
, "Filesystem features available:\n");
629 for (i
= 0; i
< ARRAY_SIZE(mkfs_features
) - 1; i
++) {
630 char *is_default
= "";
632 if (mkfs_features
[i
].flag
& mask_disallowed
)
634 if (mkfs_features
[i
].flag
& BTRFS_MKFS_DEFAULT_FEATURES
)
635 is_default
= ", default";
636 fprintf(stderr
, "%-20s- %s (0x%llx%s)\n",
637 mkfs_features
[i
].name
,
638 mkfs_features
[i
].desc
,
639 mkfs_features
[i
].flag
,
645 * Return NULL if all features were parsed fine, otherwise return the name of
646 * the first unparsed.
648 char* btrfs_parse_fs_features(char *namelist
, u64
*flags
)
651 char *save_ptr
= NULL
; /* Satisfy static checkers */
653 for (this_char
= strtok_r(namelist
, ",", &save_ptr
);
655 this_char
= strtok_r(NULL
, ",", &save_ptr
)) {
656 if (parse_one_fs_feature(this_char
, flags
))
663 u64
btrfs_device_size(int fd
, struct stat
*st
)
666 if (S_ISREG(st
->st_mode
)) {
669 if (!S_ISBLK(st
->st_mode
)) {
672 if (ioctl(fd
, BLKGETSIZE64
, &size
) >= 0) {
678 static int zero_blocks(int fd
, off_t start
, size_t len
)
680 char *buf
= malloc(len
);
687 written
= pwrite(fd
, buf
, len
, start
);
694 #define ZERO_DEV_BYTES (2 * 1024 * 1024)
696 /* don't write outside the device by clamping the region to the device size */
697 static int zero_dev_clamped(int fd
, off_t start
, ssize_t len
, u64 dev_size
)
699 off_t end
= max(start
, start
+ len
);
702 /* and don't overwrite the disk labels on sparc */
703 start
= max(start
, 1024);
704 end
= max(end
, 1024);
707 start
= min_t(u64
, start
, dev_size
);
708 end
= min_t(u64
, end
, dev_size
);
710 return zero_blocks(fd
, start
, end
- start
);
713 int btrfs_add_to_fsid(struct btrfs_trans_handle
*trans
,
714 struct btrfs_root
*root
, int fd
, char *path
,
715 u64 block_count
, u32 io_width
, u32 io_align
,
718 struct btrfs_super_block
*disk_super
;
719 struct btrfs_super_block
*super
= root
->fs_info
->super_copy
;
720 struct btrfs_device
*device
;
721 struct btrfs_dev_item
*dev_item
;
727 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
730 buf
= kmalloc(sectorsize
, GFP_NOFS
);
735 BUG_ON(sizeof(*disk_super
) > sectorsize
);
736 memset(buf
, 0, sectorsize
);
738 disk_super
= (struct btrfs_super_block
*)buf
;
739 dev_item
= &disk_super
->dev_item
;
741 uuid_generate(device
->uuid
);
744 device
->io_width
= io_width
;
745 device
->io_align
= io_align
;
746 device
->sector_size
= sectorsize
;
748 device
->writeable
= 1;
749 device
->total_bytes
= block_count
;
750 device
->bytes_used
= 0;
751 device
->total_ios
= 0;
752 device
->dev_root
= root
->fs_info
->dev_root
;
754 ret
= btrfs_add_device(trans
, root
, device
);
757 total_bytes
= btrfs_super_total_bytes(super
) + block_count
;
758 btrfs_set_super_total_bytes(super
, total_bytes
);
760 num_devs
= btrfs_super_num_devices(super
) + 1;
761 btrfs_set_super_num_devices(super
, num_devs
);
763 memcpy(disk_super
, super
, sizeof(*disk_super
));
765 printf("adding device %s id %llu\n", path
,
766 (unsigned long long)device
->devid
);
768 btrfs_set_super_bytenr(disk_super
, BTRFS_SUPER_INFO_OFFSET
);
769 btrfs_set_stack_device_id(dev_item
, device
->devid
);
770 btrfs_set_stack_device_type(dev_item
, device
->type
);
771 btrfs_set_stack_device_io_align(dev_item
, device
->io_align
);
772 btrfs_set_stack_device_io_width(dev_item
, device
->io_width
);
773 btrfs_set_stack_device_sector_size(dev_item
, device
->sector_size
);
774 btrfs_set_stack_device_total_bytes(dev_item
, device
->total_bytes
);
775 btrfs_set_stack_device_bytes_used(dev_item
, device
->bytes_used
);
776 memcpy(&dev_item
->uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
778 ret
= pwrite(fd
, buf
, sectorsize
, BTRFS_SUPER_INFO_OFFSET
);
779 BUG_ON(ret
!= sectorsize
);
782 list_add(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
783 device
->fs_devices
= root
->fs_info
->fs_devices
;
787 static void btrfs_wipe_existing_sb(int fd
)
789 const char *off
= NULL
;
794 blkid_probe pr
= NULL
;
796 pr
= blkid_new_probe();
800 if (blkid_probe_set_device(pr
, fd
, 0, 0))
803 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC_OFFSET", &off
, NULL
);
805 rc
= blkid_probe_lookup_value(pr
, "SBMAGIC", NULL
, &len
);
807 if (rc
|| len
== 0 || off
== NULL
)
810 offset
= strtoll(off
, NULL
, 10);
811 if (len
> sizeof(buf
))
815 rc
= pwrite(fd
, buf
, len
, offset
);
819 blkid_free_probe(pr
);
823 int btrfs_prepare_device(int fd
, char *file
, int zero_end
, u64
*block_count_ret
,
824 u64 max_block_count
, int *mixed
, int discard
)
830 ret
= fstat(fd
, &st
);
832 fprintf(stderr
, "unable to stat %s\n", file
);
836 block_count
= btrfs_device_size(fd
, &st
);
837 if (block_count
== 0) {
838 fprintf(stderr
, "unable to find %s size\n", file
);
842 block_count
= min(block_count
, max_block_count
);
844 if (block_count
< BTRFS_MKFS_SMALL_VOLUME_SIZE
&& !(*mixed
))
849 * We intentionally ignore errors from the discard ioctl. It
850 * is not necessary for the mkfs functionality but just an
853 if (discard_range(fd
, 0, 0) == 0) {
854 printf("Performing full device TRIM (%s) ...\n",
855 pretty_size(block_count
));
856 discard_blocks(fd
, 0, block_count
);
860 ret
= zero_dev_clamped(fd
, 0, ZERO_DEV_BYTES
, block_count
);
861 for (i
= 0 ; !ret
&& i
< BTRFS_SUPER_MIRROR_MAX
; i
++)
862 ret
= zero_dev_clamped(fd
, btrfs_sb_offset(i
),
863 BTRFS_SUPER_INFO_SIZE
, block_count
);
864 if (!ret
&& zero_end
)
865 ret
= zero_dev_clamped(fd
, block_count
- ZERO_DEV_BYTES
,
866 ZERO_DEV_BYTES
, block_count
);
869 fprintf(stderr
, "ERROR: failed to zero device '%s' - %s\n",
870 file
, strerror(-ret
));
874 btrfs_wipe_existing_sb(fd
);
876 *block_count_ret
= block_count
;
880 int btrfs_make_root_dir(struct btrfs_trans_handle
*trans
,
881 struct btrfs_root
*root
, u64 objectid
)
884 struct btrfs_inode_item inode_item
;
885 time_t now
= time(NULL
);
887 memset(&inode_item
, 0, sizeof(inode_item
));
888 btrfs_set_stack_inode_generation(&inode_item
, trans
->transid
);
889 btrfs_set_stack_inode_size(&inode_item
, 0);
890 btrfs_set_stack_inode_nlink(&inode_item
, 1);
891 btrfs_set_stack_inode_nbytes(&inode_item
, root
->nodesize
);
892 btrfs_set_stack_inode_mode(&inode_item
, S_IFDIR
| 0755);
893 btrfs_set_stack_timespec_sec(&inode_item
.atime
, now
);
894 btrfs_set_stack_timespec_nsec(&inode_item
.atime
, 0);
895 btrfs_set_stack_timespec_sec(&inode_item
.ctime
, now
);
896 btrfs_set_stack_timespec_nsec(&inode_item
.ctime
, 0);
897 btrfs_set_stack_timespec_sec(&inode_item
.mtime
, now
);
898 btrfs_set_stack_timespec_nsec(&inode_item
.mtime
, 0);
899 btrfs_set_stack_timespec_sec(&inode_item
.otime
, 0);
900 btrfs_set_stack_timespec_nsec(&inode_item
.otime
, 0);
902 if (root
->fs_info
->tree_root
== root
)
903 btrfs_set_super_root_dir(root
->fs_info
->super_copy
, objectid
);
905 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
909 ret
= btrfs_insert_inode_ref(trans
, root
, "..", 2, objectid
, objectid
, 0);
913 btrfs_set_root_dirid(&root
->root_item
, objectid
);
920 * checks if a path is a block device node
921 * Returns negative errno on failure, otherwise
922 * returns 1 for blockdev, 0 for not-blockdev
924 int is_block_device(const char *path
)
928 if (stat(path
, &statbuf
) < 0)
931 return S_ISBLK(statbuf
.st_mode
);
935 * check if given path is a mount point
936 * return 1 if yes. 0 if no. -1 for error
938 int is_mount_point(const char *path
)
944 f
= setmntent("/proc/self/mounts", "r");
948 while ((mnt
= getmntent(f
)) != NULL
) {
949 if (strcmp(mnt
->mnt_dir
, path
))
958 static int is_reg_file(const char *path
)
962 if (stat(path
, &statbuf
) < 0)
964 return S_ISREG(statbuf
.st_mode
);
968 * This function checks if the given input parameter is
970 * return <0 : some error in the given input
971 * return BTRFS_ARG_UNKNOWN: unknown input
972 * return BTRFS_ARG_UUID: given input is uuid
973 * return BTRFS_ARG_MNTPOINT: given input is path
974 * return BTRFS_ARG_REG: given input is regular file
976 int check_arg_type(const char *input
)
984 if (realpath(input
, path
)) {
985 if (is_block_device(path
) == 1)
986 return BTRFS_ARG_BLKDEV
;
988 if (is_mount_point(path
) == 1)
989 return BTRFS_ARG_MNTPOINT
;
991 if (is_reg_file(path
))
992 return BTRFS_ARG_REG
;
994 return BTRFS_ARG_UNKNOWN
;
997 if (strlen(input
) == (BTRFS_UUID_UNPARSED_SIZE
- 1) &&
998 !uuid_parse(input
, uuid
))
999 return BTRFS_ARG_UUID
;
1001 return BTRFS_ARG_UNKNOWN
;
1005 * Find the mount point for a mounted device.
1006 * On success, returns 0 with mountpoint in *mp.
1007 * On failure, returns -errno (not mounted yields -EINVAL)
1008 * Is noisy on failures, expects to be given a mounted device.
1010 int get_btrfs_mount(const char *dev
, char *mp
, size_t mp_size
)
1015 ret
= is_block_device(dev
);
1018 fprintf(stderr
, "%s is not a block device\n", dev
);
1021 fprintf(stderr
, "Could not check %s: %s\n",
1022 dev
, strerror(-ret
));
1027 fd
= open(dev
, O_RDONLY
);
1030 fprintf(stderr
, "Could not open %s: %s\n", dev
, strerror(errno
));
1034 ret
= check_mounted_where(fd
, dev
, mp
, mp_size
, NULL
);
1037 } else { /* mounted, all good */
1047 * Given a pathname, return a filehandle to:
1048 * the original pathname or,
1049 * if the pathname is a mounted btrfs device, to its mountpoint.
1051 * On error, return -1, errno should be set.
1053 int open_path_or_dev_mnt(const char *path
, DIR **dirstream
)
1055 char mp
[BTRFS_PATH_NAME_MAX
+ 1];
1058 if (is_block_device(path
)) {
1061 ret
= get_btrfs_mount(path
, mp
, sizeof(mp
));
1063 /* not a mounted btrfs dev */
1067 fdmnt
= open_file_or_dir(mp
, dirstream
);
1069 fdmnt
= open_file_or_dir(path
, dirstream
);
1075 /* checks if a device is a loop device */
1076 static int is_loop_device (const char* device
) {
1077 struct stat statbuf
;
1079 if(stat(device
, &statbuf
) < 0)
1082 return (S_ISBLK(statbuf
.st_mode
) &&
1083 MAJOR(statbuf
.st_rdev
) == LOOP_MAJOR
);
1087 /* Takes a loop device path (e.g. /dev/loop0) and returns
1088 * the associated file (e.g. /images/my_btrfs.img) */
1089 static int resolve_loop_device(const char* loop_dev
, char* loop_file
,
1096 char real_loop_dev
[PATH_MAX
];
1098 if (!realpath(loop_dev
, real_loop_dev
))
1100 snprintf(p
, PATH_MAX
, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev
, '/'));
1101 if (!(f
= fopen(p
, "r")))
1104 snprintf(fmt
, 20, "%%%i[^\n]", max_len
-1);
1105 ret
= fscanf(f
, fmt
, loop_file
);
1114 * Checks whether a and b are identical or device
1115 * files associated with the same block device
1117 static int is_same_blk_file(const char* a
, const char* b
)
1119 struct stat st_buf_a
, st_buf_b
;
1120 char real_a
[PATH_MAX
];
1121 char real_b
[PATH_MAX
];
1123 if (!realpath(a
, real_a
))
1124 strncpy_null(real_a
, a
);
1126 if (!realpath(b
, real_b
))
1127 strncpy_null(real_b
, b
);
1129 /* Identical path? */
1130 if (strcmp(real_a
, real_b
) == 0)
1133 if (stat(a
, &st_buf_a
) < 0 || stat(b
, &st_buf_b
) < 0) {
1134 if (errno
== ENOENT
)
1139 /* Same blockdevice? */
1140 if (S_ISBLK(st_buf_a
.st_mode
) && S_ISBLK(st_buf_b
.st_mode
) &&
1141 st_buf_a
.st_rdev
== st_buf_b
.st_rdev
) {
1146 if (st_buf_a
.st_dev
== st_buf_b
.st_dev
&&
1147 st_buf_a
.st_ino
== st_buf_b
.st_ino
) {
1154 /* checks if a and b are identical or device
1155 * files associated with the same block device or
1156 * if one file is a loop device that uses the other
1159 static int is_same_loop_file(const char* a
, const char* b
)
1161 char res_a
[PATH_MAX
];
1162 char res_b
[PATH_MAX
];
1163 const char* final_a
= NULL
;
1164 const char* final_b
= NULL
;
1167 /* Resolve a if it is a loop device */
1168 if((ret
= is_loop_device(a
)) < 0) {
1173 ret
= resolve_loop_device(a
, res_a
, sizeof(res_a
));
1184 /* Resolve b if it is a loop device */
1185 if ((ret
= is_loop_device(b
)) < 0) {
1190 ret
= resolve_loop_device(b
, res_b
, sizeof(res_b
));
1201 return is_same_blk_file(final_a
, final_b
);
1204 /* Checks if a file exists and is a block or regular file*/
1205 static int is_existing_blk_or_reg_file(const char* filename
)
1209 if(stat(filename
, &st_buf
) < 0) {
1216 return (S_ISBLK(st_buf
.st_mode
) || S_ISREG(st_buf
.st_mode
));
1219 /* Checks if a file is used (directly or indirectly via a loop device)
1220 * by a device in fs_devices
1222 static int blk_file_in_dev_list(struct btrfs_fs_devices
* fs_devices
,
1226 struct list_head
*head
;
1227 struct list_head
*cur
;
1228 struct btrfs_device
*device
;
1230 head
= &fs_devices
->devices
;
1231 list_for_each(cur
, head
) {
1232 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1234 if((ret
= is_same_loop_file(device
->name
, file
)))
1242 * Resolve a pathname to a device mapper node to /dev/mapper/<name>
1243 * Returns NULL on invalid input or malloc failure; Other failures
1244 * will be handled by the caller using the input pathame.
1246 char *canonicalize_dm_name(const char *ptname
)
1250 char path
[PATH_MAX
], name
[PATH_MAX
], *res
= NULL
;
1252 if (!ptname
|| !*ptname
)
1255 snprintf(path
, sizeof(path
), "/sys/block/%s/dm/name", ptname
);
1256 if (!(f
= fopen(path
, "r")))
1259 /* read <name>\n from sysfs */
1260 if (fgets(name
, sizeof(name
), f
) && (sz
= strlen(name
)) > 1) {
1261 name
[sz
- 1] = '\0';
1262 snprintf(path
, sizeof(path
), "/dev/mapper/%s", name
);
1264 if (access(path
, F_OK
) == 0)
1272 * Resolve a pathname to a canonical device node, e.g. /dev/sda1 or
1273 * to a device mapper pathname.
1274 * Returns NULL on invalid input or malloc failure; Other failures
1275 * will be handled by the caller using the input pathame.
1277 char *canonicalize_path(const char *path
)
1279 char *canonical
, *p
;
1281 if (!path
|| !*path
)
1284 canonical
= realpath(path
, NULL
);
1286 return strdup(path
);
1287 p
= strrchr(canonical
, '/');
1288 if (p
&& strncmp(p
, "/dm-", 4) == 0 && isdigit(*(p
+ 4))) {
1289 char *dm
= canonicalize_dm_name(p
+ 1);
1300 * returns 1 if the device was mounted, < 0 on error or 0 if everything
1301 * is safe to continue.
1303 int check_mounted(const char* file
)
1308 fd
= open(file
, O_RDONLY
);
1310 fprintf (stderr
, "check_mounted(): Could not open %s\n", file
);
1314 ret
= check_mounted_where(fd
, file
, NULL
, 0, NULL
);
1320 int check_mounted_where(int fd
, const char *file
, char *where
, int size
,
1321 struct btrfs_fs_devices
**fs_dev_ret
)
1326 struct btrfs_fs_devices
*fs_devices_mnt
= NULL
;
1330 /* scan the initial device */
1331 ret
= btrfs_scan_one_device(fd
, file
, &fs_devices_mnt
,
1332 &total_devs
, BTRFS_SUPER_INFO_OFFSET
, 0);
1333 is_btrfs
= (ret
>= 0);
1335 /* scan other devices */
1336 if (is_btrfs
&& total_devs
> 1) {
1337 ret
= btrfs_scan_lblkid();
1342 /* iterate over the list of currently mountes filesystems */
1343 if ((f
= setmntent ("/proc/self/mounts", "r")) == NULL
)
1346 while ((mnt
= getmntent (f
)) != NULL
) {
1348 if(strcmp(mnt
->mnt_type
, "btrfs") != 0)
1351 ret
= blk_file_in_dev_list(fs_devices_mnt
, mnt
->mnt_fsname
);
1353 /* ignore entries in the mount table that are not
1354 associated with a file*/
1355 if((ret
= is_existing_blk_or_reg_file(mnt
->mnt_fsname
)) < 0)
1356 goto out_mntloop_err
;
1360 ret
= is_same_loop_file(file
, mnt
->mnt_fsname
);
1364 goto out_mntloop_err
;
1369 /* Did we find an entry in mnt table? */
1370 if (mnt
&& size
&& where
) {
1371 strncpy(where
, mnt
->mnt_dir
, size
);
1375 *fs_dev_ret
= fs_devices_mnt
;
1377 ret
= (mnt
!= NULL
);
1385 struct pending_dir
{
1386 struct list_head list
;
1387 char name
[PATH_MAX
];
1390 int btrfs_register_one_device(const char *fname
)
1392 struct btrfs_ioctl_vol_args args
;
1397 fd
= open("/dev/btrfs-control", O_RDWR
);
1399 fprintf(stderr
, "failed to open /dev/btrfs-control "
1400 "skipping device registration: %s\n",
1404 strncpy(args
.name
, fname
, BTRFS_PATH_NAME_MAX
);
1405 args
.name
[BTRFS_PATH_NAME_MAX
-1] = 0;
1406 ret
= ioctl(fd
, BTRFS_IOC_SCAN_DEV
, &args
);
1409 fprintf(stderr
, "ERROR: device scan failed '%s' - %s\n",
1410 fname
, strerror(e
));
1418 * Register all devices in the fs_uuid list created in the user
1419 * space. Ensure btrfs_scan_lblkid() is called before this func.
1421 int btrfs_register_all_devices(void)
1424 struct btrfs_fs_devices
*fs_devices
;
1425 struct btrfs_device
*device
;
1426 struct list_head
*all_uuids
;
1428 all_uuids
= btrfs_scanned_uuids();
1430 list_for_each_entry(fs_devices
, all_uuids
, list
) {
1431 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
1432 if (strlen(device
->name
) != 0) {
1433 err
= btrfs_register_one_device(device
->name
);
1444 int btrfs_device_already_in_root(struct btrfs_root
*root
, int fd
,
1447 struct btrfs_super_block
*disk_super
;
1451 buf
= malloc(BTRFS_SUPER_INFO_SIZE
);
1456 ret
= pread(fd
, buf
, BTRFS_SUPER_INFO_SIZE
, super_offset
);
1457 if (ret
!= BTRFS_SUPER_INFO_SIZE
)
1461 disk_super
= (struct btrfs_super_block
*)buf
;
1462 if (btrfs_super_magic(disk_super
) != BTRFS_MAGIC
)
1465 if (!memcmp(disk_super
->fsid
, root
->fs_info
->super_copy
->fsid
,
1474 static const char* unit_suffix_binary
[] =
1475 { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
1476 static const char* unit_suffix_decimal
[] =
1477 { "B", "kB", "MB", "GB", "TB", "PB", "EB"};
1479 int pretty_size_snprintf(u64 size
, char *str
, size_t str_size
, unsigned unit_mode
)
1485 const char** suffix
= NULL
;
1491 if ((unit_mode
& ~UNITS_MODE_MASK
) == UNITS_RAW
) {
1492 snprintf(str
, str_size
, "%llu", size
);
1496 if ((unit_mode
& ~UNITS_MODE_MASK
) == UNITS_BINARY
) {
1499 suffix
= unit_suffix_binary
;
1500 } else if ((unit_mode
& ~UNITS_MODE_MASK
) == UNITS_DECIMAL
) {
1503 suffix
= unit_suffix_decimal
;
1508 fprintf(stderr
, "INTERNAL ERROR: unknown unit base, mode %d\n",
1516 switch (unit_mode
& UNITS_MODE_MASK
) {
1517 case UNITS_TBYTES
: base
*= mult
; num_divs
++;
1518 case UNITS_GBYTES
: base
*= mult
; num_divs
++;
1519 case UNITS_MBYTES
: base
*= mult
; num_divs
++;
1520 case UNITS_KBYTES
: num_divs
++;
1527 while (size
>= mult
) {
1534 if (num_divs
>= ARRAY_SIZE(unit_suffix_binary
)) {
1536 printf("INTERNAL ERROR: unsupported unit suffix, index %d\n",
1541 fraction
= (float)last_size
/ base
;
1543 return snprintf(str
, str_size
, "%.2f%s", fraction
, suffix
[num_divs
]);
1547 * __strncpy__null - strncpy with null termination
1548 * @dest: the target array
1549 * @src: the source string
1550 * @n: maximum bytes to copy (size of *dest)
1552 * Like strncpy, but ensures destination is null-terminated.
1554 * Copies the string pointed to by src, including the terminating null
1555 * byte ('\0'), to the buffer pointed to by dest, up to a maximum
1556 * of n bytes. Then ensure that dest is null-terminated.
1558 char *__strncpy__null(char *dest
, const char *src
, size_t n
)
1560 strncpy(dest
, src
, n
);
1567 * Checks to make sure that the label matches our requirements.
1569 0 if everything is safe and usable
1570 -1 if the label is too long
1572 static int check_label(const char *input
)
1574 int len
= strlen(input
);
1576 if (len
> BTRFS_LABEL_SIZE
- 1) {
1577 fprintf(stderr
, "ERROR: Label %s is too long (max %d)\n",
1578 input
, BTRFS_LABEL_SIZE
- 1);
1585 static int set_label_unmounted(const char *dev
, const char *label
)
1587 struct btrfs_trans_handle
*trans
;
1588 struct btrfs_root
*root
;
1591 ret
= check_mounted(dev
);
1593 fprintf(stderr
, "FATAL: error checking %s mount status\n", dev
);
1597 fprintf(stderr
, "ERROR: dev %s is mounted, use mount point\n",
1602 /* Open the super_block at the default location
1603 * and as read-write.
1605 root
= open_ctree(dev
, 0, OPEN_CTREE_WRITES
);
1606 if (!root
) /* errors are printed by open_ctree() */
1609 trans
= btrfs_start_transaction(root
, 1);
1610 snprintf(root
->fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
, "%s",
1612 btrfs_commit_transaction(trans
, root
);
1614 /* Now we close it since we are done. */
1619 static int set_label_mounted(const char *mount_path
, const char *label
)
1623 fd
= open(mount_path
, O_RDONLY
| O_NOATIME
);
1625 fprintf(stderr
, "ERROR: unable to access '%s'\n", mount_path
);
1629 if (ioctl(fd
, BTRFS_IOC_SET_FSLABEL
, label
) < 0) {
1630 fprintf(stderr
, "ERROR: unable to set label %s\n",
1640 static int get_label_unmounted(const char *dev
, char *label
)
1642 struct btrfs_root
*root
;
1645 ret
= check_mounted(dev
);
1647 fprintf(stderr
, "FATAL: error checking %s mount status\n", dev
);
1651 fprintf(stderr
, "ERROR: dev %s is mounted, use mount point\n",
1656 /* Open the super_block at the default location
1659 root
= open_ctree(dev
, 0, 0);
1663 memcpy(label
, root
->fs_info
->super_copy
->label
, BTRFS_LABEL_SIZE
);
1665 /* Now we close it since we are done. */
1671 * If a partition is mounted, try to get the filesystem label via its
1672 * mounted path rather than device. Return the corresponding error
1673 * the user specified the device path.
1675 int get_label_mounted(const char *mount_path
, char *labelp
)
1677 char label
[BTRFS_LABEL_SIZE
];
1680 fd
= open(mount_path
, O_RDONLY
| O_NOATIME
);
1682 fprintf(stderr
, "ERROR: unable to access '%s'\n", mount_path
);
1686 memset(label
, '\0', sizeof(label
));
1687 if (ioctl(fd
, BTRFS_IOC_GET_FSLABEL
, label
) < 0) {
1688 fprintf(stderr
, "ERROR: unable get label %s\n", strerror(errno
));
1693 strncpy(labelp
, label
, sizeof(label
));
1698 int get_label(const char *btrfs_dev
, char *label
)
1702 ret
= is_existing_blk_or_reg_file(btrfs_dev
);
1704 ret
= get_label_mounted(btrfs_dev
, label
);
1706 ret
= get_label_unmounted(btrfs_dev
, label
);
1711 int set_label(const char *btrfs_dev
, const char *label
)
1715 if (check_label(label
))
1718 ret
= is_existing_blk_or_reg_file(btrfs_dev
);
1720 ret
= set_label_mounted(btrfs_dev
, label
);
1722 ret
= set_label_unmounted(btrfs_dev
, label
);
1728 * Unsafe subvolume check.
1730 * This only checks ino == BTRFS_FIRST_FREE_OBJECTID, even it is not in a
1731 * btrfs mount point.
1732 * Must use together with other reliable method like btrfs ioctl.
1734 static int __is_subvol(const char *path
)
1739 ret
= lstat(path
, &st
);
1743 return st
.st_ino
== BTRFS_FIRST_FREE_OBJECTID
;
1747 * A not-so-good version fls64. No fascinating optimization since
1748 * no one except parse_size use it
1750 static int fls64(u64 x
)
1754 for (i
= 0; i
<64; i
++)
1755 if (x
<< i
& (1ULL << 63))
1760 u64
parse_size(char *s
)
1768 fprintf(stderr
, "ERROR: Size value is empty\n");
1773 "ERROR: Size value '%s' is less equal than 0\n", s
);
1776 ret
= strtoull(s
, &endptr
, 10);
1778 fprintf(stderr
, "ERROR: Size value '%s' is invalid\n", s
);
1781 if (endptr
[0] && endptr
[1]) {
1782 fprintf(stderr
, "ERROR: Illegal suffix contains character '%c' in wrong position\n",
1787 * strtoll returns LLONG_MAX when overflow, if this happens,
1788 * need to call strtoull to get the real size
1790 if (errno
== ERANGE
&& ret
== ULLONG_MAX
) {
1792 "ERROR: Size value '%s' is too large for u64\n", s
);
1796 c
= tolower(endptr
[0]);
1819 fprintf(stderr
, "ERROR: Unknown size descriptor '%c'\n",
1824 /* Check whether ret * mult overflow */
1825 if (fls64(ret
) + fls64(mult
) - 1 > 64) {
1827 "ERROR: Size value '%s' is too large for u64\n", s
);
1834 u64
parse_qgroupid(const char *p
)
1836 char *s
= strchr(p
, '/');
1837 const char *ptr_src_end
= p
+ strlen(p
);
1838 char *ptr_parse_end
= NULL
;
1847 /* Numeric format like '0/257' is the primary case */
1849 id
= strtoull(p
, &ptr_parse_end
, 10);
1850 if (ptr_parse_end
!= ptr_src_end
)
1854 level
= strtoull(p
, &ptr_parse_end
, 10);
1855 if (ptr_parse_end
!= s
)
1858 id
= strtoull(s
+ 1, &ptr_parse_end
, 10);
1859 if (ptr_parse_end
!= ptr_src_end
)
1862 return (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
1865 /* Path format like subv at 'my_subvol' is the fallback case */
1866 ret
= __is_subvol(p
);
1867 if (ret
< 0 || !ret
)
1869 fd
= open(p
, O_RDONLY
);
1872 ret
= lookup_ino_rootid(fd
, &id
);
1879 fprintf(stderr
, "ERROR: invalid qgroupid or subvolume path: %s\n", p
);
1883 int open_file_or_dir3(const char *fname
, DIR **dirstream
, int open_flags
)
1889 ret
= stat(fname
, &st
);
1893 if (S_ISDIR(st
.st_mode
)) {
1894 *dirstream
= opendir(fname
);
1897 fd
= dirfd(*dirstream
);
1898 } else if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
1899 fd
= open(fname
, open_flags
);
1902 * we set this on purpose, in case the caller output
1903 * strerror(errno) as success
1911 closedir(*dirstream
);
1916 int open_file_or_dir(const char *fname
, DIR **dirstream
)
1918 return open_file_or_dir3(fname
, dirstream
, O_RDWR
);
1921 void close_file_or_dir(int fd
, DIR *dirstream
)
1924 closedir(dirstream
);
1929 int get_device_info(int fd
, u64 devid
,
1930 struct btrfs_ioctl_dev_info_args
*di_args
)
1934 di_args
->devid
= devid
;
1935 memset(&di_args
->uuid
, '\0', sizeof(di_args
->uuid
));
1937 ret
= ioctl(fd
, BTRFS_IOC_DEV_INFO
, di_args
);
1938 return ret
? -errno
: 0;
1941 static u64
find_max_device_id(struct btrfs_ioctl_search_args
*search_args
,
1944 struct btrfs_dev_item
*dev_item
;
1945 char *buf
= search_args
->buf
;
1947 buf
+= (nr_items
- 1) * (sizeof(struct btrfs_ioctl_search_header
)
1948 + sizeof(struct btrfs_dev_item
));
1949 buf
+= sizeof(struct btrfs_ioctl_search_header
);
1951 dev_item
= (struct btrfs_dev_item
*)buf
;
1953 return btrfs_stack_device_id(dev_item
);
1956 static int search_chunk_tree_for_fs_info(int fd
,
1957 struct btrfs_ioctl_fs_info_args
*fi_args
)
1961 u64 start_devid
= 1;
1962 struct btrfs_ioctl_search_args search_args
;
1963 struct btrfs_ioctl_search_key
*search_key
= &search_args
.key
;
1965 fi_args
->num_devices
= 0;
1967 max_items
= BTRFS_SEARCH_ARGS_BUFSIZE
1968 / (sizeof(struct btrfs_ioctl_search_header
)
1969 + sizeof(struct btrfs_dev_item
));
1971 search_key
->tree_id
= BTRFS_CHUNK_TREE_OBJECTID
;
1972 search_key
->min_objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1973 search_key
->max_objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1974 search_key
->min_type
= BTRFS_DEV_ITEM_KEY
;
1975 search_key
->max_type
= BTRFS_DEV_ITEM_KEY
;
1976 search_key
->min_transid
= 0;
1977 search_key
->max_transid
= (u64
)-1;
1978 search_key
->nr_items
= max_items
;
1979 search_key
->max_offset
= (u64
)-1;
1982 search_key
->min_offset
= start_devid
;
1984 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &search_args
);
1988 fi_args
->num_devices
+= (u64
)search_key
->nr_items
;
1990 if (search_key
->nr_items
== max_items
) {
1991 start_devid
= find_max_device_id(&search_args
,
1992 search_key
->nr_items
) + 1;
1996 /* get the lastest max_id to stay consistent with the num_devices */
1997 if (search_key
->nr_items
== 0)
1999 * last tree_search returns an empty buf, use the devid of
2000 * the last dev_item of the previous tree_search
2002 fi_args
->max_id
= start_devid
- 1;
2004 fi_args
->max_id
= find_max_device_id(&search_args
,
2005 search_key
->nr_items
);
2011 * For a given path, fill in the ioctl fs_ and info_ args.
2012 * If the path is a btrfs mountpoint, fill info for all devices.
2013 * If the path is a btrfs device, fill in only that device.
2015 * The path provided must be either on a mounted btrfs fs,
2016 * or be a mounted btrfs device.
2018 * Returns 0 on success, or a negative errno.
2020 int get_fs_info(char *path
, struct btrfs_ioctl_fs_info_args
*fi_args
,
2021 struct btrfs_ioctl_dev_info_args
**di_ret
)
2028 struct btrfs_fs_devices
*fs_devices_mnt
= NULL
;
2029 struct btrfs_ioctl_dev_info_args
*di_args
;
2030 struct btrfs_ioctl_dev_info_args tmp
;
2031 char mp
[BTRFS_PATH_NAME_MAX
+ 1];
2032 DIR *dirstream
= NULL
;
2034 memset(fi_args
, 0, sizeof(*fi_args
));
2036 if (is_block_device(path
)) {
2037 struct btrfs_super_block
*disk_super
;
2038 char buf
[BTRFS_SUPER_INFO_SIZE
];
2041 /* Ensure it's mounted, then set path to the mountpoint */
2042 fd
= open(path
, O_RDONLY
);
2045 fprintf(stderr
, "Couldn't open %s: %s\n",
2046 path
, strerror(errno
));
2049 ret
= check_mounted_where(fd
, path
, mp
, sizeof(mp
),
2058 /* Only fill in this one device */
2059 fi_args
->num_devices
= 1;
2061 disk_super
= (struct btrfs_super_block
*)buf
;
2062 ret
= btrfs_read_dev_super(fd
, disk_super
,
2063 BTRFS_SUPER_INFO_OFFSET
, 0);
2068 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
2070 fi_args
->max_id
= devid
;
2073 memcpy(fi_args
->fsid
, fs_devices_mnt
->fsid
, BTRFS_FSID_SIZE
);
2077 /* at this point path must not be for a block device */
2078 fd
= open_file_or_dir(path
, &dirstream
);
2084 /* fill in fi_args if not just a single device */
2085 if (fi_args
->num_devices
!= 1) {
2086 ret
= ioctl(fd
, BTRFS_IOC_FS_INFO
, fi_args
);
2093 * The fs_args->num_devices does not include seed devices
2095 ret
= search_chunk_tree_for_fs_info(fd
, fi_args
);
2100 * search_chunk_tree_for_fs_info() will lacks the devid 0
2101 * so manual probe for it here.
2103 ret
= get_device_info(fd
, 0, &tmp
);
2105 fi_args
->num_devices
++;
2113 if (!fi_args
->num_devices
)
2116 di_args
= *di_ret
= malloc((fi_args
->num_devices
) * sizeof(*di_args
));
2123 memcpy(di_args
, &tmp
, sizeof(tmp
));
2124 for (; i
<= fi_args
->max_id
; ++i
) {
2125 ret
= get_device_info(fd
, i
, &di_args
[ndevs
]);
2134 * only when the only dev we wanted to find is not there then
2135 * let any error be returned
2137 if (fi_args
->num_devices
!= 1) {
2143 close_file_or_dir(fd
, dirstream
);
2147 #define isoctal(c) (((c) & ~7) == '0')
2149 static inline void translate(char *f
, char *t
)
2151 while (*f
!= '\0') {
2153 isoctal(f
[1]) && isoctal(f
[2]) && isoctal(f
[3])) {
2154 *t
++ = 64*(f
[1] & 7) + 8*(f
[2] & 7) + (f
[3] & 7);
2164 * Checks if the swap device.
2165 * Returns 1 if swap device, < 0 on error or 0 if not swap device.
2167 static int is_swap_device(const char *file
)
2178 if (stat(file
, &st_buf
) < 0)
2180 if (S_ISBLK(st_buf
.st_mode
))
2181 dev
= st_buf
.st_rdev
;
2182 else if (S_ISREG(st_buf
.st_mode
)) {
2183 dev
= st_buf
.st_dev
;
2184 ino
= st_buf
.st_ino
;
2188 if ((f
= fopen("/proc/swaps", "r")) == NULL
)
2191 /* skip the first line */
2192 if (fgets(tmp
, sizeof(tmp
), f
) == NULL
)
2195 while (fgets(tmp
, sizeof(tmp
), f
) != NULL
) {
2196 if ((cp
= strchr(tmp
, ' ')) != NULL
)
2198 if ((cp
= strchr(tmp
, '\t')) != NULL
)
2200 translate(tmp
, buf
);
2201 if (stat(buf
, &st_buf
) != 0)
2203 if (S_ISBLK(st_buf
.st_mode
)) {
2204 if (dev
== st_buf
.st_rdev
) {
2208 } else if (S_ISREG(st_buf
.st_mode
)) {
2209 if (dev
== st_buf
.st_dev
&& ino
== st_buf
.st_ino
) {
2223 * Check for existing filesystem or partition table on device.
2225 * 1 for existing fs or partition
2226 * 0 for nothing found
2227 * -1 for internal error
2234 blkid_probe pr
= NULL
;
2238 if (!device
|| !*device
)
2241 ret
= -1; /* will reset on success of all setup calls */
2243 pr
= blkid_new_probe_from_filename(device
);
2247 size
= blkid_probe_get_size(pr
);
2251 /* nothing to overwrite on a 0-length device */
2257 ret
= blkid_probe_enable_partitions(pr
, 1);
2261 ret
= blkid_do_fullprobe(pr
);
2266 * Blkid returns 1 for nothing found and 0 when it finds a signature,
2267 * but we want the exact opposite, so reverse the return value here.
2269 * In addition print some useful diagnostics about what actually is
2277 if (!blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
)) {
2279 "%s appears to contain an existing "
2280 "filesystem (%s).\n", device
, type
);
2281 } else if (!blkid_probe_lookup_value(pr
, "PTTYPE", &type
, NULL
)) {
2283 "%s appears to contain a partition "
2284 "table (%s).\n", device
, type
);
2287 "%s appears to contain something weird "
2288 "according to blkid\n", device
);
2294 blkid_free_probe(pr
);
2297 "probe of %s failed, cannot detect "
2298 "existing filesystem.\n", device
);
2302 static int group_profile_devs_min(u64 flag
)
2304 switch (flag
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
2305 case 0: /* single */
2306 case BTRFS_BLOCK_GROUP_DUP
:
2308 case BTRFS_BLOCK_GROUP_RAID0
:
2309 case BTRFS_BLOCK_GROUP_RAID1
:
2310 case BTRFS_BLOCK_GROUP_RAID5
:
2312 case BTRFS_BLOCK_GROUP_RAID6
:
2314 case BTRFS_BLOCK_GROUP_RAID10
:
2321 int test_num_disk_vs_raid(u64 metadata_profile
, u64 data_profile
,
2322 u64 dev_cnt
, int mixed
, char *estr
)
2330 allowed
|= BTRFS_BLOCK_GROUP_RAID10
;
2332 allowed
|= BTRFS_BLOCK_GROUP_RAID6
;
2334 allowed
|= BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2335 BTRFS_BLOCK_GROUP_RAID5
;
2338 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
2342 ((metadata_profile
| data_profile
) & BTRFS_BLOCK_GROUP_DUP
)) {
2344 "DUP is not allowed when FS has multiple devices\n");
2347 if (metadata_profile
& ~allowed
) {
2349 "unable to create FS with metadata profile %s "
2350 "(have %llu devices but %d devices are required)\n",
2351 btrfs_group_profile_str(metadata_profile
), dev_cnt
,
2352 group_profile_devs_min(metadata_profile
));
2355 if (data_profile
& ~allowed
) {
2357 "unable to create FS with data profile %s "
2358 "(have %llu devices but %d devices are required)\n",
2359 btrfs_group_profile_str(data_profile
), dev_cnt
,
2360 group_profile_devs_min(data_profile
));
2364 if (!mixed
&& (data_profile
& BTRFS_BLOCK_GROUP_DUP
)) {
2366 "dup for data is allowed only in mixed mode");
2372 int group_profile_max_safe_loss(u64 flags
)
2374 switch (flags
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
2375 case 0: /* single */
2376 case BTRFS_BLOCK_GROUP_DUP
:
2377 case BTRFS_BLOCK_GROUP_RAID0
:
2379 case BTRFS_BLOCK_GROUP_RAID1
:
2380 case BTRFS_BLOCK_GROUP_RAID5
:
2381 case BTRFS_BLOCK_GROUP_RAID10
:
2383 case BTRFS_BLOCK_GROUP_RAID6
:
2390 /* Check if disk is suitable for btrfs
2392 * 1: something is wrong, estr provides the error
2395 int test_dev_for_mkfs(char *file
, int force_overwrite
, char *estr
)
2401 ret
= is_swap_device(file
);
2403 snprintf(estr
, sz
, "error checking %s status: %s\n", file
,
2408 snprintf(estr
, sz
, "%s is a swap device\n", file
);
2411 if (!force_overwrite
) {
2412 if (check_overwrite(file
)) {
2413 snprintf(estr
, sz
, "Use the -f option to force overwrite.\n");
2417 ret
= check_mounted(file
);
2419 snprintf(estr
, sz
, "error checking %s mount status\n",
2424 snprintf(estr
, sz
, "%s is mounted\n", file
);
2427 /* check if the device is busy */
2428 fd
= open(file
, O_RDWR
|O_EXCL
);
2430 snprintf(estr
, sz
, "unable to open %s: %s\n", file
,
2434 if (fstat(fd
, &st
)) {
2435 snprintf(estr
, sz
, "unable to stat %s: %s\n", file
,
2440 if (!S_ISBLK(st
.st_mode
)) {
2441 fprintf(stderr
, "'%s' is not a block device\n", file
);
2449 int btrfs_scan_lblkid()
2454 struct btrfs_fs_devices
*tmp_devices
;
2455 blkid_dev_iterate iter
= NULL
;
2456 blkid_dev dev
= NULL
;
2457 blkid_cache cache
= NULL
;
2458 char path
[PATH_MAX
];
2460 if (btrfs_scan_done
)
2463 if (blkid_get_cache(&cache
, 0) < 0) {
2464 printf("ERROR: lblkid cache get failed\n");
2467 blkid_probe_all(cache
);
2468 iter
= blkid_dev_iterate_begin(cache
);
2469 blkid_dev_set_search(iter
, "TYPE", "btrfs");
2470 while (blkid_dev_next(iter
, &dev
) == 0) {
2471 dev
= blkid_verify(cache
, dev
);
2474 /* if we are here its definitely a btrfs disk*/
2475 strncpy_null(path
, blkid_dev_devname(dev
));
2477 fd
= open(path
, O_RDONLY
);
2479 printf("ERROR: could not open %s\n", path
);
2482 ret
= btrfs_scan_one_device(fd
, path
, &tmp_devices
,
2483 &num_devices
, BTRFS_SUPER_INFO_OFFSET
, 0);
2485 printf("ERROR: could not scan %s\n", path
);
2492 blkid_dev_iterate_end(iter
);
2493 blkid_put_cache(cache
);
2495 btrfs_scan_done
= 1;
2500 int is_vol_small(char *file
)
2507 fd
= open(file
, O_RDONLY
);
2510 if (fstat(fd
, &st
) < 0) {
2515 size
= btrfs_device_size(fd
, &st
);
2520 if (size
< BTRFS_MKFS_SMALL_VOLUME_SIZE
) {
2530 * This reads a line from the stdin and only returns non-zero if the
2531 * first whitespace delimited token is a case insensitive match with yes
2534 int ask_user(char *question
)
2536 char buf
[30] = {0,};
2537 char *saveptr
= NULL
;
2540 printf("%s [y/N]: ", question
);
2542 return fgets(buf
, sizeof(buf
) - 1, stdin
) &&
2543 (answer
= strtok_r(buf
, " \t\n\r", &saveptr
)) &&
2544 (!strcasecmp(answer
, "yes") || !strcasecmp(answer
, "y"));
2549 * - file or directory return the containing tree root id
2550 * - subvolume return its own tree id
2551 * - BTRFS_EMPTY_SUBVOL_DIR_OBJECTID (directory with ino == 2) the result is
2552 * undefined and function returns -1
2554 int lookup_ino_rootid(int fd
, u64
*rootid
)
2556 struct btrfs_ioctl_ino_lookup_args args
;
2560 memset(&args
, 0, sizeof(args
));
2562 args
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
2564 ret
= ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
);
2567 fprintf(stderr
, "ERROR: Failed to lookup root id - %s\n",
2572 *rootid
= args
.treeid
;
2578 * return 0 if a btrfs mount point is found
2579 * return 1 if a mount point is found but not btrfs
2580 * return <0 if something goes wrong
2582 int find_mount_root(const char *path
, char **mount_root
)
2590 int longest_matchlen
= 0;
2591 char *longest_match
= NULL
;
2593 fd
= open(path
, O_RDONLY
| O_NOATIME
);
2598 mnttab
= setmntent("/proc/self/mounts", "r");
2602 while ((ent
= getmntent(mnttab
))) {
2603 len
= strlen(ent
->mnt_dir
);
2604 if (strncmp(ent
->mnt_dir
, path
, len
) == 0) {
2605 /* match found and use the latest match */
2606 if (longest_matchlen
<= len
) {
2607 free(longest_match
);
2608 longest_matchlen
= len
;
2609 longest_match
= strdup(ent
->mnt_dir
);
2610 not_btrfs
= strcmp(ent
->mnt_type
, "btrfs");
2619 free(longest_match
);
2624 *mount_root
= realpath(longest_match
, NULL
);
2628 free(longest_match
);
2632 int test_minimum_size(const char *file
, u32 nodesize
)
2635 struct stat statbuf
;
2637 fd
= open(file
, O_RDONLY
);
2640 if (stat(file
, &statbuf
) < 0) {
2644 if (btrfs_device_size(fd
, &statbuf
) < btrfs_min_dev_size(nodesize
)) {
2653 * test if name is a correct subvolume name
2654 * this function return
2655 * 0-> name is not a correct subvolume name
2656 * 1-> name is a correct subvolume name
2658 int test_issubvolname(const char *name
)
2660 return name
[0] != '\0' && !strchr(name
, '/') &&
2661 strcmp(name
, ".") && strcmp(name
, "..");
2665 * test if path is a directory
2666 * this function return
2667 * 0-> path exists but it is not a directory
2668 * 1-> path exists and it is a directory
2669 * -1 -> path is unaccessible
2671 int test_isdir(const char *path
)
2676 ret
= stat(path
, &st
);
2680 return S_ISDIR(st
.st_mode
);
2683 void units_set_mode(unsigned *units
, unsigned mode
)
2685 unsigned base
= *units
& UNITS_MODE_MASK
;
2687 *units
= base
| mode
;
2690 void units_set_base(unsigned *units
, unsigned base
)
2692 unsigned mode
= *units
& ~UNITS_MODE_MASK
;
2694 *units
= base
| mode
;
2697 int find_next_key(struct btrfs_path
*path
, struct btrfs_key
*key
)
2701 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
2702 if (!path
->nodes
[level
])
2704 if (path
->slots
[level
] + 1 >=
2705 btrfs_header_nritems(path
->nodes
[level
]))
2708 btrfs_item_key_to_cpu(path
->nodes
[level
], key
,
2709 path
->slots
[level
] + 1);
2711 btrfs_node_key_to_cpu(path
->nodes
[level
], key
,
2712 path
->slots
[level
] + 1);
2718 char* btrfs_group_type_str(u64 flag
)
2720 u64 mask
= BTRFS_BLOCK_GROUP_TYPE_MASK
|
2721 BTRFS_SPACE_INFO_GLOBAL_RSV
;
2723 switch (flag
& mask
) {
2724 case BTRFS_BLOCK_GROUP_DATA
:
2726 case BTRFS_BLOCK_GROUP_SYSTEM
:
2728 case BTRFS_BLOCK_GROUP_METADATA
:
2730 case BTRFS_BLOCK_GROUP_DATA
|BTRFS_BLOCK_GROUP_METADATA
:
2731 return "Data+Metadata";
2732 case BTRFS_SPACE_INFO_GLOBAL_RSV
:
2733 return "GlobalReserve";
2739 char* btrfs_group_profile_str(u64 flag
)
2741 switch (flag
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
2744 case BTRFS_BLOCK_GROUP_RAID0
:
2746 case BTRFS_BLOCK_GROUP_RAID1
:
2748 case BTRFS_BLOCK_GROUP_RAID5
:
2750 case BTRFS_BLOCK_GROUP_RAID6
:
2752 case BTRFS_BLOCK_GROUP_DUP
:
2754 case BTRFS_BLOCK_GROUP_RAID10
:
2761 u64
disk_size(char *path
)
2765 if (statfs(path
, &sfs
) < 0)
2768 return sfs
.f_bsize
* sfs
.f_blocks
;
2771 u64
get_partition_size(char *dev
)
2774 int fd
= open(dev
, O_RDONLY
);
2778 if (ioctl(fd
, BLKGETSIZE64
, &result
) < 0) {
2787 int btrfs_tree_search2_ioctl_supported(int fd
)
2789 struct btrfs_ioctl_search_args_v2
*args2
;
2790 struct btrfs_ioctl_search_key
*sk
;
2791 int args2_size
= 1024;
2792 char args2_buf
[args2_size
];
2794 static int v2_supported
= -1;
2796 if (v2_supported
!= -1)
2797 return v2_supported
;
2799 args2
= (struct btrfs_ioctl_search_args_v2
*)args2_buf
;
2803 * Search for the extent tree item in the root tree.
2805 sk
->tree_id
= BTRFS_ROOT_TREE_OBJECTID
;
2806 sk
->min_objectid
= BTRFS_EXTENT_TREE_OBJECTID
;
2807 sk
->max_objectid
= BTRFS_EXTENT_TREE_OBJECTID
;
2808 sk
->min_type
= BTRFS_ROOT_ITEM_KEY
;
2809 sk
->max_type
= BTRFS_ROOT_ITEM_KEY
;
2811 sk
->max_offset
= (u64
)-1;
2812 sk
->min_transid
= 0;
2813 sk
->max_transid
= (u64
)-1;
2815 args2
->buf_size
= args2_size
- sizeof(struct btrfs_ioctl_search_args_v2
);
2816 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH_V2
, args2
);
2817 if (ret
== -EOPNOTSUPP
)
2824 return v2_supported
;
2827 int btrfs_check_nodesize(u32 nodesize
, u32 sectorsize
)
2829 if (nodesize
< sectorsize
) {
2831 "ERROR: Illegal nodesize %u (smaller than %u)\n",
2832 nodesize
, sectorsize
);
2834 } else if (nodesize
> BTRFS_MAX_METADATA_BLOCKSIZE
) {
2836 "ERROR: Illegal nodesize %u (larger than %u)\n",
2837 nodesize
, BTRFS_MAX_METADATA_BLOCKSIZE
);
2839 } else if (nodesize
& (sectorsize
- 1)) {
2841 "ERROR: Illegal nodesize %u (not aligned to %u)\n",
2842 nodesize
, sectorsize
);