]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_locking.h
Update bcachefs sources to 4b5917839c bcachefs: Fix a null ptr deref in check_xattr()
[bcachefs-tools-debian] / libbcachefs / btree_locking.h
index 61d5038a12a4ed9491572d5ce7f91b80fc02b228..f3e58aa2774d7d91c1537af99e3ffda83fb43c38 100644 (file)
 
 #include "btree_iter.h"
 
-extern struct lock_class_key bch2_btree_node_lock_key;
+void bch2_btree_lock_init(struct btree_bkey_cached_common *, enum six_lock_init_flags);
+
+#ifdef CONFIG_LOCKDEP
+void bch2_assert_btree_nodes_not_locked(void);
+#else
+static inline void bch2_assert_btree_nodes_not_locked(void) {}
+#endif
+
+void bch2_trans_unlock_noassert(struct btree_trans *);
 
 static inline bool is_btree_node(struct btree_path *path, unsigned l)
 {
@@ -86,9 +94,9 @@ static inline void mark_btree_node_locked(struct btree_trans *trans,
                                          unsigned level,
                                          enum six_lock_type type)
 {
-       mark_btree_node_locked_noreset(path, level, type);
+       mark_btree_node_locked_noreset(path, level, (enum btree_node_locked_type) type);
 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
-       path->l[level].lock_taken_time = ktime_get_ns();
+       path->l[level].lock_taken_time = local_clock();
 #endif
 }
 
@@ -120,7 +128,7 @@ static void btree_trans_lock_hold_time_update(struct btree_trans *trans,
        if (s)
                __bch2_time_stats_update(&s->lock_hold_times,
                                         path->l[level].lock_taken_time,
-                                        ktime_get_ns());
+                                        local_clock());
 #endif
 }
 
@@ -170,13 +178,13 @@ bch2_btree_node_unlock_write_inlined(struct btree_trans *trans, struct btree_pat
        struct btree_path *linked;
 
        EBUG_ON(path->l[b->c.level].b != b);
-       EBUG_ON(path->l[b->c.level].lock_seq + 1 != b->c.lock.state.seq);
+       EBUG_ON(path->l[b->c.level].lock_seq != six_lock_seq(&b->c.lock));
        EBUG_ON(btree_node_locked_type(path, b->c.level) != SIX_LOCK_write);
 
        mark_btree_node_locked_noreset(path, b->c.level, SIX_LOCK_intent);
 
        trans_for_each_path_with_node(trans, b, linked)
-               linked->l[b->c.level].lock_seq += 2;
+               linked->l[b->c.level].lock_seq++;
 
        six_unlock_write(&b->c.lock);
 }
