-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrefcount.go
147 lines (117 loc) · 3.83 KB
/
refcount.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
/* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2023 Damian Peckett <damian@peckett>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package qcow2
import (
"fmt"
"os"
)
func (i *Image) getRefcount(diskOffset int64) (uint64, error) {
refcountOffset, err := i.diskToRefcountOffset(diskOffset)
if err != nil {
return 0, err
}
refcountBits := int64(1 << i.hdr.RefcountOrder)
return readBits(i.f, refcountOffset, refcountBits)
}
func (i *Image) setRefcount(diskOffset int64, refcount uint64) error {
refcountOffset, err := i.diskToRefcountOffset(diskOffset)
if err != nil {
return err
}
refcountBits := int64(1 << i.hdr.RefcountOrder)
return writeBits(i.f, refcountOffset, refcountBits, refcount)
}
func (i *Image) incrementRefcounts(l1TableOffset int64, l1Size int) error {
l2EntriesPerTable := i.clusterSize / 8
// 1. Go through each L1 entry.
l1Table, err := i.readTable(l1TableOffset, l1Size)
if err != nil {
return err
}
for l1Index, l1EntryRaw := range l1Table {
l1Entry := L1TableEntry(l1EntryRaw)
// 2. Go through each L2 entry.
l2Table, err := i.readTable(l1Entry.Offset(), int(l2EntriesPerTable))
if err != nil {
return err
}
for l2Index, l2EntryRaw := range l2Table {
l2Entry := L2TableEntry(l2EntryRaw)
if l2Entry.Unallocated() {
continue
}
// 3. Calculate the disk offset for the L2 entry.
diskOffset := int64(l1Index)*l2EntriesPerTable*i.clusterSize + int64(l2Index)*i.clusterSize
// 4. Get the refcount for the disk offset.
refcount, err := i.getRefcount(diskOffset)
if err != nil {
return err
}
// 5. Increment the refcount for the disk offset.
err = i.setRefcount(diskOffset, refcount+1)
if err != nil {
return err
}
}
}
return nil
}
func (i *Image) diskToRefcountOffset(diskOffset int64) (int64, error) {
refcountBits := int64(1 << i.hdr.RefcountOrder)
refcountBlockEntries := i.clusterSize * 8 / refcountBits
refcountBlockIndex := (diskOffset / i.clusterSize) % refcountBlockEntries
refcountTableIndex := (diskOffset / i.clusterSize) / refcountBlockEntries
refCountTableEntries := (int64(i.hdr.RefcountTableClusters) * i.clusterSize) / 8
refCountTable, err := i.readTable(int64(i.hdr.RefcountTableOffset), int(refCountTableEntries))
if err != nil {
return 0, err
}
refcountBlockOffset := int64(refCountTable[refcountTableIndex] &^ ((1 << 9) - 1))
return refcountBlockOffset + refcountBlockIndex*refcountBits, nil
}
func readBits(f *os.File, imageOffset int64, nBits int64) (uint64, error) {
nBytes := (nBits + 7) / 8
buf := make([]byte, nBytes)
if _, err := f.ReadAt(buf, imageOffset); err != nil {
return 0, fmt.Errorf("failed to read bits: %w", err)
}
var bits uint64
for bitIdx := 0; bitIdx < int(nBits); bitIdx++ {
bits <<= 1
byteIdx := bitIdx / 8
bitPosition := 7 - (bitIdx % 8)
if buf[byteIdx]&(1<<bitPosition) != 0 {
bits |= 1
}
}
return bits, nil
}
func writeBits(f *os.File, imageOffset int64, nBits int64, value uint64) error {
nBytes := (nBits + 7) / 8
buf := make([]byte, nBytes)
for bitIdx := 0; bitIdx < int(nBits); bitIdx++ {
bitMask := uint64(1) << (uint64(nBits) - 1 - uint64(bitIdx))
if value&bitMask != 0 {
byteIdx := bitIdx / 8
buf[byteIdx] |= 1 << (7 - (bitIdx % 8))
}
}
if _, err := f.WriteAt(buf, imageOffset); err != nil {
return fmt.Errorf("failed to write bits: %w", err)
}
return nil
}