static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
+const char *get_argv0_buf(void)
+{
+ return argv0_buf;
+}
+
void fixup_argv0(char **argv, const char *token)
{
int len = strlen(argv0_buf);
return unique;
}
+/*
+ * @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
+ */
int make_btrfs(int fd, const char *device, const char *label, char *fs_uuid,
u64 blocks[7], u64 num_bytes, u32 nodesize,
u32 sectorsize, u32 stripesize, u64 features)
memset(&super, 0, sizeof(super));
num_bytes = (num_bytes / sectorsize) * sectorsize;
- if (fs_uuid) {
+ if (fs_uuid && *fs_uuid) {
if (uuid_parse(fs_uuid, super.fsid) != 0) {
fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
ret = -EINVAL;
}
} else {
uuid_generate(super.fsid);
+ if (fs_uuid)
+ uuid_unparse(super.fsid, fs_uuid);
}
uuid_generate(super.dev_item.uuid);
uuid_generate(chunk_tree_uuid);
return !found;
}
+void btrfs_parse_features_to_string(char *buf, u64 flags)
+{
+ int i;
+
+ buf[0] = 0;
+
+ for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
+ if (flags & mkfs_features[i].flag) {
+ if (*buf)
+ strcat(buf, ", ");
+ strcat(buf, mkfs_features[i].name);
+ }
+ }
+}
+
void btrfs_process_fs_features(u64 flags)
{
int i;
device->bytes_used = 0;
device->total_ios = 0;
device->dev_root = root->fs_info->dev_root;
+ device->name = strdup(path);
ret = btrfs_add_device(trans, root, device);
BUG_ON(ret);
memcpy(disk_super, super, sizeof(*disk_super));
- printf("adding device %s id %llu\n", path,
- (unsigned long long)device->devid);
-
btrfs_set_super_bytenr(disk_super, BTRFS_SUPER_INFO_OFFSET);
btrfs_set_stack_device_id(dev_item, device->devid);
btrfs_set_stack_device_type(dev_item, device->type);
*/
int open_path_or_dev_mnt(const char *path, DIR **dirstream)
{
- char mp[BTRFS_PATH_NAME_MAX + 1];
+ char mp[PATH_MAX];
int fdmnt;
if (is_block_device(path)) {
strerror(errno));
return -errno;
}
- strncpy(args.name, fname, BTRFS_PATH_NAME_MAX);
- args.name[BTRFS_PATH_NAME_MAX-1] = 0;
+ memset(&args, 0, sizeof(args));
+ strncpy_null(args.name, fname);
ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
e = errno;
if (ret < 0) {
return ret;
}
+/*
+ * Note: this function uses a static per-thread buffer. Do not call this
+ * function more than 10 times within one argument list!
+ */
+const char *pretty_size_mode(u64 size, unsigned mode)
+{
+ static __thread int ps_index = 0;
+ static __thread char ps_array[10][32];
+ char *ret;
+
+ ret = ps_array[ps_index];
+ ps_index++;
+ ps_index %= 10;
+ (void)pretty_size_snprintf(size, ret, 32, mode);
+
+ return ret;
+}
+
static const char* unit_suffix_binary[] =
{ "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
static const char* unit_suffix_decimal[] =
}
if (fd < 0) {
fd = -1;
- if (*dirstream)
+ if (*dirstream) {
closedir(*dirstream);
+ *dirstream = NULL;
+ }
}
return fd;
}
struct btrfs_fs_devices *fs_devices_mnt = NULL;
struct btrfs_ioctl_dev_info_args *di_args;
struct btrfs_ioctl_dev_info_args tmp;
- char mp[BTRFS_PATH_NAME_MAX + 1];
+ char mp[PATH_MAX];
DIR *dirstream = NULL;
memset(fi_args, 0, sizeof(*fi_args));
}
int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile,
- u64 dev_cnt, int mixed, char *estr)
+ u64 dev_cnt, int mixed)
{
- size_t sz = 100;
u64 allowed = 0;
switch (dev_cnt) {
if (dev_cnt > 1 &&
((metadata_profile | data_profile) & BTRFS_BLOCK_GROUP_DUP)) {
- snprintf(estr, sz,
- "DUP is not allowed when FS has multiple devices\n");
+ fprintf(stderr,
+ "ERROR: DUP is not allowed when FS has multiple devices\n");
return 1;
}
if (metadata_profile & ~allowed) {
- snprintf(estr, sz,
- "unable to create FS with metadata profile %s "
+ fprintf(stderr,
+ "ERROR: unable to create FS with metadata profile %s "
"(have %llu devices but %d devices are required)\n",
btrfs_group_profile_str(metadata_profile), dev_cnt,
group_profile_devs_min(metadata_profile));
return 1;
}
if (data_profile & ~allowed) {
- snprintf(estr, sz,
- "unable to create FS with data profile %s "
+ fprintf(stderr,
+ "ERROR: unable to create FS with data profile %s "
"(have %llu devices but %d devices are required)\n",
btrfs_group_profile_str(data_profile), dev_cnt,
group_profile_devs_min(data_profile));
}
if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
- snprintf(estr, sz,
- "dup for data is allowed only in mixed mode");
+ fprintf(stderr,
+ "ERROR: DUP for data is allowed only in mixed mode");
return 1;
}
return 0;
}
}
-/* Check if disk is suitable for btrfs
+/*
+ * Check if a device is suitable for btrfs
* returns:
- * 1: something is wrong, estr provides the error
+ * 1: something is wrong, an error is printed
* 0: all is fine
*/
-int test_dev_for_mkfs(char *file, int force_overwrite, char *estr)
+int test_dev_for_mkfs(char *file, int force_overwrite)
{
int ret, fd;
- size_t sz = 100;
struct stat st;
ret = is_swap_device(file);
if (ret < 0) {
- snprintf(estr, sz, "error checking %s status: %s\n", file,
+ fprintf(stderr, "ERROR: checking status of %s: %s\n", file,
strerror(-ret));
return 1;
}
if (ret == 1) {
- snprintf(estr, sz, "%s is a swap device\n", file);
+ fprintf(stderr, "ERROR: %s is a swap device\n", file);
return 1;
}
if (!force_overwrite) {
if (check_overwrite(file)) {
- snprintf(estr, sz, "Use the -f option to force overwrite.\n");
+ fprintf(stderr, "Use the -f option to force overwrite.\n");
return 1;
}
}
ret = check_mounted(file);
if (ret < 0) {
- snprintf(estr, sz, "error checking %s mount status\n",
- file);
+ fprintf(stderr, "ERROR: checking mount status of %s: %s\n",
+ file, strerror(-ret));
return 1;
}
if (ret == 1) {
- snprintf(estr, sz, "%s is mounted\n", file);
+ fprintf(stderr, "ERROR: %s is mounted\n", file);
return 1;
}
/* check if the device is busy */
fd = open(file, O_RDWR|O_EXCL);
if (fd < 0) {
- snprintf(estr, sz, "unable to open %s: %s\n", file,
+ fprintf(stderr, "ERROR: unable to open %s: %s\n", file,
strerror(errno));
return 1;
}
if (fstat(fd, &st)) {
- snprintf(estr, sz, "unable to stat %s: %s\n", file,
+ fprintf(stderr, "ERROR: unable to stat %s: %s\n", file,
strerror(errno));
close(fd);
return 1;
}
if (!S_ISBLK(st.st_mode)) {
- fprintf(stderr, "'%s' is not a block device\n", file);
+ fprintf(stderr, "ERROR: %s is not a block device\n", file);
close(fd);
return 1;
}
}
return 0;
}
+
+/*
+ * Copy a path argument from SRC to DEST and check the SRC length if it's at
+ * most PATH_MAX and fits into DEST. DESTLEN is supposed to be exact size of
+ * the buffer.
+ * The destination buffer is zero terminated.
+ * Return < 0 for error, 0 otherwise.
+ */
+int arg_copy_path(char *dest, const char *src, int destlen)
+{
+ size_t len = strlen(src);
+
+ if (len >= PATH_MAX || len >= destlen)
+ return -ENAMETOOLONG;
+
+ __strncpy__null(dest, src, destlen);
+
+ return 0;
+}