#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: quota2_subr.c,v 1.8 2026/01/22 03:23:36 riastradh Exp $");
#include <sys/param.h>
#include <sys/time.h>
#include <ufs/ffs/ffs_extern.h>
#include <ufs/ffs/fs.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ufs/quota2.h>
#include <ufs/ufs/ufs_bswap.h>
#ifndef _KERNEL
#include <string.h>
#endif
void
quota2_addfreeq2e(struct quota2_header *q2h, void *bp, uint64_t baseoff,
uint64_t bsize, int ns)
{
uint64_t blkoff = baseoff % bsize;
int i, nq2e;
struct quota2_entry *q2e;
q2e = (void *)((char *)bp + blkoff);
nq2e = (bsize - blkoff) / sizeof(*q2e);
for (i = 0; i < nq2e; i++) {
q2e[i].q2e_next = q2h->q2h_free;
q2h->q2h_free = ufs_rw64(i * sizeof(*q2e) + baseoff, ns);
}
}
void
quota2_create_blk0(uint64_t bsize, void *bp, int q2h_hash_shift, int type,
int ns)
{
struct quota2_header *q2h;
const int quota2_hash_size = 1 << q2h_hash_shift;
const int quota2_full_header_size = sizeof(struct quota2_header) +
sizeof(q2h->q2h_entries[0]) * quota2_hash_size;
int i;
memset(bp, 0, bsize);
q2h = bp;
q2h->q2h_magic_number = ufs_rw32(Q2_HEAD_MAGIC, ns);
q2h->q2h_type = type;
q2h->q2h_hash_shift = q2h_hash_shift;
q2h->q2h_hash_size = ufs_rw16(quota2_hash_size, ns);
for (i = 0; i < N_QL; i++) {
q2h->q2h_defentry.q2e_val[i].q2v_hardlimit =
q2h->q2h_defentry.q2e_val[i].q2v_softlimit =
ufs_rw64(UQUAD_MAX, ns);
q2h->q2h_defentry.q2e_val[i].q2v_grace =
ufs_rw64(7ULL * 24ULL * 3600ULL, ns);
}
quota2_addfreeq2e(q2h, bp, quota2_full_header_size, bsize, ns);
}
void
quota2_ufs_rwq2v(const struct quota2_val *s, struct quota2_val *d,
int needswap)
{
d->q2v_hardlimit = ufs_rw64(s->q2v_hardlimit, needswap);
d->q2v_softlimit = ufs_rw64(s->q2v_softlimit, needswap);
d->q2v_cur = ufs_rw64(s->q2v_cur, needswap);
d->q2v_time = ufs_rw64(s->q2v_time, needswap);
d->q2v_grace = ufs_rw64(s->q2v_grace, needswap);
}
void
quota2_ufs_rwq2e(const struct quota2_entry *s, struct quota2_entry *d,
int needswap)
{
quota2_ufs_rwq2v(&s->q2e_val[QL_BLOCK], &d->q2e_val[QL_BLOCK],
needswap);
quota2_ufs_rwq2v(&s->q2e_val[QL_FILE], &d->q2e_val[QL_FILE],
needswap);
d->q2e_uid = ufs_rw32(s->q2e_uid, needswap);
}
int
quota_check_limit(uint64_t cur, uint64_t change, uint64_t soft, uint64_t hard,
time_t expire, time_t now)
{
if (cur + change > hard) {
if (cur <= soft)
return (QL_F_CROSS | QL_S_DENY_HARD);
return QL_S_DENY_HARD;
} else if (cur + change > soft) {
if (cur <= soft)
return (QL_F_CROSS | QL_S_ALLOW_SOFT);
if (now > expire) {
return QL_S_DENY_GRACE;
}
return QL_S_ALLOW_SOFT;
}
return QL_S_ALLOW_OK;
}