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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
#include "bcache.h"
#include "bkey_methods.h"
#include "btree_update.h"
#include "extents.h"
#include "inode.h"
#include "io.h"
#include "keylist.h"
#include <linux/random.h>
ssize_t bch_inode_status(char *buf, size_t len, const struct bkey *k)
{
if (k->p.offset)
return scnprintf(buf, len, "offset nonzero: %llu", k->p.offset);
if (k->size)
return scnprintf(buf, len, "size nonzero: %u", k->size);
switch (k->type) {
case KEY_TYPE_DELETED:
return scnprintf(buf, len, "deleted");
case KEY_TYPE_DISCARD:
return scnprintf(buf, len, "discarded");
case KEY_TYPE_ERROR:
return scnprintf(buf, len, "error");
case KEY_TYPE_COOKIE:
return scnprintf(buf, len, "cookie");
case BCH_INODE_FS:
if (bkey_val_bytes(k) != sizeof(struct bch_inode))
return scnprintf(buf, len, "bad size: %zu",
bkey_val_bytes(k));
if (k->p.inode < BLOCKDEV_INODE_MAX)
return scnprintf(buf, len,
"fs inode in blockdev range: %llu",
k->p.inode);
return 0;
case BCH_INODE_BLOCKDEV:
if (bkey_val_bytes(k) != sizeof(struct bch_inode_blockdev))
return scnprintf(buf, len, "bad size: %zu",
bkey_val_bytes(k));
if (k->p.inode >= BLOCKDEV_INODE_MAX)
return scnprintf(buf, len,
"blockdev inode in fs range: %llu",
k->p.inode);
return 0;
default:
return scnprintf(buf, len, "unknown inode type: %u", k->type);
}
}
static const char *bch_inode_invalid(const struct cache_set *c,
struct bkey_s_c k)
{
if (k.k->p.offset)
return "nonzero offset";
switch (k.k->type) {
case BCH_INODE_FS: {
struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
if (bkey_val_bytes(k.k) != sizeof(struct bch_inode))
return "incorrect value size";
if (k.k->p.inode < BLOCKDEV_INODE_MAX)
return "fs inode in blockdev range";
if (INODE_STR_HASH_TYPE(inode.v) >= BCH_STR_HASH_NR)
return "invalid str hash type";
return NULL;
}
case BCH_INODE_BLOCKDEV:
if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_blockdev))
return "incorrect value size";
if (k.k->p.inode >= BLOCKDEV_INODE_MAX)
return "blockdev inode in fs range";
return NULL;
default:
return "invalid type";
}
}
static void bch_inode_to_text(struct cache_set *c, char *buf,
size_t size, struct bkey_s_c k)
{
struct bkey_s_c_inode inode;
switch (k.k->type) {
case BCH_INODE_FS:
inode = bkey_s_c_to_inode(k);
scnprintf(buf, size, "i_size %llu", inode.v->i_size);
break;
}
}
const struct bkey_ops bch_bkey_inode_ops = {
.key_invalid = bch_inode_invalid,
.val_to_text = bch_inode_to_text,
};
void bch_inode_init(struct cache_set *c, struct bkey_i_inode *inode,
uid_t uid, gid_t gid, umode_t mode, dev_t rdev)
{
struct timespec ts = CURRENT_TIME;
s64 now = timespec_to_ns(&ts);
struct bch_inode *bi;
bi = &bkey_inode_init(&inode->k_i)->v;
bi->i_uid = cpu_to_le32(uid);
bi->i_gid = cpu_to_le32(gid);
bi->i_mode = cpu_to_le16(mode);
bi->i_dev = cpu_to_le32(rdev);
bi->i_atime = cpu_to_le64(now);
bi->i_mtime = cpu_to_le64(now);
bi->i_ctime = cpu_to_le64(now);
bi->i_nlink = cpu_to_le32(S_ISDIR(mode) ? 2 : 1);
get_random_bytes(&bi->i_hash_seed, sizeof(bi->i_hash_seed));
SET_INODE_STR_HASH_TYPE(bi, c->sb.str_hash_type);
}
int bch_inode_create(struct cache_set *c, struct bkey_i *inode,
u64 min, u64 max, u64 *hint)
{
struct btree_iter iter;
bool searched_from_start = false;
int ret;
if (!max)
max = ULLONG_MAX;
if (c->opts.inodes_32bit)
max = min_t(u64, max, U32_MAX);
if (*hint >= max || *hint < min)
*hint = min;
if (*hint == min)
searched_from_start = true;
again:
bch_btree_iter_init_intent(&iter, c, BTREE_ID_INODES, POS(*hint, 0));
while (1) {
struct bkey_s_c k = bch_btree_iter_peek_with_holes(&iter);
ret = btree_iter_err(k);
if (ret) {
bch_btree_iter_unlock(&iter);
return ret;
}
if (k.k->type < BCH_INODE_FS) {
inode->k.p = k.k->p;
pr_debug("inserting inode %llu (size %u)",
inode->k.p.inode, inode->k.u64s);
ret = bch_btree_insert_at(c, NULL, NULL, NULL,
BTREE_INSERT_ATOMIC,
BTREE_INSERT_ENTRY(&iter, inode));
if (ret == -EINTR)
continue;
bch_btree_iter_unlock(&iter);
if (!ret)
*hint = k.k->p.inode + 1;
return ret;
} else {
if (iter.pos.inode == max)
break;
/* slot used */
bch_btree_iter_advance_pos(&iter);
}
}
bch_btree_iter_unlock(&iter);
if (!searched_from_start) {
/* Retry from start */
*hint = min;
searched_from_start = true;
goto again;
}
return -ENOSPC;
}
int bch_inode_truncate(struct cache_set *c, u64 inode_nr, u64 new_size,
struct extent_insert_hook *hook, u64 *journal_seq)
{
return bch_discard(c, POS(inode_nr, new_size), POS(inode_nr + 1, 0),
0, NULL, hook, journal_seq);
}
int bch_inode_rm(struct cache_set *c, u64 inode_nr)
{
struct bkey_i delete;
int ret;
ret = bch_inode_truncate(c, inode_nr, 0, NULL, NULL);
if (ret < 0)
return ret;
ret = bch_btree_delete_range(c, BTREE_ID_XATTRS,
POS(inode_nr, 0),
POS(inode_nr + 1, 0),
0, NULL, NULL, NULL);
if (ret < 0)
return ret;
/*
* If this was a directory, there shouldn't be any real dirents left -
* but there could be whiteouts (from hash collisions) that we should
* delete:
*
* XXX: the dirent could ideally would delete whitouts when they're no
* longer needed
*/
ret = bch_btree_delete_range(c, BTREE_ID_DIRENTS,
POS(inode_nr, 0),
POS(inode_nr + 1, 0),
0, NULL, NULL, NULL);
if (ret < 0)
return ret;
bkey_init(&delete.k);
delete.k.p.inode = inode_nr;
return bch_btree_insert(c, BTREE_ID_INODES, &delete, NULL,
NULL, NULL, BTREE_INSERT_NOFAIL);
}
int bch_inode_update(struct cache_set *c, struct bkey_i *inode,
u64 *journal_seq)
{
return bch_btree_update(c, BTREE_ID_INODES, inode, journal_seq);
}
int bch_inode_find_by_inum(struct cache_set *c, u64 inode_nr,
struct bkey_i_inode *inode)
{
struct btree_iter iter;
struct bkey_s_c k;
for_each_btree_key_with_holes(&iter, c, BTREE_ID_INODES,
POS(inode_nr, 0), k) {
switch (k.k->type) {
case BCH_INODE_FS:
bkey_reassemble(&inode->k_i, k);
bch_btree_iter_unlock(&iter);
return 0;
default:
/* hole, not found */
break;
}
break;
}
return bch_btree_iter_unlock(&iter) ?: -ENOENT;
}
int bch_cached_dev_inode_find_by_uuid(struct cache_set *c, uuid_le *uuid,
struct bkey_i_inode_blockdev *ret)
{
struct btree_iter iter;
struct bkey_s_c k;
for_each_btree_key(&iter, c, BTREE_ID_INODES, POS(0, 0), k) {
if (k.k->p.inode >= BLOCKDEV_INODE_MAX)
break;
if (k.k->type == BCH_INODE_BLOCKDEV) {
struct bkey_s_c_inode_blockdev inode =
bkey_s_c_to_inode_blockdev(k);
pr_debug("found inode %llu: %pU (u64s %u)",
inode.k->p.inode, inode.v->i_uuid.b,
inode.k->u64s);
if (CACHED_DEV(inode.v) &&
!memcmp(uuid, &inode.v->i_uuid, 16)) {
bkey_reassemble(&ret->k_i, k);
bch_btree_iter_unlock(&iter);
return 0;
}
}
bch_btree_iter_cond_resched(&iter);
}
bch_btree_iter_unlock(&iter);
return -ENOENT;
}
|