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) - sizeof(struct bch_dirent);
17 while (len && !d.v->d_name[len - 1])
23 static unsigned dirent_val_u64s(unsigned len)
25 return DIV_ROUND_UP(sizeof(struct bch_dirent) + len, sizeof(u64));
28 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
29 const struct qstr *name)
31 struct bch_str_hash_ctx ctx;
33 bch2_str_hash_init(&ctx, info);
34 bch2_str_hash_update(&ctx, info, name->name, name->len);
36 /* [0,2) reserved for dots */
37 return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
40 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
42 return bch2_dirent_hash(info, key);
45 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
47 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
48 struct qstr name = QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
50 return bch2_dirent_hash(info, &name);
53 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
55 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
56 int len = bch2_dirent_name_bytes(l);
57 const struct qstr *r = _r;
59 return len - r->len ?: memcmp(l.v->d_name, r->name, len);
62 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
64 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
65 struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
66 int l_len = bch2_dirent_name_bytes(l);
67 int r_len = bch2_dirent_name_bytes(r);
69 return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
72 const struct bch_hash_desc bch2_dirent_hash_desc = {
73 .btree_id = BTREE_ID_DIRENTS,
74 .key_type = BCH_DIRENT,
75 .whiteout_type = BCH_DIRENT_WHITEOUT,
76 .hash_key = dirent_hash_key,
77 .hash_bkey = dirent_hash_bkey,
78 .cmp_key = dirent_cmp_key,
79 .cmp_bkey = dirent_cmp_bkey,
82 const char *bch2_dirent_invalid(const struct bch_fs *c, struct bkey_s_c k)
84 struct bkey_s_c_dirent d;
89 if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent))
90 return "value too small";
92 d = bkey_s_c_to_dirent(k);
93 len = bch2_dirent_name_bytes(d);
98 if (bkey_val_u64s(k.k) > dirent_val_u64s(len))
99 return "value too big";
102 return "dirent name too big";
104 if (memchr(d.v->d_name, '/', len))
105 return "dirent name has invalid characters";
108 case BCH_DIRENT_WHITEOUT:
109 return bkey_val_bytes(k.k) != 0
110 ? "value size should be zero"
114 return "invalid type";
118 void bch2_dirent_to_text(struct bch_fs *c, char *buf,
119 size_t size, struct bkey_s_c k)
121 struct bkey_s_c_dirent d;
126 d = bkey_s_c_to_dirent(k);
128 n += bch_scnmemcpy(buf + n, size - n, d.v->d_name,
129 bch2_dirent_name_bytes(d));
130 n += scnprintf(buf + n, size - n, " -> %llu", d.v->d_inum);
132 case BCH_DIRENT_WHITEOUT:
133 scnprintf(buf, size, "whiteout");
138 static struct bkey_i_dirent *dirent_create_key(u8 type,
139 const struct qstr *name, u64 dst)
141 struct bkey_i_dirent *dirent;
142 unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
144 dirent = kmalloc(u64s * sizeof(u64), GFP_NOFS);
148 bkey_dirent_init(&dirent->k_i);
149 dirent->k.u64s = u64s;
150 dirent->v.d_inum = cpu_to_le64(dst);
151 dirent->v.d_type = type;
153 memcpy(dirent->v.d_name, name->name, name->len);
154 memset(dirent->v.d_name + name->len, 0,
155 bkey_val_bytes(&dirent->k) -
156 (sizeof(struct bch_dirent) + name->len));
158 EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
163 int bch2_dirent_create(struct bch_fs *c, u64 dir_inum,
164 const struct bch_hash_info *hash_info,
165 u8 type, const struct qstr *name, u64 dst_inum,
166 u64 *journal_seq, int flags)
168 struct bkey_i_dirent *dirent;
171 dirent = dirent_create_key(type, name, dst_inum);
175 ret = bch2_hash_set(bch2_dirent_hash_desc, hash_info, c, dir_inum,
176 journal_seq, &dirent->k_i, flags);
182 static void dirent_copy_target(struct bkey_i_dirent *dst,
183 struct bkey_s_c_dirent src)
185 dst->v.d_inum = src.v->d_inum;
186 dst->v.d_type = src.v->d_type;
189 static struct bpos bch2_dirent_pos(struct bch_inode_info *inode,
190 const struct qstr *name)
192 return POS(inode->v.i_ino, bch2_dirent_hash(&inode->ei_str_hash, name));
195 int bch2_dirent_rename(struct bch_fs *c,
196 struct bch_inode_info *src_dir, const struct qstr *src_name,
197 struct bch_inode_info *dst_dir, const struct qstr *dst_name,
198 u64 *journal_seq, enum bch_rename_mode mode)
200 struct btree_iter src_iter, dst_iter, whiteout_iter;
201 struct bkey_s_c old_src, old_dst;
203 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
204 struct bpos src_pos = bch2_dirent_pos(src_dir, src_name);
205 struct bpos dst_pos = bch2_dirent_pos(dst_dir, dst_name);
209 bch2_btree_iter_init(&src_iter, c, BTREE_ID_DIRENTS, src_pos,
210 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
211 bch2_btree_iter_init(&dst_iter, c, BTREE_ID_DIRENTS, dst_pos,
212 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
213 bch2_btree_iter_link(&src_iter, &dst_iter);
215 bch2_btree_iter_init(&whiteout_iter, c, BTREE_ID_DIRENTS, src_pos,
217 bch2_btree_iter_link(&src_iter, &whiteout_iter);
219 if (mode == BCH_RENAME_EXCHANGE) {
220 new_src = dirent_create_key(0, src_name, 0);
224 new_src = (void *) &delete;
227 new_dst = dirent_create_key(0, dst_name, 0);
232 * Note that on -EINTR/dropped locks we're not restarting the lookup
233 * from the original hashed position (like we do when creating dirents,
234 * in bch_hash_set) - we never move existing dirents to different slot:
236 old_src = bch2_hash_lookup_at(bch2_dirent_hash_desc,
237 &src_dir->ei_str_hash,
238 &src_iter, src_name);
239 if ((ret = btree_iter_err(old_src)))
242 ret = bch2_hash_needs_whiteout(bch2_dirent_hash_desc,
243 &src_dir->ei_str_hash,
244 &whiteout_iter, &src_iter);
250 * Note that in BCH_RENAME mode, we're _not_ checking if
251 * the target already exists - we're relying on the VFS
252 * to do that check for us for correctness:
254 old_dst = mode == BCH_RENAME
255 ? bch2_hash_hole_at(bch2_dirent_hash_desc, &dst_iter)
256 : bch2_hash_lookup_at(bch2_dirent_hash_desc,
257 &dst_dir->ei_str_hash,
258 &dst_iter, dst_name);
259 if ((ret = btree_iter_err(old_dst)))
264 bkey_init(&new_src->k);
265 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
267 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
268 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
270 * If we couldn't insert new_dst at its hashed
271 * position (dst_pos) due to a hash collision,
272 * and we're going to be deleting in
273 * between the hashed position and first empty
274 * slot we found - just overwrite the pos we
275 * were going to delete:
277 * Note: this is a correctness issue, in this
278 * situation bch2_hash_needs_whiteout() could
279 * return false when the whiteout would have
280 * been needed if we inserted at the pos
281 * __dirent_find_hole() found
283 new_dst->k.p = src_iter.pos;
284 ret = bch2_btree_insert_at(c, NULL, NULL,
287 BTREE_INSERT_ENTRY(&src_iter,
293 new_src->k.type = BCH_DIRENT_WHITEOUT;
295 case BCH_RENAME_OVERWRITE:
296 bkey_init(&new_src->k);
297 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
299 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
300 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
302 * Same case described above -
303 * bch_hash_needs_whiteout could spuriously
304 * return false, but we have to insert at
305 * dst_iter.pos because we're overwriting
308 new_src->k.type = BCH_DIRENT_WHITEOUT;
309 } else if (need_whiteout)
310 new_src->k.type = BCH_DIRENT_WHITEOUT;
312 case BCH_RENAME_EXCHANGE:
313 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
314 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
318 new_src->k.p = src_iter.pos;
319 new_dst->k.p = dst_iter.pos;
320 ret = bch2_btree_insert_at(c, NULL, NULL, journal_seq,
322 BTREE_INSERT_ENTRY(&src_iter, &new_src->k_i),
323 BTREE_INSERT_ENTRY(&dst_iter, &new_dst->k_i));
328 bch2_btree_iter_unlock(&whiteout_iter);
329 bch2_btree_iter_unlock(&dst_iter);
330 bch2_btree_iter_unlock(&src_iter);
332 if (new_src != (void *) &delete)
338 int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
339 const struct bch_hash_info *hash_info,
340 const struct qstr *name,
343 return bch2_hash_delete(bch2_dirent_hash_desc, hash_info,
344 c, dir_inum, journal_seq, name);
347 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
348 const struct bch_hash_info *hash_info,
349 const struct qstr *name)
351 struct btree_iter iter;
355 k = bch2_hash_lookup(bch2_dirent_hash_desc, hash_info, c,
356 dir_inum, &iter, name);
358 bch2_btree_iter_unlock(&iter);
362 inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
363 bch2_btree_iter_unlock(&iter);
368 int bch2_empty_dir(struct bch_fs *c, u64 dir_inum)
370 struct btree_iter iter;
374 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS(dir_inum, 0), 0, k) {
375 if (k.k->p.inode > dir_inum)
378 if (k.k->type == BCH_DIRENT) {
383 bch2_btree_iter_unlock(&iter);
388 int bch2_readdir(struct bch_fs *c, struct file *file,
389 struct dir_context *ctx)
391 struct bch_inode_info *inode = file_bch_inode(file);
392 struct btree_iter iter;
394 struct bkey_s_c_dirent dirent;
397 if (!dir_emit_dots(file, ctx))
400 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
401 POS(inode->v.i_ino, ctx->pos), 0, k) {
402 if (k.k->type != BCH_DIRENT)
405 dirent = bkey_s_c_to_dirent(k);
407 if (bkey_cmp(k.k->p, POS(inode->v.i_ino, ctx->pos)) < 0)
410 if (k.k->p.inode > inode->v.i_ino)
413 len = bch2_dirent_name_bytes(dirent);
416 * XXX: dir_emit() can fault and block, while we're holding
419 if (!dir_emit(ctx, dirent.v->d_name, len,
420 le64_to_cpu(dirent.v->d_inum),
424 ctx->pos = k.k->p.offset + 1;
426 bch2_btree_iter_unlock(&iter);