blkid_dev dev = NULL;
blkid_cache cache = NULL;
- if (blkid_get_cache(&cache, 0) < 0) {
+ if (blkid_get_cache(&cache, NULL) < 0) {
printf("ERROR: lblkid cache get failed\n");
return 1;
}
int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
{
struct btrfs_super_block super;
- struct extent_buffer *buf = NULL;
+ struct extent_buffer *buf;
struct btrfs_root_item root_item;
struct btrfs_disk_key disk_key;
struct btrfs_extent_item *extent_item;
BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
u64 num_bytes;
+ buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
+ if (!buf)
+ return -ENOMEM;
+
first_free = BTRFS_SUPER_INFO_OFFSET + cfg->sectorsize * 2 - 1;
first_free &= ~((u64)cfg->sectorsize - 1);
if (cfg->label)
strncpy(super.label, cfg->label, BTRFS_LABEL_SIZE - 1);
- buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
-
/* create the tree of root objects */
memset(buf->data, 0, cfg->nodesize);
buf->len = cfg->nodesize;
/* and write out the super block */
BUG_ON(sizeof(super) > cfg->sectorsize);
- memset(buf->data, 0, cfg->sectorsize);
+ memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
memcpy(buf->data, &super, sizeof(super));
- buf->len = cfg->sectorsize;
+ buf->len = BTRFS_SUPER_INFO_SIZE;
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
- ret = pwrite(fd, buf->data, cfg->sectorsize, cfg->blocks[0]);
- if (ret != cfg->sectorsize) {
+ ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, cfg->blocks[0]);
+ if (ret != BTRFS_SUPER_INFO_SIZE) {
ret = (ret < 0 ? -errno : -EIO);
goto out;
}
MAJOR(statbuf.st_rdev) == LOOP_MAJOR);
}
+/*
+ * Takes a loop device path (e.g. /dev/loop0) and returns
+ * the associated file (e.g. /images/my_btrfs.img) using
+ * loopdev API
+ */
+static int resolve_loop_device_with_loopdev(const char* loop_dev, char* loop_file)
+{
+ int fd;
+ int ret;
+ struct loop_info64 lo64;
+
+ fd = open(loop_dev, O_RDONLY | O_NONBLOCK);
+ if (fd < 0)
+ return -errno;
+ ret = ioctl(fd, LOOP_GET_STATUS64, &lo64);
+ if (ret < 0) {
+ ret = -errno;
+ goto out;
+ }
+
+ memcpy(loop_file, lo64.lo_file_name, sizeof(lo64.lo_file_name));
+ loop_file[sizeof(lo64.lo_file_name)] = 0;
+
+out:
+ close(fd);
+
+ return ret;
+}
/* Takes a loop device path (e.g. /dev/loop0) and returns
* the associated file (e.g. /images/my_btrfs.img) */
if (!realpath(loop_dev, real_loop_dev))
return -errno;
snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev, '/'));
- if (!(f = fopen(p, "r")))
+ if (!(f = fopen(p, "r"))) {
+ if (errno == ENOENT)
+ /*
+ * It's possibly a partitioned loop device, which is
+ * resolvable with loopdev API.
+ */
+ return resolve_loop_device_with_loopdev(loop_dev, loop_file);
return -errno;
+ }
snprintf(fmt, 20, "%%%i[^\n]", max_len-1);
ret = fscanf(f, fmt, loop_file);
list_for_each_entry(fs_devices, all_uuids, list) {
list_for_each_entry(device, &fs_devices->devices, dev_list) {
- if (strlen(device->name) != 0) {
+ if (*device->name) {
err = btrfs_register_one_device(device->name);
if (err < 0)
return err;
}
int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile,
- u64 dev_cnt, int mixed)
+ u64 dev_cnt, int mixed, int ssd)
{
u64 allowed = 0;
return 1;
}
- if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
- fprintf(stderr,
- "ERROR: DUP for data is allowed only in mixed mode\n");
- return 1;
- }
+ warning_on(!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP) && ssd,
+ "DUP may not actually lead to 2 copies on the device, see manual page");
+
return 0;
}
if (btrfs_scan_done)
return 0;
- if (blkid_get_cache(&cache, 0) < 0) {
+ if (blkid_get_cache(&cache, NULL) < 0) {
printf("ERROR: lblkid cache get failed\n");
return 1;
}