bool gfapi_device::d_truncate(DCR *dcr) { struct stat st; if (m_gfd) { if (glfs_ftruncate(m_gfd, 0) != 0) { berrno be; Mmsg2(errmsg, _("Unable to truncate device %s. ERR=%s\n"), print_name(), be.bstrerror()); Emsg0(M_FATAL, 0, errmsg); return false; } /* * Check for a successful glfs_truncate() and issue work-around when truncation doesn't work. * * 1. close file * 2. delete file * 3. open new file with same mode * 4. change ownership to original */ if (glfs_fstat(m_gfd, &st) != 0) { berrno be; Mmsg2(errmsg, _("Unable to stat device %s. ERR=%s\n"), print_name(), be.bstrerror()); return false; } if (st.st_size != 0) { /* glfs_truncate() didn't work */ glfs_close(m_gfd); glfs_unlink(m_glfs, getVolCatName()); set_mode(CREATE_READ_WRITE); /* * Recreate the file -- of course, empty */ m_gfd = glfs_creat(m_glfs, getVolCatName(), oflags, st.st_mode); if (!m_gfd) { berrno be; dev_errno = errno; Mmsg2(errmsg, _("Could not reopen: %s, ERR=%s\n"), getVolCatName(), be.bstrerror()); Emsg0(M_FATAL, 0, errmsg); return false; } /* * Reset proper owner */ glfs_chown(m_glfs, getVolCatName(), st.st_uid, st.st_gid); } } return true; }
static int qemu_gluster_truncate(BlockDriverState *bs, int64_t offset) { int ret; BDRVGlusterState *s = bs->opaque; ret = glfs_ftruncate(s->fd, offset); if (ret < 0) { return -errno; } return 0; }
static int qemu_gluster_create(const char *filename, QEMUOptionParameter *options) { struct glfs *glfs; struct glfs_fd *fd; int ret = 0; int64_t total_size = 0; GlusterConf *gconf = g_malloc0(sizeof(GlusterConf)); glfs = qemu_gluster_init(gconf, filename); if (!glfs) { ret = -errno; goto out; } while (options && options->name) { if (!strcmp(options->name, BLOCK_OPT_SIZE)) { total_size = options->value.n / BDRV_SECTOR_SIZE; } options++; } fd = glfs_creat(glfs, gconf->image, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR); if (!fd) { ret = -errno; } else { if (glfs_ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) { ret = -errno; } if (glfs_close(fd) != 0) { ret = -errno; } } out: qemu_gluster_gconf_free(gconf); if (glfs) { glfs_fini(glfs); } return ret; }
static int qemu_gluster_create(const char *filename, QEMUOptionParameter *options, Error **errp) { struct glfs *glfs; struct glfs_fd *fd; int ret = 0; int prealloc = 0; int64_t total_size = 0; GlusterConf *gconf = g_malloc0(sizeof(GlusterConf)); glfs = qemu_gluster_init(gconf, filename, errp); if (!glfs) { ret = -EINVAL; goto out; } while (options && options->name) { if (!strcmp(options->name, BLOCK_OPT_SIZE)) { total_size = options->value.n / BDRV_SECTOR_SIZE; } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { if (!options->value.s || !strcmp(options->value.s, "off")) { prealloc = 0; } else if (!strcmp(options->value.s, "full") && gluster_supports_zerofill()) { prealloc = 1; } else { error_setg(errp, "Invalid preallocation mode: '%s'" " or GlusterFS doesn't support zerofill API", options->value.s); ret = -EINVAL; goto out; } } options++; } fd = glfs_creat(glfs, gconf->image, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR); if (!fd) { ret = -errno; } else { if (!glfs_ftruncate(fd, total_size * BDRV_SECTOR_SIZE)) { if (prealloc && qemu_gluster_zerofill(fd, 0, total_size * BDRV_SECTOR_SIZE)) { ret = -errno; } } else { ret = -errno; } if (glfs_close(fd) != 0) { ret = -errno; } } out: qemu_gluster_gconf_free(gconf); if (glfs) { glfs_fini(glfs); } return ret; }
int gluster_put (glfs_t *fs, struct state *state) { int ret = -1; glfs_fd_t *fd = NULL; char *filename = state->gluster_url->path; char *dir_path = strdup (state->gluster_url->path); struct stat statbuf; if (dir_path == NULL) { error (EXIT_FAILURE, errno, "strdup"); goto out; } ret = glfs_lstat (fs, filename, &statbuf); if (ret != -1 && !state->append && !state->overwrite) { errno = EEXIST; ret = -1; goto out; } if (state->parents) { ret = gluster_create_path (fs, dir_path, get_default_dir_mode_perm ()); if (ret == -1) { goto out; } } fd = glfs_creat (fs, filename, O_RDWR, get_default_file_mode_perm ()); if (fd == NULL) { ret = -1; goto out; } ret = gluster_lock (fd, F_WRLCK); if (ret == -1) { goto out; } if (state->append) { ret = glfs_lseek (fd, 0, SEEK_END); if (ret == -1) { error (0, errno, "seek error: %s", filename); goto out; } } else { ret = glfs_ftruncate (fd, 0); if (ret == -1) { error (0, errno, "truncate error: %s", filename); goto out; } } if (gluster_write (STDIN_FILENO, fd) == 0) { ret = -1; } out: free (dir_path); if (fd) { glfs_close (fd); } return ret; }