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