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);
87 if (bkey_val_u64s(k.k) > dirent_val_u64s(len))
88 return "value too big";
90 if (len > BCH_NAME_MAX)
91 return "dirent name too big";
93 if (len == 1 && !memcmp(d.v->d_name, ".", 1))
94 return "invalid name";
96 if (len == 2 && !memcmp(d.v->d_name, "..", 2))
97 return "invalid name";
99 if (memchr(d.v->d_name, '/', len))
100 return "invalid name";
102 if (le64_to_cpu(d.v->d_inum) == d.k->p.inode)
103 return "dirent points to own directory";
108 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
111 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
113 bch_scnmemcpy(out, d.v->d_name,
114 bch2_dirent_name_bytes(d));
115 pr_buf(out, " -> %llu type %s", d.v->d_inum,
117 ? bch2_d_types[d.v->d_type]
121 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
122 u8 type, const struct qstr *name, u64 dst)
124 struct bkey_i_dirent *dirent;
125 unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
127 if (name->len > BCH_NAME_MAX)
128 return ERR_PTR(-ENAMETOOLONG);
130 BUG_ON(u64s > U8_MAX);
132 dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
136 bkey_dirent_init(&dirent->k_i);
137 dirent->k.u64s = u64s;
138 dirent->v.d_inum = cpu_to_le64(dst);
139 dirent->v.d_type = type;
141 memcpy(dirent->v.d_name, name->name, name->len);
142 memset(dirent->v.d_name + name->len, 0,
143 bkey_val_bytes(&dirent->k) -
144 offsetof(struct bch_dirent, d_name) -
147 EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
152 int bch2_dirent_create(struct btree_trans *trans,
153 u64 dir_inum, const struct bch_hash_info *hash_info,
154 u8 type, const struct qstr *name, u64 dst_inum,
155 u64 *dir_offset, int flags)
157 struct bkey_i_dirent *dirent;
160 dirent = dirent_create_key(trans, type, name, dst_inum);
161 ret = PTR_ERR_OR_ZERO(dirent);
165 ret = bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
166 dir_inum, &dirent->k_i, flags);
167 *dir_offset = dirent->k.p.offset;
172 static void dirent_copy_target(struct bkey_i_dirent *dst,
173 struct bkey_s_c_dirent src)
175 dst->v.d_inum = src.v->d_inum;
176 dst->v.d_type = src.v->d_type;
179 int bch2_dirent_rename(struct btree_trans *trans,
180 u64 src_dir, struct bch_hash_info *src_hash,
181 u64 dst_dir, struct bch_hash_info *dst_hash,
182 const struct qstr *src_name, u64 *src_inum, u64 *src_offset,
183 const struct qstr *dst_name, u64 *dst_inum, u64 *dst_offset,
184 enum bch_rename_mode mode)
186 struct btree_iter src_iter = { NULL };
187 struct btree_iter dst_iter = { NULL };
188 struct bkey_s_c old_src, old_dst;
189 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
190 struct bpos dst_pos =
191 POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
194 *src_inum = *dst_inum = 0;
199 * Note that in BCH_RENAME mode, we're _not_ checking if
200 * the target already exists - we're relying on the VFS
201 * to do that check for us for correctness:
203 ret = mode == BCH_RENAME
204 ? bch2_hash_hole(trans, &dst_iter, bch2_dirent_hash_desc,
205 dst_hash, dst_dir, dst_name)
206 : bch2_hash_lookup(trans, &dst_iter, bch2_dirent_hash_desc,
207 dst_hash, dst_dir, dst_name,
212 old_dst = bch2_btree_iter_peek_slot(&dst_iter);
213 ret = bkey_err(old_dst);
217 if (mode != BCH_RENAME)
218 *dst_inum = le64_to_cpu(bkey_s_c_to_dirent(old_dst).v->d_inum);
219 if (mode != BCH_RENAME_EXCHANGE)
220 *src_offset = dst_iter.pos.offset;
223 ret = bch2_hash_lookup(trans, &src_iter, bch2_dirent_hash_desc,
224 src_hash, src_dir, src_name,
229 old_src = bch2_btree_iter_peek_slot(&src_iter);
230 ret = bkey_err(old_src);
234 *src_inum = le64_to_cpu(bkey_s_c_to_dirent(old_src).v->d_inum);
236 /* Create new dst key: */
237 new_dst = dirent_create_key(trans, 0, dst_name, 0);
238 ret = PTR_ERR_OR_ZERO(new_dst);
242 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
243 new_dst->k.p = dst_iter.pos;
245 /* Create new src key: */
246 if (mode == BCH_RENAME_EXCHANGE) {
247 new_src = dirent_create_key(trans, 0, src_name, 0);
248 ret = PTR_ERR_OR_ZERO(new_src);
252 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
253 new_src->k.p = src_iter.pos;
255 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
256 ret = PTR_ERR_OR_ZERO(new_src);
260 bkey_init(&new_src->k);
261 new_src->k.p = src_iter.pos;
263 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
264 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
266 * We have a hash collision for the new dst key,
267 * and new_src - the key we're deleting - is between
268 * new_dst's hashed slot and the slot we're going to be
269 * inserting it into - oops. This will break the hash
270 * table if we don't deal with it:
272 if (mode == BCH_RENAME) {
274 * If we're not overwriting, we can just insert
275 * new_dst at the src position:
277 new_dst->k.p = src_iter.pos;
278 bch2_trans_update(trans, &src_iter,
282 /* If we're overwriting, we can't insert new_dst
283 * at a different slot because it has to
284 * overwrite old_dst - just make sure to use a
285 * whiteout when deleting src:
287 new_src->k.type = KEY_TYPE_hash_whiteout;
290 /* Check if we need a whiteout to delete src: */
291 ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
292 src_hash, &src_iter);
297 new_src->k.type = KEY_TYPE_hash_whiteout;
301 bch2_trans_update(trans, &src_iter, &new_src->k_i, 0);
302 bch2_trans_update(trans, &dst_iter, &new_dst->k_i, 0);
304 if (mode == BCH_RENAME_EXCHANGE)
305 *src_offset = new_src->k.p.offset;
306 *dst_offset = new_dst->k.p.offset;
308 bch2_trans_iter_exit(trans, &src_iter);
309 bch2_trans_iter_exit(trans, &dst_iter);
313 int bch2_dirent_delete_at(struct btree_trans *trans,
314 const struct bch_hash_info *hash_info,
315 struct btree_iter *iter)
317 return bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
321 int __bch2_dirent_lookup_trans(struct btree_trans *trans,
322 struct btree_iter *iter,
324 const struct bch_hash_info *hash_info,
325 const struct qstr *name, unsigned flags)
327 return bch2_hash_lookup(trans, iter, bch2_dirent_hash_desc,
328 hash_info, dir_inum, name, flags);
331 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
332 const struct bch_hash_info *hash_info,
333 const struct qstr *name)
335 struct btree_trans trans;
336 struct btree_iter iter;
341 bch2_trans_init(&trans, c, 0, 0);
343 ret = __bch2_dirent_lookup_trans(&trans, &iter, dir_inum,
348 k = bch2_btree_iter_peek_slot(&iter);
353 inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
354 bch2_trans_iter_exit(&trans, &iter);
356 BUG_ON(ret == -EINTR);
357 bch2_trans_exit(&trans);
361 int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
363 struct btree_iter iter;
367 for_each_btree_key(trans, iter, BTREE_ID_dirents,
368 POS(dir_inum, 0), 0, k, ret) {
369 if (k.k->p.inode > dir_inum)
372 if (k.k->type == KEY_TYPE_dirent) {
377 bch2_trans_iter_exit(trans, &iter);
382 int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
384 struct btree_trans trans;
385 struct btree_iter iter;
387 struct bkey_s_c_dirent dirent;
390 bch2_trans_init(&trans, c, 0, 0);
392 for_each_btree_key(&trans, iter, BTREE_ID_dirents,
393 POS(inum, ctx->pos), 0, k, ret) {
394 if (k.k->p.inode > inum)
397 if (k.k->type != KEY_TYPE_dirent)
400 dirent = bkey_s_c_to_dirent(k);
403 * XXX: dir_emit() can fault and block, while we're holding
406 ctx->pos = dirent.k->p.offset;
407 if (!dir_emit(ctx, dirent.v->d_name,
408 bch2_dirent_name_bytes(dirent),
409 le64_to_cpu(dirent.v->d_inum),
412 ctx->pos = dirent.k->p.offset + 1;
414 bch2_trans_iter_exit(&trans, &iter);
416 ret = bch2_trans_exit(&trans) ?: ret;