/
snapshots.go
222 lines (193 loc) · 6.48 KB
/
snapshots.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
/*
* Copyright (c) 2021, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
*/
package service
import (
"github.com/oracle/zfssa-csi-driver/pkg/utils"
"github.com/oracle/zfssa-csi-driver/pkg/zfssarest"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/protobuf/ptypes/timestamp"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"net/http"
"sync/atomic"
)
// Mount volume or BLock volume snapshot.
type zSnapshot struct {
bolt *utils.Bolt
refcount int32
state volumeState
href string
zvol zVolumeInterface
id *utils.SnapshotId
numClones int
spaceUnique int64
spaceData int64
timeStamp *timestamp.Timestamp
}
func newSnapshot(sid *utils.SnapshotId, zvol zVolumeInterface) *zSnapshot {
zsnap := new(zSnapshot)
zsnap.bolt = utils.NewBolt()
zsnap.zvol = zvol
zsnap.id = sid
zsnap.state = stateCreating
return zsnap
}
func (zsnap *zSnapshot) create(ctx context.Context, token *zfssarest.Token) (
*csi.CreateSnapshotResponse, error) {
utils.GetLogCTRL(ctx, 5).Println("zsnap.create")
snapinfo, httpStatus, err := zfssarest.CreateSnapshot(ctx, token, zsnap.zvol.getHref(), zsnap.id.Name)
if err != nil {
if httpStatus != http.StatusConflict {
zsnap.state = stateDeleted
return nil, err
}
// The creation failed because the source file system already has a snapshot
// with the same name.
if zsnap.getState() == stateCreated {
snapinfo, _, err := zfssarest.GetSnapshot(ctx, token, zsnap.zvol.getHref(), zsnap.id.Name)
if err != nil {
return nil, err
}
err = zsnap.setInfo(snapinfo)
if err != nil {
zsnap.state = stateDeleted
return nil, err
}
}
} else {
err = zsnap.setInfo(snapinfo)
if err != nil {
zsnap.state = stateDeleted
return nil, err
}
}
return &csi.CreateSnapshotResponse {
Snapshot: &csi.Snapshot{
SizeBytes: zsnap.spaceData,
SnapshotId: zsnap.id.String(),
SourceVolumeId: zsnap.id.VolumeId.String(),
CreationTime: zsnap.timeStamp,
ReadyToUse: true,
}}, nil
}
func (zsnap *zSnapshot) delete(ctx context.Context, token *zfssarest.Token) (
*csi.DeleteSnapshotResponse, error) {
utils.GetLogCTRL(ctx, 5).Println("zsnap.delete")
// Update the snapshot information.
httpStatus, err := zsnap.refreshDetails(ctx, token)
if err != nil {
if httpStatus != http.StatusNotFound {
return nil, err
}
zsnap.state = stateDeleted
return &csi.DeleteSnapshotResponse{}, nil
}
if zsnap.getNumClones() > 0 {
return nil, status.Errorf(codes.FailedPrecondition, "Snapshot has (%d) dependents", zsnap.numClones)
}
_, httpStatus, err = zfssarest.DeleteSnapshot(ctx, token, zsnap.href)
if err != nil && httpStatus != http.StatusNotFound {
return nil, err
}
zsnap.state = stateDeleted
return &csi.DeleteSnapshotResponse{}, nil
}
func (zsnap *zSnapshot) getDetails(ctx context.Context, token *zfssarest.Token) (int, error) {
utils.GetLogCTRL(ctx, 5).Println("zsnap.getDetails")
snapinfo, httpStatus, err := zfssarest.GetSnapshot(ctx, token, zsnap.zvol.getHref(), zsnap.id.Name)
if err != nil {
return httpStatus, err
}
zsnap.timeStamp, err = utils.DateToUnix(snapinfo.Creation)
if err != nil {
return httpStatus, err
}
zsnap.numClones = snapinfo.NumClones
zsnap.spaceData = snapinfo.SpaceData
zsnap.spaceUnique = snapinfo.SpaceUnique
zsnap.href = snapinfo.Href
zsnap.state = stateCreated
return httpStatus, nil
}
func (zsnap *zSnapshot) refreshDetails(ctx context.Context, token *zfssarest.Token) (int, error) {
snapinfo, httpStatus, err := zfssarest.GetSnapshot(ctx, token, zsnap.zvol.getHref(), zsnap.id.Name)
if err == nil {
zsnap.numClones = snapinfo.NumClones
zsnap.spaceData = snapinfo.SpaceData
zsnap.spaceUnique = snapinfo.SpaceUnique
}
return httpStatus, err
}
// Populate the snapshot structure with the information provided
func (zsnap *zSnapshot) setInfo(snapinfo *zfssarest.Snapshot) error {
var err error
zsnap.timeStamp, err = utils.DateToUnix(snapinfo.Creation)
if err != nil {
return err
}
zsnap.numClones = snapinfo.NumClones
zsnap.spaceData = snapinfo.SpaceData
zsnap.spaceUnique = snapinfo.SpaceUnique
zsnap.href = snapinfo.Href
zsnap.state = stateCreated
return nil
}
// Waits until the file system is available and, when it is, returns with its current state.
func (zsnap *zSnapshot) hold(ctx context.Context) volumeState {
atomic.AddInt32(&zsnap.refcount, 1)
return zsnap.state
}
func (zsnap *zSnapshot) lock(ctx context.Context) volumeState {
zsnap.bolt.Lock(ctx)
return zsnap.state
}
func (zsnap *zSnapshot) unlock(ctx context.Context) (int32, volumeState){
zsnap.bolt.Unlock(ctx)
return zsnap.refcount, zsnap.state
}
// Releases the file system and returns its current reference count.
func (zsnap *zSnapshot) release(ctx context.Context) (int32, volumeState) {
return atomic.AddInt32(&zsnap.refcount, -1), zsnap.state
}
func (zsnap *zSnapshot) isBlock() bool { return zsnap.zvol.isBlock() }
func (zsnap *zSnapshot) getSourceVolume() zVolumeInterface { return zsnap.zvol }
func (zsnap *zSnapshot) getState() volumeState { return zsnap.state }
func (zsnap *zSnapshot) getName() string { return zsnap.id.Name }
func (zsnap *zSnapshot) getStringId() string { return zsnap.id.String() }
func (zsnap *zSnapshot) getStringSourceId() string { return zsnap.id.VolumeId.String() }
func (zsnap *zSnapshot) getHref() string { return zsnap.href }
func (zsnap *zSnapshot) getSize() int64 { return zsnap.spaceData }
func (zsnap *zSnapshot) getCreationTime() *timestamp.Timestamp { return zsnap.timeStamp }
func (zsnap *zSnapshot) getNumClones() int { return zsnap.numClones }
func zfssaSnapshotList2csiSnapshotList(ctx context.Context, zfssa string,
snapList []zfssarest.Snapshot) []*csi.ListSnapshotsResponse_Entry {
utils.GetLogCTRL(ctx, 5).Println("zfssaSnapshotList2csiSnapshotList")
entries := make([]*csi.ListSnapshotsResponse_Entry, 0, len(snapList))
for _, snapInfo := range snapList {
sid, err := utils.SnapshotIdStringFromHref(zfssa, snapInfo.Href)
if err != nil {
continue
}
vid, err := utils.VolumeIdStringFromHref(zfssa, snapInfo.Href)
if err != nil {
continue
}
creationTime, err := utils.DateToUnix(snapInfo.Creation)
if err != nil {
continue
}
entry := new(csi.ListSnapshotsResponse_Entry)
entry.Snapshot = &csi.Snapshot{
SnapshotId: sid,
SizeBytes: snapInfo.SpaceData,
SourceVolumeId: vid,
CreationTime: creationTime,
ReadyToUse: true,
}
entries = append(entries, entry)
}
return entries
}