btrfs-progs: use fixed size buffer in zero_output_file

Rewrite the loop so we don't need to allocate sectorsize and write in 4k
steps instead. We know that sectorsize is divisible by 4096.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2015-11-06 18:10:29 +01:00
parent 89b40ab5bb
commit 927747bc82

17
mkfs.c
View File

@ -1091,23 +1091,20 @@ static u64 size_sourcedir(char *dir_name, u64 sectorsize,
static int zero_output_file(int out_fd, u64 size, u32 sectorsize) static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
{ {
int len = sectorsize; int loop_num;
int loop_num = size / sectorsize;
u64 location = 0; u64 location = 0;
char *buf = malloc(len); char buf[4096];
int ret = 0, i; int ret = 0, i;
ssize_t written; ssize_t written;
if (!buf) memset(buf, 0, 4096);
return -ENOMEM; loop_num = size / 4096;
memset(buf, 0, len);
for (i = 0; i < loop_num; i++) { for (i = 0; i < loop_num; i++) {
written = pwrite64(out_fd, buf, len, location); written = pwrite64(out_fd, buf, 4096, location);
if (written != len) if (written != 4096)
ret = -EIO; ret = -EIO;
location += sectorsize; location += 4096;
} }
free(buf);
return ret; return ret;
} }