Debian Codegraph @

btrfs.go

raw symbols SCIP
1 package btrfs
2
3 import (
4 "fmt"
5 "io"
6 "os"
7 "path/filepath"
8 "strconv"
9 "syscall"
10
11 "github.com/dennwc/ioctl"
12 )
13
14 const SuperMagic uint32 = 0x9123683E
15
16 func CloneFile(dst, src *os.File) error {
17 return iocClone(dst, src)
18 }
19
20 func Open(path string, ro bool) (*FS, error) {
21 if ok, err := IsSubVolume(path); err != nil {
22 return nil, err
23 } else if !ok {
24 return nil, ErrNotBtrfs{Path: path}
25 }
26 var (
27 dir *os.File
28 err error
29 )
30 if ro {
31 dir, err = os.OpenFile(path, os.O_RDONLY|syscall.O_NOATIME, 0644)
32 if err != nil {
33 // Try without O_NOATIME as it requires ownership of the file
34 // or other priviliges
35 dir, err = os.OpenFile(path, os.O_RDONLY, 0644)
36 }
37 } else {
38 dir, err = os.Open(path)
39 }
40 if err != nil {
41 return nil, err
42 } else if st, err := dir.Stat(); err != nil {
43 dir.Close()
44 return nil, err
45 } else if !st.IsDir() {
46 dir.Close()
47 return nil, fmt.Errorf("not a directory: %s", path)
48 }
49 return &FS{f: dir}, nil
50 }
51
52 type FS struct {
53 f *os.File
54 }
55
56 func (f *FS) Close() error {
57 return f.f.Close()
58 }
59
60 type Info struct {
61 MaxID uint64
62 NumDevices uint64
63 FSID FSID
64 NodeSize uint32
65 SectorSize uint32
66 CloneAlignment uint32
67 }
68
69 func (f *FS) SubVolumeID() (uint64, error) {
70 id, err := getFileRootID(f.f)
71 if err != nil {
72 return 0, err
73 }
74 return uint64(id), nil
75 }
76
77 func (f *FS) Info() (out Info, err error) {
78 var arg btrfs_ioctl_fs_info_args
79 arg, err = iocFsInfo(f.f)
80 if err == nil {
81 out = Info{
82 MaxID: arg.max_id,
83 NumDevices: arg.num_devices,
84 FSID: arg.fsid,
85 NodeSize: arg.nodesize,
86 SectorSize: arg.sectorsize,
87 CloneAlignment: arg.clone_alignment,
88 }
89 }
90 return
91 }
92
93 type DevInfo struct {
94 UUID UUID
95 BytesUsed uint64
96 TotalBytes uint64
97 Path string
98 }
99
100 func (f *FS) GetDevInfo(id uint64) (out DevInfo, err error) {
101 var arg btrfs_ioctl_dev_info_args
102 arg.devid = id
103
104 if err = ioctl.Do(f.f, _BTRFS_IOC_DEV_INFO, &arg); err != nil {
105 return
106 }
107 out.UUID = arg.uuid
108 out.BytesUsed = arg.bytes_used
109 out.TotalBytes = arg.total_bytes
110 out.Path = stringFromBytes(arg.path[:])
111
112 return
113 }
114
115 type DevStats struct {
116 WriteErrs uint64
117 ReadErrs uint64
118 FlushErrs uint64
119 // Checksum error, bytenr error or contents is illegal: this is an
120 // indication that the block was damaged during read or write, or written to
121 // wrong location or read from wrong location.
122 CorruptionErrs uint64
123 // An indication that blocks have not been written.
124 GenerationErrs uint64
125 Unknown []uint64
126 }
127
128 func (f *FS) GetDevStats(id uint64) (out DevStats, err error) {
129 var arg btrfs_ioctl_get_dev_stats
130 arg.devid = id
131 arg.nr_items = _BTRFS_DEV_STAT_VALUES_MAX
132 arg.flags = 0
133 if err = ioctl.Do(f.f, _BTRFS_IOC_GET_DEV_STATS, &arg); err != nil {
134 return
135 }
136 i := 0
137 out.WriteErrs = arg.values[i]
138 i++
139 out.ReadErrs = arg.values[i]
140 i++
141 out.FlushErrs = arg.values[i]
142 i++
143 out.CorruptionErrs = arg.values[i]
144 i++
145 out.GenerationErrs = arg.values[i]
146 i++
147 if int(arg.nr_items) > i {
148 out.Unknown = arg.values[i:arg.nr_items]
149 }
150 return
151 }
152
153 type FSFeatureFlags struct {
154 Compatible FeatureFlags
155 CompatibleRO FeatureFlags
156 Incompatible IncompatFeatures
157 }
158
159 func (f *FS) GetFeatures() (out FSFeatureFlags, err error) {
160 var arg btrfs_ioctl_feature_flags
161 if err = ioctl.Do(f.f, _BTRFS_IOC_GET_FEATURES, &arg); err != nil {
162 return
163 }
164 out = FSFeatureFlags{
165 Compatible: arg.compat_flags,
166 CompatibleRO: arg.compat_ro_flags,
167 Incompatible: arg.incompat_flags,
168 }
169 return
170 }
171
172 func (f *FS) GetSupportedFeatures() (out FSFeatureFlags, err error) {
173 var arg [3]btrfs_ioctl_feature_flags
174 if err = ioctl.Do(f.f, _BTRFS_IOC_GET_SUPPORTED_FEATURES, &arg); err != nil {
175 return
176 }
177 out = FSFeatureFlags{
178 Compatible: arg[0].compat_flags,
179 CompatibleRO: arg[0].compat_ro_flags,
180 Incompatible: arg[0].incompat_flags,
181 }
182 //for i, a := range arg {
183 // out[i] = FSFeatureFlags{
184 // Compatible: a.compat_flags,
185 // CompatibleRO: a.compat_ro_flags,
186 // Incompatible: a.incompat_flags,
187 // }
188 //}
189 return
190 }
191
192 func (f *FS) GetFlags() (SubvolFlags, error) {
193 return iocSubvolGetflags(f.f)
194 }
195
196 func (f *FS) SetFlags(flags SubvolFlags) error {
197 return iocSubvolSetflags(f.f, flags)
198 }
199
200 func (f *FS) Sync() (err error) {
201 if err = ioctl.Ioctl(f.f, _BTRFS_IOC_START_SYNC, 0); err != nil {
202 return
203 }
204 return ioctl.Ioctl(f.f, _BTRFS_IOC_WAIT_SYNC, 0)
205 }
206
207 func (f *FS) CreateSubVolume(name string) error {
208 return CreateSubVolume(filepath.Join(f.f.Name(), name))
209 }
210
211 func (f *FS) DeleteSubVolume(name string) error {
212 return DeleteSubVolume(filepath.Join(f.f.Name(), name))
213 }
214
215 func (f *FS) Snapshot(dst string, ro bool) error {
216 return SnapshotSubVolume(f.f.Name(), filepath.Join(f.f.Name(), dst), ro)
217 }
218
219 func (f *FS) SnapshotSubVolume(name string, dst string, ro bool) error {
220 return SnapshotSubVolume(filepath.Join(f.f.Name(), name),
221 filepath.Join(f.f.Name(), dst), ro)
222 }
223
224 func (f *FS) Send(w io.Writer, parent string, subvols ...string) error {
225 if parent != "" {
226 parent = filepath.Join(f.f.Name(), parent)
227 }
228 sub := make([]string, 0, len(subvols))
229 for _, s := range subvols {
230 sub = append(sub, filepath.Join(f.f.Name(), s))
231 }
232 return Send(w, parent, sub...)
233 }
234
235 func (f *FS) Receive(r io.Reader) error {
236 return Receive(r, f.f.Name())
237 }
238
239 func (f *FS) ReceiveTo(r io.Reader, mount string) error {
240 return Receive(r, filepath.Join(f.f.Name(), mount))
241 }
242
243 func (f *FS) ListSubvolumes(filter func(SubvolInfo) bool) ([]SubvolInfo, error) {
244 m, err := listSubVolumes(f.f, filter)
245 if err != nil {
246 return nil, err
247 }
248 out := make([]SubvolInfo, 0, len(m))
249 for _, v := range m {
250 out = append(out, v)
251 }
252 return out, nil
253 }
254
255 func (f *FS) SubvolumeByUUID(uuid UUID) (*SubvolInfo, error) {
256 id, err := lookupUUIDSubvolItem(f.f, uuid)
257 if err != nil {
258 return nil, err
259 }
260 return subvolSearchByRootID(f.f, id, "")
261 }
262
263 func (f *FS) SubvolumeByReceivedUUID(uuid UUID) (*SubvolInfo, error) {
264 id, err := lookupUUIDReceivedSubvolItem(f.f, uuid)
265 if err != nil {
266 return nil, err
267 }
268 return subvolSearchByRootID(f.f, id, "")
269 }
270
271 func (f *FS) SubvolumeByPath(path string) (*SubvolInfo, error) {
272 return subvolSearchByPath(f.f, path)
273 }
274
275 func (f *FS) Usage() (UsageInfo, error) { return spaceUsage(f.f) }
276
277 func (f *FS) Balance(flags BalanceFlags) (BalanceProgress, error) {
278 args := btrfs_ioctl_balance_args{flags: flags}
279 err := iocBalanceV2(f.f, &args)
280 return args.stat, err
281 }
282
283 func (f *FS) Resize(size int64) error {
284 amount := strconv.FormatInt(size, 10)
285 args := &btrfs_ioctl_vol_args{}
286 args.SetName(amount)
287 if err := iocResize(f.f, args); err != nil {
288 return fmt.Errorf("resize failed: %v", err)
289 }
290 return nil
291 }
292
293 func (f *FS) ResizeToMax() error {
294 args := &btrfs_ioctl_vol_args{}
295 args.SetName("max")
296 if err := iocResize(f.f, args); err != nil {
297 return fmt.Errorf("resize failed: %v", err)
298 }
299 return nil
300 }