]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/reflink.c
New upstream release
[bcachefs-tools-debian] / libbcachefs / reflink.c
index c8d6d73681e010c0ec00221a7a473070c4680b0a..d5c14bb2992d5d7fc4281a207140861a0cdefd1f 100644 (file)
@@ -25,18 +25,25 @@ static inline unsigned bkey_type_to_indirect(const struct bkey *k)
 
 /* reflink pointers */
 
-const char *bch2_reflink_p_invalid(const struct bch_fs *c, struct bkey_s_c k)
+int bch2_reflink_p_invalid(const struct bch_fs *c, struct bkey_s_c k,
+                          int rw, struct printbuf *err)
 {
        struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
 
-       if (bkey_val_bytes(p.k) != sizeof(*p.v))
-               return "incorrect value size";
+       if (bkey_val_bytes(p.k) != sizeof(*p.v)) {
+               prt_printf(err, "incorrect value size (%zu != %zu)",
+                      bkey_val_bytes(p.k), sizeof(*p.v));
+               return -EINVAL;
+       }
 
        if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix &&
-           le64_to_cpu(p.v->idx) < le32_to_cpu(p.v->front_pad))
-               return "idx < front_pad";
+           le64_to_cpu(p.v->idx) < le32_to_cpu(p.v->front_pad)) {
+               prt_printf(err, "idx < front_pad (%llu < %u)",
+                      le64_to_cpu(p.v->idx), le32_to_cpu(p.v->front_pad));
+               return -EINVAL;
+       }
 
