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