1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
12 #include <linux/posix_acl.h>
14 static inline int is_subdir_for_nlink(struct bch_inode_unpacked *inode)
16 return S_ISDIR(inode->bi_mode) && !inode->bi_subvol;
19 int bch2_create_trans(struct btree_trans *trans,
21 struct bch_inode_unpacked *dir_u,
22 struct bch_inode_unpacked *new_inode,
23 const struct qstr *name,
24 uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
25 struct posix_acl *default_acl,
26 struct posix_acl *acl,
27 subvol_inum snapshot_src,
30 struct bch_fs *c = trans->c;
31 struct btree_iter dir_iter = { NULL };
32 struct btree_iter inode_iter = { NULL };
33 subvol_inum new_inum = dir;
34 u64 now = bch2_current_time(c);
35 u64 cpu = raw_smp_processor_id();
38 unsigned dir_type = mode_to_type(mode);
41 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &snapshot);
45 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir,
46 BTREE_ITER_intent|BTREE_ITER_with_updates);
50 if (!(flags & BCH_CREATE_SNAPSHOT)) {
51 /* Normal create path - allocate a new inode: */
52 bch2_inode_init_late(new_inode, now, uid, gid, mode, rdev, dir_u);
54 if (flags & BCH_CREATE_TMPFILE)
55 new_inode->bi_flags |= BCH_INODE_unlinked;
57 ret = bch2_inode_create(trans, &inode_iter, new_inode, snapshot, cpu);
61 snapshot_src = (subvol_inum) { 0 };
64 * Creating a snapshot - we're not allocating a new inode, but
65 * we do have to lookup the root inode of the subvolume we're
66 * snapshotting and update it (in the new snapshot):
69 if (!snapshot_src.inum) {
70 /* Inode wasn't specified, just snapshot: */
71 struct bch_subvolume s;
72 ret = bch2_subvolume_get(trans, snapshot_src.subvol, true, &s);
76 snapshot_src.inum = le64_to_cpu(s.inode);
79 ret = bch2_inode_peek(trans, &inode_iter, new_inode, snapshot_src,
84 if (new_inode->bi_subvol != snapshot_src.subvol) {
85 /* Not a subvolume root: */
91 * If we're not root, we have to own the subvolume being
94 if (uid && new_inode->bi_uid != uid) {
99 flags |= BCH_CREATE_SUBVOL;
102 new_inum.inum = new_inode->bi_inum;
103 dir_target = new_inode->bi_inum;
105 if (flags & BCH_CREATE_SUBVOL) {
106 u32 new_subvol, dir_snapshot;
108 ret = bch2_subvolume_create(trans, new_inode->bi_inum,
111 &new_subvol, &snapshot,
112 (flags & BCH_CREATE_SNAPSHOT_RO) != 0);
116 new_inode->bi_parent_subvol = dir.subvol;
117 new_inode->bi_subvol = new_subvol;
118 new_inum.subvol = new_subvol;
119 dir_target = new_subvol;
120 dir_type = DT_SUBVOL;
122 ret = bch2_subvolume_get_snapshot(trans, dir.subvol, &dir_snapshot);
126 bch2_btree_iter_set_snapshot(&dir_iter, dir_snapshot);
127 ret = bch2_btree_iter_traverse(&dir_iter);
132 if (!(flags & BCH_CREATE_SNAPSHOT)) {
134 ret = bch2_set_acl_trans(trans, new_inum, new_inode,
135 default_acl, ACL_TYPE_DEFAULT);
141 ret = bch2_set_acl_trans(trans, new_inum, new_inode,
142 acl, ACL_TYPE_ACCESS);
148 if (!(flags & BCH_CREATE_TMPFILE)) {
149 struct bch_hash_info dir_hash = bch2_hash_info_init(c, dir_u);
152 if (is_subdir_for_nlink(new_inode))
154 dir_u->bi_mtime = dir_u->bi_ctime = now;
155 dir_u->bi_size += dirent_occupied_size(name);
157 ret = bch2_inode_write(trans, &dir_iter, dir_u);
161 ret = bch2_dirent_create(trans, dir, &dir_hash,
166 STR_HASH_must_create|BTREE_ITER_with_updates);
170 new_inode->bi_dir = dir_u->bi_inum;
171 new_inode->bi_dir_offset = dir_offset;
175 !new_inode->bi_subvol)
176 new_inode->bi_depth = dir_u->bi_depth + 1;
178 inode_iter.flags &= ~BTREE_ITER_all_snapshots;
179 bch2_btree_iter_set_snapshot(&inode_iter, snapshot);
181 ret = bch2_btree_iter_traverse(&inode_iter) ?:
182 bch2_inode_write(trans, &inode_iter, new_inode);
184 bch2_trans_iter_exit(trans, &inode_iter);
185 bch2_trans_iter_exit(trans, &dir_iter);
189 int bch2_link_trans(struct btree_trans *trans,
190 subvol_inum dir, struct bch_inode_unpacked *dir_u,
191 subvol_inum inum, struct bch_inode_unpacked *inode_u,
192 const struct qstr *name)
194 struct bch_fs *c = trans->c;
195 struct btree_iter dir_iter = { NULL };
196 struct btree_iter inode_iter = { NULL };
197 struct bch_hash_info dir_hash;
198 u64 now = bch2_current_time(c);
202 if (dir.subvol != inum.subvol)
205 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent);
209 inode_u->bi_ctime = now;
210 ret = bch2_inode_nlink_inc(inode_u);
214 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_intent);
218 if (bch2_reinherit_attrs(inode_u, dir_u)) {
223 dir_u->bi_mtime = dir_u->bi_ctime = now;
224 dir_u->bi_size += dirent_occupied_size(name);
226 dir_hash = bch2_hash_info_init(c, dir_u);
228 ret = bch2_dirent_create(trans, dir, &dir_hash,
229 mode_to_type(inode_u->bi_mode),
230 name, inum.inum, &dir_offset,
231 STR_HASH_must_create);
235 inode_u->bi_dir = dir.inum;
236 inode_u->bi_dir_offset = dir_offset;
238 ret = bch2_inode_write(trans, &dir_iter, dir_u) ?:
239 bch2_inode_write(trans, &inode_iter, inode_u);
241 bch2_trans_iter_exit(trans, &dir_iter);
242 bch2_trans_iter_exit(trans, &inode_iter);
246 int bch2_unlink_trans(struct btree_trans *trans,
248 struct bch_inode_unpacked *dir_u,
249 struct bch_inode_unpacked *inode_u,
250 const struct qstr *name,
251 bool deleting_subvol)
253 struct bch_fs *c = trans->c;
254 struct btree_iter dir_iter = { NULL };
255 struct btree_iter dirent_iter = { NULL };
256 struct btree_iter inode_iter = { NULL };
257 struct bch_hash_info dir_hash;
259 u64 now = bch2_current_time(c);
263 ret = bch2_inode_peek(trans, &dir_iter, dir_u, dir, BTREE_ITER_intent);
267 dir_hash = bch2_hash_info_init(c, dir_u);
269 ret = bch2_dirent_lookup_trans(trans, &dirent_iter, dir, &dir_hash,
270 name, &inum, BTREE_ITER_intent);
274 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum,
279 if (!deleting_subvol && S_ISDIR(inode_u->bi_mode)) {
280 ret = bch2_empty_dir_trans(trans, inum);
285 if (deleting_subvol && !inode_u->bi_subvol) {
286 ret = -BCH_ERR_ENOENT_not_subvol;
290 if (inode_u->bi_subvol) {
291 /* Recursive subvolume destroy not allowed (yet?) */
292 ret = bch2_subvol_has_children(trans, inode_u->bi_subvol);
297 if (deleting_subvol || inode_u->bi_subvol) {
298 ret = bch2_subvolume_unlink(trans, inode_u->bi_subvol);
302 k = bch2_btree_iter_peek_slot(&dirent_iter);
308 * If we're deleting a subvolume, we need to really delete the
309 * dirent, not just emit a whiteout in the current snapshot:
311 bch2_btree_iter_set_snapshot(&dirent_iter, k.k->p.snapshot);
312 ret = bch2_btree_iter_traverse(&dirent_iter);
316 bch2_inode_nlink_dec(trans, inode_u);
319 if (inode_u->bi_dir == dirent_iter.pos.inode &&
320 inode_u->bi_dir_offset == dirent_iter.pos.offset) {
322 inode_u->bi_dir_offset = 0;
325 dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
326 dir_u->bi_nlink -= is_subdir_for_nlink(inode_u);
327 dir_u->bi_size -= dirent_occupied_size(name);
329 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
330 &dir_hash, &dirent_iter,
331 BTREE_UPDATE_internal_snapshot_node) ?:
332 bch2_inode_write(trans, &dir_iter, dir_u) ?:
333 bch2_inode_write(trans, &inode_iter, inode_u);
335 bch2_trans_iter_exit(trans, &inode_iter);
336 bch2_trans_iter_exit(trans, &dirent_iter);
337 bch2_trans_iter_exit(trans, &dir_iter);
341 bool bch2_reinherit_attrs(struct bch_inode_unpacked *dst_u,
342 struct bch_inode_unpacked *src_u)
348 for (id = 0; id < Inode_opt_nr; id++) {
349 /* Skip attributes that were explicitly set on this inode */
350 if (dst_u->bi_fields_set & (1 << id))
353 src = bch2_inode_opt_get(src_u, id);
354 dst = bch2_inode_opt_get(dst_u, id);
359 bch2_inode_opt_set(dst_u, id, src);
366 static int subvol_update_parent(struct btree_trans *trans, u32 subvol, u32 new_parent)
368 struct btree_iter iter;
369 struct bkey_i_subvolume *s =
370 bch2_bkey_get_mut_typed(trans, &iter,
371 BTREE_ID_subvolumes, POS(0, subvol),
372 BTREE_ITER_cached, subvolume);
373 int ret = PTR_ERR_OR_ZERO(s);
377 s->v.fs_path_parent = cpu_to_le32(new_parent);
378 bch2_trans_iter_exit(trans, &iter);
382 int bch2_rename_trans(struct btree_trans *trans,
383 subvol_inum src_dir, struct bch_inode_unpacked *src_dir_u,
384 subvol_inum dst_dir, struct bch_inode_unpacked *dst_dir_u,
385 struct bch_inode_unpacked *src_inode_u,
386 struct bch_inode_unpacked *dst_inode_u,
387 const struct qstr *src_name,
388 const struct qstr *dst_name,
389 enum bch_rename_mode mode)
391 struct bch_fs *c = trans->c;
392 struct btree_iter src_dir_iter = { NULL };
393 struct btree_iter dst_dir_iter = { NULL };
394 struct btree_iter src_inode_iter = { NULL };
395 struct btree_iter dst_inode_iter = { NULL };
396 struct bch_hash_info src_hash, dst_hash;
397 subvol_inum src_inum, dst_inum;
398 u64 src_offset, dst_offset;
399 u64 now = bch2_current_time(c);
402 ret = bch2_inode_peek(trans, &src_dir_iter, src_dir_u, src_dir,
407 src_hash = bch2_hash_info_init(c, src_dir_u);
409 if (dst_dir.inum != src_dir.inum ||
410 dst_dir.subvol != src_dir.subvol) {
411 ret = bch2_inode_peek(trans, &dst_dir_iter, dst_dir_u, dst_dir,
416 dst_hash = bch2_hash_info_init(c, dst_dir_u);
418 dst_dir_u = src_dir_u;
422 ret = bch2_dirent_rename(trans,
425 src_name, &src_inum, &src_offset,
426 dst_name, &dst_inum, &dst_offset,
431 ret = bch2_inode_peek(trans, &src_inode_iter, src_inode_u, src_inum,
437 ret = bch2_inode_peek(trans, &dst_inode_iter, dst_inode_u, dst_inum,
443 if (src_inode_u->bi_subvol &&
444 dst_dir.subvol != src_inode_u->bi_parent_subvol) {
445 ret = subvol_update_parent(trans, src_inode_u->bi_subvol, dst_dir.subvol);
450 if (mode == BCH_RENAME_EXCHANGE &&
451 dst_inode_u->bi_subvol &&
452 src_dir.subvol != dst_inode_u->bi_parent_subvol) {
453 ret = subvol_update_parent(trans, dst_inode_u->bi_subvol, src_dir.subvol);
458 /* Can't move across subvolumes, unless it's a subvolume root: */
459 if (src_dir.subvol != dst_dir.subvol &&
460 (!src_inode_u->bi_subvol ||
461 (dst_inum.inum && !dst_inode_u->bi_subvol))) {
466 if (mode == BCH_RENAME) {
467 src_dir_u->bi_size -= dirent_occupied_size(src_name);
468 dst_dir_u->bi_size += dirent_occupied_size(dst_name);
471 if (mode == BCH_RENAME_OVERWRITE)
472 src_dir_u->bi_size -= dirent_occupied_size(src_name);
474 if (src_inode_u->bi_parent_subvol)
475 src_inode_u->bi_parent_subvol = dst_dir.subvol;
477 if ((mode == BCH_RENAME_EXCHANGE) &&
478 dst_inode_u->bi_parent_subvol)
479 dst_inode_u->bi_parent_subvol = src_dir.subvol;
481 src_inode_u->bi_dir = dst_dir_u->bi_inum;
482 src_inode_u->bi_dir_offset = dst_offset;
484 if (mode == BCH_RENAME_EXCHANGE) {
485 dst_inode_u->bi_dir = src_dir_u->bi_inum;
486 dst_inode_u->bi_dir_offset = src_offset;
489 if (mode == BCH_RENAME_OVERWRITE &&
490 dst_inode_u->bi_dir == dst_dir_u->bi_inum &&
491 dst_inode_u->bi_dir_offset == src_offset) {
492 dst_inode_u->bi_dir = 0;
493 dst_inode_u->bi_dir_offset = 0;
496 if (mode == BCH_RENAME_OVERWRITE) {
497 if (S_ISDIR(src_inode_u->bi_mode) !=
498 S_ISDIR(dst_inode_u->bi_mode)) {
503 if (S_ISDIR(dst_inode_u->bi_mode)) {
504 ret = bch2_empty_dir_trans(trans, dst_inum);
510 if (bch2_reinherit_attrs(src_inode_u, dst_dir_u) &&
511 S_ISDIR(src_inode_u->bi_mode)) {
516 if (mode == BCH_RENAME_EXCHANGE &&
517 bch2_reinherit_attrs(dst_inode_u, src_dir_u) &&
518 S_ISDIR(dst_inode_u->bi_mode)) {
523 if (is_subdir_for_nlink(src_inode_u)) {
524 src_dir_u->bi_nlink--;
525 dst_dir_u->bi_nlink++;
528 if (S_ISDIR(src_inode_u->bi_mode) &&
529 !src_inode_u->bi_subvol)
530 src_inode_u->bi_depth = dst_dir_u->bi_depth + 1;
532 if (mode == BCH_RENAME_EXCHANGE &&
533 S_ISDIR(dst_inode_u->bi_mode) &&
534 !dst_inode_u->bi_subvol)
535 dst_inode_u->bi_depth = src_dir_u->bi_depth + 1;
537 if (dst_inum.inum && is_subdir_for_nlink(dst_inode_u)) {
538 dst_dir_u->bi_nlink--;
539 src_dir_u->bi_nlink += mode == BCH_RENAME_EXCHANGE;
542 if (mode == BCH_RENAME_OVERWRITE)
543 bch2_inode_nlink_dec(trans, dst_inode_u);
545 src_dir_u->bi_mtime = now;
546 src_dir_u->bi_ctime = now;
548 if (src_dir.inum != dst_dir.inum) {
549 dst_dir_u->bi_mtime = now;
550 dst_dir_u->bi_ctime = now;
553 src_inode_u->bi_ctime = now;
556 dst_inode_u->bi_ctime = now;
558 ret = bch2_inode_write(trans, &src_dir_iter, src_dir_u) ?:
559 (src_dir.inum != dst_dir.inum
560 ? bch2_inode_write(trans, &dst_dir_iter, dst_dir_u)
562 bch2_inode_write(trans, &src_inode_iter, src_inode_u) ?:
564 ? bch2_inode_write(trans, &dst_inode_iter, dst_inode_u)
567 bch2_trans_iter_exit(trans, &dst_inode_iter);
568 bch2_trans_iter_exit(trans, &src_inode_iter);
569 bch2_trans_iter_exit(trans, &dst_dir_iter);
570 bch2_trans_iter_exit(trans, &src_dir_iter);
574 static inline void prt_bytes_reversed(struct printbuf *out, const void *b, unsigned n)
576 bch2_printbuf_make_room(out, n);
578 unsigned can_print = min(n, printbuf_remaining(out));
582 for (unsigned i = 0; i < can_print; i++)
583 out->buf[out->pos++] = *((char *) --b);
585 printbuf_nul_terminate(out);
588 static inline void prt_str_reversed(struct printbuf *out, const char *s)
590 prt_bytes_reversed(out, s, strlen(s));
593 static inline void reverse_bytes(void *b, size_t n)
595 char *e = b + n, *s = b;
604 /* XXX: we don't yet attempt to print paths when we don't know the subvol */
605 int bch2_inum_to_path(struct btree_trans *trans, subvol_inum inum, struct printbuf *path)
607 unsigned orig_pos = path->pos;
610 while (!(inum.subvol == BCACHEFS_ROOT_SUBVOL &&
611 inum.inum == BCACHEFS_ROOT_INO)) {
612 struct bch_inode_unpacked inode;
613 ret = bch2_inode_find_by_inum_trans(trans, inum, &inode);
617 if (!inode.bi_dir && !inode.bi_dir_offset) {
618 ret = -BCH_ERR_ENOENT_inode_no_backpointer;
622 inum.subvol = inode.bi_parent_subvol ?: inum.subvol;
623 inum.inum = inode.bi_dir;
626 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
630 struct btree_iter d_iter;
631 struct bkey_s_c_dirent d = bch2_bkey_get_iter_typed(trans, &d_iter,
632 BTREE_ID_dirents, SPOS(inode.bi_dir, inode.bi_dir_offset, snapshot),
634 ret = bkey_err(d.s_c);
638 struct qstr dirent_name = bch2_dirent_get_name(d);
639 prt_bytes_reversed(path, dirent_name.name, dirent_name.len);
643 bch2_trans_iter_exit(trans, &d_iter);
646 if (orig_pos == path->pos)
649 ret = path->allocation_failure ? -ENOMEM : 0;
653 reverse_bytes(path->buf + orig_pos, path->pos - orig_pos);
658 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
661 prt_str_reversed(path, "(disconnected)");