Skip to content

Commit baf3909

Browse files
committed
Merge pull request #3 from djmarcin/master
Fixes for my Raid10 restore issues.
2 parents ff0e1b1 + 1607360 commit baf3909

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

restore.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ static int copy_one_extent(struct btrfs_root *root, int fd,
197197
int compress;
198198
int ret;
199199
int dev_fd;
200-
int mirror_num = 0;
200+
int mirror_num = 1;
201201
int num_copies;
202202

203203
compress = btrfs_file_extent_compression(leaf, fi);
@@ -240,14 +240,15 @@ static int copy_one_extent(struct btrfs_root *root, int fd,
240240

241241
if (size_left < length)
242242
length = size_left;
243-
size_left -= length;
244243

245244
done = pread(dev_fd, inbuf+count, length, dev_bytenr);
246-
if (done < length) {
245+
/* Need both checks, or we miss negative values due to u64 conversion */
246+
if (done < 0 || done < length) {
247247
num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
248248
bytenr, length);
249249
mirror_num++;
250-
if (mirror_num >= num_copies) {
250+
/* mirror_num is 1-indexed, so num_copies is a valid mirror. */
251+
if (mirror_num > num_copies) {
251252
ret = -1;
252253
fprintf(stderr, "Exhausted mirrors trying to read\n");
253254
goto out;
@@ -256,6 +257,8 @@ static int copy_one_extent(struct btrfs_root *root, int fd,
256257
goto again;
257258
}
258259

260+
mirror_num = 1;
261+
size_left -= length;
259262
count += length;
260263
bytenr += length;
261264
if (size_left)

0 commit comments

Comments
 (0)