blob: 135b88600fc7ac9da0c1c3a59ccb90b3ce78e8e5 [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
Tim Windelschmidta3cd52c2024-04-18 23:21:16 +020049// FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR are defined in uapi/linux/fs.h
50// and normally would be imported from x/sys/unix. Since they don't exist
51// there define them here for now.
52const (
53 FS_IOC_FSGETXATTR = 0x801c581f
54 FS_IOC_FSSETXATTR = 0x401c5820
55)
Lorenz Brun1d801752020-04-02 09:24:51 +020056
57type FSXAttrs struct {
58 Flags FSXAttrFlag
59 ExtentSize uint32
60 ExtentCount uint32
61 ProjectID uint32
62 CoWExtentSize uint32
63 _pad [8]byte
64}
65
66func Get(file *os.File) (*FSXAttrs, error) {
67 var attrs FSXAttrs
68 _, _, errno := unix.Syscall(unix.SYS_IOCTL, file.Fd(), FS_IOC_FSGETXATTR, uintptr(unsafe.Pointer(&attrs)))
69 if errno != 0 {
70 return nil, fmt.Errorf("failed to execute getFSXAttrs: %v", errno)
71 }
72 return &attrs, nil
73}
74
75func Set(file *os.File, attrs *FSXAttrs) error {
76 _, _, errno := unix.Syscall(unix.SYS_IOCTL, file.Fd(), FS_IOC_FSSETXATTR, uintptr(unsafe.Pointer(attrs)))
77 if errno != 0 {
78 return fmt.Errorf("failed to execute setFSXAttrs: %v", errno)
79 }
80 return nil
81}