]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/dirent.c
Update bcachefs sources to 720f644e63 bcachefs: Improve reflink repair code
[bcachefs-tools-debian] / libbcachefs / dirent.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "bkey_methods.h"
5 #include "btree_update.h"
6 #include "extents.h"
7 #include "dirent.h"
8 #include "fs.h"
9 #include "keylist.h"
10 #include "str_hash.h"
11 #include "subvolume.h"
12
13 #include <linux/dcache.h>
14
15 unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
16 {
17         unsigned len = bkey_val_bytes(d.k) -
18                 offsetof(struct bch_dirent, d_name);
19
20         return strnlen(d.v->d_name, len);
21 }
22
23 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
24                             const struct qstr *name)
25 {
26         struct bch_str_hash_ctx ctx;
27
28         bch2_str_hash_init(&ctx, info);
29         bch2_str_hash_update(&ctx, info, name->name, name->len);
30
31         /* [0,2) reserved for dots */
32         return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
33 }
34
35 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
36 {
37         return bch2_dirent_hash(info, key);
38 }
39
40 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
41 {
42         struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
43         struct qstr name = QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
44
45         return bch2_dirent_hash(info, &name);
46 }
47
48 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
49 {
50         struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
51         int len = bch2_dirent_name_bytes(l);
52         const struct qstr *r = _r;
53
54         return len - r->len ?: memcmp(l.v->d_name, r->name, len);
55 }
56
57 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
58 {
59         struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
60         struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
61         int l_len = bch2_dirent_name_bytes(l);
62         int r_len = bch2_dirent_name_bytes(r);
63
64         return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
65 }
66
67 const struct bch_hash_desc bch2_dirent_hash_desc = {
68         .btree_id       = BTREE_ID_dirents,
69         .key_type       = KEY_TYPE_dirent,
70         .hash_key       = dirent_hash_key,
71         .hash_bkey      = dirent_hash_bkey,
72         .cmp_key        = dirent_cmp_key,
73         .cmp_bkey       = dirent_cmp_bkey,
74 };
75
76 const char *bch2_dirent_invalid(const struct bch_fs *c, struct bkey_s_c k)
77 {
78         struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
79         unsigned len;
80
81         if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent))
82                 return "value too small";
83
84         len = bch2_dirent_name_bytes(d);
85         if (!len)
86                 return "empty name";
87
88         if (bkey_val_u64s(k.k) > dirent_val_u64s(len))
89                 return "value too big";
90
91         if (len > BCH_NAME_MAX)
92                 return "dirent name too big";
93
94         if (len == 1 && !memcmp(d.v->d_name, ".", 1))
95                 return "invalid name";
96
97         if (len == 2 && !memcmp(d.v->d_name, "..", 2))
98                 return "invalid name";
99
100         if (memchr(d.v->d_name, '/', len))
101                 return "invalid name";
102
103         if (d.v->d_type != DT_SUBVOL &&
104             le64_to_cpu(d.v->d_inum) == d.k->p.inode)
105                 return "dirent points to own directory";
106
107         return NULL;
108 }
109
110 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
111                          struct bkey_s_c k)
112 {
113         struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
114
115         bch_scnmemcpy(out, d.v->d_name,
116                       bch2_dirent_name_bytes(d));
117         pr_buf(out, " -> %llu type %s", d.v->d_inum,
118                d.v->d_type < BCH_DT_MAX
119                ? bch2_d_types[d.v->d_type]
120                : "(bad d_type)");
121 }
122
123 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
124                                 u8 type, const struct qstr *name, u64 dst)
125 {
126         struct bkey_i_dirent *dirent;
127         unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
128
129         if (name->len > BCH_NAME_MAX)
130                 return ERR_PTR(-ENAMETOOLONG);
131
132         BUG_ON(u64s > U8_MAX);
133
134         dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
135         if (IS_ERR(dirent))
136                 return dirent;
137
138         bkey_dirent_init(&dirent->k_i);
139         dirent->k.u64s = u64s;
140         dirent->v.d_inum = cpu_to_le64(dst);
141         dirent->v.d_type = type;
142
143         memcpy(dirent->v.d_name, name->name, name->len);
144         memset(dirent->v.d_name + name->len, 0,
145                bkey_val_bytes(&dirent->k) -
146                offsetof(struct bch_dirent, d_name) -
147                name->len);
148
149         EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
150
151         return dirent;
152 }
153
154 int bch2_dirent_create(struct btree_trans *trans, subvol_inum dir,
155                        const struct bch_hash_info *hash_info,
156                        u8 type, const struct qstr *name, u64 dst_inum,
157                        u64 *dir_offset, int flags)
158 {
159         struct bkey_i_dirent *dirent;
160         int ret;
161
162         dirent = dirent_create_key(trans, type, name, dst_inum);
163         ret = PTR_ERR_OR_ZERO(dirent);
164         if (ret)
165                 return ret;
166
167         ret = bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
168                             dir, &dirent->k_i, flags);
169         *dir_offset = dirent->k.p.offset;
170
171         return ret;
172 }
173
174 static void dirent_copy_target(struct bkey_i_dirent *dst,
175                                struct bkey_s_c_dirent src)
176 {
177         dst->v.d_inum = src.v->d_inum;
178         dst->v.d_type = src.v->d_type;
179 }
180
181 int __bch2_dirent_read_target(struct btree_trans *trans,
182                               struct bkey_s_c_dirent d,
183                               u32 *subvol, u32 *snapshot, u64 *inum,
184                               bool is_fsck)
185 {
186         struct bch_subvolume s;
187         int ret = 0;
188
189         *subvol         = 0;
190         *snapshot       = d.k->p.snapshot;
191
192         if (likely(d.v->d_type != DT_SUBVOL)) {
193                 *inum = le64_to_cpu(d.v->d_inum);
194         } else {
195                 *subvol = le64_to_cpu(d.v->d_inum);
196
197                 ret = bch2_subvolume_get(trans, *subvol, !is_fsck, BTREE_ITER_CACHED, &s);
198
199                 *snapshot       = le32_to_cpu(s.snapshot);
200                 *inum           = le64_to_cpu(s.inode);
201         }
202
203         return ret;
204 }
205
206 static int bch2_dirent_read_target(struct btree_trans *trans, subvol_inum dir,
207                                    struct bkey_s_c_dirent d, subvol_inum *target)
208 {
209         u32 snapshot;
210         int ret = 0;
211
212         ret = __bch2_dirent_read_target(trans, d, &target->subvol, &snapshot,
213                                         &target->inum, false);
214         if (!target->subvol)
215                 target->subvol = dir.subvol;
216
217         return ret;
218 }
219
220 int bch2_dirent_rename(struct btree_trans *trans,
221                 subvol_inum src_dir, struct bch_hash_info *src_hash,
222                 subvol_inum dst_dir, struct bch_hash_info *dst_hash,
223                 const struct qstr *src_name, subvol_inum *src_inum, u64 *src_offset,
224                 const struct qstr *dst_name, subvol_inum *dst_inum, u64 *dst_offset,
225                 enum bch_rename_mode mode)
226 {
227         struct btree_iter src_iter = { NULL };
228         struct btree_iter dst_iter = { NULL };
229         struct bkey_s_c old_src, old_dst;
230         struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
231         struct bpos dst_pos =
232                 POS(dst_dir.inum, bch2_dirent_hash(dst_hash, dst_name));
233         int ret = 0;
234
235         if (src_dir.subvol != dst_dir.subvol)
236                 return -EXDEV;
237
238         memset(src_inum, 0, sizeof(*src_inum));
239         memset(dst_inum, 0, sizeof(*dst_inum));
240
241         /*
242          * Lookup dst:
243          *
244          * Note that in BCH_RENAME mode, we're _not_ checking if
245          * the target already exists - we're relying on the VFS
246          * to do that check for us for correctness:
247          */
248         ret = mode == BCH_RENAME
249                 ? bch2_hash_hole(trans, &dst_iter, bch2_dirent_hash_desc,
250                                  dst_hash, dst_dir, dst_name)
251                 : bch2_hash_lookup(trans, &dst_iter, bch2_dirent_hash_desc,
252                                    dst_hash, dst_dir, dst_name,
253                                    BTREE_ITER_INTENT);
254         if (ret)
255                 goto out;
256
257         old_dst = bch2_btree_iter_peek_slot(&dst_iter);
258         ret = bkey_err(old_dst);
259         if (ret)
260                 goto out;
261
262         if (mode != BCH_RENAME) {
263                 ret = bch2_dirent_read_target(trans, dst_dir,
264                                 bkey_s_c_to_dirent(old_dst), dst_inum);
265                 if (ret)
266                         goto out;
267         }
268         if (mode != BCH_RENAME_EXCHANGE)
269                 *src_offset = dst_iter.pos.offset;
270
271         /* Lookup src: */
272         ret = bch2_hash_lookup(trans, &src_iter, bch2_dirent_hash_desc,
273                                src_hash, src_dir, src_name,
274                                BTREE_ITER_INTENT);
275         if (ret)
276                 goto out;
277
278         old_src = bch2_btree_iter_peek_slot(&src_iter);
279         ret = bkey_err(old_src);
280         if (ret)
281                 goto out;
282
283         ret = bch2_dirent_read_target(trans, src_dir,
284                         bkey_s_c_to_dirent(old_src), src_inum);
285         if (ret)
286                 goto out;
287
288         /* Create new dst key: */
289         new_dst = dirent_create_key(trans, 0, dst_name, 0);
290         ret = PTR_ERR_OR_ZERO(new_dst);
291         if (ret)
292                 goto out;
293
294         dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
295         new_dst->k.p = dst_iter.pos;
296
297         /* Create new src key: */
298         if (mode == BCH_RENAME_EXCHANGE) {
299                 new_src = dirent_create_key(trans, 0, src_name, 0);
300                 ret = PTR_ERR_OR_ZERO(new_src);
301                 if (ret)
302                         goto out;
303
304                 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
305                 new_src->k.p = src_iter.pos;
306         } else {
307                 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
308                 ret = PTR_ERR_OR_ZERO(new_src);
309                 if (ret)
310                         goto out;
311
312                 bkey_init(&new_src->k);
313                 new_src->k.p = src_iter.pos;
314
315                 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
316                     bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
317                         /*
318                          * We have a hash collision for the new dst key,
319                          * and new_src - the key we're deleting - is between
320                          * new_dst's hashed slot and the slot we're going to be
321                          * inserting it into - oops.  This will break the hash
322                          * table if we don't deal with it:
323                          */
324                         if (mode == BCH_RENAME) {
325                                 /*
326                                  * If we're not overwriting, we can just insert
327                                  * new_dst at the src position:
328                                  */
329                                 new_dst->k.p = src_iter.pos;
330                                 bch2_trans_update(trans, &src_iter,
331                                                   &new_dst->k_i, 0);
332                                 goto out_set_offset;
333                         } else {
334                                 /* If we're overwriting, we can't insert new_dst
335                                  * at a different slot because it has to
336                                  * overwrite old_dst - just make sure to use a
337                                  * whiteout when deleting src:
338                                  */
339                                 new_src->k.type = KEY_TYPE_hash_whiteout;
340                         }
341                 } else {
342                         /* Check if we need a whiteout to delete src: */
343                         ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
344                                                        src_hash, &src_iter);
345                         if (ret < 0)
346                                 goto out;
347
348                         if (ret)
349                                 new_src->k.type = KEY_TYPE_hash_whiteout;
350                 }
351         }
352
353         bch2_trans_update(trans, &src_iter, &new_src->k_i, 0);
354         bch2_trans_update(trans, &dst_iter, &new_dst->k_i, 0);
355 out_set_offset:
356         if (mode == BCH_RENAME_EXCHANGE)
357                 *src_offset = new_src->k.p.offset;
358         *dst_offset = new_dst->k.p.offset;
359 out:
360         bch2_trans_iter_exit(trans, &src_iter);
361         bch2_trans_iter_exit(trans, &dst_iter);
362         return ret;
363 }
364
365 int __bch2_dirent_lookup_trans(struct btree_trans *trans,
366                                struct btree_iter *iter,
367                                subvol_inum dir,
368                                const struct bch_hash_info *hash_info,
369                                const struct qstr *name, subvol_inum *inum,
370                                unsigned flags)
371 {
372         struct bkey_s_c k;
373         struct bkey_s_c_dirent d;
374         u32 snapshot;
375         int ret;
376
377         ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
378         if (ret)
379                 return ret;
380
381         ret = bch2_hash_lookup(trans, iter, bch2_dirent_hash_desc,
382                                hash_info, dir, name, flags);
383         if (ret)
384                 return ret;
385
386         k = bch2_btree_iter_peek_slot(iter);
387         ret = bkey_err(k);
388         if (ret) {
389                 bch2_trans_iter_exit(trans, iter);
390                 return ret;
391         }
392
393         d = bkey_s_c_to_dirent(k);
394
395         ret = bch2_dirent_read_target(trans, dir, d, inum);
396         if (ret)
397                 bch2_trans_iter_exit(trans, iter);
398
399         return ret;
400 }
401
402 u64 bch2_dirent_lookup(struct bch_fs *c, subvol_inum dir,
403                        const struct bch_hash_info *hash_info,
404                        const struct qstr *name, subvol_inum *inum)
405 {
406         struct btree_trans trans;
407         struct btree_iter iter;
408         int ret;
409
410         bch2_trans_init(&trans, c, 0, 0);
411 retry:
412         bch2_trans_begin(&trans);
413
414         ret = __bch2_dirent_lookup_trans(&trans, &iter, dir, hash_info,
415                                           name, inum, 0);
416
417         bch2_trans_iter_exit(&trans, &iter);
418         if (ret == -EINTR)
419                 goto retry;
420         bch2_trans_exit(&trans);
421         return ret;
422 }
423
424 int bch2_empty_dir_trans(struct btree_trans *trans, subvol_inum dir)
425 {
426         struct btree_iter iter;
427         struct bkey_s_c k;
428         u32 snapshot;
429         int ret;
430
431         ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
432         if (ret)
433                 return ret;
434
435         for_each_btree_key(trans, iter, BTREE_ID_dirents,
436                            SPOS(dir.inum, 0, snapshot), 0, k, ret) {
437                 if (k.k->p.inode > dir.inum)
438                         break;
439
440                 if (k.k->type == KEY_TYPE_dirent) {
441                         ret = -ENOTEMPTY;
442                         break;
443                 }
444         }
445         bch2_trans_iter_exit(trans, &iter);
446
447         return ret;
448 }
449
450 int bch2_readdir(struct bch_fs *c, subvol_inum inum, struct dir_context *ctx)
451 {
452         struct btree_trans trans;
453         struct btree_iter iter;
454         struct bkey_s_c k;
455         struct bkey_s_c_dirent dirent;
456         u32 snapshot;
457         int ret;
458
459         bch2_trans_init(&trans, c, 0, 0);
460 retry:
461         bch2_trans_begin(&trans);
462
463         ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
464         if (ret)
465                 goto err;
466
467         for_each_btree_key(&trans, iter, BTREE_ID_dirents,
468                            SPOS(inum.inum, ctx->pos, snapshot), 0, k, ret) {
469                 if (k.k->p.inode > inum.inum)
470                         break;
471
472                 if (k.k->type != KEY_TYPE_dirent)
473                         continue;
474
475                 dirent = bkey_s_c_to_dirent(k);
476
477                 /*
478                  * XXX: dir_emit() can fault and block, while we're holding
479                  * locks
480                  */
481                 ctx->pos = dirent.k->p.offset;
482                 if (!dir_emit(ctx, dirent.v->d_name,
483                               bch2_dirent_name_bytes(dirent),
484                               le64_to_cpu(dirent.v->d_inum),
485                               vfs_d_type(dirent.v->d_type)))
486                         break;
487                 ctx->pos = dirent.k->p.offset + 1;
488         }
489         bch2_trans_iter_exit(&trans, &iter);
490 err:
491         if (ret == -EINTR)
492                 goto retry;
493
494         ret = bch2_trans_exit(&trans) ?: ret;
495
496         return ret;
497 }