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