1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "bkey_buf.h"
5 #include "bkey_methods.h"
6 #include "btree_update.h"
7 #include "extents.h"
8 #include "dirent.h"
9 #include "fs.h"
10 #include "keylist.h"
11 #include "str_hash.h"
12 #include "subvolume.h"
13
14 #include <linux/dcache.h>
15
bch2_casefold(struct btree_trans * trans,const struct bch_hash_info * info,const struct qstr * str,struct qstr * out_cf)16 int bch2_casefold(struct btree_trans *trans, const struct bch_hash_info *info,
17 const struct qstr *str, struct qstr *out_cf)
18 {
19 *out_cf = (struct qstr) QSTR_INIT(NULL, 0);
20
21 #ifdef CONFIG_UNICODE
22 unsigned char *buf = bch2_trans_kmalloc(trans, BCH_NAME_MAX + 1);
23 int ret = PTR_ERR_OR_ZERO(buf);
24 if (ret)
25 return ret;
26
27 ret = utf8_casefold(info->cf_encoding, str, buf, BCH_NAME_MAX + 1);
28 if (ret <= 0)
29 return ret;
30
31 *out_cf = (struct qstr) QSTR_INIT(buf, ret);
32 return 0;
33 #else
34 return -EOPNOTSUPP;
35 #endif
36 }
37
bch2_dirent_name_bytes(struct bkey_s_c_dirent d)38 static unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
39 {
40 if (bkey_val_bytes(d.k) < offsetof(struct bch_dirent, d_name))
41 return 0;
42
43 unsigned bkey_u64s = bkey_val_u64s(d.k);
44 unsigned bkey_bytes = bkey_u64s * sizeof(u64);
45 u64 last_u64 = ((u64*)d.v)[bkey_u64s - 1];
46 #if CPU_BIG_ENDIAN
47 unsigned trailing_nuls = last_u64 ? __builtin_ctzll(last_u64) / 8 : 64 / 8;
48 #else
49 unsigned trailing_nuls = last_u64 ? __builtin_clzll(last_u64) / 8 : 64 / 8;
50 #endif
51
52 return bkey_bytes -
53 (d.v->d_casefold
54 ? offsetof(struct bch_dirent, d_cf_name_block.d_names)
55 : offsetof(struct bch_dirent, d_name)) -
56 trailing_nuls;
57 }
58
bch2_dirent_get_name(struct bkey_s_c_dirent d)59 struct qstr bch2_dirent_get_name(struct bkey_s_c_dirent d)
60 {
61 if (d.v->d_casefold) {
62 unsigned name_len = le16_to_cpu(d.v->d_cf_name_block.d_name_len);
63 return (struct qstr) QSTR_INIT(&d.v->d_cf_name_block.d_names[0], name_len);
64 } else {
65 return (struct qstr) QSTR_INIT(d.v->d_name, bch2_dirent_name_bytes(d));
66 }
67 }
68
bch2_dirent_get_casefold_name(struct bkey_s_c_dirent d)69 static struct qstr bch2_dirent_get_casefold_name(struct bkey_s_c_dirent d)
70 {
71 if (d.v->d_casefold) {
72 unsigned name_len = le16_to_cpu(d.v->d_cf_name_block.d_name_len);
73 unsigned cf_name_len = le16_to_cpu(d.v->d_cf_name_block.d_cf_name_len);
74 return (struct qstr) QSTR_INIT(&d.v->d_cf_name_block.d_names[name_len], cf_name_len);
75 } else {
76 return (struct qstr) QSTR_INIT(NULL, 0);
77 }
78 }
79
bch2_dirent_get_lookup_name(struct bkey_s_c_dirent d)80 static inline struct qstr bch2_dirent_get_lookup_name(struct bkey_s_c_dirent d)
81 {
82 return d.v->d_casefold
83 ? bch2_dirent_get_casefold_name(d)
84 : bch2_dirent_get_name(d);
85 }
86
bch2_dirent_hash(const struct bch_hash_info * info,const struct qstr * name)87 static u64 bch2_dirent_hash(const struct bch_hash_info *info,
88 const struct qstr *name)
89 {
90 struct bch_str_hash_ctx ctx;
91
92 bch2_str_hash_init(&ctx, info);
93 bch2_str_hash_update(&ctx, info, name->name, name->len);
94
95 /* [0,2) reserved for dots */
96 return max_t(u64, bch2_str_hash_end(&ctx, info), 2);
97 }
98
dirent_hash_key(const struct bch_hash_info * info,const void * key)99 static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
100 {
101 return bch2_dirent_hash(info, key);
102 }
103
dirent_hash_bkey(const struct bch_hash_info * info,struct bkey_s_c k)104 static u64 dirent_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
105 {
106 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
107 struct qstr name = bch2_dirent_get_lookup_name(d);
108
109 return bch2_dirent_hash(info, &name);
110 }
111
dirent_cmp_key(struct bkey_s_c _l,const void * _r)112 static bool dirent_cmp_key(struct bkey_s_c _l, const void *_r)
113 {
114 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
115 const struct qstr l_name = bch2_dirent_get_lookup_name(l);
116 const struct qstr *r_name = _r;
117
118 return !qstr_eq(l_name, *r_name);
119 }
120
dirent_cmp_bkey(struct bkey_s_c _l,struct bkey_s_c _r)121 static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
122 {
123 struct bkey_s_c_dirent l = bkey_s_c_to_dirent(_l);
124 struct bkey_s_c_dirent r = bkey_s_c_to_dirent(_r);
125 const struct qstr l_name = bch2_dirent_get_lookup_name(l);
126 const struct qstr r_name = bch2_dirent_get_lookup_name(r);
127
128 return !qstr_eq(l_name, r_name);
129 }
130
dirent_is_visible(subvol_inum inum,struct bkey_s_c k)131 static bool dirent_is_visible(subvol_inum inum, struct bkey_s_c k)
132 {
133 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
134
135 if (d.v->d_type == DT_SUBVOL)
136 return le32_to_cpu(d.v->d_parent_subvol) == inum.subvol;
137 return true;
138 }
139
140 const struct bch_hash_desc bch2_dirent_hash_desc = {
141 .btree_id = BTREE_ID_dirents,
142 .key_type = KEY_TYPE_dirent,
143 .hash_key = dirent_hash_key,
144 .hash_bkey = dirent_hash_bkey,
145 .cmp_key = dirent_cmp_key,
146 .cmp_bkey = dirent_cmp_bkey,
147 .is_visible = dirent_is_visible,
148 };
149
bch2_dirent_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)150 int bch2_dirent_validate(struct bch_fs *c, struct bkey_s_c k,
151 struct bkey_validate_context from)
152 {
153 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
154 unsigned name_block_len = bch2_dirent_name_bytes(d);
155 struct qstr d_name = bch2_dirent_get_name(d);
156 struct qstr d_cf_name = bch2_dirent_get_casefold_name(d);
157 int ret = 0;
158
159 bkey_fsck_err_on(!d_name.len,
160 c, dirent_empty_name,
161 "empty name");
162
163 bkey_fsck_err_on(d_name.len + d_cf_name.len > name_block_len,
164 c, dirent_val_too_big,
165 "dirent names exceed bkey size (%d + %d > %d)",
166 d_name.len, d_cf_name.len, name_block_len);
167
168 /*
169 * Check new keys don't exceed the max length
170 * (older keys may be larger.)
171 */
172 bkey_fsck_err_on((from.flags & BCH_VALIDATE_commit) && d_name.len > BCH_NAME_MAX,
173 c, dirent_name_too_long,
174 "dirent name too big (%u > %u)",
175 d_name.len, BCH_NAME_MAX);
176
177 bkey_fsck_err_on(d_name.len != strnlen(d_name.name, d_name.len),
178 c, dirent_name_embedded_nul,
179 "dirent has stray data after name's NUL");
180
181 bkey_fsck_err_on((d_name.len == 1 && !memcmp(d_name.name, ".", 1)) ||
182 (d_name.len == 2 && !memcmp(d_name.name, "..", 2)),
183 c, dirent_name_dot_or_dotdot,
184 "invalid name");
185
186 bkey_fsck_err_on(memchr(d_name.name, '/', d_name.len),
187 c, dirent_name_has_slash,
188 "name with /");
189
190 bkey_fsck_err_on(d.v->d_type != DT_SUBVOL &&
191 le64_to_cpu(d.v->d_inum) == d.k->p.inode,
192 c, dirent_to_itself,
193 "dirent points to own directory");
194
195 if (d.v->d_casefold) {
196 bkey_fsck_err_on(from.from == BKEY_VALIDATE_commit &&
197 d_cf_name.len > BCH_NAME_MAX,
198 c, dirent_cf_name_too_big,
199 "dirent w/ cf name too big (%u > %u)",
200 d_cf_name.len, BCH_NAME_MAX);
201
202 bkey_fsck_err_on(d_cf_name.len != strnlen(d_cf_name.name, d_cf_name.len),
203 c, dirent_stray_data_after_cf_name,
204 "dirent has stray data after cf name's NUL");
205 }
206 fsck_err:
207 return ret;
208 }
209
bch2_dirent_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)210 void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
211 {
212 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
213 struct qstr d_name = bch2_dirent_get_name(d);
214
215 prt_printf(out, "%.*s -> ", d_name.len, d_name.name);
216
217 if (d.v->d_type != DT_SUBVOL)
218 prt_printf(out, "%llu", le64_to_cpu(d.v->d_inum));
219 else
220 prt_printf(out, "%u -> %u",
221 le32_to_cpu(d.v->d_parent_subvol),
222 le32_to_cpu(d.v->d_child_subvol));
223
224 prt_printf(out, " type %s", bch2_d_type_str(d.v->d_type));
225 }
226
dirent_alloc_key(struct btree_trans * trans,subvol_inum dir,u8 type,int name_len,int cf_name_len,u64 dst)227 static struct bkey_i_dirent *dirent_alloc_key(struct btree_trans *trans,
228 subvol_inum dir,
229 u8 type,
230 int name_len, int cf_name_len,
231 u64 dst)
232 {
233 struct bkey_i_dirent *dirent;
234 unsigned u64s = BKEY_U64s + dirent_val_u64s(name_len, cf_name_len);
235
236 BUG_ON(u64s > U8_MAX);
237
238 dirent = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
239 if (IS_ERR(dirent))
240 return dirent;
241
242 bkey_dirent_init(&dirent->k_i);
243 dirent->k.u64s = u64s;
244
245 if (type != DT_SUBVOL) {
246 dirent->v.d_inum = cpu_to_le64(dst);
247 } else {
248 dirent->v.d_parent_subvol = cpu_to_le32(dir.subvol);
249 dirent->v.d_child_subvol = cpu_to_le32(dst);
250 }
251
252 dirent->v.d_type = type;
253 dirent->v.d_unused = 0;
254 dirent->v.d_casefold = cf_name_len ? 1 : 0;
255
256 return dirent;
257 }
258
dirent_init_regular_name(struct bkey_i_dirent * dirent,const struct qstr * name)259 static void dirent_init_regular_name(struct bkey_i_dirent *dirent,
260 const struct qstr *name)
261 {
262 EBUG_ON(dirent->v.d_casefold);
263
264 memcpy(&dirent->v.d_name[0], name->name, name->len);
265 memset(&dirent->v.d_name[name->len], 0,
266 bkey_val_bytes(&dirent->k) -
267 offsetof(struct bch_dirent, d_name) -
268 name->len);
269 }
270
dirent_init_casefolded_name(struct bkey_i_dirent * dirent,const struct qstr * name,const struct qstr * cf_name)271 static void dirent_init_casefolded_name(struct bkey_i_dirent *dirent,
272 const struct qstr *name,
273 const struct qstr *cf_name)
274 {
275 EBUG_ON(!dirent->v.d_casefold);
276 EBUG_ON(!cf_name->len);
277
278 dirent->v.d_cf_name_block.d_name_len = cpu_to_le16(name->len);
279 dirent->v.d_cf_name_block.d_cf_name_len = cpu_to_le16(cf_name->len);
280 memcpy(&dirent->v.d_cf_name_block.d_names[0], name->name, name->len);
281 memcpy(&dirent->v.d_cf_name_block.d_names[name->len], cf_name->name, cf_name->len);
282 memset(&dirent->v.d_cf_name_block.d_names[name->len + cf_name->len], 0,
283 bkey_val_bytes(&dirent->k) -
284 offsetof(struct bch_dirent, d_cf_name_block.d_names) -
285 name->len + cf_name->len);
286
287 EBUG_ON(bch2_dirent_get_casefold_name(dirent_i_to_s_c(dirent)).len != cf_name->len);
288 }
289
dirent_create_key(struct btree_trans * trans,const struct bch_hash_info * hash_info,subvol_inum dir,u8 type,const struct qstr * name,const struct qstr * cf_name,u64 dst)290 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
291 const struct bch_hash_info *hash_info,
292 subvol_inum dir,
293 u8 type,
294 const struct qstr *name,
295 const struct qstr *cf_name,
296 u64 dst)
297 {
298 struct bkey_i_dirent *dirent;
299 struct qstr _cf_name;
300
301 if (name->len > BCH_NAME_MAX)
302 return ERR_PTR(-ENAMETOOLONG);
303
304 if (hash_info->cf_encoding && !cf_name) {
305 int ret = bch2_casefold(trans, hash_info, name, &_cf_name);
306 if (ret)
307 return ERR_PTR(ret);
308
309 cf_name = &_cf_name;
310 }
311
312 dirent = dirent_alloc_key(trans, dir, type, name->len, cf_name ? cf_name->len : 0, dst);
313 if (IS_ERR(dirent))
314 return dirent;
315
316 if (cf_name)
317 dirent_init_casefolded_name(dirent, name, cf_name);
318 else
319 dirent_init_regular_name(dirent, name);
320
321 EBUG_ON(bch2_dirent_get_name(dirent_i_to_s_c(dirent)).len != name->len);
322
323 return dirent;
324 }
325
bch2_dirent_create_snapshot(struct btree_trans * trans,u32 dir_subvol,u64 dir,u32 snapshot,const struct bch_hash_info * hash_info,u8 type,const struct qstr * name,u64 dst_inum,u64 * dir_offset,enum btree_iter_update_trigger_flags flags)326 int bch2_dirent_create_snapshot(struct btree_trans *trans,
327 u32 dir_subvol, u64 dir, u32 snapshot,
328 const struct bch_hash_info *hash_info,
329 u8 type, const struct qstr *name, u64 dst_inum,
330 u64 *dir_offset,
331 enum btree_iter_update_trigger_flags flags)
332 {
333 subvol_inum dir_inum = { .subvol = dir_subvol, .inum = dir };
334 struct bkey_i_dirent *dirent;
335 int ret;
336
337 dirent = dirent_create_key(trans, hash_info, dir_inum, type, name, NULL, dst_inum);
338 ret = PTR_ERR_OR_ZERO(dirent);
339 if (ret)
340 return ret;
341
342 dirent->k.p.inode = dir;
343 dirent->k.p.snapshot = snapshot;
344
345 ret = bch2_hash_set_in_snapshot(trans, bch2_dirent_hash_desc, hash_info,
346 dir_inum, snapshot, &dirent->k_i, flags);
347 *dir_offset = dirent->k.p.offset;
348
349 return ret;
350 }
351
bch2_dirent_create(struct btree_trans * trans,subvol_inum dir,const struct bch_hash_info * hash_info,u8 type,const struct qstr * name,u64 dst_inum,u64 * dir_offset,enum btree_iter_update_trigger_flags flags)352 int bch2_dirent_create(struct btree_trans *trans, subvol_inum dir,
353 const struct bch_hash_info *hash_info,
354 u8 type, const struct qstr *name, u64 dst_inum,
355 u64 *dir_offset,
356 enum btree_iter_update_trigger_flags flags)
357 {
358 struct bkey_i_dirent *dirent;
359 int ret;
360
361 dirent = dirent_create_key(trans, hash_info, dir, type, name, NULL, dst_inum);
362 ret = PTR_ERR_OR_ZERO(dirent);
363 if (ret)
364 return ret;
365
366 ret = bch2_hash_set(trans, bch2_dirent_hash_desc, hash_info,
367 dir, &dirent->k_i, flags);
368 *dir_offset = dirent->k.p.offset;
369
370 return ret;
371 }
372
bch2_dirent_read_target(struct btree_trans * trans,subvol_inum dir,struct bkey_s_c_dirent d,subvol_inum * target)373 int bch2_dirent_read_target(struct btree_trans *trans, subvol_inum dir,
374 struct bkey_s_c_dirent d, subvol_inum *target)
375 {
376 struct bch_subvolume s;
377 int ret = 0;
378
379 if (d.v->d_type == DT_SUBVOL &&
380 le32_to_cpu(d.v->d_parent_subvol) != dir.subvol)
381 return 1;
382
383 if (likely(d.v->d_type != DT_SUBVOL)) {
384 target->subvol = dir.subvol;
385 target->inum = le64_to_cpu(d.v->d_inum);
386 } else {
387 target->subvol = le32_to_cpu(d.v->d_child_subvol);
388
389 ret = bch2_subvolume_get(trans, target->subvol, true, &s);
390
391 target->inum = le64_to_cpu(s.inode);
392 }
393
394 return ret;
395 }
396
bch2_dirent_rename(struct btree_trans * trans,subvol_inum src_dir,struct bch_hash_info * src_hash,u64 * src_dir_i_size,subvol_inum dst_dir,struct bch_hash_info * dst_hash,u64 * dst_dir_i_size,const struct qstr * src_name,subvol_inum * src_inum,u64 * src_offset,const struct qstr * dst_name,subvol_inum * dst_inum,u64 * dst_offset,enum bch_rename_mode mode)397 int bch2_dirent_rename(struct btree_trans *trans,
398 subvol_inum src_dir, struct bch_hash_info *src_hash, u64 *src_dir_i_size,
399 subvol_inum dst_dir, struct bch_hash_info *dst_hash, u64 *dst_dir_i_size,
400 const struct qstr *src_name, subvol_inum *src_inum, u64 *src_offset,
401 const struct qstr *dst_name, subvol_inum *dst_inum, u64 *dst_offset,
402 enum bch_rename_mode mode)
403 {
404 struct qstr src_name_lookup, dst_name_lookup;
405 struct btree_iter src_iter = {};
406 struct btree_iter dst_iter = {};
407 struct bkey_s_c old_src, old_dst = bkey_s_c_null;
408 struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
409 struct bpos dst_pos =
410 POS(dst_dir.inum, bch2_dirent_hash(dst_hash, dst_name));
411 unsigned src_update_flags = 0;
412 bool delete_src, delete_dst;
413 int ret = 0;
414
415 memset(src_inum, 0, sizeof(*src_inum));
416 memset(dst_inum, 0, sizeof(*dst_inum));
417
418 /* Lookup src: */
419 ret = bch2_maybe_casefold(trans, src_hash, src_name, &src_name_lookup);
420 if (ret)
421 goto out;
422 old_src = bch2_hash_lookup(trans, &src_iter, bch2_dirent_hash_desc,
423 src_hash, src_dir, &src_name_lookup,
424 BTREE_ITER_intent);
425 ret = bkey_err(old_src);
426 if (ret)
427 goto out;
428
429 ret = bch2_dirent_read_target(trans, src_dir,
430 bkey_s_c_to_dirent(old_src), src_inum);
431 if (ret)
432 goto out;
433
434 /* Lookup dst: */
435 ret = bch2_maybe_casefold(trans, dst_hash, dst_name, &dst_name_lookup);
436 if (ret)
437 goto out;
438 if (mode == BCH_RENAME) {
439 /*
440 * Note that we're _not_ checking if the target already exists -
441 * we're relying on the VFS to do that check for us for
442 * correctness:
443 */
444 ret = bch2_hash_hole(trans, &dst_iter, bch2_dirent_hash_desc,
445 dst_hash, dst_dir, &dst_name_lookup);
446 if (ret)
447 goto out;
448 } else {
449 old_dst = bch2_hash_lookup(trans, &dst_iter, bch2_dirent_hash_desc,
450 dst_hash, dst_dir, &dst_name_lookup,
451 BTREE_ITER_intent);
452 ret = bkey_err(old_dst);
453 if (ret)
454 goto out;
455
456 ret = bch2_dirent_read_target(trans, dst_dir,
457 bkey_s_c_to_dirent(old_dst), dst_inum);
458 if (ret)
459 goto out;
460 }
461
462 if (mode != BCH_RENAME_EXCHANGE)
463 *src_offset = dst_iter.pos.offset;
464
465 /* Create new dst key: */
466 new_dst = dirent_create_key(trans, dst_hash, dst_dir, 0, dst_name,
467 dst_hash->cf_encoding ? &dst_name_lookup : NULL, 0);
468 ret = PTR_ERR_OR_ZERO(new_dst);
469 if (ret)
470 goto out;
471
472 dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
473 new_dst->k.p = dst_iter.pos;
474
475 /* Create new src key: */
476 if (mode == BCH_RENAME_EXCHANGE) {
477 new_src = dirent_create_key(trans, src_hash, src_dir, 0, src_name,
478 src_hash->cf_encoding ? &src_name_lookup : NULL, 0);
479 ret = PTR_ERR_OR_ZERO(new_src);
480 if (ret)
481 goto out;
482
483 dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
484 new_src->k.p = src_iter.pos;
485 } else {
486 new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
487 ret = PTR_ERR_OR_ZERO(new_src);
488 if (ret)
489 goto out;
490
491 bkey_init(&new_src->k);
492 new_src->k.p = src_iter.pos;
493
494 if (bkey_le(dst_pos, src_iter.pos) &&
495 bkey_lt(src_iter.pos, dst_iter.pos)) {
496 /*
497 * We have a hash collision for the new dst key,
498 * and new_src - the key we're deleting - is between
499 * new_dst's hashed slot and the slot we're going to be
500 * inserting it into - oops. This will break the hash
501 * table if we don't deal with it:
502 */
503 if (mode == BCH_RENAME) {
504 /*
505 * If we're not overwriting, we can just insert
506 * new_dst at the src position:
507 */
508 new_src = new_dst;
509 new_src->k.p = src_iter.pos;
510 goto out_set_src;
511 } else {
512 /* If we're overwriting, we can't insert new_dst
513 * at a different slot because it has to
514 * overwrite old_dst - just make sure to use a
515 * whiteout when deleting src:
516 */
517 new_src->k.type = KEY_TYPE_hash_whiteout;
518 }
519 } else {
520 /* Check if we need a whiteout to delete src: */
521 ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
522 src_hash, &src_iter);
523 if (ret < 0)
524 goto out;
525
526 if (ret)
527 new_src->k.type = KEY_TYPE_hash_whiteout;
528 }
529 }
530
531 if (new_dst->v.d_type == DT_SUBVOL)
532 new_dst->v.d_parent_subvol = cpu_to_le32(dst_dir.subvol);
533
534 if ((mode == BCH_RENAME_EXCHANGE) &&
535 new_src->v.d_type == DT_SUBVOL)
536 new_src->v.d_parent_subvol = cpu_to_le32(src_dir.subvol);
537
538 if (old_dst.k)
539 *dst_dir_i_size -= bkey_bytes(old_dst.k);
540 *src_dir_i_size -= bkey_bytes(old_src.k);
541
542 if (mode == BCH_RENAME_EXCHANGE)
543 *src_dir_i_size += bkey_bytes(&new_src->k);
544 *dst_dir_i_size += bkey_bytes(&new_dst->k);
545
546 ret = bch2_trans_update(trans, &dst_iter, &new_dst->k_i, 0);
547 if (ret)
548 goto out;
549 out_set_src:
550 /*
551 * If we're deleting a subvolume we need to really delete the dirent,
552 * not just emit a whiteout in the current snapshot - there can only be
553 * single dirent that points to a given subvolume.
554 *
555 * IOW, we don't maintain multiple versions in different snapshots of
556 * dirents that point to subvolumes - dirents that point to subvolumes
557 * are only visible in one particular subvolume so it's not necessary,
558 * and it would be particularly confusing for fsck to have to deal with.
559 */
560 delete_src = bkey_s_c_to_dirent(old_src).v->d_type == DT_SUBVOL &&
561 new_src->k.p.snapshot != old_src.k->p.snapshot;
562
563 delete_dst = old_dst.k &&
564 bkey_s_c_to_dirent(old_dst).v->d_type == DT_SUBVOL &&
565 new_dst->k.p.snapshot != old_dst.k->p.snapshot;
566
567 if (!delete_src || !bkey_deleted(&new_src->k)) {
568 ret = bch2_trans_update(trans, &src_iter, &new_src->k_i, src_update_flags);
569 if (ret)
570 goto out;
571 }
572
573 if (delete_src) {
574 bch2_btree_iter_set_snapshot(trans, &src_iter, old_src.k->p.snapshot);
575 ret = bch2_btree_iter_traverse(trans, &src_iter) ?:
576 bch2_btree_delete_at(trans, &src_iter, BTREE_UPDATE_internal_snapshot_node);
577 if (ret)
578 goto out;
579 }
580
581 if (delete_dst) {
582 bch2_btree_iter_set_snapshot(trans, &dst_iter, old_dst.k->p.snapshot);
583 ret = bch2_btree_iter_traverse(trans, &dst_iter) ?:
584 bch2_btree_delete_at(trans, &dst_iter, BTREE_UPDATE_internal_snapshot_node);
585 if (ret)
586 goto out;
587 }
588
589 if (mode == BCH_RENAME_EXCHANGE)
590 *src_offset = new_src->k.p.offset;
591 *dst_offset = new_dst->k.p.offset;
592 out:
593 bch2_trans_iter_exit(trans, &src_iter);
594 bch2_trans_iter_exit(trans, &dst_iter);
595 return ret;
596 }
597
bch2_dirent_lookup_trans(struct btree_trans * trans,struct btree_iter * iter,subvol_inum dir,const struct bch_hash_info * hash_info,const struct qstr * name,subvol_inum * inum,unsigned flags)598 int bch2_dirent_lookup_trans(struct btree_trans *trans,
599 struct btree_iter *iter,
600 subvol_inum dir,
601 const struct bch_hash_info *hash_info,
602 const struct qstr *name, subvol_inum *inum,
603 unsigned flags)
604 {
605 struct qstr lookup_name;
606 int ret = bch2_maybe_casefold(trans, hash_info, name, &lookup_name);
607 if (ret)
608 return ret;
609
610 struct bkey_s_c k = bch2_hash_lookup(trans, iter, bch2_dirent_hash_desc,
611 hash_info, dir, &lookup_name, flags);
612 ret = bkey_err(k);
613 if (ret)
614 goto err;
615
616 ret = bch2_dirent_read_target(trans, dir, bkey_s_c_to_dirent(k), inum);
617 if (ret > 0)
618 ret = -ENOENT;
619 err:
620 if (ret)
621 bch2_trans_iter_exit(trans, iter);
622 return ret;
623 }
624
bch2_dirent_lookup(struct bch_fs * c,subvol_inum dir,const struct bch_hash_info * hash_info,const struct qstr * name,subvol_inum * inum)625 u64 bch2_dirent_lookup(struct bch_fs *c, subvol_inum dir,
626 const struct bch_hash_info *hash_info,
627 const struct qstr *name, subvol_inum *inum)
628 {
629 struct btree_trans *trans = bch2_trans_get(c);
630 struct btree_iter iter = {};
631
632 int ret = lockrestart_do(trans,
633 bch2_dirent_lookup_trans(trans, &iter, dir, hash_info, name, inum, 0));
634 bch2_trans_iter_exit(trans, &iter);
635 bch2_trans_put(trans);
636 return ret;
637 }
638
bch2_empty_dir_snapshot(struct btree_trans * trans,u64 dir,u32 subvol,u32 snapshot)639 int bch2_empty_dir_snapshot(struct btree_trans *trans, u64 dir, u32 subvol, u32 snapshot)
640 {
641 struct btree_iter iter;
642 struct bkey_s_c k;
643 int ret;
644
645 for_each_btree_key_max_norestart(trans, iter, BTREE_ID_dirents,
646 SPOS(dir, 0, snapshot),
647 POS(dir, U64_MAX), 0, k, ret)
648 if (k.k->type == KEY_TYPE_dirent) {
649 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
650 if (d.v->d_type == DT_SUBVOL && le32_to_cpu(d.v->d_parent_subvol) != subvol)
651 continue;
652 ret = -BCH_ERR_ENOTEMPTY_dir_not_empty;
653 break;
654 }
655 bch2_trans_iter_exit(trans, &iter);
656
657 return ret;
658 }
659
bch2_empty_dir_trans(struct btree_trans * trans,subvol_inum dir)660 int bch2_empty_dir_trans(struct btree_trans *trans, subvol_inum dir)
661 {
662 u32 snapshot;
663
664 return bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot) ?:
665 bch2_empty_dir_snapshot(trans, dir.inum, dir.subvol, snapshot);
666 }
667
bch2_dir_emit(struct dir_context * ctx,struct bkey_s_c_dirent d,subvol_inum target)668 static int bch2_dir_emit(struct dir_context *ctx, struct bkey_s_c_dirent d, subvol_inum target)
669 {
670 struct qstr name = bch2_dirent_get_name(d);
671 /*
672 * Although not required by the kernel code, updating ctx->pos is needed
673 * for the bcachefs FUSE driver. Without this update, the FUSE
674 * implementation will be stuck in an infinite loop when reading
675 * directories (via the bcachefs_fuse_readdir callback).
676 * In kernel space, ctx->pos is updated by the VFS code.
677 */
678 ctx->pos = d.k->p.offset;
679 bool ret = dir_emit(ctx, name.name,
680 name.len,
681 target.inum,
682 vfs_d_type(d.v->d_type));
683 if (ret)
684 ctx->pos = d.k->p.offset + 1;
685 return !ret;
686 }
687
bch2_readdir(struct bch_fs * c,subvol_inum inum,struct dir_context * ctx)688 int bch2_readdir(struct bch_fs *c, subvol_inum inum, struct dir_context *ctx)
689 {
690 struct bkey_buf sk;
691 bch2_bkey_buf_init(&sk);
692
693 int ret = bch2_trans_run(c,
694 for_each_btree_key_in_subvolume_max(trans, iter, BTREE_ID_dirents,
695 POS(inum.inum, ctx->pos),
696 POS(inum.inum, U64_MAX),
697 inum.subvol, 0, k, ({
698 if (k.k->type != KEY_TYPE_dirent)
699 continue;
700
701 /* dir_emit() can fault and block: */
702 bch2_bkey_buf_reassemble(&sk, c, k);
703 struct bkey_s_c_dirent dirent = bkey_i_to_s_c_dirent(sk.k);
704
705 subvol_inum target;
706 int ret2 = bch2_dirent_read_target(trans, inum, dirent, &target);
707 if (ret2 > 0)
708 continue;
709
710 ret2 ?: (bch2_trans_unlock(trans), bch2_dir_emit(ctx, dirent, target));
711 })));
712
713 bch2_bkey_buf_exit(&sk, c);
714
715 return ret < 0 ? ret : 0;
716 }
717
718 /* fsck */
719
lookup_first_inode(struct btree_trans * trans,u64 inode_nr,struct bch_inode_unpacked * inode)720 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
721 struct bch_inode_unpacked *inode)
722 {
723 struct btree_iter iter;
724 struct bkey_s_c k;
725 int ret;
726
727 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inode_nr),
728 BTREE_ITER_all_snapshots, k, ret) {
729 if (k.k->p.offset != inode_nr)
730 break;
731 if (!bkey_is_inode(k.k))
732 continue;
733 ret = bch2_inode_unpack(k, inode);
734 goto found;
735 }
736 ret = -BCH_ERR_ENOENT_inode;
737 found:
738 bch_err_msg(trans->c, ret, "fetching inode %llu", inode_nr);
739 bch2_trans_iter_exit(trans, &iter);
740 return ret;
741 }
742
bch2_fsck_remove_dirent(struct btree_trans * trans,struct bpos pos)743 int bch2_fsck_remove_dirent(struct btree_trans *trans, struct bpos pos)
744 {
745 struct bch_fs *c = trans->c;
746 struct btree_iter iter;
747 struct bch_inode_unpacked dir_inode;
748 struct bch_hash_info dir_hash_info;
749 int ret;
750
751 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
752 if (ret)
753 goto err;
754
755 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
756
757 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_intent);
758
759 ret = bch2_btree_iter_traverse(trans, &iter) ?:
760 bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
761 &dir_hash_info, &iter,
762 BTREE_UPDATE_internal_snapshot_node);
763 bch2_trans_iter_exit(trans, &iter);
764 err:
765 bch_err_fn(c, ret);
766 return ret;
767 }
768