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 static const char *bch2_dirent_invalid(const struct bch_fs *c,
85 struct bkey_s_c_dirent d;
90 if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent))
91 return "value too small";
93 d = bkey_s_c_to_dirent(k);
94 len = bch2_dirent_name_bytes(d);
99 if (bkey_val_u64s(k.k) > dirent_val_u64s(len))
100 return "value too big";
103 return "dirent name too big";
105 if (memchr(d.v->d_name, '/', len))
106 return "dirent name has invalid characters";
109 case BCH_DIRENT_WHITEOUT:
110 return bkey_val_bytes(k.k) != 0
111 ? "value size should be zero"
115 return "invalid type";
119 static void bch2_dirent_to_text(struct bch_fs *c, char *buf,
120 size_t size, struct bkey_s_c k)
122 struct bkey_s_c_dirent d;
127 d = bkey_s_c_to_dirent(k);
129 n += bch_scnmemcpy(buf + n, size - n, d.v->d_name,
130 bch2_dirent_name_bytes(d));
131 n += scnprintf(buf + n, size - n, " -> %llu", d.v->d_inum);
133 case BCH_DIRENT_WHITEOUT:
134 scnprintf(buf, size, "whiteout");
139 const struct bkey_ops bch2_bkey_dirent_ops = {
140 .key_invalid = bch2_dirent_invalid,
141 .val_to_text = bch2_dirent_to_text,
144 static struct bkey_i_dirent *dirent_create_key(u8 type,
145 const struct qstr *name, u64 dst)
147 struct bkey_i_dirent *dirent;
148 unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
150 dirent = kmalloc(u64s * sizeof(u64), GFP_NOFS);
154 bkey_dirent_init(&dirent->k_i);
155 dirent->k.u64s = u64s;
156 dirent->v.d_inum = cpu_to_le64(dst);
157 dirent->v.d_type = type;
159 memcpy(dirent->v.d_name, name->name, name->len);
160 memset(dirent->v.d_name + name->len, 0,
161 bkey_val_bytes(&dirent->k) -
162 (sizeof(struct bch_dirent) + name->len));
164 EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
169 int bch2_dirent_create(struct bch_fs *c, u64 dir_inum,
170 const struct bch_hash_info *hash_info,
171 u8 type, const struct qstr *name, u64 dst_inum,
172 u64 *journal_seq, int flags)
174 struct bkey_i_dirent *dirent;
177 dirent = dirent_create_key(type, name, dst_inum);
181 ret = bch2_hash_set(bch2_dirent_hash_desc, hash_info, c, dir_inum,
182 journal_seq, &dirent->k_i, flags);
188 static void dirent_copy_target(struct bkey_i_dirent *dst,
189 struct bkey_s_c_dirent src)
191 dst->v.d_inum = src.v->d_inum;
192 dst->v.d_type = src.v->d_type;
195 static struct bpos bch2_dirent_pos(struct bch_inode_info *inode,
196 const struct qstr *name)
198 return POS(inode->v.i_ino, bch2_dirent_hash(&inode->ei_str_hash, name));
201 int bch2_dirent_rename(struct bch_fs *c,
202 struct bch_inode_info *src_dir, const struct qstr *src_name,
203 struct bch_inode_info *dst_dir, const struct qstr *dst_name,
204 u64 *journal_seq, enum bch_rename_mode mode)
206 struct btree_iter src_iter, dst_iter, whiteout_iter;
207 struct bkey_s_c old_src, old_dst;
209 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
210 struct bpos src_pos = bch2_dirent_pos(src_dir, src_name);
211 struct bpos dst_pos = bch2_dirent_pos(dst_dir, dst_name);
215 bch2_btree_iter_init(&src_iter, c, BTREE_ID_DIRENTS, src_pos,
216 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
217 bch2_btree_iter_init(&dst_iter, c, BTREE_ID_DIRENTS, dst_pos,
218 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
219 bch2_btree_iter_link(&src_iter, &dst_iter);
221 bch2_btree_iter_init(&whiteout_iter, c, BTREE_ID_DIRENTS, src_pos,
223 bch2_btree_iter_link(&src_iter, &whiteout_iter);
225 if (mode == BCH_RENAME_EXCHANGE) {
226 new_src = dirent_create_key(0, src_name, 0);
230 new_src = (void *) &delete;
233 new_dst = dirent_create_key(0, dst_name, 0);
238 * Note that on -EINTR/dropped locks we're not restarting the lookup
239 * from the original hashed position (like we do when creating dirents,
240 * in bch_hash_set) - we never move existing dirents to different slot:
242 old_src = bch2_hash_lookup_at(bch2_dirent_hash_desc,
243 &src_dir->ei_str_hash,
244 &src_iter, src_name);
245 if ((ret = btree_iter_err(old_src)))
248 ret = bch2_hash_needs_whiteout(bch2_dirent_hash_desc,
249 &src_dir->ei_str_hash,
250 &whiteout_iter, &src_iter);
256 * Note that in BCH_RENAME mode, we're _not_ checking if
257 * the target already exists - we're relying on the VFS
258 * to do that check for us for correctness:
260 old_dst = mode == BCH_RENAME
261 ? bch2_hash_hole_at(bch2_dirent_hash_desc, &dst_iter)
262 : bch2_hash_lookup_at(bch2_dirent_hash_desc,
263 &dst_dir->ei_str_hash,
264 &dst_iter, dst_name);
265 if ((ret = btree_iter_err(old_dst)))
270 bkey_init(&new_src->k);
271 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
273 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
274 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
276 * If we couldn't insert new_dst at its hashed
277 * position (dst_pos) due to a hash collision,
278 * and we're going to be deleting in
279 * between the hashed position and first empty
280 * slot we found - just overwrite the pos we
281 * were going to delete:
283 * Note: this is a correctness issue, in this
284 * situation bch2_hash_needs_whiteout() could
285 * return false when the whiteout would have
286 * been needed if we inserted at the pos
287 * __dirent_find_hole() found
289 new_dst->k.p = src_iter.pos;
290 ret = bch2_btree_insert_at(c, NULL, NULL,
293 BTREE_INSERT_ENTRY(&src_iter,
299 new_src->k.type = BCH_DIRENT_WHITEOUT;
301 case BCH_RENAME_OVERWRITE:
302 bkey_init(&new_src->k);
303 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
305 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
306 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
308 * Same case described above -
309 * bch_hash_needs_whiteout could spuriously
310 * return false, but we have to insert at
311 * dst_iter.pos because we're overwriting
314 new_src->k.type = BCH_DIRENT_WHITEOUT;
315 } else if (need_whiteout)
316 new_src->k.type = BCH_DIRENT_WHITEOUT;
318 case BCH_RENAME_EXCHANGE:
319 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
320 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
324 new_src->k.p = src_iter.pos;
325 new_dst->k.p = dst_iter.pos;
326 ret = bch2_btree_insert_at(c, NULL, NULL, journal_seq,
328 BTREE_INSERT_ENTRY(&src_iter, &new_src->k_i),
329 BTREE_INSERT_ENTRY(&dst_iter, &new_dst->k_i));
334 bch2_btree_iter_unlock(&whiteout_iter);
335 bch2_btree_iter_unlock(&dst_iter);
336 bch2_btree_iter_unlock(&src_iter);
338 if (new_src != (void *) &delete)
344 int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
345 const struct bch_hash_info *hash_info,
346 const struct qstr *name,
349 return bch2_hash_delete(bch2_dirent_hash_desc, hash_info,
350 c, dir_inum, journal_seq, name);
353 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
354 const struct bch_hash_info *hash_info,
355 const struct qstr *name)
357 struct btree_iter iter;
361 k = bch2_hash_lookup(bch2_dirent_hash_desc, hash_info, c,
362 dir_inum, &iter, name);
364 bch2_btree_iter_unlock(&iter);
368 inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
369 bch2_btree_iter_unlock(&iter);
374 int bch2_empty_dir(struct bch_fs *c, u64 dir_inum)
376 struct btree_iter iter;
380 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS(dir_inum, 0), 0, k) {
381 if (k.k->p.inode > dir_inum)
384 if (k.k->type == BCH_DIRENT) {
389 bch2_btree_iter_unlock(&iter);
394 int bch2_readdir(struct bch_fs *c, struct file *file,
395 struct dir_context *ctx)
397 struct bch_inode_info *inode = file_bch_inode(file);
398 struct btree_iter iter;
400 struct bkey_s_c_dirent dirent;
403 if (!dir_emit_dots(file, ctx))
406 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
407 POS(inode->v.i_ino, ctx->pos), 0, k) {
408 if (k.k->type != BCH_DIRENT)
411 dirent = bkey_s_c_to_dirent(k);
413 if (bkey_cmp(k.k->p, POS(inode->v.i_ino, ctx->pos)) < 0)
416 if (k.k->p.inode > inode->v.i_ino)
419 len = bch2_dirent_name_bytes(dirent);
422 * XXX: dir_emit() can fault and block, while we're holding
425 if (!dir_emit(ctx, dirent.v->d_name, len,
426 le64_to_cpu(dirent.v->d_inum),
430 ctx->pos = k.k->p.offset + 1;
432 bch2_btree_iter_unlock(&iter);