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