]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/dirent.c
rst2man: convert `which` to `command -v`
[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         int ret = 0;
187
188         *subvol         = 0;
189         *snapshot       = d.k->p.snapshot;
190
191         if (likely(d.v->d_type != DT_SUBVOL)) {
192                 *inum = le64_to_cpu(d.v->d_inum);
193         } else {
194                 struct bch_subvolume s;
195                 int ret;
196
197                 *subvol = le64_to_cpu(d.v->d_inum);
198
199                 ret = bch2_subvolume_get(trans, *subvol, !is_fsck, BTREE_ITER_CACHED, &s);
200
201                 *snapshot       = le32_to_cpu(s.snapshot);
202                 *inum           = le64_to_cpu(s.inode);
203         }
204
205         return ret;
206 }
207
208 static int bch2_dirent_read_target(struct btree_trans *trans, subvol_inum dir,
209                                    struct bkey_s_c_dirent d, subvol_inum *target)
210 {
211         u32 snapshot;
212         int ret = 0;
213
214         ret = __bch2_dirent_read_target(trans, d, &target->subvol, &snapshot,
215                                         &target->inum, false);
216         if (!target->subvol)
217                 target->subvol = dir.subvol;
218
219         return ret;
220 }
221
222 int bch2_dirent_rename(struct btree_trans *trans,
223                 subvol_inum src_dir, struct bch_hash_info *src_hash,
224                 subvol_inum dst_dir, struct bch_hash_info *dst_hash,
225                 const struct qstr *src_name, subvol_inum *src_inum, u64 *src_offset,
226                 const struct qstr *dst_name, subvol_inum *dst_inum, u64 *dst_offset,
227                 enum bch_rename_mode mode)
228 {
229         struct btree_iter src_iter = { NULL };
230         struct btree_iter dst_iter = { NULL };
231         struct bkey_s_c old_src, old_dst;
232         struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
233         struct bpos dst_pos =
234                 POS(dst_dir.inum, bch2_dirent_hash(dst_hash, dst_name));
235         int ret = 0;
236
237         if (src_dir.subvol != dst_dir.subvol)
238                 return -EXDEV;
239
240         memset(src_inum, 0, sizeof(*src_inum));
241         memset(dst_inum, 0, sizeof(*dst_inum));
242
243         /*
244          * Lookup dst:
245          *
246          * Note that in BCH_RENAME mode, we're _not_ checking if
247          * the target already exists - we're relying on the VFS
248          * to do that check for us for correctness:
249          */
250         ret = mode == BCH_RENAME
251                 ? bch2_hash_hole(trans, &dst_iter, bch2_dirent_hash_desc,
252                                  dst_hash, dst_dir, dst_name)
253                 : bch2_hash_lookup(trans, &dst_iter, bch2_dirent_hash_desc,
254                                    dst_hash, dst_dir, dst_name,
255                                    BTREE_ITER_INTENT);
256         if (ret)
257                 goto out;
258
259         old_dst = bch2_btree_iter_peek_slot(&dst_iter);
260         ret = bkey_err(old_dst);
261         if (ret)
262                 goto out;
263
264         if (mode != BCH_RENAME) {
265                 ret = bch2_dirent_read_target(trans, dst_dir,
266                                 bkey_s_c_to_dirent(old_dst), dst_inum);
267                 if (ret)
268                         goto out;
269         }
270         if (mode != BCH_RENAME_EXCHANGE)
271                 *src_offset = dst_iter.pos.offset;
272
273         /* Lookup src: */
274         ret = bch2_hash_lookup(trans, &src_iter, bch2_dirent_hash_desc,
275                                src_hash, src_dir, src_name,
276                                BTREE_ITER_INTENT);
277         if (ret)
278                 goto out;
279
280         old_src = bch2_btree_iter_peek_slot(&src_iter);
281         ret = bkey_err(old_src);
282         if (ret)
283                 goto out;
284
285         ret = bch2_dirent_read_target(trans, src_dir,
286                         bkey_s_c_to_dirent(old_src), src_inum);
287         if (ret)
288                 goto out;
289
290         /* Create new dst key: */
291         new_dst = dirent_create_key(trans, 0, dst_name, 0);
292         ret = PTR_ERR_OR_ZERO(new_dst);
293         if (ret)
294                 goto out;
295
296         dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
297         new_dst->k.p = dst_iter.pos;
298
299         /* Create new src key: */
300         if (mode == BCH_RENAME_EXCHANGE) {
301                 new_src = dirent_create_key(trans, 0, src_name, 0);
302                 ret = PTR_ERR_OR_ZERO(new_src);
303                 if (ret)
304                         goto out;
305
306                 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
307                 new_src->k.p = src_iter.pos;
308         } else {
309                 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
310                 ret = PTR_ERR_OR_ZERO(new_src);
311                 if (ret)
312                         goto out;
313
314                 bkey_init(&new_src->k);
315                 new_src->k.p = src_iter.pos;
316
317                 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
318                     bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
319                         /*
320                          * We have a hash collision for the new dst key,
321                          * and new_src - the key we're deleting - is between
322                          * new_dst's hashed slot and the slot we're going to be
323                          * inserting it into - oops.  This will break the hash
324                          * table if we don't deal with it:
325                          */
326                         if (mode == BCH_RENAME) {
327                                 /*
328                                  * If we're not overwriting, we can just insert
329                                  * new_dst at the src position:
330                                  */
331                                 new_dst->k.p = src_iter.pos;
332                                 bch2_trans_update(trans, &src_iter,
333                                                   &new_dst->k_i, 0);
334                                 goto out_set_offset;
335                         } else {
336                                 /* If we're overwriting, we can't insert new_dst
337                                  * at a different slot because it has to
338                                  * overwrite old_dst - just make sure to use a
339                                  * whiteout when deleting src:
340                                  */
341                                 new_src->k.type = KEY_TYPE_hash_whiteout;
342                         }
343                 } else {
344                         /* Check if we need a whiteout to delete src: */
345                         ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
346                                                        src_hash, &src_iter);
347                         if (ret < 0)
348                                 goto out;
349
350                         if (ret)
351                                 new_src->k.type = KEY_TYPE_hash_whiteout;
352                 }
353         }
354
355         bch2_trans_update(trans, &src_iter, &new_src->k_i, 0);
356         bch2_trans_update(trans, &dst_iter, &new_dst->k_i, 0);
357 out_set_offset:
358         if (mode == BCH_RENAME_EXCHANGE)
359                 *src_offset = new_src->k.p.offset;
360         *dst_offset = new_dst->k.p.offset;
361 out:
362         bch2_trans_iter_exit(trans, &src_iter);
363         bch2_trans_iter_exit(trans, &dst_iter);
364         return ret;
365 }
366
367 int __bch2_dirent_lookup_trans(struct btree_trans *trans,
368                                struct btree_iter *iter,
369                                subvol_inum dir,
370                                const struct bch_hash_info *hash_info,
371                                const struct qstr *name, subvol_inum *inum,
372                                unsigned flags)
373 {
374         struct bkey_s_c k;
375         struct bkey_s_c_dirent d;
376         u32 snapshot;
377         int ret;
378
379         ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
380         if (ret)
381                 return ret;
382
383         ret = bch2_hash_lookup(trans, iter, bch2_dirent_hash_desc,
384                                hash_info, dir, name, flags);
385         if (ret)
386                 return ret;
387
388         k = bch2_btree_iter_peek_slot(iter);
389         ret = bkey_err(k);
390         if (ret) {
391                 bch2_trans_iter_exit(trans, iter);
392                 return ret;
393         }
394
395         d = bkey_s_c_to_dirent(k);
396
397         ret = bch2_dirent_read_target(trans, dir, d, inum);
398         if (ret)
399                 bch2_trans_iter_exit(trans, iter);
400
401         return ret;
402 }
403
404 u64 bch2_dirent_lookup(struct bch_fs *c, subvol_inum dir,
405                        const struct bch_hash_info *hash_info,
406                        const struct qstr *name, subvol_inum *inum)
407 {
408         struct btree_trans trans;
409         struct btree_iter iter;
410         int ret;
411
412         bch2_trans_init(&trans, c, 0, 0);
413 retry:
414         bch2_trans_begin(&trans);
415
416         ret = __bch2_dirent_lookup_trans(&trans, &iter, dir, hash_info,
417                                           name, inum, 0);
418
419         bch2_trans_iter_exit(&trans, &iter);
420         if (ret == -EINTR)
421                 goto retry;
422         bch2_trans_exit(&trans);
423         return ret;
424 }
425
426 int bch2_empty_dir_trans(struct btree_trans *trans, subvol_inum dir)
427 {
428         struct btree_iter iter;
429         struct bkey_s_c k;
430         u32 snapshot;
431         int ret;
432
433         ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
434         if (ret)
435                 return ret;
436
437         for_each_btree_key(trans, iter, BTREE_ID_dirents,
438                            SPOS(dir.inum, 0, snapshot), 0, k, ret) {
439                 if (k.k->p.inode > dir.inum)
440                         break;
441
442                 if (k.k->type == KEY_TYPE_dirent) {
443                         ret = -ENOTEMPTY;
444                         break;
445                 }
446         }
447         bch2_trans_iter_exit(trans, &iter);
448
449         return ret;
450 }
451
452 int bch2_readdir(struct bch_fs *c, subvol_inum inum, struct dir_context *ctx)
453 {
454         struct btree_trans trans;
455         struct btree_iter iter;
456         struct bkey_s_c k;
457         struct bkey_s_c_dirent dirent;
458         u32 snapshot;
459         int ret;
460
461         bch2_trans_init(&trans, c, 0, 0);
462 retry:
463         bch2_trans_begin(&trans);
464
465         ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
466         if (ret)
467                 goto err;
468
469         for_each_btree_key(&trans, iter, BTREE_ID_dirents,
470                            SPOS(inum.inum, ctx->pos, snapshot), 0, k, ret) {
471                 if (k.k->p.inode > inum.inum)
472                         break;
473
474                 if (k.k->type != KEY_TYPE_dirent)
475                         continue;
476
477                 dirent = bkey_s_c_to_dirent(k);
478
479                 /*
480                  * XXX: dir_emit() can fault and block, while we're holding
481                  * locks
482                  */
483                 ctx->pos = dirent.k->p.offset;
484                 if (!dir_emit(ctx, dirent.v->d_name,
485                               bch2_dirent_name_bytes(dirent),
486                               le64_to_cpu(dirent.v->d_inum),
487                               vfs_d_type(dirent.v->d_type)))
488                         break;
489                 ctx->pos = dirent.k->p.offset + 1;
490         }
491         bch2_trans_iter_exit(&trans, &iter);
492 err:
493         if (ret == -EINTR)
494                 goto retry;
495
496         ret = bch2_trans_exit(&trans) ?: ret;
497
498         return ret;
499 }