]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/quota.c
Update bcachefs sources to 5d0a6c2b32f1 bcachefs: check_directory_structure() can...
[bcachefs-tools-debian] / libbcachefs / quota.c
index ad7130a1469167ea5e165ebd0d8806e82dd96dd6..e68b34eab90a912a55727b1da0428b3655cb3834 100644 (file)
@@ -2,9 +2,10 @@
 #include "bcachefs.h"
 #include "btree_update.h"
 #include "errcode.h"
+#include "error.h"
 #include "inode.h"
 #include "quota.h"
-#include "subvolume.h"
+#include "snapshot.h"
 #include "super-io.h"
 
 static const char * const bch2_quota_types[] = {
@@ -26,7 +27,7 @@ static int bch2_sb_quota_validate(struct bch_sb *sb, struct bch_sb_field *f,
        if (vstruct_bytes(&q->field) < sizeof(*q)) {
                prt_printf(err, "wrong size (got %zu should be %zu)",
                       vstruct_bytes(&q->field), sizeof(*q));
-               return -EINVAL;
+               return -BCH_ERR_invalid_sb_quota;
        }
 
        return 0;
@@ -58,22 +59,18 @@ const struct bch_sb_field_ops bch_sb_field_ops_quota = {
        .to_text        = bch2_sb_quota_to_text,
 };
 
-int bch2_quota_invalid(const struct bch_fs *c, struct bkey_s_c k,
-                      int rw, struct printbuf *err)
+int bch2_quota_invalid(struct bch_fs *c, struct bkey_s_c k,
+                      enum bkey_invalid_flags flags,
+                      struct printbuf *err)
 {
-       if (k.k->p.inode >= QTYP_NR) {
-               prt_printf(err, "invalid quota type (%llu >= %u)",
-                      k.k->p.inode, QTYP_NR);
-               return -EINVAL;
-       }
-
-       if (bkey_val_bytes(k.k) != sizeof(struct bch_quota)) {
-               prt_printf(err, "incorrect value size (%zu != %zu)",
-                      bkey_val_bytes(k.k), sizeof(struct bch_quota));
-               return -EINVAL;
-       }
+       int ret = 0;
 
-       return 0;
+       bkey_fsck_err_on(k.k->p.inode >= QTYP_NR, c, err,
+                        quota_type_invalid,
+                        "invalid quota type (%llu >= %u)",
+                        k.k->p.inode, QTYP_NR);
+fsck_err:
+       return ret;
 }
 
 void bch2_quota_to_text(struct printbuf *out, struct bch_fs *c,
@@ -332,34 +329,20 @@ static int bch2_quota_check_limit(struct bch_fs *c,
        if (qc->hardlimit &&
            qc->hardlimit < n &&
            !ignore_hardlimit(q)) {
-               if (mode == KEY_TYPE_QUOTA_PREALLOC)
-                       return -EDQUOT;
-
                prepare_warning(qc, qtype, counter, msgs, HARDWARN);
+               return -EDQUOT;
        }
 
        if (qc->softlimit &&
-           qc->softlimit < n &&
-           qc->timer &&
-           ktime_get_real_seconds() >= qc->timer &&
-           !ignore_hardlimit(q)) {
-               if (mode == KEY_TYPE_QUOTA_PREALLOC)
-                       return -EDQUOT;
-
-               prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
-       }
-
-       if (qc->softlimit &&
-           qc->softlimit < n &&
-           qc->timer == 0) {
-               if (mode == KEY_TYPE_QUOTA_PREALLOC)
+           qc->softlimit < n) {
+               if (qc->timer == 0) {
+                       qc->timer = ktime_get_real_seconds() + q->limits[counter].timelimit;
+                       prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
+               } else if (ktime_get_real_seconds() >= qc->timer &&
+                          !ignore_hardlimit(q)) {
+                       prepare_warning(qc, qtype, counter, msgs, SOFTLONGWARN);
                        return -EDQUOT;
-
-               prepare_warning(qc, qtype, counter, msgs, SOFTWARN);
-
-               /* XXX is this the right one? */
-               qc->timer = ktime_get_real_seconds() +
-                       q->limits[counter].warnlimit;
+               }
        }
 
        return 0;
@@ -378,16 +361,16 @@ int bch2_quota_acct(struct bch_fs *c, struct bch_qid qid,
 
        memset(&msgs, 0, sizeof(msgs));
 
+       for_each_set_qtype(c, i, q, qtypes) {
+               mq[i] = genradix_ptr_alloc(&q->table, qid.q[i], GFP_KERNEL);
+               if (!mq[i])
+                       return -ENOMEM;
+       }
+
        for_each_set_qtype(c, i, q, qtypes)
                mutex_lock_nested(&q->lock, i);
 
        for_each_set_qtype(c, i, q, qtypes) {
-               mq[i] = genradix_ptr_alloc(&q->table, qid.q[i], GFP_NOFS);
-               if (!mq[i]) {
-                       ret = -ENOMEM;
-                       goto err;
-               }
-
                ret = bch2_quota_check_limit(c, i, mq[i], &msgs, counter, v, mode);
                if (ret)
                        goto err;
@@ -430,18 +413,17 @@ int bch2_quota_transfer(struct bch_fs *c, unsigned qtypes,
 
        memset(&msgs, 0, sizeof(msgs));
 
+       for_each_set_qtype(c, i, q, qtypes) {
+               src_q[i] = genradix_ptr_alloc(&q->table, src.q[i], GFP_KERNEL);
+               dst_q[i] = genradix_ptr_alloc(&q->table, dst.q[i], GFP_KERNEL);
+               if (!src_q[i] || !dst_q[i])
+                       return -ENOMEM;
+       }
+
        for_each_set_qtype(c, i, q, qtypes)
                mutex_lock_nested(&q->lock, i);
 
        for_each_set_qtype(c, i, q, qtypes) {
-               src_q[i] = genradix_ptr_alloc(&q->table, src.q[i], GFP_NOFS);
-               dst_q[i] = genradix_ptr_alloc(&q->table, dst.q[i], GFP_NOFS);
-
-               if (!src_q[i] || !dst_q[i]) {
-                       ret = -ENOMEM;
-                       goto err;
-               }
-
                ret = bch2_quota_check_limit(c, i, dst_q[i], &msgs, Q_SPC,
                                             dst_q[i]->c[Q_SPC].v + space,
                                             mode);
@@ -469,7 +451,8 @@ err:
        return ret;
 }
 
-static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k)
+static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k,
+                           struct qc_dqblk *qdq)
 {
        struct bkey_s_c_quota dq;
        struct bch_memquota_type *q;
@@ -498,6 +481,15 @@ static int __bch2_quota_set(struct bch_fs *c, struct bkey_s_c k)
                        mq->c[i].softlimit = le64_to_cpu(dq.v->c[i].softlimit);
                }
 
+               if (qdq && qdq->d_fieldmask & QC_SPC_TIMER)
+                       mq->c[Q_SPC].timer      = qdq->d_spc_timer;
+               if (qdq && qdq->d_fieldmask & QC_SPC_WARNS)
+                       mq->c[Q_SPC].warns      = qdq->d_spc_warns;
+               if (qdq && qdq->d_fieldmask & QC_INO_TIMER)
+                       mq->c[Q_INO].timer      = qdq->d_ino_timer;
+               if (qdq && qdq->d_fieldmask & QC_INO_WARNS)
+                       mq->c[Q_INO].warns      = qdq->d_ino_warns;
+
                mutex_unlock(&q->lock);
        }
 
@@ -522,12 +514,12 @@ void bch2_fs_quota_init(struct bch_fs *c)
 
 static struct bch_sb_field_quota *bch2_sb_get_or_create_quota(struct bch_sb_handle *sb)
 {
-       struct bch_sb_field_quota *sb_quota = bch2_sb_get_quota(sb->sb);
+       struct bch_sb_field_quota *sb_quota = bch2_sb_field_get(sb->sb, quota);
 
        if (sb_quota)
                return sb_quota;
 
-       sb_quota = bch2_sb_resize_quota(sb, sizeof(*sb_quota) / sizeof(u64));
+       sb_quota = bch2_sb_field_resize(sb, quota, sizeof(*sb_quota) / sizeof(u64));
        if (sb_quota) {
                unsigned qtype, qc;
 
@@ -545,7 +537,7 @@ static void bch2_sb_quota_read(struct bch_fs *c)
        struct bch_sb_field_quota *sb_quota;
        unsigned i, j;
 
-       sb_quota = bch2_sb_get_quota(c->disk_sb.sb);
+       sb_quota = bch2_sb_field_get(c->disk_sb.sb, quota);
        if (!sb_quota)
                return;
 
@@ -567,23 +559,32 @@ static int bch2_fs_quota_read_inode(struct btree_trans *trans,
 {
        struct bch_fs *c = trans->c;
        struct bch_inode_unpacked u;
-       struct bch_subvolume subvolume;
+       struct bch_snapshot_tree s_t;
        int ret;
 
-       ret = bch2_snapshot_get_subvol(trans, k.k->p.snapshot, &subvolume);
+       ret = bch2_snapshot_tree_lookup(trans,
+                       bch2_snapshot_tree(c, k.k->p.snapshot), &s_t);
+       bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
+                       "%s: snapshot tree %u not found", __func__,
+                       snapshot_t(c, k.k->p.snapshot)->tree);
        if (ret)
                return ret;
 
-       /*
-        * We don't do quota accounting in snapshots:
-        */
-       if (BCH_SUBVOLUME_SNAP(&subvolume))
+       if (!s_t.master_subvol)
                goto advance;
 
-       if (!bkey_is_inode(k.k))
+       ret = bch2_inode_find_by_inum_nowarn_trans(trans,
+                               (subvol_inum) {
+                                       le32_to_cpu(s_t.master_subvol),
+                                       k.k->p.offset,
+                               }, &u);
+       /*
+        * Inode might be deleted in this snapshot - the easiest way to handle
+        * that is to just skip it here:
+        */
+       if (bch2_err_matches(ret, ENOENT))
                goto advance;
 
-       ret = bch2_inode_unpack(k, &u);
        if (ret)
                return ret;
 
@@ -592,20 +593,15 @@ static int bch2_fs_quota_read_inode(struct btree_trans *trans,
        bch2_quota_acct(c, bch_qid(&u), Q_INO, 1,
                        KEY_TYPE_QUOTA_NOCHECK);
 advance:
-       bch2_btree_iter_set_pos(iter, POS(iter->pos.inode, iter->pos.offset + 1));
+       bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos));
        return 0;
 }
 
 int bch2_fs_quota_read(struct bch_fs *c)
 {
-       struct bch_sb_field_quota *sb_quota;
-       struct btree_trans trans;
-       struct btree_iter iter;
-       struct bkey_s_c k;
-       int ret;
 
        mutex_lock(&c->sb_lock);
-       sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
+       struct bch_sb_field_quota *sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
        if (!sb_quota) {
                mutex_unlock(&c->sb_lock);
                return -BCH_ERR_ENOSPC_sb_quota;
@@ -614,18 +610,14 @@ int bch2_fs_quota_read(struct bch_fs *c)
        bch2_sb_quota_read(c);
        mutex_unlock(&c->sb_lock);
 
-       bch2_trans_init(&trans, c, 0, 0);
-
-       ret = for_each_btree_key2(&trans, iter, BTREE_ID_quotas,
-                       POS_MIN, BTREE_ITER_PREFETCH, k,
-               __bch2_quota_set(c, k)) ?:
-             for_each_btree_key2(&trans, iter, BTREE_ID_inodes,
-                       POS_MIN, BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
-               bch2_fs_quota_read_inode(&trans, &iter, k));
-       if (ret)
-               bch_err(c, "err in quota_read: %s", bch2_err_str(ret));
-
-       bch2_trans_exit(&trans);
+       int ret = bch2_trans_run(c,
+               for_each_btree_key(trans, iter, BTREE_ID_quotas, POS_MIN,
+                                  BTREE_ITER_PREFETCH, k,
+                       __bch2_quota_set(c, k, NULL)) ?:
+               for_each_btree_key(trans, iter, BTREE_ID_inodes, POS_MIN,
+                                  BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
+                       bch2_fs_quota_read_inode(trans, &iter, k)));
+       bch_err_fn(c, ret);
        return ret;
 }
 
@@ -714,7 +706,7 @@ static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
 
                ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
                                              POS(QTYP_USR, 0),
-                                             POS(QTYP_USR + 1, 0),
+                                             POS(QTYP_USR, U64_MAX),
                                              0, NULL);
                if (ret)
                        return ret;
@@ -726,7 +718,7 @@ static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
 
                ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
                                              POS(QTYP_GRP, 0),
-                                             POS(QTYP_GRP + 1, 0),
+                                             POS(QTYP_GRP, U64_MAX),
                                              0, NULL);
                if (ret)
                        return ret;
@@ -738,7 +730,7 @@ static int bch2_quota_remove(struct super_block *sb, unsigned uflags)
 
                ret = bch2_btree_delete_range(c, BTREE_ID_quotas,
                                              POS(QTYP_PRJ, 0),
-                                             POS(QTYP_PRJ + 1, 0),
+                                             POS(QTYP_PRJ, U64_MAX),
                                              0, NULL);
                if (ret)
                        return ret;
@@ -785,7 +777,6 @@ static int bch2_quota_set_info(struct super_block *sb, int type,
 {
        struct bch_fs *c = sb->s_fs_info;
        struct bch_sb_field_quota *sb_quota;
-       struct bch_memquota_type *q;
        int ret = 0;
 
        if (0) {
@@ -809,8 +800,6 @@ static int bch2_quota_set_info(struct super_block *sb, int type,
            ~(QC_SPC_TIMER|QC_INO_TIMER|QC_SPC_WARNS|QC_INO_WARNS))
                return -EINVAL;
 
-       q = &c->quotas[type];
-
        mutex_lock(&c->sb_lock);
        sb_quota = bch2_sb_get_or_create_quota(&c->disk_sb);
        if (!sb_quota) {
@@ -901,7 +890,7 @@ static int bch2_get_next_quota(struct super_block *sb, struct kqid *kqid,
        ret = -ENOENT;
 found:
        mutex_unlock(&q->lock);
-       return ret;
+       return bch2_err_class(ret);
 }
 
 static int bch2_set_quota_trans(struct btree_trans *trans,
@@ -912,10 +901,8 @@ static int bch2_set_quota_trans(struct btree_trans *trans,
        struct bkey_s_c k;
        int ret;
 
-       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);
-
+       k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_quotas, new_quota->k.p,
+                              BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
        ret = bkey_err(k);
        if (unlikely(ret))
                return ret;
@@ -960,10 +947,10 @@ static int bch2_set_quota(struct super_block *sb, struct kqid qid,
        new_quota.k.p = POS(qid.type, from_kqid(&init_user_ns, qid));
 
        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));
+                           bch2_set_quota_trans(trans, &new_quota, qdq)) ?:
+               __bch2_quota_set(c, bkey_i_to_s_c(&new_quota.k_i), qdq);
 
-       return ret;
+       return bch2_err_class(ret);
 }
 
 const struct quotactl_ops bch2_quotactl_operations = {