]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/dirent.c
Update bcachefs sources to 18686af684 bcachefs: Inode backpointers
[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
12 #include <linux/dcache.h>
13
14 unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
15 {
16         unsigned len = bkey_val_bytes(d.k) -
17                 offsetof(struct bch_dirent, d_name);
18
19         return strnlen(d.v->d_name, len);
20 }
21
22 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
23                             const struct qstr *name)
24 {
25         struct bch_str_hash_ctx ctx;
26
27         bch2_str_hash_init(&ctx, info);
28         bch2_str_hash_update(&ctx, info, name->name, name->len);
29
30         /* [0,2) reserved for dots */
31         return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
32 }
33
34 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
35 {
36         return bch2_dirent_hash(info, key);
37 }
38
39 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
40 {
41         struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
42         struct qstr name = QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
43
44         return bch2_dirent_hash(info, &name);
45 }
46
47 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
48 {
49         struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
50         int len = bch2_dirent_name_bytes(l);
51         const struct qstr *r = _r;
52
53         return len - r->len ?: memcmp(l.v->d_name, r->name, len);
54 }
55
56 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
57 {
58         struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
59         struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
60         int l_len = bch2_dirent_name_bytes(l);
61         int r_len = bch2_dirent_name_bytes(r);
62
63         return l_len - r_len ?: memcmp(l.v->d_name, r.v->d_name, l_len);
64 }
65
66 const struct bch_hash_desc bch2_dirent_hash_desc = {
67         .btree_id       = BTREE_ID_dirents,
68         .key_type       = KEY_TYPE_dirent,
69         .hash_key       = dirent_hash_key,
70         .hash_bkey      = dirent_hash_bkey,
71         .cmp_key        = dirent_cmp_key,
72         .cmp_bkey       = dirent_cmp_bkey,
73 };
74
75 const char *bch2_dirent_invalid(const struct bch_fs *c, struct bkey_s_c k)
76 {
77         struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
78         unsigned len;
79
80         if (bkey_val_bytes(k.k) < sizeof(struct bch_dirent))
81                 return "value too small";
82
83         len = bch2_dirent_name_bytes(d);
84         if (!len)
85                 return "empty name";
86
87         /*
88          * older versions of bcachefs were buggy and creating dirent
89          * keys that were bigger than necessary:
90          */
91         if (bkey_val_u64s(k.k) > dirent_val_u64s(len + 7))
92                 return "value too big";
93
94         if (len > BCH_NAME_MAX)
95                 return "dirent name too big";
96
97         return NULL;
98 }
99
100 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
101                          struct bkey_s_c k)
102 {
103         struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
104
105         bch_scnmemcpy(out, d.v->d_name,
106                       bch2_dirent_name_bytes(d));
107         pr_buf(out, " -> %llu type %u", d.v->d_inum, d.v->d_type);
108 }
109
110 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
111                                 u8 type, const struct qstr *name, u64 dst)
112 {
113         struct bkey_i_dirent *dirent;
114         unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
115
116         if (name->len > BCH_NAME_MAX)
117                 return ERR_PTR(-ENAMETOOLONG);
118
119         BUG_ON(u64s > U8_MAX);
120
121         dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
122         if (IS_ERR(dirent))
123                 return dirent;
124
125         bkey_dirent_init(&dirent->k_i);
126         dirent->k.u64s = u64s;
127         dirent->v.d_inum = cpu_to_le64(dst);
128         dirent->v.d_type = type;
129
130         memcpy(dirent->v.d_name, name->name, name->len);
131         memset(dirent->v.d_name + name->len, 0,
132                bkey_val_bytes(&dirent->k) -
133                offsetof(struct bch_dirent, d_name) -
134                name->len);
135
136         EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
137
138         return dirent;
139 }
140
141 int bch2_dirent_create(struct btree_trans *trans,
142                        u64 dir_inum, const struct bch_hash_info *hash_info,
143                        u8 type, const struct qstr *name, u64 dst_inum,
144                        u64 *dir_offset, int flags)
145 {
146         struct bkey_i_dirent *dirent;
147         int ret;
148
149         dirent = dirent_create_key(trans, type, name, dst_inum);
150         ret = PTR_ERR_OR_ZERO(dirent);
151         if (ret)
152                 return ret;
153
154         ret = bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
155                             dir_inum, &dirent->k_i, flags);
156         *dir_offset = dirent->k.p.offset;
157
158         return ret;
159 }
160
161 static void dirent_copy_target(struct bkey_i_dirent *dst,
162                                struct bkey_s_c_dirent src)
163 {
164         dst->v.d_inum = src.v->d_inum;
165         dst->v.d_type = src.v->d_type;
166 }
167
168 int bch2_dirent_rename(struct btree_trans *trans,
169                        u64 src_dir, struct bch_hash_info *src_hash,
170                        u64 dst_dir, struct bch_hash_info *dst_hash,
171                        const struct qstr *src_name, u64 *src_inum, u64 *src_offset,
172                        const struct qstr *dst_name, u64 *dst_inum, u64 *dst_offset,
173                        enum bch_rename_mode mode)
174 {
175         struct btree_iter *src_iter = NULL, *dst_iter = NULL;
176         struct bkey_s_c old_src, old_dst;
177         struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
178         struct bpos dst_pos =
179                 POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
180         int ret = 0;
181
182         *src_inum = *dst_inum = 0;
183
184         /*
185          * Lookup dst:
186          *
187          * Note that in BCH_RENAME mode, we're _not_ checking if
188          * the target already exists - we're relying on the VFS
189          * to do that check for us for correctness:
190          */
191         dst_iter = mode == BCH_RENAME
192                 ? bch2_hash_hole(trans, bch2_dirent_hash_desc,
193                                  dst_hash, dst_dir, dst_name)
194                 : bch2_hash_lookup(trans, bch2_dirent_hash_desc,
195                                    dst_hash, dst_dir, dst_name,
196                                    BTREE_ITER_INTENT);
197         ret = PTR_ERR_OR_ZERO(dst_iter);
198         if (ret)
199                 goto out;
200
201         old_dst = bch2_btree_iter_peek_slot(dst_iter);
202
203         if (mode != BCH_RENAME)
204                 *dst_inum = le64_to_cpu(bkey_s_c_to_dirent(old_dst).v->d_inum);
205
206         /* Lookup src: */
207         src_iter = bch2_hash_lookup(trans, bch2_dirent_hash_desc,
208                                     src_hash, src_dir, src_name,
209                                     BTREE_ITER_INTENT);
210         ret = PTR_ERR_OR_ZERO(src_iter);
211         if (ret)
212                 goto out;
213
214         old_src = bch2_btree_iter_peek_slot(src_iter);
215         *src_inum = le64_to_cpu(bkey_s_c_to_dirent(old_src).v->d_inum);
216
217         /* Create new dst key: */
218         new_dst = dirent_create_key(trans, 0, dst_name, 0);
219         ret = PTR_ERR_OR_ZERO(new_dst);
220         if (ret)
221                 goto out;
222
223         dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
224         new_dst->k.p = dst_iter->pos;
225
226         /* Create new src key: */
227         if (mode == BCH_RENAME_EXCHANGE) {
228                 new_src = dirent_create_key(trans, 0, src_name, 0);
229                 ret = PTR_ERR_OR_ZERO(new_src);
230                 if (ret)
231                         goto out;
232
233                 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
234                 new_src->k.p = src_iter->pos;
235         } else {
236                 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
237                 ret = PTR_ERR_OR_ZERO(new_src);
238                 if (ret)
239                         goto out;
240
241                 bkey_init(&new_src->k);
242                 new_src->k.p = src_iter->pos;
243
244                 if (bkey_cmp(dst_pos, src_iter->pos) <= 0 &&
245                     bkey_cmp(src_iter->pos, dst_iter->pos) < 0) {
246                         /*
247                          * We have a hash collision for the new dst key,
248                          * and new_src - the key we're deleting - is between
249                          * new_dst's hashed slot and the slot we're going to be
250                          * inserting it into - oops.  This will break the hash
251                          * table if we don't deal with it:
252                          */
253                         if (mode == BCH_RENAME) {
254                                 /*
255                                  * If we're not overwriting, we can just insert
256                                  * new_dst at the src position:
257                                  */
258                                 new_dst->k.p = src_iter->pos;
259                                 bch2_trans_update(trans, src_iter,
260                                                   &new_dst->k_i, 0);
261                                 goto out_set_offset;
262                         } else {
263                                 /* If we're overwriting, we can't insert new_dst
264                                  * at a different slot because it has to
265                                  * overwrite old_dst - just make sure to use a
266                                  * whiteout when deleting src:
267                                  */
268                                 new_src->k.type = KEY_TYPE_hash_whiteout;
269                         }
270                 } else {
271                         /* Check if we need a whiteout to delete src: */
272                         ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
273                                                        src_hash, src_iter);
274                         if (ret < 0)
275                                 goto out;
276
277                         if (ret)
278                                 new_src->k.type = KEY_TYPE_hash_whiteout;
279                 }
280         }
281
282         bch2_trans_update(trans, src_iter, &new_src->k_i, 0);
283         bch2_trans_update(trans, dst_iter, &new_dst->k_i, 0);
284 out_set_offset:
285         *src_offset = new_src->k.p.offset;
286         *dst_offset = new_dst->k.p.offset;
287 out:
288         bch2_trans_iter_put(trans, src_iter);
289         bch2_trans_iter_put(trans, dst_iter);
290         return ret;
291 }
292
293 int bch2_dirent_delete_at(struct btree_trans *trans,
294                           const struct bch_hash_info *hash_info,
295                           struct btree_iter *iter)
296 {
297         return bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
298                                    hash_info, iter);
299 }
300
301 struct btree_iter *
302 __bch2_dirent_lookup_trans(struct btree_trans *trans, u64 dir_inum,
303                            const struct bch_hash_info *hash_info,
304                            const struct qstr *name, unsigned flags)
305 {
306         return bch2_hash_lookup(trans, bch2_dirent_hash_desc,
307                                 hash_info, dir_inum, name, flags);
308 }
309
310 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
311                        const struct bch_hash_info *hash_info,
312                        const struct qstr *name)
313 {
314         struct btree_trans trans;
315         struct btree_iter *iter;
316         struct bkey_s_c k;
317         u64 inum = 0;
318
319         bch2_trans_init(&trans, c, 0, 0);
320
321         iter = __bch2_dirent_lookup_trans(&trans, dir_inum,
322                                           hash_info, name, 0);
323         if (IS_ERR(iter)) {
324                 BUG_ON(PTR_ERR(iter) == -EINTR);
325                 goto out;
326         }
327
328         k = bch2_btree_iter_peek_slot(iter);
329         inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
330         bch2_trans_iter_put(&trans, iter);
331 out:
332         bch2_trans_exit(&trans);
333         return inum;
334 }
335
336 int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
337 {
338         struct btree_iter *iter;
339         struct bkey_s_c k;
340         int ret;
341
342         for_each_btree_key(trans, iter, BTREE_ID_dirents,
343                            POS(dir_inum, 0), 0, k, ret) {
344                 if (k.k->p.inode > dir_inum)
345                         break;
346
347                 if (k.k->type == KEY_TYPE_dirent) {
348                         ret = -ENOTEMPTY;
349                         break;
350                 }
351         }
352         bch2_trans_iter_put(trans, iter);
353
354         return ret;
355 }
356
357 int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
358 {
359         struct btree_trans trans;
360         struct btree_iter *iter;
361         struct bkey_s_c k;
362         struct bkey_s_c_dirent dirent;
363         int ret;
364
365         bch2_trans_init(&trans, c, 0, 0);
366
367         for_each_btree_key(&trans, iter, BTREE_ID_dirents,
368                            POS(inum, ctx->pos), 0, k, ret) {
369                 if (k.k->p.inode > inum)
370                         break;
371
372                 if (k.k->type != KEY_TYPE_dirent)
373                         continue;
374
375                 dirent = bkey_s_c_to_dirent(k);
376
377                 /*
378                  * XXX: dir_emit() can fault and block, while we're holding
379                  * locks
380                  */
381                 ctx->pos = dirent.k->p.offset;
382                 if (!dir_emit(ctx, dirent.v->d_name,
383                               bch2_dirent_name_bytes(dirent),
384                               le64_to_cpu(dirent.v->d_inum),
385                               dirent.v->d_type))
386                         break;
387                 ctx->pos = dirent.k->p.offset + 1;
388         }
389         bch2_trans_iter_put(&trans, iter);
390
391         ret = bch2_trans_exit(&trans) ?: ret;
392
393         return ret;
394 }