1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2016 Flavius Anton 5 * Copyright (c) 2016 Mihai Tiganus 6 * Copyright (c) 2016-2019 Mihai Carabas 7 * Copyright (c) 2017-2019 Darius Mihai 8 * Copyright (c) 2017-2019 Elena Mihailescu 9 * Copyright (c) 2018-2019 Sergiu Weisz 10 * All rights reserved. 11 * The bhyve-snapshot feature was developed under sponsorships 12 * from Matthew Grooms. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #include <sys/types.h> 38 #include <sys/systm.h> 39 40 #include <machine/vmm_snapshot.h> 41 42 void 43 vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op) 44 { 45 const char *opstr; 46 47 if (op == VM_SNAPSHOT_SAVE) 48 opstr = "save"; 49 else if (op == VM_SNAPSHOT_RESTORE) 50 opstr = "restore"; 51 else 52 opstr = "unknown"; 53 54 printf("%s: snapshot-%s failed for %s\r\n", __func__, opstr, bufname); 55 } 56 57 int 58 vm_snapshot_buf(void *data, size_t data_size, struct vm_snapshot_meta *meta) 59 { 60 struct vm_snapshot_buffer *buffer; 61 int op; 62 63 buffer = &meta->buffer; 64 op = meta->op; 65 66 if (buffer->buf_rem < data_size) { 67 printf("%s: buffer too small\r\n", __func__); 68 return (E2BIG); 69 } 70 71 if (op == VM_SNAPSHOT_SAVE) 72 copyout(data, buffer->buf, data_size); 73 else if (op == VM_SNAPSHOT_RESTORE) 74 copyin(buffer->buf, data, data_size); 75 else 76 return (EINVAL); 77 78 buffer->buf += data_size; 79 buffer->buf_rem -= data_size; 80 81 return (0); 82 } 83 84 size_t 85 vm_get_snapshot_size(struct vm_snapshot_meta *meta) 86 { 87 size_t length; 88 struct vm_snapshot_buffer *buffer; 89 90 buffer = &meta->buffer; 91 92 if (buffer->buf_size < buffer->buf_rem) { 93 printf("%s: Invalid buffer: size = %zu, rem = %zu\r\n", 94 __func__, buffer->buf_size, buffer->buf_rem); 95 length = 0; 96 } else { 97 length = buffer->buf_size - buffer->buf_rem; 98 } 99 100 return (length); 101 } 102 103 int 104 vm_snapshot_buf_cmp(void *data, size_t data_size, struct vm_snapshot_meta *meta) 105 { 106 struct vm_snapshot_buffer *buffer; 107 int op; 108 int ret; 109 110 buffer = &meta->buffer; 111 op = meta->op; 112 113 if (buffer->buf_rem < data_size) { 114 printf("%s: buffer too small\r\n", __func__); 115 ret = E2BIG; 116 goto done; 117 } 118 119 if (op == VM_SNAPSHOT_SAVE) { 120 ret = 0; 121 copyout(data, buffer->buf, data_size); 122 } else if (op == VM_SNAPSHOT_RESTORE) { 123 ret = memcmp(data, buffer->buf, data_size); 124 } else { 125 ret = EINVAL; 126 goto done; 127 } 128 129 buffer->buf += data_size; 130 buffer->buf_rem -= data_size; 131 132 done: 133 return (ret); 134 } 135