-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition.go
201 lines (184 loc) · 4.18 KB
/
partition.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
package rubberhose
import (
"errors"
"fmt"
"io"
"log"
"github.com/dop251/buse"
)
type Partition struct {
*Disk
*ExposedPartition
blockSize int64
key []byte
blocks []*Block
}
type ExposedPartition struct {
*buse.Device
Path string
}
func NewPartition(blockSize int64, blocks []*Block) Partition {
return Partition{blockSize: blockSize, blocks: blocks}
}
func (par *Partition) ReadAt(p []byte, off int64) (int, error) {
blockNum := off / par.blockSize
blockOff := off % par.blockSize
if blockNum > int64(len(par.blocks)) {
return 0, io.EOF
}
originalLength := len(p)
for len(p) > 0 {
if int(blockNum) >= len(par.blocks) {
return originalLength - len(p), io.EOF
}
read, err := par.blocks[blockNum].ReadAt(p, blockOff)
if err != io.EOF {
return originalLength - len(p), err
}
p = p[read:]
blockOff = 0
blockNum++
}
return originalLength, nil
}
func (par *Partition) WriteAt(p []byte, off int64) (int, error) {
blockNum := off / par.blockSize
blockOff := off % par.blockSize
if blockNum > int64(len(par.blocks)) {
return 0, io.EOF
}
originalLength := len(p)
for len(p) > 0 {
if int(blockNum) >= len(par.blocks) {
return originalLength - len(p), io.EOF
}
written, err := par.blocks[blockNum].WriteAt(p, blockOff)
p = p[written:]
if err != nil && err != io.ErrShortWrite {
return originalLength - len(p), err
}
blockOff = 0
blockNum++
}
return originalLength, nil
}
func (par *Partition) GetBlockCount() int {
return len(par.blocks)
}
func (par *Partition) GetDataSize() int64 {
var size int64
for _, b := range par.blocks {
size += b.GetDataSize()
}
return size
}
var ErrInvalidBlockStructure = errors.New("invalid block structure")
func (par *Partition) orderBlocks() error {
blocks := par.blocks
finished := make([]*Block, 0, len(par.blocks))
for len(blocks) != 0 {
blockToRemove := -1
for i, b := range blocks {
nextBlockNum, err := b.GetNextBlockID()
if err != nil {
return err
}
if len(finished) == 0 {
if nextBlockNum != -1 {
continue
}
finished = append(finished, b)
blockToRemove = i
break
}
lastBlockNum := finished[len(finished)-1].num
if nextBlockNum == lastBlockNum {
finished = append(finished, b)
blockToRemove = i
break
}
}
if blockToRemove == -1 {
return ErrInvalidBlockStructure
}
blocks[len(blocks)-1], blocks[blockToRemove] = blocks[blockToRemove], blocks[len(blocks)-1]
blocks = blocks[:len(blocks)-1]
}
for i, j := 0, len(finished)-1; i < j; i, j = i+1, j-1 {
finished[i], finished[j] = finished[j], finished[i]
}
copy(par.blocks, finished)
return nil
}
func (par *Partition) Close() error {
return par.Sync()
}
var counter int
func (par *Partition) Expose() (string, *buse.Device) {
for {
path := fmt.Sprintf("/dev/nbd%d", counter)
counter++
bd, err := par.ExposePath(path)
if err != nil {
continue
}
return path, bd
}
}
func (par *Partition) ExposePath(path string) (*buse.Device, error) {
if ep := par.ExposedPartition; ep != nil {
ep.Device.Disconnect()
par.ExposedPartition = nil
}
bd, err := buse.NewDevice(path, par.GetDataSize(), par)
if err != nil {
return nil, err
}
go func() {
err := bd.Run()
if err != nil {
log.Fatal("Error running buse device: ", err)
}
}()
par.ExposedPartition = &ExposedPartition{Path: path, Device: bd}
return bd, nil
}
func (par Partition) Delete() error {
for _, b := range par.blocks {
err := b.Delete()
if err != nil {
return err
}
delete(par.Disk.usedBlocks, b.num)
}
return par.Sync()
}
func (par *Partition) Resize(blockCount int) error {
delta := blockCount - len(par.blocks)
if delta == 0 {
return nil
}
if delta > 0 {
lastBlock := par.blocks[len(par.blocks)-1]
for i := 0; i < delta; i++ {
block, err := par.Disk.allocateBlock(par.key)
if err != nil {
return err
}
if err := lastBlock.Write(block.num); err != nil {
return err
}
par.blocks = append(par.blocks, block)
}
} else {
off := len(par.blocks) + delta
toDelete := par.blocks[off:]
par.blocks = par.blocks[:off]
for _, b := range toDelete {
if err := b.Delete(); err != nil {
return err
}
}
}
return par.blocks[len(par.blocks)-1].Write(-1)
}