1 // SPDX-License-Identifier: GPL-2.0
3 #include "btree_update.h"
9 static const char *bch2_sb_validate_quota(struct bch_sb *sb,
10 struct bch_sb_field *f)
12 struct bch_sb_field_quota *q = field_to_type(f, quota);
14 if (vstruct_bytes(&q->field) != sizeof(*q))
15 return "invalid field quota: wrong size";
20 const struct bch_sb_field_ops bch_sb_field_ops_quota = {
21 .validate = bch2_sb_validate_quota,
24 const char *bch2_quota_invalid(const struct bch_fs *c, struct bkey_s_c k)
26 if (k.k->p.inode >= QTYP_NR)
27 return "invalid quota type";
29 if (bkey_val_bytes(k.k) != sizeof(struct bch_quota))
30 return "incorrect value size";
35 static const char * const bch2_quota_counters[] = {
40 void bch2_quota_to_text(struct printbuf *out, struct bch_fs *c,
43 struct bkey_s_c_quota dq = bkey_s_c_to_quota(k);
46 for (i = 0; i < Q_COUNTERS; i++)
47 pr_buf(out, "%s hardlimit %llu softlimit %llu",
48 bch2_quota_counters[i],
49 le64_to_cpu(dq.v->c[i].hardlimit),
50 le64_to_cpu(dq.v->c[i].softlimit));
53 #ifdef CONFIG_BCACHEFS_QUOTA
55 #include <linux/cred.h>
57 #include <linux/quota.h>
59 static inline unsigned __next_qtype(unsigned i, unsigned qtypes)
62 return qtypes ? i + __ffs(qtypes) : QTYP_NR;
65 #define for_each_set_qtype(_c, _i, _q, _qtypes) \
67 (_i = __next_qtype(_i, _qtypes), \
68 _q = &(_c)->quotas[_i], \
72 static bool ignore_hardlimit(struct bch_memquota_type *q)
74 if (capable(CAP_SYS_RESOURCE))
77 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
79 return capable(CAP_SYS_RESOURCE) &&
80 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
81 !(info->dqi_flags & DQF_ROOT_SQUASH));
87 SOFTWARN, /* Softlimit reached */
88 SOFTLONGWARN, /* Grace time expired */
89 HARDWARN, /* Hardlimit reached */
91 HARDBELOW, /* Usage got below inode hardlimit */
92 SOFTBELOW, /* Usage got below inode softlimit */
95 static int quota_nl[][Q_COUNTERS] = {
96 [HARDWARN][Q_SPC] = QUOTA_NL_BHARDWARN,
97 [SOFTLONGWARN][Q_SPC] = QUOTA_NL_BSOFTLONGWARN,
98 [SOFTWARN][Q_SPC] = QUOTA_NL_BSOFTWARN,
99 [HARDBELOW][Q_SPC] = QUOTA_NL_BHARDBELOW,
100 [SOFTBELOW][Q_SPC] = QUOTA_NL_BSOFTBELOW,
102 [HARDWARN][Q_INO] = QUOTA_NL_IHARDWARN,
103 [SOFTLONGWARN][Q_INO] = QUOTA_NL_ISOFTLONGWARN,
104 [SOFTWARN][Q_INO] = QUOTA_NL_ISOFTWARN,
105 [HARDBELOW][Q_INO] = QUOTA_NL_IHARDBELOW,
106 [SOFTBELOW][Q_INO] = QUOTA_NL_ISOFTBELOW,
114 } m[QTYP_NR * Q_COUNTERS];
117 static void prepare_msg(unsigned qtype,
118 enum quota_counters counter,
119 struct quota_msgs *msgs,
120 enum quota_msg msg_type)
122 BUG_ON(msgs->nr >= ARRAY_SIZE(msgs->m));
124 msgs->m[msgs->nr].qtype = qtype;
125 msgs->m[msgs->nr].msg = quota_nl[msg_type][counter];
129 static void prepare_warning(struct memquota_counter *qc,
131 enum quota_counters counter,
132 struct quota_msgs *msgs,
133 enum quota_msg msg_type)
135 if (qc->warning_issued & (1 << msg_type))
138 prepare_msg(qtype, counter, msgs, msg_type);
141 static void flush_warnings(struct bch_qid qid,
142 struct super_block *sb,
143 struct quota_msgs *msgs)
147 for (i = 0; i < msgs->nr; i++)
148 quota_send_warning(make_kqid(&init_user_ns, msgs->m[i].qtype, qid.q[i]),
149 sb->s_dev, msgs->m[i].msg);
152 static int bch2_quota_check_limit(struct bch_fs *c,
154 struct bch_memquota *mq,
155 struct quota_msgs *msgs,
156 enum quota_counters counter,
158 enum quota_acct_mode mode)
160 struct bch_memquota_type *q = &c->quotas[qtype];
161 struct memquota_counter *qc = &mq->c[counter];
166 if (mode == KEY_TYPE_QUOTA_NOCHECK)
170 if (n < qc->hardlimit &&
171 (qc->warning_issued & (1 << HARDWARN))) {
172 qc->warning_issued &= ~(1 << HARDWARN);
173 prepare_msg(qtype, counter, msgs, HARDBELOW);
176 if (n < qc->softlimit &&
177 (qc->warning_issued & (1 << SOFTWARN))) {
178 qc->warning_issued &= ~(1 << SOFTWARN);
179 prepare_msg(qtype, counter, msgs, SOFTBELOW);
182 qc->warning_issued = 0;
188 !ignore_hardlimit(q)) {
189 if (mode == KEY_TYPE_QUOTA_PREALLOC)
192 prepare_warning(qc, qtype, counter, msgs, HARDWARN);
198 ktime_get_real_seconds() >= qc->timer &&
199 !ignore_hardlimit(q)) {
200 if (mode == KEY_TYPE_QUOTA_PREALLOC)
203 prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
209 if (mode == KEY_TYPE_QUOTA_PREALLOC)
212 prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
214 /* XXX is this the right one? */
215 qc->timer = ktime_get_real_seconds() +
216 q->limits[counter].warnlimit;
222 int bch2_quota_acct(struct bch_fs *c, struct bch_qid qid,
223 enum quota_counters counter, s64 v,
224 enum quota_acct_mode mode)
226 unsigned qtypes = enabled_qtypes(c);
227 struct bch_memquota_type *q;
228 struct bch_memquota *mq[QTYP_NR];
229 struct quota_msgs msgs;
233 memset(&msgs, 0, sizeof(msgs));
235 for_each_set_qtype(c, i, q, qtypes)
236 mutex_lock_nested(&q->lock, i);
238 for_each_set_qtype(c, i, q, qtypes) {
239 mq[i] = genradix_ptr_alloc(&q->table, qid.q[i], GFP_NOFS);
245 ret = bch2_quota_check_limit(c, i, mq[i], &msgs, counter, v, mode);
250 for_each_set_qtype(c, i, q, qtypes)
251 mq[i]->c[counter].v += v;
253 for_each_set_qtype(c, i, q, qtypes)
254 mutex_unlock(&q->lock);
256 flush_warnings(qid, c->vfs_sb, &msgs);
261 static void __bch2_quota_transfer(struct bch_memquota *src_q,
262 struct bch_memquota *dst_q,
263 enum quota_counters counter, s64 v)
265 BUG_ON(v > src_q->c[counter].v);
266 BUG_ON(v + dst_q->c[counter].v < v);
268 src_q->c[counter].v -= v;
269 dst_q->c[counter].v += v;
272 int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
274 struct bch_qid src, u64 space,
275 enum quota_acct_mode mode)
277 struct bch_memquota_type *q;
278 struct bch_memquota *src_q[3], *dst_q[3];
279 struct quota_msgs msgs;
283 qtypes &= enabled_qtypes(c);
285 memset(&msgs, 0, sizeof(msgs));
287 for_each_set_qtype(c, i, q, qtypes)
288 mutex_lock_nested(&q->lock, i);
290 for_each_set_qtype(c, i, q, qtypes) {
291 src_q[i] = genradix_ptr_alloc(&q->table, src.q[i], GFP_NOFS);
292 dst_q[i] = genradix_ptr_alloc(&q->table, dst.q[i], GFP_NOFS);
294 if (!src_q[i] || !dst_q[i]) {
299 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_SPC,
300 dst_q[i]->c[Q_SPC].v + space,
305 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_INO,
306 dst_q[i]->c[Q_INO].v + 1,
312 for_each_set_qtype(c, i, q, qtypes) {
313 __bch2_quota_transfer(src_q[i], dst_q[i], Q_SPC, space);
314 __bch2_quota_transfer(src_q[i], dst_q[i], Q_INO, 1);
318 for_each_set_qtype(c, i, q, qtypes)
319 mutex_unlock(&q->lock);
321 flush_warnings(dst, c->vfs_sb, &msgs);
326 static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k)
328 struct bkey_s_c_quota dq;
329 struct bch_memquota_type *q;
330 struct bch_memquota *mq;
333 BUG_ON(k.k->p.inode >= QTYP_NR);
337 dq = bkey_s_c_to_quota(k);
338 q = &c->quotas[k.k->p.inode];
340 mutex_lock(&q->lock);
341 mq = genradix_ptr_alloc(&q->table, k.k->p.offset, GFP_KERNEL);
343 mutex_unlock(&q->lock);
347 for (i = 0; i < Q_COUNTERS; i++) {
348 mq->c[i].hardlimit = le64_to_cpu(dq.v->c[i].hardlimit);
349 mq->c[i].softlimit = le64_to_cpu(dq.v->c[i].softlimit);
352 mutex_unlock(&q->lock);
358 static int bch2_quota_init_type(struct bch_fs *c, enum quota_types type)
360 struct btree_trans trans;
361 struct btree_iter iter;
365 bch2_trans_init(&trans, c, 0, 0);
367 for_each_btree_key(&trans, iter, BTREE_ID_quotas, POS(type, 0),
368 BTREE_ITER_PREFETCH, k, ret) {
369 if (k.k->p.inode != type)
372 ret = __bch2_quota_set(c, k);
376 bch2_trans_iter_exit(&trans, &iter);
378 bch2_trans_exit(&trans);
382 void bch2_fs_quota_exit(struct bch_fs *c)
386 for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
387 genradix_free(&c->quotas[i].table);
390 void bch2_fs_quota_init(struct bch_fs *c)
394 for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
395 mutex_init(&c->quotas[i].lock);
398 static void bch2_sb_quota_read(struct bch_fs *c)
400 struct bch_sb_field_quota *sb_quota;
403 sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
407 for (i = 0; i < QTYP_NR; i++) {
408 struct bch_memquota_type *q = &c->quotas[i];
410 for (j = 0; j < Q_COUNTERS; j++) {
411 q->limits[j].timelimit =
412 le32_to_cpu(sb_quota->q[i].c[j].timelimit);
413 q->limits[j].warnlimit =
414 le32_to_cpu(sb_quota->q[i].c[j].warnlimit);
419 static int bch2_fs_quota_read_inode(struct btree_trans *trans,
420 struct btree_iter *iter)
422 struct bch_fs *c = trans->c;
423 struct bch_inode_unpacked u;
424 struct bch_subvolume subvolume;
428 k = bch2_btree_iter_peek(iter);
436 ret = bch2_snapshot_get_subvol(trans, k.k->p.snapshot, &subvolume);
441 * We don't do quota accounting in snapshots:
443 if (BCH_SUBVOLUME_SNAP(&subvolume))
446 if (!bkey_is_inode(k.k))
449 ret = bch2_inode_unpack(k, &u);
453 bch2_quota_acct(c, bch_qid(&u), Q_SPC, u.bi_sectors,
454 KEY_TYPE_QUOTA_NOCHECK);
455 bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
456 KEY_TYPE_QUOTA_NOCHECK);
458 bch2_btree_iter_set_pos(iter, POS(iter->pos.inode, iter->pos.offset + 1));
462 int bch2_fs_quota_read(struct bch_fs *c)
464 unsigned i, qtypes = enabled_qtypes(c);
465 struct bch_memquota_type *q;
466 struct btree_trans trans;
467 struct btree_iter iter;
470 mutex_lock(&c->sb_lock);
471 bch2_sb_quota_read(c);
472 mutex_unlock(&c->sb_lock);
474 for_each_set_qtype(c, i, q, qtypes) {
475 ret = bch2_quota_init_type(c, i);
480 bch2_trans_init(&trans, c, 0, 0);
482 bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes, POS_MIN,
485 BTREE_ITER_ALL_SNAPSHOTS);
487 ret = lockrestart_do(&trans,
488 bch2_fs_quota_read_inode(&trans, &iter));
490 bch2_trans_iter_exit(&trans, &iter);
492 bch2_trans_exit(&trans);
493 return ret < 0 ? ret : 0;
496 /* Enable/disable/delete quotas for an entire filesystem: */
498 static int bch2_quota_enable(struct super_block *sb, unsigned uflags)
500 struct bch_fs *c = sb->s_fs_info;
502 if (sb->s_flags & SB_RDONLY)
505 /* Accounting must be enabled at mount time: */
506 if (uflags & (FS_QUOTA_UDQ_ACCT|FS_QUOTA_GDQ_ACCT|FS_QUOTA_PDQ_ACCT))
509 /* Can't enable enforcement without accounting: */
510 if ((uflags & FS_QUOTA_UDQ_ENFD) && !c->opts.usrquota)
513 if ((uflags & FS_QUOTA_GDQ_ENFD) && !c->opts.grpquota)
516 if (uflags & FS_QUOTA_PDQ_ENFD && !c->opts.prjquota)
519 mutex_lock(&c->sb_lock);
520 if (uflags & FS_QUOTA_UDQ_ENFD)
521 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, true);
523 if (uflags & FS_QUOTA_GDQ_ENFD)
524 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, true);
526 if (uflags & FS_QUOTA_PDQ_ENFD)
527 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, true);
530 mutex_unlock(&c->sb_lock);
535 static int bch2_quota_disable(struct super_block *sb, unsigned uflags)
537 struct bch_fs *c = sb->s_fs_info;
539 if (sb->s_flags & SB_RDONLY)
542 mutex_lock(&c->sb_lock);
543 if (uflags & FS_QUOTA_UDQ_ENFD)
544 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, false);
546 if (uflags & FS_QUOTA_GDQ_ENFD)
547 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, false);
549 if (uflags & FS_QUOTA_PDQ_ENFD)
550 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, false);
553 mutex_unlock(&c->sb_lock);
558 static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
560 struct bch_fs *c = sb->s_fs_info;
563 if (sb->s_flags & SB_RDONLY)
566 if (uflags & FS_USER_QUOTA) {
567 if (c->opts.usrquota)
570 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
572 POS(QTYP_USR + 1, 0),
578 if (uflags & FS_GROUP_QUOTA) {
579 if (c->opts.grpquota)
582 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
584 POS(QTYP_GRP + 1, 0),
590 if (uflags & FS_PROJ_QUOTA) {
591 if (c->opts.prjquota)
594 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
596 POS(QTYP_PRJ + 1, 0),
606 * Return quota status information, such as enforcements, quota file inode
609 static int bch2_quota_get_state(struct super_block *sb, struct qc_state *state)
611 struct bch_fs *c = sb->s_fs_info;
612 unsigned qtypes = enabled_qtypes(c);
615 memset(state, 0, sizeof(*state));
617 for (i = 0; i < QTYP_NR; i++) {
618 state->s_state[i].flags |= QCI_SYSFILE;
620 if (!(qtypes & (1 << i)))
623 state->s_state[i].flags |= QCI_ACCT_ENABLED;
625 state->s_state[i].spc_timelimit = c->quotas[i].limits[Q_SPC].timelimit;
626 state->s_state[i].spc_warnlimit = c->quotas[i].limits[Q_SPC].warnlimit;
628 state->s_state[i].ino_timelimit = c->quotas[i].limits[Q_INO].timelimit;
629 state->s_state[i].ino_warnlimit = c->quotas[i].limits[Q_INO].warnlimit;
636 * Adjust quota timers & warnings
638 static int bch2_quota_set_info(struct super_block *sb, int type,
639 struct qc_info *info)
641 struct bch_fs *c = sb->s_fs_info;
642 struct bch_sb_field_quota *sb_quota;
643 struct bch_memquota_type *q;
645 if (sb->s_flags & SB_RDONLY)
651 if (!((1 << type) & enabled_qtypes(c)))
654 if (info->i_fieldmask &
655 ~(QC_SPC_TIMER|QC_INO_TIMER|QC_SPC_WARNS|QC_INO_WARNS))
658 q = &c->quotas[type];
660 mutex_lock(&c->sb_lock);
661 sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
663 sb_quota = bch2_sb_resize_quota(&c->disk_sb,
664 sizeof(*sb_quota) / sizeof(u64));
669 if (info->i_fieldmask & QC_SPC_TIMER)
670 sb_quota->q[type].c[Q_SPC].timelimit =
671 cpu_to_le32(info->i_spc_timelimit);
673 if (info->i_fieldmask & QC_SPC_WARNS)
674 sb_quota->q[type].c[Q_SPC].warnlimit =
675 cpu_to_le32(info->i_spc_warnlimit);
677 if (info->i_fieldmask & QC_INO_TIMER)
678 sb_quota->q[type].c[Q_INO].timelimit =
679 cpu_to_le32(info->i_ino_timelimit);
681 if (info->i_fieldmask & QC_INO_WARNS)
682 sb_quota->q[type].c[Q_INO].warnlimit =
683 cpu_to_le32(info->i_ino_warnlimit);
685 bch2_sb_quota_read(c);
688 mutex_unlock(&c->sb_lock);
693 /* Get/set individual quotas: */
695 static void __bch2_quota_get(struct qc_dqblk *dst, struct bch_memquota *src)
697 dst->d_space = src->c[Q_SPC].v << 9;
698 dst->d_spc_hardlimit = src->c[Q_SPC].hardlimit << 9;
699 dst->d_spc_softlimit = src->c[Q_SPC].softlimit << 9;
700 dst->d_spc_timer = src->c[Q_SPC].timer;
701 dst->d_spc_warns = src->c[Q_SPC].warns;
703 dst->d_ino_count = src->c[Q_INO].v;
704 dst->d_ino_hardlimit = src->c[Q_INO].hardlimit;
705 dst->d_ino_softlimit = src->c[Q_INO].softlimit;
706 dst->d_ino_timer = src->c[Q_INO].timer;
707 dst->d_ino_warns = src->c[Q_INO].warns;
710 static int bch2_get_quota(struct super_block *sb, struct kqid kqid,
711 struct qc_dqblk *qdq)
713 struct bch_fs *c = sb->s_fs_info;
714 struct bch_memquota_type *q = &c->quotas[kqid.type];
715 qid_t qid = from_kqid(&init_user_ns, kqid);
716 struct bch_memquota *mq;
718 memset(qdq, 0, sizeof(*qdq));
720 mutex_lock(&q->lock);
721 mq = genradix_ptr(&q->table, qid);
723 __bch2_quota_get(qdq, mq);
724 mutex_unlock(&q->lock);
729 static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
730 struct qc_dqblk *qdq)
732 struct bch_fs *c = sb->s_fs_info;
733 struct bch_memquota_type *q = &c->quotas[kqid->type];
734 qid_t qid = from_kqid(&init_user_ns, *kqid);
735 struct genradix_iter iter;
736 struct bch_memquota *mq;
739 mutex_lock(&q->lock);
741 genradix_for_each_from(&q->table, iter, mq, qid)
742 if (memcmp(mq, page_address(ZERO_PAGE(0)), sizeof(*mq))) {
743 __bch2_quota_get(qdq, mq);
744 *kqid = make_kqid(current_user_ns(), kqid->type, iter.pos);
750 mutex_unlock(&q->lock);
754 static int bch2_set_quota_trans(struct btree_trans *trans,
755 struct bkey_i_quota *new_quota,
756 struct qc_dqblk *qdq)
758 struct btree_iter iter;
762 bch2_trans_iter_init(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
763 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
764 k = bch2_btree_iter_peek_slot(&iter);
770 if (k.k->type == KEY_TYPE_quota)
771 new_quota->v = *bkey_s_c_to_quota(k).v;
773 if (qdq->d_fieldmask & QC_SPC_SOFT)
774 new_quota->v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
775 if (qdq->d_fieldmask & QC_SPC_HARD)
776 new_quota->v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
778 if (qdq->d_fieldmask & QC_INO_SOFT)
779 new_quota->v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
780 if (qdq->d_fieldmask & QC_INO_HARD)
781 new_quota->v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
783 ret = bch2_trans_update(trans, &iter, &new_quota->k_i, 0);
784 bch2_trans_iter_exit(trans, &iter);
788 static int bch2_set_quota(struct super_block *sb, struct kqid qid,
789 struct qc_dqblk *qdq)
791 struct bch_fs *c = sb->s_fs_info;
792 struct bkey_i_quota new_quota;
795 if (sb->s_flags & SB_RDONLY)
798 bkey_quota_init(&new_quota.k_i);
799 new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
801 ret = bch2_trans_do(c, NULL, NULL, 0,
802 bch2_set_quota_trans(&trans, &new_quota, qdq)) ?:
803 __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i));
808 const struct quotactl_ops bch2_quotactl_operations = {
809 .quota_enable = bch2_quota_enable,
810 .quota_disable = bch2_quota_disable,
811 .rm_xquota = bch2_quota_remove,
813 .get_state = bch2_quota_get_state,
814 .set_info = bch2_quota_set_info,
816 .get_dqblk = bch2_get_quota,
817 .get_nextdqblk = bch2_get_next_quota,
818 .set_dqblk = bch2_set_quota,
821 #endif /* CONFIG_BCACHEFS_QUOTA */