/
block.go
210 lines (183 loc) · 5.31 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package qcow2
/*
Copyright (c) 2023 Yunpeng Deng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import (
"bytes"
"encoding/binary"
"math"
"os"
"unsafe"
)
func Blk_Create(filename string, options map[string]any) error {
var err error
if err = bdrv_create(filename, options); err != nil {
return err
}
return err
}
func Blk_Probe(filename string) (string, error) {
var err error
var file *os.File
var buffer bytes.Buffer
var magic uint32
buf := make([]byte, 4)
if file, err = os.Open(filename); err != nil {
return "", err
}
defer file.Close()
if _, err = file.Read(buf); err != nil {
return "", err
}
buffer.Write(buf)
binary.Read(&buffer, binary.BigEndian, &magic)
if magic == binary.BigEndian.Uint32(QCOW_MAGIC) {
return TYPE_QCOW2_NAME, nil
} else {
return TYPE_RAW_NAME, nil
}
}
// this function return root BdrvChild
func Blk_Open(filename string, options map[string]any, flags int) (*BdrvChild, error) {
var child *BdrvChild
var err error
var format string
if val, ok := options[OPT_FMT]; !ok {
return nil, Err_IncompleteParameters
} else {
format = val.(string)
}
if child, err = bdrv_open_child(filename, format, options, flags); err != nil {
return nil, err
} else {
bdrv_set_perm(child, PERM_ALL)
}
return child, err
}
func Blk_Close(child *BdrvChild) {
if child == nil || child.bs == nil {
return
}
bdrv_close(child.bs)
}
func Blk_Pread(root *BdrvChild, offset uint64, buf []uint8, bytes uint64) (uint64, error) {
var qiov QEMUIOVector
var err error
if root == nil || root.bs == nil {
return 0, Err_NullObject
}
qemu_iovec_init_buf(&qiov, unsafe.Pointer(&buf[0]), bytes)
if err = bdrv_preadv_part(root, offset, bytes, &qiov, 0, 0); err != nil {
return 0, err
}
return bytes, nil
}
// read object from the file, the object's size must be obtainable
func Blk_Pread_Object(child *BdrvChild, offset uint64, object any, size uint64) (uint64, error) {
var buffer bytes.Buffer
var err error
var buf []byte
var ret uint64
if child == nil || child.bs == nil {
return 0, Err_NullObject
}
buf = make([]byte, size)
if ret, err = Blk_Pread(child, offset, buf[:], size); err != nil {
return ret, err
}
buffer.Write(buf)
binary.Read(&buffer, binary.BigEndian, object)
return ret, nil
}
func Blk_Pwrite(root *BdrvChild, offset uint64, buf []uint8,
bytes uint64, flags BdrvRequestFlags) (uint64, error) {
var qiov QEMUIOVector
var err error
if root == nil {
return 0, Err_NullObject
}
qemu_iovec_init_buf(&qiov, unsafe.Pointer(&buf[0]), bytes)
if err = bdrv_pwritev_part(root, offset, bytes, &qiov, 0, flags); err != nil {
return 0, err
}
return bytes, nil
}
func Blk_Pwrite_Zeroes(root *BdrvChild, offset uint64,
bytes uint64, flags BdrvRequestFlags) (uint64, error) {
var qiov QEMUIOVector
var err error
if root == nil {
return 0, Err_NullObject
}
qemu_iovec_init_buf(&qiov, nil, bytes)
if err = bdrv_pwritev_part(root, offset, bytes, &qiov, 0, flags|BDRV_REQ_ZERO_WRITE); err != nil {
return 0, err
}
return bytes, nil
}
// Write the object to the file in the bigendian manner, return -1 if error occurs
func Blk_Pwrite_Object(child *BdrvChild, offset uint64, object any, size uint64) (uint64, error) {
var buffer bytes.Buffer
var err error
var ret uint64
if child == nil || child.bs == nil {
return 0, Err_NullObject
}
binary.Write(&buffer, binary.BigEndian, object)
buffer.Truncate(int(size))
if ret, err = Blk_Pwrite(child, offset, buffer.Bytes(), size, 0); err != nil {
return ret, err
}
return ret, nil
}
/**
* Return length in bytes on success, -errno on error.
* The length is always a multiple of BDRV_SECTOR_SIZE.
*/
/*
int64_t bdrv_getlength(BlockDriverState *bs)
*/
func Blk_Getlength(child *BdrvChild) (uint64, error) {
bs := child.bs
//equal to has_variable_length
if bs.Drv != nil && bs.Drv.bdrv_getlength != nil {
return bs.Drv.bdrv_getlength(bs)
}
var ret uint64 = bdrv_nb_sectors(bs)
if ret > math.MaxInt64/BDRV_SECTOR_SIZE {
return 0, ERR_E2BIG
}
return ret * BDRV_SECTOR_SIZE, nil
}
func Blk_Discard(child *BdrvChild, offset uint64, bytes uint64) error {
return bdrv_pdiscard(child, offset, bytes)
}
func Blk_Info(child *BdrvChild, detail bool, pretty bool) string {
bs := child.bs
return bs.Info(detail, pretty)
}
func get_driver(fmt string) *BlockDriver {
switch fmt {
case "raw":
return newRawDriver()
case "qcow2":
return newQcow2Driver()
default:
return nil
}
}