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 while (len && !d.v->d_name[len - 1])
24 static unsigned dirent_val_u64s(unsigned len)
26 return DIV_ROUND_UP(offsetof(struct bch_dirent, d_name) + len,
30 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
31 const struct qstr *name)
33 struct bch_str_hash_ctx ctx;
35 bch2_str_hash_init(&ctx, info);
36 bch2_str_hash_update(&ctx, info, name->name, name->len);
38 /* [0,2) reserved for dots */
39 return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
42 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
44 return bch2_dirent_hash(info, key);
47 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
49 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
50 struct qstr name = QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
52 return bch2_dirent_hash(info, &name);
55 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
57 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
58 int len = bch2_dirent_name_bytes(l);
59 const struct qstr *r = _r;
61 return len - r->len ?: memcmp(l.v->d_name, r->name, len);
64 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
66 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
67 struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
68 int l_len = bch2_dirent_name_bytes(l);
69 int r_len = bch2_dirent_name_bytes(r);
71 return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
74 const struct bch_hash_desc bch2_dirent_hash_desc = {
75 .btree_id = BTREE_ID_DIRENTS,
76 .key_type = BCH_DIRENT,
77 .whiteout_type = BCH_DIRENT_WHITEOUT,
78 .hash_key = dirent_hash_key,
79 .hash_bkey = dirent_hash_bkey,
80 .cmp_key = dirent_cmp_key,
81 .cmp_bkey = dirent_cmp_bkey,
84 const char *bch2_dirent_invalid(const struct bch_fs *c, struct bkey_s_c k)
86 struct bkey_s_c_dirent d;
91 if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent))
92 return "value too small";
94 d = bkey_s_c_to_dirent(k);
95 len = bch2_dirent_name_bytes(d);
100 if (bkey_val_u64s(k.k) > dirent_val_u64s(len))
101 return "value too big";
103 if (len > BCH_NAME_MAX)
104 return "dirent name too big";
106 if (memchr(d.v->d_name, '/', len))
107 return "dirent name has invalid characters";
110 case BCH_DIRENT_WHITEOUT:
111 return bkey_val_bytes(k.k) != 0
112 ? "value size should be zero"
116 return "invalid type";
120 void bch2_dirent_to_text(struct bch_fs *c, char *buf,
121 size_t size, struct bkey_s_c k)
123 struct bkey_s_c_dirent d;
128 d = bkey_s_c_to_dirent(k);
130 n += bch_scnmemcpy(buf + n, size - n, d.v->d_name,
131 bch2_dirent_name_bytes(d));
132 n += scnprintf(buf + n, size - n, " -> %llu", d.v->d_inum);
134 case BCH_DIRENT_WHITEOUT:
135 scnprintf(buf, size, "whiteout");
140 static struct bkey_i_dirent *dirent_create_key(u8 type,
141 const struct qstr *name, u64 dst)
143 struct bkey_i_dirent *dirent;
144 unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
146 if (name->len > BCH_NAME_MAX)
147 return ERR_PTR(-ENAMETOOLONG);
149 BUG_ON(u64s > U8_MAX);
151 dirent = kmalloc(u64s * sizeof(u64), GFP_NOFS);
153 return ERR_PTR(-ENOMEM);
155 bkey_dirent_init(&dirent->k_i);
156 dirent->k.u64s = u64s;
157 dirent->v.d_inum = cpu_to_le64(dst);
158 dirent->v.d_type = type;
160 memcpy(dirent->v.d_name, name->name, name->len);
161 memset(dirent->v.d_name + name->len, 0,
162 bkey_val_bytes(&dirent->k) -
163 offsetof(struct bch_dirent, d_name) -
166 EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
171 int bch2_dirent_create(struct bch_fs *c, u64 dir_inum,
172 const struct bch_hash_info *hash_info,
173 u8 type, const struct qstr *name, u64 dst_inum,
174 u64 *journal_seq, int flags)
176 struct bkey_i_dirent *dirent;
179 dirent = dirent_create_key(type, name, dst_inum);
181 return PTR_ERR(dirent);
183 ret = bch2_hash_set(bch2_dirent_hash_desc, hash_info, c, dir_inum,
184 journal_seq, &dirent->k_i, flags);
190 static void dirent_copy_target(struct bkey_i_dirent *dst,
191 struct bkey_s_c_dirent src)
193 dst->v.d_inum = src.v->d_inum;
194 dst->v.d_type = src.v->d_type;
197 static struct bpos bch2_dirent_pos(struct bch_inode_info *inode,
198 const struct qstr *name)
200 return POS(inode->v.i_ino, bch2_dirent_hash(&inode->ei_str_hash, name));
203 int bch2_dirent_rename(struct bch_fs *c,
204 struct bch_inode_info *src_dir, const struct qstr *src_name,
205 struct bch_inode_info *dst_dir, const struct qstr *dst_name,
206 u64 *journal_seq, enum bch_rename_mode mode)
208 struct btree_iter src_iter, dst_iter, whiteout_iter;
209 struct bkey_s_c old_src, old_dst;
211 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
212 struct bpos src_pos = bch2_dirent_pos(src_dir, src_name);
213 struct bpos dst_pos = bch2_dirent_pos(dst_dir, dst_name);
217 bch2_btree_iter_init(&src_iter, c, BTREE_ID_DIRENTS, src_pos,
218 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
219 bch2_btree_iter_init(&dst_iter, c, BTREE_ID_DIRENTS, dst_pos,
220 BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
221 bch2_btree_iter_link(&src_iter, &dst_iter);
223 bch2_btree_iter_init(&whiteout_iter, c, BTREE_ID_DIRENTS, src_pos,
225 bch2_btree_iter_link(&src_iter, &whiteout_iter);
227 if (mode == BCH_RENAME_EXCHANGE) {
228 new_src = dirent_create_key(0, src_name, 0);
229 if (IS_ERR(new_src)) {
230 ret = PTR_ERR(new_src);
234 new_src = (void *) &delete;
237 new_dst = dirent_create_key(0, dst_name, 0);
238 if (IS_ERR(new_dst)) {
239 ret = PTR_ERR(new_dst);
244 * Note that on -EINTR/dropped locks we're not restarting the lookup
245 * from the original hashed position (like we do when creating dirents,
246 * in bch_hash_set) - we never move existing dirents to different slot:
248 old_src = bch2_hash_lookup_at(bch2_dirent_hash_desc,
249 &src_dir->ei_str_hash,
250 &src_iter, src_name);
251 if ((ret = btree_iter_err(old_src)))
254 ret = bch2_hash_needs_whiteout(bch2_dirent_hash_desc,
255 &src_dir->ei_str_hash,
256 &whiteout_iter, &src_iter);
262 * Note that in BCH_RENAME mode, we're _not_ checking if
263 * the target already exists - we're relying on the VFS
264 * to do that check for us for correctness:
266 old_dst = mode == BCH_RENAME
267 ? bch2_hash_hole_at(bch2_dirent_hash_desc, &dst_iter)
268 : bch2_hash_lookup_at(bch2_dirent_hash_desc,
269 &dst_dir->ei_str_hash,
270 &dst_iter, dst_name);
271 if ((ret = btree_iter_err(old_dst)))
276 bkey_init(&new_src->k);
277 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
279 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
280 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
282 * If we couldn't insert new_dst at its hashed
283 * position (dst_pos) due to a hash collision,
284 * and we're going to be deleting in
285 * between the hashed position and first empty
286 * slot we found - just overwrite the pos we
287 * were going to delete:
289 * Note: this is a correctness issue, in this
290 * situation bch2_hash_needs_whiteout() could
291 * return false when the whiteout would have
292 * been needed if we inserted at the pos
293 * __dirent_find_hole() found
295 new_dst->k.p = src_iter.pos;
296 ret = bch2_btree_insert_at(c, NULL, NULL,
299 BTREE_INSERT_ENTRY(&src_iter,
305 new_src->k.type = BCH_DIRENT_WHITEOUT;
307 case BCH_RENAME_OVERWRITE:
308 bkey_init(&new_src->k);
309 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
311 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
312 bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
314 * Same case described above -
315 * bch_hash_needs_whiteout could spuriously
316 * return false, but we have to insert at
317 * dst_iter.pos because we're overwriting
320 new_src->k.type = BCH_DIRENT_WHITEOUT;
321 } else if (need_whiteout)
322 new_src->k.type = BCH_DIRENT_WHITEOUT;
324 case BCH_RENAME_EXCHANGE:
325 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
326 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
330 new_src->k.p = src_iter.pos;
331 new_dst->k.p = dst_iter.pos;
332 ret = bch2_btree_insert_at(c, NULL, NULL, journal_seq,
334 BTREE_INSERT_ENTRY(&src_iter, &new_src->k_i),
335 BTREE_INSERT_ENTRY(&dst_iter, &new_dst->k_i));
340 bch2_btree_iter_unlock(&whiteout_iter);
341 bch2_btree_iter_unlock(&dst_iter);
342 bch2_btree_iter_unlock(&src_iter);
344 if (new_src != (void *) &delete)
350 int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
351 const struct bch_hash_info *hash_info,
352 const struct qstr *name,
355 return bch2_hash_delete(bch2_dirent_hash_desc, hash_info,
356 c, dir_inum, journal_seq, name);
359 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
360 const struct bch_hash_info *hash_info,
361 const struct qstr *name)
363 struct btree_iter iter;
367 k = bch2_hash_lookup(bch2_dirent_hash_desc, hash_info, c,
368 dir_inum, &iter, name);
370 bch2_btree_iter_unlock(&iter);
374 inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
375 bch2_btree_iter_unlock(&iter);
380 int bch2_empty_dir(struct bch_fs *c, u64 dir_inum)
382 struct btree_iter iter;
386 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS(dir_inum, 0), 0, k) {
387 if (k.k->p.inode > dir_inum)
390 if (k.k->type == BCH_DIRENT) {
395 bch2_btree_iter_unlock(&iter);
400 int bch2_readdir(struct bch_fs *c, struct file *file,
401 struct dir_context *ctx)
403 struct bch_inode_info *inode = file_bch_inode(file);
404 struct btree_iter iter;
406 struct bkey_s_c_dirent dirent;
409 if (!dir_emit_dots(file, ctx))
412 for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
413 POS(inode->v.i_ino, ctx->pos), 0, k) {
414 if (k.k->type != BCH_DIRENT)
417 dirent = bkey_s_c_to_dirent(k);
419 if (bkey_cmp(k.k->p, POS(inode->v.i_ino, ctx->pos)) < 0)
422 if (k.k->p.inode > inode->v.i_ino)
425 len = bch2_dirent_name_bytes(dirent);
428 * XXX: dir_emit() can fault and block, while we're holding
431 if (!dir_emit(ctx, dirent.v->d_name, len,
432 le64_to_cpu(dirent.v->d_inum),
436 ctx->pos = k.k->p.offset + 1;
438 bch2_btree_iter_unlock(&iter);