1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include "bcachefs.h"
#include "btree_iter.h"
#include "btree_key_cache.h"
static const struct rhashtable_params bch_btree_key_cache_params = {
.head_offset = offsetof(struct btree_key_cache, hash),
.key_offset = offsetof(struct btree_key_cache, k.k.p),
.key_len = sizeof(struct bpos),
};
__flatten
static inline struct btree_key_cache *btree_key_cache_find(struct bch_fs *c,
enum btree_id btree_id,
struct bpos pos)
{
return rhashtable_lookup_fast(&c->btree_key_cache[btree_id], &pos,
bch_btree_key_cache_params);
}
static struct btree_key_cache *
btree_key_cache_fill(struct bch_fs *c,
enum btree_id btree_id,
struct bpos pos)
{
struct btree_key_cache *c_k;
unsigned u64s = 64;
int ret;
mutex_lock(&c->btree_key_cache_lock);
rcu_read_lock();
c_k = btree_key_cache_find(c, btree_id, pos);
if (c_k) {
atomic_inc(&c_k->ref);
rcu_read_unlock();
return c_k;
}
rcu_read_unlock();
c_k = kmalloc(offsetof(struct btree_key_cache, k) +
u64s * sizeof(u64), GFP_NOFS);
if (!c_k) {
mutex_unlock(&c->btree_key_cache_lock);
return ERR_PTR(-ENOMEM);
}
memset(c_k, 0, offsetof(struct btree_key_cache, k));
mutex_init(&c_k->lock);
BUG_ON(!mutex_trylock(&c_k->lock));
atomic_set(&c_k->ref, 1);
c_k->allocated_u64s = u64s;
c_k->btree_id = btree_id;
c_k->k.k.p = pos;
ret = rhashtable_lookup_insert_fast(&c->btree_key_cache[btree_id],
&c_k->hash,
bch_btree_key_cache_params);
BUG_ON(ret);
mutex_unlock(&c->btree_key_cache_lock);
return c_k;
}
static int btree_key_cache_read(struct btree_trans *trans,
struct btree_key_cache *c_k)
{
struct btree_iter *iter;
struct bkey_s_c k;
int ret;
iter = bch2_trans_get_iter(trans, c_k->btree_id, c_k->k.k.p, 0);
if (IS_ERR(iter))
return PTR_ERR(iter);
k = bch2_btree_iter_peek_slot(iter);
ret = btree_iter_err(k);
if (ret)
return ret;
BUG_ON(k.k->u64s > c_k->allocated_u64s);
bkey_reassemble(&c_k->k, k);
c_k->read_done = true;
bch2_trans_iter_put(trans, iter);
return 0;
}
void bch2_btree_key_cache_put(struct bch_fs *c,
struct btree_key_cache *c_k)
{
if (atomic_dec_and_test(&c_k->ref)) {
}
}
struct btree_key_cache *
bch2_btree_key_cache_get(struct btree_trans *trans,
enum btree_id btree_id,
struct bpos pos)
{
struct bch_fs *c = trans->c;
struct btree_key_cache *c_k;
rcu_read_lock();
c_k = btree_key_cache_find(c, btree_id, pos);
if (c_k) {
atomic_inc(&c_k->ref);
rcu_read_unlock();
goto out;
}
rcu_read_unlock();
c_k = btree_key_cache_fill(c, btree_id, pos);
if (IS_ERR(c_k))
return c_k;
out:
if (!c_k->read_done) {
int ret = 0;
mutex_lock(&c_k->lock);
if (!c_k->read_done)
ret = btree_key_cache_read(trans, c_k);
mutex_unlock(&c_k->lock);
if (ret) {
bch2_btree_key_cache_put(c, c_k);
return ERR_PTR(ret);
}
}
return c_k;
}
void bch2_btree_key_cache_exit(struct bch_fs *c)
{
unsigned i;
for (i = 0; i < ARRAY_SIZE(c->btree_key_cache); i++) {
rhashtable_destroy(&c->btree_key_cache[i]);
}
}
int bch2_btree_key_cache_init(struct bch_fs *c)
{
unsigned i;
int ret;
for (i = 0; i < ARRAY_SIZE(c->btree_key_cache); i++) {
ret = rhashtable_init(&c->btree_key_cache[i],
&bch_btree_key_cache_params);
if (ret)
return ret;
}
return 0;
}
|