projquota

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2026 License: BSD-3-Clause Imports: 8 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-fsctl/projquota

Go Reference CI Coverage

Pure-Go Linux project quotas for XFS and ext4: give a directory tree a project id, limit its space and inodes, and read its usage, straight through the kernel — FS_IOC_FSGETXATTR / FS_IOC_FSSETXATTR and quotactl_fd(2) — with no cgo and no shelling out to xfs_quota, setquota or chattr.

This is the project-quota member of the go-fsctl family, alongside go-fsctl/btrfs (whose qgroups are btrfs's own answer to the same question), go-fsctl/zfs and go-fsctl/loop.

API

import "github.com/go-fsctl/projquota"

// Which filesystem is this? (fstatfs f_type: XFS or ext4, else an error)
fs, err := projquota.Detect("/srv/shares")

// Tag a fresh directory: project 4242, and FS_XFLAG_PROJINHERIT so that
// everything created inside it joins the project.   FS_IOC_FSSETXATTR
err = projquota.SetProject("/srv/shares/alice", 4242, true)
id, inherit, err := projquota.GetProject("/srv/shares/alice")

// Tag a directory that already has content (never follows a symlink, stays
// on one filesystem).
err = projquota.SetProjectTree("/srv/shares/bob", 4243)

// Limit the project (bytes; 0 = no limit).   quotactl_fd Q_XSETQLIM / Q_SETQUOTA
err = projquota.SetLimits("/srv/shares", 4242, projquota.Limits{
    BlockSoft: 9 << 30,
    BlockHard: 10 << 30,
    InodeHard: 1_000_000,
})

// Usage and limits.   quotactl_fd Q_XGETQUOTA / Q_GETQUOTA
q, err := projquota.Usage("/srv/shares", 4242)
// q.Bytes, q.Inodes, q.BlockHard, q.BlockSoft, q.BlockGrace, ...

Each path function has a …File(*os.File, …) twin (GetProjectFile, SetProjectFile, SetLimitsFile, UsageFile, DetectFile). The limits functions take any file or directory on the filesystem, usually the mount point. Off Linux every function returns projquota.ErrUnsupported.

Operation Kernel interface
GetProject, SetProject, SetProjectTree FS_IOC_FSGETXATTR, FS_IOC_FSSETXATTR (read-modify-write, every other flag kept)
SetLimits on XFS quotactl_fd(QCMD(Q_XSETQLIM, PRJQUOTA)), struct fs_disk_quota, 512-byte basic blocks
SetLimits on ext4 quotactl_fd(QCMD(Q_SETQUOTA, PRJQUOTA)), struct if_dqblk, 1 KiB block limits
Usage on XFS / ext4 Q_XGETQUOTA / Q_GETQUOTA
Detect fstatfs f_type: XFS_SUPER_MAGIC, EXT4_SUPER_MAGIC

Requirements

  • Linux ≥ 5.14, for quotactl_fd(2). The older quotactl(2) needs the block device behind the mount; this package does not go looking for it.
  • Project quotas on the mount. XFS: mount -o prjquota. ext4: a filesystem made with mkfs.ext4 -O quota,project -I 256 (the project feature needs inodes larger than 128 bytes) and mounted with -o prjquota; the kernel needs the quota_v2 format module (CONFIG_QFMT_V2), or the mount fails with ESRCH (on Ubuntu it ships in linux-modules-extra).
  • CAP_SYS_ADMIN for SetLimits and for Usage. The kernel lets an unprivileged caller read only its own user and group quotas, never a project's (fs/quota/quota.c, check_quotactl_permission).
  • XFS project ids above 65535 need the projid32bit feature, the mkfs.xfs default for years.
  • /etc/projects and /etc/projid are not needed: they are xfs_quota's name tables and the kernel never reads them.

⛔ On ext4, root is not held to the limit

The generic quota code ext4 uses lets any writer with CAP_SYS_RESOURCE past the hard limits (fs/quota/dquot.c, ignore_hardlimit). XFS has no such exemption. A file server that writes into an ext4 project directory as root — or with CAP_SYS_RESOURCE — is not limited at all: it must write as an unprivileged uid or drop the capability. The first CI run found this by writing 16 MiB as root under an 8 MiB ext4 limit; the integration tests now write as nobody and assert both behaviours.

⛔ Do not give the tenant ownership of its directory

Changing a project id does not need privilege. vfs_fileattr_set (fs/file_attr.c) asks only that the caller own the file (inode_owner_or_capable), and fileattr_set_prepare refuses a project change only to callers outside the initial user namespace. So the owner of a project directory, as an ordinary user on the host, can move it — or any file they own inside it — to project 0, out of the limit.

A provisioner that gives each tenant a project-limited directory must keep the directory, and the files in it, owned by someone other than the tenant while the tenant can run code in the initial user namespace: serve it through a file server that writes on the tenant's behalf under its own uid, or confine the tenant to a user namespace, where the kernel answers EINVAL. The integration tests prove the hazard rather than assert it: an unprivileged owner clears its project id on real XFS and ext4.

Units and rounding

Limits and Quota are in bytes. The kernel ABIs are not: XFS speaks 512-byte basic blocks and the generic interface 1 KiB blocks for limits, so limits are rounded up to those units, as the kernel's own conversions (quota_btobb, stoqb) do — and XFS rounds up again to its filesystem block. Usage reads back what was stored. On ext4 Quota.Bytes is exact; on XFS it is a multiple of 512.

A full project is ENOSPC on XFS, EDQUOT on ext4. XFS reports an exhausted project quota as a full filesystem (fs/xfs/xfs_trans_dquot.c: if (xfs_dquot_type(dqp) == XFS_DQTYPE_PROJ) return -ENOSPC;); a file server should treat both as "share full".

Three things the kernel does that this package makes explicit:

  • XFS silently ignores a hard limit below its soft limit: xfs_setqlim_limits logs hard < soft, keeps the old limits and the call returns success. SetLimits refuses it with ErrSoftAboveHard.
  • Project 0 on XFS is the default for every project (xfs_qm_scall_setqlim: qlim = id == 0 ? &defq->blk : NULL). SetLimits refuses id 0 with ErrInvalidProject.
  • XFS has no record of a project that never used anything and answers ENOENT; Usage reports that as a zero Quota.

statfs(2) — and so df — of a directory carrying FS_XFLAG_PROJINHERIT reports the project's limit as the size of the filesystem: the soft limit when one is set, else the hard limit.

Testing

# Unprivileged: ABI layouts and ioctl numbers, unit conversions, and every
# kernel-call branch through fault-injecting seams. 100% coverage on Linux.
GOWORK=off go test ./...

# Root: real XFS and ext4 in loop-mounted images (needs mkfs.xfs, mkfs.ext4).
GOWORK=off go test -c -o projquota.test . && sudo ./projquota.test -test.v -test.run '^TestIntegration'

CI runs the unit tests natively on amd64 and arm64 with a 100.0% coverage floor, under QEMU on riscv64, loong64, ppc64le and s390x, and the root integration tests on a hosted runner's own kernel.

License

BSD-3-Clause. See LICENSE.

Documentation

Overview

Package projquota drives Linux project quotas on XFS and ext4 directly through the kernel -- FS_IOC_FSGETXATTR / FS_IOC_FSSETXATTR to tag a directory tree with a project id, quotactl_fd(2) to set its limits and read its usage -- with no cgo and without xfs_quota(8), setquota(8) or chattr(1).

A project quota limits a directory tree rather than a user: every inode carries a project id, a directory with FS_XFLAG_PROJINHERIT hands its id to whatever is created inside it, and the filesystem charges the space and inodes of all those files to the project. Once the hard limit is reached, writes fail -- with EDQUOT on ext4, but with ENOSPC on XFS, which reports an exhausted project quota as a full filesystem (fs/xfs/xfs_trans_dquot.c: "if (xfs_dquot_type(dqp) == XFS_DQTYPE_PROJ) return -ENOSPC;"). statfs(2) -- and so df(1) -- of a directory that carries FS_XFLAG_PROJINHERIT reports the project's limit as the size of the filesystem: the SOFT limit when one is set, else the hard limit (fs/xfs/xfs_qm_bhv.c xfs_fill_statvfs_from_dquot, called when project quotas are accounted and enforced; fs/ext4/super.c ext4_statfs_project, "min_not_zero(dqb_bsoftlimit, dqb_bhardlimit)").

Requirements

  • Linux 5.14 or later, for quotactl_fd(2). The older quotactl(2) needs the path of the block device under the mount, which this package does not go looking for.
  • The mount has project quotas enabled. XFS: mount with -o prjquota (or pquota). ext4: a filesystem made with the project feature and inodes larger than 128 bytes (mkfs.ext4 -O quota,project -I 256), mounted with -o prjquota. Without the project feature ext4 refuses any non-zero project id with EOPNOTSUPP (fs/ext4/ioctl.c ext4_ioctl_setproject). The kernel also needs the quota_v2 format (CONFIG_QFMT_V2, module quota_v2): without it the mount itself fails with ESRCH (fs/quota/dquot.c: find_quota_format fails, "return -ESRCH").
  • XFS project ids above 65535 need the projid32bit feature, the default of mkfs.xfs for years (fs/xfs/xfs_ioctl.c xfs_ioctl_setattr_check_projid returns EINVAL otherwise).
  • CAP_SYS_ADMIN for SetLimits AND for Usage: check_quotactl_permission in fs/quota/quota.c lets an unprivileged caller read only its own user and group quotas, never a project's.
  • /etc/projects and /etc/projid are not needed: they are xfs_quota's name tables, and the kernel never reads them.

On ext4, root is not held to the limit

The generic quota code that ext4 uses lets any writer with CAP_SYS_RESOURCE past the hard limits (fs/quota/dquot.c ignore_hardlimit: "return capable_noaudit(CAP_SYS_RESOURCE) && ..."). XFS has no such exemption: root gets ENOSPC like everyone else. A file server that writes into an ext4 project directory as root, or with CAP_SYS_RESOURCE, is not limited at all; it must write as an unprivileged uid, or drop that capability. The integration tests check both behaviours.

The owner of a directory can change its project

Changing a project id with FS_IOC_FSSETXATTR does NOT require privilege: vfs_fileattr_set (fs/file_attr.c) asks only inode_owner_or_capable, and fileattr_set_prepare forbids a change of project id only to callers OUTSIDE the initial user namespace. So the owner of a directory, running as an ordinary user on the host, can move it -- or any file they own in it -- to another project, or to project 0, and out of the limit.

A provisioner that gives each tenant a project-limited directory must therefore NOT give the tenant ownership of the directory or of the files in it while the tenant can run code in the initial user namespace. Serve it through something that does the writing on the tenant's behalf (a file server running under its own uid), or confine the tenant to a user namespace, where the kernel refuses the change with EINVAL.

Units

Limits and usage are in bytes. The kernel ABIs are not: XFS speaks 512-byte basic blocks (struct fs_disk_quota) and the generic interface 1 KiB blocks for limits (struct if_dqblk, QIF_DQBLKSIZE). Limits are rounded UP to those units, exactly as the kernel rounds its own conversions (quota_btobb, stoqb), and XFS rounds up again to its filesystem block. Usage reads back what the filesystem stored.

Off Linux every operation returns ErrUnsupported.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupported is returned by every kernel operation off Linux.
	ErrUnsupported = errors.New("projquota: project quotas are only supported on Linux")

	// ErrUnsupportedFilesystem is returned (wrapped, with the statfs magic)
	// for a filesystem that is neither XFS nor ext4.
	ErrUnsupportedFilesystem = errors.New("projquota: filesystem is neither XFS nor ext4")

	// ErrInvalidProject is returned for project id 4294967295, which the
	// kernel reserves as INVALID_PROJID, and by SetLimits for project id 0:
	// on XFS the limits of id 0 are the DEFAULT limits of every project
	// (fs/xfs/xfs_qm_syscalls.c xfs_qm_scall_setqlim: "qlim = id == 0 ?
	// &defq->blk : NULL"), which is never what a per-directory limit means.
	ErrInvalidProject = errors.New("projquota: invalid project id")

	// ErrSoftAboveHard is returned when a non-zero hard limit is below its
	// soft limit. XFS does not report this case: xfs_setqlim_limits only
	// logs "hard < soft" and leaves the old limits in place while the
	// syscall returns success. The check is made here instead.
	ErrSoftAboveHard = errors.New("projquota: soft limit above hard limit")
)

