1 // SPDX-License-Identifier: GPL-2.0
4 #include "bkey_methods.h"
5 #include "btree_update.h"
11 #include "subvolume.h"
13 #include <linux/dcache.h>
15 unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
17 unsigned len = bkey_val_bytes(d.k) -
18 offsetof(struct bch_dirent, d_name);
20 return strnlen(d.v->d_name, len);
23 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
24 const struct qstr *name)
26 struct bch_str_hash_ctx ctx;
28 bch2_str_hash_init(&ctx, info);
29 bch2_str_hash_update(&ctx, info, name->name, name->len);
31 /* [0,2) reserved for dots */
32 return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
35 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
37 return bch2_dirent_hash(info, key);
40 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
42 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
43 struct qstr name = QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
45 return bch2_dirent_hash(info, &name);
48 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
50 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
51 int len = bch2_dirent_name_bytes(l);
52 const struct qstr *r = _r;
54 return len - r->len ?: memcmp(l.v->d_name, r->name, len);
57 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
59 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
60 struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
61 int l_len = bch2_dirent_name_bytes(l);
62 int r_len = bch2_dirent_name_bytes(r);
64 return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
67 const struct bch_hash_desc bch2_dirent_hash_desc = {
68 .btree_id = BTREE_ID_dirents,
69 .key_type = KEY_TYPE_dirent,
70 .hash_key = dirent_hash_key,
71 .hash_bkey = dirent_hash_bkey,
72 .cmp_key = dirent_cmp_key,
73 .cmp_bkey = dirent_cmp_bkey,
76 const char *bch2_dirent_invalid(const struct bch_fs *c, struct bkey_s_c k)
78 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
81 if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent))
82 return "value too small";
84 len = bch2_dirent_name_bytes(d);
88 if (bkey_val_u64s(k.k) > dirent_val_u64s(len))
89 return "value too big";
91 if (len > BCH_NAME_MAX)
92 return "dirent name too big";
94 if (len == 1 && !memcmp(d.v->d_name, ".", 1))
95 return "invalid name";
97 if (len == 2 && !memcmp(d.v->d_name, "..", 2))
98 return "invalid name";
100 if (memchr(d.v->d_name, '/', len))
101 return "invalid name";
103 if (d.v->d_type != DT_SUBVOL &&
104 le64_to_cpu(d.v->d_inum) == d.k->p.inode)
105 return "dirent points to own directory";
110 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
113 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
115 bch_scnmemcpy(out, d.v->d_name,
116 bch2_dirent_name_bytes(d));
117 pr_buf(out, " -> %llu type %s", d.v->d_inum,
118 d.v->d_type < BCH_DT_MAX
119 ? bch2_d_types[d.v->d_type]
123 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
124 u8 type, const struct qstr *name, u64 dst)
126 struct bkey_i_dirent *dirent;
127 unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
129 if (name->len > BCH_NAME_MAX)
130 return ERR_PTR(-ENAMETOOLONG);
132 BUG_ON(u64s > U8_MAX);
134 dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
138 bkey_dirent_init(&dirent->k_i);
139 dirent->k.u64s = u64s;
140 dirent->v.d_inum = cpu_to_le64(dst);
141 dirent->v.d_type = type;
143 memcpy(dirent->v.d_name, name->name, name->len);
144 memset(dirent->v.d_name + name->len, 0,
145 bkey_val_bytes(&dirent->k) -
146 offsetof(struct bch_dirent, d_name) -
149 EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
154 int bch2_dirent_create(struct btree_trans *trans, subvol_inum dir,
155 const struct bch_hash_info *hash_info,
156 u8 type, const struct qstr *name, u64 dst_inum,
157 u64 *dir_offset, int flags)
159 struct bkey_i_dirent *dirent;
162 dirent = dirent_create_key(trans, type, name, dst_inum);
163 ret = PTR_ERR_OR_ZERO(dirent);
167 ret = bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
168 dir, &dirent->k_i, flags);
169 *dir_offset = dirent->k.p.offset;
174 static void dirent_copy_target(struct bkey_i_dirent *dst,
175 struct bkey_s_c_dirent src)
177 dst->v.d_inum = src.v->d_inum;
178 dst->v.d_type = src.v->d_type;
181 int __bch2_dirent_read_target(struct btree_trans *trans,
182 struct bkey_s_c_dirent d,
183 u32 *subvol, u32 *snapshot, u64 *inum,
186 struct bch_subvolume s;
190 *snapshot = d.k->p.snapshot;
192 if (likely(d.v->d_type != DT_SUBVOL)) {
193 *inum = le64_to_cpu(d.v->d_inum);
195 *subvol = le64_to_cpu(d.v->d_inum);
197 ret = bch2_subvolume_get(trans, *subvol, !is_fsck, BTREE_ITER_CACHED, &s);
199 *snapshot = le32_to_cpu(s.snapshot);
200 *inum = le64_to_cpu(s.inode);
206 static int bch2_dirent_read_target(struct btree_trans *trans, subvol_inum dir,
207 struct bkey_s_c_dirent d, subvol_inum *target)
212 ret = __bch2_dirent_read_target(trans, d, &target->subvol, &snapshot,
213 &target->inum, false);
215 target->subvol = dir.subvol;
220 int bch2_dirent_rename(struct btree_trans *trans,
221 subvol_inum src_dir, struct bch_hash_info *src_hash,
222 subvol_inum dst_dir, struct bch_hash_info *dst_hash,
223 const struct qstr *src_name, subvol_inum *src_inum, u64 *src_offset,
224 const struct qstr *dst_name, subvol_inum *dst_inum, u64 *dst_offset,
225 enum bch_rename_mode mode)
227 struct btree_iter src_iter = { NULL };
228 struct btree_iter dst_iter = { NULL };
229 struct bkey_s_c old_src, old_dst;
230 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
231 struct bpos dst_pos =
232 POS(dst_dir.inum, bch2_dirent_hash(dst_hash, dst_name));
235 if (src_dir.subvol != dst_dir.subvol)
238 memset(src_inum, 0, sizeof(*src_inum));
239 memset(dst_inum, 0, sizeof(*dst_inum));
244 * Note that in BCH_RENAME mode, we're _not_ checking if
245 * the target already exists - we're relying on the VFS
246 * to do that check for us for correctness:
248 ret = mode == BCH_RENAME
249 ? bch2_hash_hole(trans, &dst_iter, bch2_dirent_hash_desc,
250 dst_hash, dst_dir, dst_name)
251 : bch2_hash_lookup(trans, &dst_iter, bch2_dirent_hash_desc,
252 dst_hash, dst_dir, dst_name,
257 old_dst = bch2_btree_iter_peek_slot(&dst_iter);
258 ret = bkey_err(old_dst);
262 if (mode != BCH_RENAME) {
263 ret = bch2_dirent_read_target(trans, dst_dir,
264 bkey_s_c_to_dirent(old_dst), dst_inum);
268 if (mode != BCH_RENAME_EXCHANGE)
269 *src_offset = dst_iter.pos.offset;
272 ret = bch2_hash_lookup(trans, &src_iter, bch2_dirent_hash_desc,
273 src_hash, src_dir, src_name,
278 old_src = bch2_btree_iter_peek_slot(&src_iter);
279 ret = bkey_err(old_src);
283 ret = bch2_dirent_read_target(trans, src_dir,
284 bkey_s_c_to_dirent(old_src), src_inum);
288 /* Create new dst key: */
289 new_dst = dirent_create_key(trans, 0, dst_name, 0);
290 ret = PTR_ERR_OR_ZERO(new_dst);
294 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
295 new_dst->k.p = dst_iter.pos;
297 /* Create new src key: */
298 if (mode == BCH_RENAME_EXCHANGE) {
299 new_src = dirent_create_key(trans, 0, src_name, 0);
300 ret = PTR_ERR_OR_ZERO(new_src);
304 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
305 new_src->k.p = src_iter.pos;
307 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
308 ret = PTR_ERR_OR_ZERO(new_src);
312 bkey_init(&new_src->k);
313 new_src->k.p = src_iter.pos;
315 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
316 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
318 * We have a hash collision for the new dst key,
319 * and new_src - the key we're deleting - is between
320 * new_dst's hashed slot and the slot we're going to be
321 * inserting it into - oops. This will break the hash
322 * table if we don't deal with it:
324 if (mode == BCH_RENAME) {
326 * If we're not overwriting, we can just insert
327 * new_dst at the src position:
329 new_dst->k.p = src_iter.pos;
330 bch2_trans_update(trans, &src_iter,
334 /* If we're overwriting, we can't insert new_dst
335 * at a different slot because it has to
336 * overwrite old_dst - just make sure to use a
337 * whiteout when deleting src:
339 new_src->k.type = KEY_TYPE_hash_whiteout;
342 /* Check if we need a whiteout to delete src: */
343 ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
344 src_hash, &src_iter);
349 new_src->k.type = KEY_TYPE_hash_whiteout;
353 bch2_trans_update(trans, &src_iter, &new_src->k_i, 0);
354 bch2_trans_update(trans, &dst_iter, &new_dst->k_i, 0);
356 if (mode == BCH_RENAME_EXCHANGE)
357 *src_offset = new_src->k.p.offset;
358 *dst_offset = new_dst->k.p.offset;
360 bch2_trans_iter_exit(trans, &src_iter);
361 bch2_trans_iter_exit(trans, &dst_iter);
365 int __bch2_dirent_lookup_trans(struct btree_trans *trans,
366 struct btree_iter *iter,
368 const struct bch_hash_info *hash_info,
369 const struct qstr *name, subvol_inum *inum,
373 struct bkey_s_c_dirent d;
377 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
381 ret = bch2_hash_lookup(trans, iter, bch2_dirent_hash_desc,
382 hash_info, dir, name, flags);
386 k = bch2_btree_iter_peek_slot(iter);
389 bch2_trans_iter_exit(trans, iter);
393 d = bkey_s_c_to_dirent(k);
395 ret = bch2_dirent_read_target(trans, dir, d, inum);
397 bch2_trans_iter_exit(trans, iter);
402 u64 bch2_dirent_lookup(struct bch_fs *c, subvol_inum dir,
403 const struct bch_hash_info *hash_info,
404 const struct qstr *name, subvol_inum *inum)
406 struct btree_trans trans;
407 struct btree_iter iter;
410 bch2_trans_init(&trans, c, 0, 0);
412 bch2_trans_begin(&trans);
414 ret = __bch2_dirent_lookup_trans(&trans, &iter, dir, hash_info,
417 bch2_trans_iter_exit(&trans, &iter);
420 bch2_trans_exit(&trans);
424 int bch2_empty_dir_trans(struct btree_trans *trans, subvol_inum dir)
426 struct btree_iter iter;
431 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
435 for_each_btree_key(trans, iter, BTREE_ID_dirents,
436 SPOS(dir.inum, 0, snapshot), 0, k, ret) {
437 if (k.k->p.inode > dir.inum)
440 if (k.k->type == KEY_TYPE_dirent) {
445 bch2_trans_iter_exit(trans, &iter);
450 int bch2_readdir(struct bch_fs *c, subvol_inum inum, struct dir_context *ctx)
452 struct btree_trans trans;
453 struct btree_iter iter;
455 struct bkey_s_c_dirent dirent;
459 bch2_trans_init(&trans, c, 0, 0);
461 bch2_trans_begin(&trans);
463 ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
467 for_each_btree_key(&trans, iter, BTREE_ID_dirents,
468 SPOS(inum.inum, ctx->pos, snapshot), 0, k, ret) {
469 if (k.k->p.inode > inum.inum)
472 if (k.k->type != KEY_TYPE_dirent)
475 dirent = bkey_s_c_to_dirent(k);
478 * XXX: dir_emit() can fault and block, while we're holding
481 ctx->pos = dirent.k->p.offset;
482 if (!dir_emit(ctx, dirent.v->d_name,
483 bch2_dirent_name_bytes(dirent),
484 le64_to_cpu(dirent.v->d_inum),
485 vfs_d_type(dirent.v->d_type)))
487 ctx->pos = dirent.k->p.offset + 1;
489 bch2_trans_iter_exit(&trans, &iter);
494 ret = bch2_trans_exit(&trans) ?: ret;