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