<commit_before>/*
 * storage_backend_logical.c: storage backend for logical volume handling
 *
 * Copyright (C) 2007-2016 Red Hat, Inc.
 * Copyright (C) 2007-2008 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <config.h>

#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <regex.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include "virerror.h"
#include "storage_backend_logical.h"
#include "storage_conf.h"
#include "vircommand.h"
#include "viralloc.h"
#include "virlog.h"
#include "virfile.h"
#include "virstring.h"
#include "storage_util.h"

#define VIR_FROM_THIS VIR_FROM_STORAGE

VIR_LOG_INIT("storage.storage_backend_logical");

#define PV_BLANK_SECTOR_SIZE 512


static int
virStorageBackendLogicalSetActive(virStoragePoolObjPtr pool,
                                  int on)
{
    int ret;
    virCommandPtr cmd =
        virCommandNewArgList(VGCHANGE,
                             on ? "-aly" : "-aln",
                             pool->def->source.name,
                             NULL);

    ret = virCommandRun(cmd, NULL);
    virCommandFree(cmd);
    return ret;
}


/*
 * @path: Path to the device
 *
 * Remove the pv device since we're done with it. This ensures a subsequent
 * create won't require special arguments in order for force recreation.
 */
static void
virStorageBackendLogicalRemoveDevice(const char *path)
{
    virCommandPtr cmd = virCommandNewArgList(PVREMOVE, path, NULL);

    if (virCommandRun(cmd, NULL) < 0)
        VIR_INFO("Failed to pvremove logical device '%s'", path);
    virCommandFree(cmd);
}


/*
 * @path: Path to the device
 *
 * Initialize and pvcreate the device.
 *
 * Returns 0 on success, -1 on failure with error message set
 */
static int
virStorageBackendLogicalInitializeDevice(const char *path)
{
    int fd = -1;
    char zeros[4 * PV_BLANK_SECTOR_SIZE] = {0};
    off_t size;
    int ret = -1;
    virCommandPtr pvcmd = NULL;

    /*
     * LVM requires that the first sector is blanked if using
     * a whole disk as a PV. So we just blank them out regardless
     * rather than trying to figure out if we're a disk or partition
     */
    if ((fd = open(path, O_WRONLY)) < 0) {
        virReportSystemError(errno, _("cannot open device '%s'"), path);
        return -1;
    }

    if ((size = lseek(fd, 0, SEEK_END)) == (off_t)-1) {
        virReportSystemError(errno,
                             _("failed to seek to end of %s"), path);
        goto cleanup;
    }

    if (size < 4 * PV_BLANK_SECTOR_SIZE) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
                       _("cannot initialize '%s' detected size='%lu' less "
                         "than minimum required='%d"),
                         path, size, 4 * PV_BLANK_SECTOR_SIZE);
        goto cleanup;
    }
    if ((size = lseek(fd, 0, SEEK_SET)) == (off_t)-1) {
        virReportSystemError(errno,
                             _("failed to seek to start of %s"), path);
        goto cleanup;
    }

    if (safewrite(fd, zeros, sizeof(zeros)) < 0) {
        virReportSystemError(errno, _("cannot clear device header of '%s'"),
                             path);<commit_msg>storage: Fix build on i686<commit_after>/*
 * storage_backend_logical.c: storage backend for logical volume handling
 *
 * Copyright (C) 2007-2016 Red Hat, Inc.
 * Copyright (C) 2007-2008 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <config.h>

#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <regex.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include "virerror.h"
#include "storage_backend_logical.h"
#include "storage_conf.h"
#include "vircommand.h"
#include "viralloc.h"
#include "virlog.h"
#include "virfile.h"
#include "virstring.h"
#include "storage_util.h"

#define VIR_FROM_THIS VIR_FROM_STORAGE

VIR_LOG_INIT("storage.storage_backend_logical");

#define PV_BLANK_SECTOR_SIZE 512


static int
virStorageBackendLogicalSetActive(virStoragePoolObjPtr pool,
                                  int on)
{
    int ret;
    virCommandPtr cmd =
        virCommandNewArgList(VGCHANGE,
                             on ? "-aly" : "-aln",
                             pool->def->source.name,
                             NULL);

    ret = virCommandRun(cmd, NULL);
    virCommandFree(cmd);
    return ret;
}


/*
 * @path: Path to the device
 *
 * Remove the pv device since we're done with it. This ensures a subsequent
 * create won't require special arguments in order for force recreation.
 */
static void
virStorageBackendLogicalRemoveDevice(const char *path)
{
    virCommandPtr cmd = virCommandNewArgList(PVREMOVE, path, NULL);

    if (virCommandRun(cmd, NULL) < 0)
        VIR_INFO("Failed to pvremove logical device '%s'", path);
    virCommandFree(cmd);
}


/*
 * @path: Path to the device
 *
 * Initialize and pvcreate the device.
 *
 * Returns 0 on success, -1 on failure with error message set
 */
static int
virStorageBackendLogicalInitializeDevice(const char *path)
{
    int fd = -1;
    char zeros[4 * PV_BLANK_SECTOR_SIZE] = {0};
    off_t size;
    int ret = -1;
    virCommandPtr pvcmd = NULL;

    /*
     * LVM requires that the first sector is blanked if using
     * a whole disk as a PV. So we just blank them out regardless
     * rather than trying to figure out if we're a disk or partition
     */
    if ((fd = open(path, O_WRONLY)) < 0) {
        virReportSystemError(errno, _("cannot open device '%s'"), path);
        return -1;
    }

    if ((size = lseek(fd, 0, SEEK_END)) == (off_t)-1) {
        virReportSystemError(errno,
                             _("failed to seek to end of %s"), path);
        goto cleanup;
    }

    if (size < 4 * PV_BLANK_SECTOR_SIZE) {
        virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
                       _("cannot initialize '%s' detected size='%zd' less "
                         "than minimum required='%d"),
                         path, (ssize_t) size, 4 * PV_BLANK_SECTOR_SIZE);
        goto cleanup;
    }
    if ((size = lseek(fd, 0, SEEK_SET)) == (off_t)-1) {
        virReportSystemError(errno,
                             _("failed to seek to start of %s"), path);
        goto cleanup;
    }

    if (safewrite(fd, zeros, sizeof(zeros)) < 0) {
        virReportSystemError(errno, _("cannot clear device header of '%s'"),
                             path);
{'fraction_non_alphanumeric': 0.10439631694981195, 'fraction_numerical': 0.009985734664764621, 'mean_word_length': 2.6308851224105463, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3089585', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2336, 'n_words': 794}