]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/nocow_locking.h
Update bcachefs sources to 5963d1b1a4 bcacehfs: Fix bch2_get_alloc_in_memory_pos()
[bcachefs-tools-debian] / libbcachefs / nocow_locking.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_NOCOW_LOCKING_H
3 #define _BCACHEFS_NOCOW_LOCKING_H
4
5 #include "bcachefs_format.h"
6 #include "two_state_shared_lock.h"
7
8 #include <linux/hash.h>
9
10 #define BUCKET_NOCOW_LOCKS_BITS         10
11 #define BUCKET_NOCOW_LOCKS              (1U << BUCKET_NOCOW_LOCKS_BITS)
12
13 struct bucket_nocow_lock_table {
14         two_state_lock_t                l[BUCKET_NOCOW_LOCKS];
15 };
16
17 #define BUCKET_NOCOW_LOCK_UPDATE        (1 << 0)
18
19 static inline two_state_lock_t *bucket_nocow_lock(struct bucket_nocow_lock_table *t,
20                                                   struct bpos bucket)
21 {
22         u64 dev_bucket = bucket.inode << 56 | bucket.offset;
23         unsigned h = hash_64(dev_bucket, BUCKET_NOCOW_LOCKS_BITS);
24
25         return t->l + (h & (BUCKET_NOCOW_LOCKS - 1));
26 }
27
28 static inline bool bch2_bucket_nocow_is_locked(struct bucket_nocow_lock_table *t,
29                                                struct bpos bucket)
30 {
31         two_state_lock_t *l = bucket_nocow_lock(t, bucket);
32
33         return atomic_long_read(&l->v) != 0;
34 }
35
36 static inline void bch2_bucket_nocow_unlock(struct bucket_nocow_lock_table *t,
37                                             struct bpos bucket, int flags)
38 {
39         two_state_lock_t *l = bucket_nocow_lock(t, bucket);
40
41         bch2_two_state_unlock(l, flags & BUCKET_NOCOW_LOCK_UPDATE);
42 }
43
44 void __bch2_bucket_nocow_lock(struct bucket_nocow_lock_table *, two_state_lock_t *, int);
45
46 static inline void bch2_bucket_nocow_lock(struct bucket_nocow_lock_table *t,
47                                           struct bpos bucket, int flags)
48 {
49         two_state_lock_t *l = bucket_nocow_lock(t, bucket);
50
51         if (!bch2_two_state_trylock(l, flags & BUCKET_NOCOW_LOCK_UPDATE))
52                 __bch2_bucket_nocow_lock(t, l, flags);
53 }
54
55 #endif /* _BCACHEFS_NOCOW_LOCKING_H */