btrfs-progs: Error handling in scrub_progress_cycle() thread
[platform/upstream/btrfs-progs.git] / cmds-scrub.c
1 /*
2  * Copyright (C) 2011 STRATO.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include "kerncompat.h"
20
21 #include <sys/ioctl.h>
22 #include <sys/wait.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <poll.h>
28 #include <sys/file.h>
29 #include <uuid/uuid.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <pthread.h>
33 #include <ctype.h>
34 #include <signal.h>
35 #include <stdarg.h>
36
37 #include "ctree.h"
38 #include "ioctl.h"
39 #include "utils.h"
40 #include "volumes.h"
41 #include "disk-io.h"
42
43 #include "commands.h"
44
45 static const char * const scrub_cmd_group_usage[] = {
46         "btrfs scrub <command> [options] <path>|<device>",
47         NULL
48 };
49
50 #define SCRUB_DATA_FILE "/var/lib/btrfs/scrub.status"
51 #define SCRUB_PROGRESS_SOCKET_PATH "/var/lib/btrfs/scrub.progress"
52 #define SCRUB_FILE_VERSION_PREFIX "scrub status"
53 #define SCRUB_FILE_VERSION "1"
54
55 struct scrub_stats {
56         time_t t_start;
57         time_t t_resumed;
58         u64 duration;
59         u64 finished;
60         u64 canceled;
61 };
62
63 struct scrub_progress {
64         struct btrfs_ioctl_scrub_args scrub_args;
65         int fd;
66         int ret;
67         int skip;
68         struct scrub_stats stats;
69         struct scrub_file_record *resumed;
70         int ioctl_errno;
71         pthread_mutex_t progress_mutex;
72 };
73
74 struct scrub_file_record {
75         u8 fsid[BTRFS_FSID_SIZE];
76         u64 devid;
77         struct scrub_stats stats;
78         struct btrfs_scrub_progress p;
79 };
80
81 struct scrub_progress_cycle {
82         int fdmnt;
83         int prg_fd;
84         int do_record;
85         struct btrfs_ioctl_fs_info_args *fi;
86         struct scrub_progress *progress;
87         struct scrub_progress *shared_progress;
88         pthread_mutex_t *write_mutex;
89 };
90
91 struct scrub_fs_stat {
92         struct btrfs_scrub_progress p;
93         struct scrub_stats s;
94         int i;
95 };
96
97 static void print_scrub_full(struct btrfs_scrub_progress *sp)
98 {
99         printf("\tdata_extents_scrubbed: %lld\n", sp->data_extents_scrubbed);
100         printf("\ttree_extents_scrubbed: %lld\n", sp->tree_extents_scrubbed);
101         printf("\tdata_bytes_scrubbed: %lld\n", sp->data_bytes_scrubbed);
102         printf("\ttree_bytes_scrubbed: %lld\n", sp->tree_bytes_scrubbed);
103         printf("\tread_errors: %lld\n", sp->read_errors);
104         printf("\tcsum_errors: %lld\n", sp->csum_errors);
105         printf("\tverify_errors: %lld\n", sp->verify_errors);
106         printf("\tno_csum: %lld\n", sp->no_csum);
107         printf("\tcsum_discards: %lld\n", sp->csum_discards);
108         printf("\tsuper_errors: %lld\n", sp->super_errors);
109         printf("\tmalloc_errors: %lld\n", sp->malloc_errors);
110         printf("\tuncorrectable_errors: %lld\n", sp->uncorrectable_errors);
111         printf("\tunverified_errors: %lld\n", sp->unverified_errors);
112         printf("\tcorrected_errors: %lld\n", sp->corrected_errors);
113         printf("\tlast_physical: %lld\n", sp->last_physical);
114 }
115
116 #define ERR(test, ...) do {                     \
117         if (test)                               \
118                 fprintf(stderr, __VA_ARGS__);   \
119 } while (0)
120
121 #define PRINT_SCRUB_ERROR(test, desc) do {      \
122         if (test)                               \
123                 printf(" %s=%llu", desc, test); \
124 } while (0)
125
126 static void print_scrub_summary(struct btrfs_scrub_progress *p)
127 {
128         u64 err_cnt;
129         u64 err_cnt2;
130         char *bytes;
131
132         err_cnt = p->read_errors +
133                         p->csum_errors +
134                         p->verify_errors +
135                         p->super_errors;
136
137         err_cnt2 = p->corrected_errors + p->uncorrectable_errors;
138
139         if (p->malloc_errors)
140                 printf("*** WARNING: memory allocation failed while scrubbing. "
141                        "results may be inaccurate\n");
142         bytes = pretty_sizes(p->data_bytes_scrubbed + p->tree_bytes_scrubbed);
143         printf("\ttotal bytes scrubbed: %s with %llu errors\n", bytes,
144                 max(err_cnt, err_cnt2));
145         free(bytes);
146         if (err_cnt || err_cnt2) {
147                 printf("\terror details:");
148                 PRINT_SCRUB_ERROR(p->read_errors, "read");
149                 PRINT_SCRUB_ERROR(p->super_errors, "super");
150                 PRINT_SCRUB_ERROR(p->verify_errors, "verify");
151                 PRINT_SCRUB_ERROR(p->csum_errors, "csum");
152                 printf("\n");
153                 printf("\tcorrected errors: %llu, uncorrectable errors: %llu, "
154                         "unverified errors: %llu\n", p->corrected_errors,
155                         p->uncorrectable_errors, p->unverified_errors);
156         }
157 }
158
159 #define _SCRUB_FS_STAT(p, name, fs_stat) do {   \
160         fs_stat->p.name += p->name;             \
161 } while (0)
162
163 #define _SCRUB_FS_STAT_MIN(ss, name, fs_stat)   \
164 do {                                            \
165         if (fs_stat->s.name > ss->name) {       \
166                 fs_stat->s.name = ss->name;     \
167         }                                       \
168 } while (0)
169
170 #define _SCRUB_FS_STAT_ZMIN(ss, name, fs_stat)                  \
171 do {                                                            \
172         if (!fs_stat->s.name || fs_stat->s.name > ss->name) {   \
173                 fs_stat->s.name = ss->name;                     \
174         }                                                       \
175 } while (0)
176
177 #define _SCRUB_FS_STAT_ZMAX(ss, name, fs_stat)                          \
178 do {                                                                    \
179         if (!(fs_stat)->s.name || (fs_stat)->s.name < (ss)->name) {     \
180                 (fs_stat)->s.name = (ss)->name;                         \
181         }                                                               \
182 } while (0)
183
184 static void add_to_fs_stat(struct btrfs_scrub_progress *p,
185                                 struct scrub_stats *ss,
186                                 struct scrub_fs_stat *fs_stat)
187 {
188         _SCRUB_FS_STAT(p, data_extents_scrubbed, fs_stat);
189         _SCRUB_FS_STAT(p, tree_extents_scrubbed, fs_stat);
190         _SCRUB_FS_STAT(p, data_bytes_scrubbed, fs_stat);
191         _SCRUB_FS_STAT(p, tree_bytes_scrubbed, fs_stat);
192         _SCRUB_FS_STAT(p, read_errors, fs_stat);
193         _SCRUB_FS_STAT(p, csum_errors, fs_stat);
194         _SCRUB_FS_STAT(p, verify_errors, fs_stat);
195         _SCRUB_FS_STAT(p, no_csum, fs_stat);
196         _SCRUB_FS_STAT(p, csum_discards, fs_stat);
197         _SCRUB_FS_STAT(p, super_errors, fs_stat);
198         _SCRUB_FS_STAT(p, malloc_errors, fs_stat);
199         _SCRUB_FS_STAT(p, uncorrectable_errors, fs_stat);
200         _SCRUB_FS_STAT(p, corrected_errors, fs_stat);
201         _SCRUB_FS_STAT(p, last_physical, fs_stat);
202         _SCRUB_FS_STAT_ZMIN(ss, t_start, fs_stat);
203         _SCRUB_FS_STAT_ZMIN(ss, t_resumed, fs_stat);
204         _SCRUB_FS_STAT_ZMAX(ss, duration, fs_stat);
205         _SCRUB_FS_STAT_ZMAX(ss, canceled, fs_stat);
206         _SCRUB_FS_STAT_MIN(ss, finished, fs_stat);
207 }
208
209 static void init_fs_stat(struct scrub_fs_stat *fs_stat)
210 {
211         memset(fs_stat, 0, sizeof(*fs_stat));
212         fs_stat->s.finished = 1;
213 }
214
215 static void _print_scrub_ss(struct scrub_stats *ss)
216 {
217         char t[4096];
218         struct tm tm;
219
220         if (!ss || !ss->t_start) {
221                 printf("\tno stats available\n");
222                 return;
223         }
224         if (ss->t_resumed) {
225                 localtime_r(&ss->t_resumed, &tm);
226                 strftime(t, sizeof(t), "%c", &tm);
227                 t[sizeof(t) - 1] = '\0';
228                 printf("\tscrub resumed at %s", t);
229         } else {
230                 localtime_r(&ss->t_start, &tm);
231                 strftime(t, sizeof(t), "%c", &tm);
232                 t[sizeof(t) - 1] = '\0';
233                 printf("\tscrub started at %s", t);
234         }
235         if (ss->finished && !ss->canceled) {
236                 printf(" and finished after %llu seconds\n",
237                        ss->duration);
238         } else if (ss->canceled) {
239                 printf(" and was aborted after %llu seconds\n",
240                        ss->duration);
241         } else {
242                 printf(", running for %llu seconds\n", ss->duration);
243         }
244 }
245
246 static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
247                                 struct btrfs_scrub_progress *p, int raw,
248                                 const char *append, struct scrub_stats *ss)
249 {
250         printf("scrub device %s (id %llu) %s\n", di->path, di->devid,
251                append ? append : "");
252
253         _print_scrub_ss(ss);
254
255         if (p) {
256                 if (raw)
257                         print_scrub_full(p);
258                 else
259                         print_scrub_summary(p);
260         }
261 }
262
263 static void print_fs_stat(struct scrub_fs_stat *fs_stat, int raw)
264 {
265         _print_scrub_ss(&fs_stat->s);
266
267         if (raw)
268                 print_scrub_full(&fs_stat->p);
269         else
270                 print_scrub_summary(&fs_stat->p);
271 }
272
273 static void free_history(struct scrub_file_record **last_scrubs)
274 {
275         struct scrub_file_record **l = last_scrubs;
276         if (!l)
277                 return;
278         while (*l)
279                 free(*l++);
280         free(last_scrubs);
281 }
282
283 /*
284  * cancels a running scrub and makes the master process record the current
285  * progress status before exiting.
286  */
287 static int cancel_fd = -1;
288 static void scrub_sigint_record_progress(int signal)
289 {
290         int ret;
291
292         ret = ioctl(cancel_fd, BTRFS_IOC_SCRUB_CANCEL, NULL);
293         if (ret < 0)
294                 perror("Scrub cancel failed");
295 }
296
297 static int scrub_handle_sigint_parent(void)
298 {
299         struct sigaction sa = {
300                 .sa_handler = SIG_IGN,
301                 .sa_flags = SA_RESTART,
302         };
303
304         return sigaction(SIGINT, &sa, NULL);
305 }
306
307 static int scrub_handle_sigint_child(int fd)
308 {
309         struct sigaction sa = {
310                 .sa_handler = fd == -1 ? SIG_DFL : scrub_sigint_record_progress,
311         };
312
313         cancel_fd = fd;
314         return sigaction(SIGINT, &sa, NULL);
315 }
316
317 static int scrub_datafile(const char *fn_base, const char *fn_local,
318                                 const char *fn_tmp, char *datafile, int size)
319 {
320         int ret;
321         int end = size - 2;
322
323         datafile[end + 1] = '\0';
324         strncpy(datafile, fn_base, end);
325         ret = strlen(datafile);
326
327         if (ret + 1 > end)
328                 return -EOVERFLOW;
329
330         datafile[ret] = '.';
331         strncpy(datafile + ret + 1, fn_local, end - ret - 1);
332         ret = strlen(datafile);
333
334         if (ret + 1 > end)
335                 return -EOVERFLOW;
336
337         if (fn_tmp) {
338                 datafile[ret] = '_';
339                 strncpy(datafile + ret + 1, fn_tmp, end - ret - 1);
340                 ret = strlen(datafile);
341
342                 if (ret > end)
343                         return -EOVERFLOW;
344         }
345
346         return 0;
347 }
348
349 static int scrub_open_file(const char *datafile, int m)
350 {
351         int fd;
352         int ret;
353
354         fd = open(datafile, m, 0600);
355         if (fd < 0)
356                 return -errno;
357
358         ret = flock(fd, LOCK_EX|LOCK_NB);
359         if (ret) {
360                 ret = errno;
361                 close(fd);
362                 return -ret;
363         }
364
365         return fd;
366 }
367
368 static int scrub_open_file_r(const char *fn_base, const char *fn_local)
369 {
370         int ret;
371         char datafile[BTRFS_PATH_NAME_MAX + 1];
372         ret = scrub_datafile(fn_base, fn_local, NULL,
373                                 datafile, sizeof(datafile));
374         if (ret < 0)
375                 return ret;
376         return scrub_open_file(datafile, O_RDONLY);
377 }
378
379 static int scrub_open_file_w(const char *fn_base, const char *fn_local,
380                                 const char *tmp)
381 {
382         int ret;
383         char datafile[BTRFS_PATH_NAME_MAX + 1];
384         ret = scrub_datafile(fn_base, fn_local, tmp,
385                                 datafile, sizeof(datafile));
386         if (ret < 0)
387                 return ret;
388         return scrub_open_file(datafile, O_WRONLY|O_CREAT);
389 }
390
391 static int scrub_rename_file(const char *fn_base, const char *fn_local,
392                                 const char *tmp)
393 {
394         int ret;
395         char datafile_old[BTRFS_PATH_NAME_MAX + 1];
396         char datafile_new[BTRFS_PATH_NAME_MAX + 1];
397         ret = scrub_datafile(fn_base, fn_local, tmp,
398                                 datafile_old, sizeof(datafile_old));
399         if (ret < 0)
400                 return ret;
401         ret = scrub_datafile(fn_base, fn_local, NULL,
402                                 datafile_new, sizeof(datafile_new));
403         if (ret < 0)
404                 return ret;
405         ret = rename(datafile_old, datafile_new);
406         return ret ? -errno : 0;
407 }
408
409 #define _SCRUB_KVREAD(ret, i, name, avail, l, dest) if (ret == 0) {       \
410         ret = scrub_kvread(i, sizeof(#name), avail, l, #name, dest.name); \
411 }
412
413 /*
414  * returns 0 if the key did not match (nothing was read)
415  *         1 if the key did match (success)
416  *        -1 if the key did match and an error occured
417  */
418 static int scrub_kvread(int *i, int len, int avail, const char *buf,
419                         const char *key, u64 *dest)
420 {
421         int j;
422
423         if (*i + len + 1 < avail && strncmp(&buf[*i], key, len - 1) == 0) {
424                 *i += len - 1;
425                 if (buf[*i] != ':')
426                         return -1;
427                 *i += 1;
428                 for (j = 0; isdigit(buf[*i + j]) && *i + j < avail; ++j)
429                         ;
430                 if (*i + j >= avail)
431                         return -1;
432                 *dest = atoll(&buf[*i]);
433                 *i += j;
434                 return 1;
435         }
436
437         return 0;
438 }
439
440 #define _SCRUB_INVALID do {                                             \
441         if (report_errors)                                              \
442                 fprintf(stderr, "WARNING: invalid data in line %d pos " \
443                         "%d state %d (near \"%.*s\") at %s:%d\n",       \
444                         lineno, i, state, 20 > avail ? avail : 20,      \
445                         l + i,  __FILE__, __LINE__);                    \
446         goto skip;                                                      \
447 } while (0)
448
449 static struct scrub_file_record **scrub_read_file(int fd, int report_errors)
450 {
451         int avail = 0;
452         int old_avail = 0;
453         char l[16 * 1024];
454         int state = 0;
455         int curr = -1;
456         int i = 0;
457         int j;
458         int ret;
459         int eof = 0;
460         int lineno = 0;
461         u64 version;
462         char empty_uuid[BTRFS_FSID_SIZE] = {0};
463         struct scrub_file_record **p = NULL;
464
465         if (fd < 0)
466                 return ERR_PTR(-EINVAL);
467
468 again:
469         old_avail = avail - i;
470         BUG_ON(old_avail < 0);
471         if (old_avail)
472                 memmove(l, l + i, old_avail);
473         avail = read(fd, l + old_avail, sizeof(l) - old_avail);
474         if (avail == 0)
475                 eof = 1;
476         if (avail == 0 && old_avail == 0) {
477                 if (curr >= 0 &&
478                     memcmp(p[curr]->fsid, empty_uuid, BTRFS_FSID_SIZE) == 0) {
479                         p[curr] = NULL;
480                 } else if (curr == -1) {
481                         p = ERR_PTR(-ENODATA);
482                 }
483                 return p;
484         }
485         if (avail == -1)
486                 return ERR_PTR(-errno);
487         avail += old_avail;
488
489         i = 0;
490         while (i < avail) {
491                 switch (state) {
492                 case 0: /* start of file */
493                         ret = scrub_kvread(&i,
494                                 sizeof(SCRUB_FILE_VERSION_PREFIX), avail, l,
495                                 SCRUB_FILE_VERSION_PREFIX, &version);
496                         if (ret != 1)
497                                 _SCRUB_INVALID;
498                         if (version != atoll(SCRUB_FILE_VERSION))
499                                 return ERR_PTR(-ENOTSUP);
500                         state = 6;
501                         continue;
502                 case 1: /* start of line, alloc */
503                         /*
504                          * this state makes sure we have a complete line in
505                          * further processing, so we don't need wrap-tracking
506                          * everywhere.
507                          */
508                         if (!eof && !memchr(l + i, '\n', avail - i))
509                                 goto again;
510                         ++lineno;
511                         if (curr > -1 && memcmp(p[curr]->fsid, empty_uuid,
512                                                 BTRFS_FSID_SIZE) == 0) {
513                                 state = 2;
514                                 continue;
515                         }
516                         ++curr;
517                         p = realloc(p, (curr + 2) * sizeof(*p));
518                         if (p)
519                                 p[curr] = malloc(sizeof(**p));
520                         if (!p || !p[curr])
521                                 return ERR_PTR(-errno);
522                         memset(p[curr], 0, sizeof(**p));
523                         p[curr + 1] = NULL;
524                         ++state;
525                         /* fall through */
526                 case 2: /* start of line, skip space */
527                         while (isspace(l[i]) && i < avail) {
528                                 if (l[i] == '\n')
529                                         ++lineno;
530                                 ++i;
531                         }
532                         if (i >= avail ||
533                             (!eof && !memchr(l + i, '\n', avail - i)))
534                                 goto again;
535                         ++state;
536                         /* fall through */
537                 case 3: /* read fsid */
538                         if (i == avail)
539                                 continue;
540                         for (j = 0; l[i + j] != ':' && i + j < avail; ++j)
541                                 ;
542                         if (i + j + 1 >= avail)
543                                 _SCRUB_INVALID;
544                         if (j != 36)
545                                 _SCRUB_INVALID;
546                         l[i + j] = '\0';
547                         ret = uuid_parse(l + i, p[curr]->fsid);
548                         if (ret)
549                                 _SCRUB_INVALID;
550                         i += j + 1;
551                         ++state;
552                         /* fall through */
553                 case 4: /* read dev id */
554                         for (j = 0; isdigit(l[i + j]) && i+j < avail; ++j)
555                                 ;
556                         if (j == 0 || i + j + 1 >= avail)
557                                 _SCRUB_INVALID;
558                         p[curr]->devid = atoll(&l[i]);
559                         i += j + 1;
560                         ++state;
561                         /* fall through */
562                 case 5: /* read key/value pair */
563                         ret = 0;
564                         _SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
565                                         &p[curr]->p);
566                         _SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
567                                         &p[curr]->p);
568                         _SCRUB_KVREAD(ret, &i, tree_extents_scrubbed, avail, l,
569                                         &p[curr]->p);
570                         _SCRUB_KVREAD(ret, &i, data_bytes_scrubbed, avail, l,
571                                         &p[curr]->p);
572                         _SCRUB_KVREAD(ret, &i, tree_bytes_scrubbed, avail, l,
573                                         &p[curr]->p);
574                         _SCRUB_KVREAD(ret, &i, read_errors, avail, l,
575                                         &p[curr]->p);
576                         _SCRUB_KVREAD(ret, &i, csum_errors, avail, l,
577                                         &p[curr]->p);
578                         _SCRUB_KVREAD(ret, &i, verify_errors, avail, l,
579                                         &p[curr]->p);
580                         _SCRUB_KVREAD(ret, &i, no_csum, avail, l,
581                                         &p[curr]->p);
582                         _SCRUB_KVREAD(ret, &i, csum_discards, avail, l,
583                                         &p[curr]->p);
584                         _SCRUB_KVREAD(ret, &i, super_errors, avail, l,
585                                         &p[curr]->p);
586                         _SCRUB_KVREAD(ret, &i, malloc_errors, avail, l,
587                                         &p[curr]->p);
588                         _SCRUB_KVREAD(ret, &i, uncorrectable_errors, avail, l,
589                                         &p[curr]->p);
590                         _SCRUB_KVREAD(ret, &i, corrected_errors, avail, l,
591                                         &p[curr]->p);
592                         _SCRUB_KVREAD(ret, &i, last_physical, avail, l,
593                                         &p[curr]->p);
594                         _SCRUB_KVREAD(ret, &i, finished, avail, l,
595                                         &p[curr]->stats);
596                         _SCRUB_KVREAD(ret, &i, t_start, avail, l,
597                                         (u64 *)&p[curr]->stats);
598                         _SCRUB_KVREAD(ret, &i, t_resumed, avail, l,
599                                         (u64 *)&p[curr]->stats);
600                         _SCRUB_KVREAD(ret, &i, duration, avail, l,
601                                         (u64 *)&p[curr]->stats);
602                         _SCRUB_KVREAD(ret, &i, canceled, avail, l,
603                                         &p[curr]->stats);
604                         if (ret != 1)
605                                 _SCRUB_INVALID;
606                         ++state;
607                         /* fall through */
608                 case 6: /* after number */
609                         if (l[i] == '|')
610                                 state = 5;
611                         else if (l[i] == '\n')
612                                 state = 1;
613                         else
614                                 _SCRUB_INVALID;
615                         ++i;
616                         continue;
617                 case 99: /* skip rest of line */
618 skip:
619                         state = 99;
620                         do {
621                                 ++i;
622                                 if (l[i - 1] == '\n') {
623                                         state = 1;
624                                         break;
625                                 }
626                         } while (i < avail);
627                         continue;
628                 }
629                 BUG();
630         }
631         goto again;
632 }
633
634 static int scrub_write_buf(int fd, const void *data, int len)
635 {
636         int ret;
637         ret = write(fd, data, len);
638         return ret - len;
639 }
640
641 static int scrub_writev(int fd, char *buf, int max, const char *fmt, ...)
642                                 __attribute__ ((format (printf, 4, 5)));
643 static int scrub_writev(int fd, char *buf, int max, const char *fmt, ...)
644 {
645         int ret;
646         va_list args;
647
648         va_start(args, fmt);
649         ret = vsnprintf(buf, max, fmt, args);
650         va_end(args);
651         if (ret >= max)
652                 return ret - max;
653         return scrub_write_buf(fd, buf, ret);
654 }
655
656 #define _SCRUB_SUM(dest, data, name) dest->scrub_args.progress.name =   \
657                         data->resumed->p.name + data->scrub_args.progress.name
658
659 static struct scrub_progress *scrub_resumed_stats(struct scrub_progress *data,
660                                                   struct scrub_progress *dest)
661 {
662         if (!data->resumed || data->skip)
663                 return data;
664
665         _SCRUB_SUM(dest, data, data_extents_scrubbed);
666         _SCRUB_SUM(dest, data, tree_extents_scrubbed);
667         _SCRUB_SUM(dest, data, data_bytes_scrubbed);
668         _SCRUB_SUM(dest, data, tree_bytes_scrubbed);
669         _SCRUB_SUM(dest, data, read_errors);
670         _SCRUB_SUM(dest, data, csum_errors);
671         _SCRUB_SUM(dest, data, verify_errors);
672         _SCRUB_SUM(dest, data, no_csum);
673         _SCRUB_SUM(dest, data, csum_discards);
674         _SCRUB_SUM(dest, data, super_errors);
675         _SCRUB_SUM(dest, data, malloc_errors);
676         _SCRUB_SUM(dest, data, uncorrectable_errors);
677         _SCRUB_SUM(dest, data, corrected_errors);
678         _SCRUB_SUM(dest, data, last_physical);
679         dest->stats.canceled = data->stats.canceled;
680         dest->stats.finished = data->stats.finished;
681         dest->stats.t_resumed = data->stats.t_start;
682         dest->stats.t_start = data->resumed->stats.t_start;
683         dest->stats.duration = data->resumed->stats.duration +
684                                                         data->stats.duration;
685         dest->scrub_args.devid = data->scrub_args.devid;
686         return dest;
687 }
688
689 #define _SCRUB_KVWRITE(fd, buf, name, use)              \
690         scrub_kvwrite(fd, buf, sizeof(buf), #name,      \
691                         use->scrub_args.progress.name)
692
693 #define _SCRUB_KVWRITE_STATS(fd, buf, name, use)        \
694         scrub_kvwrite(fd, buf, sizeof(buf), #name,      \
695                         use->stats.name)
696
697 static int scrub_kvwrite(int fd, char *buf, int max, const char *key, u64 val)
698 {
699         return scrub_writev(fd, buf, max, "|%s:%lld", key, val);
700 }
701
702 static int scrub_write_file(int fd, const char *fsid,
703                                 struct scrub_progress *data, int n)
704 {
705         int ret = 0;
706         int i;
707         char buf[1024];
708         struct scrub_progress local;
709         struct scrub_progress *use;
710
711         if (n < 1)
712                 return -EINVAL;
713
714         /* each -1 is to subtract one \0 byte, the + 2 is for ':' and '\n' */
715         ret = scrub_write_buf(fd, SCRUB_FILE_VERSION_PREFIX ":"
716                                 SCRUB_FILE_VERSION "\n",
717                                 (sizeof(SCRUB_FILE_VERSION_PREFIX) - 1) +
718                                 (sizeof(SCRUB_FILE_VERSION) - 1) + 2);
719         if (ret)
720                 return -EOVERFLOW;
721
722         for (i = 0; i < n; ++i) {
723                 use = scrub_resumed_stats(&data[i], &local);
724                 if (scrub_write_buf(fd, fsid, strlen(fsid)) ||
725                     scrub_write_buf(fd, ":", 1) ||
726                     scrub_writev(fd, buf, sizeof(buf), "%lld",
727                                         use->scrub_args.devid) ||
728                     scrub_write_buf(fd, buf, ret) ||
729                     _SCRUB_KVWRITE(fd, buf, data_extents_scrubbed, use) ||
730                     _SCRUB_KVWRITE(fd, buf, tree_extents_scrubbed, use) ||
731                     _SCRUB_KVWRITE(fd, buf, data_bytes_scrubbed, use) ||
732                     _SCRUB_KVWRITE(fd, buf, tree_bytes_scrubbed, use) ||
733                     _SCRUB_KVWRITE(fd, buf, read_errors, use) ||
734                     _SCRUB_KVWRITE(fd, buf, csum_errors, use) ||
735                     _SCRUB_KVWRITE(fd, buf, verify_errors, use) ||
736                     _SCRUB_KVWRITE(fd, buf, no_csum, use) ||
737                     _SCRUB_KVWRITE(fd, buf, csum_discards, use) ||
738                     _SCRUB_KVWRITE(fd, buf, super_errors, use) ||
739                     _SCRUB_KVWRITE(fd, buf, malloc_errors, use) ||
740                     _SCRUB_KVWRITE(fd, buf, uncorrectable_errors, use) ||
741                     _SCRUB_KVWRITE(fd, buf, corrected_errors, use) ||
742                     _SCRUB_KVWRITE(fd, buf, last_physical, use) ||
743                     _SCRUB_KVWRITE_STATS(fd, buf, t_start, use) ||
744                     _SCRUB_KVWRITE_STATS(fd, buf, t_resumed, use) ||
745                     _SCRUB_KVWRITE_STATS(fd, buf, duration, use) ||
746                     _SCRUB_KVWRITE_STATS(fd, buf, canceled, use) ||
747                     _SCRUB_KVWRITE_STATS(fd, buf, finished, use) ||
748                     scrub_write_buf(fd, "\n", 1)) {
749                         return -EOVERFLOW;
750                 }
751         }
752
753         return 0;
754 }
755
756 static int scrub_write_progress(pthread_mutex_t *m, const char *fsid,
757                                 struct scrub_progress *data, int n)
758 {
759         int ret;
760         int err;
761         int fd = -1;
762         int old;
763
764         ret = pthread_mutex_lock(m);
765         if (ret) {
766                 err = -errno;
767                 goto out;
768         }
769
770         ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
771         if (ret) {
772                 err = -ret;
773                 goto out;
774         }
775
776         fd = scrub_open_file_w(SCRUB_DATA_FILE, fsid, "tmp");
777         if (fd < 0) {
778                 err = fd;
779                 goto out;
780         }
781         err = scrub_write_file(fd, fsid, data, n);
782         if (err)
783                 goto out;
784         err = scrub_rename_file(SCRUB_DATA_FILE, fsid, "tmp");
785         if (err)
786                 goto out;
787
788 out:
789         if (fd >= 0) {
790                 ret = close(fd);
791                 if (ret)
792                         err = -errno;
793         }
794
795         ret = pthread_mutex_unlock(m);
796         if (ret && !err)
797                 err = -ret;
798
799         ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
800         if (ret && !err)
801                 err = -ret;
802
803         return err;
804 }
805
806 static void *scrub_one_dev(void *ctx)
807 {
808         struct scrub_progress *sp = ctx;
809         int ret;
810         struct timeval tv;
811
812         sp->stats.canceled = 0;
813         sp->stats.duration = 0;
814         sp->stats.finished = 0;
815
816         ret = ioctl(sp->fd, BTRFS_IOC_SCRUB, &sp->scrub_args);
817         gettimeofday(&tv, NULL);
818         sp->ret = ret;
819         sp->stats.duration = tv.tv_sec - sp->stats.t_start;
820         sp->stats.canceled = !!ret;
821         sp->ioctl_errno = errno;
822         ret = pthread_mutex_lock(&sp->progress_mutex);
823         if (ret)
824                 return ERR_PTR(-ret);
825         sp->stats.finished = 1;
826         ret = pthread_mutex_unlock(&sp->progress_mutex);
827         if (ret)
828                 return ERR_PTR(-ret);
829
830         return NULL;
831 }
832
833 static void *progress_one_dev(void *ctx)
834 {
835         struct scrub_progress *sp = ctx;
836
837         sp->ret = ioctl(sp->fd, BTRFS_IOC_SCRUB_PROGRESS, &sp->scrub_args);
838         sp->ioctl_errno = errno;
839
840         return NULL;
841 }
842
843 /* nb: returns a negative errno via ERR_PTR */
844 static void *scrub_progress_cycle(void *ctx)
845 {
846         int ret;
847         int  perr = 0;  /* positive / pthread error returns */
848         int old;
849         int i;
850         char fsid[37];
851         struct scrub_progress *sp;
852         struct scrub_progress *sp_last;
853         struct scrub_progress *sp_shared;
854         struct timeval tv;
855         struct scrub_progress_cycle *spc = ctx;
856         int ndev = spc->fi->num_devices;
857         int this = 1;
858         int last = 0;
859         int peer_fd = -1;
860         struct pollfd accept_poll_fd = {
861                 .fd = spc->prg_fd,
862                 .events = POLLIN,
863                 .revents = 0,
864         };
865         struct pollfd write_poll_fd = {
866                 .events = POLLOUT,
867                 .revents = 0,
868         };
869         struct sockaddr_un peer;
870         socklen_t peer_size = sizeof(peer);
871
872         perr = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
873         if (perr)
874                 goto out;
875
876         uuid_unparse(spc->fi->fsid, fsid);
877
878         for (i = 0; i < ndev; ++i) {
879                 sp = &spc->progress[i];
880                 sp_last = &spc->progress[i + ndev];
881                 sp_shared = &spc->shared_progress[i];
882                 sp->scrub_args.devid = sp_last->scrub_args.devid =
883                                                 sp_shared->scrub_args.devid;
884                 sp->fd = sp_last->fd = spc->fdmnt;
885                 sp->stats.t_start = sp_last->stats.t_start =
886                                                 sp_shared->stats.t_start;
887                 sp->resumed = sp_last->resumed = sp_shared->resumed;
888                 sp->skip = sp_last->skip = sp_shared->skip;
889                 sp->stats.finished = sp_last->stats.finished =
890                                                 sp_shared->stats.finished;
891         }
892
893         while (1) {
894                 ret = poll(&accept_poll_fd, 1, 5 * 1000);
895                 if (ret == -1) {
896                         ret = -errno;
897                         goto out;
898                 }
899                 if (ret)
900                         peer_fd = accept(spc->prg_fd, (struct sockaddr *)&peer,
901                                          &peer_size);
902                 gettimeofday(&tv, NULL);
903                 this = (this + 1)%2;
904                 last = (last + 1)%2;
905                 for (i = 0; i < ndev; ++i) {
906                         sp = &spc->progress[this * ndev + i];
907                         sp_last = &spc->progress[last * ndev + i];
908                         sp_shared = &spc->shared_progress[i];
909                         if (sp->stats.finished)
910                                 continue;
911                         progress_one_dev(sp);
912                         sp->stats.duration = tv.tv_sec - sp->stats.t_start;
913                         if (!sp->ret)
914                                 continue;
915                         if (sp->ioctl_errno != ENOTCONN &&
916                             sp->ioctl_errno != ENODEV) {
917                                 ret = -sp->ioctl_errno;
918                                 goto out;
919                         }
920                         /*
921                          * scrub finished or device removed, check the
922                          * finished flag. if unset, just use the last
923                          * result we got for the current write and go
924                          * on. flag should be set on next cycle, then.
925                          */
926                         perr = pthread_mutex_lock(&sp_shared->progress_mutex);
927                         if (perr)
928                                 goto out;
929                         if (!sp_shared->stats.finished) {
930                                 perr = pthread_mutex_unlock(
931                                                 &sp_shared->progress_mutex);
932                                 if (perr)
933                                         goto out;
934                                 memcpy(sp, sp_last, sizeof(*sp));
935                                 continue;
936                         }
937                         perr = pthread_mutex_unlock(&sp_shared->progress_mutex);
938                         if (perr)
939                                 goto out;
940                         memcpy(sp, sp_shared, sizeof(*sp));
941                         memcpy(sp_last, sp_shared, sizeof(*sp));
942                 }
943                 if (peer_fd != -1) {
944                         write_poll_fd.fd = peer_fd;
945                         ret = poll(&write_poll_fd, 1, 0);
946                         if (ret == -1) {
947                                 ret = -errno;
948                                 goto out;
949                         }
950                         if (ret) {
951                                 ret = scrub_write_file(
952                                         peer_fd, fsid,
953                                         &spc->progress[this * ndev], ndev);
954                                 if (ret)
955                                         goto out;
956                         }
957                         close(peer_fd);
958                         peer_fd = -1;
959                 }
960                 if (!spc->do_record)
961                         continue;
962                 ret = scrub_write_progress(spc->write_mutex, fsid,
963                                            &spc->progress[this * ndev], ndev);
964                 if (ret)
965                         goto out;
966         }
967 out:
968         if (peer_fd != -1)
969                 close(peer_fd);
970         if (perr)
971                 ret = -perr;
972         return ERR_PTR(ret);
973 }
974
975 static struct scrub_file_record *last_dev_scrub(
976                 struct scrub_file_record *const *const past_scrubs, u64 devid)
977 {
978         int i;
979
980         if (!past_scrubs || IS_ERR(past_scrubs))
981                 return NULL;
982
983         for (i = 0; past_scrubs[i]; ++i)
984                 if (past_scrubs[i]->devid == devid)
985                         return past_scrubs[i];
986
987         return NULL;
988 }
989
990 int mkdir_p(char *path)
991 {
992         int i;
993         int ret;
994
995         for (i = 1; i < strlen(path); ++i) {
996                 if (path[i] != '/')
997                         continue;
998                 path[i] = '\0';
999                 ret = mkdir(path, 0777);
1000                 if (ret && errno != EEXIST)
1001                         return 1;
1002                 path[i] = '/';
1003         }
1004
1005         return 0;
1006 }
1007
1008 static const char * const cmd_scrub_start_usage[];
1009 static const char * const cmd_scrub_resume_usage[];
1010
1011 static int scrub_start(int argc, char **argv, int resume)
1012 {
1013         int fdmnt;
1014         int prg_fd = -1;
1015         int fdres = -1;
1016         int ret;
1017         pid_t pid;
1018         int c;
1019         int i;
1020         int err = 0;
1021         int e_uncorrectable = 0;
1022         int e_correctable = 0;
1023         int print_raw = 0;
1024         char *path;
1025         int do_background = 1;
1026         int do_wait = 0;
1027         int do_print = 0;
1028         int do_quiet = 0;
1029         int do_record = 1;
1030         int readonly = 0;
1031         int do_stats_per_dev = 0;
1032         int n_start = 0;
1033         int n_skip = 0;
1034         int n_resume = 0;
1035         struct btrfs_ioctl_fs_info_args fi_args;
1036         struct btrfs_ioctl_dev_info_args *di_args = NULL;
1037         struct scrub_progress *sp = NULL;
1038         struct scrub_fs_stat fs_stat;
1039         struct timeval tv;
1040         struct sockaddr_un addr = {
1041                 .sun_family = AF_UNIX,
1042         };
1043         pthread_t *t_devs = NULL;
1044         pthread_t t_prog;
1045         pthread_attr_t t_attr;
1046         struct scrub_file_record **past_scrubs = NULL;
1047         struct scrub_file_record *last_scrub = NULL;
1048         char *datafile = strdup(SCRUB_DATA_FILE);
1049         char fsid[37];
1050         char sock_path[BTRFS_PATH_NAME_MAX + 1] = "";
1051         struct scrub_progress_cycle spc;
1052         pthread_mutex_t spc_write_mutex = PTHREAD_MUTEX_INITIALIZER;
1053         void *terr;
1054         u64 devid;
1055
1056         optind = 1;
1057         while ((c = getopt(argc, argv, "BdqrR")) != -1) {
1058                 switch (c) {
1059                 case 'B':
1060                         do_background = 0;
1061                         do_wait = 1;
1062                         do_print = 1;
1063                         break;
1064                 case 'd':
1065                         do_stats_per_dev = 1;
1066                         break;
1067                 case 'q':
1068                         do_quiet = 1;
1069                         break;
1070                 case 'r':
1071                         readonly = 1;
1072                         break;
1073                 case 'R':
1074                         print_raw = 1;
1075                         break;
1076                 case '?':
1077                 default:
1078                         usage(resume ? cmd_scrub_resume_usage :
1079                                                 cmd_scrub_start_usage);
1080                 }
1081         }
1082
1083         /* try to catch most error cases before forking */
1084
1085         if (check_argc_exact(argc - optind, 1)) {
1086                 usage(resume ? cmd_scrub_resume_usage :
1087                                         cmd_scrub_start_usage);
1088         }
1089
1090         spc.progress = NULL;
1091         if (do_quiet && do_print)
1092                 do_print = 0;
1093
1094         if (mkdir_p(datafile)) {
1095                 ERR(!do_quiet, "WARNING: cannot create scrub data "
1096                                "file, mkdir %s failed: %s. Status recording "
1097                                "disabled\n", datafile, strerror(errno));
1098                 do_record = 0;
1099         }
1100         free(datafile);
1101
1102         path = argv[optind];
1103
1104         fdmnt = open_file_or_dir(path);
1105         if (fdmnt < 0) {
1106                 ERR(!do_quiet, "ERROR: can't access '%s'\n", path);
1107                 return 12;
1108         }
1109
1110         ret = get_fs_info(fdmnt, path, &fi_args, &di_args);
1111         if (ret) {
1112                 ERR(!do_quiet, "ERROR: getting dev info for scrub failed: "
1113                     "%s\n", strerror(-ret));
1114                 err = 1;
1115                 goto out;
1116         }
1117         if (!fi_args.num_devices) {
1118                 ERR(!do_quiet, "ERROR: no devices found\n");
1119                 err = 1;
1120                 goto out;
1121         }
1122
1123         uuid_unparse(fi_args.fsid, fsid);
1124         fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid);
1125         if (fdres < 0 && fdres != -ENOENT) {
1126                 ERR(!do_quiet, "WARNING: failed to open status file: "
1127                     "%s\n", strerror(-fdres));
1128         } else if (fdres >= 0) {
1129                 past_scrubs = scrub_read_file(fdres, !do_quiet);
1130                 if (IS_ERR(past_scrubs))
1131                         ERR(!do_quiet, "WARNING: failed to read status file: "
1132                             "%s\n", strerror(-PTR_ERR(past_scrubs)));
1133                 close(fdres);
1134         }
1135
1136         t_devs = malloc(fi_args.num_devices * sizeof(*t_devs));
1137         sp = calloc(fi_args.num_devices, sizeof(*sp));
1138         spc.progress = calloc(fi_args.num_devices * 2, sizeof(*spc.progress));
1139
1140         if (!t_devs || !sp || !spc.progress) {
1141                 ERR(!do_quiet, "ERROR: scrub failed: %s", strerror(errno));
1142                 err = 1;
1143                 goto out;
1144         }
1145
1146         ret = pthread_attr_init(&t_attr);
1147         if (ret) {
1148                 ERR(!do_quiet, "ERROR: pthread_attr_init failed: %s\n",
1149                     strerror(ret));
1150                 err = 1;
1151                 goto out;
1152         }
1153
1154         for (i = 0; i < fi_args.num_devices; ++i) {
1155                 devid = di_args[i].devid;
1156                 ret = pthread_mutex_init(&sp[i].progress_mutex, NULL);
1157                 if (ret) {
1158                         ERR(!do_quiet, "ERROR: pthread_mutex_init failed: "
1159                             "%s\n", strerror(ret));
1160                         err = 1;
1161                         goto out;
1162                 }
1163                 last_scrub = last_dev_scrub(past_scrubs, devid);
1164                 sp[i].scrub_args.devid = devid;
1165                 sp[i].fd = fdmnt;
1166                 if (resume && last_scrub && (last_scrub->stats.canceled ||
1167                                              !last_scrub->stats.finished)) {
1168                         ++n_resume;
1169                         sp[i].scrub_args.start = last_scrub->p.last_physical;
1170                         sp[i].resumed = last_scrub;
1171                 } else if (resume) {
1172                         ++n_skip;
1173                         sp[i].skip = 1;
1174                         sp[i].resumed = last_scrub;
1175                         continue;
1176                 } else {
1177                         ++n_start;
1178                         sp[i].scrub_args.start = 0ll;
1179                         sp[i].resumed = NULL;
1180                 }
1181                 sp[i].skip = 0;
1182                 sp[i].scrub_args.end = (u64)-1ll;
1183                 sp[i].scrub_args.flags = readonly ? BTRFS_SCRUB_READONLY : 0;
1184         }
1185
1186         if (!n_start && !n_resume) {
1187                 if (!do_quiet)
1188                         printf("scrub: nothing to resume for %s, fsid %s\n",
1189                                path, fsid);
1190                 err = 0;
1191                 goto out;
1192         }
1193
1194         ret = prg_fd = socket(AF_UNIX, SOCK_STREAM, 0);
1195         while (ret != -1) {
1196                 ret = scrub_datafile(SCRUB_PROGRESS_SOCKET_PATH, fsid, NULL,
1197                                         sock_path, sizeof(sock_path));
1198                 /* ignore EOVERFLOW, try using a shorter path for the socket */
1199                 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
1200                 strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);
1201                 ret = bind(prg_fd, (struct sockaddr *)&addr, sizeof(addr));
1202                 if (ret != -1 || errno != EADDRINUSE)
1203                         break;
1204                 /*
1205                  * bind failed with EADDRINUSE. so let's see if anyone answers
1206                  * when we make a call to the socket ...
1207                  */
1208                 ret = connect(prg_fd, (struct sockaddr *)&addr, sizeof(addr));
1209                 if (!ret || errno != ECONNREFUSED) {
1210                         /* ... yes, so scrub must be running. error out */
1211                         fprintf(stderr, "ERROR: scrub already running\n");
1212                         close(prg_fd);
1213                         prg_fd = -1;
1214                         goto out;
1215                 }
1216                 /*
1217                  * ... no, this means someone left us alone with an unused
1218                  * socket in the file system. remove it and try again.
1219                  */
1220                 ret = unlink(sock_path);
1221         }
1222         if (ret != -1)
1223                 ret = listen(prg_fd, 100);
1224         if (ret == -1) {
1225                 ERR(!do_quiet, "WARNING: failed to open the progress status "
1226                     "socket at %s: %s. Progress cannot be queried\n",
1227                     sock_path[0] ? sock_path : SCRUB_PROGRESS_SOCKET_PATH,
1228                     strerror(errno));
1229                 if (prg_fd != -1) {
1230                         close(prg_fd);
1231                         prg_fd = -1;
1232                         if (sock_path[0])
1233                                 unlink(sock_path);
1234                 }
1235         }
1236
1237         if (do_record) {
1238                 /* write all-zero progress file for a start */
1239                 ret = scrub_write_progress(&spc_write_mutex, fsid, sp,
1240                                            fi_args.num_devices);
1241                 if (ret) {
1242                         ERR(!do_quiet, "WARNING: failed to write the progress "
1243                             "status file: %s. Status recording disabled\n",
1244                             strerror(-ret));
1245                         do_record = 0;
1246                 }
1247         }
1248
1249         if (do_background) {
1250                 pid = fork();
1251                 if (pid == -1) {
1252                         ERR(!do_quiet, "ERROR: cannot scrub, fork failed: "
1253                                         "%s\n", strerror(errno));
1254                         err = 1;
1255                         goto out;
1256                 }
1257
1258                 if (pid) {
1259                         int stat;
1260                         scrub_handle_sigint_parent();
1261                         if (!do_quiet)
1262                                 printf("scrub %s on %s, fsid %s (pid=%d)\n",
1263                                        n_start ? "started" : "resumed",
1264                                        path, fsid, pid);
1265                         if (!do_wait) {
1266                                 err = 0;
1267                                 goto out;
1268                         }
1269                         ret = wait(&stat);
1270                         if (ret != pid) {
1271                                 ERR(!do_quiet, "ERROR: wait failed: (ret=%d) "
1272                                     "%s\n", ret, strerror(errno));
1273                                 err = 1;
1274                                 goto out;
1275                         }
1276                         if (!WIFEXITED(stat) || WEXITSTATUS(stat)) {
1277                                 ERR(!do_quiet, "ERROR: scrub process failed\n");
1278                                 err = WIFEXITED(stat) ? WEXITSTATUS(stat) : -1;
1279                                 goto out;
1280                         }
1281                         err = 0;
1282                         goto out;
1283                 }
1284         }
1285
1286         scrub_handle_sigint_child(fdmnt);
1287
1288         for (i = 0; i < fi_args.num_devices; ++i) {
1289                 if (sp[i].skip) {
1290                         sp[i].scrub_args.progress = sp[i].resumed->p;
1291                         sp[i].stats = sp[i].resumed->stats;
1292                         sp[i].ret = 0;
1293                         sp[i].stats.finished = 1;
1294                         continue;
1295                 }
1296                 devid = di_args[i].devid;
1297                 gettimeofday(&tv, NULL);
1298                 sp[i].stats.t_start = tv.tv_sec;
1299                 ret = pthread_create(&t_devs[i], &t_attr,
1300                                         scrub_one_dev, &sp[i]);
1301                 if (ret) {
1302                         if (do_print)
1303                                 fprintf(stderr, "ERROR: creating "
1304                                         "scrub_one_dev[%llu] thread failed: "
1305                                         "%s\n", devid, strerror(ret));
1306                         err = 1;
1307                         goto out;
1308                 }
1309         }
1310
1311         spc.fdmnt = fdmnt;
1312         spc.prg_fd = prg_fd;
1313         spc.do_record = do_record;
1314         spc.write_mutex = &spc_write_mutex;
1315         spc.shared_progress = sp;
1316         spc.fi = &fi_args;
1317         ret = pthread_create(&t_prog, &t_attr, scrub_progress_cycle, &spc);
1318         if (ret) {
1319                 if (do_print)
1320                         fprintf(stderr, "ERROR: creating progress thread "
1321                                 "failed: %s\n", strerror(ret));
1322                 err = 1;
1323                 goto out;
1324         }
1325
1326         err = 0;
1327         for (i = 0; i < fi_args.num_devices; ++i) {
1328                 if (sp[i].skip)
1329                         continue;
1330                 devid = di_args[i].devid;
1331                 ret = pthread_join(t_devs[i], NULL);
1332                 if (ret) {
1333                         if (do_print)
1334                                 fprintf(stderr, "ERROR: pthread_join failed "
1335                                         "for scrub_one_dev[%llu]: %s\n", devid,
1336                                         strerror(ret));
1337                         ++err;
1338                         continue;
1339                 }
1340                 if (sp[i].ret && sp[i].ioctl_errno == ENODEV) {
1341                         if (do_print)
1342                                 fprintf(stderr, "WARNING: device %lld not "
1343                                         "present\n", devid);
1344                         continue;
1345                 }
1346                 if (sp[i].ret && sp[i].ioctl_errno == ECANCELED) {
1347                         ++err;
1348                 } else if (sp[i].ret) {
1349                         if (do_print)
1350                                 fprintf(stderr, "ERROR: scrubbing %s failed "
1351                                         "for device id %lld (%s)\n", path,
1352                                         devid, strerror(sp[i].ioctl_errno));
1353                         ++err;
1354                         continue;
1355                 }
1356                 if (sp[i].scrub_args.progress.uncorrectable_errors > 0)
1357                         e_uncorrectable++;
1358                 if (sp[i].scrub_args.progress.corrected_errors > 0
1359                     || sp[i].scrub_args.progress.unverified_errors > 0)
1360                         e_correctable++;
1361         }
1362
1363         if (do_print) {
1364                 const char *append = "done";
1365                 if (!do_stats_per_dev)
1366                         init_fs_stat(&fs_stat);
1367                 for (i = 0; i < fi_args.num_devices; ++i) {
1368                         if (do_stats_per_dev) {
1369                                 print_scrub_dev(&di_args[i],
1370                                                 &sp[i].scrub_args.progress,
1371                                                 print_raw,
1372                                                 sp[i].ret ? "canceled" : "done",
1373                                                 &sp[i].stats);
1374                         } else {
1375                                 if (sp[i].ret)
1376                                         append = "canceled";
1377                                 add_to_fs_stat(&sp[i].scrub_args.progress,
1378                                                 &sp[i].stats, &fs_stat);
1379                         }
1380                 }
1381                 if (!do_stats_per_dev) {
1382                         printf("scrub %s for %s\n", append, fsid);
1383                         print_fs_stat(&fs_stat, print_raw);
1384                 }
1385         }
1386
1387         ret = pthread_cancel(t_prog);
1388         if (!ret)
1389                 ret = pthread_join(t_prog, &terr);
1390
1391         /* check for errors from the handling of the progress thread */
1392         if (do_print && ret) {
1393                 fprintf(stderr, "ERROR: progress thread handling failed: %s\n",
1394                         strerror(ret));
1395         }
1396
1397         /* check for errors returned from the progress thread itself */
1398         if (do_print && terr && terr != PTHREAD_CANCELED) {
1399                 fprintf(stderr, "ERROR: recording progress "
1400                         "failed: %s\n", strerror(-PTR_ERR(terr)));
1401         }
1402
1403         if (do_record) {
1404                 ret = scrub_write_progress(&spc_write_mutex, fsid, sp,
1405                                            fi_args.num_devices);
1406                 if (ret && do_print) {
1407                         fprintf(stderr, "ERROR: failed to record the result: "
1408                                 "%s\n", strerror(-ret));
1409                 }
1410         }
1411
1412         scrub_handle_sigint_child(-1);
1413
1414 out:
1415         free_history(past_scrubs);
1416         free(di_args);
1417         free(t_devs);
1418         free(sp);
1419         free(spc.progress);
1420         if (prg_fd > -1) {
1421                 close(prg_fd);
1422                 if (sock_path[0])
1423                         unlink(sock_path);
1424         }
1425         close(fdmnt);
1426
1427         if (err)
1428                 return 1;
1429         if (e_correctable)
1430                 return 7;
1431         if (e_uncorrectable)
1432                 return 8;
1433         return 0;
1434 }
1435
1436 static const char * const cmd_scrub_start_usage[] = {
1437         "btrfs scrub start [-Bdqr] <path>|<device>",
1438         "Start a new scrub",
1439         "",
1440         "-B     do not background",
1441         "-d     stats per device (-B only)",
1442         "-q     be quiet",
1443         "-r     read only mode",
1444         NULL
1445 };
1446
1447 static int cmd_scrub_start(int argc, char **argv)
1448 {
1449         return scrub_start(argc, argv, 0);
1450 }
1451
1452 static const char * const cmd_scrub_cancel_usage[] = {
1453         "btrfs scrub cancel <path>|<device>",
1454         "Cancel a running scrub",
1455         NULL
1456 };
1457
1458 static int cmd_scrub_cancel(int argc, char **argv)
1459 {
1460         char *path;
1461         int ret;
1462         int fdmnt;
1463         int err;
1464         char mp[BTRFS_PATH_NAME_MAX + 1];
1465         struct btrfs_fs_devices *fs_devices_mnt = NULL;
1466
1467         if (check_argc_exact(argc, 2))
1468                 usage(cmd_scrub_cancel_usage);
1469
1470         path = argv[1];
1471
1472 again:
1473         fdmnt = open_file_or_dir(path);
1474         if (fdmnt < 0) {
1475                 perror("ERROR: scrub cancel failed:");
1476                 return 1;
1477         }
1478
1479         ret = ioctl(fdmnt, BTRFS_IOC_SCRUB_CANCEL, NULL);
1480         err = errno;
1481
1482         if (ret && err == EINVAL) {
1483                 /* path is not a btrfs mount point.  See if it's a device. */
1484                 ret = check_mounted_where(fdmnt, path, mp, sizeof(mp),
1485                                           &fs_devices_mnt);
1486                 if (ret > 0) {
1487                         /* It's a mounted btrfs device; retry w/ mountpoint. */
1488                         close(fdmnt);
1489                         path = mp;
1490                         goto again;
1491                 } else {
1492                         /* It's not a mounted btrfs device either */
1493                         fprintf(stderr,
1494                                 "ERROR: %s is not a mounted btrfs device\n",
1495                                 path);
1496                         ret = 1;
1497                         err = EINVAL;
1498                 }
1499         }
1500
1501         close(fdmnt);
1502
1503         if (ret) {
1504                 fprintf(stderr, "ERROR: scrub cancel failed on %s: %s\n", path,
1505                         err == ENOTCONN ? "not running" : strerror(err));
1506                 return 1;
1507         }
1508
1509         printf("scrub cancelled\n");
1510
1511         return 0;
1512 }
1513
1514 static const char * const cmd_scrub_resume_usage[] = {
1515         "btrfs scrub resume [-Bdqr] <path>|<device>",
1516         "Resume previously canceled or interrupted scrub",
1517         "",
1518         "-B     do not background",
1519         "-d     stats per device (-B only)",
1520         "-q     be quiet",
1521         "-r     read only mode",
1522         NULL
1523 };
1524
1525 static int cmd_scrub_resume(int argc, char **argv)
1526 {
1527         return scrub_start(argc, argv, 1);
1528 }
1529
1530 static const char * const cmd_scrub_status_usage[] = {
1531         "btrfs scrub status [-dR] <path>|<device>",
1532         "Show status of running or finished scrub",
1533         "",
1534         "-d     stats per device",
1535         "-R     print raw stats",
1536         NULL
1537 };
1538
1539 static int cmd_scrub_status(int argc, char **argv)
1540 {
1541         char *path;
1542         struct btrfs_ioctl_fs_info_args fi_args;
1543         struct btrfs_ioctl_dev_info_args *di_args = NULL;
1544         struct scrub_file_record **past_scrubs = NULL;
1545         struct scrub_file_record *last_scrub;
1546         struct scrub_fs_stat fs_stat;
1547         struct sockaddr_un addr = {
1548                 .sun_family = AF_UNIX,
1549         };
1550         int ret;
1551         int i;
1552         int fdmnt;
1553         int print_raw = 0;
1554         int do_stats_per_dev = 0;
1555         int c;
1556         char fsid[37];
1557         int fdres = -1;
1558         int err = 0;
1559
1560         optind = 1;
1561         while ((c = getopt(argc, argv, "dR")) != -1) {
1562                 switch (c) {
1563                 case 'd':
1564                         do_stats_per_dev = 1;
1565                         break;
1566                 case 'R':
1567                         print_raw = 1;
1568                         break;
1569                 case '?':
1570                 default:
1571                         usage(cmd_scrub_status_usage);
1572                 }
1573         }
1574
1575         if (check_argc_exact(argc - optind, 1))
1576                 usage(cmd_scrub_status_usage);
1577
1578         path = argv[optind];
1579
1580         fdmnt = open_file_or_dir(path);
1581         if (fdmnt < 0) {
1582                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
1583                 return 12;
1584         }
1585
1586         ret = get_fs_info(fdmnt, path, &fi_args, &di_args);
1587         if (ret) {
1588                 fprintf(stderr, "ERROR: getting dev info for scrub failed: "
1589                                 "%s\n", strerror(-ret));
1590                 err = 1;
1591                 goto out;
1592         }
1593         if (!fi_args.num_devices) {
1594                 fprintf(stderr, "ERROR: no devices found\n");
1595                 err = 1;
1596                 goto out;
1597         }
1598
1599         uuid_unparse(fi_args.fsid, fsid);
1600
1601         fdres = socket(AF_UNIX, SOCK_STREAM, 0);
1602         if (fdres == -1) {
1603                 fprintf(stderr, "ERROR: failed to create socket to "
1604                         "receive progress information: %s\n",
1605                         strerror(errno));
1606                 err = 1;
1607                 goto out;
1608         }
1609         scrub_datafile(SCRUB_PROGRESS_SOCKET_PATH, fsid,
1610                         NULL, addr.sun_path, sizeof(addr.sun_path));
1611         /* ignore EOVERFLOW, just use shorter name and hope for the best */
1612         addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
1613         ret = connect(fdres, (struct sockaddr *)&addr, sizeof(addr));
1614         if (ret == -1) {
1615                 close(fdres);
1616                 fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid);
1617                 if (fdres < 0 && fdres != -ENOENT) {
1618                         fprintf(stderr, "WARNING: failed to open status file: "
1619                                 "%s\n", strerror(-fdres));
1620                         err = 1;
1621                         goto out;
1622                 }
1623         }
1624
1625         if (fdres >= 0) {
1626                 past_scrubs = scrub_read_file(fdres, 1);
1627                 if (IS_ERR(past_scrubs))
1628                         fprintf(stderr, "WARNING: failed to read status: %s\n",
1629                                 strerror(-PTR_ERR(past_scrubs)));
1630         }
1631
1632         printf("scrub status for %s\n", fsid);
1633
1634         if (do_stats_per_dev) {
1635                 for (i = 0; i < fi_args.num_devices; ++i) {
1636                         last_scrub = last_dev_scrub(past_scrubs,
1637                                                         di_args[i].devid);
1638                         if (!last_scrub) {
1639                                 print_scrub_dev(&di_args[i], NULL, print_raw,
1640                                                 NULL, NULL);
1641                                 continue;
1642                         }
1643                         print_scrub_dev(&di_args[i], &last_scrub->p, print_raw,
1644                                         last_scrub->stats.finished ?
1645                                                         "history" : "status",
1646                                         &last_scrub->stats);
1647                 }
1648         } else {
1649                 init_fs_stat(&fs_stat);
1650                 for (i = 0; i < fi_args.num_devices; ++i) {
1651                         last_scrub = last_dev_scrub(past_scrubs,
1652                                                         di_args[i].devid);
1653                         if (!last_scrub)
1654                                 continue;
1655                         add_to_fs_stat(&last_scrub->p, &last_scrub->stats,
1656                                         &fs_stat);
1657                 }
1658                 print_fs_stat(&fs_stat, print_raw);
1659         }
1660
1661 out:
1662         free_history(past_scrubs);
1663         free(di_args);
1664         if (fdres > -1)
1665                 close(fdres);
1666
1667         return err;
1668 }
1669
1670 const struct cmd_group scrub_cmd_group = {
1671         scrub_cmd_group_usage, NULL, {
1672                 { "start", cmd_scrub_start, cmd_scrub_start_usage, NULL, 0 },
1673                 { "cancel", cmd_scrub_cancel, cmd_scrub_cancel_usage, NULL, 0 },
1674                 { "resume", cmd_scrub_resume, cmd_scrub_resume_usage, NULL, 0 },
1675                 { "status", cmd_scrub_status, cmd_scrub_status_usage, NULL, 0 },
1676                 { 0, 0, 0, 0, 0 }
1677         }
1678 };
1679
1680 int cmd_scrub(int argc, char **argv)
1681 {
1682         return handle_command_group(&scrub_cmd_group, argc, argv);
1683 }