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/types.h> 37 #include <sys/systm.h> 38 39 #include <machine/vmm_snapshot.h> 40 41 void 42 vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op) 43 { 44 const char *opstr; 45 46 if (op == VM_SNAPSHOT_SAVE) 47 opstr = "save"; 48 else if (op == VM_SNAPSHOT_RESTORE) 49 opstr = "restore"; 50 else 51 opstr = "unknown"; 52 53 printf("%s: snapshot-%s failed for %s\r\n", __func__, opstr, bufname); 54 } 55 56 int 57 vm_snapshot_buf(void *data, size_t data_size, struct vm_snapshot_meta *meta) 58 { 59 struct vm_snapshot_buffer *buffer; 60 int op; 61 62 buffer = &meta->buffer; 63 op = meta->op; 64 65 if (buffer->buf_rem < data_size) { 66 printf("%s: buffer too small\r\n", __func__); 67 return (E2BIG); 68 } 69 70 if (op == VM_SNAPSHOT_SAVE) 71 copyout(data, buffer->buf, data_size); 72 else if (op == VM_SNAPSHOT_RESTORE) 73 copyin(buffer->buf, data, data_size); 74 else 75 return (EINVAL); 76 77 buffer->buf += data_size; 78 buffer->buf_rem -= data_size; 79 80 return (0); 81 } 82 83 size_t 84 vm_get_snapshot_size(struct vm_snapshot_meta *meta) 85 { 86 size_t length; 87 struct vm_snapshot_buffer *buffer; 88 89 buffer = &meta->buffer; 90 91 if (buffer->buf_size < buffer->buf_rem) { 92 printf("%s: Invalid buffer: size = %zu, rem = %zu\r\n", 93 __func__, buffer->buf_size, buffer->buf_rem); 94 length = 0; 95 } else { 96 length = buffer->buf_size - buffer->buf_rem; 97 } 98 99 return (length); 100 } 101 102 int 103 vm_snapshot_buf_cmp(void *data, size_t data_size, struct vm_snapshot_meta *meta) 104 { 105 struct vm_snapshot_buffer *buffer; 106 int op; 107 int ret; 108 109 buffer = &meta->buffer; 110 op = meta->op; 111 112 if (buffer->buf_rem < data_size) { 113 printf("%s: buffer too small\r\n", __func__); 114 ret = E2BIG; 115 goto done; 116 } 117 118 if (op == VM_SNAPSHOT_SAVE) { 119 ret = 0; 120 copyout(data, buffer->buf, data_size); 121 } else if (op == VM_SNAPSHOT_RESTORE) { 122 ret = memcmp(data, buffer->buf, data_size); 123 } else { 124 ret = EINVAL; 125 goto done; 126 } 127 128 buffer->buf += data_size; 129 buffer->buf_rem -= data_size; 130 131 done: 132 return (ret); 133 } 134