1 // SPDX-License-Identifier: GPL-2.0
3 #include "btree_update.h"
11 static const char * const bch2_quota_types[] = {
17 static const char * const bch2_quota_counters[] = {
22 static int bch2_sb_quota_validate(struct bch_sb *sb, struct bch_sb_field *f,
25 struct bch_sb_field_quota *q = field_to_type(f, quota);
27 if (vstruct_bytes(&q->field) < sizeof(*q)) {
28 prt_printf(err, "wrong size (got %zu should be %zu)",
29 vstruct_bytes(&q->field), sizeof(*q));
30 return -BCH_ERR_invalid_sb_quota;
36 static void bch2_sb_quota_to_text(struct printbuf *out, struct bch_sb *sb,
37 struct bch_sb_field *f)
39 struct bch_sb_field_quota *q = field_to_type(f, quota);
40 unsigned qtyp, counter;
42 for (qtyp = 0; qtyp < ARRAY_SIZE(q->q); qtyp++) {
43 prt_printf(out, "%s: flags %llx",
44 bch2_quota_types[qtyp],
45 le64_to_cpu(q->q[qtyp].flags));
47 for (counter = 0; counter < Q_COUNTERS; counter++)
48 prt_printf(out, " %s timelimit %u warnlimit %u",
49 bch2_quota_counters[counter],
50 le32_to_cpu(q->q[qtyp].c[counter].timelimit),
51 le32_to_cpu(q->q[qtyp].c[counter].warnlimit));
57 const struct bch_sb_field_ops bch_sb_field_ops_quota = {
58 .validate = bch2_sb_quota_validate,
59 .to_text = bch2_sb_quota_to_text,
62 int bch2_quota_invalid(struct bch_fs *c, struct bkey_s_c k,
63 enum bkey_invalid_flags flags,
68 bkey_fsck_err_on(k.k->p.inode >= QTYP_NR, c, err,
70 "invalid quota type (%llu >= %u)",
71 k.k->p.inode, QTYP_NR);
76 void bch2_quota_to_text(struct printbuf *out, struct bch_fs *c,
79 struct bkey_s_c_quota dq = bkey_s_c_to_quota(k);
82 for (i = 0; i < Q_COUNTERS; i++)
83 prt_printf(out, "%s hardlimit %llu softlimit %llu",
84 bch2_quota_counters[i],
85 le64_to_cpu(dq.v->c[i].hardlimit),
86 le64_to_cpu(dq.v->c[i].softlimit));
89 #ifdef CONFIG_BCACHEFS_QUOTA
91 #include <linux/cred.h>
93 #include <linux/quota.h>
95 static void qc_info_to_text(struct printbuf *out, struct qc_info *i)
97 printbuf_tabstops_reset(out);
98 printbuf_tabstop_push(out, 20);
100 prt_str(out, "i_fieldmask");
102 prt_printf(out, "%x", i->i_fieldmask);
105 prt_str(out, "i_flags");
107 prt_printf(out, "%u", i->i_flags);
110 prt_str(out, "i_spc_timelimit");
112 prt_printf(out, "%u", i->i_spc_timelimit);
115 prt_str(out, "i_ino_timelimit");
117 prt_printf(out, "%u", i->i_ino_timelimit);
120 prt_str(out, "i_rt_spc_timelimit");
122 prt_printf(out, "%u", i->i_rt_spc_timelimit);
125 prt_str(out, "i_spc_warnlimit");
127 prt_printf(out, "%u", i->i_spc_warnlimit);
130 prt_str(out, "i_ino_warnlimit");
132 prt_printf(out, "%u", i->i_ino_warnlimit);
135 prt_str(out, "i_rt_spc_warnlimit");
137 prt_printf(out, "%u", i->i_rt_spc_warnlimit);
141 static void qc_dqblk_to_text(struct printbuf *out, struct qc_dqblk *q)
143 printbuf_tabstops_reset(out);
144 printbuf_tabstop_push(out, 20);
146 prt_str(out, "d_fieldmask");
148 prt_printf(out, "%x", q->d_fieldmask);
151 prt_str(out, "d_spc_hardlimit");
153 prt_printf(out, "%llu", q->d_spc_hardlimit);
156 prt_str(out, "d_spc_softlimit");
158 prt_printf(out, "%llu", q->d_spc_softlimit);
161 prt_str(out, "d_ino_hardlimit");
163 prt_printf(out, "%llu", q->d_ino_hardlimit);
166 prt_str(out, "d_ino_softlimit");
168 prt_printf(out, "%llu", q->d_ino_softlimit);
171 prt_str(out, "d_space");
173 prt_printf(out, "%llu", q->d_space);
176 prt_str(out, "d_ino_count");
178 prt_printf(out, "%llu", q->d_ino_count);
181 prt_str(out, "d_ino_timer");
183 prt_printf(out, "%llu", q->d_ino_timer);
186 prt_str(out, "d_spc_timer");
188 prt_printf(out, "%llu", q->d_spc_timer);
191 prt_str(out, "d_ino_warns");
193 prt_printf(out, "%i", q->d_ino_warns);
196 prt_str(out, "d_spc_warns");
198 prt_printf(out, "%i", q->d_spc_warns);
202 static inline unsigned __next_qtype(unsigned i, unsigned qtypes)
205 return qtypes ? i + __ffs(qtypes) : QTYP_NR;
208 #define for_each_set_qtype(_c, _i, _q, _qtypes) \
210 (_i = __next_qtype(_i, _qtypes), \
211 _q = &(_c)->quotas[_i], \
215 static bool ignore_hardlimit(struct bch_memquota_type *q)
217 if (capable(CAP_SYS_RESOURCE))
220 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
222 return capable(CAP_SYS_RESOURCE) &&
223 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
224 !(info->dqi_flags & DQF_ROOT_SQUASH));
230 SOFTWARN, /* Softlimit reached */
231 SOFTLONGWARN, /* Grace time expired */
232 HARDWARN, /* Hardlimit reached */
234 HARDBELOW, /* Usage got below inode hardlimit */
235 SOFTBELOW, /* Usage got below inode softlimit */
238 static int quota_nl[][Q_COUNTERS] = {
239 [HARDWARN][Q_SPC] = QUOTA_NL_BHARDWARN,
240 [SOFTLONGWARN][Q_SPC] = QUOTA_NL_BSOFTLONGWARN,
241 [SOFTWARN][Q_SPC] = QUOTA_NL_BSOFTWARN,
242 [HARDBELOW][Q_SPC] = QUOTA_NL_BHARDBELOW,
243 [SOFTBELOW][Q_SPC] = QUOTA_NL_BSOFTBELOW,
245 [HARDWARN][Q_INO] = QUOTA_NL_IHARDWARN,
246 [SOFTLONGWARN][Q_INO] = QUOTA_NL_ISOFTLONGWARN,
247 [SOFTWARN][Q_INO] = QUOTA_NL_ISOFTWARN,
248 [HARDBELOW][Q_INO] = QUOTA_NL_IHARDBELOW,
249 [SOFTBELOW][Q_INO] = QUOTA_NL_ISOFTBELOW,
257 } m[QTYP_NR * Q_COUNTERS];
260 static void prepare_msg(unsigned qtype,
261 enum quota_counters counter,
262 struct quota_msgs *msgs,
263 enum quota_msg msg_type)
265 BUG_ON(msgs->nr >= ARRAY_SIZE(msgs->m));
267 msgs->m[msgs->nr].qtype = qtype;
268 msgs->m[msgs->nr].msg = quota_nl[msg_type][counter];
272 static void prepare_warning(struct memquota_counter *qc,
274 enum quota_counters counter,
275 struct quota_msgs *msgs,
276 enum quota_msg msg_type)
278 if (qc->warning_issued & (1 << msg_type))
281 prepare_msg(qtype, counter, msgs, msg_type);
284 static void flush_warnings(struct bch_qid qid,
285 struct super_block *sb,
286 struct quota_msgs *msgs)
290 for (i = 0; i < msgs->nr; i++)
291 quota_send_warning(make_kqid(&init_user_ns, msgs->m[i].qtype, qid.q[i]),
292 sb->s_dev, msgs->m[i].msg);
295 static int bch2_quota_check_limit(struct bch_fs *c,
297 struct bch_memquota *mq,
298 struct quota_msgs *msgs,
299 enum quota_counters counter,
301 enum quota_acct_mode mode)
303 struct bch_memquota_type *q = &c->quotas[qtype];
304 struct memquota_counter *qc = &mq->c[counter];
309 if (mode == KEY_TYPE_QUOTA_NOCHECK)
313 if (n < qc->hardlimit &&
314 (qc->warning_issued & (1 << HARDWARN))) {
315 qc->warning_issued &= ~(1 << HARDWARN);
316 prepare_msg(qtype, counter, msgs, HARDBELOW);
319 if (n < qc->softlimit &&
320 (qc->warning_issued & (1 << SOFTWARN))) {
321 qc->warning_issued &= ~(1 << SOFTWARN);
322 prepare_msg(qtype, counter, msgs, SOFTBELOW);
325 qc->warning_issued = 0;
331 !ignore_hardlimit(q)) {
332 prepare_warning(qc, qtype, counter, msgs, HARDWARN);
338 if (qc->timer == 0) {
339 qc->timer = ktime_get_real_seconds() + q->limits[counter].timelimit;
340 prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
341 } else if (ktime_get_real_seconds() >= qc->timer &&
342 !ignore_hardlimit(q)) {
343 prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
351 int bch2_quota_acct(struct bch_fs *c, struct bch_qid qid,
352 enum quota_counters counter, s64 v,
353 enum quota_acct_mode mode)
355 unsigned qtypes = enabled_qtypes(c);
356 struct bch_memquota_type *q;
357 struct bch_memquota *mq[QTYP_NR];
358 struct quota_msgs msgs;
362 memset(&msgs, 0, sizeof(msgs));
364 for_each_set_qtype(c, i, q, qtypes) {
365 mq[i] = genradix_ptr_alloc(&q->table, qid.q[i], GFP_KERNEL);
370 for_each_set_qtype(c, i, q, qtypes)
371 mutex_lock_nested(&q->lock, i);
373 for_each_set_qtype(c, i, q, qtypes) {
374 ret = bch2_quota_check_limit(c, i, mq[i], &msgs, counter, v, mode);
379 for_each_set_qtype(c, i, q, qtypes)
380 mq[i]->c[counter].v += v;
382 for_each_set_qtype(c, i, q, qtypes)
383 mutex_unlock(&q->lock);
385 flush_warnings(qid, c->vfs_sb, &msgs);
390 static void __bch2_quota_transfer(struct bch_memquota *src_q,
391 struct bch_memquota *dst_q,
392 enum quota_counters counter, s64 v)
394 BUG_ON(v > src_q->c[counter].v);
395 BUG_ON(v + dst_q->c[counter].v < v);
397 src_q->c[counter].v -= v;
398 dst_q->c[counter].v += v;
401 int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
403 struct bch_qid src, u64 space,
404 enum quota_acct_mode mode)
406 struct bch_memquota_type *q;
407 struct bch_memquota *src_q[3], *dst_q[3];
408 struct quota_msgs msgs;
412 qtypes &= enabled_qtypes(c);
414 memset(&msgs, 0, sizeof(msgs));
416 for_each_set_qtype(c, i, q, qtypes) {
417 src_q[i] = genradix_ptr_alloc(&q->table, src.q[i], GFP_KERNEL);
418 dst_q[i] = genradix_ptr_alloc(&q->table, dst.q[i], GFP_KERNEL);
419 if (!src_q[i] || !dst_q[i])
423 for_each_set_qtype(c, i, q, qtypes)
424 mutex_lock_nested(&q->lock, i);
426 for_each_set_qtype(c, i, q, qtypes) {
427 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_SPC,
428 dst_q[i]->c[Q_SPC].v + space,
433 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_INO,
434 dst_q[i]->c[Q_INO].v + 1,
440 for_each_set_qtype(c, i, q, qtypes) {
441 __bch2_quota_transfer(src_q[i], dst_q[i], Q_SPC, space);
442 __bch2_quota_transfer(src_q[i], dst_q[i], Q_INO, 1);
446 for_each_set_qtype(c, i, q, qtypes)
447 mutex_unlock(&q->lock);
449 flush_warnings(dst, c->vfs_sb, &msgs);
454 static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k,
455 struct qc_dqblk *qdq)
457 struct bkey_s_c_quota dq;
458 struct bch_memquota_type *q;
459 struct bch_memquota *mq;
462 BUG_ON(k.k->p.inode >= QTYP_NR);
464 if (!((1U << k.k->p.inode) & enabled_qtypes(c)))
469 dq = bkey_s_c_to_quota(k);
470 q = &c->quotas[k.k->p.inode];
472 mutex_lock(&q->lock);
473 mq = genradix_ptr_alloc(&q->table, k.k->p.offset, GFP_KERNEL);
475 mutex_unlock(&q->lock);
479 for (i = 0; i < Q_COUNTERS; i++) {
480 mq->c[i].hardlimit = le64_to_cpu(dq.v->c[i].hardlimit);
481 mq->c[i].softlimit = le64_to_cpu(dq.v->c[i].softlimit);
484 if (qdq && qdq->d_fieldmask & QC_SPC_TIMER)
485 mq->c[Q_SPC].timer = qdq->d_spc_timer;
486 if (qdq && qdq->d_fieldmask & QC_SPC_WARNS)
487 mq->c[Q_SPC].warns = qdq->d_spc_warns;
488 if (qdq && qdq->d_fieldmask & QC_INO_TIMER)
489 mq->c[Q_INO].timer = qdq->d_ino_timer;
490 if (qdq && qdq->d_fieldmask & QC_INO_WARNS)
491 mq->c[Q_INO].warns = qdq->d_ino_warns;
493 mutex_unlock(&q->lock);
499 void bch2_fs_quota_exit(struct bch_fs *c)
503 for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
504 genradix_free(&c->quotas[i].table);
507 void bch2_fs_quota_init(struct bch_fs *c)
511 for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
512 mutex_init(&c->quotas[i].lock);
515 static struct bch_sb_field_quota *bch2_sb_get_or_create_quota(struct bch_sb_handle *sb)
517 struct bch_sb_field_quota *sb_quota = bch2_sb_field_get(sb->sb, quota);
522 sb_quota = bch2_sb_field_resize(sb, quota, sizeof(*sb_quota) / sizeof(u64));
526 for (qtype = 0; qtype < QTYP_NR; qtype++)
527 for (qc = 0; qc < Q_COUNTERS; qc++)
528 sb_quota->q[qtype].c[qc].timelimit =
529 cpu_to_le32(7 * 24 * 60 * 60);
535 static void bch2_sb_quota_read(struct bch_fs *c)
537 struct bch_sb_field_quota *sb_quota;
540 sb_quota = bch2_sb_field_get(c->disk_sb.sb, quota);
544 for (i = 0; i < QTYP_NR; i++) {
545 struct bch_memquota_type *q = &c->quotas[i];
547 for (j = 0; j < Q_COUNTERS; j++) {
548 q->limits[j].timelimit =
549 le32_to_cpu(sb_quota->q[i].c[j].timelimit);
550 q->limits[j].warnlimit =
551 le32_to_cpu(sb_quota->q[i].c[j].warnlimit);
556 static int bch2_fs_quota_read_inode(struct btree_trans *trans,
557 struct btree_iter *iter,
560 struct bch_fs *c = trans->c;
561 struct bch_inode_unpacked u;
562 struct bch_snapshot_tree s_t;
565 ret = bch2_snapshot_tree_lookup(trans,
566 bch2_snapshot_tree(c, k.k->p.snapshot), &s_t);
567 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
568 "%s: snapshot tree %u not found", __func__,
569 snapshot_t(c, k.k->p.snapshot)->tree);
573 if (!s_t.master_subvol)
576 ret = bch2_inode_find_by_inum_nowarn_trans(trans,
578 le32_to_cpu(s_t.master_subvol),
582 * Inode might be deleted in this snapshot - the easiest way to handle
583 * that is to just skip it here:
585 if (bch2_err_matches(ret, ENOENT))
591 bch2_quota_acct(c, bch_qid(&u), Q_SPC, u.bi_sectors,
592 KEY_TYPE_QUOTA_NOCHECK);
593 bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
594 KEY_TYPE_QUOTA_NOCHECK);
596 bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos));
600 int bch2_fs_quota_read(struct bch_fs *c)
603 mutex_lock(&c->sb_lock);
604 struct bch_sb_field_quota *sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
606 mutex_unlock(&c->sb_lock);
607 return -BCH_ERR_ENOSPC_sb_quota;
610 bch2_sb_quota_read(c);
611 mutex_unlock(&c->sb_lock);
613 int ret = bch2_trans_run(c,
614 for_each_btree_key(trans, iter, BTREE_ID_quotas, POS_MIN,
615 BTREE_ITER_PREFETCH, k,
616 __bch2_quota_set(c, k, NULL)) ?:
617 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS_MIN,
618 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
619 bch2_fs_quota_read_inode(trans, &iter, k)));
624 /* Enable/disable/delete quotas for an entire filesystem: */
626 static int bch2_quota_enable(struct super_block *sb, unsigned uflags)
628 struct bch_fs *c = sb->s_fs_info;
629 struct bch_sb_field_quota *sb_quota;
632 if (sb->s_flags & SB_RDONLY)
635 /* Accounting must be enabled at mount time: */
636 if (uflags & (FS_QUOTA_UDQ_ACCT|FS_QUOTA_GDQ_ACCT|FS_QUOTA_PDQ_ACCT))
639 /* Can't enable enforcement without accounting: */
640 if ((uflags & FS_QUOTA_UDQ_ENFD) && !c->opts.usrquota)
643 if ((uflags & FS_QUOTA_GDQ_ENFD) && !c->opts.grpquota)
646 if (uflags & FS_QUOTA_PDQ_ENFD && !c->opts.prjquota)
649 mutex_lock(&c->sb_lock);
650 sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
652 ret = -BCH_ERR_ENOSPC_sb_quota;
656 if (uflags & FS_QUOTA_UDQ_ENFD)
657 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, true);
659 if (uflags & FS_QUOTA_GDQ_ENFD)
660 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, true);
662 if (uflags & FS_QUOTA_PDQ_ENFD)
663 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, true);
667 mutex_unlock(&c->sb_lock);
669 return bch2_err_class(ret);
672 static int bch2_quota_disable(struct super_block *sb, unsigned uflags)
674 struct bch_fs *c = sb->s_fs_info;
676 if (sb->s_flags & SB_RDONLY)
679 mutex_lock(&c->sb_lock);
680 if (uflags & FS_QUOTA_UDQ_ENFD)
681 SET_BCH_SB_USRQUOTA(c->disk_sb.sb, false);
683 if (uflags & FS_QUOTA_GDQ_ENFD)
684 SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, false);
686 if (uflags & FS_QUOTA_PDQ_ENFD)
687 SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, false);
690 mutex_unlock(&c->sb_lock);
695 static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
697 struct bch_fs *c = sb->s_fs_info;
700 if (sb->s_flags & SB_RDONLY)
703 if (uflags & FS_USER_QUOTA) {
704 if (c->opts.usrquota)
707 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
709 POS(QTYP_USR, U64_MAX),
715 if (uflags & FS_GROUP_QUOTA) {
716 if (c->opts.grpquota)
719 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
721 POS(QTYP_GRP, U64_MAX),
727 if (uflags & FS_PROJ_QUOTA) {
728 if (c->opts.prjquota)
731 ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
733 POS(QTYP_PRJ, U64_MAX),
743 * Return quota status information, such as enforcements, quota file inode
746 static int bch2_quota_get_state(struct super_block *sb, struct qc_state *state)
748 struct bch_fs *c = sb->s_fs_info;
749 unsigned qtypes = enabled_qtypes(c);
752 memset(state, 0, sizeof(*state));
754 for (i = 0; i < QTYP_NR; i++) {
755 state->s_state[i].flags |= QCI_SYSFILE;
757 if (!(qtypes & (1 << i)))
760 state->s_state[i].flags |= QCI_ACCT_ENABLED;
762 state->s_state[i].spc_timelimit = c->quotas[i].limits[Q_SPC].timelimit;
763 state->s_state[i].spc_warnlimit = c->quotas[i].limits[Q_SPC].warnlimit;
765 state->s_state[i].ino_timelimit = c->quotas[i].limits[Q_INO].timelimit;
766 state->s_state[i].ino_warnlimit = c->quotas[i].limits[Q_INO].warnlimit;
773 * Adjust quota timers & warnings
775 static int bch2_quota_set_info(struct super_block *sb, int type,
776 struct qc_info *info)
778 struct bch_fs *c = sb->s_fs_info;
779 struct bch_sb_field_quota *sb_quota;
783 struct printbuf buf = PRINTBUF;
785 qc_info_to_text(&buf, info);
786 pr_info("setting:\n%s", buf.buf);
790 if (sb->s_flags & SB_RDONLY)
796 if (!((1 << type) & enabled_qtypes(c)))
799 if (info->i_fieldmask &
800 ~(QC_SPC_TIMER|QC_INO_TIMER|QC_SPC_WARNS|QC_INO_WARNS))
803 mutex_lock(&c->sb_lock);
804 sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
806 ret = -BCH_ERR_ENOSPC_sb_quota;
810 if (info->i_fieldmask & QC_SPC_TIMER)
811 sb_quota->q[type].c[Q_SPC].timelimit =
812 cpu_to_le32(info->i_spc_timelimit);
814 if (info->i_fieldmask & QC_SPC_WARNS)
815 sb_quota->q[type].c[Q_SPC].warnlimit =
816 cpu_to_le32(info->i_spc_warnlimit);
818 if (info->i_fieldmask & QC_INO_TIMER)
819 sb_quota->q[type].c[Q_INO].timelimit =
820 cpu_to_le32(info->i_ino_timelimit);
822 if (info->i_fieldmask & QC_INO_WARNS)
823 sb_quota->q[type].c[Q_INO].warnlimit =
824 cpu_to_le32(info->i_ino_warnlimit);
826 bch2_sb_quota_read(c);
830 mutex_unlock(&c->sb_lock);
832 return bch2_err_class(ret);
835 /* Get/set individual quotas: */
837 static void __bch2_quota_get(struct qc_dqblk *dst, struct bch_memquota *src)
839 dst->d_space = src->c[Q_SPC].v << 9;
840 dst->d_spc_hardlimit = src->c[Q_SPC].hardlimit << 9;
841 dst->d_spc_softlimit = src->c[Q_SPC].softlimit << 9;
842 dst->d_spc_timer = src->c[Q_SPC].timer;
843 dst->d_spc_warns = src->c[Q_SPC].warns;
845 dst->d_ino_count = src->c[Q_INO].v;
846 dst->d_ino_hardlimit = src->c[Q_INO].hardlimit;
847 dst->d_ino_softlimit = src->c[Q_INO].softlimit;
848 dst->d_ino_timer = src->c[Q_INO].timer;
849 dst->d_ino_warns = src->c[Q_INO].warns;
852 static int bch2_get_quota(struct super_block *sb, struct kqid kqid,
853 struct qc_dqblk *qdq)
855 struct bch_fs *c = sb->s_fs_info;
856 struct bch_memquota_type *q = &c->quotas[kqid.type];
857 qid_t qid = from_kqid(&init_user_ns, kqid);
858 struct bch_memquota *mq;
860 memset(qdq, 0, sizeof(*qdq));
862 mutex_lock(&q->lock);
863 mq = genradix_ptr(&q->table, qid);
865 __bch2_quota_get(qdq, mq);
866 mutex_unlock(&q->lock);
871 static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
872 struct qc_dqblk *qdq)
874 struct bch_fs *c = sb->s_fs_info;
875 struct bch_memquota_type *q = &c->quotas[kqid->type];
876 qid_t qid = from_kqid(&init_user_ns, *kqid);
877 struct genradix_iter iter;
878 struct bch_memquota *mq;
881 mutex_lock(&q->lock);
883 genradix_for_each_from(&q->table, iter, mq, qid)
884 if (memcmp(mq, page_address(ZERO_PAGE(0)), sizeof(*mq))) {
885 __bch2_quota_get(qdq, mq);
886 *kqid = make_kqid(current_user_ns(), kqid->type, iter.pos);
892 mutex_unlock(&q->lock);
893 return bch2_err_class(ret);
896 static int bch2_set_quota_trans(struct btree_trans *trans,
897 struct bkey_i_quota *new_quota,
898 struct qc_dqblk *qdq)
900 struct btree_iter iter;
904 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
905 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
910 if (k.k->type == KEY_TYPE_quota)
911 new_quota->v = *bkey_s_c_to_quota(k).v;
913 if (qdq->d_fieldmask & QC_SPC_SOFT)
914 new_quota->v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
915 if (qdq->d_fieldmask & QC_SPC_HARD)
916 new_quota->v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
918 if (qdq->d_fieldmask & QC_INO_SOFT)
919 new_quota->v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
920 if (qdq->d_fieldmask & QC_INO_HARD)
921 new_quota->v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
923 ret = bch2_trans_update(trans, &iter, &new_quota->k_i, 0);
924 bch2_trans_iter_exit(trans, &iter);
928 static int bch2_set_quota(struct super_block *sb, struct kqid qid,
929 struct qc_dqblk *qdq)
931 struct bch_fs *c = sb->s_fs_info;
932 struct bkey_i_quota new_quota;
936 struct printbuf buf = PRINTBUF;
938 qc_dqblk_to_text(&buf, qdq);
939 pr_info("setting:\n%s", buf.buf);
943 if (sb->s_flags & SB_RDONLY)
946 bkey_quota_init(&new_quota.k_i);
947 new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
949 ret = bch2_trans_do(c, NULL, NULL, 0,
950 bch2_set_quota_trans(trans, &new_quota, qdq)) ?:
951 __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i), qdq);
953 return bch2_err_class(ret);
956 const struct quotactl_ops bch2_quotactl_operations = {
957 .quota_enable = bch2_quota_enable,
958 .quota_disable = bch2_quota_disable,
959 .rm_xquota = bch2_quota_remove,
961 .get_state = bch2_quota_get_state,
962 .set_info = bch2_quota_set_info,
964 .get_dqblk = bch2_get_quota,
965 .get_nextdqblk = bch2_get_next_quota,
966 .set_dqblk = bch2_set_quota,
969 #endif /* CONFIG_BCACHEFS_QUOTA */