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