-
Notifications
You must be signed in to change notification settings - Fork 11
/
volumes.go
234 lines (201 loc) · 4.96 KB
/
volumes.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package api
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
rhp2 "go.sia.tech/core/rhp/v2"
"go.sia.tech/core/types"
"go.sia.tech/hostd/host/storage"
"go.sia.tech/jape"
)
type (
volumeJobs struct {
volumes VolumeManager
mu sync.Mutex // protects jobs
jobs map[int64]context.CancelFunc
}
)
func (vj *volumeJobs) AddVolume(path string, maxSectors uint64) (storage.Volume, error) {
ctx, cancel := context.WithCancel(context.Background())
complete := make(chan error, 1)
volume, err := vj.volumes.AddVolume(ctx, path, maxSectors, complete)
if err != nil {
cancel()
return storage.Volume{}, err
}
vj.mu.Lock()
defer vj.mu.Unlock()
vj.jobs[volume.ID] = cancel
go func() {
defer cancel()
select {
case <-ctx.Done():
case <-complete:
}
vj.mu.Lock()
defer vj.mu.Unlock()
delete(vj.jobs, volume.ID)
}()
return volume, nil
}
func (vj *volumeJobs) RemoveVolume(id int64, force bool) error {
vj.mu.Lock()
defer vj.mu.Unlock()
if _, exists := vj.jobs[id]; exists {
return errors.New("volume is busy")
}
ctx, cancel := context.WithCancel(context.Background())
complete := make(chan error, 1)
err := vj.volumes.RemoveVolume(ctx, id, force, complete)
if err != nil {
cancel()
return err
}
vj.jobs[id] = cancel
go func() {
defer cancel()
select {
case <-ctx.Done():
case <-complete:
}
vj.mu.Lock()
defer vj.mu.Unlock()
delete(vj.jobs, id)
}()
return nil
}
func (vj *volumeJobs) ResizeVolume(id int64, newSize uint64) error {
vj.mu.Lock()
defer vj.mu.Unlock()
if _, exists := vj.jobs[id]; exists {
return errors.New("volume is busy")
}
ctx, cancel := context.WithCancel(context.Background())
complete := make(chan error, 1)
err := vj.volumes.ResizeVolume(ctx, id, newSize, complete)
if err != nil {
cancel()
return err
}
vj.jobs[id] = cancel
go func() {
defer cancel()
select {
case <-ctx.Done():
case <-complete:
}
vj.mu.Lock()
defer vj.mu.Unlock()
delete(vj.jobs, id)
}()
return nil
}
func (vj *volumeJobs) Cancel(id int64) error {
vj.mu.Lock()
defer vj.mu.Unlock()
cancel, exists := vj.jobs[id]
if !exists {
return fmt.Errorf("no job for volume %d", id)
}
cancel()
delete(vj.jobs, id)
return nil
}
func (a *api) handleGETVolumes(c jape.Context) {
volumes, err := a.volumes.Volumes()
if !a.checkServerError(c, "failed to get volumes", err) {
return
}
var jsonVolumes []VolumeMeta
for _, volume := range volumes {
jsonVolumes = append(jsonVolumes, toJSONVolume(volume))
}
a.writeResponse(c, VolumeResp(jsonVolumes))
}
func (a *api) handlePOSTVolume(c jape.Context) {
var req AddVolumeRequest
if err := c.Decode(&req); err != nil {
return
} else if req.LocalPath == "" {
c.Error(errors.New("local path is required"), http.StatusBadRequest)
return
} else if req.MaxSectors == 0 {
c.Error(errors.New("max sectors is required"), http.StatusBadRequest)
return
}
volume, err := a.volumeJobs.AddVolume(req.LocalPath, req.MaxSectors)
if !a.checkServerError(c, "failed to add volume", err) {
return
}
c.Encode(volume)
}
func (a *api) handleDeleteVolume(c jape.Context) {
var id int64
var force bool
if err := c.DecodeParam("id", &id); err != nil {
return
} else if id < 0 {
c.Error(errors.New("invalid volume id"), http.StatusBadRequest)
return
} else if err := c.DecodeForm("force", &force); err != nil {
return
}
err := a.volumeJobs.RemoveVolume(id, force)
a.checkServerError(c, "failed to remove volume", err)
}
func (a *api) handlePUTVolumeResize(c jape.Context) {
var id int64
if err := c.DecodeParam("id", &id); err != nil {
return
} else if id < 0 {
c.Error(errors.New("invalid volume id"), http.StatusBadRequest)
return
}
var req ResizeVolumeRequest
if err := c.Decode(&req); err != nil {
return
}
err := a.volumeJobs.ResizeVolume(id, req.MaxSectors)
a.checkServerError(c, "failed to resize volume", err)
}
func (a *api) handleDELETEVolumeCancelOp(c jape.Context) {
var id int64
if err := c.DecodeParam("id", &id); err != nil {
return
} else if id < 0 {
c.Error(errors.New("invalid volume id"), http.StatusBadRequest)
return
}
err := a.volumeJobs.Cancel(id)
a.checkServerError(c, "failed to cancel operation", err)
}
func (a *api) handleGETVerifySector(jc jape.Context) {
var root types.Hash256
if err := jc.DecodeParam("root", &root); err != nil {
return
}
refs, err := a.volumes.SectorReferences(root)
if err != nil {
jc.Error(err, http.StatusInternalServerError)
return
}
resp := VerifySectorResponse{
SectorReference: refs,
}
// if the sector is not referenced return the empty response without
// attempting to read the sector data
if len(refs.Contracts) == 0 && refs.TempStorage == 0 && refs.Locks == 0 {
jc.Encode(resp)
return
}
// try to read the sector data and verify the root
data, err := a.volumes.Read(root)
if err != nil {
resp.Error = err.Error()
} else if calc := rhp2.SectorRoot(data); calc != root {
resp.Error = fmt.Sprintf("sector is corrupt: expected root %q, got %q", root, calc)
}
jc.Encode(resp)
}