block/file-posix: Preallocation for truncate

By using raw_regular_truncate() in raw_truncate(), we can now easily
support preallocation.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 20170613202107.10125-9-mreitz@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Max Reitz 2017-06-13 22:20:59 +02:00
parent d0bc9e5d5e
commit 35d72602ec
1 changed files with 10 additions and 12 deletions

View File

@ -1745,12 +1745,6 @@ static int raw_truncate(BlockDriverState *bs, int64_t offset,
struct stat st; struct stat st;
int ret; int ret;
if (prealloc != PREALLOC_MODE_OFF) {
error_setg(errp, "Unsupported preallocation mode '%s'",
PreallocMode_lookup[prealloc]);
return -ENOTSUP;
}
if (fstat(s->fd, &st)) { if (fstat(s->fd, &st)) {
ret = -errno; ret = -errno;
error_setg_errno(errp, -ret, "Failed to fstat() the file"); error_setg_errno(errp, -ret, "Failed to fstat() the file");
@ -1758,12 +1752,16 @@ static int raw_truncate(BlockDriverState *bs, int64_t offset,
} }
if (S_ISREG(st.st_mode)) { if (S_ISREG(st.st_mode)) {
if (ftruncate(s->fd, offset) < 0) { return raw_regular_truncate(s->fd, offset, prealloc, errp);
ret = -errno; }
error_setg_errno(errp, -ret, "Failed to resize the file");
return ret; if (prealloc != PREALLOC_MODE_OFF) {
} error_setg(errp, "Preallocation mode '%s' unsupported for this "
} else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { "non-regular file", PreallocMode_lookup[prealloc]);
return -ENOTSUP;
}
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
if (offset > raw_getlength(bs)) { if (offset > raw_getlength(bs)) {
error_setg(errp, "Cannot grow device files"); error_setg(errp, "Cannot grow device files");
return -EINVAL; return -EINVAL;