1 // SPDX-License-Identifier: GPL-2.0
5 #include "bkey_methods.h"
6 #include "btree_update.h"
12 #include "subvolume.h"
14 #include <linux/dcache.h>
16 unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
18 unsigned len = bkey_val_bytes(d.k) -
19 offsetof(struct bch_dirent, d_name);
21 return strnlen(d.v->d_name, len);
24 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
25 const struct qstr *name)
27 struct bch_str_hash_ctx ctx;
29 bch2_str_hash_init(&ctx, info);
30 bch2_str_hash_update(&ctx, info, name->name, name->len);
32 /* [0,2) reserved for dots */
33 return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
36 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
38 return bch2_dirent_hash(info, key);
41 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
43 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
44 struct qstr name = QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
46 return bch2_dirent_hash(info, &name);
49 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
51 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
52 int len = bch2_dirent_name_bytes(l);
53 const struct qstr *r = _r;
55 return len - r->len ?: memcmp(l.v->d_name, r->name, len);
58 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
60 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
61 struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
62 int l_len = bch2_dirent_name_bytes(l);
63 int r_len = bch2_dirent_name_bytes(r);
65 return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
68 static bool dirent_is_visible(subvol_inum inum, struct bkey_s_c k)
70 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
72 if (d.v->d_type == DT_SUBVOL)
73 return le32_to_cpu(d.v->d_parent_subvol) == inum.subvol;
77 const struct bch_hash_desc bch2_dirent_hash_desc = {
78 .btree_id = BTREE_ID_dirents,
79 .key_type = KEY_TYPE_dirent,
80 .hash_key = dirent_hash_key,
81 .hash_bkey = dirent_hash_bkey,
82 .cmp_key = dirent_cmp_key,
83 .cmp_bkey = dirent_cmp_bkey,
84 .is_visible = dirent_is_visible,
87 int bch2_dirent_invalid(const struct bch_fs *c, struct bkey_s_c k,
88 enum bkey_invalid_flags flags,
91 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
94 len = bch2_dirent_name_bytes(d);
96 prt_printf(err, "empty name");
97 return -BCH_ERR_invalid_bkey;
100 if (bkey_val_u64s(k.k) > dirent_val_u64s(len)) {
101 prt_printf(err, "value too big (%zu > %u)",
102 bkey_val_u64s(k.k), dirent_val_u64s(len));
103 return -BCH_ERR_invalid_bkey;
106 if (len > BCH_NAME_MAX) {
107 prt_printf(err, "dirent name too big (%u > %u)",
109 return -BCH_ERR_invalid_bkey;
112 if (len == 1 && !memcmp(d.v->d_name, ".", 1)) {
113 prt_printf(err, "invalid name");
114 return -BCH_ERR_invalid_bkey;
117 if (len == 2 && !memcmp(d.v->d_name, "..", 2)) {
118 prt_printf(err, "invalid name");
119 return -BCH_ERR_invalid_bkey;
122 if (memchr(d.v->d_name, '/', len)) {
123 prt_printf(err, "invalid name");
124 return -BCH_ERR_invalid_bkey;
127 if (d.v->d_type != DT_SUBVOL &&
128 le64_to_cpu(d.v->d_inum) == d.k->p.inode) {
129 prt_printf(err, "dirent points to own directory");
130 return -BCH_ERR_invalid_bkey;
136 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
139 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
141 prt_printf(out, "%.*s -> %llu type %s",
142 bch2_dirent_name_bytes(d),
144 d.v->d_type != DT_SUBVOL
145 ? le64_to_cpu(d.v->d_inum)
146 : le32_to_cpu(d.v->d_child_subvol),
147 bch2_d_type_str(d.v->d_type));
150 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
151 subvol_inum dir, u8 type,
152 const struct qstr *name, u64 dst)
154 struct bkey_i_dirent *dirent;
155 unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
157 if (name->len > BCH_NAME_MAX)
158 return ERR_PTR(-ENAMETOOLONG);
160 BUG_ON(u64s > U8_MAX);
162 dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
166 bkey_dirent_init(&dirent->k_i);
167 dirent->k.u64s = u64s;
169 if (type != DT_SUBVOL) {
170 dirent->v.d_inum = cpu_to_le64(dst);
172 dirent->v.d_parent_subvol = cpu_to_le32(dir.subvol);
173 dirent->v.d_child_subvol = cpu_to_le32(dst);
176 dirent->v.d_type = type;
178 memcpy(dirent->v.d_name, name->name, name->len);
179 memset(dirent->v.d_name + name->len, 0,
180 bkey_val_bytes(&dirent->k) -
181 offsetof(struct bch_dirent, d_name) -
184 EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
189 int bch2_dirent_create(struct btree_trans *trans, subvol_inum dir,
190 const struct bch_hash_info *hash_info,
191 u8 type, const struct qstr *name, u64 dst_inum,
192 u64 *dir_offset, int flags)
194 struct bkey_i_dirent *dirent;
197 dirent = dirent_create_key(trans, dir, type, name, dst_inum);
198 ret = PTR_ERR_OR_ZERO(dirent);
202 ret = bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
203 dir, &dirent->k_i, flags);
204 *dir_offset = dirent->k.p.offset;
209 static void dirent_copy_target(struct bkey_i_dirent *dst,
210 struct bkey_s_c_dirent src)
212 dst->v.d_inum = src.v->d_inum;
213 dst->v.d_type = src.v->d_type;
216 int bch2_dirent_read_target(struct btree_trans *trans, subvol_inum dir,
217 struct bkey_s_c_dirent d, subvol_inum *target)
219 struct bch_subvolume s;
222 if (d.v->d_type == DT_SUBVOL &&
223 le32_to_cpu(d.v->d_parent_subvol) != dir.subvol)
226 if (likely(d.v->d_type != DT_SUBVOL)) {
227 target->subvol = dir.subvol;
228 target->inum = le64_to_cpu(d.v->d_inum);
230 target->subvol = le32_to_cpu(d.v->d_child_subvol);
232 ret = bch2_subvolume_get(trans, target->subvol, true, BTREE_ITER_CACHED, &s);
234 target->inum = le64_to_cpu(s.inode);
240 int bch2_dirent_rename(struct btree_trans *trans,
241 subvol_inum src_dir, struct bch_hash_info *src_hash,
242 subvol_inum dst_dir, struct bch_hash_info *dst_hash,
243 const struct qstr *src_name, subvol_inum *src_inum, u64 *src_offset,
244 const struct qstr *dst_name, subvol_inum *dst_inum, u64 *dst_offset,
245 enum bch_rename_mode mode)
247 struct btree_iter src_iter = { NULL };
248 struct btree_iter dst_iter = { NULL };
249 struct bkey_s_c old_src, old_dst = bkey_s_c_null;
250 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
251 struct bpos dst_pos =
252 POS(dst_dir.inum, bch2_dirent_hash(dst_hash, dst_name));
253 unsigned src_type = 0, dst_type = 0, src_update_flags = 0;
256 if (src_dir.subvol != dst_dir.subvol)
259 memset(src_inum, 0, sizeof(*src_inum));
260 memset(dst_inum, 0, sizeof(*dst_inum));
263 ret = bch2_hash_lookup(trans, &src_iter, bch2_dirent_hash_desc,
264 src_hash, src_dir, src_name,
269 old_src = bch2_btree_iter_peek_slot(&src_iter);
270 ret = bkey_err(old_src);
274 ret = bch2_dirent_read_target(trans, src_dir,
275 bkey_s_c_to_dirent(old_src), src_inum);
279 src_type = bkey_s_c_to_dirent(old_src).v->d_type;
281 if (src_type == DT_SUBVOL && mode == BCH_RENAME_EXCHANGE)
286 if (mode == BCH_RENAME) {
288 * Note that we're _not_ checking if the target already exists -
289 * we're relying on the VFS to do that check for us for
292 ret = bch2_hash_hole(trans, &dst_iter, bch2_dirent_hash_desc,
293 dst_hash, dst_dir, dst_name);
297 ret = bch2_hash_lookup(trans, &dst_iter, bch2_dirent_hash_desc,
298 dst_hash, dst_dir, dst_name,
303 old_dst = bch2_btree_iter_peek_slot(&dst_iter);
304 ret = bkey_err(old_dst);
308 ret = bch2_dirent_read_target(trans, dst_dir,
309 bkey_s_c_to_dirent(old_dst), dst_inum);
313 dst_type = bkey_s_c_to_dirent(old_dst).v->d_type;
315 if (dst_type == DT_SUBVOL)
319 if (mode != BCH_RENAME_EXCHANGE)
320 *src_offset = dst_iter.pos.offset;
322 /* Create new dst key: */
323 new_dst = dirent_create_key(trans, dst_dir, 0, dst_name, 0);
324 ret = PTR_ERR_OR_ZERO(new_dst);
328 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
329 new_dst->k.p = dst_iter.pos;
331 /* Create new src key: */
332 if (mode == BCH_RENAME_EXCHANGE) {
333 new_src = dirent_create_key(trans, src_dir, 0, src_name, 0);
334 ret = PTR_ERR_OR_ZERO(new_src);
338 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
339 new_src->k.p = src_iter.pos;
341 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
342 ret = PTR_ERR_OR_ZERO(new_src);
346 bkey_init(&new_src->k);
347 new_src->k.p = src_iter.pos;
349 if (bkey_le(dst_pos, src_iter.pos) &&
350 bkey_lt(src_iter.pos, dst_iter.pos)) {
352 * We have a hash collision for the new dst key,
353 * and new_src - the key we're deleting - is between
354 * new_dst's hashed slot and the slot we're going to be
355 * inserting it into - oops. This will break the hash
356 * table if we don't deal with it:
358 if (mode == BCH_RENAME) {
360 * If we're not overwriting, we can just insert
361 * new_dst at the src position:
364 new_src->k.p = src_iter.pos;
367 /* If we're overwriting, we can't insert new_dst
368 * at a different slot because it has to
369 * overwrite old_dst - just make sure to use a
370 * whiteout when deleting src:
372 new_src->k.type = KEY_TYPE_hash_whiteout;
375 /* Check if we need a whiteout to delete src: */
376 ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
377 src_hash, &src_iter);
382 new_src->k.type = KEY_TYPE_hash_whiteout;
386 ret = bch2_trans_update(trans, &dst_iter, &new_dst->k_i, 0);
392 * If we're deleting a subvolume, we need to really delete the dirent,
393 * not just emit a whiteout in the current snapshot:
395 if (src_type == DT_SUBVOL) {
396 bch2_btree_iter_set_snapshot(&src_iter, old_src.k->p.snapshot);
397 ret = bch2_btree_iter_traverse(&src_iter);
401 new_src->k.p = src_iter.pos;
402 src_update_flags |= BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE;
405 ret = bch2_trans_update(trans, &src_iter, &new_src->k_i, src_update_flags);
409 if (mode == BCH_RENAME_EXCHANGE)
410 *src_offset = new_src->k.p.offset;
411 *dst_offset = new_dst->k.p.offset;
413 bch2_trans_iter_exit(trans, &src_iter);
414 bch2_trans_iter_exit(trans, &dst_iter);
418 int __bch2_dirent_lookup_trans(struct btree_trans *trans,
419 struct btree_iter *iter,
421 const struct bch_hash_info *hash_info,
422 const struct qstr *name, subvol_inum *inum,
426 struct bkey_s_c_dirent d;
430 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
434 ret = bch2_hash_lookup(trans, iter, bch2_dirent_hash_desc,
435 hash_info, dir, name, flags);
439 k = bch2_btree_iter_peek_slot(iter);
444 d = bkey_s_c_to_dirent(k);
446 ret = bch2_dirent_read_target(trans, dir, d, inum);
451 bch2_trans_iter_exit(trans, iter);
456 u64 bch2_dirent_lookup(struct bch_fs *c, subvol_inum dir,
457 const struct bch_hash_info *hash_info,
458 const struct qstr *name, subvol_inum *inum)
460 struct btree_trans trans;
461 struct btree_iter iter;
464 bch2_trans_init(&trans, c, 0, 0);
466 bch2_trans_begin(&trans);
468 ret = __bch2_dirent_lookup_trans(&trans, &iter, dir, hash_info,
470 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
473 bch2_trans_iter_exit(&trans, &iter);
474 bch2_trans_exit(&trans);
478 int bch2_empty_dir_trans(struct btree_trans *trans, subvol_inum dir)
480 struct btree_iter iter;
485 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
489 for_each_btree_key_upto_norestart(trans, iter, BTREE_ID_dirents,
490 SPOS(dir.inum, 0, snapshot),
491 POS(dir.inum, U64_MAX), 0, k, ret)
492 if (k.k->type == KEY_TYPE_dirent) {
496 bch2_trans_iter_exit(trans, &iter);
501 int bch2_readdir(struct bch_fs *c, subvol_inum inum, struct dir_context *ctx)
503 struct btree_trans trans;
504 struct btree_iter iter;
506 struct bkey_s_c_dirent dirent;
512 bch2_bkey_buf_init(&sk);
513 bch2_trans_init(&trans, c, 0, 0);
515 bch2_trans_begin(&trans);
517 ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
521 for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_dirents,
522 SPOS(inum.inum, ctx->pos, snapshot),
523 POS(inum.inum, U64_MAX), 0, k, ret) {
524 if (k.k->type != KEY_TYPE_dirent)
527 dirent = bkey_s_c_to_dirent(k);
529 ret = bch2_dirent_read_target(&trans, inum, dirent, &target);
535 /* dir_emit() can fault and block: */
536 bch2_bkey_buf_reassemble(&sk, c, k);
537 dirent = bkey_i_to_s_c_dirent(sk.k);
538 bch2_trans_unlock(&trans);
540 ctx->pos = dirent.k->p.offset;
541 if (!dir_emit(ctx, dirent.v->d_name,
542 bch2_dirent_name_bytes(dirent),
544 vfs_d_type(dirent.v->d_type)))
546 ctx->pos = dirent.k->p.offset + 1;
549 * read_target looks up subvolumes, we can overflow paths if the
550 * directory has many subvolumes in it
552 ret = btree_trans_too_many_iters(&trans);
556 bch2_trans_iter_exit(&trans, &iter);
558 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
561 bch2_trans_exit(&trans);
562 bch2_bkey_buf_exit(&sk, c);