]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/dirent.c
Update bcachefs sources to 3f3f969859 bcachefs: Fix some compiler warnings
[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 };
187         struct btree_iter dst_iter = { NULL };
188         struct bkey_s_c old_src, old_dst;
189         struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
190         struct bpos dst_pos =
191                 POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
192         int ret = 0;
193
194         *src_inum = *dst_inum = 0;
195
196         /*
197          * Lookup dst:
198          *
199          * Note that in BCH_RENAME mode, we're _not_ checking if
200          * the target already exists - we're relying on the VFS
201          * to do that check for us for correctness:
202          */
203         ret = mode == BCH_RENAME
204                 ? bch2_hash_hole(trans, &dst_iter, bch2_dirent_hash_desc,
205                                  dst_hash, dst_dir, dst_name)
206                 : bch2_hash_lookup(trans, &dst_iter, bch2_dirent_hash_desc,
207                                    dst_hash, dst_dir, dst_name,
208                                    BTREE_ITER_INTENT);
209         if (ret)
210                 goto out;
211
212         old_dst = bch2_btree_iter_peek_slot(&dst_iter);
213         ret = bkey_err(old_dst);
214         if (ret)
215                 goto out;
216
217         if (mode != BCH_RENAME)
218                 *dst_inum = le64_to_cpu(bkey_s_c_to_dirent(old_dst).v->d_inum);
219         if (mode != BCH_RENAME_EXCHANGE)
220                 *src_offset = dst_iter.pos.offset;
221
222         /* Lookup src: */
223         ret = bch2_hash_lookup(trans, &src_iter, bch2_dirent_hash_desc,
224                                src_hash, src_dir, src_name,
225                                BTREE_ITER_INTENT);
226         if (ret)
227                 goto out;
228
229         old_src = bch2_btree_iter_peek_slot(&src_iter);
230         ret = bkey_err(old_src);
231         if (ret)
232                 goto out;
233
234         *src_inum = le64_to_cpu(bkey_s_c_to_dirent(old_src).v->d_inum);
235
236         /* Create new dst key: */
237         new_dst = dirent_create_key(trans, 0, dst_name, 0);
238         ret = PTR_ERR_OR_ZERO(new_dst);
239         if (ret)
240                 goto out;
241
242         dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
243         new_dst->k.p = dst_iter.pos;
244
245         /* Create new src key: */
246         if (mode == BCH_RENAME_EXCHANGE) {
247                 new_src = dirent_create_key(trans, 0, src_name, 0);
248                 ret = PTR_ERR_OR_ZERO(new_src);
249                 if (ret)
250                         goto out;
251
252                 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
253                 new_src->k.p = src_iter.pos;
254         } else {
255                 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
256                 ret = PTR_ERR_OR_ZERO(new_src);
257                 if (ret)
258                         goto out;
259
260                 bkey_init(&new_src->k);
261                 new_src->k.p = src_iter.pos;
262
263                 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
264                     bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
265                         /*
266                          * We have a hash collision for the new dst key,
267                          * and new_src - the key we're deleting - is between
268                          * new_dst's hashed slot and the slot we're going to be
269                          * inserting it into - oops.  This will break the hash
270                          * table if we don't deal with it:
271                          */
272                         if (mode == BCH_RENAME) {
273                                 /*
274                                  * If we're not overwriting, we can just insert
275                                  * new_dst at the src position:
276                                  */
277                                 new_dst->k.p = src_iter.pos;
278                                 bch2_trans_update(trans, &src_iter,
279                                                   &new_dst->k_i, 0);
280                                 goto out_set_offset;
281                         } else {
282                                 /* If we're overwriting, we can't insert new_dst
283                                  * at a different slot because it has to
284                                  * overwrite old_dst - just make sure to use a
285                                  * whiteout when deleting src:
286                                  */
287                                 new_src->k.type = KEY_TYPE_hash_whiteout;
288                         }
289                 } else {
290                         /* Check if we need a whiteout to delete src: */
291                         ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
292                                                        src_hash, &src_iter);
293                         if (ret < 0)
294                                 goto out;
295
296                         if (ret)
297                                 new_src->k.type = KEY_TYPE_hash_whiteout;
298                 }
299         }
300
301         bch2_trans_update(trans, &src_iter, &new_src->k_i, 0);
302         bch2_trans_update(trans, &dst_iter, &new_dst->k_i, 0);
303 out_set_offset:
304         if (mode == BCH_RENAME_EXCHANGE)
305                 *src_offset = new_src->k.p.offset;
306         *dst_offset = new_dst->k.p.offset;
307 out:
308         bch2_trans_iter_exit(trans, &src_iter);
309         bch2_trans_iter_exit(trans, &dst_iter);
310         return ret;
311 }
312
313 int bch2_dirent_delete_at(struct btree_trans *trans,
314                           const struct bch_hash_info *hash_info,
315                           struct btree_iter *iter)
316 {
317         return bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
318                                    hash_info, iter);
319 }
320
321 int __bch2_dirent_lookup_trans(struct btree_trans *trans,
322                                struct btree_iter *iter,
323                                u64 dir_inum,
324                                const struct bch_hash_info *hash_info,
325                                const struct qstr *name, unsigned flags)
326 {
327         return bch2_hash_lookup(trans, iter, bch2_dirent_hash_desc,
328                                 hash_info, dir_inum, name, flags);
329 }
330
331 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
332                        const struct bch_hash_info *hash_info,
333                        const struct qstr *name)
334 {
335         struct btree_trans trans;
336         struct btree_iter iter;
337         struct bkey_s_c k;
338         u64 inum = 0;
339         int ret = 0;
340
341         bch2_trans_init(&trans, c, 0, 0);
342
343         ret = __bch2_dirent_lookup_trans(&trans, &iter, dir_inum,
344                                          hash_info, name, 0);
345         if (ret)
346                 goto out;
347
348         k = bch2_btree_iter_peek_slot(&iter);
349         ret = bkey_err(k);
350         if (ret)
351                 goto out;
352
353         inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
354         bch2_trans_iter_exit(&trans, &iter);
355 out:
356         BUG_ON(ret == -EINTR);
357         bch2_trans_exit(&trans);
358         return inum;
359 }
360
361 int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
362 {
363         struct btree_iter iter;
364         struct bkey_s_c k;
365         int ret;
366
367         for_each_btree_key(trans, iter, BTREE_ID_dirents,
368                            POS(dir_inum, 0), 0, k, ret) {
369                 if (k.k->p.inode > dir_inum)
370                         break;
371
372                 if (k.k->type == KEY_TYPE_dirent) {
373                         ret = -ENOTEMPTY;
374                         break;
375                 }
376         }
377         bch2_trans_iter_exit(trans, &iter);
378
379         return ret;
380 }
381
382 int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
383 {
384         struct btree_trans trans;
385         struct btree_iter iter;
386         struct bkey_s_c k;
387         struct bkey_s_c_dirent dirent;
388         int ret;
389
390         bch2_trans_init(&trans, c, 0, 0);
391
392         for_each_btree_key(&trans, iter, BTREE_ID_dirents,
393                            POS(inum, ctx->pos), 0, k, ret) {
394                 if (k.k->p.inode > inum)
395                         break;
396
397                 if (k.k->type != KEY_TYPE_dirent)
398                         continue;
399
400                 dirent = bkey_s_c_to_dirent(k);
401
402                 /*
403                  * XXX: dir_emit() can fault and block, while we're holding
404                  * locks
405                  */
406                 ctx->pos = dirent.k->p.offset;
407                 if (!dir_emit(ctx, dirent.v->d_name,
408                               bch2_dirent_name_bytes(dirent),
409                               le64_to_cpu(dirent.v->d_inum),
410                               dirent.v->d_type))
411                         break;
412                 ctx->pos = dirent.k->p.offset + 1;
413         }
414         bch2_trans_iter_exit(&trans, &iter);
415
416         ret = bch2_trans_exit(&trans) ?: ret;
417
418         return ret;
419 }