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 ¶
- Variables
- func GetProject(path string) (id uint32, inherit bool, err error)
- func GetProjectFile(f *os.File) (id uint32, inherit bool, err error)
- func SetLimits(path string, id uint32, l Limits) error
- func SetLimitsFile(f *os.File, id uint32, l Limits) error
- func SetProject(path string, id uint32, inherit bool) error
- func SetProjectFile(f *os.File, id uint32, inherit bool) error
- func SetProjectTree(root string, id uint32) error
- type Filesystem
- type Limits
- type Quota
- type QuotaError
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
GetProjectFile is GetProject on an open file (not opened with O_PATH).
func SetLimits ¶
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 ¶
SetLimitsFile is SetLimits through any open file on the filesystem.
func SetProject ¶
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 ¶
SetProjectFile is SetProject on an open file (not opened with O_PATH).
func SetProjectTree ¶
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.
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 ¶
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.
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.