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