xref: /linux/fs/bcachefs/quota.c (revision c1e822754cc7f28b98c6897d62e8b47b4001e422)
11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0
21c6fdbd8SKent Overstreet #include "bcachefs.h"
31c6fdbd8SKent Overstreet #include "btree_update.h"
4d4bf5eecSKent Overstreet #include "errcode.h"
5cb1b479dSKent Overstreet #include "error.h"
61c6fdbd8SKent Overstreet #include "inode.h"
71c6fdbd8SKent Overstreet #include "quota.h"
88e877caaSKent Overstreet #include "snapshot.h"
91c6fdbd8SKent Overstreet #include "super-io.h"
101c6fdbd8SKent Overstreet 
1112bf93a4SKent Overstreet static const char * const bch2_quota_types[] = {
1212bf93a4SKent Overstreet 	"user",
1312bf93a4SKent Overstreet 	"group",
1412bf93a4SKent Overstreet 	"project",
1512bf93a4SKent Overstreet };
1612bf93a4SKent Overstreet 
1712bf93a4SKent Overstreet static const char * const bch2_quota_counters[] = {
1812bf93a4SKent Overstreet 	"space",
1912bf93a4SKent Overstreet 	"inodes",
2012bf93a4SKent Overstreet };
2112bf93a4SKent Overstreet 
bch2_sb_quota_validate(struct bch_sb * sb,struct bch_sb_field * f,enum bch_validate_flags flags,struct printbuf * err)2212bf93a4SKent Overstreet static int bch2_sb_quota_validate(struct bch_sb *sb, struct bch_sb_field *f,
23a5c3e265SKent Overstreet 				  enum bch_validate_flags flags, struct printbuf *err)
241c6fdbd8SKent Overstreet {
251c6fdbd8SKent Overstreet 	struct bch_sb_field_quota *q = field_to_type(f, quota);
261c6fdbd8SKent Overstreet 
27efe68e1dSKent Overstreet 	if (vstruct_bytes(&q->field) < sizeof(*q)) {
28401ec4dbSKent Overstreet 		prt_printf(err, "wrong size (got %zu should be %zu)",
29efe68e1dSKent Overstreet 		       vstruct_bytes(&q->field), sizeof(*q));
3078c0b75cSKent Overstreet 		return -BCH_ERR_invalid_sb_quota;
31efe68e1dSKent Overstreet 	}
321c6fdbd8SKent Overstreet 
33efe68e1dSKent Overstreet 	return 0;
341c6fdbd8SKent Overstreet }
351c6fdbd8SKent Overstreet 
bch2_sb_quota_to_text(struct printbuf * out,struct bch_sb * sb,struct bch_sb_field * f)3612bf93a4SKent Overstreet static void bch2_sb_quota_to_text(struct printbuf *out, struct bch_sb *sb,
3712bf93a4SKent Overstreet 				  struct bch_sb_field *f)
3812bf93a4SKent Overstreet {
3912bf93a4SKent Overstreet 	struct bch_sb_field_quota *q = field_to_type(f, quota);
4012bf93a4SKent Overstreet 	unsigned qtyp, counter;
4112bf93a4SKent Overstreet 
4212bf93a4SKent Overstreet 	for (qtyp = 0; qtyp < ARRAY_SIZE(q->q); qtyp++) {
43401ec4dbSKent Overstreet 		prt_printf(out, "%s: flags %llx",
4412bf93a4SKent Overstreet 		       bch2_quota_types[qtyp],
4512bf93a4SKent Overstreet 		       le64_to_cpu(q->q[qtyp].flags));
4612bf93a4SKent Overstreet 
4712bf93a4SKent Overstreet 		for (counter = 0; counter < Q_COUNTERS; counter++)
48401ec4dbSKent Overstreet 			prt_printf(out, " %s timelimit %u warnlimit %u",
4912bf93a4SKent Overstreet 			       bch2_quota_counters[counter],
5012bf93a4SKent Overstreet 			       le32_to_cpu(q->q[qtyp].c[counter].timelimit),
5112bf93a4SKent Overstreet 			       le32_to_cpu(q->q[qtyp].c[counter].warnlimit));
5212bf93a4SKent Overstreet 
53401ec4dbSKent Overstreet 		prt_newline(out);
5412bf93a4SKent Overstreet 	}
5512bf93a4SKent Overstreet }
5612bf93a4SKent Overstreet 
571c6fdbd8SKent Overstreet const struct bch_sb_field_ops bch_sb_field_ops_quota = {
5812bf93a4SKent Overstreet 	.validate	= bch2_sb_quota_validate,
5912bf93a4SKent Overstreet 	.to_text	= bch2_sb_quota_to_text,
601c6fdbd8SKent Overstreet };
611c6fdbd8SKent Overstreet 
bch2_quota_validate(struct bch_fs * c,struct bkey_s_c k,enum bch_validate_flags flags)62d97de0d0SKent Overstreet int bch2_quota_validate(struct bch_fs *c, struct bkey_s_c k,
63d97de0d0SKent Overstreet 			enum bch_validate_flags flags)
641c6fdbd8SKent Overstreet {
65b65db750SKent Overstreet 	int ret = 0;
661c6fdbd8SKent Overstreet 
67d97de0d0SKent Overstreet 	bkey_fsck_err_on(k.k->p.inode >= QTYP_NR,
68d97de0d0SKent Overstreet 			 c, quota_type_invalid,
69b65db750SKent Overstreet 			 "invalid quota type (%llu >= %u)",
70b65db750SKent Overstreet 			 k.k->p.inode, QTYP_NR);
71b65db750SKent Overstreet fsck_err:
72b65db750SKent Overstreet 	return ret;
731c6fdbd8SKent Overstreet }
741c6fdbd8SKent Overstreet 
bch2_quota_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)75319f9ac3SKent Overstreet void bch2_quota_to_text(struct printbuf *out, struct bch_fs *c,
76319f9ac3SKent Overstreet 			struct bkey_s_c k)
771c6fdbd8SKent Overstreet {
7826609b61SKent Overstreet 	struct bkey_s_c_quota dq = bkey_s_c_to_quota(k);
791c6fdbd8SKent Overstreet 	unsigned i;
801c6fdbd8SKent Overstreet 
811c6fdbd8SKent Overstreet 	for (i = 0; i < Q_COUNTERS; i++)
82401ec4dbSKent Overstreet 		prt_printf(out, "%s hardlimit %llu softlimit %llu",
831c6fdbd8SKent Overstreet 		       bch2_quota_counters[i],
841c6fdbd8SKent Overstreet 		       le64_to_cpu(dq.v->c[i].hardlimit),
851c6fdbd8SKent Overstreet 		       le64_to_cpu(dq.v->c[i].softlimit));
861c6fdbd8SKent Overstreet }
871c6fdbd8SKent Overstreet 
881c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA
891c6fdbd8SKent Overstreet 
901c6fdbd8SKent Overstreet #include <linux/cred.h>
911c6fdbd8SKent Overstreet #include <linux/fs.h>
921c6fdbd8SKent Overstreet #include <linux/quota.h>
931c6fdbd8SKent Overstreet 
qc_info_to_text(struct printbuf * out,struct qc_info * i)94f866870fSKent Overstreet static void qc_info_to_text(struct printbuf *out, struct qc_info *i)
95f866870fSKent Overstreet {
96f866870fSKent Overstreet 	printbuf_tabstops_reset(out);
97f866870fSKent Overstreet 	printbuf_tabstop_push(out, 20);
98f866870fSKent Overstreet 
997423330eSKent Overstreet 	prt_printf(out, "i_fieldmask\t%x\n",		i->i_fieldmask);
1007423330eSKent Overstreet 	prt_printf(out, "i_flags\t%u\n",		i->i_flags);
1017423330eSKent Overstreet 	prt_printf(out, "i_spc_timelimit\t%u\n",	i->i_spc_timelimit);
1027423330eSKent Overstreet 	prt_printf(out, "i_ino_timelimit\t%u\n",	i->i_ino_timelimit);
1037423330eSKent Overstreet 	prt_printf(out, "i_rt_spc_timelimit\t%u\n",	i->i_rt_spc_timelimit);
1047423330eSKent Overstreet 	prt_printf(out, "i_spc_warnlimit\t%u\n",	i->i_spc_warnlimit);
1057423330eSKent Overstreet 	prt_printf(out, "i_ino_warnlimit\t%u\n",	i->i_ino_warnlimit);
1067423330eSKent Overstreet 	prt_printf(out, "i_rt_spc_warnlimit\t%u\n",	i->i_rt_spc_warnlimit);
107f866870fSKent Overstreet }
108f866870fSKent Overstreet 
qc_dqblk_to_text(struct printbuf * out,struct qc_dqblk * q)109f866870fSKent Overstreet static void qc_dqblk_to_text(struct printbuf *out, struct qc_dqblk *q)
110f866870fSKent Overstreet {
111f866870fSKent Overstreet 	printbuf_tabstops_reset(out);
112f866870fSKent Overstreet 	printbuf_tabstop_push(out, 20);
113f866870fSKent Overstreet 
1147423330eSKent Overstreet 	prt_printf(out, "d_fieldmask\t%x\n",		q->d_fieldmask);
1157423330eSKent Overstreet 	prt_printf(out, "d_spc_hardlimit\t%llu\n",	q->d_spc_hardlimit);
1167423330eSKent Overstreet 	prt_printf(out, "d_spc_softlimit\t%llu\n",	q->d_spc_softlimit);
1177423330eSKent Overstreet 	prt_printf(out, "d_ino_hardlimit\%llu\n",	q->d_ino_hardlimit);
1187423330eSKent Overstreet 	prt_printf(out, "d_ino_softlimit\t%llu\n",	q->d_ino_softlimit);
1197423330eSKent Overstreet 	prt_printf(out, "d_space\t%llu\n",		q->d_space);
1207423330eSKent Overstreet 	prt_printf(out, "d_ino_count\t%llu\n",		q->d_ino_count);
1217423330eSKent Overstreet 	prt_printf(out, "d_ino_timer\t%llu\n",		q->d_ino_timer);
1227423330eSKent Overstreet 	prt_printf(out, "d_spc_timer\t%llu\n",		q->d_spc_timer);
1237423330eSKent Overstreet 	prt_printf(out, "d_ino_warns\t%i\n",		q->d_ino_warns);
1247423330eSKent Overstreet 	prt_printf(out, "d_spc_warns\t%i\n",		q->d_spc_warns);
125f866870fSKent Overstreet }
126f866870fSKent Overstreet 
__next_qtype(unsigned i,unsigned qtypes)1271c6fdbd8SKent Overstreet static inline unsigned __next_qtype(unsigned i, unsigned qtypes)
1281c6fdbd8SKent Overstreet {
1291c6fdbd8SKent Overstreet 	qtypes >>= i;
1301c6fdbd8SKent Overstreet 	return qtypes ? i + __ffs(qtypes) : QTYP_NR;
1311c6fdbd8SKent Overstreet }
1321c6fdbd8SKent Overstreet 
1331c6fdbd8SKent Overstreet #define for_each_set_qtype(_c, _i, _q, _qtypes)				\
1341c6fdbd8SKent Overstreet 	for (_i = 0;							\
1351c6fdbd8SKent Overstreet 	     (_i = __next_qtype(_i, _qtypes),				\
1361c6fdbd8SKent Overstreet 	      _q = &(_c)->quotas[_i],					\
1371c6fdbd8SKent Overstreet 	      _i < QTYP_NR);						\
1381c6fdbd8SKent Overstreet 	     _i++)
1391c6fdbd8SKent Overstreet 
ignore_hardlimit(struct bch_memquota_type * q)1401c6fdbd8SKent Overstreet static bool ignore_hardlimit(struct bch_memquota_type *q)
1411c6fdbd8SKent Overstreet {
1421c6fdbd8SKent Overstreet 	if (capable(CAP_SYS_RESOURCE))
1431c6fdbd8SKent Overstreet 		return true;
1441c6fdbd8SKent Overstreet #if 0
1451c6fdbd8SKent Overstreet 	struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1461c6fdbd8SKent Overstreet 
1471c6fdbd8SKent Overstreet 	return capable(CAP_SYS_RESOURCE) &&
1481c6fdbd8SKent Overstreet 	       (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1491c6fdbd8SKent Overstreet 		!(info->dqi_flags & DQF_ROOT_SQUASH));
1501c6fdbd8SKent Overstreet #endif
1511c6fdbd8SKent Overstreet 	return false;
1521c6fdbd8SKent Overstreet }
1531c6fdbd8SKent Overstreet 
1541c6fdbd8SKent Overstreet enum quota_msg {
1551c6fdbd8SKent Overstreet 	SOFTWARN,	/* Softlimit reached */
1561c6fdbd8SKent Overstreet 	SOFTLONGWARN,	/* Grace time expired */
1571c6fdbd8SKent Overstreet 	HARDWARN,	/* Hardlimit reached */
1581c6fdbd8SKent Overstreet 
1591c6fdbd8SKent Overstreet 	HARDBELOW,	/* Usage got below inode hardlimit */
1601c6fdbd8SKent Overstreet 	SOFTBELOW,	/* Usage got below inode softlimit */
1611c6fdbd8SKent Overstreet };
1621c6fdbd8SKent Overstreet 
1631c6fdbd8SKent Overstreet static int quota_nl[][Q_COUNTERS] = {
1641c6fdbd8SKent Overstreet 	[HARDWARN][Q_SPC]	= QUOTA_NL_BHARDWARN,
1651c6fdbd8SKent Overstreet 	[SOFTLONGWARN][Q_SPC]	= QUOTA_NL_BSOFTLONGWARN,
1661c6fdbd8SKent Overstreet 	[SOFTWARN][Q_SPC]	= QUOTA_NL_BSOFTWARN,
1671c6fdbd8SKent Overstreet 	[HARDBELOW][Q_SPC]	= QUOTA_NL_BHARDBELOW,
1681c6fdbd8SKent Overstreet 	[SOFTBELOW][Q_SPC]	= QUOTA_NL_BSOFTBELOW,
1691c6fdbd8SKent Overstreet 
1701c6fdbd8SKent Overstreet 	[HARDWARN][Q_INO]	= QUOTA_NL_IHARDWARN,
1711c6fdbd8SKent Overstreet 	[SOFTLONGWARN][Q_INO]	= QUOTA_NL_ISOFTLONGWARN,
1721c6fdbd8SKent Overstreet 	[SOFTWARN][Q_INO]	= QUOTA_NL_ISOFTWARN,
1731c6fdbd8SKent Overstreet 	[HARDBELOW][Q_INO]	= QUOTA_NL_IHARDBELOW,
1741c6fdbd8SKent Overstreet 	[SOFTBELOW][Q_INO]	= QUOTA_NL_ISOFTBELOW,
1751c6fdbd8SKent Overstreet };
1761c6fdbd8SKent Overstreet 
1771c6fdbd8SKent Overstreet struct quota_msgs {
1781c6fdbd8SKent Overstreet 	u8		nr;
1791c6fdbd8SKent Overstreet 	struct {
1801c6fdbd8SKent Overstreet 		u8	qtype;
1811c6fdbd8SKent Overstreet 		u8	msg;
1821c6fdbd8SKent Overstreet 	}		m[QTYP_NR * Q_COUNTERS];
1831c6fdbd8SKent Overstreet };
1841c6fdbd8SKent Overstreet 
prepare_msg(unsigned qtype,enum quota_counters counter,struct quota_msgs * msgs,enum quota_msg msg_type)1851c6fdbd8SKent Overstreet static void prepare_msg(unsigned qtype,
1861c6fdbd8SKent Overstreet 			enum quota_counters counter,
1871c6fdbd8SKent Overstreet 			struct quota_msgs *msgs,
1881c6fdbd8SKent Overstreet 			enum quota_msg msg_type)
1891c6fdbd8SKent Overstreet {
1901c6fdbd8SKent Overstreet 	BUG_ON(msgs->nr >= ARRAY_SIZE(msgs->m));
1911c6fdbd8SKent Overstreet 
1921c6fdbd8SKent Overstreet 	msgs->m[msgs->nr].qtype	= qtype;
1931c6fdbd8SKent Overstreet 	msgs->m[msgs->nr].msg	= quota_nl[msg_type][counter];
1941c6fdbd8SKent Overstreet 	msgs->nr++;
1951c6fdbd8SKent Overstreet }
1961c6fdbd8SKent Overstreet 
prepare_warning(struct memquota_counter * qc,unsigned qtype,enum quota_counters counter,struct quota_msgs * msgs,enum quota_msg msg_type)1971c6fdbd8SKent Overstreet static void prepare_warning(struct memquota_counter *qc,
1981c6fdbd8SKent Overstreet 			    unsigned qtype,
1991c6fdbd8SKent Overstreet 			    enum quota_counters counter,
2001c6fdbd8SKent Overstreet 			    struct quota_msgs *msgs,
2011c6fdbd8SKent Overstreet 			    enum quota_msg msg_type)
2021c6fdbd8SKent Overstreet {
2031c6fdbd8SKent Overstreet 	if (qc->warning_issued & (1 << msg_type))
2041c6fdbd8SKent Overstreet 		return;
2051c6fdbd8SKent Overstreet 
2061c6fdbd8SKent Overstreet 	prepare_msg(qtype, counter, msgs, msg_type);
2071c6fdbd8SKent Overstreet }
2081c6fdbd8SKent Overstreet 
flush_warnings(struct bch_qid qid,struct super_block * sb,struct quota_msgs * msgs)2091c6fdbd8SKent Overstreet static void flush_warnings(struct bch_qid qid,
2101c6fdbd8SKent Overstreet 			   struct super_block *sb,
2111c6fdbd8SKent Overstreet 			   struct quota_msgs *msgs)
2121c6fdbd8SKent Overstreet {
2131c6fdbd8SKent Overstreet 	unsigned i;
2141c6fdbd8SKent Overstreet 
2151c6fdbd8SKent Overstreet 	for (i = 0; i < msgs->nr; i++)
2161c6fdbd8SKent Overstreet 		quota_send_warning(make_kqid(&init_user_ns, msgs->m[i].qtype, qid.q[i]),
2171c6fdbd8SKent Overstreet 				   sb->s_dev, msgs->m[i].msg);
2181c6fdbd8SKent Overstreet }
2191c6fdbd8SKent Overstreet 
bch2_quota_check_limit(struct bch_fs * c,unsigned qtype,struct bch_memquota * mq,struct quota_msgs * msgs,enum quota_counters counter,s64 v,enum quota_acct_mode mode)2201c6fdbd8SKent Overstreet static int bch2_quota_check_limit(struct bch_fs *c,
2211c6fdbd8SKent Overstreet 				  unsigned qtype,
2221c6fdbd8SKent Overstreet 				  struct bch_memquota *mq,
2231c6fdbd8SKent Overstreet 				  struct quota_msgs *msgs,
2241c6fdbd8SKent Overstreet 				  enum quota_counters counter,
2251c6fdbd8SKent Overstreet 				  s64 v,
2261c6fdbd8SKent Overstreet 				  enum quota_acct_mode mode)
2271c6fdbd8SKent Overstreet {
2281c6fdbd8SKent Overstreet 	struct bch_memquota_type *q = &c->quotas[qtype];
2291c6fdbd8SKent Overstreet 	struct memquota_counter *qc = &mq->c[counter];
2301c6fdbd8SKent Overstreet 	u64 n = qc->v + v;
2311c6fdbd8SKent Overstreet 
2321c6fdbd8SKent Overstreet 	BUG_ON((s64) n < 0);
2331c6fdbd8SKent Overstreet 
23426609b61SKent Overstreet 	if (mode == KEY_TYPE_QUOTA_NOCHECK)
2351c6fdbd8SKent Overstreet 		return 0;
2361c6fdbd8SKent Overstreet 
2371c6fdbd8SKent Overstreet 	if (v <= 0) {
2381c6fdbd8SKent Overstreet 		if (n < qc->hardlimit &&
2391c6fdbd8SKent Overstreet 		    (qc->warning_issued & (1 << HARDWARN))) {
2401c6fdbd8SKent Overstreet 			qc->warning_issued &= ~(1 << HARDWARN);
2411c6fdbd8SKent Overstreet 			prepare_msg(qtype, counter, msgs, HARDBELOW);
2421c6fdbd8SKent Overstreet 		}
2431c6fdbd8SKent Overstreet 
2441c6fdbd8SKent Overstreet 		if (n < qc->softlimit &&
2451c6fdbd8SKent Overstreet 		    (qc->warning_issued & (1 << SOFTWARN))) {
2461c6fdbd8SKent Overstreet 			qc->warning_issued &= ~(1 << SOFTWARN);
2471c6fdbd8SKent Overstreet 			prepare_msg(qtype, counter, msgs, SOFTBELOW);
2481c6fdbd8SKent Overstreet 		}
2491c6fdbd8SKent Overstreet 
2501c6fdbd8SKent Overstreet 		qc->warning_issued = 0;
2511c6fdbd8SKent Overstreet 		return 0;
2521c6fdbd8SKent Overstreet 	}
2531c6fdbd8SKent Overstreet 
2541c6fdbd8SKent Overstreet 	if (qc->hardlimit &&
2551c6fdbd8SKent Overstreet 	    qc->hardlimit < n &&
2561c6fdbd8SKent Overstreet 	    !ignore_hardlimit(q)) {
2571c6fdbd8SKent Overstreet 		prepare_warning(qc, qtype, counter, msgs, HARDWARN);
258bd954215SKent Overstreet 		return -EDQUOT;
2591c6fdbd8SKent Overstreet 	}
2601c6fdbd8SKent Overstreet 
2611c6fdbd8SKent Overstreet 	if (qc->softlimit &&
262bd954215SKent Overstreet 	    qc->softlimit < n) {
263bd954215SKent Overstreet 		if (qc->timer == 0) {
264bd954215SKent Overstreet 			qc->timer = ktime_get_real_seconds() + q->limits[counter].timelimit;
2651c6fdbd8SKent Overstreet 			prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
266bd954215SKent Overstreet 		} else if (ktime_get_real_seconds() >= qc->timer &&
267bd954215SKent Overstreet 			   !ignore_hardlimit(q)) {
268bd954215SKent Overstreet 			prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
269bd954215SKent Overstreet 			return -EDQUOT;
270bd954215SKent Overstreet 		}
2711c6fdbd8SKent Overstreet 	}
2721c6fdbd8SKent Overstreet 
2731c6fdbd8SKent Overstreet 	return 0;
2741c6fdbd8SKent Overstreet }
2751c6fdbd8SKent Overstreet 
bch2_quota_acct(struct bch_fs * c,struct bch_qid qid,enum quota_counters counter,s64 v,enum quota_acct_mode mode)2761c6fdbd8SKent Overstreet int bch2_quota_acct(struct bch_fs *c, struct bch_qid qid,
2771c6fdbd8SKent Overstreet 		    enum quota_counters counter, s64 v,
2781c6fdbd8SKent Overstreet 		    enum quota_acct_mode mode)
2791c6fdbd8SKent Overstreet {
2801c6fdbd8SKent Overstreet 	unsigned qtypes = enabled_qtypes(c);
2811c6fdbd8SKent Overstreet 	struct bch_memquota_type *q;
2821c6fdbd8SKent Overstreet 	struct bch_memquota *mq[QTYP_NR];
2831c6fdbd8SKent Overstreet 	struct quota_msgs msgs;
2841c6fdbd8SKent Overstreet 	unsigned i;
2851c6fdbd8SKent Overstreet 	int ret = 0;
2861c6fdbd8SKent Overstreet 
2871c6fdbd8SKent Overstreet 	memset(&msgs, 0, sizeof(msgs));
2881c6fdbd8SKent Overstreet 
28984fea8e5SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes) {
29084fea8e5SKent Overstreet 		mq[i] = genradix_ptr_alloc(&q->table, qid.q[i], GFP_KERNEL);
29184fea8e5SKent Overstreet 		if (!mq[i])
29284fea8e5SKent Overstreet 			return -ENOMEM;
29384fea8e5SKent Overstreet 	}
29484fea8e5SKent Overstreet 
2951c6fdbd8SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes)
2961c6fdbd8SKent Overstreet 		mutex_lock_nested(&q->lock, i);
2971c6fdbd8SKent Overstreet 
2981c6fdbd8SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes) {
2991c6fdbd8SKent Overstreet 		ret = bch2_quota_check_limit(c, i, mq[i], &msgs, counter, v, mode);
3001c6fdbd8SKent Overstreet 		if (ret)
3011c6fdbd8SKent Overstreet 			goto err;
3021c6fdbd8SKent Overstreet 	}
3031c6fdbd8SKent Overstreet 
3041c6fdbd8SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes)
3051c6fdbd8SKent Overstreet 		mq[i]->c[counter].v += v;
3061c6fdbd8SKent Overstreet err:
3071c6fdbd8SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes)
3081c6fdbd8SKent Overstreet 		mutex_unlock(&q->lock);
3091c6fdbd8SKent Overstreet 
3101c6fdbd8SKent Overstreet 	flush_warnings(qid, c->vfs_sb, &msgs);
3111c6fdbd8SKent Overstreet 
3121c6fdbd8SKent Overstreet 	return ret;
3131c6fdbd8SKent Overstreet }
3141c6fdbd8SKent Overstreet 
__bch2_quota_transfer(struct bch_memquota * src_q,struct bch_memquota * dst_q,enum quota_counters counter,s64 v)3151c6fdbd8SKent Overstreet static void __bch2_quota_transfer(struct bch_memquota *src_q,
3161c6fdbd8SKent Overstreet 				  struct bch_memquota *dst_q,
3171c6fdbd8SKent Overstreet 				  enum quota_counters counter, s64 v)
3181c6fdbd8SKent Overstreet {
3191c6fdbd8SKent Overstreet 	BUG_ON(v > src_q->c[counter].v);
3201c6fdbd8SKent Overstreet 	BUG_ON(v + dst_q->c[counter].v < v);
3211c6fdbd8SKent Overstreet 
3221c6fdbd8SKent Overstreet 	src_q->c[counter].v -= v;
3231c6fdbd8SKent Overstreet 	dst_q->c[counter].v += v;
3241c6fdbd8SKent Overstreet }
3251c6fdbd8SKent Overstreet 
bch2_quota_transfer(struct bch_fs * c,unsigned qtypes,struct bch_qid dst,struct bch_qid src,u64 space,enum quota_acct_mode mode)3261c6fdbd8SKent Overstreet int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
3271c6fdbd8SKent Overstreet 			struct bch_qid dst,
3280f5254aaSKent Overstreet 			struct bch_qid src, u64 space,
3290f5254aaSKent Overstreet 			enum quota_acct_mode mode)
3301c6fdbd8SKent Overstreet {
3311c6fdbd8SKent Overstreet 	struct bch_memquota_type *q;
3321c6fdbd8SKent Overstreet 	struct bch_memquota *src_q[3], *dst_q[3];
3331c6fdbd8SKent Overstreet 	struct quota_msgs msgs;
3341c6fdbd8SKent Overstreet 	unsigned i;
3351c6fdbd8SKent Overstreet 	int ret = 0;
3361c6fdbd8SKent Overstreet 
3371c6fdbd8SKent Overstreet 	qtypes &= enabled_qtypes(c);
3381c6fdbd8SKent Overstreet 
3391c6fdbd8SKent Overstreet 	memset(&msgs, 0, sizeof(msgs));
3401c6fdbd8SKent Overstreet 
34184fea8e5SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes) {
34284fea8e5SKent Overstreet 		src_q[i] = genradix_ptr_alloc(&q->table, src.q[i], GFP_KERNEL);
34384fea8e5SKent Overstreet 		dst_q[i] = genradix_ptr_alloc(&q->table, dst.q[i], GFP_KERNEL);
34484fea8e5SKent Overstreet 		if (!src_q[i] || !dst_q[i])
34584fea8e5SKent Overstreet 			return -ENOMEM;
34684fea8e5SKent Overstreet 	}
34784fea8e5SKent Overstreet 
3481c6fdbd8SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes)
3491c6fdbd8SKent Overstreet 		mutex_lock_nested(&q->lock, i);
3501c6fdbd8SKent Overstreet 
3511c6fdbd8SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes) {
3521c6fdbd8SKent Overstreet 		ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_SPC,
3531c6fdbd8SKent Overstreet 					     dst_q[i]->c[Q_SPC].v + space,
3540f5254aaSKent Overstreet 					     mode);
3551c6fdbd8SKent Overstreet 		if (ret)
3561c6fdbd8SKent Overstreet 			goto err;
3571c6fdbd8SKent Overstreet 
3581c6fdbd8SKent Overstreet 		ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_INO,
3591c6fdbd8SKent Overstreet 					     dst_q[i]->c[Q_INO].v + 1,
3600f5254aaSKent Overstreet 					     mode);
3611c6fdbd8SKent Overstreet 		if (ret)
3621c6fdbd8SKent Overstreet 			goto err;
3631c6fdbd8SKent Overstreet 	}
3641c6fdbd8SKent Overstreet 
3651c6fdbd8SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes) {
3661c6fdbd8SKent Overstreet 		__bch2_quota_transfer(src_q[i], dst_q[i], Q_SPC, space);
3671c6fdbd8SKent Overstreet 		__bch2_quota_transfer(src_q[i], dst_q[i], Q_INO, 1);
3681c6fdbd8SKent Overstreet 	}
3691c6fdbd8SKent Overstreet 
3701c6fdbd8SKent Overstreet err:
3711c6fdbd8SKent Overstreet 	for_each_set_qtype(c, i, q, qtypes)
3721c6fdbd8SKent Overstreet 		mutex_unlock(&q->lock);
3731c6fdbd8SKent Overstreet 
3741c6fdbd8SKent Overstreet 	flush_warnings(dst, c->vfs_sb, &msgs);
3751c6fdbd8SKent Overstreet 
3761c6fdbd8SKent Overstreet 	return ret;
3771c6fdbd8SKent Overstreet }
3781c6fdbd8SKent Overstreet 
__bch2_quota_set(struct bch_fs * c,struct bkey_s_c k,struct qc_dqblk * qdq)379bd954215SKent Overstreet static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k,
380bd954215SKent Overstreet 			    struct qc_dqblk *qdq)
3811c6fdbd8SKent Overstreet {
3821c6fdbd8SKent Overstreet 	struct bkey_s_c_quota dq;
3831c6fdbd8SKent Overstreet 	struct bch_memquota_type *q;
3841c6fdbd8SKent Overstreet 	struct bch_memquota *mq;
3851c6fdbd8SKent Overstreet 	unsigned i;
3861c6fdbd8SKent Overstreet 
3871c6fdbd8SKent Overstreet 	BUG_ON(k.k->p.inode >= QTYP_NR);
3881c6fdbd8SKent Overstreet 
3891329c7ceSKent Overstreet 	if (!((1U << k.k->p.inode) & enabled_qtypes(c)))
3901329c7ceSKent Overstreet 		return 0;
3911329c7ceSKent Overstreet 
3921c6fdbd8SKent Overstreet 	switch (k.k->type) {
39326609b61SKent Overstreet 	case KEY_TYPE_quota:
3941c6fdbd8SKent Overstreet 		dq = bkey_s_c_to_quota(k);
3951c6fdbd8SKent Overstreet 		q = &c->quotas[k.k->p.inode];
3961c6fdbd8SKent Overstreet 
3971c6fdbd8SKent Overstreet 		mutex_lock(&q->lock);
3981c6fdbd8SKent Overstreet 		mq = genradix_ptr_alloc(&q->table, k.k->p.offset, GFP_KERNEL);
3991c6fdbd8SKent Overstreet 		if (!mq) {
4001c6fdbd8SKent Overstreet 			mutex_unlock(&q->lock);
4011c6fdbd8SKent Overstreet 			return -ENOMEM;
4021c6fdbd8SKent Overstreet 		}
4031c6fdbd8SKent Overstreet 
4041c6fdbd8SKent Overstreet 		for (i = 0; i < Q_COUNTERS; i++) {
4051c6fdbd8SKent Overstreet 			mq->c[i].hardlimit = le64_to_cpu(dq.v->c[i].hardlimit);
4061c6fdbd8SKent Overstreet 			mq->c[i].softlimit = le64_to_cpu(dq.v->c[i].softlimit);
4071c6fdbd8SKent Overstreet 		}
4081c6fdbd8SKent Overstreet 
409bd954215SKent Overstreet 		if (qdq && qdq->d_fieldmask & QC_SPC_TIMER)
41073bd774dSKent Overstreet 			mq->c[Q_SPC].timer	= qdq->d_spc_timer;
411bd954215SKent Overstreet 		if (qdq && qdq->d_fieldmask & QC_SPC_WARNS)
41273bd774dSKent Overstreet 			mq->c[Q_SPC].warns	= qdq->d_spc_warns;
413bd954215SKent Overstreet 		if (qdq && qdq->d_fieldmask & QC_INO_TIMER)
41473bd774dSKent Overstreet 			mq->c[Q_INO].timer	= qdq->d_ino_timer;
415bd954215SKent Overstreet 		if (qdq && qdq->d_fieldmask & QC_INO_WARNS)
41673bd774dSKent Overstreet 			mq->c[Q_INO].warns	= qdq->d_ino_warns;
417bd954215SKent Overstreet 
4181c6fdbd8SKent Overstreet 		mutex_unlock(&q->lock);
4191c6fdbd8SKent Overstreet 	}
4201c6fdbd8SKent Overstreet 
4211c6fdbd8SKent Overstreet 	return 0;
4221c6fdbd8SKent Overstreet }
4231c6fdbd8SKent Overstreet 
bch2_fs_quota_exit(struct bch_fs * c)4241c6fdbd8SKent Overstreet void bch2_fs_quota_exit(struct bch_fs *c)
4251c6fdbd8SKent Overstreet {
4261c6fdbd8SKent Overstreet 	unsigned i;
4271c6fdbd8SKent Overstreet 
4281c6fdbd8SKent Overstreet 	for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
4291c6fdbd8SKent Overstreet 		genradix_free(&c->quotas[i].table);
4301c6fdbd8SKent Overstreet }
4311c6fdbd8SKent Overstreet 
bch2_fs_quota_init(struct bch_fs * c)4321c6fdbd8SKent Overstreet void bch2_fs_quota_init(struct bch_fs *c)
4331c6fdbd8SKent Overstreet {
4341c6fdbd8SKent Overstreet 	unsigned i;
4351c6fdbd8SKent Overstreet 
4361c6fdbd8SKent Overstreet 	for (i = 0; i < ARRAY_SIZE(c->quotas); i++)
4371c6fdbd8SKent Overstreet 		mutex_init(&c->quotas[i].lock);
4381c6fdbd8SKent Overstreet }
4391c6fdbd8SKent Overstreet 
bch2_sb_get_or_create_quota(struct bch_sb_handle * sb)440f866870fSKent Overstreet static struct bch_sb_field_quota *bch2_sb_get_or_create_quota(struct bch_sb_handle *sb)
441f866870fSKent Overstreet {
4424637429eSKent Overstreet 	struct bch_sb_field_quota *sb_quota = bch2_sb_field_get(sb->sb, quota);
443f866870fSKent Overstreet 
444f866870fSKent Overstreet 	if (sb_quota)
445f866870fSKent Overstreet 		return sb_quota;
446f866870fSKent Overstreet 
4474637429eSKent Overstreet 	sb_quota = bch2_sb_field_resize(sb, quota, sizeof(*sb_quota) / sizeof(u64));
448f866870fSKent Overstreet 	if (sb_quota) {
449f866870fSKent Overstreet 		unsigned qtype, qc;
450f866870fSKent Overstreet 
451f866870fSKent Overstreet 		for (qtype = 0; qtype < QTYP_NR; qtype++)
452f866870fSKent Overstreet 			for (qc = 0; qc < Q_COUNTERS; qc++)
453f866870fSKent Overstreet 				sb_quota->q[qtype].c[qc].timelimit =
454f866870fSKent Overstreet 					cpu_to_le32(7 * 24 * 60 * 60);
455f866870fSKent Overstreet 	}
456f866870fSKent Overstreet 
457f866870fSKent Overstreet 	return sb_quota;
458f866870fSKent Overstreet }
459f866870fSKent Overstreet 
bch2_sb_quota_read(struct bch_fs * c)4601c6fdbd8SKent Overstreet static void bch2_sb_quota_read(struct bch_fs *c)
4611c6fdbd8SKent Overstreet {
4621c6fdbd8SKent Overstreet 	struct bch_sb_field_quota *sb_quota;
4631c6fdbd8SKent Overstreet 	unsigned i, j;
4641c6fdbd8SKent Overstreet 
4654637429eSKent Overstreet 	sb_quota = bch2_sb_field_get(c->disk_sb.sb, quota);
4661c6fdbd8SKent Overstreet 	if (!sb_quota)
4671c6fdbd8SKent Overstreet 		return;
4681c6fdbd8SKent Overstreet 
4691c6fdbd8SKent Overstreet 	for (i = 0; i < QTYP_NR; i++) {
4701c6fdbd8SKent Overstreet 		struct bch_memquota_type *q = &c->quotas[i];
4711c6fdbd8SKent Overstreet 
4721c6fdbd8SKent Overstreet 		for (j = 0; j < Q_COUNTERS; j++) {
4731c6fdbd8SKent Overstreet 			q->limits[j].timelimit =
4741c6fdbd8SKent Overstreet 				le32_to_cpu(sb_quota->q[i].c[j].timelimit);
4751c6fdbd8SKent Overstreet 			q->limits[j].warnlimit =
4761c6fdbd8SKent Overstreet 				le32_to_cpu(sb_quota->q[i].c[j].warnlimit);
4771c6fdbd8SKent Overstreet 		}
4781c6fdbd8SKent Overstreet 	}
4791c6fdbd8SKent Overstreet }
4801c6fdbd8SKent Overstreet 
bch2_fs_quota_read_inode(struct btree_trans * trans,struct btree_iter * iter,struct bkey_s_c k)4819ca4853bSKent Overstreet static int bch2_fs_quota_read_inode(struct btree_trans *trans,
482a1783320SKent Overstreet 				    struct btree_iter *iter,
483a1783320SKent Overstreet 				    struct bkey_s_c k)
4849ca4853bSKent Overstreet {
4859ca4853bSKent Overstreet 	struct bch_fs *c = trans->c;
4869ca4853bSKent Overstreet 	struct bch_inode_unpacked u;
487cb1b479dSKent Overstreet 	struct bch_snapshot_tree s_t;
4888060bf1dSKent Overstreet 	u32 tree = bch2_snapshot_tree(c, k.k->p.snapshot);
4899ca4853bSKent Overstreet 
4908060bf1dSKent Overstreet 	int ret = bch2_snapshot_tree_lookup(trans, tree, &s_t);
491cb1b479dSKent Overstreet 	bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
4928060bf1dSKent Overstreet 			"%s: snapshot tree %u not found", __func__, tree);
4939ca4853bSKent Overstreet 	if (ret)
4949ca4853bSKent Overstreet 		return ret;
4959ca4853bSKent Overstreet 
496cb1b479dSKent Overstreet 	if (!s_t.master_subvol)
4979ca4853bSKent Overstreet 		goto advance;
4989ca4853bSKent Overstreet 
499da187cacSKent Overstreet 	ret = bch2_inode_find_by_inum_nowarn_trans(trans,
500cb1b479dSKent Overstreet 				(subvol_inum) {
501cb1b479dSKent Overstreet 					le32_to_cpu(s_t.master_subvol),
502cb1b479dSKent Overstreet 					k.k->p.offset,
503cb1b479dSKent Overstreet 				}, &u);
504e7ffda56SKent Overstreet 	/*
505e7ffda56SKent Overstreet 	 * Inode might be deleted in this snapshot - the easiest way to handle
506e7ffda56SKent Overstreet 	 * that is to just skip it here:
507e7ffda56SKent Overstreet 	 */
508e7ffda56SKent Overstreet 	if (bch2_err_matches(ret, ENOENT))
509e7ffda56SKent Overstreet 		goto advance;
510e7ffda56SKent Overstreet 
5119ca4853bSKent Overstreet 	if (ret)
5129ca4853bSKent Overstreet 		return ret;
5139ca4853bSKent Overstreet 
5149ca4853bSKent Overstreet 	bch2_quota_acct(c, bch_qid(&u), Q_SPC, u.bi_sectors,
5159ca4853bSKent Overstreet 			KEY_TYPE_QUOTA_NOCHECK);
5169ca4853bSKent Overstreet 	bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
5179ca4853bSKent Overstreet 			KEY_TYPE_QUOTA_NOCHECK);
5189ca4853bSKent Overstreet advance:
519cb1b479dSKent Overstreet 	bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos));
5209ca4853bSKent Overstreet 	return 0;
5219ca4853bSKent Overstreet }
5229ca4853bSKent Overstreet 
bch2_fs_quota_read(struct bch_fs * c)5231c6fdbd8SKent Overstreet int bch2_fs_quota_read(struct bch_fs *c)
5241c6fdbd8SKent Overstreet {
5251c6fdbd8SKent Overstreet 
5261c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
52780eab7a7SKent Overstreet 	struct bch_sb_field_quota *sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
528f866870fSKent Overstreet 	if (!sb_quota) {
529f866870fSKent Overstreet 		mutex_unlock(&c->sb_lock);
530f866870fSKent Overstreet 		return -BCH_ERR_ENOSPC_sb_quota;
531f866870fSKent Overstreet 	}
532f866870fSKent Overstreet 
5331c6fdbd8SKent Overstreet 	bch2_sb_quota_read(c);
5341c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
5351c6fdbd8SKent Overstreet 
53680eab7a7SKent Overstreet 	int ret = bch2_trans_run(c,
53780eab7a7SKent Overstreet 		for_each_btree_key(trans, iter, BTREE_ID_quotas, POS_MIN,
5385dd8c60eSKent Overstreet 				   BTREE_ITER_prefetch, k,
539bd954215SKent Overstreet 			__bch2_quota_set(c, k, NULL)) ?:
5405028b907SKent Overstreet 		for_each_btree_key(trans, iter, BTREE_ID_inodes, POS_MIN,
5415dd8c60eSKent Overstreet 				   BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
54280eab7a7SKent Overstreet 			bch2_fs_quota_read_inode(trans, &iter, k)));
5431bb3c2a9SKent Overstreet 	bch_err_fn(c, ret);
544a1783320SKent Overstreet 	return ret;
5451c6fdbd8SKent Overstreet }
5461c6fdbd8SKent Overstreet 
5471c6fdbd8SKent Overstreet /* Enable/disable/delete quotas for an entire filesystem: */
5481c6fdbd8SKent Overstreet 
bch2_quota_enable(struct super_block * sb,unsigned uflags)5491c6fdbd8SKent Overstreet static int bch2_quota_enable(struct super_block	*sb, unsigned uflags)
5501c6fdbd8SKent Overstreet {
5511c6fdbd8SKent Overstreet 	struct bch_fs *c = sb->s_fs_info;
552f866870fSKent Overstreet 	struct bch_sb_field_quota *sb_quota;
553f866870fSKent Overstreet 	int ret = 0;
5541c6fdbd8SKent Overstreet 
5551c6fdbd8SKent Overstreet 	if (sb->s_flags & SB_RDONLY)
5561c6fdbd8SKent Overstreet 		return -EROFS;
5571c6fdbd8SKent Overstreet 
5581c6fdbd8SKent Overstreet 	/* Accounting must be enabled at mount time: */
5591c6fdbd8SKent Overstreet 	if (uflags & (FS_QUOTA_UDQ_ACCT|FS_QUOTA_GDQ_ACCT|FS_QUOTA_PDQ_ACCT))
5601c6fdbd8SKent Overstreet 		return -EINVAL;
5611c6fdbd8SKent Overstreet 
5621c6fdbd8SKent Overstreet 	/* Can't enable enforcement without accounting: */
5631c6fdbd8SKent Overstreet 	if ((uflags & FS_QUOTA_UDQ_ENFD) && !c->opts.usrquota)
5641c6fdbd8SKent Overstreet 		return -EINVAL;
5651c6fdbd8SKent Overstreet 
5661c6fdbd8SKent Overstreet 	if ((uflags & FS_QUOTA_GDQ_ENFD) && !c->opts.grpquota)
5671c6fdbd8SKent Overstreet 		return -EINVAL;
5681c6fdbd8SKent Overstreet 
5691c6fdbd8SKent Overstreet 	if (uflags & FS_QUOTA_PDQ_ENFD && !c->opts.prjquota)
5701c6fdbd8SKent Overstreet 		return -EINVAL;
5711c6fdbd8SKent Overstreet 
5721c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
573f866870fSKent Overstreet 	sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
574f866870fSKent Overstreet 	if (!sb_quota) {
575f866870fSKent Overstreet 		ret = -BCH_ERR_ENOSPC_sb_quota;
576f866870fSKent Overstreet 		goto unlock;
577f866870fSKent Overstreet 	}
578f866870fSKent Overstreet 
5791c6fdbd8SKent Overstreet 	if (uflags & FS_QUOTA_UDQ_ENFD)
5801c6fdbd8SKent Overstreet 		SET_BCH_SB_USRQUOTA(c->disk_sb.sb, true);
5811c6fdbd8SKent Overstreet 
5821c6fdbd8SKent Overstreet 	if (uflags & FS_QUOTA_GDQ_ENFD)
5831c6fdbd8SKent Overstreet 		SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, true);
5841c6fdbd8SKent Overstreet 
5851c6fdbd8SKent Overstreet 	if (uflags & FS_QUOTA_PDQ_ENFD)
5861c6fdbd8SKent Overstreet 		SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, true);
5871c6fdbd8SKent Overstreet 
5881c6fdbd8SKent Overstreet 	bch2_write_super(c);
589f866870fSKent Overstreet unlock:
5901c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
5911c6fdbd8SKent Overstreet 
592f866870fSKent Overstreet 	return bch2_err_class(ret);
5931c6fdbd8SKent Overstreet }
5941c6fdbd8SKent Overstreet 
bch2_quota_disable(struct super_block * sb,unsigned uflags)5951c6fdbd8SKent Overstreet static int bch2_quota_disable(struct super_block *sb, unsigned uflags)
5961c6fdbd8SKent Overstreet {
5971c6fdbd8SKent Overstreet 	struct bch_fs *c = sb->s_fs_info;
5981c6fdbd8SKent Overstreet 
5991c6fdbd8SKent Overstreet 	if (sb->s_flags & SB_RDONLY)
6001c6fdbd8SKent Overstreet 		return -EROFS;
6011c6fdbd8SKent Overstreet 
6021c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
6031c6fdbd8SKent Overstreet 	if (uflags & FS_QUOTA_UDQ_ENFD)
6041c6fdbd8SKent Overstreet 		SET_BCH_SB_USRQUOTA(c->disk_sb.sb, false);
6051c6fdbd8SKent Overstreet 
6061c6fdbd8SKent Overstreet 	if (uflags & FS_QUOTA_GDQ_ENFD)
6071c6fdbd8SKent Overstreet 		SET_BCH_SB_GRPQUOTA(c->disk_sb.sb, false);
6081c6fdbd8SKent Overstreet 
6091c6fdbd8SKent Overstreet 	if (uflags & FS_QUOTA_PDQ_ENFD)
6101c6fdbd8SKent Overstreet 		SET_BCH_SB_PRJQUOTA(c->disk_sb.sb, false);
6111c6fdbd8SKent Overstreet 
6121c6fdbd8SKent Overstreet 	bch2_write_super(c);
6131c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
6141c6fdbd8SKent Overstreet 
6151c6fdbd8SKent Overstreet 	return 0;
6161c6fdbd8SKent Overstreet }
6171c6fdbd8SKent Overstreet 
bch2_quota_remove(struct super_block * sb,unsigned uflags)6181c6fdbd8SKent Overstreet static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
6191c6fdbd8SKent Overstreet {
6201c6fdbd8SKent Overstreet 	struct bch_fs *c = sb->s_fs_info;
6211c6fdbd8SKent Overstreet 	int ret;
6221c6fdbd8SKent Overstreet 
6231c6fdbd8SKent Overstreet 	if (sb->s_flags & SB_RDONLY)
6241c6fdbd8SKent Overstreet 		return -EROFS;
6251c6fdbd8SKent Overstreet 
6261c6fdbd8SKent Overstreet 	if (uflags & FS_USER_QUOTA) {
6271c6fdbd8SKent Overstreet 		if (c->opts.usrquota)
6281c6fdbd8SKent Overstreet 			return -EINVAL;
6291c6fdbd8SKent Overstreet 
63041f8b09eSKent Overstreet 		ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
6311c6fdbd8SKent Overstreet 					      POS(QTYP_USR, 0),
632c72f687aSKent Overstreet 					      POS(QTYP_USR, U64_MAX),
633d248ee56SKent Overstreet 					      0, NULL);
6341c6fdbd8SKent Overstreet 		if (ret)
6351c6fdbd8SKent Overstreet 			return ret;
6361c6fdbd8SKent Overstreet 	}
6371c6fdbd8SKent Overstreet 
6381c6fdbd8SKent Overstreet 	if (uflags & FS_GROUP_QUOTA) {
6391c6fdbd8SKent Overstreet 		if (c->opts.grpquota)
6401c6fdbd8SKent Overstreet 			return -EINVAL;
6411c6fdbd8SKent Overstreet 
64241f8b09eSKent Overstreet 		ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
6431c6fdbd8SKent Overstreet 					      POS(QTYP_GRP, 0),
644c72f687aSKent Overstreet 					      POS(QTYP_GRP, U64_MAX),
645d248ee56SKent Overstreet 					      0, NULL);
6461c6fdbd8SKent Overstreet 		if (ret)
6471c6fdbd8SKent Overstreet 			return ret;
6481c6fdbd8SKent Overstreet 	}
6491c6fdbd8SKent Overstreet 
6501c6fdbd8SKent Overstreet 	if (uflags & FS_PROJ_QUOTA) {
6511c6fdbd8SKent Overstreet 		if (c->opts.prjquota)
6521c6fdbd8SKent Overstreet 			return -EINVAL;
6531c6fdbd8SKent Overstreet 
65441f8b09eSKent Overstreet 		ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
6551c6fdbd8SKent Overstreet 					      POS(QTYP_PRJ, 0),
656c72f687aSKent Overstreet 					      POS(QTYP_PRJ, U64_MAX),
657d248ee56SKent Overstreet 					      0, NULL);
6581c6fdbd8SKent Overstreet 		if (ret)
6591c6fdbd8SKent Overstreet 			return ret;
6601c6fdbd8SKent Overstreet 	}
6611c6fdbd8SKent Overstreet 
6621c6fdbd8SKent Overstreet 	return 0;
6631c6fdbd8SKent Overstreet }
6641c6fdbd8SKent Overstreet 
6651c6fdbd8SKent Overstreet /*
6661c6fdbd8SKent Overstreet  * Return quota status information, such as enforcements, quota file inode
6671c6fdbd8SKent Overstreet  * numbers etc.
6681c6fdbd8SKent Overstreet  */
bch2_quota_get_state(struct super_block * sb,struct qc_state * state)6691c6fdbd8SKent Overstreet static int bch2_quota_get_state(struct super_block *sb, struct qc_state *state)
6701c6fdbd8SKent Overstreet {
6711c6fdbd8SKent Overstreet 	struct bch_fs *c = sb->s_fs_info;
6721c6fdbd8SKent Overstreet 	unsigned qtypes = enabled_qtypes(c);
6731c6fdbd8SKent Overstreet 	unsigned i;
6741c6fdbd8SKent Overstreet 
6751c6fdbd8SKent Overstreet 	memset(state, 0, sizeof(*state));
6761c6fdbd8SKent Overstreet 
6771c6fdbd8SKent Overstreet 	for (i = 0; i < QTYP_NR; i++) {
6781c6fdbd8SKent Overstreet 		state->s_state[i].flags |= QCI_SYSFILE;
6791c6fdbd8SKent Overstreet 
6801c6fdbd8SKent Overstreet 		if (!(qtypes & (1 << i)))
6811c6fdbd8SKent Overstreet 			continue;
6821c6fdbd8SKent Overstreet 
6831c6fdbd8SKent Overstreet 		state->s_state[i].flags |= QCI_ACCT_ENABLED;
6841c6fdbd8SKent Overstreet 
6851c6fdbd8SKent Overstreet 		state->s_state[i].spc_timelimit = c->quotas[i].limits[Q_SPC].timelimit;
6861c6fdbd8SKent Overstreet 		state->s_state[i].spc_warnlimit = c->quotas[i].limits[Q_SPC].warnlimit;
6871c6fdbd8SKent Overstreet 
6881c6fdbd8SKent Overstreet 		state->s_state[i].ino_timelimit = c->quotas[i].limits[Q_INO].timelimit;
6891c6fdbd8SKent Overstreet 		state->s_state[i].ino_warnlimit = c->quotas[i].limits[Q_INO].warnlimit;
6901c6fdbd8SKent Overstreet 	}
6911c6fdbd8SKent Overstreet 
6921c6fdbd8SKent Overstreet 	return 0;
6931c6fdbd8SKent Overstreet }
6941c6fdbd8SKent Overstreet 
6951c6fdbd8SKent Overstreet /*
6961c6fdbd8SKent Overstreet  * Adjust quota timers & warnings
6971c6fdbd8SKent Overstreet  */
bch2_quota_set_info(struct super_block * sb,int type,struct qc_info * info)6981c6fdbd8SKent Overstreet static int bch2_quota_set_info(struct super_block *sb, int type,
6991c6fdbd8SKent Overstreet 			       struct qc_info *info)
7001c6fdbd8SKent Overstreet {
7011c6fdbd8SKent Overstreet 	struct bch_fs *c = sb->s_fs_info;
7021c6fdbd8SKent Overstreet 	struct bch_sb_field_quota *sb_quota;
703f866870fSKent Overstreet 	int ret = 0;
704f866870fSKent Overstreet 
705f866870fSKent Overstreet 	if (0) {
706f866870fSKent Overstreet 		struct printbuf buf = PRINTBUF;
707f866870fSKent Overstreet 
708f866870fSKent Overstreet 		qc_info_to_text(&buf, info);
709f866870fSKent Overstreet 		pr_info("setting:\n%s", buf.buf);
710f866870fSKent Overstreet 		printbuf_exit(&buf);
711f866870fSKent Overstreet 	}
7121c6fdbd8SKent Overstreet 
7131c6fdbd8SKent Overstreet 	if (sb->s_flags & SB_RDONLY)
7141c6fdbd8SKent Overstreet 		return -EROFS;
7151c6fdbd8SKent Overstreet 
7161c6fdbd8SKent Overstreet 	if (type >= QTYP_NR)
7171c6fdbd8SKent Overstreet 		return -EINVAL;
7181c6fdbd8SKent Overstreet 
7191c6fdbd8SKent Overstreet 	if (!((1 << type) & enabled_qtypes(c)))
7201c6fdbd8SKent Overstreet 		return -ESRCH;
7211c6fdbd8SKent Overstreet 
7221c6fdbd8SKent Overstreet 	if (info->i_fieldmask &
7231c6fdbd8SKent Overstreet 	    ~(QC_SPC_TIMER|QC_INO_TIMER|QC_SPC_WARNS|QC_INO_WARNS))
7241c6fdbd8SKent Overstreet 		return -EINVAL;
7251c6fdbd8SKent Overstreet 
7261c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
727f866870fSKent Overstreet 	sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
7281c6fdbd8SKent Overstreet 	if (!sb_quota) {
729f866870fSKent Overstreet 		ret = -BCH_ERR_ENOSPC_sb_quota;
730f866870fSKent Overstreet 		goto unlock;
7311c6fdbd8SKent Overstreet 	}
7321c6fdbd8SKent Overstreet 
7331c6fdbd8SKent Overstreet 	if (info->i_fieldmask & QC_SPC_TIMER)
7341c6fdbd8SKent Overstreet 		sb_quota->q[type].c[Q_SPC].timelimit =
7351c6fdbd8SKent Overstreet 			cpu_to_le32(info->i_spc_timelimit);
7361c6fdbd8SKent Overstreet 
7371c6fdbd8SKent Overstreet 	if (info->i_fieldmask & QC_SPC_WARNS)
7381c6fdbd8SKent Overstreet 		sb_quota->q[type].c[Q_SPC].warnlimit =
7391c6fdbd8SKent Overstreet 			cpu_to_le32(info->i_spc_warnlimit);
7401c6fdbd8SKent Overstreet 
7411c6fdbd8SKent Overstreet 	if (info->i_fieldmask & QC_INO_TIMER)
7421c6fdbd8SKent Overstreet 		sb_quota->q[type].c[Q_INO].timelimit =
7431c6fdbd8SKent Overstreet 			cpu_to_le32(info->i_ino_timelimit);
7441c6fdbd8SKent Overstreet 
7451c6fdbd8SKent Overstreet 	if (info->i_fieldmask & QC_INO_WARNS)
7461c6fdbd8SKent Overstreet 		sb_quota->q[type].c[Q_INO].warnlimit =
7471c6fdbd8SKent Overstreet 			cpu_to_le32(info->i_ino_warnlimit);
7481c6fdbd8SKent Overstreet 
7491c6fdbd8SKent Overstreet 	bch2_sb_quota_read(c);
7501c6fdbd8SKent Overstreet 
7511c6fdbd8SKent Overstreet 	bch2_write_super(c);
752f866870fSKent Overstreet unlock:
7531c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
7541c6fdbd8SKent Overstreet 
755f866870fSKent Overstreet 	return bch2_err_class(ret);
7561c6fdbd8SKent Overstreet }
7571c6fdbd8SKent Overstreet 
7581c6fdbd8SKent Overstreet /* Get/set individual quotas: */
7591c6fdbd8SKent Overstreet 
__bch2_quota_get(struct qc_dqblk * dst,struct bch_memquota * src)7601c6fdbd8SKent Overstreet static void __bch2_quota_get(struct qc_dqblk *dst, struct bch_memquota *src)
7611c6fdbd8SKent Overstreet {
7621c6fdbd8SKent Overstreet 	dst->d_space		= src->c[Q_SPC].v << 9;
7631c6fdbd8SKent Overstreet 	dst->d_spc_hardlimit	= src->c[Q_SPC].hardlimit << 9;
7641c6fdbd8SKent Overstreet 	dst->d_spc_softlimit	= src->c[Q_SPC].softlimit << 9;
7651c6fdbd8SKent Overstreet 	dst->d_spc_timer	= src->c[Q_SPC].timer;
7661c6fdbd8SKent Overstreet 	dst->d_spc_warns	= src->c[Q_SPC].warns;
7671c6fdbd8SKent Overstreet 
7681c6fdbd8SKent Overstreet 	dst->d_ino_count	= src->c[Q_INO].v;
7691c6fdbd8SKent Overstreet 	dst->d_ino_hardlimit	= src->c[Q_INO].hardlimit;
7701c6fdbd8SKent Overstreet 	dst->d_ino_softlimit	= src->c[Q_INO].softlimit;
7711c6fdbd8SKent Overstreet 	dst->d_ino_timer	= src->c[Q_INO].timer;
7721c6fdbd8SKent Overstreet 	dst->d_ino_warns	= src->c[Q_INO].warns;
7731c6fdbd8SKent Overstreet }
7741c6fdbd8SKent Overstreet 
bch2_get_quota(struct super_block * sb,struct kqid kqid,struct qc_dqblk * qdq)7751c6fdbd8SKent Overstreet static int bch2_get_quota(struct super_block *sb, struct kqid kqid,
7761c6fdbd8SKent Overstreet 			  struct qc_dqblk *qdq)
7771c6fdbd8SKent Overstreet {
7781c6fdbd8SKent Overstreet 	struct bch_fs *c		= sb->s_fs_info;
7791c6fdbd8SKent Overstreet 	struct bch_memquota_type *q	= &c->quotas[kqid.type];
7801c6fdbd8SKent Overstreet 	qid_t qid			= from_kqid(&init_user_ns, kqid);
7811c6fdbd8SKent Overstreet 	struct bch_memquota *mq;
7821c6fdbd8SKent Overstreet 
7831c6fdbd8SKent Overstreet 	memset(qdq, 0, sizeof(*qdq));
7841c6fdbd8SKent Overstreet 
7851c6fdbd8SKent Overstreet 	mutex_lock(&q->lock);
7861c6fdbd8SKent Overstreet 	mq = genradix_ptr(&q->table, qid);
7871c6fdbd8SKent Overstreet 	if (mq)
7881c6fdbd8SKent Overstreet 		__bch2_quota_get(qdq, mq);
7891c6fdbd8SKent Overstreet 	mutex_unlock(&q->lock);
7901c6fdbd8SKent Overstreet 
7911c6fdbd8SKent Overstreet 	return 0;
7921c6fdbd8SKent Overstreet }
7931c6fdbd8SKent Overstreet 
bch2_get_next_quota(struct super_block * sb,struct kqid * kqid,struct qc_dqblk * qdq)7941c6fdbd8SKent Overstreet static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
7951c6fdbd8SKent Overstreet 			       struct qc_dqblk *qdq)
7961c6fdbd8SKent Overstreet {
7971c6fdbd8SKent Overstreet 	struct bch_fs *c		= sb->s_fs_info;
7981c6fdbd8SKent Overstreet 	struct bch_memquota_type *q	= &c->quotas[kqid->type];
7991c6fdbd8SKent Overstreet 	qid_t qid			= from_kqid(&init_user_ns, *kqid);
800dfe9bfb3SKent Overstreet 	struct genradix_iter iter;
8011c6fdbd8SKent Overstreet 	struct bch_memquota *mq;
8021c6fdbd8SKent Overstreet 	int ret = 0;
8031c6fdbd8SKent Overstreet 
8041c6fdbd8SKent Overstreet 	mutex_lock(&q->lock);
8051c6fdbd8SKent Overstreet 
806dfe9bfb3SKent Overstreet 	genradix_for_each_from(&q->table, iter, mq, qid)
8071c6fdbd8SKent Overstreet 		if (memcmp(mq, page_address(ZERO_PAGE(0)), sizeof(*mq))) {
8081c6fdbd8SKent Overstreet 			__bch2_quota_get(qdq, mq);
8091c6fdbd8SKent Overstreet 			*kqid = make_kqid(current_user_ns(), kqid->type, iter.pos);
8101c6fdbd8SKent Overstreet 			goto found;
8111c6fdbd8SKent Overstreet 		}
8121c6fdbd8SKent Overstreet 
8131c6fdbd8SKent Overstreet 	ret = -ENOENT;
8141c6fdbd8SKent Overstreet found:
8151c6fdbd8SKent Overstreet 	mutex_unlock(&q->lock);
816e47a390aSKent Overstreet 	return bch2_err_class(ret);
8171c6fdbd8SKent Overstreet }
8181c6fdbd8SKent Overstreet 
bch2_set_quota_trans(struct btree_trans * trans,struct bkey_i_quota * new_quota,struct qc_dqblk * qdq)819d3baf289SKent Overstreet static int bch2_set_quota_trans(struct btree_trans *trans,
820d3baf289SKent Overstreet 				struct bkey_i_quota *new_quota,
821d3baf289SKent Overstreet 				struct qc_dqblk *qdq)
822d3baf289SKent Overstreet {
82367e0dd8fSKent Overstreet 	struct btree_iter iter;
824d3baf289SKent Overstreet 	struct bkey_s_c k;
825d3baf289SKent Overstreet 	int ret;
826d3baf289SKent Overstreet 
827bcb79a51SKent Overstreet 	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
8285dd8c60eSKent Overstreet 			       BTREE_ITER_slots|BTREE_ITER_intent);
829d3baf289SKent Overstreet 	ret = bkey_err(k);
830d3baf289SKent Overstreet 	if (unlikely(ret))
831d3baf289SKent Overstreet 		return ret;
832d3baf289SKent Overstreet 
833d3baf289SKent Overstreet 	if (k.k->type == KEY_TYPE_quota)
834d3baf289SKent Overstreet 		new_quota->v = *bkey_s_c_to_quota(k).v;
835d3baf289SKent Overstreet 
836d3baf289SKent Overstreet 	if (qdq->d_fieldmask & QC_SPC_SOFT)
837d3baf289SKent Overstreet 		new_quota->v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
838d3baf289SKent Overstreet 	if (qdq->d_fieldmask & QC_SPC_HARD)
839d3baf289SKent Overstreet 		new_quota->v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
840d3baf289SKent Overstreet 
841d3baf289SKent Overstreet 	if (qdq->d_fieldmask & QC_INO_SOFT)
842d3baf289SKent Overstreet 		new_quota->v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
843d3baf289SKent Overstreet 	if (qdq->d_fieldmask & QC_INO_HARD)
844d3baf289SKent Overstreet 		new_quota->v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
845d3baf289SKent Overstreet 
84667e0dd8fSKent Overstreet 	ret = bch2_trans_update(trans, &iter, &new_quota->k_i, 0);
84767e0dd8fSKent Overstreet 	bch2_trans_iter_exit(trans, &iter);
84850dc0f69SKent Overstreet 	return ret;
849d3baf289SKent Overstreet }
850d3baf289SKent Overstreet 
bch2_set_quota(struct super_block * sb,struct kqid qid,struct qc_dqblk * qdq)8511c6fdbd8SKent Overstreet static int bch2_set_quota(struct super_block *sb, struct kqid qid,
8521c6fdbd8SKent Overstreet 			  struct qc_dqblk *qdq)
8531c6fdbd8SKent Overstreet {
8541c6fdbd8SKent Overstreet 	struct bch_fs *c = sb->s_fs_info;
8551c6fdbd8SKent Overstreet 	struct bkey_i_quota new_quota;
8561c6fdbd8SKent Overstreet 	int ret;
8571c6fdbd8SKent Overstreet 
858f866870fSKent Overstreet 	if (0) {
859f866870fSKent Overstreet 		struct printbuf buf = PRINTBUF;
860f866870fSKent Overstreet 
861f866870fSKent Overstreet 		qc_dqblk_to_text(&buf, qdq);
862f866870fSKent Overstreet 		pr_info("setting:\n%s", buf.buf);
863f866870fSKent Overstreet 		printbuf_exit(&buf);
864f866870fSKent Overstreet 	}
865f866870fSKent Overstreet 
8661c6fdbd8SKent Overstreet 	if (sb->s_flags & SB_RDONLY)
8671c6fdbd8SKent Overstreet 		return -EROFS;
8681c6fdbd8SKent Overstreet 
8691c6fdbd8SKent Overstreet 	bkey_quota_init(&new_quota.k_i);
8701c6fdbd8SKent Overstreet 	new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
8711c6fdbd8SKent Overstreet 
872*a0d11feeSKent Overstreet 	ret = bch2_trans_commit_do(c, NULL, NULL, 0,
8736bd68ec2SKent Overstreet 			    bch2_set_quota_trans(trans, &new_quota, qdq)) ?:
874bd954215SKent Overstreet 		__bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i), qdq);
8750564b167SKent Overstreet 
876e47a390aSKent Overstreet 	return bch2_err_class(ret);
8771c6fdbd8SKent Overstreet }
8781c6fdbd8SKent Overstreet 
8791c6fdbd8SKent Overstreet const struct quotactl_ops bch2_quotactl_operations = {
8801c6fdbd8SKent Overstreet 	.quota_enable		= bch2_quota_enable,
8811c6fdbd8SKent Overstreet 	.quota_disable		= bch2_quota_disable,
8821c6fdbd8SKent Overstreet 	.rm_xquota		= bch2_quota_remove,
8831c6fdbd8SKent Overstreet 
8841c6fdbd8SKent Overstreet 	.get_state		= bch2_quota_get_state,
8851c6fdbd8SKent Overstreet 	.set_info		= bch2_quota_set_info,
8861c6fdbd8SKent Overstreet 
8871c6fdbd8SKent Overstreet 	.get_dqblk		= bch2_get_quota,
8881c6fdbd8SKent Overstreet 	.get_nextdqblk		= bch2_get_next_quota,
8891c6fdbd8SKent Overstreet 	.set_dqblk		= bch2_set_quota,
8901c6fdbd8SKent Overstreet };
8911c6fdbd8SKent Overstreet 
8921c6fdbd8SKent Overstreet #endif /* CONFIG_BCACHEFS_QUOTA */
893