@@ -191,15 +199,17 @@ int bch2_six_check_for_deadlock(struct six_lock *lock, void *p);
 static inline int __btree_node_lock_nopath(struct btree_trans *trans,
                                         struct btree_bkey_cached_common *b,
                                         enum six_lock_type type,
-                                        bool lock_may_not_fail)
+                                        bool lock_may_not_fail,
+                                        unsigned long ip)
 {
        int ret;
+
        trans->lock_may_not_fail = lock_may_not_fail;
        trans->lock_must_abort  = false;
        trans->locking          = b;
 
-       ret = six_lock_type_waiter(&b->lock, type, &trans->locking_wait,
-                                  bch2_six_check_for_deadlock, trans);
+       ret = six_lock_ip_waiter(&b->lock, type, &trans->locking_wait,
+                                bch2_six_check_for_deadlock, trans, ip);
        WRITE_ONCE(trans->locking, NULL);
        WRITE_ONCE(trans->locking_wait.start_time, 0);
        return ret;
@@ -208,16 +218,17 @@ static inline int __btree_node_lock_nopath(struct btree_trans *trans,
 static inline int __must_check
 btree_node_lock_nopath(struct btree_trans *trans,
                       struct btree_bkey_cached_common *b,
-                      enum six_lock_type type)
+                      enum six_lock_type type,
+                      unsigned long ip)
 {
-       return __btree_node_lock_nopath(trans, b, type, false);
+       return __btree_node_lock_nopath(trans, b, type, false, ip);
 }
 
 static inline void btree_node_lock_nopath_nofail(struct btree_trans *trans,
                                         struct btree_bkey_cached_common *b,
                                         enum six_lock_type type)
 {
-       int ret = __btree_node_lock_nopath(trans, b, type, true);
+       int ret = __btree_node_lock_nopath(trans, b, type, true, _THIS_IP_);
 
        BUG_ON(ret);
 }
@@ -236,7 +247,7 @@ static inline bool btree_node_lock_increment(struct btree_trans *trans,
        trans_for_each_path(trans, path)
                if (&path->l[level].b->c == b &&
                    btree_node_locked_type(path, level) >= want) {
-                       six_lock_increment(&b->lock, want);
+                       six_lock_increment(&b->lock, (enum six_lock_type) want);
                        return true;
                }
 
@@ -256,10 +267,10 @@ static inline int btree_node_lock(struct btree_trans *trans,
        EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
 
        if (likely(six_trylock_type(&b->lock, type)) ||
-           btree_node_lock_increment(trans, b, level, type) ||
-           !(ret = btree_node_lock_nopath(trans, b, type))) {
+           btree_node_lock_increment(trans, b, level, (enum btree_node_locked_type) type) ||
+           !(ret = btree_node_lock_nopath(trans, b, type, btree_path_ip_allocated(path)))) {
 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
-               path->l[b->level].lock_taken_time = ktime_get_ns();
+               path->l[b->level].lock_taken_time = local_clock();
 #endif
        }
 
@@ -275,7 +286,7 @@ static inline int __btree_node_lock_write(struct btree_trans *trans,
                                          bool lock_may_not_fail)
 {
        EBUG_ON(&path->l[b->level].b->c != b);
-       EBUG_ON(path->l[b->level].lock_seq != b->lock.state.seq);
+       EBUG_ON(path->l[b->level].lock_seq != six_lock_seq(&b->lock));
        EBUG_ON(!btree_node_intent_locked(path, b->level));
 
        /*
@@ -290,14 +301,6 @@ static inline int __btree_node_lock_write(struct btree_trans *trans,
                : __bch2_btree_node_lock_write(trans, path, b, lock_may_not_fail);
 }
 
-static inline void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
-                                             struct btree_path *path,
-                                             struct btree_bkey_cached_common *b)
-{
-       int ret = __btree_node_lock_write(trans, path, b, true);
-       BUG_ON(ret);
-}
-
 static inline int __must_check
 bch2_btree_node_lock_write(struct btree_trans *trans,
                           struct btree_path *path,
@@ -306,10 +309,25 @@ bch2_btree_node_lock_write(struct btree_trans *trans,
        return __btree_node_lock_write(trans, path, b, false);
 }
 
+void bch2_btree_node_lock_write_nofail(struct btree_trans *,
+                                      struct btree_path *,
+                                      struct btree_bkey_cached_common *);
+
 /* relock: */
 
 bool bch2_btree_path_relock_norestart(struct btree_trans *,
                                      struct btree_path *, unsigned long);
+int __bch2_btree_path_relock(struct btree_trans *,
+                            struct btree_path *, unsigned long);
+
+static inline int bch2_btree_path_relock(struct btree_trans *trans,
+                               struct btree_path *path, unsigned long trace_ip)
+{
+       return btree_node_locked(path, path->level)
+               ? 0
+               : __bch2_btree_path_relock(trans, path, trace_ip);
+}
+
 bool __bch2_btree_node_relock(struct btree_trans *, struct btree_path *, unsigned, bool trace);
 
 static inline bool bch2_btree_node_relock(struct btree_trans *trans,
@@ -336,17 +354,6 @@ static inline bool bch2_btree_node_relock_notrace(struct btree_trans *trans,
                 __bch2_btree_node_relock(trans, path, level, false));
 }
 
-static inline int bch2_btree_path_relock(struct btree_trans *trans,
-                               struct btree_path *path, unsigned long trace_ip)
-{
-       if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
-               trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
-               return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
-       }
-
-       return 0;
-}
-
 /* upgrade */
 
 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *,