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