blob: 1d455ebcefbd14f0867ac2fd4e61310a6dd133bf [file] [log] [blame]
Lorenz Brun1d801752020-04-02 09:24:51 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package fsxattrs
18
19import (
20 "fmt"
21 "os"
22 "unsafe"
23
24 "golang.org/x/sys/unix"
25)
26
27type FSXAttrFlag uint32
28
29// Defined in uapi/linux/fs.h
30const (
31 FlagRealtime FSXAttrFlag = 0x00000001
32 FlagPreallocated FSXAttrFlag = 0x00000002
33 FlagImmutable FSXAttrFlag = 0x00000008
34 FlagAppend FSXAttrFlag = 0x00000010
35 FlagSync FSXAttrFlag = 0x00000020
36 FlagNoATime FSXAttrFlag = 0x00000040
37 FlagNoDump FSXAttrFlag = 0x00000080
38 FlagRealtimeInherit FSXAttrFlag = 0x00000100
39 FlagProjectInherit FSXAttrFlag = 0x00000200
40 FlagNoSymlinks FSXAttrFlag = 0x00000400
41 FlagExtentSize FSXAttrFlag = 0x00000800
42 FlagNoDefragment FSXAttrFlag = 0x00002000
43 FlagFilestream FSXAttrFlag = 0x00004000
44 FlagDAX FSXAttrFlag = 0x00008000
45 FlagCOWExtentSize FSXAttrFlag = 0x00010000
46 FlagHasAttribute FSXAttrFlag = 0x80000000
47)
48
49// FS_IOC_FSGETXATTR/FS_IOC_FSSETXATTR are defined in uapi/linux/fs.h
50const FS_IOC_FSGETXATTR = 0x801c581f
51const FS_IOC_FSSETXATTR = 0x401c5820
52
53type FSXAttrs struct {
54 Flags FSXAttrFlag
55 ExtentSize uint32
56 ExtentCount uint32
57 ProjectID uint32
58 CoWExtentSize uint32
59 _pad [8]byte
60}
61
62func Get(file *os.File) (*FSXAttrs, error) {
63 var attrs FSXAttrs
64 _, _, errno := unix.Syscall(unix.SYS_IOCTL, file.Fd(), FS_IOC_FSGETXATTR, uintptr(unsafe.Pointer(&attrs)))
65 if errno != 0 {
66 return nil, fmt.Errorf("failed to execute getFSXAttrs: %v", errno)
67 }
68 return &attrs, nil
69}
70
71func Set(file *os.File, attrs *FSXAttrs) error {
72 _, _, errno := unix.Syscall(unix.SYS_IOCTL, file.Fd(), FS_IOC_FSSETXATTR, uintptr(unsafe.Pointer(attrs)))
73 if errno != 0 {
74 return fmt.Errorf("failed to execute setFSXAttrs: %v", errno)
75 }
76 return nil
77}