-
Notifications
You must be signed in to change notification settings - Fork 13
/
cmds-scrub.c
1781 lines (1607 loc) · 43.7 KB
/
cmds-scrub.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011 STRATO. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include "kerncompat.h"
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/syscall.h>
#include <poll.h>
#include <sys/file.h>
#include <uuid/uuid.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <ctype.h>
#include <signal.h>
#include <stdarg.h>
#include "ctree.h"
#include "ioctl.h"
#include "utils.h"
#include "volumes.h"
#include "disk-io.h"
#include "commands.h"
static const char * const scrub_cmd_group_usage[] = {
"btrfs scrub <command> [options] <path>|<device>",
NULL
};
#define SCRUB_DATA_FILE "/var/lib/btrfs/scrub.status"
#define SCRUB_PROGRESS_SOCKET_PATH "/var/lib/btrfs/scrub.progress"
#define SCRUB_FILE_VERSION_PREFIX "scrub status"
#define SCRUB_FILE_VERSION "1"
struct scrub_stats {
time_t t_start;
time_t t_resumed;
u64 duration;
u64 finished;
u64 canceled;
};
/* TBD: replace with #include "linux/ioprio.h" in some years */
#if !defined (IOPRIO_H)
#define IOPRIO_WHO_PROCESS 1
#define IOPRIO_CLASS_SHIFT 13
#define IOPRIO_PRIO_VALUE(class, data) \
(((class) << IOPRIO_CLASS_SHIFT) | (data))
#define IOPRIO_CLASS_IDLE 3
#endif
struct scrub_progress {
struct btrfs_ioctl_scrub_args scrub_args;
int fd;
int ret;
int skip;
struct scrub_stats stats;
struct scrub_file_record *resumed;
int ioctl_errno;
pthread_mutex_t progress_mutex;
int ioprio_class;
int ioprio_classdata;
};
struct scrub_file_record {
u8 fsid[BTRFS_FSID_SIZE];
u64 devid;
struct scrub_stats stats;
struct btrfs_scrub_progress p;
};
struct scrub_progress_cycle {
int fdmnt;
int prg_fd;
int do_record;
struct btrfs_ioctl_fs_info_args *fi;
struct scrub_progress *progress;
struct scrub_progress *shared_progress;
pthread_mutex_t *write_mutex;
};
struct scrub_fs_stat {
struct btrfs_scrub_progress p;
struct scrub_stats s;
int i;
};
static void print_scrub_full(struct btrfs_scrub_progress *sp)
{
printf("\tdata_extents_scrubbed: %lld\n", sp->data_extents_scrubbed);
printf("\ttree_extents_scrubbed: %lld\n", sp->tree_extents_scrubbed);
printf("\tdata_bytes_scrubbed: %lld\n", sp->data_bytes_scrubbed);
printf("\ttree_bytes_scrubbed: %lld\n", sp->tree_bytes_scrubbed);
printf("\tread_errors: %lld\n", sp->read_errors);
printf("\tcsum_errors: %lld\n", sp->csum_errors);
printf("\tverify_errors: %lld\n", sp->verify_errors);
printf("\tno_csum: %lld\n", sp->no_csum);
printf("\tcsum_discards: %lld\n", sp->csum_discards);
printf("\tsuper_errors: %lld\n", sp->super_errors);
printf("\tmalloc_errors: %lld\n", sp->malloc_errors);
printf("\tuncorrectable_errors: %lld\n", sp->uncorrectable_errors);
printf("\tunverified_errors: %lld\n", sp->unverified_errors);
printf("\tcorrected_errors: %lld\n", sp->corrected_errors);
printf("\tlast_physical: %lld\n", sp->last_physical);
}
#define ERR(test, ...) do { \
if (test) \
fprintf(stderr, __VA_ARGS__); \
} while (0)
#define PRINT_SCRUB_ERROR(test, desc) do { \
if (test) \
printf(" %s=%llu", desc, test); \
} while (0)
static void print_scrub_summary(struct btrfs_scrub_progress *p)
{
u64 err_cnt;
u64 err_cnt2;
err_cnt = p->read_errors +
p->csum_errors +
p->verify_errors +
p->super_errors;
err_cnt2 = p->corrected_errors + p->uncorrectable_errors;
if (p->malloc_errors)
printf("*** WARNING: memory allocation failed while scrubbing. "
"results may be inaccurate\n");
printf("\ttotal bytes scrubbed: %s with %llu errors\n",
pretty_size(p->data_bytes_scrubbed + p->tree_bytes_scrubbed),
max(err_cnt, err_cnt2));
if (err_cnt || err_cnt2) {
printf("\terror details:");
PRINT_SCRUB_ERROR(p->read_errors, "read");
PRINT_SCRUB_ERROR(p->super_errors, "super");
PRINT_SCRUB_ERROR(p->verify_errors, "verify");
PRINT_SCRUB_ERROR(p->csum_errors, "csum");
printf("\n");
printf("\tcorrected errors: %llu, uncorrectable errors: %llu, "
"unverified errors: %llu\n", p->corrected_errors,
p->uncorrectable_errors, p->unverified_errors);
}
}
#define _SCRUB_FS_STAT(p, name, fs_stat) do { \
fs_stat->p.name += p->name; \
} while (0)
#define _SCRUB_FS_STAT_MIN(ss, name, fs_stat) \
do { \
if (fs_stat->s.name > ss->name) { \
fs_stat->s.name = ss->name; \
} \
} while (0)
#define _SCRUB_FS_STAT_ZMIN(ss, name, fs_stat) \
do { \
if (!fs_stat->s.name || fs_stat->s.name > ss->name) { \
fs_stat->s.name = ss->name; \
} \
} while (0)
#define _SCRUB_FS_STAT_ZMAX(ss, name, fs_stat) \
do { \
if (!(fs_stat)->s.name || (fs_stat)->s.name < (ss)->name) { \
(fs_stat)->s.name = (ss)->name; \
} \
} while (0)
static void add_to_fs_stat(struct btrfs_scrub_progress *p,
struct scrub_stats *ss,
struct scrub_fs_stat *fs_stat)
{
_SCRUB_FS_STAT(p, data_extents_scrubbed, fs_stat);
_SCRUB_FS_STAT(p, tree_extents_scrubbed, fs_stat);
_SCRUB_FS_STAT(p, data_bytes_scrubbed, fs_stat);
_SCRUB_FS_STAT(p, tree_bytes_scrubbed, fs_stat);
_SCRUB_FS_STAT(p, read_errors, fs_stat);
_SCRUB_FS_STAT(p, csum_errors, fs_stat);
_SCRUB_FS_STAT(p, verify_errors, fs_stat);
_SCRUB_FS_STAT(p, no_csum, fs_stat);
_SCRUB_FS_STAT(p, csum_discards, fs_stat);
_SCRUB_FS_STAT(p, super_errors, fs_stat);
_SCRUB_FS_STAT(p, malloc_errors, fs_stat);
_SCRUB_FS_STAT(p, uncorrectable_errors, fs_stat);
_SCRUB_FS_STAT(p, corrected_errors, fs_stat);
_SCRUB_FS_STAT(p, last_physical, fs_stat);
_SCRUB_FS_STAT_ZMIN(ss, t_start, fs_stat);
_SCRUB_FS_STAT_ZMIN(ss, t_resumed, fs_stat);
_SCRUB_FS_STAT_ZMAX(ss, duration, fs_stat);
_SCRUB_FS_STAT_ZMAX(ss, canceled, fs_stat);
_SCRUB_FS_STAT_MIN(ss, finished, fs_stat);
}
static void init_fs_stat(struct scrub_fs_stat *fs_stat)
{
memset(fs_stat, 0, sizeof(*fs_stat));
fs_stat->s.finished = 1;
}
static void _print_scrub_ss(struct scrub_stats *ss)
{
char t[4096];
struct tm tm;
if (!ss || !ss->t_start) {
printf("\tno stats available\n");
return;
}
if (ss->t_resumed) {
localtime_r(&ss->t_resumed, &tm);
strftime(t, sizeof(t), "%c", &tm);
t[sizeof(t) - 1] = '\0';
printf("\tscrub resumed at %s", t);
} else {
localtime_r(&ss->t_start, &tm);
strftime(t, sizeof(t), "%c", &tm);
t[sizeof(t) - 1] = '\0';
printf("\tscrub started at %s", t);
}
if (ss->finished && !ss->canceled) {
printf(" and finished after %llu seconds\n",
ss->duration);
} else if (ss->canceled) {
printf(" and was aborted after %llu seconds\n",
ss->duration);
} else {
printf(", running for %llu seconds\n", ss->duration);
}
}
static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
struct btrfs_scrub_progress *p, int raw,
const char *append, struct scrub_stats *ss)
{
printf("scrub device %s (id %llu) %s\n", di->path, di->devid,
append ? append : "");
_print_scrub_ss(ss);
if (p) {
if (raw)
print_scrub_full(p);
else
print_scrub_summary(p);
}
}
static void print_fs_stat(struct scrub_fs_stat *fs_stat, int raw)
{
_print_scrub_ss(&fs_stat->s);
if (raw)
print_scrub_full(&fs_stat->p);
else
print_scrub_summary(&fs_stat->p);
}
static void free_history(struct scrub_file_record **last_scrubs)
{
struct scrub_file_record **l = last_scrubs;
if (!l || IS_ERR(l))
return;
while (*l)
free(*l++);
free(last_scrubs);
}
/*
* cancels a running scrub and makes the master process record the current
* progress status before exiting.
*/
static int cancel_fd = -1;
static void scrub_sigint_record_progress(int signal)
{
int ret;
ret = ioctl(cancel_fd, BTRFS_IOC_SCRUB_CANCEL, NULL);
if (ret < 0)
perror("Scrub cancel failed");
}
static int scrub_handle_sigint_parent(void)
{
struct sigaction sa = {
.sa_handler = SIG_IGN,
.sa_flags = SA_RESTART,
};
return sigaction(SIGINT, &sa, NULL);
}
static int scrub_handle_sigint_child(int fd)
{
struct sigaction sa = {
.sa_handler = fd == -1 ? SIG_DFL : scrub_sigint_record_progress,
};
cancel_fd = fd;
return sigaction(SIGINT, &sa, NULL);
}
static int scrub_datafile(const char *fn_base, const char *fn_local,
const char *fn_tmp, char *datafile, int size)
{
int ret;
int end = size - 2;
datafile[end + 1] = '\0';
strncpy(datafile, fn_base, end);
ret = strlen(datafile);
if (ret + 1 > end)
return -EOVERFLOW;
datafile[ret] = '.';
strncpy(datafile + ret + 1, fn_local, end - ret - 1);
ret = strlen(datafile);
if (ret + 1 > end)
return -EOVERFLOW;
if (fn_tmp) {
datafile[ret] = '_';
strncpy(datafile + ret + 1, fn_tmp, end - ret - 1);
ret = strlen(datafile);
if (ret > end)
return -EOVERFLOW;
}
return 0;
}
static int scrub_open_file(const char *datafile, int m)
{
int fd;
int ret;
fd = open(datafile, m, 0600);
if (fd < 0)
return -errno;
ret = flock(fd, LOCK_EX|LOCK_NB);
if (ret) {
ret = errno;
close(fd);
return -ret;
}
return fd;
}
static int scrub_open_file_r(const char *fn_base, const char *fn_local)
{
int ret;
char datafile[BTRFS_PATH_NAME_MAX + 1];
ret = scrub_datafile(fn_base, fn_local, NULL,
datafile, sizeof(datafile));
if (ret < 0)
return ret;
return scrub_open_file(datafile, O_RDONLY);
}
static int scrub_open_file_w(const char *fn_base, const char *fn_local,
const char *tmp)
{
int ret;
char datafile[BTRFS_PATH_NAME_MAX + 1];
ret = scrub_datafile(fn_base, fn_local, tmp,
datafile, sizeof(datafile));
if (ret < 0)
return ret;
return scrub_open_file(datafile, O_WRONLY|O_CREAT);
}
static int scrub_rename_file(const char *fn_base, const char *fn_local,
const char *tmp)
{
int ret;
char datafile_old[BTRFS_PATH_NAME_MAX + 1];
char datafile_new[BTRFS_PATH_NAME_MAX + 1];
ret = scrub_datafile(fn_base, fn_local, tmp,
datafile_old, sizeof(datafile_old));
if (ret < 0)
return ret;
ret = scrub_datafile(fn_base, fn_local, NULL,
datafile_new, sizeof(datafile_new));
if (ret < 0)
return ret;
ret = rename(datafile_old, datafile_new);
return ret ? -errno : 0;
}
#define _SCRUB_KVREAD(ret, i, name, avail, l, dest) if (ret == 0) { \
ret = scrub_kvread(i, sizeof(#name), avail, l, #name, dest.name); \
}
/*
* returns 0 if the key did not match (nothing was read)
* 1 if the key did match (success)
* -1 if the key did match and an error occured
*/
static int scrub_kvread(int *i, int len, int avail, const char *buf,
const char *key, u64 *dest)
{
int j;
if (*i + len + 1 < avail && strncmp(&buf[*i], key, len - 1) == 0) {
*i += len - 1;
if (buf[*i] != ':')
return -1;
*i += 1;
for (j = 0; isdigit(buf[*i + j]) && *i + j < avail; ++j)
;
if (*i + j >= avail)
return -1;
*dest = atoll(&buf[*i]);
*i += j;
return 1;
}
return 0;
}
#define _SCRUB_INVALID do { \
if (report_errors) \
fprintf(stderr, "WARNING: invalid data in line %d pos " \
"%d state %d (near \"%.*s\") at %s:%d\n", \
lineno, i, state, 20 > avail ? avail : 20, \
l + i, __FILE__, __LINE__); \
goto skip; \
} while (0)
static struct scrub_file_record **scrub_read_file(int fd, int report_errors)
{
int avail = 0;
int old_avail = 0;
char l[16 * 1024];
int state = 0;
int curr = -1;
int i = 0;
int j;
int ret;
int eof = 0;
int lineno = 0;
u64 version;
char empty_uuid[BTRFS_FSID_SIZE] = {0};
struct scrub_file_record **p = NULL;
again:
old_avail = avail - i;
BUG_ON(old_avail < 0);
if (old_avail)
memmove(l, l + i, old_avail);
avail = read(fd, l + old_avail, sizeof(l) - old_avail);
if (avail == 0)
eof = 1;
if (avail == 0 && old_avail == 0) {
if (curr >= 0 &&
memcmp(p[curr]->fsid, empty_uuid, BTRFS_FSID_SIZE) == 0) {
p[curr] = NULL;
} else if (curr == -1) {
p = ERR_PTR(-ENODATA);
}
return p;
}
if (avail == -1)
return ERR_PTR(-errno);
avail += old_avail;
i = 0;
while (i < avail) {
switch (state) {
case 0: /* start of file */
ret = scrub_kvread(&i,
sizeof(SCRUB_FILE_VERSION_PREFIX), avail, l,
SCRUB_FILE_VERSION_PREFIX, &version);
if (ret != 1)
_SCRUB_INVALID;
if (version != atoll(SCRUB_FILE_VERSION))
return ERR_PTR(-ENOTSUP);
state = 6;
continue;
case 1: /* start of line, alloc */
/*
* this state makes sure we have a complete line in
* further processing, so we don't need wrap-tracking
* everywhere.
*/
if (!eof && !memchr(l + i, '\n', avail - i))
goto again;
++lineno;
if (curr > -1 && memcmp(p[curr]->fsid, empty_uuid,
BTRFS_FSID_SIZE) == 0) {
state = 2;
continue;
}
++curr;
p = realloc(p, (curr + 2) * sizeof(*p));
if (p)
p[curr] = malloc(sizeof(**p));
if (!p || !p[curr])
return ERR_PTR(-errno);
memset(p[curr], 0, sizeof(**p));
p[curr + 1] = NULL;
++state;
/* fall through */
case 2: /* start of line, skip space */
while (isspace(l[i]) && i < avail) {
if (l[i] == '\n')
++lineno;
++i;
}
if (i >= avail ||
(!eof && !memchr(l + i, '\n', avail - i)))
goto again;
++state;
/* fall through */
case 3: /* read fsid */
if (i == avail)
continue;
for (j = 0; l[i + j] != ':' && i + j < avail; ++j)
;
if (i + j + 1 >= avail)
_SCRUB_INVALID;
if (j != BTRFS_UUID_UNPARSED_SIZE - 1)
_SCRUB_INVALID;
l[i + j] = '\0';
ret = uuid_parse(l + i, p[curr]->fsid);
if (ret)
_SCRUB_INVALID;
i += j + 1;
++state;
/* fall through */
case 4: /* read dev id */
for (j = 0; isdigit(l[i + j]) && i+j < avail; ++j)
;
if (j == 0 || i + j + 1 >= avail)
_SCRUB_INVALID;
p[curr]->devid = atoll(&l[i]);
i += j + 1;
++state;
/* fall through */
case 5: /* read key/value pair */
ret = 0;
_SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, tree_extents_scrubbed, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, data_bytes_scrubbed, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, tree_bytes_scrubbed, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, read_errors, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, csum_errors, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, verify_errors, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, no_csum, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, csum_discards, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, super_errors, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, malloc_errors, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, uncorrectable_errors, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, corrected_errors, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, last_physical, avail, l,
&p[curr]->p);
_SCRUB_KVREAD(ret, &i, finished, avail, l,
&p[curr]->stats);
_SCRUB_KVREAD(ret, &i, t_start, avail, l,
(u64 *)&p[curr]->stats);
_SCRUB_KVREAD(ret, &i, t_resumed, avail, l,
(u64 *)&p[curr]->stats);
_SCRUB_KVREAD(ret, &i, duration, avail, l,
(u64 *)&p[curr]->stats);
_SCRUB_KVREAD(ret, &i, canceled, avail, l,
&p[curr]->stats);
if (ret != 1)
_SCRUB_INVALID;
++state;
/* fall through */
case 6: /* after number */
if (l[i] == '|')
state = 5;
else if (l[i] == '\n')
state = 1;
else
_SCRUB_INVALID;
++i;
continue;
case 99: /* skip rest of line */
skip:
state = 99;
do {
++i;
if (l[i - 1] == '\n') {
state = 1;
break;
}
} while (i < avail);
continue;
}
BUG();
}
goto again;
}
static int scrub_write_buf(int fd, const void *data, int len)
{
int ret;
ret = write(fd, data, len);
return ret - len;
}
static int scrub_writev(int fd, char *buf, int max, const char *fmt, ...)
__attribute__ ((format (printf, 4, 5)));
static int scrub_writev(int fd, char *buf, int max, const char *fmt, ...)
{
int ret;
va_list args;
va_start(args, fmt);
ret = vsnprintf(buf, max, fmt, args);
va_end(args);
if (ret >= max)
return ret - max;
return scrub_write_buf(fd, buf, ret);
}
#define _SCRUB_SUM(dest, data, name) dest->scrub_args.progress.name = \
data->resumed->p.name + data->scrub_args.progress.name
static struct scrub_progress *scrub_resumed_stats(struct scrub_progress *data,
struct scrub_progress *dest)
{
if (!data->resumed || data->skip)
return data;
_SCRUB_SUM(dest, data, data_extents_scrubbed);
_SCRUB_SUM(dest, data, tree_extents_scrubbed);
_SCRUB_SUM(dest, data, data_bytes_scrubbed);
_SCRUB_SUM(dest, data, tree_bytes_scrubbed);
_SCRUB_SUM(dest, data, read_errors);
_SCRUB_SUM(dest, data, csum_errors);
_SCRUB_SUM(dest, data, verify_errors);
_SCRUB_SUM(dest, data, no_csum);
_SCRUB_SUM(dest, data, csum_discards);
_SCRUB_SUM(dest, data, super_errors);
_SCRUB_SUM(dest, data, malloc_errors);
_SCRUB_SUM(dest, data, uncorrectable_errors);
_SCRUB_SUM(dest, data, corrected_errors);
_SCRUB_SUM(dest, data, last_physical);
dest->stats.canceled = data->stats.canceled;
dest->stats.finished = data->stats.finished;
dest->stats.t_resumed = data->stats.t_start;
dest->stats.t_start = data->resumed->stats.t_start;
dest->stats.duration = data->resumed->stats.duration +
data->stats.duration;
dest->scrub_args.devid = data->scrub_args.devid;
return dest;
}
#define _SCRUB_KVWRITE(fd, buf, name, use) \
scrub_kvwrite(fd, buf, sizeof(buf), #name, \
use->scrub_args.progress.name)
#define _SCRUB_KVWRITE_STATS(fd, buf, name, use) \
scrub_kvwrite(fd, buf, sizeof(buf), #name, \
use->stats.name)
static int scrub_kvwrite(int fd, char *buf, int max, const char *key, u64 val)
{
return scrub_writev(fd, buf, max, "|%s:%lld", key, val);
}
static int scrub_write_file(int fd, const char *fsid,
struct scrub_progress *data, int n)
{
int ret = 0;
int i;
char buf[1024];
struct scrub_progress local;
struct scrub_progress *use;
if (n < 1)
return -EINVAL;
/* each -1 is to subtract one \0 byte, the + 2 is for ':' and '\n' */
ret = scrub_write_buf(fd, SCRUB_FILE_VERSION_PREFIX ":"
SCRUB_FILE_VERSION "\n",
(sizeof(SCRUB_FILE_VERSION_PREFIX) - 1) +
(sizeof(SCRUB_FILE_VERSION) - 1) + 2);
if (ret)
return -EOVERFLOW;
for (i = 0; i < n; ++i) {
use = scrub_resumed_stats(&data[i], &local);
if (scrub_write_buf(fd, fsid, strlen(fsid)) ||
scrub_write_buf(fd, ":", 1) ||
scrub_writev(fd, buf, sizeof(buf), "%lld",
use->scrub_args.devid) ||
scrub_write_buf(fd, buf, ret) ||
_SCRUB_KVWRITE(fd, buf, data_extents_scrubbed, use) ||
_SCRUB_KVWRITE(fd, buf, tree_extents_scrubbed, use) ||
_SCRUB_KVWRITE(fd, buf, data_bytes_scrubbed, use) ||
_SCRUB_KVWRITE(fd, buf, tree_bytes_scrubbed, use) ||
_SCRUB_KVWRITE(fd, buf, read_errors, use) ||
_SCRUB_KVWRITE(fd, buf, csum_errors, use) ||
_SCRUB_KVWRITE(fd, buf, verify_errors, use) ||
_SCRUB_KVWRITE(fd, buf, no_csum, use) ||
_SCRUB_KVWRITE(fd, buf, csum_discards, use) ||
_SCRUB_KVWRITE(fd, buf, super_errors, use) ||
_SCRUB_KVWRITE(fd, buf, malloc_errors, use) ||
_SCRUB_KVWRITE(fd, buf, uncorrectable_errors, use) ||
_SCRUB_KVWRITE(fd, buf, corrected_errors, use) ||
_SCRUB_KVWRITE(fd, buf, last_physical, use) ||
_SCRUB_KVWRITE_STATS(fd, buf, t_start, use) ||
_SCRUB_KVWRITE_STATS(fd, buf, t_resumed, use) ||
_SCRUB_KVWRITE_STATS(fd, buf, duration, use) ||
_SCRUB_KVWRITE_STATS(fd, buf, canceled, use) ||
_SCRUB_KVWRITE_STATS(fd, buf, finished, use) ||
scrub_write_buf(fd, "\n", 1)) {
return -EOVERFLOW;
}
}
return 0;
}
static int scrub_write_progress(pthread_mutex_t *m, const char *fsid,
struct scrub_progress *data, int n)
{
int ret;
int err;
int fd = -1;
int old;
ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
if (ret) {
err = -ret;
goto out3;
}
ret = pthread_mutex_lock(m);
if (ret) {
err = -ret;
goto out2;
}
fd = scrub_open_file_w(SCRUB_DATA_FILE, fsid, "tmp");
if (fd < 0) {
err = fd;
goto out1;
}
err = scrub_write_file(fd, fsid, data, n);
if (err)
goto out1;
err = scrub_rename_file(SCRUB_DATA_FILE, fsid, "tmp");
if (err)
goto out1;
out1:
if (fd >= 0) {
ret = close(fd);
if (ret)
err = -errno;
}
ret = pthread_mutex_unlock(m);
if (ret && !err)
err = -ret;
out2:
ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
if (ret && !err)
err = -ret;
out3:
return err;
}
static void *scrub_one_dev(void *ctx)
{
struct scrub_progress *sp = ctx;
int ret;
struct timeval tv;
sp->stats.canceled = 0;
sp->stats.duration = 0;
sp->stats.finished = 0;
ret = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0,
IOPRIO_PRIO_VALUE(sp->ioprio_class,
sp->ioprio_classdata));
if (ret)
fprintf(stderr,
"WARNING: setting ioprio failed: %s (ignored).\n",
strerror(errno));
ret = ioctl(sp->fd, BTRFS_IOC_SCRUB, &sp->scrub_args);
gettimeofday(&tv, NULL);
sp->ret = ret;
sp->stats.duration = tv.tv_sec - sp->stats.t_start;
sp->stats.canceled = !!ret;
sp->ioctl_errno = errno;
ret = pthread_mutex_lock(&sp->progress_mutex);
if (ret)
return ERR_PTR(-ret);
sp->stats.finished = 1;
ret = pthread_mutex_unlock(&sp->progress_mutex);
if (ret)
return ERR_PTR(-ret);
return NULL;
}
static void *progress_one_dev(void *ctx)
{
struct scrub_progress *sp = ctx;
sp->ret = ioctl(sp->fd, BTRFS_IOC_SCRUB_PROGRESS, &sp->scrub_args);
sp->ioctl_errno = errno;
return NULL;
}
/* nb: returns a negative errno via ERR_PTR */
static void *scrub_progress_cycle(void *ctx)
{
int ret = 0;
int perr = 0; /* positive / pthread error returns */
int old;
int i;
char fsid[BTRFS_UUID_UNPARSED_SIZE];
struct scrub_progress *sp;
struct scrub_progress *sp_last;
struct scrub_progress *sp_shared;
struct timeval tv;
struct scrub_progress_cycle *spc = ctx;
int ndev = spc->fi->num_devices;
int this = 1;
int last = 0;
int peer_fd = -1;
struct pollfd accept_poll_fd = {
.fd = spc->prg_fd,
.events = POLLIN,
.revents = 0,
};
struct pollfd write_poll_fd = {
.events = POLLOUT,
.revents = 0,
};
struct sockaddr_un peer;
socklen_t peer_size = sizeof(peer);
perr = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
if (perr)
goto out;
uuid_unparse(spc->fi->fsid, fsid);
for (i = 0; i < ndev; ++i) {
sp = &spc->progress[i];
sp_last = &spc->progress[i + ndev];
sp_shared = &spc->shared_progress[i];
sp->scrub_args.devid = sp_last->scrub_args.devid =
sp_shared->scrub_args.devid;
sp->fd = sp_last->fd = spc->fdmnt;
sp->stats.t_start = sp_last->stats.t_start =
sp_shared->stats.t_start;
sp->resumed = sp_last->resumed = sp_shared->resumed;
sp->skip = sp_last->skip = sp_shared->skip;
sp->stats.finished = sp_last->stats.finished =
sp_shared->stats.finished;
}
while (1) {
ret = poll(&accept_poll_fd, 1, 5 * 1000);
if (ret == -1) {
ret = -errno;
goto out;
}
if (ret)
peer_fd = accept(spc->prg_fd, (struct sockaddr *)&peer,
&peer_size);
gettimeofday(&tv, NULL);
this = (this + 1)%2;
last = (last + 1)%2;
for (i = 0; i < ndev; ++i) {
sp = &spc->progress[this * ndev + i];
sp_last = &spc->progress[last * ndev + i];
sp_shared = &spc->shared_progress[i];
if (sp->stats.finished)
continue;
progress_one_dev(sp);
sp->stats.duration = tv.tv_sec - sp->stats.t_start;
if (!sp->ret)
continue;
if (sp->ioctl_errno != ENOTCONN &&
sp->ioctl_errno != ENODEV) {
ret = -sp->ioctl_errno;
goto out;
}
/*
* scrub finished or device removed, check the
* finished flag. if unset, just use the last
* result we got for the current write and go
* on. flag should be set on next cycle, then.
*/
perr = pthread_setcancelstate(
PTHREAD_CANCEL_DISABLE, &old);
if (perr)
goto out;
perr = pthread_mutex_lock(&sp_shared->progress_mutex);
if (perr)
goto out;
if (!sp_shared->stats.finished) {
perr = pthread_mutex_unlock(
&sp_shared->progress_mutex);
if (perr)
goto out;
perr = pthread_setcancelstate(
PTHREAD_CANCEL_ENABLE, &old);
if (perr)
goto out;
memcpy(sp, sp_last, sizeof(*sp));
continue;
}
perr = pthread_mutex_unlock(&sp_shared->progress_mutex);
if (perr)
goto out;
perr = pthread_setcancelstate(
PTHREAD_CANCEL_ENABLE, &old);
if (perr)
goto out;
memcpy(sp, sp_shared, sizeof(*sp));
memcpy(sp_last, sp_shared, sizeof(*sp));
}
if (peer_fd != -1) {
write_poll_fd.fd = peer_fd;
ret = poll(&write_poll_fd, 1, 0);
if (ret == -1) {
ret = -errno;
goto out;
}
if (ret) {
ret = scrub_write_file(
peer_fd, fsid,
&spc->progress[this * ndev], ndev);
if (ret)
goto out;
}
close(peer_fd);
peer_fd = -1;
}
if (!spc->do_record)
continue;
ret = scrub_write_progress(spc->write_mutex, fsid,
&spc->progress[this * ndev], ndev);
if (ret)
goto out;
}
out:
if (peer_fd != -1)
close(peer_fd);