]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/io_read.c
convert main() from C to Rust
[bcachefs-tools-debian] / libbcachefs / io_read.c
index 443c3ea655271efc649bb425d752ef9d447f4dd4..3c574d8873a1e209dc7f7f48faacf9928f8a1272 100644 (file)
@@ -80,7 +80,7 @@ struct promote_op {
        struct bpos             pos;
 
        struct data_update      write;
-       struct bio_vec          bi_inline_vecs[0]; /* must be last */
+       struct bio_vec          bi_inline_vecs[]; /* must be last */
 };
 
 static const struct rhashtable_params bch_promote_params = {
@@ -172,11 +172,13 @@ static struct promote_op *__promote_alloc(struct btree_trans *trans,
        int ret;
 
        if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_promote))
-               return NULL;
+               return ERR_PTR(-BCH_ERR_nopromote_no_writes);
 
-       op = kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_NOFS);
-       if (!op)
+       op = kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_KERNEL);
+       if (!op) {
+               ret = -BCH_ERR_nopromote_enomem;
                goto err;
+       }
 
        op->start_time = local_clock();
        op->pos = pos;
@@ -187,29 +189,34 @@ static struct promote_op *__promote_alloc(struct btree_trans *trans,
         */
        *rbio = kzalloc(sizeof(struct bch_read_bio) +
                        sizeof(struct bio_vec) * pages,
-                       GFP_NOFS);
-       if (!*rbio)
+                       GFP_KERNEL);
+       if (!*rbio) {
+               ret = -BCH_ERR_nopromote_enomem;
                goto err;
+       }
 
        rbio_init(&(*rbio)->bio, opts);
        bio_init(&(*rbio)->bio, NULL, (*rbio)->bio.bi_inline_vecs, pages, 0);
 
-       if (bch2_bio_alloc_pages(&(*rbio)->bio, sectors << 9,
-                                GFP_NOFS))
+       if (bch2_bio_alloc_pages(&(*rbio)->bio, sectors << 9, GFP_KERNEL)) {
+               ret = -BCH_ERR_nopromote_enomem;
                goto err;
+       }
 
        (*rbio)->bounce         = true;
        (*rbio)->split          = true;
        (*rbio)->kmalloc        = true;
 
        if (rhashtable_lookup_insert_fast(&c->promote_table, &op->hash,
-                                         bch_promote_params))
+                                         bch_promote_params)) {
+               ret = -BCH_ERR_nopromote_in_flight;
                goto err;
+       }
 
        bio = &op->write.op.wbio.bio;
        bio_init(bio, NULL, bio->bi_inline_vecs, pages, 0);
 
-       ret = bch2_data_update_init(trans, NULL, &op->write,
+       ret = bch2_data_update_init(trans, NULL, NULL, &op->write,
                        writepoint_hashed((unsigned long) current),
                        opts,
                        (struct data_update_opts) {
@@ -223,9 +230,8 @@ static struct promote_op *__promote_alloc(struct btree_trans *trans,
         * -BCH_ERR_ENOSPC_disk_reservation:
         */
        if (ret) {
-               ret = rhashtable_remove_fast(&c->promote_table, &op->hash,
-                                       bch_promote_params);
-               BUG_ON(ret);
+               BUG_ON(rhashtable_remove_fast(&c->promote_table, &op->hash,
+                                             bch_promote_params));
                goto err;
        }
 
@@ -239,7 +245,7 @@ err:
        *rbio = NULL;
        kfree(op);
        bch2_write_ref_put(c, BCH_WRITE_REF_promote);
-       return NULL;
+       return ERR_PTR(ret);
 }
 
 noinline
@@ -274,10 +280,9 @@ static struct promote_op *promote_alloc(struct btree_trans *trans,
                                  ? BTREE_ID_reflink
                                  : BTREE_ID_extents,
                                  k, pos, pick, opts, sectors, rbio);
-       if (!promote) {
-               ret = -BCH_ERR_nopromote_enomem;
+       ret = PTR_ERR_OR_ZERO(promote);
+       if (ret)
                goto nopromote;
-       }
 
        *bounce         = true;
        *read_full      = promote_full;
@@ -526,7 +531,7 @@ out:
 
 static noinline void bch2_rbio_narrow_crcs(struct bch_read_bio *rbio)
 {
-       bch2_trans_do(rbio->c, NULL, NULL, BTREE_INSERT_NOFAIL,
+       bch2_trans_do(rbio->c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
                      __bch2_rbio_narrow_crcs(trans, rbio));
 }
 
@@ -637,13 +642,18 @@ csum_err:
                goto out;
        }
 
+       struct printbuf buf = PRINTBUF;
+       buf.atomic++;
+       prt_str(&buf, "data ");
+       bch2_csum_err_msg(&buf, crc.csum_type, rbio->pick.crc.csum, csum);
+
        bch_err_inum_offset_ratelimited(ca,
                rbio->read_pos.inode,
                rbio->read_pos.offset << 9,
-               "data checksum error: expected %0llx:%0llx got %0llx:%0llx (type %s)",
-               rbio->pick.crc.csum.hi, rbio->pick.crc.csum.lo,
-               csum.hi, csum.lo, bch2_csum_types[crc.csum_type]);
-       bch2_io_error(ca);
+               "data %s", buf.buf);
+       printbuf_exit(&buf);
+
+       bch2_io_error(ca, BCH_MEMBER_ERROR_checksum);
        bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
        goto out;
 decompression_err:
@@ -677,7 +687,7 @@ static void bch2_read_endio(struct bio *bio)
        if (!rbio->split)
                rbio->bio.bi_end_io = rbio->end_io;
 
-       if (bch2_dev_inum_io_err_on(bio->bi_status, ca,
+       if (bch2_dev_inum_io_err_on(bio->bi_status, ca, BCH_MEMBER_ERROR_read,
                                    rbio->read_pos.inode,
                                    rbio->read_pos.offset,
                                    "data read error: %s",
@@ -1025,7 +1035,7 @@ get_bio:
                trans->notrace_relock_fail = true;
        } else {
                /* Attempting reconstruct read: */
-               if (bch2_ec_read_extent(c, rbio)) {
+               if (bch2_ec_read_extent(trans, rbio)) {
                        bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
                        goto out;
                }