-       return NULL;
+       return 0;
 }
 
 void bch2_reflink_p_to_text(struct printbuf *out, struct bch_fs *c,
@@ -44,7 +51,7 @@ void bch2_reflink_p_to_text(struct printbuf *out, struct bch_fs *c,
 {
        struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
 
-       pr_buf(out, "idx %llu front_pad %u back_pad %u",
+       prt_printf(out, "idx %llu front_pad %u back_pad %u",
               le64_to_cpu(p.v->idx),
               le32_to_cpu(p.v->front_pad),
               le32_to_cpu(p.v->back_pad));
@@ -70,14 +77,18 @@ bool bch2_reflink_p_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r
 
 /* indirect extents */
 
-const char *bch2_reflink_v_invalid(const struct bch_fs *c, struct bkey_s_c k)
+int bch2_reflink_v_invalid(const struct bch_fs *c, struct bkey_s_c k,
+                          int rw, struct printbuf *err)
 {
        struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k);
 
-       if (bkey_val_bytes(r.k) < sizeof(*r.v))
-               return "incorrect value size";
+       if (bkey_val_bytes(r.k) < sizeof(*r.v)) {
+               prt_printf(err, "incorrect value size (%zu < %zu)",
+                      bkey_val_bytes(r.k), sizeof(*r.v));
+               return -EINVAL;
+       }
 
-       return bch2_bkey_ptrs_invalid(c, k);
+       return bch2_bkey_ptrs_invalid(c, k, rw, err);
 }
 
 void bch2_reflink_v_to_text(struct printbuf *out, struct bch_fs *c,
@@ -85,7 +96,7 @@ void bch2_reflink_v_to_text(struct printbuf *out, struct bch_fs *c,
 {
        struct bkey_s_c_reflink_v r = bkey_s_c_to_reflink_v(k);
 
-       pr_buf(out, "refcount: %llu ", le64_to_cpu(r.v->refcount));
+       prt_printf(out, "refcount: %llu ", le64_to_cpu(r.v->refcount));
 
        bch2_bkey_ptrs_to_text(out, c, k);
 }
@@ -98,14 +109,37 @@ bool bch2_reflink_v_merge(struct bch_fs *c, struct bkey_s _l, struct bkey_s_c _r
        return l.v->refcount == r.v->refcount && bch2_extent_merge(c, _l, _r);
 }
 
+int bch2_trans_mark_reflink_v(struct btree_trans *trans,
+                             enum btree_id btree_id, unsigned level,
+                             struct bkey_s_c old, struct bkey_i *new,
+                             unsigned flags)
+{
+       if (!(flags & BTREE_TRIGGER_OVERWRITE)) {
+               struct bkey_i_reflink_v *r = bkey_i_to_reflink_v(new);
+
+               if (!r->v.refcount) {
+                       r->k.type = KEY_TYPE_deleted;
+                       r->k.size = 0;
+                       set_bkey_val_u64s(&r->k, 0);
+                       return 0;
+               }
+       }
+
+       return bch2_trans_mark_extent(trans, btree_id, level, old, new, flags);
+}
+
 /* indirect inline data */
 
-const char *bch2_indirect_inline_data_invalid(const struct bch_fs *c,
-                                             struct bkey_s_c k)
+int bch2_indirect_inline_data_invalid(const struct bch_fs *c, struct bkey_s_c k,
+                                     int rw, struct printbuf *err)
 {
-       if (bkey_val_bytes(k.k) < sizeof(struct bch_indirect_inline_data))
-               return "incorrect value size";
-       return NULL;
+       if (bkey_val_bytes(k.k) < sizeof(struct bch_indirect_inline_data)) {
+               prt_printf(err, "incorrect value size (%zu < %zu)",
+                      bkey_val_bytes(k.k), sizeof(struct bch_indirect_inline_data));
+               return -EINVAL;
+       }
+
+       return 0;
 }
 
 void bch2_indirect_inline_data_to_text(struct printbuf *out,
@@ -114,11 +148,30 @@ void bch2_indirect_inline_data_to_text(struct printbuf *out,
        struct bkey_s_c_indirect_inline_data d = bkey_s_c_to_indirect_inline_data(k);
        unsigned datalen = bkey_inline_data_bytes(k.k);
 
-       pr_buf(out, "refcount %llu datalen %u: %*phN",
+       prt_printf(out, "refcount %llu datalen %u: %*phN",
               le64_to_cpu(d.v->refcount), datalen,
               min(datalen, 32U), d.v->data);
 }
 
+int bch2_trans_mark_indirect_inline_data(struct btree_trans *trans,
+                             enum btree_id btree_id, unsigned level,
+                             struct bkey_s_c old, struct bkey_i *new,
+                             unsigned flags)
+{
+       if (!(flags & BTREE_TRIGGER_OVERWRITE)) {
+               struct bkey_i_indirect_inline_data *r =
+                       bkey_i_to_indirect_inline_data(new);
+
+               if (!r->v.refcount) {
+                       r->k.type = KEY_TYPE_deleted;
+                       r->k.size = 0;
+                       set_bkey_val_u64s(&r->k, 0);
+               }
+       }
+
+       return 0;
+}
+
 static int bch2_make_extent_indirect(struct btree_trans *trans,
                                     struct btree_iter *extent_iter,
                                     struct bkey_i *orig)
@@ -229,7 +282,7 @@ s64 bch2_remap_range(struct bch_fs *c,
        u32 dst_snapshot, src_snapshot;
        int ret = 0, ret2 = 0;
 
-       if (!percpu_ref_tryget(&c->writes))
+       if (!percpu_ref_tryget_live(&c->writes))
                return -EROFS;
 
        bch2_check_set_feature(c, BCH_FEATURE_reflink);
@@ -246,7 +299,8 @@ s64 bch2_remap_range(struct bch_fs *c,
        bch2_trans_iter_init(&trans, &dst_iter, BTREE_ID_extents, dst_start,
                             BTREE_ITER_INTENT);
 
-       while ((ret == 0 || ret == -EINTR) &&
+       while ((ret == 0 ||
+               bch2_err_matches(ret, BCH_ERR_transaction_restart)) &&
               bkey_cmp(dst_iter.pos, dst_end) < 0) {
                struct disk_reservation disk_res = { 0 };
 
@@ -356,7 +410,7 @@ s64 bch2_remap_range(struct bch_fs *c,
                }
 
                bch2_trans_iter_exit(&trans, &inode_iter);
-       } while (ret2 == -EINTR);
+       } while (bch2_err_matches(ret2, BCH_ERR_transaction_restart));
 
        bch2_trans_exit(&trans);
        bch2_bkey_buf_exit(&new_src, c);