]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/quota.c
Update bcachefs sources to 3f3f969859 bcachefs: Fix some compiler warnings
[bcachefs-tools-debian] / libbcachefs / quota.c
index 75104ea98027f449196420148578438a022eb05f..9b0f4d3f176d5fcbd7c0e0e189679c40416abefd 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 #include "bcachefs.h"
 #include "btree_update.h"
 #include "inode.h"
@@ -21,23 +22,13 @@ const struct bch_sb_field_ops bch_sb_field_ops_quota = {
 
 const char *bch2_quota_invalid(const struct bch_fs *c, struct bkey_s_c k)
 {
-       struct bkey_s_c_quota dq;
-
        if (k.k->p.inode >= QTYP_NR)
                return "invalid quota type";
 
-       switch (k.k->type) {
-       case BCH_QUOTA: {
-               dq = bkey_s_c_to_quota(k);
-
-               if (bkey_val_bytes(k.k) != sizeof(struct bch_quota))
-                       return "incorrect value size";
+       if (bkey_val_bytes(k.k) != sizeof(struct bch_quota))
+               return "incorrect value size";
 
-               return NULL;
-       }
-       default:
-               return "invalid type";
-       }
+       return NULL;
 }
 
 static const char * const bch2_quota_counters[] = {
@@ -48,20 +39,14 @@ static const char * const bch2_quota_counters[] = {
 void bch2_quota_to_text(struct printbuf *out, struct bch_fs *c,
                        struct bkey_s_c k)
 {
-       struct bkey_s_c_quota dq;
+       struct bkey_s_c_quota dq = bkey_s_c_to_quota(k);
        unsigned i;
 
-       switch (k.k->type) {
-       case BCH_QUOTA:
-               dq = bkey_s_c_to_quota(k);
-
-               for (i = 0; i < Q_COUNTERS; i++)
-                       pr_buf(out, "%s hardlimit %llu softlimit %llu",
-                              bch2_quota_counters[i],
-                              le64_to_cpu(dq.v->c[i].hardlimit),
-                              le64_to_cpu(dq.v->c[i].softlimit));
-               break;
-       }
+       for (i = 0; i < Q_COUNTERS; i++)
+               pr_buf(out, "%s hardlimit %llu softlimit %llu",
+                      bch2_quota_counters[i],
+                      le64_to_cpu(dq.v->c[i].hardlimit),
+                      le64_to_cpu(dq.v->c[i].softlimit));
 }
 
 #ifdef CONFIG_BCACHEFS_QUOTA
@@ -177,7 +162,7 @@ static int bch2_quota_check_limit(struct bch_fs *c,
 
        BUG_ON((s64) n < 0);
 
-       if (mode == BCH_QUOTA_NOCHECK)
+       if (mode == KEY_TYPE_QUOTA_NOCHECK)
                return 0;
 
        if (v <= 0) {
@@ -200,7 +185,7 @@ static int bch2_quota_check_limit(struct bch_fs *c,
        if (qc->hardlimit &&
            qc->hardlimit < n &&
            !ignore_hardlimit(q)) {
-               if (mode == BCH_QUOTA_PREALLOC)
+               if (mode == KEY_TYPE_QUOTA_PREALLOC)
                        return -EDQUOT;
 
                prepare_warning(qc, qtype, counter, msgs, HARDWARN);
@@ -211,7 +196,7 @@ static int bch2_quota_check_limit(struct bch_fs *c,
            qc->timer &&
            ktime_get_real_seconds() >= qc->timer &&
            !ignore_hardlimit(q)) {
-               if (mode == BCH_QUOTA_PREALLOC)
+               if (mode == KEY_TYPE_QUOTA_PREALLOC)
                        return -EDQUOT;
 
                prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
@@ -220,7 +205,7 @@ static int bch2_quota_check_limit(struct bch_fs *c,
        if (qc->softlimit &&
            qc->softlimit < n &&
            qc->timer == 0) {
-               if (mode == BCH_QUOTA_PREALLOC)
+               if (mode == KEY_TYPE_QUOTA_PREALLOC)
                        return -EDQUOT;
 
                prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
@@ -285,7 +270,8 @@ static void __bch2_quota_transfer(struct bch_memquota *src_q,
 
 int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
                        struct bch_qid dst,
-                       struct bch_qid src, u64 space)
+                       struct bch_qid src, u64 space,
+                       enum quota_acct_mode mode)
 {
        struct bch_memquota_type *q;
        struct bch_memquota *src_q[3], *dst_q[3];
@@ -311,13 +297,13 @@ int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
 
                ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_SPC,
                                             dst_q[i]->c[Q_SPC].v + space,
-                                            BCH_QUOTA_PREALLOC);
+                                            mode);
                if (ret)
                        goto err;
 
                ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_INO,
                                             dst_q[i]->c[Q_INO].v + 1,
-                                            BCH_QUOTA_PREALLOC);
+                                            mode);
                if (ret)
                        goto err;
        }
@@ -346,7 +332,7 @@ static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k)
        BUG_ON(k.k->p.inode >= QTYP_NR);
 
        switch (k.k->type) {
-       case BCH_QUOTA:
+       case KEY_TYPE_quota:
                dq = bkey_s_c_to_quota(k);
                q = &c->quotas[k.k->p.inode];
 
@@ -370,12 +356,15 @@ static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k)
 
 static int bch2_quota_init_type(struct bch_fs *c, enum quota_types type)
 {
+       struct btree_trans trans;
        struct btree_iter iter;
        struct bkey_s_c k;
        int ret = 0;
 
-       for_each_btree_key(&iter, c, BTREE_ID_QUOTAS, POS(type, 0),
-                          BTREE_ITER_PREFETCH, k) {
+       bch2_trans_init(&trans, c, 0, 0);
+
+       for_each_btree_key(&trans, iter, BTREE_ID_quotas, POS(type, 0),
+                          BTREE_ITER_PREFETCH, k, ret) {
                if (k.k->p.inode != type)
                        break;
 
@@ -383,8 +372,9 @@ static int bch2_quota_init_type(struct bch_fs *c, enum quota_types type)
                if (ret)
                        break;
        }
+       bch2_trans_iter_exit(&trans, &iter);
 
-       return bch2_btree_iter_unlock(&iter) ?: ret;
+       return bch2_trans_exit(&trans) ?: ret;
 }
 
 void bch2_fs_quota_exit(struct bch_fs *c)
@@ -428,6 +418,7 @@ int bch2_fs_quota_read(struct bch_fs *c)
 {
        unsigned i, qtypes = enabled_qtypes(c);
        struct bch_memquota_type *q;
+       struct btree_trans trans;
        struct btree_iter iter;
        struct bch_inode_unpacked u;
        struct bkey_s_c k;
@@ -443,21 +434,25 @@ int bch2_fs_quota_read(struct bch_fs *c)
                        return ret;
        }
 
-       for_each_btree_key(&iter, c, BTREE_ID_INODES, POS_MIN,
-                          BTREE_ITER_PREFETCH, k) {
+       bch2_trans_init(&trans, c, 0, 0);
+
+       for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
+                          BTREE_ITER_PREFETCH, k, ret) {
                switch (k.k->type) {
-               case BCH_INODE_FS:
+               case KEY_TYPE_inode:
                        ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
                        if (ret)
                                return ret;
 
                        bch2_quota_acct(c, bch_qid(&u), Q_SPC, u.bi_sectors,
-                                       BCH_QUOTA_NOCHECK);
+                                       KEY_TYPE_QUOTA_NOCHECK);
                        bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
-                                       BCH_QUOTA_NOCHECK);
+                                       KEY_TYPE_QUOTA_NOCHECK);
                }
        }
-       return bch2_btree_iter_unlock(&iter) ?: ret;
+       bch2_trans_iter_exit(&trans, &iter);
+
+       return bch2_trans_exit(&trans) ?: ret;
 }
 
 /* Enable/disable/delete quotas for an entire filesystem: */
@@ -466,7 +461,7 @@ static int bch2_quota_enable(struct super_block     *sb, unsigned uflags)
 {
        struct bch_fs *c = sb->s_fs_info;
 
-       if (sb->s_flags & MS_RDONLY)
+       if (sb->s_flags & SB_RDONLY)
                return -EROFS;
 
        /* Accounting must be enabled at mount time: */
@@ -503,7 +498,7 @@ static int bch2_quota_disable(struct super_block *sb, unsigned uflags)
 {
        struct bch_fs *c = sb->s_fs_info;
 
-       if (sb->s_flags & MS_RDONLY)
+       if (sb->s_flags & SB_RDONLY)
                return -EROFS;
 
        mutex_lock(&c->sb_lock);
@@ -527,14 +522,14 @@ static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
        struct bch_fs *c = sb->s_fs_info;
        int ret;
 
-       if (sb->s_flags & MS_RDONLY)
+       if (sb->s_flags & SB_RDONLY)
                return -EROFS;
 
        if (uflags & FS_USER_QUOTA) {
                if (c->opts.usrquota)
                        return -EINVAL;
 
-               ret = bch2_btree_delete_range(c, BTREE_ID_QUOTAS,
+               ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
                                              POS(QTYP_USR, 0),
                                              POS(QTYP_USR + 1, 0),
                                              NULL);
@@ -546,7 +541,7 @@ static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
                if (c->opts.grpquota)
                        return -EINVAL;
 
-               ret = bch2_btree_delete_range(c, BTREE_ID_QUOTAS,
+               ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
                                              POS(QTYP_GRP, 0),
                                              POS(QTYP_GRP + 1, 0),
                                              NULL);
@@ -558,7 +553,7 @@ static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
                if (c->opts.prjquota)
                        return -EINVAL;
 
-               ret = bch2_btree_delete_range(c, BTREE_ID_QUOTAS,
+               ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
                                              POS(QTYP_PRJ, 0),
                                              POS(QTYP_PRJ + 1, 0),
                                              NULL);
@@ -609,7 +604,7 @@ static int bch2_quota_set_info(struct super_block *sb, int type,
        struct bch_sb_field_quota *sb_quota;
        struct bch_memquota_type *q;
 
-       if (sb->s_flags & MS_RDONLY)
+       if (sb->s_flags & SB_RDONLY)
                return -EROFS;
 
        if (type >= QTYP_NR)
@@ -699,75 +694,75 @@ static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
        struct bch_fs *c                = sb->s_fs_info;
        struct bch_memquota_type *q     = &c->quotas[kqid->type];
        qid_t qid                       = from_kqid(&init_user_ns, *kqid);
-       struct genradix_iter iter       = genradix_iter_init(&q->table, qid);
+       struct genradix_iter iter;
        struct bch_memquota *mq;
        int ret = 0;
 
        mutex_lock(&q->lock);
 
-       while ((mq = genradix_iter_peek(&iter, &q->table))) {
+       genradix_for_each_from(&q->table, iter, mq, qid)
                if (memcmp(mq, page_address(ZERO_PAGE(0)), sizeof(*mq))) {
                        __bch2_quota_get(qdq, mq);
                        *kqid = make_kqid(current_user_ns(), kqid->type, iter.pos);
                        goto found;
                }
 
-               genradix_iter_advance(&iter, &q->table);
-       }
-
        ret = -ENOENT;
 found:
        mutex_unlock(&q->lock);
        return ret;
 }
 
-static int bch2_set_quota(struct super_block *sb, struct kqid qid,
-                         struct qc_dqblk *qdq)
+static int bch2_set_quota_trans(struct btree_trans *trans,
+                               struct bkey_i_quota *new_quota,
+                               struct qc_dqblk *qdq)
 {
-       struct bch_fs *c = sb->s_fs_info;
        struct btree_iter iter;
        struct bkey_s_c k;
-       struct bkey_i_quota new_quota;
        int ret;
 
-       if (sb->s_flags & MS_RDONLY)
-               return -EROFS;
-
-       bkey_quota_init(&new_quota.k_i);
-       new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
-
-       bch2_btree_iter_init(&iter, c, BTREE_ID_QUOTAS, new_quota.k.p,
+       bch2_trans_iter_init(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
                             BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
        k = bch2_btree_iter_peek_slot(&iter);
 
-       ret = btree_iter_err(k);
+       ret = bkey_err(k);
        if (unlikely(ret))
                return ret;
 
-       switch (k.k->type) {
-       case BCH_QUOTA:
-               new_quota.v = *bkey_s_c_to_quota(k).v;
-               break;
-       }
+       if (k.k->type == KEY_TYPE_quota)
+               new_quota->v = *bkey_s_c_to_quota(k).v;
 
        if (qdq->d_fieldmask & QC_SPC_SOFT)
-               new_quota.v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
+               new_quota->v.c[Q_SPC].softlimit = cpu_to_le64(qdq->d_spc_softlimit >> 9);
        if (qdq->d_fieldmask & QC_SPC_HARD)
-               new_quota.v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
+               new_quota->v.c[Q_SPC].hardlimit = cpu_to_le64(qdq->d_spc_hardlimit >> 9);
 
        if (qdq->d_fieldmask & QC_INO_SOFT)
-               new_quota.v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
+               new_quota->v.c[Q_INO].softlimit = cpu_to_le64(qdq->d_ino_softlimit);
        if (qdq->d_fieldmask & QC_INO_HARD)
-               new_quota.v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
+               new_quota->v.c[Q_INO].hardlimit = cpu_to_le64(qdq->d_ino_hardlimit);
 
-       ret = bch2_btree_insert_at(c, NULL, NULL, 0,
-                                  BTREE_INSERT_ENTRY(&iter, &new_quota.k_i));
-       bch2_btree_iter_unlock(&iter);
+       ret = bch2_trans_update(trans, &iter, &new_quota->k_i, 0);
+       bch2_trans_iter_exit(trans, &iter);
+       return ret;
+}
 
-       if (ret)
-               return ret;
+static int bch2_set_quota(struct super_block *sb, struct kqid qid,
+                         struct qc_dqblk *qdq)
+{
+       struct bch_fs *c = sb->s_fs_info;
+       struct bkey_i_quota new_quota;
+       int ret;
+
+       if (sb->s_flags & SB_RDONLY)
+               return -EROFS;
+
+       bkey_quota_init(&new_quota.k_i);
+       new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
 
-       ret = __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i));
+       ret = bch2_trans_do(c, NULL, NULL, 0,
+                           bch2_set_quota_trans(&trans, &new_quota, qdq)) ?:
+               __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i));
 
        return ret;
 }