]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/dirent.c
Update bcachefs sources to ac3ab6a511 bcachefs: Fix an uninitialized var
[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         if (bkey_val_u64s(k.k) > dirent_val_u64s(len))
88                 return "value too big";
89
90         if (len > BCH_NAME_MAX)
91                 return "dirent name too big";
92
93         if (len == 1 && !memcmp(d.v->d_name, ".", 1))
94                 return "invalid name";
95
96         if (len == 2 && !memcmp(d.v->d_name, "..", 2))
97                 return "invalid name";
98
99         if (memchr(d.v->d_name, '/', len))
100                 return "invalid name";
101
102         if (le64_to_cpu(d.v->d_inum) == d.k->p.inode)
103                 return "dirent points to own directory";
104
105         return NULL;
106 }
107
108 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
109                          struct bkey_s_c k)
110 {
111         struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
112
113         bch_scnmemcpy(out, d.v->d_name,
114                       bch2_dirent_name_bytes(d));
115         pr_buf(out, " -> %llu type %u", d.v->d_inum, d.v->d_type);
116 }
117
118 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
119                                 u8 type, const struct qstr *name, u64 dst)
120 {
121         struct bkey_i_dirent *dirent;
122         unsigned u64s = BKEY_U64s + dirent_val_u64s(name->len);
123
124         if (name->len > BCH_NAME_MAX)
125                 return ERR_PTR(-ENAMETOOLONG);
126
127         BUG_ON(u64s > U8_MAX);
128
129         dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
130         if (IS_ERR(dirent))
131                 return dirent;
132
133         bkey_dirent_init(&dirent->k_i);
134         dirent->k.u64s = u64s;
135         dirent->v.d_inum = cpu_to_le64(dst);
136         dirent->v.d_type = type;
137
138         memcpy(dirent->v.d_name, name->name, name->len);
139         memset(dirent->v.d_name + name->len, 0,
140                bkey_val_bytes(&dirent->k) -
141                offsetof(struct bch_dirent, d_name) -
142                name->len);
143
144         EBUG_ON(bch2_dirent_name_bytes(dirent_i_to_s_c(dirent)) != name->len);
145
146         return dirent;
147 }
148
149 int bch2_dirent_create(struct btree_trans *trans,
150                        u64 dir_inum, const struct bch_hash_info *hash_info,
151                        u8 type, const struct qstr *name, u64 dst_inum,
152                        u64 *dir_offset, int flags)
153 {
154         struct bkey_i_dirent *dirent;
155         int ret;
156
157         dirent = dirent_create_key(trans, type, name, dst_inum);
158         ret = PTR_ERR_OR_ZERO(dirent);
159         if (ret)
160                 return ret;
161
162         ret = bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
163                             dir_inum, &dirent->k_i, flags);
164         *dir_offset = dirent->k.p.offset;
165
166         return ret;
167 }
168
169 static void dirent_copy_target(struct bkey_i_dirent *dst,
170                                struct bkey_s_c_dirent src)
171 {
172         dst->v.d_inum = src.v->d_inum;
173         dst->v.d_type = src.v->d_type;
174 }
175
176 int bch2_dirent_rename(struct btree_trans *trans,
177                        u64 src_dir, struct bch_hash_info *src_hash,
178                        u64 dst_dir, struct bch_hash_info *dst_hash,
179                        const struct qstr *src_name, u64 *src_inum, u64 *src_offset,
180                        const struct qstr *dst_name, u64 *dst_inum, u64 *dst_offset,
181                        enum bch_rename_mode mode)
182 {
183         struct btree_iter *src_iter = NULL, *dst_iter = NULL;
184         struct bkey_s_c old_src, old_dst;
185         struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
186         struct bpos dst_pos =
187                 POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
188         int ret = 0;
189
190         *src_inum = *dst_inum = 0;
191
192         /*
193          * Lookup dst:
194          *
195          * Note that in BCH_RENAME mode, we're _not_ checking if
196          * the target already exists - we're relying on the VFS
197          * to do that check for us for correctness:
198          */
199         dst_iter = mode == BCH_RENAME
200                 ? bch2_hash_hole(trans, bch2_dirent_hash_desc,
201                                  dst_hash, dst_dir, dst_name)
202                 : bch2_hash_lookup(trans, bch2_dirent_hash_desc,
203                                    dst_hash, dst_dir, dst_name,
204                                    BTREE_ITER_INTENT);
205         ret = PTR_ERR_OR_ZERO(dst_iter);
206         if (ret)
207                 goto out;
208
209         old_dst = bch2_btree_iter_peek_slot(dst_iter);
210
211         if (mode != BCH_RENAME)
212                 *dst_inum = le64_to_cpu(bkey_s_c_to_dirent(old_dst).v->d_inum);
213         if (mode != BCH_RENAME_EXCHANGE)
214                 *src_offset = dst_iter->pos.offset;
215
216         /* Lookup src: */
217         src_iter = bch2_hash_lookup(trans, bch2_dirent_hash_desc,
218                                     src_hash, src_dir, src_name,
219                                     BTREE_ITER_INTENT);
220         ret = PTR_ERR_OR_ZERO(src_iter);
221         if (ret)
222                 goto out;
223
224         old_src = bch2_btree_iter_peek_slot(src_iter);
225         *src_inum = le64_to_cpu(bkey_s_c_to_dirent(old_src).v->d_inum);
226
227         /* Create new dst key: */
228         new_dst = dirent_create_key(trans, 0, dst_name, 0);
229         ret = PTR_ERR_OR_ZERO(new_dst);
230         if (ret)
231                 goto out;
232
233         dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
234         new_dst->k.p = dst_iter->pos;
235
236         /* Create new src key: */
237         if (mode == BCH_RENAME_EXCHANGE) {
238                 new_src = dirent_create_key(trans, 0, src_name, 0);
239                 ret = PTR_ERR_OR_ZERO(new_src);
240                 if (ret)
241                         goto out;
242
243                 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
244                 new_src->k.p = src_iter->pos;
245         } else {
246                 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
247                 ret = PTR_ERR_OR_ZERO(new_src);
248                 if (ret)
249                         goto out;
250
251                 bkey_init(&new_src->k);
252                 new_src->k.p = src_iter->pos;
253
254                 if (bkey_cmp(dst_pos, src_iter->pos) <= 0 &&
255                     bkey_cmp(src_iter->pos, dst_iter->pos) < 0) {
256                         /*
257                          * We have a hash collision for the new dst key,
258                          * and new_src - the key we're deleting - is between
259                          * new_dst's hashed slot and the slot we're going to be
260                          * inserting it into - oops.  This will break the hash
261                          * table if we don't deal with it:
262                          */
263                         if (mode == BCH_RENAME) {
264                                 /*
265                                  * If we're not overwriting, we can just insert
266                                  * new_dst at the src position:
267                                  */
268                                 new_dst->k.p = src_iter->pos;
269                                 bch2_trans_update(trans, src_iter,
270                                                   &new_dst->k_i, 0);
271                                 goto out_set_offset;
272                         } else {
273                                 /* If we're overwriting, we can't insert new_dst
274                                  * at a different slot because it has to
275                                  * overwrite old_dst - just make sure to use a
276                                  * whiteout when deleting src:
277                                  */
278                                 new_src->k.type = KEY_TYPE_hash_whiteout;
279                         }
280                 } else {
281                         /* Check if we need a whiteout to delete src: */
282                         ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
283                                                        src_hash, src_iter);
284                         if (ret < 0)
285                                 goto out;
286
287                         if (ret)
288                                 new_src->k.type = KEY_TYPE_hash_whiteout;
289                 }
290         }
291
292         bch2_trans_update(trans, src_iter, &new_src->k_i, 0);
293         bch2_trans_update(trans, dst_iter, &new_dst->k_i, 0);
294 out_set_offset:
295         if (mode == BCH_RENAME_EXCHANGE)
296                 *src_offset = new_src->k.p.offset;
297         *dst_offset = new_dst->k.p.offset;
298 out:
299         bch2_trans_iter_put(trans, src_iter);
300         bch2_trans_iter_put(trans, dst_iter);
301         return ret;
302 }
303
304 int bch2_dirent_delete_at(struct btree_trans *trans,
305                           const struct bch_hash_info *hash_info,
306                           struct btree_iter *iter)
307 {
308         return bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
309                                    hash_info, iter);
310 }
311
312 struct btree_iter *
313 __bch2_dirent_lookup_trans(struct btree_trans *trans, u64 dir_inum,
314                            const struct bch_hash_info *hash_info,
315                            const struct qstr *name, unsigned flags)
316 {
317         return bch2_hash_lookup(trans, bch2_dirent_hash_desc,
318                                 hash_info, dir_inum, name, flags);
319 }
320
321 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
322                        const struct bch_hash_info *hash_info,
323                        const struct qstr *name)
324 {
325         struct btree_trans trans;
326         struct btree_iter *iter;
327         struct bkey_s_c k;
328         u64 inum = 0;
329
330         bch2_trans_init(&trans, c, 0, 0);
331
332         iter = __bch2_dirent_lookup_trans(&trans, dir_inum,
333                                           hash_info, name, 0);
334         if (IS_ERR(iter)) {
335                 BUG_ON(PTR_ERR(iter) == -EINTR);
336                 goto out;
337         }
338
339         k = bch2_btree_iter_peek_slot(iter);
340         inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
341         bch2_trans_iter_put(&trans, iter);
342 out:
343         bch2_trans_exit(&trans);
344         return inum;
345 }
346
347 int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
348 {
349         struct btree_iter *iter;
350         struct bkey_s_c k;
351         int ret;
352
353         for_each_btree_key(trans, iter, BTREE_ID_dirents,
354                            POS(dir_inum, 0), 0, k, ret) {
355                 if (k.k->p.inode > dir_inum)
356                         break;
357
358                 if (k.k->type == KEY_TYPE_dirent) {
359                         ret = -ENOTEMPTY;
360                         break;
361                 }
362         }
363         bch2_trans_iter_put(trans, iter);
364
365         return ret;
366 }
367
368 int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
369 {
370         struct btree_trans trans;
371         struct btree_iter *iter;
372         struct bkey_s_c k;
373         struct bkey_s_c_dirent dirent;
374         int ret;
375
376         bch2_trans_init(&trans, c, 0, 0);
377
378         for_each_btree_key(&trans, iter, BTREE_ID_dirents,
379                            POS(inum, ctx->pos), 0, k, ret) {
380                 if (k.k->p.inode > inum)
381                         break;
382
383                 if (k.k->type != KEY_TYPE_dirent)
384                         continue;
385
386                 dirent = bkey_s_c_to_dirent(k);
387
388                 /*
389                  * XXX: dir_emit() can fault and block, while we're holding
390                  * locks
391                  */
392                 ctx->pos = dirent.k->p.offset;
393                 if (!dir_emit(ctx, dirent.v->d_name,
394                               bch2_dirent_name_bytes(dirent),
395                               le64_to_cpu(dirent.v->d_inum),
396                               dirent.v->d_type))
397                         break;
398                 ctx->pos = dirent.k->p.offset + 1;
399         }
400         bch2_trans_iter_put(&trans, iter);
401
402         ret = bch2_trans_exit(&trans) ?: ret;
403
404         return ret;
405 }