Functions

func GetProject

func GetProject(path string) (id uint32, inherit bool, err error)

GetProject returns the project id of path and whether FS_XFLAG_PROJINHERIT is set on it (new files and directories created inside then take the directory's project id). It needs no privilege.

func GetProjectFile

func GetProjectFile(f *os.File) (id uint32, inherit bool, err error)

GetProjectFile is GetProject on an open file (not opened with O_PATH).

func SetLimits

func SetLimits(path string, id uint32, l Limits) error

SetLimits sets the limits of project id on the filesystem holding path (any file or directory on it; the mount point is the usual choice). It requires CAP_SYS_ADMIN, Linux >= 5.14 (quotactl_fd), and a mount with project quotas enforced (XFS: -o prjquota; ext4: a filesystem made with -O quota,project and mounted with -o prjquota).

Project id 0 is refused (ErrInvalidProject): on XFS its limits are the default limits of every project.

func SetLimitsFile

func SetLimitsFile(f *os.File, id uint32, l Limits) error

SetLimitsFile is SetLimits through any open file on the filesystem.

func SetProject

func SetProject(path string, id uint32, inherit bool) error

SetProject sets the project id of path, and sets or clears FS_XFLAG_PROJINHERIT on it, keeping every other attribute (read, modify, write). Only the directory itself changes: files already inside keep their project; use SetProjectTree for a populated tree.

inherit only makes sense on a directory; ext4 refuses it on a regular file (EOPNOTSUPP).

The kernel lets the OWNER of the file do this, not only root (vfs_fileattr_set: inode_owner_or_capable), as long as the caller is in the initial user namespace. See the package documentation.

func SetProjectFile

func SetProjectFile(f *os.File, id uint32, inherit bool) error

SetProjectFile is SetProject on an open file (not opened with O_PATH).

func SetProjectTree

func SetProjectTree(root string, id uint32) error

SetProjectTree gives root and everything below it on the same filesystem project id: directories get FS_XFLAG_PROJINHERIT too, regular files get the id only. Symbolic links, devices, FIFOs and sockets are left alone, as are other filesystems mounted inside the tree.

The walk never follows a symbolic link: every entry is opened relative to its parent's fd with O_NOFOLLOW, so a name swapped for a link during the walk is skipped rather than followed out of the tree. An entry that vanishes or turns into a link between listing and opening is skipped.

A regular file with other hard links outside the tree is charged to the project all the same: the project id belongs to the inode, not the name.

Types

type Filesystem

type Filesystem int

Filesystem is a filesystem this package knows how to drive.

const (
	// XFS is driven with the XFS quota ABI (Q_XGETQUOTA/Q_XSETQLIM,
	// struct fs_disk_quota, 512-byte basic blocks).
	XFS Filesystem = iota + 1
	// Ext4 is driven with the generic VFS quota ABI (Q_GETQUOTA/Q_SETQUOTA,
	// struct if_dqblk, 1 KiB block-limit units, usage in bytes). The magic
	// is shared by ext2 and ext3 mounts, which the ext4 driver also serves;
	// only a filesystem made with the project feature accepts project ids.
	Ext4
)

func Detect

func Detect(path string) (Filesystem, error)

Detect reports which filesystem path lives on, or ErrUnsupportedFilesystem.

func DetectFile

func DetectFile(f *os.File) (Filesystem, error)

DetectFile is Detect on an open file.

func (Filesystem) String

func (f Filesystem) String() string

String returns "xfs" or "ext4".

type Limits

type Limits struct {
	BlockSoft uint64 // bytes
	BlockHard uint64 // bytes
	InodeSoft uint64
	InodeHard uint64
}

Limits are the limits of one project. Zero means "no limit".

Block limits are in BYTES. The kernel ABI does not carry bytes, so they are rounded UP to the ABI unit: 512 bytes on XFS, 1 KiB on ext4. XFS then rounds up again to its filesystem block (XFS_B_TO_FSB). Read the limits back with Usage to see what the filesystem actually stored.

type Quota

type Quota struct {
	Limits

	// Bytes is the space charged to the project. On ext4 it is exact; on
	// XFS the ABI reports 512-byte basic blocks, so it is a multiple of 512.
	Bytes uint64
	// Inodes is the number of inodes charged to the project.
	Inodes uint64

	// BlockGrace and InodeGrace are when the soft-limit grace period
	// expires; zero when the project is not over its soft limit.
	BlockGrace time.Time
	InodeGrace time.Time
}

Quota is the usage and the limits of one project.

func Usage

func Usage(path string, id uint32) (Quota, error)

Usage returns the usage and limits of project id on the filesystem holding path. It requires CAP_SYS_ADMIN: fs/quota/quota.c check_quotactl_permission lets an unprivileged caller read only its own user and group quotas, never a project's.

A project XFS has never charged anything to has no quota record (xfs_qm_scall_getquota: "If it doesn't exist, we'll get ENOENT back"); Usage reports it as a zero Quota, which is what it is.

On ext4, Q_GETQUOTA needs write access to the mount (fs/quota/quota.c quotactl_cmd_write) and fails with EROFS on a read-only one.

func UsageFile

func UsageFile(f *os.File, id uint32) (Quota, error)

UsageFile is Usage through any open file on the filesystem.

type QuotaError

type QuotaError struct {
	Op   string // "Q_XSETQLIM", "Q_XGETQUOTA", "Q_SETQUOTA" or "Q_GETQUOTA"
	Path string // the file the call was made through
	ID   uint32 // the project id
	Err  error  // the errno
	Hint string // the likely cause of that errno, or ""
}

QuotaError is a failed quotactl_fd(2) call.

func (*QuotaError) Error

func (e *QuotaError) Error() string

func (*QuotaError) Unwrap

func (e *QuotaError) Unwrap() error

Unwrap returns the errno, so errors.Is(err, unix.EPERM) works.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址