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