btrfs-progs: make pretty Documentation/ build match the rest
[platform/upstream/btrfs-progs.git] / chunk-recover.c
index a05e644..dfa7ff6 100644 (file)
@@ -745,8 +745,9 @@ static int scan_one_device(void *dev_scan_struct)
        struct recover_control *rc = dev_scan->rc;
        struct btrfs_device *device = dev_scan->dev;
        int fd = dev_scan->fd;
+       int oldtype;
 
-       ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+       ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
        if (ret)
                return 1;
 
@@ -845,7 +846,8 @@ static int scan_devices(struct recover_control *rc)
                if (fd < 0) {
                        fprintf(stderr, "Failed to open device %s\n",
                                dev->name);
-                       return -1;
+                       ret = 1;
+                       goto out2;
                }
                dev_scans[devidx].rc = rc;
                dev_scans[devidx].dev = dev;
@@ -856,7 +858,7 @@ static int scan_devices(struct recover_control *rc)
                if (ret) {
                        cancel_from = 0;
                        cancel_to = devidx - 1;
-                       goto out;
+                       goto out1;
                }
                devidx++;
        }
@@ -868,15 +870,16 @@ static int scan_devices(struct recover_control *rc)
                        ret = 1;
                        cancel_from = i + 1;
                        cancel_to = devnr - 1;
-                       break;
+                       goto out1;
                }
                i++;
        }
-out:
-       while (cancel_from <= cancel_to) {
+out1:
+       while (ret && (cancel_from <= cancel_to)) {
                pthread_cancel(t_scans[cancel_from]);
                cancel_from++;
        }
+out2:
        free(dev_scans);
        free(t_scans);
        free(t_rets);
@@ -1771,6 +1774,17 @@ static int insert_stripe(struct list_head *devexts,
        return 0;
 }
 
+static inline int count_devext_records(struct list_head *record_list)
+{
+       int num_of_records = 0;
+       struct device_extent_record *devext;
+
+       list_for_each_entry(devext, record_list, chunk_list)
+               num_of_records++;
+
+       return num_of_records;
+}
+
 #define EQUAL_STRIPE (1 << 0)
 
 static int rebuild_raid_data_chunk_stripes(struct recover_control *rc,
@@ -1874,8 +1888,7 @@ next_csum:
                fprintf(stderr, "Fetch csum failed\n");
                goto fail_out;
        } else if (ret == 1) {
-               list_for_each_entry(devext, &unordered, chunk_list)
-                       num_unordered++;
+               num_unordered = count_devext_records(&unordered);
                if (!(*flags & EQUAL_STRIPE))
                        *flags |= EQUAL_STRIPE;
                goto out;
@@ -1903,17 +1916,15 @@ next_csum:
        }
 
        if (list_empty(&candidates)) {
-               list_for_each_entry(devext, &unordered, chunk_list)
-                       num_unordered++;
+               num_unordered = count_devext_records(&unordered);
                if (chunk->type_flags & BTRFS_BLOCK_GROUP_RAID6
                                        && num_unordered == 2) {
                        list_splice_init(&unordered, &chunk->dextents);
                        btrfs_release_path(&path);
                        return 0;
-               } else
-                       ret = 1;
+               }
 
-               goto fail_out;
+               goto next_stripe;
        }
 
        if (list_is_last(candidates.next, &candidates)) {
@@ -1939,8 +1950,7 @@ next_stripe:
 out:
        ret = 0;
        list_splice_init(&candidates, &unordered);
-       list_for_each_entry(devext, &unordered, chunk_list)
-               num_unordered++;
+       num_unordered = count_devext_records(&unordered);
        if (num_unordered == 1) {
                for (i = 0; i < chunk->num_stripes; i++) {
                        if (!chunk->stripes[i].devid) {