#include <string.h> /* for memset. */
#include <libaio.h>
#include <sys/mman.h>
+#include <sys/syscall.h>
#include "libvhd.h"
#include "tapdisk.h"
return 0;
}
+static int
+vhd_fallocate(int fd, int mode, off_t offset, off_t length)
+{
+ return syscall(SYS_fallocate, fd, mode, offset, length);
+}
+
static int
allocate_block(struct vhd_state *s, uint32_t blk)
{
}
s->bat.pbw_offset = s->next_db;
+ size = vhd_sectors_to_bytes(s->spb + s->bm_secs + gap);
DBG(TLOG_DBG, "blk: 0x%04x, pbwo: 0x%08"PRIx64"\n",
blk, s->bat.pbw_offset);
- if (lseek(s->vhd.fd, offset, SEEK_SET) == (off_t)-1) {
- ERR(s, -errno, "lseek failed\n");
- return -errno;
- }
+ err = vhd_fallocate(s->vhd.fd, 0, offset, size);
+ if (err) {
+ if (errno != ENOSYS) {
+ ERR(s, -errno, "fallocate failed\n");
+ return -errno;
+ }
- size = vhd_sectors_to_bytes(s->spb + s->bm_secs + gap);
- count = write(s->vhd.fd, vhd_zeros(size), size);
- if (count != size) {
- err = count < 0 ? -errno : -ENOSPC;
- ERR(s, -errno,
- "write failed (%zd, offset %"PRIu64")\n", count, offset);
- return err;
+ if (lseek(s->vhd.fd, offset, SEEK_SET) == (off_t)-1) {
+ ERR(s, -errno, "lseek failed\n");
+ return -errno;
+ }
+
+ count = write(s->vhd.fd, vhd_zeros(size), size);
+ if (count != size) {
+ err = count < 0 ? -errno : -ENOSPC;
+ ERR(s, -errno,
+ "write failed (%zd, offset %"PRIu64")\n",
+ count, offset);
+ return err;
+ }
}
- err = fdatasync(s->vhd.fd);
+ err = fdatasync(s->vhd.fd);
if (err) {
err = -errno;
ERR(s, err, "sync failed");