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