summaryrefslogtreecommitdiff
path: root/mkfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'mkfs.c')
-rw-r--r--mkfs.c56
1 files changed, 31 insertions, 25 deletions
diff --git a/mkfs.c b/mkfs.c
index 0ea39f3..ea58404 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -252,7 +252,6 @@ static int create_raid_groups(struct btrfs_trans_handle *trans,
u64 metadata_profile, int mixed,
struct mkfs_allocation *allocation)
{
- u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
int ret;
if (metadata_profile) {
@@ -271,7 +270,7 @@ static int create_raid_groups(struct btrfs_trans_handle *trans,
BUG_ON(ret);
}
- if (!mixed && num_devices > 1 && data_profile) {
+ if (!mixed && data_profile) {
ret = create_one_raid_group(trans, root,
BTRFS_BLOCK_GROUP_DATA |
data_profile, allocation);
@@ -592,15 +591,14 @@ static int add_symbolic_link(struct btrfs_trans_handle *trans,
u64 objectid, const char *path_name)
{
int ret;
- u64 sectorsize = root->sectorsize;
- char *buf = malloc(sectorsize);
+ char buf[PATH_MAX];
- ret = readlink(path_name, buf, sectorsize);
+ ret = readlink(path_name, buf, sizeof(buf));
if (ret <= 0) {
fprintf(stderr, "readlink failed for %s\n", path_name);
goto fail;
}
- if (ret >= sectorsize) {
+ if (ret >= sizeof(buf)) {
fprintf(stderr, "symlink too long for %s\n", path_name);
ret = -1;
goto fail;
@@ -610,7 +608,6 @@ static int add_symbolic_link(struct btrfs_trans_handle *trans,
ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
buf, ret + 1);
fail:
- free(buf);
return ret;
}
@@ -648,6 +645,12 @@ static int add_file_items(struct btrfs_trans_handle *trans,
if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
char *buffer = malloc(st->st_size);
+
+ if (!buffer) {
+ ret = -ENOMEM;
+ goto end;
+ }
+
ret_read = pread64(fd, buffer, st->st_size, bytes_read);
if (ret_read == -1) {
fprintf(stderr, "%s read failed\n", path_name);
@@ -780,6 +783,8 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
/* Add list for source directory */
dir_entry = malloc(sizeof(struct directory_name_entry));
+ if (!dir_entry)
+ return -ENOMEM;
dir_entry->dir_name = dir_name;
dir_entry->path = realpath(dir_name, real_path);
if (!dir_entry->path) {
@@ -881,6 +886,10 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
if (S_ISDIR(st.st_mode)) {
dir_entry = malloc(sizeof(struct directory_name_entry));
+ if (!dir_entry) {
+ ret = -ENOMEM;
+ goto fail;
+ }
dir_entry->dir_name = cur_file->d_name;
dir_entry->path = make_path(parent_dir_entry->path,
cur_file->d_name);
@@ -1020,16 +1029,15 @@ out:
* This ignores symlinks with unreadable targets and subdirs that can't
* be read. It's a best-effort to give a rough estimate of the size of
* a subdir. It doesn't guarantee that prepopulating btrfs from this
- * tree won't still run out of space.
- *
- * The rounding up to 4096 is questionable. Previous code used du -B 4096.
+ * tree won't still run out of space.
*/
static u64 global_total_size;
+static u64 fs_block_size;
static int ftw_add_entry_size(const char *fpath, const struct stat *st,
int type)
{
if (type == FTW_F || type == FTW_D)
- global_total_size += round_up(st->st_size, 4096);
+ global_total_size += round_up(st->st_size, fs_block_size);
return 0;
}
@@ -1049,6 +1057,7 @@ static u64 size_sourcedir(char *dir_name, u64 sectorsize,
allocated_meta_size / default_chunk_size;
global_total_size = 0;
+ fs_block_size = sectorsize;
ret = ftw(dir_name, ftw_add_entry_size, 10);
dir_size = global_total_size;
if (ret < 0) {
@@ -1077,32 +1086,29 @@ static u64 size_sourcedir(char *dir_name, u64 sectorsize,
return total_size;
}
-static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
+static int zero_output_file(int out_fd, u64 size)
{
- int len = sectorsize;
- int loop_num = size / sectorsize;
+ int loop_num;
u64 location = 0;
- char *buf = malloc(len);
+ char buf[4096];
int ret = 0, i;
ssize_t written;
- if (!buf)
- return -ENOMEM;
- memset(buf, 0, len);
+ memset(buf, 0, 4096);
+ loop_num = size / 4096;
for (i = 0; i < loop_num; i++) {
- written = pwrite64(out_fd, buf, len, location);
- if (written != len)
+ written = pwrite64(out_fd, buf, 4096, location);
+ if (written != 4096)
ret = -EIO;
- location += sectorsize;
+ location += 4096;
}
- free(buf);
return ret;
}
static int is_ssd(const char *file)
{
blkid_probe probe;
- char wholedisk[32];
+ char wholedisk[PATH_MAX];
char sysfs_path[PATH_MAX];
dev_t devno;
int fd;
@@ -1594,7 +1600,7 @@ int main(int ac, char **av)
}
}
ret = test_num_disk_vs_raid(metadata_profile, data_profile,
- dev_cnt, mixed);
+ dev_cnt, mixed, ssd);
if (ret)
exit(1);
@@ -1633,7 +1639,7 @@ int main(int ac, char **av)
&num_of_meta_chunks, &size_of_data);
if(block_count < source_dir_size)
block_count = source_dir_size;
- ret = zero_output_file(fd, block_count, sectorsize);
+ ret = zero_output_file(fd, block_count);
if (ret) {
fprintf(stderr, "unable to zero the output file\n");
exit(1);