/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2016 Flavius Anton4* Copyright (c) 2016 Mihai Tiganus5* Copyright (c) 2016-2019 Mihai Carabas6* Copyright (c) 2017-2019 Darius Mihai7* Copyright (c) 2017-2019 Elena Mihailescu8* Copyright (c) 2018-2019 Sergiu Weisz9* All rights reserved.10* The bhyve-snapshot feature was developed under sponsorships11* from Matthew Grooms.12*13* Redistribution and use in source and binary forms, with or without14* modification, are permitted provided that the following conditions15* are met:16* 1. Redistributions of source code must retain the above copyright17* notice, this list of conditions and the following disclaimer.18* 2. Redistributions in binary form must reproduce the above copyright19* notice, this list of conditions and the following disclaimer in the20* documentation and/or other materials provided with the distribution.21*22* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <sys/types.h>36#include <sys/systm.h>3738#include <machine/vmm_snapshot.h>3940void41vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op)42{43const char *opstr;4445if (op == VM_SNAPSHOT_SAVE)46opstr = "save";47else if (op == VM_SNAPSHOT_RESTORE)48opstr = "restore";49else50opstr = "unknown";5152printf("%s: snapshot-%s failed for %s\r\n", __func__, opstr, bufname);53}5455int56vm_snapshot_buf(void *data, size_t data_size, struct vm_snapshot_meta *meta)57{58struct vm_snapshot_buffer *buffer;59int op, error;6061buffer = &meta->buffer;62op = meta->op;6364if (buffer->buf_rem < data_size) {65printf("%s: buffer too small\r\n", __func__);66return (E2BIG);67}6869if (op == VM_SNAPSHOT_SAVE)70error = copyout(data, buffer->buf, data_size);71else if (op == VM_SNAPSHOT_RESTORE)72error = copyin(buffer->buf, data, data_size);73else74error = EINVAL;7576if (error)77return (error);7879buffer->buf += data_size;80buffer->buf_rem -= data_size;8182return (0);83}8485size_t86vm_get_snapshot_size(struct vm_snapshot_meta *meta)87{88size_t length;89struct vm_snapshot_buffer *buffer;9091buffer = &meta->buffer;9293if (buffer->buf_size < buffer->buf_rem) {94printf("%s: Invalid buffer: size = %zu, rem = %zu\r\n",95__func__, buffer->buf_size, buffer->buf_rem);96length = 0;97} else {98length = buffer->buf_size - buffer->buf_rem;99}100101return (length);102}103104105