-
Notifications
You must be signed in to change notification settings - Fork 11
/
btrfs.go
300 lines (263 loc) · 6.66 KB
/
btrfs.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package btrfs
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"syscall"
"github.com/dennwc/ioctl"
)
const SuperMagic uint32 = 0x9123683E
func CloneFile(dst, src *os.File) error {
return iocClone(dst, src)
}
func Open(path string, ro bool) (*FS, error) {
if ok, err := IsSubVolume(path); err != nil {
return nil, err
} else if !ok {
return nil, ErrNotBtrfs{Path: path}
}
var (
dir *os.File
err error
)
if ro {
dir, err = os.OpenFile(path, os.O_RDONLY|syscall.O_NOATIME, 0644)
if err != nil {
// Try without O_NOATIME as it requires ownership of the file
// or other priviliges
dir, err = os.OpenFile(path, os.O_RDONLY, 0644)
}
} else {
dir, err = os.Open(path)
}
if err != nil {
return nil, err
} else if st, err := dir.Stat(); err != nil {
dir.Close()
return nil, err
} else if !st.IsDir() {
dir.Close()
return nil, fmt.Errorf("not a directory: %s", path)
}
return &FS{f: dir}, nil
}
type FS struct {
f *os.File
}
func (f *FS) Close() error {
return f.f.Close()
}
type Info struct {
MaxID uint64
NumDevices uint64
FSID FSID
NodeSize uint32
SectorSize uint32
CloneAlignment uint32
}
func (f *FS) SubVolumeID() (uint64, error) {
id, err := getFileRootID(f.f)
if err != nil {
return 0, err
}
return uint64(id), nil
}
func (f *FS) Info() (out Info, err error) {
var arg btrfs_ioctl_fs_info_args
arg, err = iocFsInfo(f.f)
if err == nil {
out = Info{
MaxID: arg.max_id,
NumDevices: arg.num_devices,
FSID: arg.fsid,
NodeSize: arg.nodesize,
SectorSize: arg.sectorsize,
CloneAlignment: arg.clone_alignment,
}
}
return
}
type DevInfo struct {
UUID UUID
BytesUsed uint64
TotalBytes uint64
Path string
}
func (f *FS) GetDevInfo(id uint64) (out DevInfo, err error) {
var arg btrfs_ioctl_dev_info_args
arg.devid = id
if err = ioctl.Do(f.f, _BTRFS_IOC_DEV_INFO, &arg); err != nil {
return
}
out.UUID = arg.uuid
out.BytesUsed = arg.bytes_used
out.TotalBytes = arg.total_bytes
out.Path = stringFromBytes(arg.path[:])
return
}
type DevStats struct {
WriteErrs uint64
ReadErrs uint64
FlushErrs uint64
// Checksum error, bytenr error or contents is illegal: this is an
// indication that the block was damaged during read or write, or written to
// wrong location or read from wrong location.
CorruptionErrs uint64
// An indication that blocks have not been written.
GenerationErrs uint64
Unknown []uint64
}
func (f *FS) GetDevStats(id uint64) (out DevStats, err error) {
var arg btrfs_ioctl_get_dev_stats
arg.devid = id
arg.nr_items = _BTRFS_DEV_STAT_VALUES_MAX
arg.flags = 0
if err = ioctl.Do(f.f, _BTRFS_IOC_GET_DEV_STATS, &arg); err != nil {
return
}
i := 0
out.WriteErrs = arg.values[i]
i++
out.ReadErrs = arg.values[i]
i++
out.FlushErrs = arg.values[i]
i++
out.CorruptionErrs = arg.values[i]
i++
out.GenerationErrs = arg.values[i]
i++
if int(arg.nr_items) > i {
out.Unknown = arg.values[i:arg.nr_items]
}
return
}
type FSFeatureFlags struct {
Compatible FeatureFlags
CompatibleRO FeatureFlags
Incompatible IncompatFeatures
}
func (f *FS) GetFeatures() (out FSFeatureFlags, err error) {
var arg btrfs_ioctl_feature_flags
if err = ioctl.Do(f.f, _BTRFS_IOC_GET_FEATURES, &arg); err != nil {
return
}
out = FSFeatureFlags{
Compatible: arg.compat_flags,
CompatibleRO: arg.compat_ro_flags,
Incompatible: arg.incompat_flags,
}
return
}
func (f *FS) GetSupportedFeatures() (out FSFeatureFlags, err error) {
var arg [3]btrfs_ioctl_feature_flags
if err = ioctl.Do(f.f, _BTRFS_IOC_GET_SUPPORTED_FEATURES, &arg); err != nil {
return
}
out = FSFeatureFlags{
Compatible: arg[0].compat_flags,
CompatibleRO: arg[0].compat_ro_flags,
Incompatible: arg[0].incompat_flags,
}
//for i, a := range arg {
// out[i] = FSFeatureFlags{
// Compatible: a.compat_flags,
// CompatibleRO: a.compat_ro_flags,
// Incompatible: a.incompat_flags,
// }
//}
return
}
func (f *FS) GetFlags() (SubvolFlags, error) {
return iocSubvolGetflags(f.f)
}
func (f *FS) SetFlags(flags SubvolFlags) error {
return iocSubvolSetflags(f.f, flags)
}
func (f *FS) Sync() (err error) {
if err = ioctl.Ioctl(f.f, _BTRFS_IOC_START_SYNC, 0); err != nil {
return
}
return ioctl.Ioctl(f.f, _BTRFS_IOC_WAIT_SYNC, 0)
}
func (f *FS) CreateSubVolume(name string) error {
return CreateSubVolume(filepath.Join(f.f.Name(), name))
}
func (f *FS) DeleteSubVolume(name string) error {
return DeleteSubVolume(filepath.Join(f.f.Name(), name))
}
func (f *FS) Snapshot(dst string, ro bool) error {
return SnapshotSubVolume(f.f.Name(), filepath.Join(f.f.Name(), dst), ro)
}
func (f *FS) SnapshotSubVolume(name string, dst string, ro bool) error {
return SnapshotSubVolume(filepath.Join(f.f.Name(), name),
filepath.Join(f.f.Name(), dst), ro)
}
func (f *FS) Send(w io.Writer, parent string, subvols ...string) error {
if parent != "" {
parent = filepath.Join(f.f.Name(), parent)
}
sub := make([]string, 0, len(subvols))
for _, s := range subvols {
sub = append(sub, filepath.Join(f.f.Name(), s))
}
return Send(w, parent, sub...)
}
func (f *FS) Receive(r io.Reader) error {
return Receive(r, f.f.Name())
}
func (f *FS) ReceiveTo(r io.Reader, mount string) error {
return Receive(r, filepath.Join(f.f.Name(), mount))
}
func (f *FS) ListSubvolumes(filter func(SubvolInfo) bool) ([]SubvolInfo, error) {
m, err := listSubVolumes(f.f, filter)
if err != nil {
return nil, err
}
out := make([]SubvolInfo, 0, len(m))
for _, v := range m {
out = append(out, v)
}
return out, nil
}
func (f *FS) SubvolumeByUUID(uuid UUID) (*SubvolInfo, error) {
id, err := lookupUUIDSubvolItem(f.f, uuid)
if err != nil {
return nil, err
}
return subvolSearchByRootID(f.f, id, "")
}
func (f *FS) SubvolumeByReceivedUUID(uuid UUID) (*SubvolInfo, error) {
id, err := lookupUUIDReceivedSubvolItem(f.f, uuid)
if err != nil {
return nil, err
}
return subvolSearchByRootID(f.f, id, "")
}
func (f *FS) SubvolumeByPath(path string) (*SubvolInfo, error) {
return subvolSearchByPath(f.f, path)
}
func (f *FS) Usage() (UsageInfo, error) { return spaceUsage(f.f) }
func (f *FS) Balance(flags BalanceFlags) (BalanceProgress, error) {
args := btrfs_ioctl_balance_args{flags: flags}
err := iocBalanceV2(f.f, &args)
return args.stat, err
}
func (f *FS) Resize(size int64) error {
amount := strconv.FormatInt(size, 10)
args := &btrfs_ioctl_vol_args{}
args.SetName(amount)
if err := iocResize(f.f, args); err != nil {
return fmt.Errorf("resize failed: %v", err)
}
return nil
}
func (f *FS) ResizeToMax() error {
args := &btrfs_ioctl_vol_args{}
args.SetName("max")
if err := iocResize(f.f, args); err != nil {
return fmt.Errorf("resize failed: %v", err)
}
return nil
}