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(const struct bch_fs *c, struct bkey_s_c k,
63 unsigned flags, struct printbuf *err)
65 if (k.k->p.inode >= QTYP_NR) {
66 prt_printf(err, "invalid quota type (%llu >= %u)",
67 k.k->p.inode, QTYP_NR);
68 return -BCH_ERR_invalid_bkey;
74 void bch2_quota_to_text(struct printbuf *out, struct bch_fs *c,
77 struct bkey_s_c_quota dq = bkey_s_c_to_quota(k);
80 for (i = 0; i < Q_COUNTERS; i++)
81 prt_printf(out, "%s hardlimit %llu softlimit %llu",
82 bch2_quota_counters[i],
83 le64_to_cpu(dq.v->c[i].hardlimit),
84 le64_to_cpu(dq.v->c[i].softlimit));
87 #ifdef CONFIG_BCACHEFS_QUOTA
89 #include <linux/cred.h>
91 #include <linux/quota.h>
93 static void qc_info_to_text(struct printbuf *out, struct qc_info *i)
95 printbuf_tabstops_reset(out);
96 printbuf_tabstop_push(out, 20);
98 prt_str(out, "i_fieldmask");
100 prt_printf(out, "%x", i->i_fieldmask);
103 prt_str(out, "i_flags");
105 prt_printf(out, "%u", i->i_flags);
108 prt_str(out, "i_spc_timelimit");
110 prt_printf(out, "%u", i->i_spc_timelimit);
113 prt_str(out, "i_ino_timelimit");
115 prt_printf(out, "%u", i->i_ino_timelimit);
118 prt_str(out, "i_rt_spc_timelimit");
120 prt_printf(out, "%u", i->i_rt_spc_timelimit);
123 prt_str(out, "i_spc_warnlimit");
125 prt_printf(out, "%u", i->i_spc_warnlimit);
128 prt_str(out, "i_ino_warnlimit");
130 prt_printf(out, "%u", i->i_ino_warnlimit);
133 prt_str(out, "i_rt_spc_warnlimit");
135 prt_printf(out, "%u", i->i_rt_spc_warnlimit);
139 static void qc_dqblk_to_text(struct printbuf *out, struct qc_dqblk *q)
141 printbuf_tabstops_reset(out);
142 printbuf_tabstop_push(out, 20);
144 prt_str(out, "d_fieldmask");
146 prt_printf(out, "%x", q->d_fieldmask);
149 prt_str(out, "d_spc_hardlimit");
151 prt_printf(out, "%llu", q->d_spc_hardlimit);
154 prt_str(out, "d_spc_softlimit");
156 prt_printf(out, "%llu", q->d_spc_softlimit);
159 prt_str(out, "d_ino_hardlimit");
161 prt_printf(out, "%llu", q->d_ino_hardlimit);
164 prt_str(out, "d_ino_softlimit");
166 prt_printf(out, "%llu", q->d_ino_softlimit);
169 prt_str(out, "d_space");
171 prt_printf(out, "%llu", q->d_space);
174 prt_str(out, "d_ino_count");
176 prt_printf(out, "%llu", q->d_ino_count);
179 prt_str(out, "d_ino_timer");
181 prt_printf(out, "%llu", q->d_ino_timer);
184 prt_str(out, "d_spc_timer");
186 prt_printf(out, "%llu", q->d_spc_timer);
189 prt_str(out, "d_ino_warns");
191 prt_printf(out, "%i", q->d_ino_warns);
194 prt_str(out, "d_spc_warns");
196 prt_printf(out, "%i", q->d_spc_warns);
200 static inline unsigned __next_qtype(unsigned i, unsigned qtypes)
203 return qtypes ? i + __ffs(qtypes) : QTYP_NR;
206 #define for_each_set_qtype(_c, _i, _q, _qtypes) \
208 (_i = __next_qtype(_i, _qtypes), \
209 _q = &(_c)->quotas[_i], \
213 static bool ignore_hardlimit(struct bch_memquota_type *q)
215 if (capable(CAP_SYS_RESOURCE))
218 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
220 return capable(CAP_SYS_RESOURCE) &&
221 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
222 !(info->dqi_flags & DQF_ROOT_SQUASH));
228 SOFTWARN, /* Softlimit reached */
229 SOFTLONGWARN, /* Grace time expired */
230 HARDWARN, /* Hardlimit reached */
232 HARDBELOW, /* Usage got below inode hardlimit */
233 SOFTBELOW, /* Usage got below inode softlimit */
236 static int quota_nl[][Q_COUNTERS] = {
237 [HARDWARN][Q_SPC] = QUOTA_NL_BHARDWARN,
238 [SOFTLONGWARN][Q_SPC] = QUOTA_NL_BSOFTLONGWARN,
239 [SOFTWARN][Q_SPC] = QUOTA_NL_BSOFTWARN,
240 [HARDBELOW][Q_SPC] = QUOTA_NL_BHARDBELOW,
241 [SOFTBELOW][Q_SPC] = QUOTA_NL_BSOFTBELOW,
243 [HARDWARN][Q_INO] = QUOTA_NL_IHARDWARN,
244 [SOFTLONGWARN][Q_INO] = QUOTA_NL_ISOFTLONGWARN,
245 [SOFTWARN][Q_INO] = QUOTA_NL_ISOFTWARN,
246 [HARDBELOW][Q_INO] = QUOTA_NL_IHARDBELOW,
247 [SOFTBELOW][Q_INO] = QUOTA_NL_ISOFTBELOW,
255 } m[QTYP_NR * Q_COUNTERS];
258 static void prepare_msg(unsigned qtype,
259 enum quota_counters counter,
260 struct quota_msgs *msgs,
261 enum quota_msg msg_type)
263 BUG_ON(msgs->nr >= ARRAY_SIZE(msgs->m));
265 msgs->m[msgs->nr].qtype = qtype;
266 msgs->m[msgs->nr].msg = quota_nl[msg_type][counter];
270 static void prepare_warning(struct memquota_counter *qc,
272 enum quota_counters counter,
273 struct quota_msgs *msgs,
274 enum quota_msg msg_type)
276 if (qc->warning_issued & (1 << msg_type))
279 prepare_msg(qtype, counter, msgs, msg_type);
282 static void flush_warnings(struct bch_qid qid,
283 struct super_block *sb,
284 struct quota_msgs *msgs)
288 for (i = 0; i < msgs->nr; i++)
289 quota_send_warning(make_kqid(&init_user_ns, msgs->m[i].qtype, qid.q[i]),
290 sb->s_dev, msgs->m[i].msg);
293 static int bch2_quota_check_limit(struct bch_fs *c,
295 struct bch_memquota *mq,
296 struct quota_msgs *msgs,
297 enum quota_counters counter,
299 enum quota_acct_mode mode)
301 struct bch_memquota_type *q = &c->quotas[qtype];
302 struct memquota_counter *qc = &mq->c[counter];
307 if (mode == KEY_TYPE_QUOTA_NOCHECK)
311 if (n < qc->hardlimit &&
312 (qc->warning_issued & (1 << HARDWARN))) {
313 qc->warning_issued &= ~(1 << HARDWARN);
314 prepare_msg(qtype, counter, msgs, HARDBELOW);
317 if (n < qc->softlimit &&
318 (qc->warning_issued & (1 << SOFTWARN))) {
319 qc->warning_issued &= ~(1 << SOFTWARN);
320 prepare_msg(qtype, counter, msgs, SOFTBELOW);
323 qc->warning_issued = 0;
329 !ignore_hardlimit(q)) {
330 prepare_warning(qc, qtype, counter, msgs, HARDWARN);
336 if (qc->timer == 0) {
337 qc->timer = ktime_get_real_seconds() + q->limits[counter].timelimit;
338 prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
339 } else if (ktime_get_real_seconds() >= qc->timer &&
340 !ignore_hardlimit(q)) {
341 prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
349 int bch2_quota_acct(struct bch_fs *c, struct bch_qid qid,
350 enum quota_counters counter, s64 v,
351 enum quota_acct_mode mode)
353 unsigned qtypes = enabled_qtypes(c);
354 struct bch_memquota_type *q;
355 struct bch_memquota *mq[QTYP_NR];
356 struct quota_msgs msgs;
360 memset(&msgs, 0, sizeof(msgs));
362 for_each_set_qtype(c, i, q, qtypes) {
363 mq[i] = genradix_ptr_alloc(&q->table, qid.q[i], GFP_KERNEL);
368 for_each_set_qtype(c, i, q, qtypes)
369 mutex_lock_nested(&q->lock, i);
371 for_each_set_qtype(c, i, q, qtypes) {
372 ret = bch2_quota_check_limit(c, i, mq[i], &msgs, counter, v, mode);
377 for_each_set_qtype(c, i, q, qtypes)
378 mq[i]->c[counter].v += v;
380 for_each_set_qtype(c, i, q, qtypes)
381 mutex_unlock(&q->lock);
383 flush_warnings(qid, c->vfs_sb, &msgs);
388 static void __bch2_quota_transfer(struct bch_memquota *src_q,
389 struct bch_memquota *dst_q,
390 enum quota_counters counter, s64 v)
392 BUG_ON(v > src_q->c[counter].v);
393 BUG_ON(v + dst_q->c[counter].v < v);
395 src_q->c[counter].v -= v;
396 dst_q->c[counter].v += v;
399 int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
401 struct bch_qid src, u64 space,
402 enum quota_acct_mode mode)
404 struct bch_memquota_type *q;
405 struct bch_memquota *src_q[3], *dst_q[3];
406 struct quota_msgs msgs;
410 qtypes &= enabled_qtypes(c);
412 memset(&msgs, 0, sizeof(msgs));
414 for_each_set_qtype(c, i, q, qtypes) {
415 src_q[i] = genradix_ptr_alloc(&q->table, src.q[i], GFP_KERNEL);
416 dst_q[i] = genradix_ptr_alloc(&q->table, dst.q[i], GFP_KERNEL);
417 if (!src_q[i] || !dst_q[i])
421 for_each_set_qtype(c, i, q, qtypes)
422 mutex_lock_nested(&q->lock, i);
424 for_each_set_qtype(c, i, q, qtypes) {
425 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_SPC,
426 dst_q[i]->c[Q_SPC].v + space,
431 ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_INO,
432 dst_q[i]->c[Q_INO].v + 1,
438 for_each_set_qtype(c, i, q, qtypes) {
439 __bch2_quota_transfer(src_q[i], dst_q[i], Q_SPC, space);
440 __bch2_quota_transfer(src_q[i], dst_q[i], Q_INO, 1);
444 for_each_set_qtype(c, i, q, qtypes)
445 mutex_unlock(&q->lock);
447 flush_warnings(dst, c->vfs_sb, &msgs);
452 static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k,
453 struct qc_dqblk *qdq)
455 struct bkey_s_c_quota dq;
456 struct bch_memquota_type *q;
457 struct bch_memquota *mq;
460 BUG_ON(k.k->p.inode >= QTYP_NR);
462 if (!((1U << k.k->p.inode) & enabled_qtypes(c)))
467 dq = bkey_s_c_to_quota(k);
468 q = &c->quotas[k.k->p.inode];
470 mutex_lock(&q->lock);
471 mq = genradix_ptr_alloc(&q->table, k.k->p.offset, GFP_KERNEL);
473 mutex_unlock(&q->lock);
477 for (i = 0; i < Q_COUNTERS; i++) {
478 mq->c[i].hardlimit = le64_to_cpu(dq.v->c[i].hardlimit);
479 mq->c[i].softlimit = le64_to_cpu(dq.v->c[i].softlimit);
482 if (qdq && qdq->d_fieldmask & QC_SPC_TIMER)
483 mq->c[Q_SPC].timer = cpu_to_le64(qdq->d_spc_timer);
484 if (qdq && qdq->d_fieldmask & QC_SPC_WARNS)
485 mq->c[Q_SPC].warns = cpu_to_le64(qdq->d_spc_warns);
486 if (qdq && qdq->d_fieldmask & QC_INO_TIMER)
487 mq->c[Q_INO].timer = cpu_to_le64(qdq->d_ino_timer);
488 if (qdq && qdq->d_fieldmask & QC_INO_WARNS)
489 mq->c[Q_INO].warns = cpu_to_le64(qdq->d_ino_warns);
491 mutex_unlock(&q->lock);
497 void bch2_fs_quota_exit(struct bch_fs *c)
501 for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
502 genradix_free(&c->quotas[i].table);
505 void bch2_fs_quota_init(struct bch_fs *c)
509 for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
510 mutex_init(&c->quotas[i].lock);
513 static struct bch_sb_field_quota *bch2_sb_get_or_create_quota(struct bch_sb_handle *sb)
515 struct bch_sb_field_quota *sb_quota = bch2_sb_get_quota(sb->sb);
520 sb_quota = bch2_sb_resize_quota(sb, sizeof(*sb_quota) / sizeof(u64));
524 for (qtype = 0; qtype < QTYP_NR; qtype++)
525 for (qc = 0; qc < Q_COUNTERS; qc++)
526 sb_quota->q[qtype].c[qc].timelimit =
527 cpu_to_le32(7 * 24 * 60 * 60);
533 static void bch2_sb_quota_read(struct bch_fs *c)
535 struct bch_sb_field_quota *sb_quota;
538 sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
542 for (i = 0; i < QTYP_NR; i++) {
543 struct bch_memquota_type *q = &c->quotas[i];
545 for (j = 0; j < Q_COUNTERS; j++) {
546 q->limits[j].timelimit =
547 le32_to_cpu(sb_quota->q[i].c[j].timelimit);
548 q->limits[j].warnlimit =
549 le32_to_cpu(sb_quota->q[i].c[j].warnlimit);
554 static int bch2_fs_quota_read_inode(struct btree_trans *trans,
555 struct btree_iter *iter,
558 struct bch_fs *c = trans->c;
559 struct bch_inode_unpacked u;
560 struct bch_snapshot_tree s_t;
563 ret = bch2_snapshot_tree_lookup(trans,
564 snapshot_t(c, k.k->p.snapshot)->tree, &s_t);
565 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
566 "%s: snapshot tree %u not found", __func__,
567 snapshot_t(c, k.k->p.snapshot)->tree);
571 if (!s_t.master_subvol)
574 ret = bch2_inode_find_by_inum_trans(trans,
576 le32_to_cpu(s_t.master_subvol),
582 bch2_quota_acct(c, bch_qid(&u), Q_SPC, u.bi_sectors,
583 KEY_TYPE_QUOTA_NOCHECK);
584 bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
585 KEY_TYPE_QUOTA_NOCHECK);
587 bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos));
591 int bch2_fs_quota_read(struct bch_fs *c)
593 struct bch_sb_field_quota *sb_quota;
594 struct btree_trans trans;
595 struct btree_iter iter;
599 mutex_lock(&c->sb_lock);
600 sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
602 mutex_unlock(&c->sb_lock);
603 return -BCH_ERR_ENOSPC_sb_quota;
606 bch2_sb_quota_read(c);
607 mutex_unlock(&c->sb_lock);
609 bch2_trans_init(&trans, c, 0, 0);
611 ret = for_each_btree_key2(&trans, iter, BTREE_ID_quotas,
612 POS_MIN, BTREE_ITER_PREFETCH, k,
613 __bch2_quota_set(c, k, NULL)) ?:
614 for_each_btree_key2(&trans, iter, BTREE_ID_inodes,
615 POS_MIN, BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
616 bch2_fs_quota_read_inode(&trans, &iter, k));
618 bch_err(c, "err in quota_read: %s", bch2_err_str(ret));
620 bch2_trans_exit(&trans);
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;
780 struct bch_memquota_type *q;
784 struct printbuf buf = PRINTBUF;
786 qc_info_to_text(&buf, info);
787 pr_info("setting:\n%s", buf.buf);
791 if (sb->s_flags & SB_RDONLY)
797 if (!((1 << type) & enabled_qtypes(c)))
800 if (info->i_fieldmask &
801 ~(QC_SPC_TIMER|QC_INO_TIMER|QC_SPC_WARNS|QC_INO_WARNS))
804 q = &c->quotas[type];
806 mutex_lock(&c->sb_lock);
807 sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
809 ret = -BCH_ERR_ENOSPC_sb_quota;
813 if (info->i_fieldmask & QC_SPC_TIMER)
814 sb_quota->q[type].c[Q_SPC].timelimit =
815 cpu_to_le32(info->i_spc_timelimit);
817 if (info->i_fieldmask & QC_SPC_WARNS)
818 sb_quota->q[type].c[Q_SPC].warnlimit =
819 cpu_to_le32(info->i_spc_warnlimit);
821 if (info->i_fieldmask & QC_INO_TIMER)
822 sb_quota->q[type].c[Q_INO].timelimit =
823 cpu_to_le32(info->i_ino_timelimit);
825 if (info->i_fieldmask & QC_INO_WARNS)
826 sb_quota->q[type].c[Q_INO].warnlimit =
827 cpu_to_le32(info->i_ino_warnlimit);
829 bch2_sb_quota_read(c);
833 mutex_unlock(&c->sb_lock);
835 return bch2_err_class(ret);
838 /* Get/set individual quotas: */
840 static void __bch2_quota_get(struct qc_dqblk *dst, struct bch_memquota *src)
842 dst->d_space = src->c[Q_SPC].v << 9;
843 dst->d_spc_hardlimit = src->c[Q_SPC].hardlimit << 9;
844 dst->d_spc_softlimit = src->c[Q_SPC].softlimit << 9;
845 dst->d_spc_timer = src->c[Q_SPC].timer;
846 dst->d_spc_warns = src->c[Q_SPC].warns;
848 dst->d_ino_count = src->c[Q_INO].v;
849 dst->d_ino_hardlimit = src->c[Q_INO].hardlimit;
850 dst->d_ino_softlimit = src->c[Q_INO].softlimit;
851 dst->d_ino_timer = src->c[Q_INO].timer;
852 dst->d_ino_warns = src->c[Q_INO].warns;
855 static int bch2_get_quota(struct super_block *sb, struct kqid kqid,
856 struct qc_dqblk *qdq)
858 struct bch_fs *c = sb->s_fs_info;
859 struct bch_memquota_type *q = &c->quotas[kqid.type];
860 qid_t qid = from_kqid(&init_user_ns, kqid);
861 struct bch_memquota *mq;
863 memset(qdq, 0, sizeof(*qdq));
865 mutex_lock(&q->lock);
866 mq = genradix_ptr(&q->table, qid);
868 __bch2_quota_get(qdq, mq);
869 mutex_unlock(&q->lock);
874 static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
875 struct qc_dqblk *qdq)
877 struct bch_fs *c = sb->s_fs_info;
878 struct bch_memquota_type *q = &c->quotas[kqid->type];
879 qid_t qid = from_kqid(&init_user_ns, *kqid);
880 struct genradix_iter iter;
881 struct bch_memquota *mq;
884 mutex_lock(&q->lock);
886 genradix_for_each_from(&q->table, iter, mq, qid)
887 if (memcmp(mq, page_address(ZERO_PAGE(0)), sizeof(*mq))) {
888 __bch2_quota_get(qdq, mq);
889 *kqid = make_kqid(current_user_ns(), kqid->type, iter.pos);
895 mutex_unlock(&q->lock);
899 static int bch2_set_quota_trans(struct btree_trans *trans,
900 struct bkey_i_quota *new_quota,
901 struct qc_dqblk *qdq)
903 struct btree_iter iter;
907 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
908 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
913 if (k.k->type == KEY_TYPE_quota)
914 new_quota->v = *bkey_s_c_to_quota(k).v;
916 if (qdq->d_fieldmask & QC_SPC_SOFT)
917 new_quota->v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
918 if (qdq->d_fieldmask & QC_SPC_HARD)
919 new_quota->v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
921 if (qdq->d_fieldmask & QC_INO_SOFT)
922 new_quota->v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
923 if (qdq->d_fieldmask & QC_INO_HARD)
924 new_quota->v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
926 ret = bch2_trans_update(trans, &iter, &new_quota->k_i, 0);
927 bch2_trans_iter_exit(trans, &iter);
931 static int bch2_set_quota(struct super_block *sb, struct kqid qid,
932 struct qc_dqblk *qdq)
934 struct bch_fs *c = sb->s_fs_info;
935 struct bkey_i_quota new_quota;
939 struct printbuf buf = PRINTBUF;
941 qc_dqblk_to_text(&buf, qdq);
942 pr_info("setting:\n%s", buf.buf);
946 if (sb->s_flags & SB_RDONLY)
949 bkey_quota_init(&new_quota.k_i);
950 new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
952 ret = bch2_trans_do(c, NULL, NULL, 0,
953 bch2_set_quota_trans(&trans, &new_quota, qdq)) ?:
954 __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i), qdq);
959 const struct quotactl_ops bch2_quotactl_operations = {
960 .quota_enable = bch2_quota_enable,
961 .quota_disable = bch2_quota_disable,
962 .rm_xquota = bch2_quota_remove,
964 .get_state = bch2_quota_get_state,
965 .set_info = bch2_quota_set_info,
967 .get_dqblk = bch2_get_quota,
968 .get_nextdqblk = bch2_get_next_quota,
969 .set_dqblk = bch2_set_quota,
972 #endif /* CONFIG_BCACHEFS_QUOTA */