| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 3 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 4 | // Package quotactl implements a low-level wrapper around the modern portion of |
| 5 | // Linux's quotactl() syscall. See the fsquota package for a nicer interface to |
| 6 | // the most common part of this API. |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 7 | package quotactl |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 11 | "os" |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 12 | "unsafe" |
| 13 | |
| 14 | "golang.org/x/sys/unix" |
| 15 | ) |
| 16 | |
| 17 | type QuotaType uint |
| 18 | |
| 19 | const ( |
| 20 | QuotaTypeUser QuotaType = iota |
| 21 | QuotaTypeGroup |
| 22 | QuotaTypeProject |
| 23 | ) |
| 24 | |
| 25 | const ( |
| Tim Windelschmidt | 1d3a83d | 2024-04-11 01:40:58 +0200 | [diff] [blame] | 26 | Q_SYNC uint = (0x800001 + iota) << 8 |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 27 | Q_QUOTAON |
| 28 | Q_QUOTAOFF |
| 29 | Q_GETFMT |
| 30 | Q_GETINFO |
| 31 | Q_SETINFO |
| 32 | Q_GETQUOTA |
| 33 | Q_SETQUOTA |
| 34 | Q_GETNEXTQUOTA |
| 35 | ) |
| 36 | |
| 37 | const ( |
| 38 | FlagBLimitsValid = 1 << iota |
| 39 | FlagSpaceValid |
| 40 | FlagILimitsValid |
| 41 | FlagInodesValid |
| 42 | FlagBTimeValid |
| 43 | FlagITimeValid |
| 44 | ) |
| 45 | |
| 46 | type DQInfo struct { |
| 47 | Bgrace uint64 |
| 48 | Igrace uint64 |
| 49 | Flags uint32 |
| 50 | Valid uint32 |
| 51 | } |
| 52 | |
| 53 | type Quota struct { |
| 54 | BHardLimit uint64 // Both Byte limits are prescaled by 1024 (so are in KiB), but CurSpace is in B |
| 55 | BSoftLimit uint64 |
| 56 | CurSpace uint64 |
| 57 | IHardLimit uint64 |
| 58 | ISoftLimit uint64 |
| 59 | CurInodes uint64 |
| 60 | BTime uint64 |
| 61 | ITime uint64 |
| 62 | Valid uint32 |
| 63 | } |
| 64 | |
| 65 | type NextDQBlk struct { |
| 66 | HardLimitBytes uint64 |
| 67 | SoftLimitBytes uint64 |
| 68 | CurrentBytes uint64 |
| 69 | HardLimitInodes uint64 |
| 70 | SoftLimitInodes uint64 |
| 71 | CurrentInodes uint64 |
| 72 | BTime uint64 |
| 73 | ITime uint64 |
| 74 | Valid uint32 |
| 75 | ID uint32 |
| 76 | } |
| 77 | |
| 78 | type QuotaFormat uint32 |
| 79 | |
| 80 | // Collected from quota_format_type structs |
| 81 | const ( |
| 82 | // QuotaFormatNone is a special case where all quota information is |
| 83 | // stored inside filesystem metadata and thus requires no quotaFilePath. |
| 84 | QuotaFormatNone QuotaFormat = 0 |
| 85 | QuotaFormatVFSOld QuotaFormat = 1 |
| 86 | QuotaFormatVFSV0 QuotaFormat = 2 |
| 87 | QuotaFormatOCFS2 QuotaFormat = 3 |
| 88 | QuotaFormatVFSV1 QuotaFormat = 4 |
| 89 | ) |
| 90 | |
| 91 | // QuotaOn turns quota accounting and enforcement on |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 92 | func QuotaOn(fd *os.File, qtype QuotaType, quotaFormat QuotaFormat, quotaFilePath string) error { |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 93 | pathArg, err := unix.BytePtrFromString(quotaFilePath) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| Tim Windelschmidt | 06c1964 | 2024-04-23 15:07:40 +0200 | [diff] [blame] | 97 | _, _, errNo := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_QUOTAON|uint(qtype)), uintptr(quotaFormat), uintptr(unsafe.Pointer(pathArg)), 0, 0) |
| 98 | if errNo != unix.Errno(0) { |
| 99 | return errNo |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | // QuotaOff turns quotas off |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 105 | func QuotaOff(fd *os.File, qtype QuotaType) error { |
| 106 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_QUOTAOFF|uint(qtype)), 0, 0, 0, 0) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 107 | if err != unix.Errno(0) { |
| 108 | return err |
| 109 | } |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | // GetFmt gets the quota format used on given filesystem |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 114 | func GetFmt(fd *os.File, qtype QuotaType) (QuotaFormat, error) { |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 115 | var fmt uint32 |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 116 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_GETFMT|uint(qtype)), 0, uintptr(unsafe.Pointer(&fmt)), 0, 0) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 117 | if err != unix.Errno(0) { |
| 118 | return 0, err |
| 119 | } |
| Lorenz Brun | 5999e92 | 2021-01-27 18:53:54 +0100 | [diff] [blame] | 120 | return QuotaFormat(fmt), nil |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // GetInfo gets information about quota files |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 124 | func GetInfo(fd *os.File, qtype QuotaType) (*DQInfo, error) { |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 125 | var info DQInfo |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 126 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_GETINFO|uint(qtype)), 0, uintptr(unsafe.Pointer(&info)), 0, 0) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 127 | if err != unix.Errno(0) { |
| 128 | return nil, err |
| 129 | } |
| 130 | return &info, nil |
| 131 | } |
| 132 | |
| 133 | // SetInfo sets information about quota files |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 134 | func SetInfo(fd *os.File, qtype QuotaType, info *DQInfo) error { |
| 135 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_SETINFO|uint(qtype)), 0, uintptr(unsafe.Pointer(info)), 0, 0) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 136 | if err != unix.Errno(0) { |
| 137 | return err |
| 138 | } |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | // GetQuota gets user quota structure |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 143 | func GetQuota(fd *os.File, qtype QuotaType, id uint32) (*Quota, error) { |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 144 | var info Quota |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 145 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_GETQUOTA|uint(qtype)), uintptr(id), uintptr(unsafe.Pointer(&info)), 0, 0) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 146 | if err != unix.Errno(0) { |
| 147 | return nil, err |
| 148 | } |
| 149 | return &info, nil |
| 150 | } |
| 151 | |
| 152 | // GetNextQuota gets disk limits and usage > ID |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 153 | func GetNextQuota(fd *os.File, qtype QuotaType, id uint32) (*NextDQBlk, error) { |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 154 | var info NextDQBlk |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 155 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_GETNEXTQUOTA|uint(qtype)), uintptr(id), uintptr(unsafe.Pointer(&info)), 0, 0) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 156 | if err != unix.Errno(0) { |
| 157 | return nil, err |
| 158 | } |
| 159 | return &info, nil |
| 160 | } |
| 161 | |
| 162 | // SetQuota sets the given quota |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 163 | func SetQuota(fd *os.File, qtype QuotaType, id uint32, quota *Quota) error { |
| 164 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_SETQUOTA|uint(qtype)), uintptr(id), uintptr(unsafe.Pointer(quota)), 0, 0) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 165 | if err != unix.Errno(0) { |
| 166 | return fmt.Errorf("failed to set quota: %w", err) |
| 167 | } |
| 168 | return nil |
| 169 | } |
| 170 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 171 | // Sync syncs disk copy of filesystems quotas. If device is empty it syncs all |
| 172 | // filesystems. |
| Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame] | 173 | func Sync(fd *os.File) error { |
| 174 | if fd != nil { |
| 175 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL_FD, fd.Fd(), uintptr(Q_SYNC), 0, 0, 0, 0) |
| Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 176 | if err != unix.Errno(0) { |
| 177 | return err |
| 178 | } |
| 179 | } else { |
| 180 | _, _, err := unix.Syscall6(unix.SYS_QUOTACTL, uintptr(Q_SYNC), 0, 0, 0, 0, 0) |
| 181 | if err != unix.Errno(0) { |
| 182 | return err |
| 183 | } |
| 184 | } |
| 185 | return nil |
| 186 | } |