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,
189 *snapshot = d.k->p.snapshot;
191 if (likely(d.v->d_type != DT_SUBVOL)) {
192 *inum = le64_to_cpu(d.v->d_inum);
194 struct bch_subvolume s;
197 *subvol = le64_to_cpu(d.v->d_inum);
199 ret = bch2_subvolume_get(trans, *subvol, !is_fsck, BTREE_ITER_CACHED, &s);
201 *snapshot = le32_to_cpu(s.snapshot);
202 *inum = le64_to_cpu(s.inode);
208 static int bch2_dirent_read_target(struct btree_trans *trans, subvol_inum dir,
209 struct bkey_s_c_dirent d, subvol_inum *target)
214 ret = __bch2_dirent_read_target(trans, d, &target->subvol, &snapshot,
215 &target->inum, false);
217 target->subvol = dir.subvol;
222 int bch2_dirent_rename(struct btree_trans *trans,
223 subvol_inum src_dir, struct bch_hash_info *src_hash,
224 subvol_inum dst_dir, struct bch_hash_info *dst_hash,
225 const struct qstr *src_name, subvol_inum *src_inum, u64 *src_offset,
226 const struct qstr *dst_name, subvol_inum *dst_inum, u64 *dst_offset,
227 enum bch_rename_mode mode)
229 struct btree_iter src_iter = { NULL };
230 struct btree_iter dst_iter = { NULL };
231 struct bkey_s_c old_src, old_dst;
232 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
233 struct bpos dst_pos =
234 POS(dst_dir.inum, bch2_dirent_hash(dst_hash, dst_name));
237 if (src_dir.subvol != dst_dir.subvol)
240 memset(src_inum, 0, sizeof(*src_inum));
241 memset(dst_inum, 0, sizeof(*dst_inum));
246 * Note that in BCH_RENAME mode, we're _not_ checking if
247 * the target already exists - we're relying on the VFS
248 * to do that check for us for correctness:
250 ret = mode == BCH_RENAME
251 ? bch2_hash_hole(trans, &dst_iter, bch2_dirent_hash_desc,
252 dst_hash, dst_dir, dst_name)
253 : bch2_hash_lookup(trans, &dst_iter, bch2_dirent_hash_desc,
254 dst_hash, dst_dir, dst_name,
259 old_dst = bch2_btree_iter_peek_slot(&dst_iter);
260 ret = bkey_err(old_dst);
264 if (mode != BCH_RENAME) {
265 ret = bch2_dirent_read_target(trans, dst_dir,
266 bkey_s_c_to_dirent(old_dst), dst_inum);
270 if (mode != BCH_RENAME_EXCHANGE)
271 *src_offset = dst_iter.pos.offset;
274 ret = bch2_hash_lookup(trans, &src_iter, bch2_dirent_hash_desc,
275 src_hash, src_dir, src_name,
280 old_src = bch2_btree_iter_peek_slot(&src_iter);
281 ret = bkey_err(old_src);
285 ret = bch2_dirent_read_target(trans, src_dir,
286 bkey_s_c_to_dirent(old_src), src_inum);
290 /* Create new dst key: */
291 new_dst = dirent_create_key(trans, 0, dst_name, 0);
292 ret = PTR_ERR_OR_ZERO(new_dst);
296 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
297 new_dst->k.p = dst_iter.pos;
299 /* Create new src key: */
300 if (mode == BCH_RENAME_EXCHANGE) {
301 new_src = dirent_create_key(trans, 0, src_name, 0);
302 ret = PTR_ERR_OR_ZERO(new_src);
306 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
307 new_src->k.p = src_iter.pos;
309 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
310 ret = PTR_ERR_OR_ZERO(new_src);
314 bkey_init(&new_src->k);
315 new_src->k.p = src_iter.pos;
317 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
318 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
320 * We have a hash collision for the new dst key,
321 * and new_src - the key we're deleting - is between
322 * new_dst's hashed slot and the slot we're going to be
323 * inserting it into - oops. This will break the hash
324 * table if we don't deal with it:
326 if (mode == BCH_RENAME) {
328 * If we're not overwriting, we can just insert
329 * new_dst at the src position:
331 new_dst->k.p = src_iter.pos;
332 bch2_trans_update(trans, &src_iter,
336 /* If we're overwriting, we can't insert new_dst
337 * at a different slot because it has to
338 * overwrite old_dst - just make sure to use a
339 * whiteout when deleting src:
341 new_src->k.type = KEY_TYPE_hash_whiteout;
344 /* Check if we need a whiteout to delete src: */
345 ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
346 src_hash, &src_iter);
351 new_src->k.type = KEY_TYPE_hash_whiteout;
355 bch2_trans_update(trans, &src_iter, &new_src->k_i, 0);
356 bch2_trans_update(trans, &dst_iter, &new_dst->k_i, 0);
358 if (mode == BCH_RENAME_EXCHANGE)
359 *src_offset = new_src->k.p.offset;
360 *dst_offset = new_dst->k.p.offset;
362 bch2_trans_iter_exit(trans, &src_iter);
363 bch2_trans_iter_exit(trans, &dst_iter);
367 int __bch2_dirent_lookup_trans(struct btree_trans *trans,
368 struct btree_iter *iter,
370 const struct bch_hash_info *hash_info,
371 const struct qstr *name, subvol_inum *inum,
375 struct bkey_s_c_dirent d;
379 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
383 ret = bch2_hash_lookup(trans, iter, bch2_dirent_hash_desc,
384 hash_info, dir, name, flags);
388 k = bch2_btree_iter_peek_slot(iter);
391 bch2_trans_iter_exit(trans, iter);
395 d = bkey_s_c_to_dirent(k);
397 ret = bch2_dirent_read_target(trans, dir, d, inum);
399 bch2_trans_iter_exit(trans, iter);
404 u64 bch2_dirent_lookup(struct bch_fs *c, subvol_inum dir,
405 const struct bch_hash_info *hash_info,
406 const struct qstr *name, subvol_inum *inum)
408 struct btree_trans trans;
409 struct btree_iter iter;
412 bch2_trans_init(&trans, c, 0, 0);
414 bch2_trans_begin(&trans);
416 ret = __bch2_dirent_lookup_trans(&trans, &iter, dir, hash_info,
419 bch2_trans_iter_exit(&trans, &iter);
422 bch2_trans_exit(&trans);
426 int bch2_empty_dir_trans(struct btree_trans *trans, subvol_inum dir)
428 struct btree_iter iter;
433 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
437 for_each_btree_key(trans, iter, BTREE_ID_dirents,
438 SPOS(dir.inum, 0, snapshot), 0, k, ret) {
439 if (k.k->p.inode > dir.inum)
442 if (k.k->type == KEY_TYPE_dirent) {
447 bch2_trans_iter_exit(trans, &iter);
452 int bch2_readdir(struct bch_fs *c, subvol_inum inum, struct dir_context *ctx)
454 struct btree_trans trans;
455 struct btree_iter iter;
457 struct bkey_s_c_dirent dirent;
461 bch2_trans_init(&trans, c, 0, 0);
463 bch2_trans_begin(&trans);
465 ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
469 for_each_btree_key(&trans, iter, BTREE_ID_dirents,
470 SPOS(inum.inum, ctx->pos, snapshot), 0, k, ret) {
471 if (k.k->p.inode > inum.inum)
474 if (k.k->type != KEY_TYPE_dirent)
477 dirent = bkey_s_c_to_dirent(k);
480 * XXX: dir_emit() can fault and block, while we're holding
483 ctx->pos = dirent.k->p.offset;
484 if (!dir_emit(ctx, dirent.v->d_name,
485 bch2_dirent_name_bytes(dirent),
486 le64_to_cpu(dirent.v->d_inum),
487 vfs_d_type(dirent.v->d_type)))
489 ctx->pos = dirent.k->p.offset + 1;
491 bch2_trans_iter_exit(&trans, &iter);
496 ret = bch2_trans_exit(&trans) ?: ret;