blob: af87d9fcfdfc0c9b1bf17545a46a955d76afc5d6 [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
Serge Bazanski216fe7b2021-05-21 18:36:16 +020017// 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 Brun1d801752020-04-02 09:24:51 +020022package fsquota
23
24import (
Tim Windelschmidtd5f851b2024-04-23 14:59:37 +020025 "errors"
Lorenz Brun1d801752020-04-02 09:24:51 +020026 "fmt"
27 "math"
28 "os"
29
Lorenz Brun1d801752020-04-02 09:24:51 +020030 "golang.org/x/sys/unix"
Serge Bazanski77cb6c52020-12-19 00:09:22 +010031
Serge Bazanski31370b02021-01-07 16:31:14 +010032 "source.monogon.dev/metropolis/pkg/fsquota/fsxattrs"
33 "source.monogon.dev/metropolis/pkg/fsquota/quotactl"
Lorenz Brun1d801752020-04-02 09:24:51 +020034)
35
Serge Bazanski216fe7b2021-05-21 18:36:16 +020036// SetQuota sets the quota of bytes and/or inodes in a given path. To not set a
37// limit, set the corresponding argument to zero. Setting both arguments to
38// zero removes the quota entirely. This function can only be called on an
39// empty directory. It can't be used to create a quota below a directory which
40// already has a quota since Linux doesn't offer hierarchical quotas.
Lorenz Brun1d801752020-04-02 09:24:51 +020041func SetQuota(path string, maxBytes uint64, maxInodes uint64) error {
42 dir, err := os.Open(path)
43 if err != nil {
44 return err
45 }
46 defer dir.Close()
Lorenz Brun1d801752020-04-02 09:24:51 +020047 var valid uint32
48 if maxBytes > 0 {
49 valid |= quotactl.FlagBLimitsValid
50 }
51 if maxInodes > 0 {
52 valid |= quotactl.FlagILimitsValid
53 }
54
55 attrs, err := fsxattrs.Get(dir)
56 if err != nil {
57 return err
58 }
59
Tim Windelschmidt5e460a92024-04-11 01:33:09 +020060 var lastID = attrs.ProjectID
Lorenz Brun1d801752020-04-02 09:24:51 +020061 if lastID == 0 {
Serge Bazanski216fe7b2021-05-21 18:36:16 +020062 // No project/quota exists for this directory, assign a new project
63 // quota.
64 // TODO(lorenz): This is racy, but the kernel does not support
65 // atomically assigning quotas. So this needs to be added to the
66 // kernels setquota interface. Due to the short time window and
67 // infrequent calls this should not be an immediate issue.
Lorenz Brun1d801752020-04-02 09:24:51 +020068 for {
Lorenz Brun531e2c22021-11-17 20:00:05 +010069 quota, err := quotactl.GetNextQuota(dir, quotactl.QuotaTypeProject, lastID)
Tim Windelschmidtd5f851b2024-04-23 14:59:37 +020070 if errors.Is(err, unix.ENOENT) || errors.Is(err, unix.ESRCH) {
Lorenz Brun1d801752020-04-02 09:24:51 +020071 // We have enumerated all quotas, nothing exists here
72 break
73 } else if err != nil {
74 return fmt.Errorf("failed to call GetNextQuota: %w", err)
75 }
76 if quota.ID > lastID+1 {
77 // Take the first ID in the quota ID gap
78 lastID++
79 break
80 }
81 lastID++
82 }
83 }
84
85 // If both limits are zero, this is a delete operation, process it as such
86 if maxBytes == 0 && maxInodes == 0 {
87 valid = quotactl.FlagBLimitsValid | quotactl.FlagILimitsValid
88 attrs.ProjectID = 0
89 attrs.Flags &= ^fsxattrs.FlagProjectInherit
90 } else {
91 attrs.ProjectID = lastID
92 attrs.Flags |= fsxattrs.FlagProjectInherit
93 }
94
95 if err := fsxattrs.Set(dir, attrs); err != nil {
96 return err
97 }
98
99 // Always round up to the nearest block size
100 bytesLimitBlocks := uint64(math.Ceil(float64(maxBytes) / float64(1024)))
101
Lorenz Brun531e2c22021-11-17 20:00:05 +0100102 return quotactl.SetQuota(dir, quotactl.QuotaTypeProject, lastID, &quotactl.Quota{
Lorenz Brun1d801752020-04-02 09:24:51 +0200103 BHardLimit: bytesLimitBlocks,
104 BSoftLimit: bytesLimitBlocks,
105 IHardLimit: maxInodes,
106 ISoftLimit: maxInodes,
107 Valid: valid,
108 })
109}
110
111type Quota struct {
112 Bytes uint64
113 BytesUsed uint64
114 Inodes uint64
115 InodesUsed uint64
116}
117
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200118// GetQuota returns the current active quota and its utilization at the given
119// path
Lorenz Brun1d801752020-04-02 09:24:51 +0200120func GetQuota(path string) (*Quota, error) {
121 dir, err := os.Open(path)
122 if err != nil {
123 return nil, err
124 }
125 defer dir.Close()
Lorenz Brun1d801752020-04-02 09:24:51 +0200126 attrs, err := fsxattrs.Get(dir)
127 if err != nil {
128 return nil, err
129 }
130 if attrs.ProjectID == 0 {
131 return nil, os.ErrNotExist
132 }
Lorenz Brun531e2c22021-11-17 20:00:05 +0100133 quota, err := quotactl.GetQuota(dir, quotactl.QuotaTypeProject, attrs.ProjectID)
Lorenz Brun1d801752020-04-02 09:24:51 +0200134 if err != nil {
135 return nil, err
136 }
137 return &Quota{
Lorenz Brun547b33f2020-04-23 15:27:06 +0200138 Bytes: quota.BHardLimit * 1024,
Lorenz Brun1d801752020-04-02 09:24:51 +0200139 BytesUsed: quota.CurSpace,
140 Inodes: quota.IHardLimit,
141 InodesUsed: quota.CurInodes,
142 }, nil
143}