3 #include "bkey_methods.h"
4 #include "btree_update.h"
11 #include <linux/dcache.h>
13 static unsigned dirent_name_bytes(struct bkey_s_c_dirent d)
15 unsigned len = bkey_val_bytes(d.k) - sizeof(struct bch_dirent);
17 while (len && !d.v->d_name[len - 1])
23 static u64 bch_dirent_hash(const struct bch_hash_info *info,
24 const struct qstr *name)
27 case BCH_STR_HASH_SHA1: {
28 SHASH_DESC_ON_STACK(desc, bch_sha1);
29 u8 digest[SHA1_DIGEST_SIZE];
33 crypto_shash_init(desc);
35 crypto_shash_update(desc, (void *) &info->seed, sizeof(info->seed));
37 crypto_shash_update(desc, (void *) name->name, name->len);
38 crypto_shash_final(desc, digest);
39 memcpy(&ret, &digest, sizeof(ret));
40 return max_t(u64, ret >> 1, 2);
43 struct bch_str_hash_ctx ctx;
45 bch_str_hash_init(&ctx, info->type);
46 bch_str_hash_update(&ctx, info->type, &info->seed, sizeof(info->seed));
48 bch_str_hash_update(&ctx, info->type, name->name, name->len);
50 /* [0,2) reserved for dots */
51 return max_t(u64, bch_str_hash_end(&ctx, info->type), 2);
56 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
58 return bch_dirent_hash(info, key);
61 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
63 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
64 struct qstr name = QSTR_INIT(d.v->d_name, dirent_name_bytes(d));
66 return bch_dirent_hash(info, &name);
69 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
71 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
72 int len = dirent_name_bytes(l);
73 const struct qstr *r = _r;
75 return len - r->len ?: memcmp(l.v->d_name, r->name, len);
78 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
80 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
81 struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
82 int l_len = dirent_name_bytes(l);
83 int r_len = dirent_name_bytes(r);
85 return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
88 static const struct bch_hash_desc dirent_hash_desc = {
89 .btree_id = BTREE_ID_DIRENTS,
90 .key_type = BCH_DIRENT,
91 .whiteout_type = BCH_DIRENT_WHITEOUT,
92 .hash_key = dirent_hash_key,
93 .hash_bkey = dirent_hash_bkey,
94 .cmp_key = dirent_cmp_key,
95 .cmp_bkey = dirent_cmp_bkey,
98 static const char *bch_dirent_invalid(const struct cache_set *c,
103 return bkey_val_bytes(k.k) < sizeof(struct bch_dirent)
107 case BCH_DIRENT_WHITEOUT:
108 return bkey_val_bytes(k.k) != 0
109 ? "value size should be zero"
113 return "invalid type";
117 static void bch_dirent_to_text(struct cache_set *c, char *buf,
118 size_t size, struct bkey_s_c k)
120 struct bkey_s_c_dirent d;
124 d = bkey_s_c_to_dirent(k);
127 unsigned n = min_t(unsigned, size,
128 dirent_name_bytes(d));
129 memcpy(buf, d.v->d_name, n);
130 buf[size - 1] = '\0';
135 scnprintf(buf, size, " -> %llu", d.v->d_inum);
137 case BCH_DIRENT_WHITEOUT:
138 scnprintf(buf, size, "whiteout");
143 const struct bkey_ops bch_bkey_dirent_ops = {
144 .key_invalid = bch_dirent_invalid,
145 .val_to_text = bch_dirent_to_text,
148 static struct bkey_i_dirent *dirent_create_key(u8 type,
149 const struct qstr *name, u64 dst)
151 struct bkey_i_dirent *dirent;
152 unsigned u64s = BKEY_U64s +
153 DIV_ROUND_UP(sizeof(struct bch_dirent) + name->len,
156 dirent = kmalloc(u64s * sizeof(u64), GFP_NOFS);
160 bkey_dirent_init(&dirent->k_i);
161 dirent->k.u64s = u64s;
162 dirent->v.d_inum = cpu_to_le64(dst);
163 dirent->v.d_type = type;
165 memcpy(dirent->v.d_name, name->name, name->len);
166 memset(dirent->v.d_name + name->len, 0,
167 bkey_val_bytes(&dirent->k) -
168 (sizeof(struct bch_dirent) + name->len));
170 EBUG_ON(dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
175 int bch_dirent_create(struct cache_set *c, struct inode *dir, u8 type,
176 const struct qstr *name, u64 dst_inum)
178 struct bch_inode_info *ei = to_bch_ei(dir);
179 struct bkey_i_dirent *dirent;
182 dirent = dirent_create_key(type, name, dst_inum);
186 ret = bch_hash_set(dirent_hash_desc, &ei->str_hash, c,
187 ei->vfs_inode.i_ino, &ei->journal_seq,
188 &dirent->k_i, BCH_HASH_SET_MUST_CREATE);
194 static void dirent_copy_target(struct bkey_i_dirent *dst,
195 struct bkey_s_c_dirent src)
197 dst->v.d_inum = src.v->d_inum;
198 dst->v.d_type = src.v->d_type;
201 static struct bpos bch_dirent_pos(struct bch_inode_info *ei,
202 const struct qstr *name)
204 return POS(ei->vfs_inode.i_ino, bch_dirent_hash(&ei->str_hash, name));
207 int bch_dirent_rename(struct cache_set *c,
208 struct inode *src_dir, const struct qstr *src_name,
209 struct inode *dst_dir, const struct qstr *dst_name,
210 u64 *journal_seq, enum bch_rename_mode mode)
212 struct bch_inode_info *src_ei = to_bch_ei(src_dir);
213 struct bch_inode_info *dst_ei = to_bch_ei(dst_dir);
214 struct btree_iter src_iter, dst_iter, whiteout_iter;
215 struct bkey_s_c old_src, old_dst;
217 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
218 struct bpos src_pos = bch_dirent_pos(src_ei, src_name);
219 struct bpos dst_pos = bch_dirent_pos(dst_ei, dst_name);
223 bch_btree_iter_init_intent(&src_iter, c, BTREE_ID_DIRENTS, src_pos);
224 bch_btree_iter_init_intent(&dst_iter, c, BTREE_ID_DIRENTS, dst_pos);
225 bch_btree_iter_link(&src_iter, &dst_iter);
227 bch_btree_iter_init(&whiteout_iter, c, BTREE_ID_DIRENTS, src_pos);
228 bch_btree_iter_link(&src_iter, &whiteout_iter);
230 if (mode == BCH_RENAME_EXCHANGE) {
231 new_src = dirent_create_key(0, src_name, 0);
235 new_src = (void *) &delete;
238 new_dst = dirent_create_key(0, dst_name, 0);
243 * Note that on -EINTR/dropped locks we're not restarting the lookup
244 * from the original hashed position (like we do when creating dirents,
245 * in bch_hash_set) - we never move existing dirents to different slot:
247 old_src = bch_hash_lookup_at(dirent_hash_desc,
249 &src_iter, src_name);
250 if ((ret = btree_iter_err(old_src)))
253 ret = bch_hash_needs_whiteout(dirent_hash_desc,
255 &whiteout_iter, &src_iter);
261 * Note that in BCH_RENAME mode, we're _not_ checking if
262 * the target already exists - we're relying on the VFS
263 * to do that check for us for correctness:
265 old_dst = mode == BCH_RENAME
266 ? bch_hash_hole_at(dirent_hash_desc, &dst_iter)
267 : bch_hash_lookup_at(dirent_hash_desc,
269 &dst_iter, dst_name);
270 if ((ret = btree_iter_err(old_dst)))
275 bkey_init(&new_src->k);
276 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
278 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
279 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
281 * If we couldn't insert new_dst at its hashed
282 * position (dst_pos) due to a hash collision,
283 * and we're going to be deleting in
284 * between the hashed position and first empty
285 * slot we found - just overwrite the pos we
286 * were going to delete:
288 * Note: this is a correctness issue, in this
289 * situation bch_hash_needs_whiteout() could
290 * return false when the whiteout would have
291 * been needed if we inserted at the pos
292 * __dirent_find_hole() found
294 new_dst->k.p = src_iter.pos;
295 ret = bch_btree_insert_at(c, NULL, NULL,
298 BTREE_INSERT_ENTRY(&src_iter,
304 new_src->k.type = BCH_DIRENT_WHITEOUT;
306 case BCH_RENAME_OVERWRITE:
307 bkey_init(&new_src->k);
308 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
310 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
311 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
313 * Same case described above -
314 * bch_hash_needs_whiteout could spuriously
315 * return false, but we have to insert at
316 * dst_iter.pos because we're overwriting
319 new_src->k.type = BCH_DIRENT_WHITEOUT;
320 } else if (need_whiteout)
321 new_src->k.type = BCH_DIRENT_WHITEOUT;
323 case BCH_RENAME_EXCHANGE:
324 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
325 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
329 new_src->k.p = src_iter.pos;
330 new_dst->k.p = dst_iter.pos;
331 ret = bch_btree_insert_at(c, NULL, NULL, journal_seq,
333 BTREE_INSERT_ENTRY(&src_iter, &new_src->k_i),
334 BTREE_INSERT_ENTRY(&dst_iter, &new_dst->k_i));
339 bch_btree_iter_unlock(&whiteout_iter);
340 bch_btree_iter_unlock(&dst_iter);
341 bch_btree_iter_unlock(&src_iter);
343 if (new_src != (void *) &delete)
349 int bch_dirent_delete(struct cache_set *c, struct inode *dir,
350 const struct qstr *name)
352 struct bch_inode_info *ei = to_bch_ei(dir);
354 return bch_hash_delete(dirent_hash_desc, &ei->str_hash,
355 c, ei->vfs_inode.i_ino,
356 &ei->journal_seq, name);
359 u64 bch_dirent_lookup(struct cache_set *c, struct inode *dir,
360 const struct qstr *name)
362 struct bch_inode_info *ei = to_bch_ei(dir);
363 struct btree_iter iter;
367 k = bch_hash_lookup(dirent_hash_desc, &ei->str_hash, c,
368 ei->vfs_inode.i_ino, &iter, name);
370 bch_btree_iter_unlock(&iter);
374 inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
375 bch_btree_iter_unlock(&iter);
380 int bch_empty_dir(struct cache_set *c, u64 dir_inum)
382 struct btree_iter iter;
386 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS(dir_inum, 0), k) {
387 if (k.k->p.inode > dir_inum)
390 if (k.k->type == BCH_DIRENT) {
395 bch_btree_iter_unlock(&iter);
400 int bch_readdir(struct cache_set *c, struct file *file,
401 struct dir_context *ctx)
403 struct inode *inode = file_inode(file);
404 struct btree_iter iter;
406 struct bkey_s_c_dirent dirent;
409 if (!dir_emit_dots(file, ctx))
412 pr_debug("listing for %lu from %llu", inode->i_ino, ctx->pos);
414 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
415 POS(inode->i_ino, ctx->pos), k) {
416 if (k.k->type != BCH_DIRENT)
419 dirent = bkey_s_c_to_dirent(k);
421 pr_debug("saw %llu:%llu (%s) -> %llu",
422 k.k->p.inode, k.k->p.offset,
423 dirent.v->d_name, dirent.v->d_inum);
425 if (bkey_cmp(k.k->p, POS(inode->i_ino, ctx->pos)) < 0)
428 if (k.k->p.inode > inode->i_ino)
431 len = dirent_name_bytes(dirent);
433 pr_debug("emitting %s", dirent.v->d_name);
436 * XXX: dir_emit() can fault and block, while we're holding
439 if (!dir_emit(ctx, dirent.v->d_name, len,
440 le64_to_cpu(dirent.v->d_inum),
444 ctx->pos = k.k->p.offset + 1;
446 bch_btree_iter_unlock(&iter);