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