Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 1 | // 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 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 17 | // Package fsquota provides a simplified interface to interact with Linux's |
| 18 | // filesystem qouta API. It only supports setting quotas on directories, not |
| 19 | // groups or users. Quotas need to be already enabled on the filesystem to be |
| 20 | // able to use them using this package. See the quotactl package if you intend |
| 21 | // to use this on a filesystem where quotas need to be enabled manually. |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 22 | package fsquota |
| 23 | |
| 24 | import ( |
| 25 | "fmt" |
| 26 | "math" |
| 27 | "os" |
| 28 | |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 29 | "golang.org/x/sys/unix" |
Serge Bazanski | 77cb6c5 | 2020-12-19 00:09:22 +0100 | [diff] [blame] | 30 | |
Serge Bazanski | 31370b0 | 2021-01-07 16:31:14 +0100 | [diff] [blame] | 31 | "source.monogon.dev/metropolis/pkg/fsquota/fsxattrs" |
| 32 | "source.monogon.dev/metropolis/pkg/fsquota/quotactl" |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 33 | ) |
| 34 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 35 | // SetQuota sets the quota of bytes and/or inodes in a given path. To not set a |
| 36 | // limit, set the corresponding argument to zero. Setting both arguments to |
| 37 | // zero removes the quota entirely. This function can only be called on an |
| 38 | // empty directory. It can't be used to create a quota below a directory which |
| 39 | // already has a quota since Linux doesn't offer hierarchical quotas. |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 40 | func SetQuota(path string, maxBytes uint64, maxInodes uint64) error { |
| 41 | dir, err := os.Open(path) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | defer dir.Close() |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 46 | var valid uint32 |
| 47 | if maxBytes > 0 { |
| 48 | valid |= quotactl.FlagBLimitsValid |
| 49 | } |
| 50 | if maxInodes > 0 { |
| 51 | valid |= quotactl.FlagILimitsValid |
| 52 | } |
| 53 | |
| 54 | attrs, err := fsxattrs.Get(dir) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | var lastID uint32 = attrs.ProjectID |
| 60 | if lastID == 0 { |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 61 | // No project/quota exists for this directory, assign a new project |
| 62 | // quota. |
| 63 | // TODO(lorenz): This is racy, but the kernel does not support |
| 64 | // atomically assigning quotas. So this needs to be added to the |
| 65 | // kernels setquota interface. Due to the short time window and |
| 66 | // infrequent calls this should not be an immediate issue. |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 67 | for { |
Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame^] | 68 | quota, err := quotactl.GetNextQuota(dir, quotactl.QuotaTypeProject, lastID) |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 69 | if err == unix.ENOENT || err == unix.ESRCH { |
| 70 | // We have enumerated all quotas, nothing exists here |
| 71 | break |
| 72 | } else if err != nil { |
| 73 | return fmt.Errorf("failed to call GetNextQuota: %w", err) |
| 74 | } |
| 75 | if quota.ID > lastID+1 { |
| 76 | // Take the first ID in the quota ID gap |
| 77 | lastID++ |
| 78 | break |
| 79 | } |
| 80 | lastID++ |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // If both limits are zero, this is a delete operation, process it as such |
| 85 | if maxBytes == 0 && maxInodes == 0 { |
| 86 | valid = quotactl.FlagBLimitsValid | quotactl.FlagILimitsValid |
| 87 | attrs.ProjectID = 0 |
| 88 | attrs.Flags &= ^fsxattrs.FlagProjectInherit |
| 89 | } else { |
| 90 | attrs.ProjectID = lastID |
| 91 | attrs.Flags |= fsxattrs.FlagProjectInherit |
| 92 | } |
| 93 | |
| 94 | if err := fsxattrs.Set(dir, attrs); err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | // Always round up to the nearest block size |
| 99 | bytesLimitBlocks := uint64(math.Ceil(float64(maxBytes) / float64(1024))) |
| 100 | |
Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame^] | 101 | return quotactl.SetQuota(dir, quotactl.QuotaTypeProject, lastID, "actl.Quota{ |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 102 | BHardLimit: bytesLimitBlocks, |
| 103 | BSoftLimit: bytesLimitBlocks, |
| 104 | IHardLimit: maxInodes, |
| 105 | ISoftLimit: maxInodes, |
| 106 | Valid: valid, |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | type Quota struct { |
| 111 | Bytes uint64 |
| 112 | BytesUsed uint64 |
| 113 | Inodes uint64 |
| 114 | InodesUsed uint64 |
| 115 | } |
| 116 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 117 | // GetQuota returns the current active quota and its utilization at the given |
| 118 | // path |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 119 | func GetQuota(path string) (*Quota, error) { |
| 120 | dir, err := os.Open(path) |
| 121 | if err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | defer dir.Close() |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 125 | attrs, err := fsxattrs.Get(dir) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | if attrs.ProjectID == 0 { |
| 130 | return nil, os.ErrNotExist |
| 131 | } |
Lorenz Brun | 531e2c2 | 2021-11-17 20:00:05 +0100 | [diff] [blame^] | 132 | quota, err := quotactl.GetQuota(dir, quotactl.QuotaTypeProject, attrs.ProjectID) |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | return &Quota{ |
Lorenz Brun | 547b33f | 2020-04-23 15:27:06 +0200 | [diff] [blame] | 137 | Bytes: quota.BHardLimit * 1024, |
Lorenz Brun | 1d80175 | 2020-04-02 09:24:51 +0200 | [diff] [blame] | 138 | BytesUsed: quota.CurSpace, |
| 139 | Inodes: quota.IHardLimit, |
| 140 | InodesUsed: quota.CurInodes, |
| 141 | }, nil |
| 142 | } |