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