]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/str_hash.h
Update bcachefs sources to c887148ebf99 thread_with_file: add f_ops.flush
[bcachefs-tools-debian] / libbcachefs / str_hash.h
index 591bbb9f8beb544de1369204ffc666018ef40740..3976f80721bf1b40736a3d882c9f841fa7ab961b 100644 (file)
 #include <crypto/hash.h>
 #include <crypto/sha2.h>
 
+typedef unsigned __bitwise bch_str_hash_flags_t;
+
+enum bch_str_hash_flags {
+       __BCH_HASH_SET_MUST_CREATE,
+       __BCH_HASH_SET_MUST_REPLACE,
+};
+
+#define BCH_HASH_SET_MUST_CREATE       (__force bch_str_hash_flags_t) BIT(__BCH_HASH_SET_MUST_CREATE)
+#define BCH_HASH_SET_MUST_REPLACE      (__force bch_str_hash_flags_t) BIT(__BCH_HASH_SET_MUST_REPLACE)
+
 static inline enum bch_str_hash_type
 bch2_str_hash_opt_to_type(struct bch_fs *c, enum bch_str_hash_opts opt)
 {
@@ -144,25 +154,22 @@ struct bch_hash_desc {
 static inline bool is_visible_key(struct bch_hash_desc desc, subvol_inum inum, struct bkey_s_c k)
 {
        return k.k->type == desc.key_type &&
-               (!desc.is_visible || desc.is_visible(inum, k));
+               (!desc.is_visible ||
+                !inum.inum ||
+                desc.is_visible(inum, k));
 }
 
 static __always_inline int
-bch2_hash_lookup(struct btree_trans *trans,
+bch2_hash_lookup_in_snapshot(struct btree_trans *trans,
                 struct btree_iter *iter,
                 const struct bch_hash_desc desc,
                 const struct bch_hash_info *info,
                 subvol_inum inum, const void *key,
-                unsigned flags)
+                unsigned flags, u32 snapshot)
 {
        struct bkey_s_c k;
-       u32 snapshot;
        int ret;
 
-       ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
-       if (ret)
-               return ret;
-
        for_each_btree_key_upto_norestart(trans, *iter, desc.btree_id,
                           SPOS(inum.inum, desc.hash_key(info, key), snapshot),
                           POS(inum.inum, U64_MAX),
@@ -179,7 +186,20 @@ bch2_hash_lookup(struct btree_trans *trans,
        }
        bch2_trans_iter_exit(trans, iter);
 
-       return ret ?: -ENOENT;
+       return ret ?: -BCH_ERR_ENOENT_str_hash_lookup;
+}
+
+static __always_inline int
+bch2_hash_lookup(struct btree_trans *trans,
+                struct btree_iter *iter,
+                const struct bch_hash_desc desc,
+                const struct bch_hash_info *info,
+                subvol_inum inum, const void *key,
+                unsigned flags)
+{
+       u32 snapshot;
+       return  bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot) ?:
+               bch2_hash_lookup_in_snapshot(trans, iter, desc, info, inum, key, flags, snapshot);
 }
 
 static __always_inline int
@@ -205,7 +225,7 @@ bch2_hash_hole(struct btree_trans *trans,
                        return 0;
        bch2_trans_iter_exit(trans, iter);
 
-       return ret ?: -ENOSPC;
+       return ret ?: -BCH_ERR_ENOSPC_str_hash_create;
 }
 
 static __always_inline
@@ -239,27 +259,24 @@ int bch2_hash_needs_whiteout(struct btree_trans *trans,
 }
 
 static __always_inline
-int bch2_hash_set(struct btree_trans *trans,
-                 const struct bch_hash_desc desc,
-                 const struct bch_hash_info *info,
-                 subvol_inum inum,
-                 struct bkey_i *insert, int flags)
+int bch2_hash_set_in_snapshot(struct btree_trans *trans,
+                          const struct bch_hash_desc desc,
+                          const struct bch_hash_info *info,
+                          subvol_inum inum, u32 snapshot,
+                          struct bkey_i *insert,
+                          bch_str_hash_flags_t str_hash_flags,
+                          int update_flags)
 {
        struct btree_iter iter, slot = { NULL };
        struct bkey_s_c k;
        bool found = false;
-       u32 snapshot;
        int ret;
 
-       ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
-       if (ret)
-               return ret;
-
        for_each_btree_key_upto_norestart(trans, iter, desc.btree_id,
-                          SPOS(inum.inum,
+                          SPOS(insert->k.p.inode,
                                desc.hash_bkey(info, bkey_i_to_s_c(insert)),
                                snapshot),
-                          POS(inum.inum, U64_MAX),
+                          POS(insert->k.p.inode, U64_MAX),
                           BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
                if (is_visible_key(desc, inum, k)) {
                        if (!desc.cmp_bkey(k, bkey_i_to_s_c(insert)))
@@ -270,7 +287,7 @@ int bch2_hash_set(struct btree_trans *trans,
                }
 
                if (!slot.path &&
-                   !(flags & BCH_HASH_SET_MUST_REPLACE))
+                   !(str_hash_flags & BCH_HASH_SET_MUST_REPLACE))
                        bch2_trans_copy_iter(&slot, &iter);
 
                if (k.k->type != KEY_TYPE_hash_whiteout)
@@ -278,7 +295,7 @@ int bch2_hash_set(struct btree_trans *trans,
        }
 
        if (!ret)
-               ret = -ENOSPC;
+               ret = -BCH_ERR_ENOSPC_str_hash_create;
 out:
        bch2_trans_iter_exit(trans, &slot);
        bch2_trans_iter_exit(trans, &iter);
@@ -288,21 +305,37 @@ found:
        found = true;
 not_found:
 
-       if (!found && (flags & BCH_HASH_SET_MUST_REPLACE)) {
-               ret = -ENOENT;
-       } else if (found && (flags & BCH_HASH_SET_MUST_CREATE)) {
+       if (!found && (str_hash_flags & BCH_HASH_SET_MUST_REPLACE)) {
+               ret = -BCH_ERR_ENOENT_str_hash_set_must_replace;
+       } else if (found && (str_hash_flags & BCH_HASH_SET_MUST_CREATE)) {
                ret = -EEXIST;
        } else {
                if (!found && slot.path)
                        swap(iter, slot);
 
                insert->k.p = iter.pos;
-               ret = bch2_trans_update(trans, &iter, insert, 0);
+               ret = bch2_trans_update(trans, &iter, insert, update_flags);
        }
 
        goto out;
 }
 
+static __always_inline
+int bch2_hash_set(struct btree_trans *trans,
+                 const struct bch_hash_desc desc,
+                 const struct bch_hash_info *info,
+                 subvol_inum inum,
+                 struct bkey_i *insert,
+                 bch_str_hash_flags_t str_hash_flags)
+{
+       insert->k.p.inode = inum.inum;
+
+       u32 snapshot;
+       return  bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot) ?:
+               bch2_hash_set_in_snapshot(trans, desc, info, inum,
+                                         snapshot, insert, str_hash_flags, 0);
+}
+
 static __always_inline
 int bch2_hash_delete_at(struct btree_trans *trans,
                        const struct bch_hash_desc desc,