1 // SPDX-License-Identifier: GPL-2.0
4 #include "bkey_methods.h"
5 #include "btree_update.h"
12 #include <linux/dcache.h>
14 unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
16 unsigned len = bkey_val_bytes(d.k) -
17 offsetof(struct bch_dirent, d_name);
19 return strnlen(d.v->d_name, len);
22 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
23 const struct qstr *name)
25 struct bch_str_hash_ctx ctx;
27 bch2_str_hash_init(&ctx, info);
28 bch2_str_hash_update(&ctx, info, name->name, name->len);
30 /* [0,2) reserved for dots */
31 return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
34 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
36 return bch2_dirent_hash(info, key);
39 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
41 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
42 struct qstr name = QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
44 return bch2_dirent_hash(info, &name);
47 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
49 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
50 int len = bch2_dirent_name_bytes(l);
51 const struct qstr *r = _r;
53 return len - r->len ?: memcmp(l.v->d_name, r->name, len);
56 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
58 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
59 struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
60 int l_len = bch2_dirent_name_bytes(l);
61 int r_len = bch2_dirent_name_bytes(r);
63 return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
66 const struct bch_hash_desc bch2_dirent_hash_desc = {
67 .btree_id = BTREE_ID_DIRENTS,
68 .key_type = KEY_TYPE_dirent,
69 .hash_key = dirent_hash_key,
70 .hash_bkey = dirent_hash_bkey,
71 .cmp_key = dirent_cmp_key,
72 .cmp_bkey = dirent_cmp_bkey,
75 const char *bch2_dirent_invalid(const struct bch_fs *c, struct bkey_s_c k)
77 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
80 if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent))
81 return "value too small";
83 len = bch2_dirent_name_bytes(d);
88 * older versions of bcachefs were buggy and creating dirent
89 * keys that were bigger than necessary:
91 if (bkey_val_u64s(k.k) > dirent_val_u64s(len + 7))
92 return "value too big";
94 if (len > BCH_NAME_MAX)
95 return "dirent name too big";
100 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
103 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
105 bch_scnmemcpy(out, d.v->d_name,
106 bch2_dirent_name_bytes(d));
107 pr_buf(out, " -> %llu", d.v->d_inum);
110 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
111 u8 type, const struct qstr *name, u64 dst)
113 struct bkey_i_dirent *dirent;
114 unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
116 if (name->len > BCH_NAME_MAX)
117 return ERR_PTR(-ENAMETOOLONG);
119 BUG_ON(u64s > U8_MAX);
121 dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
125 bkey_dirent_init(&dirent->k_i);
126 dirent->k.u64s = u64s;
127 dirent->v.d_inum = cpu_to_le64(dst);
128 dirent->v.d_type = type;
130 memcpy(dirent->v.d_name, name->name, name->len);
131 memset(dirent->v.d_name + name->len, 0,
132 bkey_val_bytes(&dirent->k) -
133 offsetof(struct bch_dirent, d_name) -
136 EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
141 int bch2_dirent_create(struct btree_trans *trans,
142 u64 dir_inum, const struct bch_hash_info *hash_info,
143 u8 type, const struct qstr *name, u64 dst_inum,
146 struct bkey_i_dirent *dirent;
149 dirent = dirent_create_key(trans, type, name, dst_inum);
150 ret = PTR_ERR_OR_ZERO(dirent);
154 return bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
155 dir_inum, &dirent->k_i, flags);
158 static void dirent_copy_target(struct bkey_i_dirent *dst,
159 struct bkey_s_c_dirent src)
161 dst->v.d_inum = src.v->d_inum;
162 dst->v.d_type = src.v->d_type;
165 int bch2_dirent_rename(struct btree_trans *trans,
166 u64 src_dir, struct bch_hash_info *src_hash,
167 u64 dst_dir, struct bch_hash_info *dst_hash,
168 const struct qstr *src_name, u64 *src_inum,
169 const struct qstr *dst_name, u64 *dst_inum,
170 enum bch_rename_mode mode)
172 struct btree_iter *src_iter, *dst_iter;
173 struct bkey_s_c old_src, old_dst;
174 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
175 struct bpos dst_pos =
176 POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
179 *src_inum = *dst_inum = 0;
184 * Note that in BCH_RENAME mode, we're _not_ checking if
185 * the target already exists - we're relying on the VFS
186 * to do that check for us for correctness:
188 dst_iter = mode == BCH_RENAME
189 ? bch2_hash_hole(trans, bch2_dirent_hash_desc,
190 dst_hash, dst_dir, dst_name)
191 : bch2_hash_lookup(trans, bch2_dirent_hash_desc,
192 dst_hash, dst_dir, dst_name,
194 if (IS_ERR(dst_iter))
195 return PTR_ERR(dst_iter);
196 old_dst = bch2_btree_iter_peek_slot(dst_iter);
198 if (mode != BCH_RENAME)
199 *dst_inum = le64_to_cpu(bkey_s_c_to_dirent(old_dst).v->d_inum);
202 src_iter = bch2_hash_lookup(trans, bch2_dirent_hash_desc,
203 src_hash, src_dir, src_name,
205 if (IS_ERR(src_iter))
206 return PTR_ERR(src_iter);
207 old_src = bch2_btree_iter_peek_slot(src_iter);
208 *src_inum = le64_to_cpu(bkey_s_c_to_dirent(old_src).v->d_inum);
210 /* Create new dst key: */
211 new_dst = dirent_create_key(trans, 0, dst_name, 0);
213 return PTR_ERR(new_dst);
215 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
216 new_dst->k.p = dst_iter->pos;
218 /* Create new src key: */
219 if (mode == BCH_RENAME_EXCHANGE) {
220 new_src = dirent_create_key(trans, 0, src_name, 0);
222 return PTR_ERR(new_src);
224 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
225 new_src->k.p = src_iter->pos;
227 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
229 return PTR_ERR(new_src);
230 bkey_init(&new_src->k);
231 new_src->k.p = src_iter->pos;
233 if (bkey_cmp(dst_pos, src_iter->pos) <= 0 &&
234 bkey_cmp(src_iter->pos, dst_iter->pos) < 0) {
236 * We have a hash collision for the new dst key,
237 * and new_src - the key we're deleting - is between
238 * new_dst's hashed slot and the slot we're going to be
239 * inserting it into - oops. This will break the hash
240 * table if we don't deal with it:
242 if (mode == BCH_RENAME) {
244 * If we're not overwriting, we can just insert
245 * new_dst at the src position:
247 new_dst->k.p = src_iter->pos;
248 bch2_trans_update(trans, src_iter,
252 /* If we're overwriting, we can't insert new_dst
253 * at a different slot because it has to
254 * overwrite old_dst - just make sure to use a
255 * whiteout when deleting src:
257 new_src->k.type = KEY_TYPE_whiteout;
260 /* Check if we need a whiteout to delete src: */
261 ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
267 new_src->k.type = KEY_TYPE_whiteout;
271 bch2_trans_update(trans, src_iter, &new_src->k_i);
272 bch2_trans_update(trans, dst_iter, &new_dst->k_i);
276 int bch2_dirent_delete_at(struct btree_trans *trans,
277 const struct bch_hash_info *hash_info,
278 struct btree_iter *iter)
280 return bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
284 int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
285 const struct bch_hash_info *hash_info,
286 const struct qstr *name,
289 return bch2_trans_do(c, journal_seq,
292 bch2_hash_delete(&trans, bch2_dirent_hash_desc, hash_info,
297 __bch2_dirent_lookup_trans(struct btree_trans *trans, u64 dir_inum,
298 const struct bch_hash_info *hash_info,
299 const struct qstr *name, unsigned flags)
301 return bch2_hash_lookup(trans, bch2_dirent_hash_desc,
302 hash_info, dir_inum, name, flags);
305 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
306 const struct bch_hash_info *hash_info,
307 const struct qstr *name)
309 struct btree_trans trans;
310 struct btree_iter *iter;
314 bch2_trans_init(&trans, c, 0, 0);
316 iter = __bch2_dirent_lookup_trans(&trans, dir_inum,
319 BUG_ON(PTR_ERR(iter) == -EINTR);
323 k = bch2_btree_iter_peek_slot(iter);
324 inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
326 bch2_trans_exit(&trans);
330 int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
332 struct btree_iter *iter;
336 for_each_btree_key(trans, iter, BTREE_ID_DIRENTS,
337 POS(dir_inum, 0), 0, k, ret) {
338 if (k.k->p.inode > dir_inum)
341 if (k.k->type == KEY_TYPE_dirent) {
346 bch2_trans_iter_put(trans, iter);
351 int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
353 struct btree_trans trans;
354 struct btree_iter *iter;
356 struct bkey_s_c_dirent dirent;
359 bch2_trans_init(&trans, c, 0, 0);
361 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
362 POS(inum, ctx->pos), 0, k, ret) {
363 if (k.k->p.inode > inum)
366 if (k.k->type != KEY_TYPE_dirent)
369 dirent = bkey_s_c_to_dirent(k);
372 * XXX: dir_emit() can fault and block, while we're holding
375 ctx->pos = dirent.k->p.offset;
376 if (!dir_emit(ctx, dirent.v->d_name,
377 bch2_dirent_name_bytes(dirent),
378 le64_to_cpu(dirent.v->d_inum),
381 ctx->pos = dirent.k->p.offset + 1;
383 ret = bch2_trans_exit(&trans) ?: ret;