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