]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/dirent.c
Update bcachefs sources to 14ce2a2031 bcachefs: fixes for building in userspace
[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_INTENT);
217         bch2_btree_iter_init(&dst_iter, c, BTREE_ID_DIRENTS, dst_pos,
218                              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, 0);
222         bch2_btree_iter_link(&src_iter, &whiteout_iter);
223
224         if (mode == BCH_RENAME_EXCHANGE) {
225                 new_src = dirent_create_key(0, src_name, 0);
226                 if (!new_src)
227                         goto err;
228         } else {
229                 new_src = (void *) &delete;
230         }
231
232         new_dst = dirent_create_key(0, dst_name, 0);
233         if (!new_dst)
234                 goto err;
235 retry:
236         /*
237          * Note that on -EINTR/dropped locks we're not restarting the lookup
238          * from the original hashed position (like we do when creating dirents,
239          * in bch_hash_set) -  we never move existing dirents to different slot:
240          */
241         old_src = bch2_hash_lookup_at(bch2_dirent_hash_desc,
242                                      &src_dir->ei_str_hash,
243                                      &src_iter, src_name);
244         if ((ret = btree_iter_err(old_src)))
245                 goto err;
246
247         ret = bch2_hash_needs_whiteout(bch2_dirent_hash_desc,
248                                 &src_dir->ei_str_hash,
249                                 &whiteout_iter, &src_iter);
250         if (ret < 0)
251                 goto err;
252         need_whiteout = ret;
253
254         /*
255          * Note that in BCH_RENAME mode, we're _not_ checking if
256          * the target already exists - we're relying on the VFS
257          * to do that check for us for correctness:
258          */
259         old_dst = mode == BCH_RENAME
260                 ? bch2_hash_hole_at(bch2_dirent_hash_desc, &dst_iter)
261                 : bch2_hash_lookup_at(bch2_dirent_hash_desc,
262                                      &dst_dir->ei_str_hash,
263                                      &dst_iter, dst_name);
264         if ((ret = btree_iter_err(old_dst)))
265                 goto err;
266
267         switch (mode) {
268         case BCH_RENAME:
269                 bkey_init(&new_src->k);
270                 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
271
272                 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
273                     bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
274                         /*
275                          * If we couldn't insert new_dst at its hashed
276                          * position (dst_pos) due to a hash collision,
277                          * and we're going to be deleting in
278                          * between the hashed position and first empty
279                          * slot we found - just overwrite the pos we
280                          * were going to delete:
281                          *
282                          * Note: this is a correctness issue, in this
283                          * situation bch2_hash_needs_whiteout() could
284                          * return false when the whiteout would have
285                          * been needed if we inserted at the pos
286                          * __dirent_find_hole() found
287                          */
288                         new_dst->k.p = src_iter.pos;
289                         ret = bch2_btree_insert_at(c, NULL, NULL,
290                                         journal_seq,
291                                         BTREE_INSERT_ATOMIC,
292                                         BTREE_INSERT_ENTRY(&src_iter,
293                                                            &new_dst->k_i));
294                         goto err;
295                 }
296
297                 if (need_whiteout)
298                         new_src->k.type = BCH_DIRENT_WHITEOUT;
299                 break;
300         case BCH_RENAME_OVERWRITE:
301                 bkey_init(&new_src->k);
302                 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
303
304                 if (bkey_cmp(dst_pos, src_iter.pos) <= 0 &&
305                     bkey_cmp(src_iter.pos, dst_iter.pos) < 0) {
306                         /*
307                          * Same case described above -
308                          * bch_hash_needs_whiteout could spuriously
309                          * return false, but we have to insert at
310                          * dst_iter.pos because we're overwriting
311                          * another dirent:
312                          */
313                         new_src->k.type = BCH_DIRENT_WHITEOUT;
314                 } else if (need_whiteout)
315                         new_src->k.type = BCH_DIRENT_WHITEOUT;
316                 break;
317         case BCH_RENAME_EXCHANGE:
318                 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
319                 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
320                 break;
321         }
322
323         new_src->k.p = src_iter.pos;
324         new_dst->k.p = dst_iter.pos;
325         ret = bch2_btree_insert_at(c, NULL, NULL, journal_seq,
326                         BTREE_INSERT_ATOMIC,
327                         BTREE_INSERT_ENTRY(&src_iter, &new_src->k_i),
328                         BTREE_INSERT_ENTRY(&dst_iter, &new_dst->k_i));
329 err:
330         if (ret == -EINTR)
331                 goto retry;
332
333         bch2_btree_iter_unlock(&whiteout_iter);
334         bch2_btree_iter_unlock(&dst_iter);
335         bch2_btree_iter_unlock(&src_iter);
336
337         if (new_src != (void *) &delete)
338                 kfree(new_src);
339         kfree(new_dst);
340         return ret;
341 }
342
343 int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
344                        const struct bch_hash_info *hash_info,
345                        const struct qstr *name,
346                        u64 *journal_seq)
347 {
348         return bch2_hash_delete(bch2_dirent_hash_desc, hash_info,
349                                c, dir_inum, journal_seq, name);
350 }
351
352 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
353                        const struct bch_hash_info *hash_info,
354                        const struct qstr *name)
355 {
356         struct btree_iter iter;
357         struct bkey_s_c k;
358         u64 inum;
359
360         k = bch2_hash_lookup(bch2_dirent_hash_desc, hash_info, c,
361                             dir_inum, &iter, name);
362         if (IS_ERR(k.k)) {
363                 bch2_btree_iter_unlock(&iter);
364                 return 0;
365         }
366
367         inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
368         bch2_btree_iter_unlock(&iter);
369
370         return inum;
371 }
372
373 int bch2_empty_dir(struct bch_fs *c, u64 dir_inum)
374 {
375         struct btree_iter iter;
376         struct bkey_s_c k;
377         int ret = 0;
378
379         for_each_btree_key(&iter, c, BTREE_ID_DIRENTS, POS(dir_inum, 0), 0, k) {
380                 if (k.k->p.inode > dir_inum)
381                         break;
382
383                 if (k.k->type == BCH_DIRENT) {
384                         ret = -ENOTEMPTY;
385                         break;
386                 }
387         }
388         bch2_btree_iter_unlock(&iter);
389
390         return ret;
391 }
392
393 int bch2_readdir(struct bch_fs *c, struct file *file,
394                  struct dir_context *ctx)
395 {
396         struct bch_inode_info *inode = file_bch_inode(file);
397         struct btree_iter iter;
398         struct bkey_s_c k;
399         struct bkey_s_c_dirent dirent;
400         unsigned len;
401
402         if (!dir_emit_dots(file, ctx))
403                 return 0;
404
405         for_each_btree_key(&iter, c, BTREE_ID_DIRENTS,
406                            POS(inode->v.i_ino, ctx->pos), 0, k) {
407                 if (k.k->type != BCH_DIRENT)
408                         continue;
409
410                 dirent = bkey_s_c_to_dirent(k);
411
412                 if (bkey_cmp(k.k->p, POS(inode->v.i_ino, ctx->pos)) < 0)
413                         continue;
414
415                 if (k.k->p.inode > inode->v.i_ino)
416                         break;
417
418                 len = bch2_dirent_name_bytes(dirent);
419
420                 /*
421                  * XXX: dir_emit() can fault and block, while we're holding
422                  * locks
423                  */
424                 if (!dir_emit(ctx, dirent.v->d_name, len,
425                               le64_to_cpu(dirent.v->d_inum),
426                               dirent.v->d_type))
427                         break;
428
429                 ctx->pos = k.k->p.offset + 1;
430         }
431         bch2_btree_iter_unlock(&iter);
432
433         return 0;
434 }