blob: 0d49592514bb08c4a50e1fce321b59d6cc65e2e9 [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 (
25 "fmt"
26 "math"
27 "os"
28
Lorenz Brun1d801752020-04-02 09:24:51 +020029 "golang.org/x/sys/unix"
Serge Bazanski77cb6c52020-12-19 00:09:22 +010030
Serge Bazanski31370b02021-01-07 16:31:14 +010031 "source.monogon.dev/metropolis/pkg/fsquota/fsxattrs"
32 "source.monogon.dev/metropolis/pkg/fsquota/quotactl"
Lorenz Brun1d801752020-04-02 09:24:51 +020033)
34
Serge Bazanski216fe7b2021-05-21 18:36:16 +020035// 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 Brun1d801752020-04-02 09:24:51 +020040func 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 Brun1d801752020-04-02 09:24:51 +020046 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 Bazanski216fe7b2021-05-21 18:36:16 +020061 // 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 Brun1d801752020-04-02 09:24:51 +020067 for {
Lorenz Brun531e2c22021-11-17 20:00:05 +010068 quota, err := quotactl.GetNextQuota(dir, quotactl.QuotaTypeProject, lastID)
Lorenz Brun1d801752020-04-02 09:24:51 +020069 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 Brun531e2c22021-11-17 20:00:05 +0100101 return quotactl.SetQuota(dir, quotactl.QuotaTypeProject, lastID, &quotactl.Quota{
Lorenz Brun1d801752020-04-02 09:24:51 +0200102 BHardLimit: bytesLimitBlocks,
103 BSoftLimit: bytesLimitBlocks,
104 IHardLimit: maxInodes,
105 ISoftLimit: maxInodes,
106 Valid: valid,
107 })
108}
109
110type Quota struct {
111 Bytes uint64
112 BytesUsed uint64
113 Inodes uint64
114 InodesUsed uint64
115}
116
Serge Bazanski216fe7b2021-05-21 18:36:16 +0200117// GetQuota returns the current active quota and its utilization at the given
118// path
Lorenz Brun1d801752020-04-02 09:24:51 +0200119func 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 Brun1d801752020-04-02 09:24:51 +0200125 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 Brun531e2c22021-11-17 20:00:05 +0100132 quota, err := quotactl.GetQuota(dir, quotactl.QuotaTypeProject, attrs.ProjectID)
Lorenz Brun1d801752020-04-02 09:24:51 +0200133 if err != nil {
134 return nil, err
135 }
136 return &Quota{
Lorenz Brun547b33f2020-04-23 15:27:06 +0200137 Bytes: quota.BHardLimit * 1024,
Lorenz Brun1d801752020-04-02 09:24:51 +0200138 BytesUsed: quota.CurSpace,
139 Inodes: quota.IHardLimit,
140 InodesUsed: quota.CurInodes,
141 }, nil
142}