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