+static void
+dmu_bio_put(struct bio *bio)
+{
+ struct bio *bio_next;
+
+ while (bio) {
+ bio_next = bio->bi_next;
+ bio_put(bio);
+ bio = bio_next;
+ }
+}
+
+static int
+dmu_bio_clone(struct bio *bio, struct bio **bio_copy)
+{
+ struct bio *bio_root = NULL;
+ struct bio *bio_last = NULL;
+ struct bio *bio_new;
+
+ if (bio == NULL)
+ return EINVAL;
+
+ while (bio) {
+ bio_new = bio_clone(bio, GFP_NOIO);
+ if (bio_new == NULL) {
+ dmu_bio_put(bio_root);
+ return ENOMEM;
+ }
+
+ if (bio_last) {
+ bio_last->bi_next = bio_new;
+ bio_last = bio_new;
+ } else {
+ bio_root = bio_new;
+ bio_last = bio_new;
+ }
+
+ bio = bio->bi_next;
+ }
+
+ *bio_copy = bio_root;
+
+ return 0;
+}
+