btrfs-progs: cleanup whitespace in is_same_blk_file
[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 < 0 && errno == ENODEV)
1077                         continue;
1078                 if (ret < 0 && errno == ENOTCONN)
1079                         return 0;
1080                 if (!ret)
1081                         return 1;
1082         }
1083
1084         return 1;
1085 }
1086
1087 static const char * const cmd_scrub_start_usage[];
1088 static const char * const cmd_scrub_resume_usage[];
1089
1090 static int scrub_start(int argc, char **argv, int resume)
1091 {
1092         int fdmnt;
1093         int prg_fd = -1;
1094         int fdres = -1;
1095         int ret;
1096         pid_t pid;
1097         int c;
1098         int i;
1099         int err = 0;
1100         int e_uncorrectable = 0;
1101         int e_correctable = 0;
1102         int print_raw = 0;
1103         char *path;
1104         int do_background = 1;
1105         int do_wait = 0;
1106         int do_print = 0;
1107         int do_quiet = 0;
1108         int do_record = 1;
1109         int readonly = 0;
1110         int do_stats_per_dev = 0;
1111         int ioprio_class = IOPRIO_CLASS_IDLE;
1112         int ioprio_classdata = 0;
1113         int n_start = 0;
1114         int n_skip = 0;
1115         int n_resume = 0;
1116         struct btrfs_ioctl_fs_info_args fi_args;
1117         struct btrfs_ioctl_dev_info_args *di_args = NULL;
1118         struct scrub_progress *sp = NULL;
1119         struct scrub_fs_stat fs_stat;
1120         struct timeval tv;
1121         struct sockaddr_un addr = {
1122                 .sun_family = AF_UNIX,
1123         };
1124         pthread_t *t_devs = NULL;
1125         pthread_t t_prog;
1126         struct scrub_file_record **past_scrubs = NULL;
1127         struct scrub_file_record *last_scrub = NULL;
1128         char *datafile = strdup(SCRUB_DATA_FILE);
1129         char fsid[BTRFS_UUID_UNPARSED_SIZE];
1130         char sock_path[BTRFS_PATH_NAME_MAX + 1] = "";
1131         struct scrub_progress_cycle spc;
1132         pthread_mutex_t spc_write_mutex = PTHREAD_MUTEX_INITIALIZER;
1133         void *terr;
1134         u64 devid;
1135         DIR *dirstream = NULL;
1136         int force = 0;
1137         int nothing_to_resume = 0;
1138
1139         optind = 1;
1140         while ((c = getopt(argc, argv, "BdqrRc:n:f")) != -1) {
1141                 switch (c) {
1142                 case 'B':
1143                         do_background = 0;
1144                         do_wait = 1;
1145                         do_print = 1;
1146                         break;
1147                 case 'd':
1148                         do_stats_per_dev = 1;
1149                         break;
1150                 case 'q':
1151                         do_quiet = 1;
1152                         break;
1153                 case 'r':
1154                         readonly = 1;
1155                         break;
1156                 case 'R':
1157                         print_raw = 1;
1158                         break;
1159                 case 'c':
1160                         ioprio_class = (int)strtol(optarg, NULL, 10);
1161                         break;
1162                 case 'n':
1163                         ioprio_classdata = (int)strtol(optarg, NULL, 10);
1164                         break;
1165                 case 'f':
1166                         force = 1;
1167                         break;
1168                 case '?':
1169                 default:
1170                         usage(resume ? cmd_scrub_resume_usage :
1171                                                 cmd_scrub_start_usage);
1172                 }
1173         }
1174
1175         /* try to catch most error cases before forking */
1176
1177         if (check_argc_exact(argc - optind, 1)) {
1178                 usage(resume ? cmd_scrub_resume_usage :
1179                                         cmd_scrub_start_usage);
1180         }
1181
1182         spc.progress = NULL;
1183         if (do_quiet && do_print)
1184                 do_print = 0;
1185
1186         if (mkdir_p(datafile)) {
1187                 ERR(!do_quiet, "WARNING: cannot create scrub data "
1188                                "file, mkdir %s failed: %s. Status recording "
1189                                "disabled\n", datafile, strerror(errno));
1190                 do_record = 0;
1191         }
1192         free(datafile);
1193
1194         path = argv[optind];
1195
1196         fdmnt = open_path_or_dev_mnt(path, &dirstream);
1197
1198         if (fdmnt < 0) {
1199                 if (errno == EINVAL)
1200                         ERR(!do_quiet,
1201                             "ERROR: '%s' is not a mounted btrfs device\n",
1202                             path);
1203                 else
1204                         ERR(!do_quiet, "ERROR: can't access '%s': %s\n",
1205                             path, strerror(errno));
1206                 return 1;
1207         }
1208
1209         ret = get_fs_info(path, &fi_args, &di_args);
1210         if (ret) {
1211                 ERR(!do_quiet, "ERROR: getting dev info for scrub failed: "
1212                     "%s\n", strerror(-ret));
1213                 err = 1;
1214                 goto out;
1215         }
1216         if (!fi_args.num_devices) {
1217                 ERR(!do_quiet, "ERROR: no devices found\n");
1218                 err = 1;
1219                 goto out;
1220         }
1221
1222         uuid_unparse(fi_args.fsid, fsid);
1223         fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid);
1224         if (fdres < 0 && fdres != -ENOENT) {
1225                 ERR(!do_quiet, "WARNING: failed to open status file: "
1226                     "%s\n", strerror(-fdres));
1227         } else if (fdres >= 0) {
1228                 past_scrubs = scrub_read_file(fdres, !do_quiet);
1229                 if (IS_ERR(past_scrubs))
1230                         ERR(!do_quiet, "WARNING: failed to read status file: "
1231                             "%s\n", strerror(-PTR_ERR(past_scrubs)));
1232                 close(fdres);
1233         }
1234
1235         /*
1236          * Check for stale information in the status file, ie. if it's
1237          * canceled=0, finished=0 but no scrub is running.
1238          */
1239         if (!is_scrub_running_in_kernel(fdmnt, di_args, fi_args.num_devices))
1240                 force = 1;
1241
1242         /*
1243          * check whether any involved device is already busy running a
1244          * scrub. This would cause damaged status messages and the state
1245          * "aborted" without the explanation that a scrub was already
1246          * running. Therefore check it first, prevent it and give some
1247          * feedback to the user if scrub is already running.
1248          * Note that if scrub is started with a block device as the
1249          * parameter, only that particular block device is checked. It
1250          * is a normal mode of operation to start scrub on multiple
1251          * single devices, there is no reason to prevent this.
1252          */
1253         if (!force && is_scrub_running_on_fs(&fi_args, di_args, past_scrubs)) {
1254                 ERR(!do_quiet,
1255                     "ERROR: scrub is already running.\n"
1256                     "To cancel use 'btrfs scrub cancel %s'.\n"
1257                     "To see the status use 'btrfs scrub status [-d] %s'.\n",
1258                     path, path);
1259                 err = 1;
1260                 goto out;
1261         }
1262
1263         t_devs = malloc(fi_args.num_devices * sizeof(*t_devs));
1264         sp = calloc(fi_args.num_devices, sizeof(*sp));
1265         spc.progress = calloc(fi_args.num_devices * 2, sizeof(*spc.progress));
1266
1267         if (!t_devs || !sp || !spc.progress) {
1268                 ERR(!do_quiet, "ERROR: scrub failed: %s", strerror(errno));
1269                 err = 1;
1270                 goto out;
1271         }
1272
1273         for (i = 0; i < fi_args.num_devices; ++i) {
1274                 devid = di_args[i].devid;
1275                 ret = pthread_mutex_init(&sp[i].progress_mutex, NULL);
1276                 if (ret) {
1277                         ERR(!do_quiet, "ERROR: pthread_mutex_init failed: "
1278                             "%s\n", strerror(ret));
1279                         err = 1;
1280                         goto out;
1281                 }
1282                 last_scrub = last_dev_scrub(past_scrubs, devid);
1283                 sp[i].scrub_args.devid = devid;
1284                 sp[i].fd = fdmnt;
1285                 if (resume && last_scrub && (last_scrub->stats.canceled ||
1286                                              !last_scrub->stats.finished)) {
1287                         ++n_resume;
1288                         sp[i].scrub_args.start = last_scrub->p.last_physical;
1289                         sp[i].resumed = last_scrub;
1290                 } else if (resume) {
1291                         ++n_skip;
1292                         sp[i].skip = 1;
1293                         sp[i].resumed = last_scrub;
1294                         continue;
1295                 } else {
1296                         ++n_start;
1297                         sp[i].scrub_args.start = 0ll;
1298                         sp[i].resumed = NULL;
1299                 }
1300                 sp[i].skip = 0;
1301                 sp[i].scrub_args.end = (u64)-1ll;
1302                 sp[i].scrub_args.flags = readonly ? BTRFS_SCRUB_READONLY : 0;
1303                 sp[i].ioprio_class = ioprio_class;
1304                 sp[i].ioprio_classdata = ioprio_classdata;
1305         }
1306
1307         if (!n_start && !n_resume) {
1308                 if (!do_quiet)
1309                         printf("scrub: nothing to resume for %s, fsid %s\n",
1310                                path, fsid);
1311                 nothing_to_resume = 1;
1312                 goto out;
1313         }
1314
1315         ret = prg_fd = socket(AF_UNIX, SOCK_STREAM, 0);
1316         while (ret != -1) {
1317                 ret = scrub_datafile(SCRUB_PROGRESS_SOCKET_PATH, fsid, NULL,
1318                                         sock_path, sizeof(sock_path));
1319                 /* ignore EOVERFLOW, try using a shorter path for the socket */
1320                 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
1321                 strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);
1322                 ret = bind(prg_fd, (struct sockaddr *)&addr, sizeof(addr));
1323                 if (ret != -1 || errno != EADDRINUSE)
1324                         break;
1325                 /*
1326                  * bind failed with EADDRINUSE. so let's see if anyone answers
1327                  * when we make a call to the socket ...
1328                  */
1329                 ret = connect(prg_fd, (struct sockaddr *)&addr, sizeof(addr));
1330                 if (!ret || errno != ECONNREFUSED) {
1331                         /* ... yes, so scrub must be running. error out */
1332                         fprintf(stderr, "ERROR: scrub already running\n");
1333                         close(prg_fd);
1334                         prg_fd = -1;
1335                         goto out;
1336                 }
1337                 /*
1338                  * ... no, this means someone left us alone with an unused
1339                  * socket in the file system. remove it and try again.
1340                  */
1341                 ret = unlink(sock_path);
1342         }
1343         if (ret != -1)
1344                 ret = listen(prg_fd, 100);
1345         if (ret == -1) {
1346                 ERR(!do_quiet, "WARNING: failed to open the progress status "
1347                     "socket at %s: %s. Progress cannot be queried\n",
1348                     sock_path[0] ? sock_path : SCRUB_PROGRESS_SOCKET_PATH,
1349                     strerror(errno));
1350                 if (prg_fd != -1) {
1351                         close(prg_fd);
1352                         prg_fd = -1;
1353                         if (sock_path[0])
1354                                 unlink(sock_path);
1355                 }
1356         }
1357
1358         if (do_record) {
1359                 /* write all-zero progress file for a start */
1360                 ret = scrub_write_progress(&spc_write_mutex, fsid, sp,
1361                                            fi_args.num_devices);
1362                 if (ret) {
1363                         ERR(!do_quiet, "WARNING: failed to write the progress "
1364                             "status file: %s. Status recording disabled\n",
1365                             strerror(-ret));
1366                         do_record = 0;
1367                 }
1368         }
1369
1370         if (do_background) {
1371                 pid = fork();
1372                 if (pid == -1) {
1373                         ERR(!do_quiet, "ERROR: cannot scrub, fork failed: "
1374                                         "%s\n", strerror(errno));
1375                         err = 1;
1376                         goto out;
1377                 }
1378
1379                 if (pid) {
1380                         int stat;
1381                         scrub_handle_sigint_parent();
1382                         if (!do_quiet)
1383                                 printf("scrub %s on %s, fsid %s (pid=%d)\n",
1384                                        n_start ? "started" : "resumed",
1385                                        path, fsid, pid);
1386                         if (!do_wait) {
1387                                 err = 0;
1388                                 goto out;
1389                         }
1390                         ret = wait(&stat);
1391                         if (ret != pid) {
1392                                 ERR(!do_quiet, "ERROR: wait failed: (ret=%d) "
1393                                     "%s\n", ret, strerror(errno));
1394                                 err = 1;
1395                                 goto out;
1396                         }
1397                         if (!WIFEXITED(stat) || WEXITSTATUS(stat)) {
1398                                 ERR(!do_quiet, "ERROR: scrub process failed\n");
1399                                 err = WIFEXITED(stat) ? WEXITSTATUS(stat) : -1;
1400                                 goto out;
1401                         }
1402                         err = 0;
1403                         goto out;
1404                 }
1405         }
1406
1407         scrub_handle_sigint_child(fdmnt);
1408
1409         for (i = 0; i < fi_args.num_devices; ++i) {
1410                 if (sp[i].skip) {
1411                         sp[i].scrub_args.progress = sp[i].resumed->p;
1412                         sp[i].stats = sp[i].resumed->stats;
1413                         sp[i].ret = 0;
1414                         sp[i].stats.finished = 1;
1415                         continue;
1416                 }
1417                 devid = di_args[i].devid;
1418                 gettimeofday(&tv, NULL);
1419                 sp[i].stats.t_start = tv.tv_sec;
1420                 ret = pthread_create(&t_devs[i], NULL,
1421                                         scrub_one_dev, &sp[i]);
1422                 if (ret) {
1423                         if (do_print)
1424                                 fprintf(stderr, "ERROR: creating "
1425                                         "scrub_one_dev[%llu] thread failed: "
1426                                         "%s\n", devid, strerror(ret));
1427                         err = 1;
1428                         goto out;
1429                 }
1430         }
1431
1432         spc.fdmnt = fdmnt;
1433         spc.prg_fd = prg_fd;
1434         spc.do_record = do_record;
1435         spc.write_mutex = &spc_write_mutex;
1436         spc.shared_progress = sp;
1437         spc.fi = &fi_args;
1438         ret = pthread_create(&t_prog, NULL, scrub_progress_cycle, &spc);
1439         if (ret) {
1440                 if (do_print)
1441                         fprintf(stderr, "ERROR: creating progress thread "
1442                                 "failed: %s\n", strerror(ret));
1443                 err = 1;
1444                 goto out;
1445         }
1446
1447         err = 0;
1448         for (i = 0; i < fi_args.num_devices; ++i) {
1449                 if (sp[i].skip)
1450                         continue;
1451                 devid = di_args[i].devid;
1452                 ret = pthread_join(t_devs[i], NULL);
1453                 if (ret) {
1454                         if (do_print)
1455                                 fprintf(stderr, "ERROR: pthread_join failed "
1456                                         "for scrub_one_dev[%llu]: %s\n", devid,
1457                                         strerror(ret));
1458                         ++err;
1459                         continue;
1460                 }
1461                 if (sp[i].ret && sp[i].ioctl_errno == ENODEV) {
1462                         if (do_print)
1463                                 fprintf(stderr, "WARNING: device %lld not "
1464                                         "present\n", devid);
1465                         continue;
1466                 }
1467                 if (sp[i].ret && sp[i].ioctl_errno == ECANCELED) {
1468                         ++err;
1469                 } else if (sp[i].ret) {
1470                         if (do_print)
1471                                 fprintf(stderr, "ERROR: scrubbing %s failed "
1472                                         "for device id %lld (%s)\n", path,
1473                                         devid, strerror(sp[i].ioctl_errno));
1474                         ++err;
1475                         continue;
1476                 }
1477                 if (sp[i].scrub_args.progress.uncorrectable_errors > 0)
1478                         e_uncorrectable++;
1479                 if (sp[i].scrub_args.progress.corrected_errors > 0
1480                     || sp[i].scrub_args.progress.unverified_errors > 0)
1481                         e_correctable++;
1482         }
1483
1484         if (do_print) {
1485                 const char *append = "done";
1486                 if (!do_stats_per_dev)
1487                         init_fs_stat(&fs_stat);
1488                 for (i = 0; i < fi_args.num_devices; ++i) {
1489                         if (do_stats_per_dev) {
1490                                 print_scrub_dev(&di_args[i],
1491                                                 &sp[i].scrub_args.progress,
1492                                                 print_raw,
1493                                                 sp[i].ret ? "canceled" : "done",
1494                                                 &sp[i].stats);
1495                         } else {
1496                                 if (sp[i].ret)
1497                                         append = "canceled";
1498                                 add_to_fs_stat(&sp[i].scrub_args.progress,
1499                                                 &sp[i].stats, &fs_stat);
1500                         }
1501                 }
1502                 if (!do_stats_per_dev) {
1503                         printf("scrub %s for %s\n", append, fsid);
1504                         print_fs_stat(&fs_stat, print_raw);
1505                 }
1506         }
1507
1508         ret = pthread_cancel(t_prog);
1509         if (!ret)
1510                 ret = pthread_join(t_prog, &terr);
1511
1512         /* check for errors from the handling of the progress thread */
1513         if (do_print && ret) {
1514                 fprintf(stderr, "ERROR: progress thread handling failed: %s\n",
1515                         strerror(ret));
1516         }
1517
1518         /* check for errors returned from the progress thread itself */
1519         if (do_print && terr && terr != PTHREAD_CANCELED) {
1520                 fprintf(stderr, "ERROR: recording progress "
1521                         "failed: %s\n", strerror(-PTR_ERR(terr)));
1522         }
1523
1524         if (do_record) {
1525                 ret = scrub_write_progress(&spc_write_mutex, fsid, sp,
1526                                            fi_args.num_devices);
1527                 if (ret && do_print) {
1528                         fprintf(stderr, "ERROR: failed to record the result: "
1529                                 "%s\n", strerror(-ret));
1530                 }
1531         }
1532
1533         scrub_handle_sigint_child(-1);
1534
1535 out:
1536         free_history(past_scrubs);
1537         free(di_args);
1538         free(t_devs);
1539         free(sp);
1540         free(spc.progress);
1541         if (prg_fd > -1) {
1542                 close(prg_fd);
1543                 if (sock_path[0])
1544                         unlink(sock_path);
1545         }
1546         close_file_or_dir(fdmnt, dirstream);
1547
1548         if (err)
1549                 return 1;
1550         if (nothing_to_resume)
1551                 return 2;
1552         if (e_uncorrectable) {
1553                 ERR(!do_quiet, "ERROR: There are uncorrectable errors.\n");
1554                 return 3;
1555         }
1556         if (e_correctable)
1557                 ERR(!do_quiet, "WARNING: errors detected during scrubbing, corrected.\n");
1558
1559         return 0;
1560 }
1561
1562 static const char * const cmd_scrub_start_usage[] = {
1563         "btrfs scrub start [-BdqrRf] [-c ioprio_class -n ioprio_classdata] <path>|<device>",
1564         "Start a new scrub. If a scrub is already running, the new one fails.",
1565         "",
1566         "-B     do not background",
1567         "-d     stats per device (-B only)",
1568         "-q     be quiet",
1569         "-r     read only mode",
1570         "-R     raw print mode, print full data instead of summary",
1571         "-c     set ioprio class (see ionice(1) manpage)",
1572         "-n     set ioprio classdata (see ionice(1) manpage)",
1573         "-f     force starting new scrub even if a scrub is already running",
1574         "       this is useful when scrub stats record file is damaged",
1575         NULL
1576 };
1577
1578 static int cmd_scrub_start(int argc, char **argv)
1579 {
1580         return scrub_start(argc, argv, 0);
1581 }
1582
1583 static const char * const cmd_scrub_cancel_usage[] = {
1584         "btrfs scrub cancel <path>|<device>",
1585         "Cancel a running scrub",
1586         NULL
1587 };
1588
1589 static int cmd_scrub_cancel(int argc, char **argv)
1590 {
1591         char *path;
1592         int ret;
1593         int fdmnt = -1;
1594         DIR *dirstream = NULL;
1595
1596         if (check_argc_exact(argc, 2))
1597                 usage(cmd_scrub_cancel_usage);
1598
1599         path = argv[1];
1600
1601         fdmnt = open_path_or_dev_mnt(path, &dirstream);
1602         if (fdmnt < 0) {
1603                 if (errno == EINVAL)
1604                         fprintf(stderr,
1605                                 "ERROR: '%s' is not a mounted btrfs device\n",
1606                                 path);
1607                 else
1608                         fprintf(stderr, "ERROR: can't access '%s': %s\n",
1609                                 path, strerror(errno));
1610                 ret = 1;
1611                 goto out;
1612         }
1613
1614         ret = ioctl(fdmnt, BTRFS_IOC_SCRUB_CANCEL, NULL);
1615
1616         if (ret < 0) {
1617                 fprintf(stderr, "ERROR: scrub cancel failed on %s: %s\n", path,
1618                         errno == ENOTCONN ? "not running" : strerror(errno));
1619                 if (errno == ENOTCONN)
1620                         ret = 2;
1621                 else
1622                         ret = 1;
1623                 goto out;
1624         }
1625
1626         ret = 0;
1627         printf("scrub cancelled\n");
1628
1629 out:
1630         close_file_or_dir(fdmnt, dirstream);
1631         return ret;
1632 }
1633
1634 static const char * const cmd_scrub_resume_usage[] = {
1635         "btrfs scrub resume [-BdqrR] [-c ioprio_class -n ioprio_classdata] <path>|<device>",
1636         "Resume previously canceled or interrupted scrub",
1637         "",
1638         "-B     do not background",
1639         "-d     stats per device (-B only)",
1640         "-q     be quiet",
1641         "-r     read only mode",
1642         "-R     raw print mode, print full data instead of summary",
1643         "-c     set ioprio class (see ionice(1) manpage)",
1644         "-n     set ioprio classdata (see ionice(1) manpage)",
1645         NULL
1646 };
1647
1648 static int cmd_scrub_resume(int argc, char **argv)
1649 {
1650         return scrub_start(argc, argv, 1);
1651 }
1652
1653 static const char * const cmd_scrub_status_usage[] = {
1654         "btrfs scrub status [-dR] <path>|<device>",
1655         "Show status of running or finished scrub",
1656         "",
1657         "-d     stats per device",
1658         "-R     print raw stats",
1659         NULL
1660 };
1661
1662 static int cmd_scrub_status(int argc, char **argv)
1663 {
1664         char *path;
1665         struct btrfs_ioctl_fs_info_args fi_args;
1666         struct btrfs_ioctl_dev_info_args *di_args = NULL;
1667         struct scrub_file_record **past_scrubs = NULL;
1668         struct scrub_file_record *last_scrub;
1669         struct scrub_fs_stat fs_stat;
1670         struct sockaddr_un addr = {
1671                 .sun_family = AF_UNIX,
1672         };
1673         int in_progress;
1674         int ret;
1675         int i;
1676         int fdmnt;
1677         int print_raw = 0;
1678         int do_stats_per_dev = 0;
1679         int c;
1680         char fsid[BTRFS_UUID_UNPARSED_SIZE];
1681         int fdres = -1;
1682         int err = 0;
1683         DIR *dirstream = NULL;
1684
1685         optind = 1;
1686         while ((c = getopt(argc, argv, "dR")) != -1) {
1687                 switch (c) {
1688                 case 'd':
1689                         do_stats_per_dev = 1;
1690                         break;
1691                 case 'R':
1692                         print_raw = 1;
1693                         break;
1694                 case '?':
1695                 default:
1696                         usage(cmd_scrub_status_usage);
1697                 }
1698         }
1699
1700         if (check_argc_exact(argc - optind, 1))
1701                 usage(cmd_scrub_status_usage);
1702
1703         path = argv[optind];
1704
1705         fdmnt = open_path_or_dev_mnt(path, &dirstream);
1706
1707         if (fdmnt < 0) {
1708                 if (errno == EINVAL)
1709                         fprintf(stderr,
1710                                 "ERROR: '%s' is not a mounted btrfs device\n",
1711                                 path);
1712                 else
1713                         fprintf(stderr, "ERROR: can't access '%s': %s\n",
1714                                 path, strerror(errno));
1715                 return 1;
1716         }
1717
1718         ret = get_fs_info(path, &fi_args, &di_args);
1719         if (ret) {
1720                 fprintf(stderr, "ERROR: getting dev info for scrub failed: "
1721                                 "%s\n", strerror(-ret));
1722                 err = 1;
1723                 goto out;
1724         }
1725         if (!fi_args.num_devices) {
1726                 fprintf(stderr, "ERROR: no devices found\n");
1727                 err = 1;
1728                 goto out;
1729         }
1730
1731         uuid_unparse(fi_args.fsid, fsid);
1732
1733         fdres = socket(AF_UNIX, SOCK_STREAM, 0);
1734         if (fdres == -1) {
1735                 fprintf(stderr, "ERROR: failed to create socket to "
1736                         "receive progress information: %s\n",
1737                         strerror(errno));
1738                 err = 1;
1739                 goto out;
1740         }
1741         scrub_datafile(SCRUB_PROGRESS_SOCKET_PATH, fsid,
1742                         NULL, addr.sun_path, sizeof(addr.sun_path));
1743         /* ignore EOVERFLOW, just use shorter name and hope for the best */
1744         addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
1745         ret = connect(fdres, (struct sockaddr *)&addr, sizeof(addr));
1746         if (ret == -1) {
1747                 close(fdres);
1748                 fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid);
1749                 if (fdres < 0 && fdres != -ENOENT) {
1750                         fprintf(stderr, "WARNING: failed to open status file: "
1751                                 "%s\n", strerror(-fdres));
1752                         err = 1;
1753                         goto out;
1754                 }
1755         }
1756
1757         if (fdres >= 0) {
1758                 past_scrubs = scrub_read_file(fdres, 1);
1759                 if (IS_ERR(past_scrubs))
1760                         fprintf(stderr, "WARNING: failed to read status: %s\n",
1761                                 strerror(-PTR_ERR(past_scrubs)));
1762         }
1763         in_progress = is_scrub_running_in_kernel(fdmnt, di_args, fi_args.num_devices);
1764
1765         printf("scrub status for %s\n", fsid);
1766
1767         if (do_stats_per_dev) {
1768                 for (i = 0; i < fi_args.num_devices; ++i) {
1769                         last_scrub = last_dev_scrub(past_scrubs,
1770                                                         di_args[i].devid);
1771                         if (!last_scrub) {
1772                                 print_scrub_dev(&di_args[i], NULL, print_raw,
1773                                                 NULL, NULL);
1774                                 continue;
1775                         }
1776                         last_scrub->stats.in_progress = in_progress;
1777                         print_scrub_dev(&di_args[i], &last_scrub->p, print_raw,
1778                                         last_scrub->stats.finished ?
1779                                                         "history" : "status",
1780                                         &last_scrub->stats);
1781                 }
1782         } else {
1783                 init_fs_stat(&fs_stat);
1784                 fs_stat.s.in_progress = in_progress;
1785                 for (i = 0; i < fi_args.num_devices; ++i) {
1786                         last_scrub = last_dev_scrub(past_scrubs,
1787                                                         di_args[i].devid);
1788                         if (!last_scrub)
1789                                 continue;
1790                         add_to_fs_stat(&last_scrub->p, &last_scrub->stats,
1791                                         &fs_stat);
1792                 }
1793                 print_fs_stat(&fs_stat, print_raw);
1794         }
1795
1796 out:
1797         free_history(past_scrubs);
1798         free(di_args);
1799         if (fdres > -1)
1800                 close(fdres);
1801         close_file_or_dir(fdmnt, dirstream);
1802
1803         return !!err;
1804 }
1805
1806 const struct cmd_group scrub_cmd_group = {
1807         scrub_cmd_group_usage, NULL, {
1808                 { "start", cmd_scrub_start, cmd_scrub_start_usage, NULL, 0 },
1809                 { "cancel", cmd_scrub_cancel, cmd_scrub_cancel_usage, NULL, 0 },
1810                 { "resume", cmd_scrub_resume, cmd_scrub_resume_usage, NULL, 0 },
1811                 { "status", cmd_scrub_status, cmd_scrub_status_usage, NULL, 0 },
1812                 NULL_CMD_STRUCT
1813         }
1814 };
1815
1816 int cmd_scrub(int argc, char **argv)
1817 {
1818         return handle_command_group(&scrub_cmd_group, argc, argv);
1819 }