1 // SPDX-License-Identifier: GPL-2.0
3 #include "btree_update.h"
9 static int bch2_sb_validate_quota(struct bch_sb *sb, 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 pr_buf(err, "wrong size (got %llu should be %zu)",
16 vstruct_bytes(&q->field), sizeof(*q));
22 const struct bch_sb_field_ops bch_sb_field_ops_quota = {
23 .validate = bch2_sb_validate_quota,
26 const char *bch2_quota_invalid(const struct bch_fs *c, struct bkey_s_c k)
28 if (k.k->p.inode >= QTYP_NR)
29 return "invalid quota type";
31 if (bkey_val_bytes(k.k) != sizeof(struct bch_quota))
32 return "incorrect value size";
37 static const char * const bch2_quota_counters[] = {
42 void bch2_quota_to_text(struct printbuf *out, struct bch_fs *c,
45 struct bkey_s_c_quota dq = bkey_s_c_to_quota(k);
48 for (i = 0; i < Q_COUNTERS; i++)
49 pr_buf(out, "%s hardlimit %llu softlimit %llu",
50 bch2_quota_counters[i],
51 le64_to_cpu(dq.v->c[i].hardlimit),
52 le64_to_cpu(dq.v->c[i].softlimit));
55 #ifdef CONFIG_BCACHEFS_QUOTA
57 #include <linux/cred.h>
59 #include <linux/quota.h>
61 static inline unsigned __next_qtype(unsigned i, unsigned qtypes)
64 return qtypes ? i + __ffs(qtypes) : QTYP_NR;
67 #define for_each_set_qtype(_c, _i, _q, _qtypes) \
69 (_i = __next_qtype(_i, _qtypes), \
70 _q = &(_c)->quotas[_i], \
74 static bool ignore_hardlimit(struct bch_memquota_type *q)
76 if (capable(CAP_SYS_RESOURCE))
79 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
81 return capable(CAP_SYS_RESOURCE) &&
82 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
83 !(info->dqi_flags & DQF_ROOT_SQUASH));
89 SOFTWARN, /* Softlimit reached */
90 SOFTLONGWARN, /* Grace time expired */
91 HARDWARN, /* Hardlimit reached */
93 HARDBELOW, /* Usage got below inode hardlimit */
94 SOFTBELOW, /* Usage got below inode softlimit */
97 static int quota_nl[][Q_COUNTERS] = {
98 [HARDWARN][Q_SPC] = QUOTA_NL_BHARDWARN,
99 [SOFTLONGWARN][Q_SPC] = QUOTA_NL_BSOFTLONGWARN,
100 [SOFTWARN][Q_SPC] = QUOTA_NL_BSOFTWARN,
101 [HARDBELOW][Q_SPC] = QUOTA_NL_BHARDBELOW,
102 [SOFTBELOW][Q_SPC] = QUOTA_NL_BSOFTBELOW,
104 [HARDWARN][Q_INO] = QUOTA_NL_IHARDWARN,
105 [SOFTLONGWARN][Q_INO] = QUOTA_NL_ISOFTLONGWARN,
106 [SOFTWARN][Q_INO] = QUOTA_NL_ISOFTWARN,
107 [HARDBELOW][Q_INO] = QUOTA_NL_IHARDBELOW,
108 [SOFTBELOW][Q_INO] = QUOTA_NL_ISOFTBELOW,
116 } m[QTYP_NR * Q_COUNTERS];
119 static void prepare_msg(unsigned qtype,
120 enum quota_counters counter,
121 struct quota_msgs *msgs,
122 enum quota_msg msg_type)
124 BUG_ON(msgs->nr >= ARRAY_SIZE(msgs->m));
126 msgs->m[msgs->nr].qtype = qtype;
127 msgs->m[msgs->nr].msg = quota_nl[msg_type][counter];
131 static void prepare_warning(struct memquota_counter *qc,
133 enum quota_counters counter,
134 struct quota_msgs *msgs,
135 enum quota_msg msg_type)
137 if (qc->warning_issued & (1 << msg_type))
140 prepare_msg(qtype, counter, msgs, msg_type);
143 static void flush_warnings(struct bch_qid qid,
144 struct super_block *sb,
145 struct quota_msgs *msgs)
149 for (i = 0; i < msgs->nr; i++)
150 quota_send_warning(make_kqid(&init_user_ns, msgs->m[i].qtype, qid.q[i]),
151 sb->s_dev, msgs->m[i].msg);
154 static int bch2_quota_check_limit(struct bch_fs *c,
156 struct bch_memquota *mq,
157 struct quota_msgs *msgs,
158 enum quota_counters counter,
160 enum quota_acct_mode mode)
162 struct bch_memquota_type *q = &c->quotas[qtype];
163 struct memquota_counter *qc = &mq->c[counter];
168 if (mode == KEY_TYPE_QUOTA_NOCHECK)
172 if (n < qc->hardlimit &&
173 (qc->warning_issued & (1 << HARDWARN))) {
174 qc->warning_issued &= ~(1 << HARDWARN);
175 prepare_msg(qtype, counter, msgs, HARDBELOW);
178 if (n < qc->softlimit &&
179 (qc->warning_issued & (1 << SOFTWARN))) {
180 qc->warning_issued &= ~(1 << SOFTWARN);
181 prepare_msg(qtype, counter, msgs, SOFTBELOW);
184 qc->warning_issued = 0;
190 !ignore_hardlimit(q)) {
191 if (mode == KEY_TYPE_QUOTA_PREALLOC)
194 prepare_warning(qc, qtype, counter, msgs, HARDWARN);
200 ktime_get_real_seconds() >= qc->timer &&
201 !ignore_hardlimit(q)) {
202 if (mode == KEY_TYPE_QUOTA_PREALLOC)
205 prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
211 if (mode == KEY_TYPE_QUOTA_PREALLOC)
214 prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
216 /* XXX is this the right one? */
217 qc->timer = ktime_get_real_seconds() +
218 q->limits[counter].warnlimit;
224 int bch2_quota_acct(struct bch_fs *c, struct bch_qid qid,
225 enum quota_counters counter, s64 v,
226 enum quota_acct_mode mode)
228 unsigned qtypes = enabled_qtypes(c);
229 struct bch_memquota_type *q;
230 struct bch_memquota *mq[QTYP_NR];
231 struct quota_msgs msgs;
235 memset(&msgs, 0, sizeof(msgs));
237 for_each_set_qtype(c, i, q, qtypes)
238 mutex_lock_nested(&q->lock, i);
240 for_each_set_qtype(c, i, q, qtypes) {
241 mq[i] = genradix_ptr_alloc(&q->table, qid.q[i], GFP_NOFS);
247 ret = bch2_quota_check_limit(c, i, mq[i], &msgs, counter, v, mode);
252 for_each_set_qtype(c, i, q, qtypes)
253 mq[i]->c[counter].v += v;
255 for_each_set_qtype(c, i, q, qtypes)
256 mutex_unlock(&q->lock);
258 flush_warnings(qid, c->vfs_sb, &msgs);
263 static void __bch2_quota_transfer(struct bch_memquota *src_q,
264 struct bch_memquota *dst_q,
265 enum quota_counters counter, s64 v)
267 BUG_ON(v > src_q->c[counter].v);
268 BUG_ON(v + dst_q->c[counter].v < v);
270 src_q->c[counter].v -= v;
271 dst_q->c[counter].v += v;
274 int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
276 struct bch_qid src, u64 space,
277 enum quota_acct_mode mode)
279 struct bch_memquota_type *q;
280 struct bch_memquota *src_q[3], *dst_q[3];
281 struct quota_msgs msgs;
285 qtypes &= enabled_qtypes(c);
287 memset(&msgs, 0, sizeof(msgs));
289 for_each_set_qtype(c, i, q, qtypes)
290 mutex_lock_nested(&q->lock, i);
292 for_each_set_qtype(c, i, q, qtypes) {
293 src_q[i] = genradix_ptr_alloc(&q->table, src.q[i], GFP_NOFS);
294 dst_q[i] = genradix_ptr_alloc(&q->table, dst.q[i], GFP_NOFS);
296 if (!src_q[i] || !dst_q[i]) {
301 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_SPC,
302 dst_q[i]->c[Q_SPC].v + space,
307 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_INO,
308 dst_q[i]->c[Q_INO].v + 1,
314 for_each_set_qtype(c, i, q, qtypes) {
315 __bch2_quota_transfer(src_q[i], dst_q[i], Q_SPC, space);
316 __bch2_quota_transfer(src_q[i], dst_q[i], Q_INO, 1);
320 for_each_set_qtype(c, i, q, qtypes)
321 mutex_unlock(&q->lock);
323 flush_warnings(dst, c->vfs_sb, &msgs);
328 static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k)
330 struct bkey_s_c_quota dq;
331 struct bch_memquota_type *q;
332 struct bch_memquota *mq;
335 BUG_ON(k.k->p.inode >= QTYP_NR);
339 dq = bkey_s_c_to_quota(k);
340 q = &c->quotas[k.k->p.inode];
342 mutex_lock(&q->lock);
343 mq = genradix_ptr_alloc(&q->table, k.k->p.offset, GFP_KERNEL);
345 mutex_unlock(&q->lock);
349 for (i = 0; i < Q_COUNTERS; i++) {
350 mq->c[i].hardlimit = le64_to_cpu(dq.v->c[i].hardlimit);
351 mq->c[i].softlimit = le64_to_cpu(dq.v->c[i].softlimit);
354 mutex_unlock(&q->lock);
360 static int bch2_quota_init_type(struct bch_fs *c, enum quota_types type)
362 struct btree_trans trans;
363 struct btree_iter iter;
367 bch2_trans_init(&trans, c, 0, 0);
369 for_each_btree_key(&trans, iter, BTREE_ID_quotas, POS(type, 0),
370 BTREE_ITER_PREFETCH, k, ret) {
371 if (k.k->p.inode != type)
374 ret = __bch2_quota_set(c, k);
378 bch2_trans_iter_exit(&trans, &iter);
380 bch2_trans_exit(&trans);
384 void bch2_fs_quota_exit(struct bch_fs *c)
388 for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
389 genradix_free(&c->quotas[i].table);
392 void bch2_fs_quota_init(struct bch_fs *c)
396 for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
397 mutex_init(&c->quotas[i].lock);
400 static void bch2_sb_quota_read(struct bch_fs *c)
402 struct bch_sb_field_quota *sb_quota;
405 sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
409 for (i = 0; i < QTYP_NR; i++) {
410 struct bch_memquota_type *q = &c->quotas[i];
412 for (j = 0; j < Q_COUNTERS; j++) {
413 q->limits[j].timelimit =
414 le32_to_cpu(sb_quota->q[i].c[j].timelimit);
415 q->limits[j].warnlimit =
416 le32_to_cpu(sb_quota->q[i].c[j].warnlimit);
421 static int bch2_fs_quota_read_inode(struct btree_trans *trans,
422 struct btree_iter *iter)
424 struct bch_fs *c = trans->c;
425 struct bch_inode_unpacked u;
426 struct bch_subvolume subvolume;
430 k = bch2_btree_iter_peek(iter);
438 ret = bch2_snapshot_get_subvol(trans, k.k->p.snapshot, &subvolume);
443 * We don't do quota accounting in snapshots:
445 if (BCH_SUBVOLUME_SNAP(&subvolume))
448 if (!bkey_is_inode(k.k))
451 ret = bch2_inode_unpack(k, &u);
455 bch2_quota_acct(c, bch_qid(&u), Q_SPC, u.bi_sectors,
456 KEY_TYPE_QUOTA_NOCHECK);
457 bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
458 KEY_TYPE_QUOTA_NOCHECK);
460 bch2_btree_iter_set_pos(iter, POS(iter->pos.inode, iter->pos.offset + 1));
464 int bch2_fs_quota_read(struct bch_fs *c)
466 unsigned i, qtypes = enabled_qtypes(c);
467 struct bch_memquota_type *q;
468 struct btree_trans trans;
469 struct btree_iter iter;
472 mutex_lock(&c->sb_lock);
473 bch2_sb_quota_read(c);
474 mutex_unlock(&c->sb_lock);
476 for_each_set_qtype(c, i, q, qtypes) {
477 ret = bch2_quota_init_type(c, i);
482 bch2_trans_init(&trans, c, 0, 0);
484 bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes, POS_MIN,
487 BTREE_ITER_ALL_SNAPSHOTS);
489 ret = lockrestart_do(&trans,
490 bch2_fs_quota_read_inode(&trans, &iter));
492 bch2_trans_iter_exit(&trans, &iter);
494 bch2_trans_exit(&trans);
495 return ret < 0 ? ret : 0;
498 /* Enable/disable/delete quotas for an entire filesystem: */
500 static int bch2_quota_enable(struct super_block *sb, unsigned uflags)
502 struct bch_fs *c = sb->s_fs_info;
504 if (sb->s_flags & SB_RDONLY)
507 /* Accounting must be enabled at mount time: */
508 if (uflags & (FS_QUOTA_UDQ_ACCT|FS_QUOTA_GDQ_ACCT|FS_QUOTA_PDQ_ACCT))
511 /* Can't enable enforcement without accounting: */
512 if ((uflags & FS_QUOTA_UDQ_ENFD) && !c->opts.usrquota)
515 if ((uflags & FS_QUOTA_GDQ_ENFD) && !c->opts.grpquota)
518 if (uflags & FS_QUOTA_PDQ_ENFD && !c->opts.prjquota)
521 mutex_lock(&c->sb_lock);
522 if (uflags & FS_QUOTA_UDQ_ENFD)
523 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, true);
525 if (uflags & FS_QUOTA_GDQ_ENFD)
526 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, true);
528 if (uflags & FS_QUOTA_PDQ_ENFD)
529 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, true);
532 mutex_unlock(&c->sb_lock);
537 static int bch2_quota_disable(struct super_block *sb, unsigned uflags)
539 struct bch_fs *c = sb->s_fs_info;
541 if (sb->s_flags & SB_RDONLY)
544 mutex_lock(&c->sb_lock);
545 if (uflags & FS_QUOTA_UDQ_ENFD)
546 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, false);
548 if (uflags & FS_QUOTA_GDQ_ENFD)
549 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, false);
551 if (uflags & FS_QUOTA_PDQ_ENFD)
552 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, false);
555 mutex_unlock(&c->sb_lock);
560 static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
562 struct bch_fs *c = sb->s_fs_info;
565 if (sb->s_flags & SB_RDONLY)
568 if (uflags & FS_USER_QUOTA) {
569 if (c->opts.usrquota)
572 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
574 POS(QTYP_USR + 1, 0),
580 if (uflags & FS_GROUP_QUOTA) {
581 if (c->opts.grpquota)
584 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
586 POS(QTYP_GRP + 1, 0),
592 if (uflags & FS_PROJ_QUOTA) {
593 if (c->opts.prjquota)
596 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
598 POS(QTYP_PRJ + 1, 0),
608 * Return quota status information, such as enforcements, quota file inode
611 static int bch2_quota_get_state(struct super_block *sb, struct qc_state *state)
613 struct bch_fs *c = sb->s_fs_info;
614 unsigned qtypes = enabled_qtypes(c);
617 memset(state, 0, sizeof(*state));
619 for (i = 0; i < QTYP_NR; i++) {
620 state->s_state[i].flags |= QCI_SYSFILE;
622 if (!(qtypes & (1 << i)))
625 state->s_state[i].flags |= QCI_ACCT_ENABLED;
627 state->s_state[i].spc_timelimit = c->quotas[i].limits[Q_SPC].timelimit;
628 state->s_state[i].spc_warnlimit = c->quotas[i].limits[Q_SPC].warnlimit;
630 state->s_state[i].ino_timelimit = c->quotas[i].limits[Q_INO].timelimit;
631 state->s_state[i].ino_warnlimit = c->quotas[i].limits[Q_INO].warnlimit;
638 * Adjust quota timers & warnings
640 static int bch2_quota_set_info(struct super_block *sb, int type,
641 struct qc_info *info)
643 struct bch_fs *c = sb->s_fs_info;
644 struct bch_sb_field_quota *sb_quota;
645 struct bch_memquota_type *q;
647 if (sb->s_flags & SB_RDONLY)
653 if (!((1 << type) & enabled_qtypes(c)))
656 if (info->i_fieldmask &
657 ~(QC_SPC_TIMER|QC_INO_TIMER|QC_SPC_WARNS|QC_INO_WARNS))
660 q = &c->quotas[type];
662 mutex_lock(&c->sb_lock);
663 sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
665 sb_quota = bch2_sb_resize_quota(&c->disk_sb,
666 sizeof(*sb_quota) / sizeof(u64));
671 if (info->i_fieldmask & QC_SPC_TIMER)
672 sb_quota->q[type].c[Q_SPC].timelimit =
673 cpu_to_le32(info->i_spc_timelimit);
675 if (info->i_fieldmask & QC_SPC_WARNS)
676 sb_quota->q[type].c[Q_SPC].warnlimit =
677 cpu_to_le32(info->i_spc_warnlimit);
679 if (info->i_fieldmask & QC_INO_TIMER)
680 sb_quota->q[type].c[Q_INO].timelimit =
681 cpu_to_le32(info->i_ino_timelimit);
683 if (info->i_fieldmask & QC_INO_WARNS)
684 sb_quota->q[type].c[Q_INO].warnlimit =
685 cpu_to_le32(info->i_ino_warnlimit);
687 bch2_sb_quota_read(c);
690 mutex_unlock(&c->sb_lock);
695 /* Get/set individual quotas: */
697 static void __bch2_quota_get(struct qc_dqblk *dst, struct bch_memquota *src)
699 dst->d_space = src->c[Q_SPC].v << 9;
700 dst->d_spc_hardlimit = src->c[Q_SPC].hardlimit << 9;
701 dst->d_spc_softlimit = src->c[Q_SPC].softlimit << 9;
702 dst->d_spc_timer = src->c[Q_SPC].timer;
703 dst->d_spc_warns = src->c[Q_SPC].warns;
705 dst->d_ino_count = src->c[Q_INO].v;
706 dst->d_ino_hardlimit = src->c[Q_INO].hardlimit;
707 dst->d_ino_softlimit = src->c[Q_INO].softlimit;
708 dst->d_ino_timer = src->c[Q_INO].timer;
709 dst->d_ino_warns = src->c[Q_INO].warns;
712 static int bch2_get_quota(struct super_block *sb, struct kqid kqid,
713 struct qc_dqblk *qdq)
715 struct bch_fs *c = sb->s_fs_info;
716 struct bch_memquota_type *q = &c->quotas[kqid.type];
717 qid_t qid = from_kqid(&init_user_ns, kqid);
718 struct bch_memquota *mq;
720 memset(qdq, 0, sizeof(*qdq));
722 mutex_lock(&q->lock);
723 mq = genradix_ptr(&q->table, qid);
725 __bch2_quota_get(qdq, mq);
726 mutex_unlock(&q->lock);
731 static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
732 struct qc_dqblk *qdq)
734 struct bch_fs *c = sb->s_fs_info;
735 struct bch_memquota_type *q = &c->quotas[kqid->type];
736 qid_t qid = from_kqid(&init_user_ns, *kqid);
737 struct genradix_iter iter;
738 struct bch_memquota *mq;
741 mutex_lock(&q->lock);
743 genradix_for_each_from(&q->table, iter, mq, qid)
744 if (memcmp(mq, page_address(ZERO_PAGE(0)), sizeof(*mq))) {
745 __bch2_quota_get(qdq, mq);
746 *kqid = make_kqid(current_user_ns(), kqid->type, iter.pos);
752 mutex_unlock(&q->lock);
756 static int bch2_set_quota_trans(struct btree_trans *trans,
757 struct bkey_i_quota *new_quota,
758 struct qc_dqblk *qdq)
760 struct btree_iter iter;
764 bch2_trans_iter_init(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
765 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
766 k = bch2_btree_iter_peek_slot(&iter);
772 if (k.k->type == KEY_TYPE_quota)
773 new_quota->v = *bkey_s_c_to_quota(k).v;
775 if (qdq->d_fieldmask & QC_SPC_SOFT)
776 new_quota->v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
777 if (qdq->d_fieldmask & QC_SPC_HARD)
778 new_quota->v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
780 if (qdq->d_fieldmask & QC_INO_SOFT)
781 new_quota->v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
782 if (qdq->d_fieldmask & QC_INO_HARD)
783 new_quota->v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
785 ret = bch2_trans_update(trans, &iter, &new_quota->k_i, 0);
786 bch2_trans_iter_exit(trans, &iter);
790 static int bch2_set_quota(struct super_block *sb, struct kqid qid,
791 struct qc_dqblk *qdq)
793 struct bch_fs *c = sb->s_fs_info;
794 struct bkey_i_quota new_quota;
797 if (sb->s_flags & SB_RDONLY)
800 bkey_quota_init(&new_quota.k_i);
801 new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
803 ret = bch2_trans_do(c, NULL, NULL, 0,
804 bch2_set_quota_trans(&trans, &new_quota, qdq)) ?:
805 __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i));
810 const struct quotactl_ops bch2_quotactl_operations = {
811 .quota_enable = bch2_quota_enable,
812 .quota_disable = bch2_quota_disable,
813 .rm_xquota = bch2_quota_remove,
815 .get_state = bch2_quota_get_state,
816 .set_info = bch2_quota_set_info,
818 .get_dqblk = bch2_get_quota,
819 .get_nextdqblk = bch2_get_next_quota,
820 .set_dqblk = bch2_set_quota,
823 #endif /* CONFIG_BCACHEFS_QUOTA */