3 #include "bkey_methods.h"
4 #include "btree_update.h"
11 #include <linux/dcache.h>
13 unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
15 unsigned len = bkey_val_bytes(d.k) -
16 offsetof(struct bch_dirent, d_name);
18 return strnlen(d.v->d_name, len);
21 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
22 const struct qstr *name)
24 struct bch_str_hash_ctx ctx;
26 bch2_str_hash_init(&ctx, info);
27 bch2_str_hash_update(&ctx, info, name->name, name->len);
29 /* [0,2) reserved for dots */
30 return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
33 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
35 return bch2_dirent_hash(info, key);
38 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
40 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
41 struct qstr name = QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
43 return bch2_dirent_hash(info, &name);
46 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
48 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
49 int len = bch2_dirent_name_bytes(l);
50 const struct qstr *r = _r;
52 return len - r->len ?: memcmp(l.v->d_name, r->name, len);
55 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
57 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
58 struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
59 int l_len = bch2_dirent_name_bytes(l);
60 int r_len = bch2_dirent_name_bytes(r);
62 return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
65 const struct bch_hash_desc bch2_dirent_hash_desc = {
66 .btree_id = BTREE_ID_DIRENTS,
67 .key_type = BCH_DIRENT,
68 .whiteout_type = BCH_DIRENT_WHITEOUT,
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;
82 if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent))
83 return "value too small";
85 d = bkey_s_c_to_dirent(k);
86 len = bch2_dirent_name_bytes(d);
92 * older versions of bcachefs were buggy and creating dirent
93 * keys that were bigger than necessary:
95 if (bkey_val_u64s(k.k) > dirent_val_u64s(len + 7))
96 return "value too big";
98 if (len > BCH_NAME_MAX)
99 return "dirent name too big";
102 case BCH_DIRENT_WHITEOUT:
103 return bkey_val_bytes(k.k) != 0
104 ? "value size should be zero"
108 return "invalid type";
112 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
115 struct bkey_s_c_dirent d;
119 d = bkey_s_c_to_dirent(k);
121 bch_scnmemcpy(out, d.v->d_name,
122 bch2_dirent_name_bytes(d));
123 pr_buf(out, " -> %llu", d.v->d_inum);
125 case BCH_DIRENT_WHITEOUT:
126 pr_buf(out, "whiteout");
131 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
132 u8 type, const struct qstr *name, u64 dst)
134 struct bkey_i_dirent *dirent;
135 unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
137 if (name->len > BCH_NAME_MAX)
138 return ERR_PTR(-ENAMETOOLONG);
140 BUG_ON(u64s > U8_MAX);
142 dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
146 bkey_dirent_init(&dirent->k_i);
147 dirent->k.u64s = u64s;
148 dirent->v.d_inum = cpu_to_le64(dst);
149 dirent->v.d_type = type;
151 memcpy(dirent->v.d_name, name->name, name->len);
152 memset(dirent->v.d_name + name->len, 0,
153 bkey_val_bytes(&dirent->k) -
154 offsetof(struct bch_dirent, d_name) -
157 EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
162 int __bch2_dirent_create(struct btree_trans *trans,
163 u64 dir_inum, const struct bch_hash_info *hash_info,
164 u8 type, const struct qstr *name, u64 dst_inum,
167 struct bkey_i_dirent *dirent;
170 dirent = dirent_create_key(trans, type, name, dst_inum);
171 ret = PTR_ERR_OR_ZERO(dirent);
175 return __bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
176 dir_inum, &dirent->k_i, flags);
179 int bch2_dirent_create(struct bch_fs *c, u64 dir_inum,
180 const struct bch_hash_info *hash_info,
181 u8 type, const struct qstr *name, u64 dst_inum,
182 u64 *journal_seq, int flags)
184 return bch2_trans_do(c, journal_seq, flags,
185 __bch2_dirent_create(&trans, dir_inum, hash_info,
186 type, name, dst_inum, flags));
189 static void dirent_copy_target(struct bkey_i_dirent *dst,
190 struct bkey_s_c_dirent src)
192 dst->v.d_inum = src.v->d_inum;
193 dst->v.d_type = src.v->d_type;
196 static struct bpos bch2_dirent_pos(struct bch_inode_info *inode,
197 const struct qstr *name)
199 return POS(inode->v.i_ino, bch2_dirent_hash(&inode->ei_str_hash, name));
202 int bch2_dirent_rename(struct btree_trans *trans,
203 struct bch_inode_info *src_dir, const struct qstr *src_name,
204 struct bch_inode_info *dst_dir, const struct qstr *dst_name,
205 enum bch_rename_mode mode)
207 struct btree_iter *src_iter, *dst_iter;
208 struct bkey_s_c old_src, old_dst;
209 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
210 struct bpos dst_pos = bch2_dirent_pos(dst_dir, dst_name);
216 * Note that in BCH_RENAME mode, we're _not_ checking if
217 * the target already exists - we're relying on the VFS
218 * to do that check for us for correctness:
220 dst_iter = mode == BCH_RENAME
221 ? bch2_hash_hole(trans, bch2_dirent_hash_desc,
222 &dst_dir->ei_str_hash,
223 dst_dir->v.i_ino, dst_name)
224 : bch2_hash_lookup(trans, bch2_dirent_hash_desc,
225 &dst_dir->ei_str_hash,
226 dst_dir->v.i_ino, dst_name,
228 if (IS_ERR(dst_iter))
229 return PTR_ERR(dst_iter);
230 old_dst = bch2_btree_iter_peek_slot(dst_iter);
233 src_iter = bch2_hash_lookup(trans, bch2_dirent_hash_desc,
234 &src_dir->ei_str_hash,
235 src_dir->v.i_ino, src_name,
237 if (IS_ERR(src_iter))
238 return PTR_ERR(src_iter);
239 old_src = bch2_btree_iter_peek_slot(src_iter);
241 /* Create new dst key: */
242 new_dst = dirent_create_key(trans, 0, dst_name, 0);
244 return PTR_ERR(new_dst);
246 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
247 new_dst->k.p = dst_iter->pos;
249 /* Create new src key: */
250 if (mode == BCH_RENAME_EXCHANGE) {
251 new_src = dirent_create_key(trans, 0, src_name, 0);
253 return PTR_ERR(new_src);
255 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
256 new_src->k.p = src_iter->pos;
258 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
260 return PTR_ERR(new_src);
261 bkey_init(&new_src->k);
262 new_src->k.p = src_iter->pos;
264 if (bkey_cmp(dst_pos, src_iter->pos) <= 0 &&
265 bkey_cmp(src_iter->pos, dst_iter->pos) < 0) {
267 * We have a hash collision for the new dst key,
268 * and new_src - the key we're deleting - is between
269 * new_dst's hashed slot and the slot we're going to be
270 * inserting it into - oops. This will break the hash
271 * table if we don't deal with it:
273 if (mode == BCH_RENAME) {
275 * If we're not overwriting, we can just insert
276 * new_dst at the src position:
278 new_dst->k.p = src_iter->pos;
279 bch2_trans_update(trans,
280 BTREE_INSERT_ENTRY(src_iter,
284 /* If we're overwriting, we can't insert new_dst
285 * at a different slot because it has to
286 * overwrite old_dst - just make sure to use a
287 * whiteout when deleting src:
289 new_src->k.type = BCH_DIRENT_WHITEOUT;
292 /* Check if we need a whiteout to delete src: */
293 ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
294 &src_dir->ei_str_hash,
300 new_src->k.type = BCH_DIRENT_WHITEOUT;
304 bch2_trans_update(trans, BTREE_INSERT_ENTRY(src_iter, &new_src->k_i));
305 bch2_trans_update(trans, BTREE_INSERT_ENTRY(dst_iter, &new_dst->k_i));
309 int __bch2_dirent_delete(struct btree_trans *trans, u64 dir_inum,
310 const struct bch_hash_info *hash_info,
311 const struct qstr *name)
313 return bch2_hash_delete(trans, bch2_dirent_hash_desc, hash_info,
317 int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
318 const struct bch_hash_info *hash_info,
319 const struct qstr *name,
322 return bch2_trans_do(c, journal_seq,
325 __bch2_dirent_delete(&trans, dir_inum, hash_info, name));
328 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
329 const struct bch_hash_info *hash_info,
330 const struct qstr *name)
332 struct btree_trans trans;
333 struct btree_iter *iter;
337 bch2_trans_init(&trans, c);
339 iter = bch2_hash_lookup(&trans, bch2_dirent_hash_desc,
340 hash_info, dir_inum, name, 0);
342 BUG_ON(PTR_ERR(iter) == -EINTR);
346 k = bch2_btree_iter_peek_slot(iter);
347 inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
349 bch2_trans_exit(&trans);
353 int bch2_empty_dir(struct bch_fs *c, u64 dir_inum)
355 struct btree_iter iter;
359 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS(dir_inum, 0), 0, k) {
360 if (k.k->p.inode > dir_inum)
363 if (k.k->type == BCH_DIRENT) {
368 bch2_btree_iter_unlock(&iter);
373 int bch2_readdir(struct bch_fs *c, struct file *file,
374 struct dir_context *ctx)
376 struct bch_inode_info *inode = file_bch_inode(file);
377 struct btree_iter iter;
379 struct bkey_s_c_dirent dirent;
382 if (!dir_emit_dots(file, ctx))
385 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
386 POS(inode->v.i_ino, ctx->pos), 0, k) {
387 if (k.k->type != BCH_DIRENT)
390 dirent = bkey_s_c_to_dirent(k);
392 if (bkey_cmp(k.k->p, POS(inode->v.i_ino, ctx->pos)) < 0)
395 if (k.k->p.inode > inode->v.i_ino)
398 len = bch2_dirent_name_bytes(dirent);
401 * XXX: dir_emit() can fault and block, while we're holding
404 if (!dir_emit(ctx, dirent.v->d_name, len,
405 le64_to_cpu(dirent.v->d_inum),
409 ctx->pos = k.k->p.offset + 1;
411 bch2_btree_iter_unlock(&iter);