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