2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux && !exclude_disk_quota && cgo
|
2016-09-18 10:44:36 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// projectquota.go - implements XFS project quota controls
|
|
|
|
// for setting quota limits on a newly created directory.
|
|
|
|
// It currently supports the legacy XFS specific ioctls.
|
|
|
|
//
|
|
|
|
// TODO: use generic quota control ioctl FS_IOC_FS{GET,SET}XATTR
|
|
|
|
// for both xfs/ext4 for kernel version >= v4.5
|
|
|
|
//
|
|
|
|
|
2020-08-09 20:04:43 +00:00
|
|
|
package quota // import "github.com/docker/docker/quota"
|
2016-09-18 10:44:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/quota.h>
|
|
|
|
#include <linux/dqblk_xfs.h>
|
2016-10-17 18:10:46 +00:00
|
|
|
|
|
|
|
#ifndef FS_XFLAG_PROJINHERIT
|
2016-09-18 10:44:36 +00:00
|
|
|
struct fsxattr {
|
|
|
|
__u32 fsx_xflags;
|
|
|
|
__u32 fsx_extsize;
|
|
|
|
__u32 fsx_nextents;
|
|
|
|
__u32 fsx_projid;
|
|
|
|
unsigned char fsx_pad[12];
|
|
|
|
};
|
|
|
|
#define FS_XFLAG_PROJINHERIT 0x00000200
|
2016-10-17 18:10:46 +00:00
|
|
|
#endif
|
|
|
|
#ifndef FS_IOC_FSGETXATTR
|
2016-09-18 10:44:36 +00:00
|
|
|
#define FS_IOC_FSGETXATTR _IOR ('X', 31, struct fsxattr)
|
2016-10-17 18:10:46 +00:00
|
|
|
#endif
|
|
|
|
#ifndef FS_IOC_FSSETXATTR
|
2016-09-18 10:44:36 +00:00
|
|
|
#define FS_IOC_FSSETXATTR _IOW ('X', 32, struct fsxattr)
|
2016-10-17 18:10:46 +00:00
|
|
|
#endif
|
2016-09-18 10:44:36 +00:00
|
|
|
|
2016-10-17 18:10:46 +00:00
|
|
|
#ifndef PRJQUOTA
|
2016-09-18 10:44:36 +00:00
|
|
|
#define PRJQUOTA 2
|
2016-10-17 18:10:46 +00:00
|
|
|
#endif
|
|
|
|
#ifndef XFS_PROJ_QUOTA
|
2016-09-18 10:44:36 +00:00
|
|
|
#define XFS_PROJ_QUOTA 2
|
2016-10-17 18:10:46 +00:00
|
|
|
#endif
|
|
|
|
#ifndef Q_XSETPQLIM
|
2016-09-18 10:44:36 +00:00
|
|
|
#define Q_XSETPQLIM QCMD(Q_XSETQLIM, PRJQUOTA)
|
2016-10-17 18:10:46 +00:00
|
|
|
#endif
|
|
|
|
#ifndef Q_XGETPQUOTA
|
2016-09-18 10:44:36 +00:00
|
|
|
#define Q_XGETPQUOTA QCMD(Q_XGETQUOTA, PRJQUOTA)
|
2016-10-17 18:10:46 +00:00
|
|
|
#endif
|
2017-09-03 04:25:36 +00:00
|
|
|
|
|
|
|
const int Q_XGETQSTAT_PRJQUOTA = QCMD(Q_XGETQSTAT, PRJQUOTA);
|
2016-09-18 10:44:36 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
2022-01-20 13:13:30 +00:00
|
|
|
|
2016-09-18 10:44:36 +00:00
|
|
|
import (
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
2021-08-24 10:10:50 +00:00
|
|
|
"os"
|
2016-09-18 10:44:36 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-08-09 19:52:20 +00:00
|
|
|
"sync"
|
2016-09-18 10:44:36 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
"github.com/containerd/containerd/log"
|
2021-06-18 09:01:24 +00:00
|
|
|
"github.com/containerd/containerd/pkg/userns"
|
2019-03-13 18:22:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-05-23 14:22:32 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-09-18 10:44:36 +00:00
|
|
|
)
|
|
|
|
|
2020-08-09 19:52:20 +00:00
|
|
|
type pquotaState struct {
|
|
|
|
sync.Mutex
|
|
|
|
nextProjectID uint32
|
|
|
|
}
|
|
|
|
|
2022-01-20 13:13:30 +00:00
|
|
|
var (
|
|
|
|
pquotaStateInst *pquotaState
|
|
|
|
pquotaStateOnce sync.Once
|
|
|
|
)
|
2020-08-09 19:52:20 +00:00
|
|
|
|
|
|
|
// getPquotaState - get global pquota state tracker instance
|
|
|
|
func getPquotaState() *pquotaState {
|
|
|
|
pquotaStateOnce.Do(func() {
|
|
|
|
pquotaStateInst = &pquotaState{
|
|
|
|
nextProjectID: 1,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return pquotaStateInst
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerBasePath - register a new base path and update nextProjectID
|
|
|
|
func (state *pquotaState) updateMinProjID(minProjectID uint32) {
|
|
|
|
state.Lock()
|
|
|
|
defer state.Unlock()
|
|
|
|
if state.nextProjectID <= minProjectID {
|
|
|
|
state.nextProjectID = minProjectID + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:28:14 +00:00
|
|
|
// NewControl - initialize project quota support.
|
2016-09-18 10:44:36 +00:00
|
|
|
// Test to make sure that quota can be set on a test dir and find
|
|
|
|
// the first project id to be used for the next container create.
|
|
|
|
//
|
|
|
|
// Returns nil (and error) if project quota is not supported.
|
|
|
|
//
|
|
|
|
// First get the project id of the home directory.
|
|
|
|
// This test will fail if the backing fs is not xfs.
|
|
|
|
//
|
|
|
|
// xfs_quota tool can be used to assign a project id to the driver home directory, e.g.:
|
2022-07-08 16:27:07 +00:00
|
|
|
//
|
|
|
|
// echo 999:/var/lib/docker/overlay2 >> /etc/projects
|
|
|
|
// echo docker:999 >> /etc/projid
|
|
|
|
// xfs_quota -x -c 'project -s docker' /<xfs mount point>
|
2016-09-18 10:44:36 +00:00
|
|
|
//
|
|
|
|
// In that case, the home directory project id will be used as a "start offset"
|
|
|
|
// and all containers will be assigned larger project ids (e.g. >= 1000).
|
|
|
|
// This is a way to prevent xfs_quota management from conflicting with docker.
|
|
|
|
//
|
|
|
|
// Then try to create a test directory with the next project id and set a quota
|
|
|
|
// on it. If that works, continue to scan existing containers to map allocated
|
|
|
|
// project ids.
|
2016-10-18 00:28:14 +00:00
|
|
|
func NewControl(basePath string) (*Control, error) {
|
2017-11-16 11:54:31 +00:00
|
|
|
//
|
|
|
|
// If we are running in a user namespace quota won't be supported for
|
|
|
|
// now since makeBackingFsDev() will try to mknod().
|
|
|
|
//
|
2021-06-18 09:01:24 +00:00
|
|
|
if userns.RunningInUserNS() {
|
2017-11-16 11:54:31 +00:00
|
|
|
return nil, ErrQuotaNotSupported
|
|
|
|
}
|
|
|
|
|
2016-09-18 10:44:36 +00:00
|
|
|
//
|
2017-09-03 04:25:36 +00:00
|
|
|
// create backing filesystem device node
|
2016-09-18 10:44:36 +00:00
|
|
|
//
|
2017-09-03 04:25:36 +00:00
|
|
|
backingFsBlockDev, err := makeBackingFsDev(basePath)
|
2016-09-18 10:44:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-03 04:25:36 +00:00
|
|
|
|
|
|
|
// check if we can call quotactl with project quotas
|
|
|
|
// as a mechanism to determine (early) if we have support
|
|
|
|
hasQuotaSupport, err := hasQuotaSupport(backingFsBlockDev)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasQuotaSupport {
|
|
|
|
return nil, ErrQuotaNotSupported
|
|
|
|
}
|
2016-09-18 10:44:36 +00:00
|
|
|
|
|
|
|
//
|
2017-09-03 04:25:36 +00:00
|
|
|
// Get project id of parent dir as minimal id to be used by driver
|
2016-09-18 10:44:36 +00:00
|
|
|
//
|
2020-08-09 19:52:20 +00:00
|
|
|
baseProjectID, err := getProjectID(basePath)
|
2016-09-18 10:44:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-09 19:52:20 +00:00
|
|
|
minProjectID := baseProjectID + 1
|
2016-09-18 10:44:36 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Test if filesystem supports project quotas by trying to set
|
|
|
|
// a quota on the first available project id
|
|
|
|
//
|
|
|
|
quota := Quota{
|
|
|
|
Size: 0,
|
|
|
|
}
|
|
|
|
if err := setProjectQuota(backingFsBlockDev, minProjectID, quota); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:28:14 +00:00
|
|
|
q := Control{
|
2016-09-18 10:44:36 +00:00
|
|
|
backingFsBlockDev: backingFsBlockDev,
|
|
|
|
quotas: make(map[string]uint32),
|
|
|
|
}
|
|
|
|
|
2020-08-09 19:52:20 +00:00
|
|
|
//
|
|
|
|
// update minimum project ID
|
|
|
|
//
|
|
|
|
state := getPquotaState()
|
|
|
|
state.updateMinProjID(minProjectID)
|
|
|
|
|
2016-09-18 10:44:36 +00:00
|
|
|
//
|
|
|
|
// get first project id to be used for next container
|
|
|
|
//
|
2020-08-09 19:52:20 +00:00
|
|
|
err = q.findNextProjectID(basePath, baseProjectID)
|
2016-09-18 10:44:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("NewControl(%s): nextProjectID = %d", basePath, state.nextProjectID)
|
2016-09-18 10:44:36 +00:00
|
|
|
return &q, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetQuota - assign a unique project id to directory and set the quota limits
|
|
|
|
// for that project id
|
2016-10-18 00:28:14 +00:00
|
|
|
func (q *Control) SetQuota(targetPath string, quota Quota) error {
|
2019-07-31 18:02:42 +00:00
|
|
|
q.RLock()
|
2016-09-18 10:44:36 +00:00
|
|
|
projectID, ok := q.quotas[targetPath]
|
2019-07-31 18:02:42 +00:00
|
|
|
q.RUnlock()
|
2016-09-18 10:44:36 +00:00
|
|
|
if !ok {
|
2020-08-09 19:52:20 +00:00
|
|
|
state := getPquotaState()
|
|
|
|
state.Lock()
|
|
|
|
projectID = state.nextProjectID
|
2016-09-18 10:44:36 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// assign project id to new container directory
|
|
|
|
//
|
|
|
|
err := setProjectID(targetPath, projectID)
|
|
|
|
if err != nil {
|
2020-08-09 19:52:20 +00:00
|
|
|
state.Unlock()
|
2016-09-18 10:44:36 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-09 19:52:20 +00:00
|
|
|
|
|
|
|
state.nextProjectID++
|
|
|
|
state.Unlock()
|
|
|
|
|
|
|
|
q.Lock()
|
2016-09-18 10:44:36 +00:00
|
|
|
q.quotas[targetPath] = projectID
|
2019-07-31 18:02:42 +00:00
|
|
|
q.Unlock()
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// set the quota limit for the container's project id
|
|
|
|
//
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("SetQuota(%s, %d): projectID=%d", targetPath, quota.Size, projectID)
|
2016-09-18 10:44:36 +00:00
|
|
|
return setProjectQuota(q.backingFsBlockDev, projectID, quota)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setProjectQuota - set the quota for project id on xfs block device
|
|
|
|
func setProjectQuota(backingFsBlockDev string, projectID uint32, quota Quota) error {
|
|
|
|
var d C.fs_disk_quota_t
|
|
|
|
d.d_version = C.FS_DQUOT_VERSION
|
|
|
|
d.d_id = C.__u32(projectID)
|
|
|
|
d.d_flags = C.XFS_PROJ_QUOTA
|
|
|
|
|
|
|
|
d.d_fieldmask = C.FS_DQ_BHARD | C.FS_DQ_BSOFT
|
|
|
|
d.d_blk_hardlimit = C.__u64(quota.Size / 512)
|
|
|
|
d.d_blk_softlimit = d.d_blk_hardlimit
|
|
|
|
|
2022-01-20 13:13:30 +00:00
|
|
|
cs := C.CString(backingFsBlockDev)
|
2016-09-18 10:44:36 +00:00
|
|
|
defer C.free(unsafe.Pointer(cs))
|
|
|
|
|
2017-05-23 14:22:32 +00:00
|
|
|
_, _, errno := unix.Syscall6(unix.SYS_QUOTACTL, C.Q_XSETPQLIM,
|
2016-09-18 10:44:36 +00:00
|
|
|
uintptr(unsafe.Pointer(cs)), uintptr(d.d_id),
|
|
|
|
uintptr(unsafe.Pointer(&d)), 0, 0)
|
|
|
|
if errno != 0 {
|
2019-03-13 18:22:13 +00:00
|
|
|
return errors.Wrapf(errno, "failed to set quota limit for projid %d on %s",
|
|
|
|
projectID, backingFsBlockDev)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetQuota - get the quota limits of a directory that was configured with SetQuota
|
2016-10-18 00:28:14 +00:00
|
|
|
func (q *Control) GetQuota(targetPath string, quota *Quota) error {
|
2019-07-31 18:02:42 +00:00
|
|
|
q.RLock()
|
2016-09-18 10:44:36 +00:00
|
|
|
projectID, ok := q.quotas[targetPath]
|
2019-07-31 18:02:42 +00:00
|
|
|
q.RUnlock()
|
2016-09-18 10:44:36 +00:00
|
|
|
if !ok {
|
2019-03-13 22:39:38 +00:00
|
|
|
return errors.Errorf("quota not found for path: %s", targetPath)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// get the quota limit for the container's project id
|
|
|
|
//
|
|
|
|
var d C.fs_disk_quota_t
|
|
|
|
|
2022-01-20 13:13:30 +00:00
|
|
|
cs := C.CString(q.backingFsBlockDev)
|
2016-09-18 10:44:36 +00:00
|
|
|
defer C.free(unsafe.Pointer(cs))
|
|
|
|
|
2017-05-23 14:22:32 +00:00
|
|
|
_, _, errno := unix.Syscall6(unix.SYS_QUOTACTL, C.Q_XGETPQUOTA,
|
2016-09-18 10:44:36 +00:00
|
|
|
uintptr(unsafe.Pointer(cs)), uintptr(C.__u32(projectID)),
|
|
|
|
uintptr(unsafe.Pointer(&d)), 0, 0)
|
|
|
|
if errno != 0 {
|
2019-03-13 18:22:13 +00:00
|
|
|
return errors.Wrapf(errno, "Failed to get quota limit for projid %d on %s",
|
|
|
|
projectID, q.backingFsBlockDev)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
quota.Size = uint64(d.d_blk_hardlimit) * 512
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getProjectID - get the project id of path on xfs
|
|
|
|
func getProjectID(targetPath string) (uint32, error) {
|
|
|
|
dir, err := openDir(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var fsx C.struct_fsxattr
|
2017-05-23 14:22:32 +00:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.FS_IOC_FSGETXATTR,
|
2016-09-18 10:44:36 +00:00
|
|
|
uintptr(unsafe.Pointer(&fsx)))
|
|
|
|
if errno != 0 {
|
2019-03-13 18:22:13 +00:00
|
|
|
return 0, errors.Wrapf(errno, "failed to get projid for %s", targetPath)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return uint32(fsx.fsx_projid), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setProjectID - set the project id of path on xfs
|
|
|
|
func setProjectID(targetPath string, projectID uint32) error {
|
|
|
|
dir, err := openDir(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var fsx C.struct_fsxattr
|
2017-05-23 14:22:32 +00:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.FS_IOC_FSGETXATTR,
|
2016-09-18 10:44:36 +00:00
|
|
|
uintptr(unsafe.Pointer(&fsx)))
|
|
|
|
if errno != 0 {
|
2019-03-13 18:22:13 +00:00
|
|
|
return errors.Wrapf(errno, "failed to get projid for %s", targetPath)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
fsx.fsx_projid = C.__u32(projectID)
|
|
|
|
fsx.fsx_xflags |= C.FS_XFLAG_PROJINHERIT
|
2017-05-23 14:22:32 +00:00
|
|
|
_, _, errno = unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.FS_IOC_FSSETXATTR,
|
2016-09-18 10:44:36 +00:00
|
|
|
uintptr(unsafe.Pointer(&fsx)))
|
|
|
|
if errno != 0 {
|
2019-03-13 18:22:13 +00:00
|
|
|
return errors.Wrapf(errno, "failed to set projid for %s", targetPath)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// findNextProjectID - find the next project id to be used for containers
|
|
|
|
// by scanning driver home directory to find used project ids
|
2020-08-09 19:52:20 +00:00
|
|
|
func (q *Control) findNextProjectID(home string, baseID uint32) error {
|
|
|
|
state := getPquotaState()
|
|
|
|
state.Lock()
|
|
|
|
defer state.Unlock()
|
|
|
|
|
|
|
|
checkProjID := func(path string) (uint32, error) {
|
|
|
|
projid, err := getProjectID(path)
|
|
|
|
if err != nil {
|
|
|
|
return projid, err
|
|
|
|
}
|
|
|
|
if projid > 0 {
|
|
|
|
q.quotas[path] = projid
|
|
|
|
}
|
|
|
|
if state.nextProjectID <= projid {
|
|
|
|
state.nextProjectID = projid + 1
|
|
|
|
}
|
|
|
|
return projid, nil
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
files, err := os.ReadDir(home)
|
2016-09-18 10:44:36 +00:00
|
|
|
if err != nil {
|
2019-03-13 22:39:38 +00:00
|
|
|
return errors.Errorf("read directory failed: %s", home)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path := filepath.Join(home, file.Name())
|
2020-08-09 19:52:20 +00:00
|
|
|
projid, err := checkProjID(path)
|
2016-09-18 10:44:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-09 19:52:20 +00:00
|
|
|
if projid > 0 && projid != baseID {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-24 10:10:50 +00:00
|
|
|
subfiles, err := os.ReadDir(path)
|
2020-08-09 19:52:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("read directory failed: %s", path)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
2020-08-09 19:52:20 +00:00
|
|
|
for _, subfile := range subfiles {
|
|
|
|
if !subfile.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
subpath := filepath.Join(path, subfile.Name())
|
|
|
|
_, err := checkProjID(subpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func free(p *C.char) {
|
|
|
|
C.free(unsafe.Pointer(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
func openDir(path string) (*C.DIR, error) {
|
|
|
|
Cpath := C.CString(path)
|
|
|
|
defer free(Cpath)
|
|
|
|
|
|
|
|
dir := C.opendir(Cpath)
|
|
|
|
if dir == nil {
|
2019-03-13 22:39:38 +00:00
|
|
|
return nil, errors.Errorf("failed to open dir: %s", path)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeDir(dir *C.DIR) {
|
|
|
|
if dir != nil {
|
|
|
|
C.closedir(dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDirFd(dir *C.DIR) uintptr {
|
|
|
|
return uintptr(C.dirfd(dir))
|
|
|
|
}
|
|
|
|
|
2021-03-19 15:31:46 +00:00
|
|
|
// makeBackingFsDev gets the backing block device of the driver home directory
|
|
|
|
// and creates a block device node under the home directory to be used by
|
|
|
|
// quotactl commands.
|
2016-09-18 10:44:36 +00:00
|
|
|
func makeBackingFsDev(home string) (string, error) {
|
2017-07-27 07:51:23 +00:00
|
|
|
var stat unix.Stat_t
|
|
|
|
if err := unix.Stat(home, &stat); err != nil {
|
2016-09-18 10:44:36 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
backingFsBlockDev := path.Join(home, "backingFsBlockDev")
|
2017-05-21 23:24:07 +00:00
|
|
|
// Re-create just in case someone copied the home directory over to a new device
|
2017-05-23 14:22:32 +00:00
|
|
|
unix.Unlink(backingFsBlockDev)
|
2022-01-20 13:13:30 +00:00
|
|
|
err := unix.Mknod(backingFsBlockDev, unix.S_IFBLK|0o600, int(stat.Dev))
|
2017-12-18 18:23:49 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return backingFsBlockDev, nil
|
|
|
|
|
2019-03-13 18:22:13 +00:00
|
|
|
case unix.ENOSYS, unix.EPERM:
|
2017-12-18 18:23:49 +00:00
|
|
|
return "", ErrQuotaNotSupported
|
|
|
|
|
|
|
|
default:
|
2019-03-13 18:22:13 +00:00
|
|
|
return "", errors.Wrapf(err, "failed to mknod %s", backingFsBlockDev)
|
2016-09-18 10:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 04:25:36 +00:00
|
|
|
|
|
|
|
func hasQuotaSupport(backingFsBlockDev string) (bool, error) {
|
2022-01-20 13:13:30 +00:00
|
|
|
cs := C.CString(backingFsBlockDev)
|
2017-09-03 04:25:36 +00:00
|
|
|
defer free(cs)
|
|
|
|
var qstat C.fs_quota_stat_t
|
|
|
|
|
|
|
|
_, _, errno := unix.Syscall6(unix.SYS_QUOTACTL, uintptr(C.Q_XGETQSTAT_PRJQUOTA), uintptr(unsafe.Pointer(cs)), 0, uintptr(unsafe.Pointer(&qstat)), 0, 0)
|
|
|
|
if errno == 0 && qstat.qs_flags&C.FS_QUOTA_PDQ_ENFD > 0 && qstat.qs_flags&C.FS_QUOTA_PDQ_ACCT > 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch errno {
|
|
|
|
// These are the known fatal errors, consider all other errors (ENOTTY, etc.. not supporting quota)
|
|
|
|
case unix.EFAULT, unix.ENOENT, unix.ENOTBLK, unix.EPERM:
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errno
|
|
|
|
}
|