btrfs-progs: test_skip_this_disk() isn't needed anymore
[btrfs-progs-unstable/devel.git] / cmds-scrub.c
blob230a641f6cb613260b1cabb8ca5d4dd124f0d694
1 /*
2 * Copyright (C) 2011 STRATO. All rights reserved.
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.
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.
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.
19 #include "kerncompat.h"
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>
38 #include "ctree.h"
39 #include "ioctl.h"
40 #include "utils.h"
41 #include "volumes.h"
42 #include "disk-io.h"
44 #include "commands.h"
46 static const char * const scrub_cmd_group_usage[] = {
47 "btrfs scrub <command> [options] <path>|<device>",
48 NULL
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"
56 struct scrub_stats {
57 time_t t_start;
58 time_t t_resumed;
59 u64 duration;
60 u64 finished;
61 u64 canceled;
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
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;
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;
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;
103 struct scrub_fs_stat {
104 struct btrfs_scrub_progress p;
105 struct scrub_stats s;
106 int i;
109 static void print_scrub_full(struct btrfs_scrub_progress *sp)
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);
128 #define ERR(test, ...) do { \
129 if (test) \
130 fprintf(stderr, __VA_ARGS__); \
131 } while (0)
133 #define PRINT_SCRUB_ERROR(test, desc) do { \
134 if (test) \
135 printf(" %s=%llu", desc, test); \
136 } while (0)
138 static void print_scrub_summary(struct btrfs_scrub_progress *p)
140 u64 err_cnt;
141 u64 err_cnt2;
143 err_cnt = p->read_errors +
144 p->csum_errors +
145 p->verify_errors +
146 p->super_errors;
148 err_cnt2 = p->corrected_errors + p->uncorrectable_errors;
150 if (p->malloc_errors)
151 printf("*** WARNING: memory allocation failed while scrubbing. "
152 "results may be inaccurate\n");
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));
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);
171 #define _SCRUB_FS_STAT(p, name, fs_stat) do { \
172 fs_stat->p.name += p->name; \
173 } while (0)
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; \
180 } while (0)
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; \
187 } while (0)
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; \
194 } while (0)
196 static void add_to_fs_stat(struct btrfs_scrub_progress *p,
197 struct scrub_stats *ss,
198 struct scrub_fs_stat *fs_stat)
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);
221 static void init_fs_stat(struct scrub_fs_stat *fs_stat)
223 memset(fs_stat, 0, sizeof(*fs_stat));
224 fs_stat->s.finished = 1;
227 static void _print_scrub_ss(struct scrub_stats *ss)
229 char t[4096];
230 struct tm tm;
232 if (!ss || !ss->t_start) {
233 printf("\tno stats available\n");
234 return;
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);
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);
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)
262 printf("scrub device %s (id %llu) %s\n", di->path, di->devid,
263 append ? append : "");
265 _print_scrub_ss(ss);
267 if (p) {
268 if (raw)
269 print_scrub_full(p);
270 else
271 print_scrub_summary(p);
275 static void print_fs_stat(struct scrub_fs_stat *fs_stat, int raw)
277 _print_scrub_ss(&fs_stat->s);
279 if (raw)
280 print_scrub_full(&fs_stat->p);
281 else
282 print_scrub_summary(&fs_stat->p);
285 static void free_history(struct scrub_file_record **last_scrubs)
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);
296 * cancels a running scrub and makes the master process record the current
297 * progress status before exiting.
299 static int cancel_fd = -1;
300 static void scrub_sigint_record_progress(int signal)
302 int ret;
304 ret = ioctl(cancel_fd, BTRFS_IOC_SCRUB_CANCEL, NULL);
305 if (ret < 0)
306 perror("Scrub cancel failed");
309 static int scrub_handle_sigint_parent(void)
311 struct sigaction sa = {
312 .sa_handler = SIG_IGN,
313 .sa_flags = SA_RESTART,
316 return sigaction(SIGINT, &sa, NULL);
319 static int scrub_handle_sigint_child(int fd)
321 struct sigaction sa = {
322 .sa_handler = fd == -1 ? SIG_DFL : scrub_sigint_record_progress,
325 cancel_fd = fd;
326 return sigaction(SIGINT, &sa, NULL);
329 static int scrub_datafile(const char *fn_base, const char *fn_local,
330 const char *fn_tmp, char *datafile, int size)
332 int ret;
333 int end = size - 2;
335 datafile[end + 1] = '\0';
336 strncpy(datafile, fn_base, end);
337 ret = strlen(datafile);
339 if (ret + 1 > end)
340 return -EOVERFLOW;
342 datafile[ret] = '.';
343 strncpy(datafile + ret + 1, fn_local, end - ret - 1);
344 ret = strlen(datafile);
346 if (ret + 1 > end)
347 return -EOVERFLOW;
349 if (fn_tmp) {
350 datafile[ret] = '_';
351 strncpy(datafile + ret + 1, fn_tmp, end - ret - 1);
352 ret = strlen(datafile);
354 if (ret > end)
355 return -EOVERFLOW;
358 return 0;
361 static int scrub_open_file(const char *datafile, int m)
363 int fd;
364 int ret;
366 fd = open(datafile, m, 0600);
367 if (fd < 0)
368 return -errno;
370 ret = flock(fd, LOCK_EX|LOCK_NB);
371 if (ret) {
372 ret = errno;
373 close(fd);
374 return -ret;
377 return fd;
380 static int scrub_open_file_r(const char *fn_base, const char *fn_local)
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);
391 static int scrub_open_file_w(const char *fn_base, const char *fn_local,
392 const char *tmp)
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);
403 static int scrub_rename_file(const char *fn_base, const char *fn_local,
404 const char *tmp)
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;
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); \
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
430 static int scrub_kvread(int *i, int len, int avail, const char *buf,
431 const char *key, u64 *dest)
433 int j;
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)
442 if (*i + j >= avail)
443 return -1;
444 *dest = atoll(&buf[*i]);
445 *i += j;
446 return 1;
449 return 0;
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)
461 static struct scrub_file_record **scrub_read_file(int fd, int report_errors)
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;
477 if (fd < 0)
478 return ERR_PTR(-EINVAL);
480 again:
481 old_avail = avail - i;
482 BUG_ON(old_avail < 0);
483 if (old_avail)
484 memmove(l, l + i, old_avail);
485 avail = read(fd, l + old_avail, sizeof(l) - old_avail);
486 if (avail == 0)
487 eof = 1;
488 if (avail == 0 && old_avail == 0) {
489 if (curr >= 0 &&
490 memcmp(p[curr]->fsid, empty_uuid, BTRFS_FSID_SIZE) == 0) {
491 p[curr] = NULL;
492 } else if (curr == -1) {
493 p = ERR_PTR(-ENODATA);
495 return p;
497 if (avail == -1)
498 return ERR_PTR(-errno);
499 avail += old_avail;
501 i = 0;
502 while (i < avail) {
503 switch (state) {
504 case 0: /* start of file */
505 ret = scrub_kvread(&i,
506 sizeof(SCRUB_FILE_VERSION_PREFIX), avail, l,
507 SCRUB_FILE_VERSION_PREFIX, &version);
508 if (ret != 1)
509 _SCRUB_INVALID;
510 if (version != atoll(SCRUB_FILE_VERSION))
511 return ERR_PTR(-ENOTSUP);
512 state = 6;
513 continue;
514 case 1: /* start of line, alloc */
516 * this state makes sure we have a complete line in
517 * further processing, so we don't need wrap-tracking
518 * everywhere.
520 if (!eof && !memchr(l + i, '\n', avail - i))
521 goto again;
522 ++lineno;
523 if (curr > -1 && memcmp(p[curr]->fsid, empty_uuid,
524 BTRFS_FSID_SIZE) == 0) {
525 state = 2;
526 continue;
528 ++curr;
529 p = realloc(p, (curr + 2) * sizeof(*p));
530 if (p)
531 p[curr] = malloc(sizeof(**p));
532 if (!p || !p[curr])
533 return ERR_PTR(-errno);
534 memset(p[curr], 0, sizeof(**p));
535 p[curr + 1] = NULL;
536 ++state;
537 /* fall through */
538 case 2: /* start of line, skip space */
539 while (isspace(l[i]) && i < avail) {
540 if (l[i] == '\n')
541 ++lineno;
542 ++i;
544 if (i >= avail ||
545 (!eof && !memchr(l + i, '\n', avail - i)))
546 goto again;
547 ++state;
548 /* fall through */
549 case 3: /* read fsid */
550 if (i == avail)
551 continue;
552 for (j = 0; l[i + j] != ':' && i + j < avail; ++j)
554 if (i + j + 1 >= avail)
555 _SCRUB_INVALID;
556 if (j != 36)
557 _SCRUB_INVALID;
558 l[i + j] = '\0';
559 ret = uuid_parse(l + i, p[curr]->fsid);
560 if (ret)
561 _SCRUB_INVALID;
562 i += j + 1;
563 ++state;
564 /* fall through */
565 case 4: /* read dev id */
566 for (j = 0; isdigit(l[i + j]) && i+j < avail; ++j)
568 if (j == 0 || i + j + 1 >= avail)
569 _SCRUB_INVALID;
570 p[curr]->devid = atoll(&l[i]);
571 i += j + 1;
572 ++state;
573 /* fall through */
574 case 5: /* read key/value pair */
575 ret = 0;
576 _SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
577 &p[curr]->p);
578 _SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
579 &p[curr]->p);
580 _SCRUB_KVREAD(ret, &i, tree_extents_scrubbed, avail, l,
581 &p[curr]->p);
582 _SCRUB_KVREAD(ret, &i, data_bytes_scrubbed, avail, l,
583 &p[curr]->p);
584 _SCRUB_KVREAD(ret, &i, tree_bytes_scrubbed, avail, l,
585 &p[curr]->p);
586 _SCRUB_KVREAD(ret, &i, read_errors, avail, l,
587 &p[curr]->p);
588 _SCRUB_KVREAD(ret, &i, csum_errors, avail, l,
589 &p[curr]->p);
590 _SCRUB_KVREAD(ret, &i, verify_errors, avail, l,
591 &p[curr]->p);
592 _SCRUB_KVREAD(ret, &i, no_csum, avail, l,
593 &p[curr]->p);
594 _SCRUB_KVREAD(ret, &i, csum_discards, avail, l,
595 &p[curr]->p);
596 _SCRUB_KVREAD(ret, &i, super_errors, avail, l,
597 &p[curr]->p);
598 _SCRUB_KVREAD(ret, &i, malloc_errors, avail, l,
599 &p[curr]->p);
600 _SCRUB_KVREAD(ret, &i, uncorrectable_errors, avail, l,
601 &p[curr]->p);
602 _SCRUB_KVREAD(ret, &i, corrected_errors, avail, l,
603 &p[curr]->p);
604 _SCRUB_KVREAD(ret, &i, last_physical, avail, l,
605 &p[curr]->p);
606 _SCRUB_KVREAD(ret, &i, finished, avail, l,
607 &p[curr]->stats);
608 _SCRUB_KVREAD(ret, &i, t_start, avail, l,
609 (u64 *)&p[curr]->stats);
610 _SCRUB_KVREAD(ret, &i, t_resumed, avail, l,
611 (u64 *)&p[curr]->stats);
612 _SCRUB_KVREAD(ret, &i, duration, avail, l,
613 (u64 *)&p[curr]->stats);
614 _SCRUB_KVREAD(ret, &i, canceled, avail, l,
615 &p[curr]->stats);
616 if (ret != 1)
617 _SCRUB_INVALID;
618 ++state;
619 /* fall through */
620 case 6: /* after number */
621 if (l[i] == '|')
622 state = 5;
623 else if (l[i] == '\n')
624 state = 1;
625 else
626 _SCRUB_INVALID;
627 ++i;
628 continue;
629 case 99: /* skip rest of line */
630 skip:
631 state = 99;
632 do {
633 ++i;
634 if (l[i - 1] == '\n') {
635 state = 1;
636 break;
638 } while (i < avail);
639 continue;
641 BUG();
643 goto again;
646 static int scrub_write_buf(int fd, const void *data, int len)
648 int ret;
649 ret = write(fd, data, len);
650 return ret - len;
653 static int scrub_writev(int fd, char *buf, int max, const char *fmt, ...)
654 __attribute__ ((format (printf, 4, 5)));
655 static int scrub_writev(int fd, char *buf, int max, const char *fmt, ...)
657 int ret;
658 va_list args;
660 va_start(args, fmt);
661 ret = vsnprintf(buf, max, fmt, args);
662 va_end(args);
663 if (ret >= max)
664 return ret - max;
665 return scrub_write_buf(fd, buf, ret);
668 #define _SCRUB_SUM(dest, data, name) dest->scrub_args.progress.name = \
669 data->resumed->p.name + data->scrub_args.progress.name
671 static struct scrub_progress *scrub_resumed_stats(struct scrub_progress *data,
672 struct scrub_progress *dest)
674 if (!data->resumed || data->skip)
675 return data;
677 _SCRUB_SUM(dest, data, data_extents_scrubbed);
678 _SCRUB_SUM(dest, data, tree_extents_scrubbed);
679 _SCRUB_SUM(dest, data, data_bytes_scrubbed);
680 _SCRUB_SUM(dest, data, tree_bytes_scrubbed);
681 _SCRUB_SUM(dest, data, read_errors);
682 _SCRUB_SUM(dest, data, csum_errors);
683 _SCRUB_SUM(dest, data, verify_errors);
684 _SCRUB_SUM(dest, data, no_csum);
685 _SCRUB_SUM(dest, data, csum_discards);
686 _SCRUB_SUM(dest, data, super_errors);
687 _SCRUB_SUM(dest, data, malloc_errors);
688 _SCRUB_SUM(dest, data, uncorrectable_errors);
689 _SCRUB_SUM(dest, data, corrected_errors);
690 _SCRUB_SUM(dest, data, last_physical);
691 dest->stats.canceled = data->stats.canceled;
692 dest->stats.finished = data->stats.finished;
693 dest->stats.t_resumed = data->stats.t_start;
694 dest->stats.t_start = data->resumed->stats.t_start;
695 dest->stats.duration = data->resumed->stats.duration +
696 data->stats.duration;
697 dest->scrub_args.devid = data->scrub_args.devid;
698 return dest;
701 #define _SCRUB_KVWRITE(fd, buf, name, use) \
702 scrub_kvwrite(fd, buf, sizeof(buf), #name, \
703 use->scrub_args.progress.name)
705 #define _SCRUB_KVWRITE_STATS(fd, buf, name, use) \
706 scrub_kvwrite(fd, buf, sizeof(buf), #name, \
707 use->stats.name)
709 static int scrub_kvwrite(int fd, char *buf, int max, const char *key, u64 val)
711 return scrub_writev(fd, buf, max, "|%s:%lld", key, val);
714 static int scrub_write_file(int fd, const char *fsid,
715 struct scrub_progress *data, int n)
717 int ret = 0;
718 int i;
719 char buf[1024];
720 struct scrub_progress local;
721 struct scrub_progress *use;
723 if (n < 1)
724 return -EINVAL;
726 /* each -1 is to subtract one \0 byte, the + 2 is for ':' and '\n' */
727 ret = scrub_write_buf(fd, SCRUB_FILE_VERSION_PREFIX ":"
728 SCRUB_FILE_VERSION "\n",
729 (sizeof(SCRUB_FILE_VERSION_PREFIX) - 1) +
730 (sizeof(SCRUB_FILE_VERSION) - 1) + 2);
731 if (ret)
732 return -EOVERFLOW;
734 for (i = 0; i < n; ++i) {
735 use = scrub_resumed_stats(&data[i], &local);
736 if (scrub_write_buf(fd, fsid, strlen(fsid)) ||
737 scrub_write_buf(fd, ":", 1) ||
738 scrub_writev(fd, buf, sizeof(buf), "%lld",
739 use->scrub_args.devid) ||
740 scrub_write_buf(fd, buf, ret) ||
741 _SCRUB_KVWRITE(fd, buf, data_extents_scrubbed, use) ||
742 _SCRUB_KVWRITE(fd, buf, tree_extents_scrubbed, use) ||
743 _SCRUB_KVWRITE(fd, buf, data_bytes_scrubbed, use) ||
744 _SCRUB_KVWRITE(fd, buf, tree_bytes_scrubbed, use) ||
745 _SCRUB_KVWRITE(fd, buf, read_errors, use) ||
746 _SCRUB_KVWRITE(fd, buf, csum_errors, use) ||
747 _SCRUB_KVWRITE(fd, buf, verify_errors, use) ||
748 _SCRUB_KVWRITE(fd, buf, no_csum, use) ||
749 _SCRUB_KVWRITE(fd, buf, csum_discards, use) ||
750 _SCRUB_KVWRITE(fd, buf, super_errors, use) ||
751 _SCRUB_KVWRITE(fd, buf, malloc_errors, use) ||
752 _SCRUB_KVWRITE(fd, buf, uncorrectable_errors, use) ||
753 _SCRUB_KVWRITE(fd, buf, corrected_errors, use) ||
754 _SCRUB_KVWRITE(fd, buf, last_physical, use) ||
755 _SCRUB_KVWRITE_STATS(fd, buf, t_start, use) ||
756 _SCRUB_KVWRITE_STATS(fd, buf, t_resumed, use) ||
757 _SCRUB_KVWRITE_STATS(fd, buf, duration, use) ||
758 _SCRUB_KVWRITE_STATS(fd, buf, canceled, use) ||
759 _SCRUB_KVWRITE_STATS(fd, buf, finished, use) ||
760 scrub_write_buf(fd, "\n", 1)) {
761 return -EOVERFLOW;
765 return 0;
768 static int scrub_write_progress(pthread_mutex_t *m, const char *fsid,
769 struct scrub_progress *data, int n)
771 int ret;
772 int err;
773 int fd = -1;
774 int old;
776 ret = pthread_mutex_lock(m);
777 if (ret) {
778 err = -ret;
779 goto out;
782 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
783 if (ret) {
784 err = -ret;
785 goto out;
788 fd = scrub_open_file_w(SCRUB_DATA_FILE, fsid, "tmp");
789 if (fd < 0) {
790 err = fd;
791 goto out;
793 err = scrub_write_file(fd, fsid, data, n);
794 if (err)
795 goto out;
796 err = scrub_rename_file(SCRUB_DATA_FILE, fsid, "tmp");
797 if (err)
798 goto out;
800 out:
801 if (fd >= 0) {
802 ret = close(fd);
803 if (ret)
804 err = -errno;
807 ret = pthread_mutex_unlock(m);
808 if (ret && !err)
809 err = -ret;
811 ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
812 if (ret && !err)
813 err = -ret;
815 return err;
818 static void *scrub_one_dev(void *ctx)
820 struct scrub_progress *sp = ctx;
821 int ret;
822 struct timeval tv;
824 sp->stats.canceled = 0;
825 sp->stats.duration = 0;
826 sp->stats.finished = 0;
828 ret = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0,
829 IOPRIO_PRIO_VALUE(sp->ioprio_class,
830 sp->ioprio_classdata));
831 if (ret)
832 fprintf(stderr,
833 "WARNING: setting ioprio failed: %s (ignored).\n",
834 strerror(errno));
836 ret = ioctl(sp->fd, BTRFS_IOC_SCRUB, &sp->scrub_args);
837 gettimeofday(&tv, NULL);
838 sp->ret = ret;
839 sp->stats.duration = tv.tv_sec - sp->stats.t_start;
840 sp->stats.canceled = !!ret;
841 sp->ioctl_errno = errno;
842 ret = pthread_mutex_lock(&sp->progress_mutex);
843 if (ret)
844 return ERR_PTR(-ret);
845 sp->stats.finished = 1;
846 ret = pthread_mutex_unlock(&sp->progress_mutex);
847 if (ret)
848 return ERR_PTR(-ret);
850 return NULL;
853 static void *progress_one_dev(void *ctx)
855 struct scrub_progress *sp = ctx;
857 sp->ret = ioctl(sp->fd, BTRFS_IOC_SCRUB_PROGRESS, &sp->scrub_args);
858 sp->ioctl_errno = errno;
860 return NULL;
863 /* nb: returns a negative errno via ERR_PTR */
864 static void *scrub_progress_cycle(void *ctx)
866 int ret;
867 int perr = 0; /* positive / pthread error returns */
868 int old;
869 int i;
870 char fsid[BTRFS_UUID_UNPARSED_SIZE];
871 struct scrub_progress *sp;
872 struct scrub_progress *sp_last;
873 struct scrub_progress *sp_shared;
874 struct timeval tv;
875 struct scrub_progress_cycle *spc = ctx;
876 int ndev = spc->fi->num_devices;
877 int this = 1;
878 int last = 0;
879 int peer_fd = -1;
880 struct pollfd accept_poll_fd = {
881 .fd = spc->prg_fd,
882 .events = POLLIN,
883 .revents = 0,
885 struct pollfd write_poll_fd = {
886 .events = POLLOUT,
887 .revents = 0,
889 struct sockaddr_un peer;
890 socklen_t peer_size = sizeof(peer);
892 perr = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
893 if (perr)
894 goto out;
896 uuid_unparse(spc->fi->fsid, fsid);
898 for (i = 0; i < ndev; ++i) {
899 sp = &spc->progress[i];
900 sp_last = &spc->progress[i + ndev];
901 sp_shared = &spc->shared_progress[i];
902 sp->scrub_args.devid = sp_last->scrub_args.devid =
903 sp_shared->scrub_args.devid;
904 sp->fd = sp_last->fd = spc->fdmnt;
905 sp->stats.t_start = sp_last->stats.t_start =
906 sp_shared->stats.t_start;
907 sp->resumed = sp_last->resumed = sp_shared->resumed;
908 sp->skip = sp_last->skip = sp_shared->skip;
909 sp->stats.finished = sp_last->stats.finished =
910 sp_shared->stats.finished;
913 while (1) {
914 ret = poll(&accept_poll_fd, 1, 5 * 1000);
915 if (ret == -1) {
916 ret = -errno;
917 goto out;
919 if (ret)
920 peer_fd = accept(spc->prg_fd, (struct sockaddr *)&peer,
921 &peer_size);
922 gettimeofday(&tv, NULL);
923 this = (this + 1)%2;
924 last = (last + 1)%2;
925 for (i = 0; i < ndev; ++i) {
926 sp = &spc->progress[this * ndev + i];
927 sp_last = &spc->progress[last * ndev + i];
928 sp_shared = &spc->shared_progress[i];
929 if (sp->stats.finished)
930 continue;
931 progress_one_dev(sp);
932 sp->stats.duration = tv.tv_sec - sp->stats.t_start;
933 if (!sp->ret)
934 continue;
935 if (sp->ioctl_errno != ENOTCONN &&
936 sp->ioctl_errno != ENODEV) {
937 ret = -sp->ioctl_errno;
938 goto out;
941 * scrub finished or device removed, check the
942 * finished flag. if unset, just use the last
943 * result we got for the current write and go
944 * on. flag should be set on next cycle, then.
946 perr = pthread_mutex_lock(&sp_shared->progress_mutex);
947 if (perr)
948 goto out;
949 if (!sp_shared->stats.finished) {
950 perr = pthread_mutex_unlock(
951 &sp_shared->progress_mutex);
952 if (perr)
953 goto out;
954 memcpy(sp, sp_last, sizeof(*sp));
955 continue;
957 perr = pthread_mutex_unlock(&sp_shared->progress_mutex);
958 if (perr)
959 goto out;
960 memcpy(sp, sp_shared, sizeof(*sp));
961 memcpy(sp_last, sp_shared, sizeof(*sp));
963 if (peer_fd != -1) {
964 write_poll_fd.fd = peer_fd;
965 ret = poll(&write_poll_fd, 1, 0);
966 if (ret == -1) {
967 ret = -errno;
968 goto out;
970 if (ret) {
971 ret = scrub_write_file(
972 peer_fd, fsid,
973 &spc->progress[this * ndev], ndev);
974 if (ret)
975 goto out;
977 close(peer_fd);
978 peer_fd = -1;
980 if (!spc->do_record)
981 continue;
982 ret = scrub_write_progress(spc->write_mutex, fsid,
983 &spc->progress[this * ndev], ndev);
984 if (ret)
985 goto out;
987 out:
988 if (peer_fd != -1)
989 close(peer_fd);
990 if (perr)
991 ret = -perr;
992 return ERR_PTR(ret);
995 static struct scrub_file_record *last_dev_scrub(
996 struct scrub_file_record *const *const past_scrubs, u64 devid)
998 int i;
1000 if (!past_scrubs || IS_ERR(past_scrubs))
1001 return NULL;
1003 for (i = 0; past_scrubs[i]; ++i)
1004 if (past_scrubs[i]->devid == devid)
1005 return past_scrubs[i];
1007 return NULL;
1010 static int mkdir_p(char *path)
1012 int i;
1013 int ret;
1015 for (i = 1; i < strlen(path); ++i) {
1016 if (path[i] != '/')
1017 continue;
1018 path[i] = '\0';
1019 ret = mkdir(path, 0777);
1020 if (ret && errno != EEXIST)
1021 return -errno;
1022 path[i] = '/';
1025 return 0;
1028 static int is_scrub_running_on_fs(struct btrfs_ioctl_fs_info_args *fi_args,
1029 struct btrfs_ioctl_dev_info_args *di_args,
1030 struct scrub_file_record **past_scrubs)
1032 int i;
1034 if (!fi_args || !di_args || !past_scrubs)
1035 return 0;
1037 for (i = 0; i < fi_args->num_devices; i++) {
1038 struct scrub_file_record *sfr =
1039 last_dev_scrub(past_scrubs, di_args[i].devid);
1041 if (!sfr)
1042 continue;
1043 if (!(sfr->stats.finished || sfr->stats.canceled))
1044 return 1;
1046 return 0;
1049 static const char * const cmd_scrub_start_usage[];
1050 static const char * const cmd_scrub_resume_usage[];
1052 static int scrub_start(int argc, char **argv, int resume)
1054 int fdmnt;
1055 int prg_fd = -1;
1056 int fdres = -1;
1057 int ret;
1058 pid_t pid;
1059 int c;
1060 int i;
1061 int err = 0;
1062 int e_uncorrectable = 0;
1063 int e_correctable = 0;
1064 int print_raw = 0;
1065 char *path;
1066 int do_background = 1;
1067 int do_wait = 0;
1068 int do_print = 0;
1069 int do_quiet = 0;
1070 int do_record = 1;
1071 int readonly = 0;
1072 int do_stats_per_dev = 0;
1073 int ioprio_class = IOPRIO_CLASS_IDLE;
1074 int ioprio_classdata = 0;
1075 int n_start = 0;
1076 int n_skip = 0;
1077 int n_resume = 0;
1078 struct btrfs_ioctl_fs_info_args fi_args;
1079 struct btrfs_ioctl_dev_info_args *di_args = NULL;
1080 struct scrub_progress *sp = NULL;
1081 struct scrub_fs_stat fs_stat;
1082 struct timeval tv;
1083 struct sockaddr_un addr = {
1084 .sun_family = AF_UNIX,
1086 pthread_t *t_devs = NULL;
1087 pthread_t t_prog;
1088 pthread_attr_t t_attr;
1089 struct scrub_file_record **past_scrubs = NULL;
1090 struct scrub_file_record *last_scrub = NULL;
1091 char *datafile = strdup(SCRUB_DATA_FILE);
1092 char fsid[BTRFS_UUID_UNPARSED_SIZE];
1093 char sock_path[BTRFS_PATH_NAME_MAX + 1] = "";
1094 struct scrub_progress_cycle spc;
1095 pthread_mutex_t spc_write_mutex = PTHREAD_MUTEX_INITIALIZER;
1096 void *terr;
1097 u64 devid;
1098 DIR *dirstream = NULL;
1099 int force = 0;
1100 int nothing_to_resume = 0;
1102 optind = 1;
1103 while ((c = getopt(argc, argv, "BdqrRc:n:f")) != -1) {
1104 switch (c) {
1105 case 'B':
1106 do_background = 0;
1107 do_wait = 1;
1108 do_print = 1;
1109 break;
1110 case 'd':
1111 do_stats_per_dev = 1;
1112 break;
1113 case 'q':
1114 do_quiet = 1;
1115 break;
1116 case 'r':
1117 readonly = 1;
1118 break;
1119 case 'R':
1120 print_raw = 1;
1121 break;
1122 case 'c':
1123 ioprio_class = (int)strtol(optarg, NULL, 10);
1124 break;
1125 case 'n':
1126 ioprio_classdata = (int)strtol(optarg, NULL, 10);
1127 break;
1128 case 'f':
1129 force = 1;
1130 break;
1131 case '?':
1132 default:
1133 usage(resume ? cmd_scrub_resume_usage :
1134 cmd_scrub_start_usage);
1138 /* try to catch most error cases before forking */
1140 if (check_argc_exact(argc - optind, 1)) {
1141 usage(resume ? cmd_scrub_resume_usage :
1142 cmd_scrub_start_usage);
1145 spc.progress = NULL;
1146 if (do_quiet && do_print)
1147 do_print = 0;
1149 if (mkdir_p(datafile)) {
1150 ERR(!do_quiet, "WARNING: cannot create scrub data "
1151 "file, mkdir %s failed: %s. Status recording "
1152 "disabled\n", datafile, strerror(errno));
1153 do_record = 0;
1155 free(datafile);
1157 path = argv[optind];
1159 fdmnt = open_path_or_dev_mnt(path, &dirstream);
1161 if (fdmnt < 0) {
1162 ERR(!do_quiet, "ERROR: can't access '%s'\n", path);
1163 return 1;
1166 ret = get_fs_info(path, &fi_args, &di_args);
1167 if (ret) {
1168 ERR(!do_quiet, "ERROR: getting dev info for scrub failed: "
1169 "%s\n", strerror(-ret));
1170 err = 1;
1171 goto out;
1173 if (!fi_args.num_devices) {
1174 ERR(!do_quiet, "ERROR: no devices found\n");
1175 err = 1;
1176 goto out;
1179 uuid_unparse(fi_args.fsid, fsid);
1180 fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid);
1181 if (fdres < 0 && fdres != -ENOENT) {
1182 ERR(!do_quiet, "WARNING: failed to open status file: "
1183 "%s\n", strerror(-fdres));
1184 } else if (fdres >= 0) {
1185 past_scrubs = scrub_read_file(fdres, !do_quiet);
1186 if (IS_ERR(past_scrubs))
1187 ERR(!do_quiet, "WARNING: failed to read status file: "
1188 "%s\n", strerror(-PTR_ERR(past_scrubs)));
1189 close(fdres);
1193 * check whether any involved device is already busy running a
1194 * scrub. This would cause damaged status messages and the state
1195 * "aborted" without the explanation that a scrub was already
1196 * running. Therefore check it first, prevent it and give some
1197 * feedback to the user if scrub is already running.
1198 * Note that if scrub is started with a block device as the
1199 * parameter, only that particular block device is checked. It
1200 * is a normal mode of operation to start scrub on multiple
1201 * single devices, there is no reason to prevent this.
1203 if (!force && is_scrub_running_on_fs(&fi_args, di_args, past_scrubs)) {
1204 ERR(!do_quiet,
1205 "ERROR: scrub is already running.\n"
1206 "To cancel use 'btrfs scrub cancel %s'.\n"
1207 "To see the status use 'btrfs scrub status [-d] %s'.\n",
1208 path, path);
1209 err = 1;
1210 goto out;
1213 t_devs = malloc(fi_args.num_devices * sizeof(*t_devs));
1214 sp = calloc(fi_args.num_devices, sizeof(*sp));
1215 spc.progress = calloc(fi_args.num_devices * 2, sizeof(*spc.progress));
1217 if (!t_devs || !sp || !spc.progress) {
1218 ERR(!do_quiet, "ERROR: scrub failed: %s", strerror(errno));
1219 err = 1;
1220 goto out;
1223 ret = pthread_attr_init(&t_attr);
1224 if (ret) {
1225 ERR(!do_quiet, "ERROR: pthread_attr_init failed: %s\n",
1226 strerror(ret));
1227 err = 1;
1228 goto out;
1231 for (i = 0; i < fi_args.num_devices; ++i) {
1232 devid = di_args[i].devid;
1233 ret = pthread_mutex_init(&sp[i].progress_mutex, NULL);
1234 if (ret) {
1235 ERR(!do_quiet, "ERROR: pthread_mutex_init failed: "
1236 "%s\n", strerror(ret));
1237 err = 1;
1238 goto out;
1240 last_scrub = last_dev_scrub(past_scrubs, devid);
1241 sp[i].scrub_args.devid = devid;
1242 sp[i].fd = fdmnt;
1243 if (resume && last_scrub && (last_scrub->stats.canceled ||
1244 !last_scrub->stats.finished)) {
1245 ++n_resume;
1246 sp[i].scrub_args.start = last_scrub->p.last_physical;
1247 sp[i].resumed = last_scrub;
1248 } else if (resume) {
1249 ++n_skip;
1250 sp[i].skip = 1;
1251 sp[i].resumed = last_scrub;
1252 continue;
1253 } else {
1254 ++n_start;
1255 sp[i].scrub_args.start = 0ll;
1256 sp[i].resumed = NULL;
1258 sp[i].skip = 0;
1259 sp[i].scrub_args.end = (u64)-1ll;
1260 sp[i].scrub_args.flags = readonly ? BTRFS_SCRUB_READONLY : 0;
1261 sp[i].ioprio_class = ioprio_class;
1262 sp[i].ioprio_classdata = ioprio_classdata;
1265 if (!n_start && !n_resume) {
1266 if (!do_quiet)
1267 printf("scrub: nothing to resume for %s, fsid %s\n",
1268 path, fsid);
1269 nothing_to_resume = 1;
1270 goto out;
1273 ret = prg_fd = socket(AF_UNIX, SOCK_STREAM, 0);
1274 while (ret != -1) {
1275 ret = scrub_datafile(SCRUB_PROGRESS_SOCKET_PATH, fsid, NULL,
1276 sock_path, sizeof(sock_path));
1277 /* ignore EOVERFLOW, try using a shorter path for the socket */
1278 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
1279 strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);
1280 ret = bind(prg_fd, (struct sockaddr *)&addr, sizeof(addr));
1281 if (ret != -1 || errno != EADDRINUSE)
1282 break;
1284 * bind failed with EADDRINUSE. so let's see if anyone answers
1285 * when we make a call to the socket ...
1287 ret = connect(prg_fd, (struct sockaddr *)&addr, sizeof(addr));
1288 if (!ret || errno != ECONNREFUSED) {
1289 /* ... yes, so scrub must be running. error out */
1290 fprintf(stderr, "ERROR: scrub already running\n");
1291 close(prg_fd);
1292 prg_fd = -1;
1293 goto out;
1296 * ... no, this means someone left us alone with an unused
1297 * socket in the file system. remove it and try again.
1299 ret = unlink(sock_path);
1301 if (ret != -1)
1302 ret = listen(prg_fd, 100);
1303 if (ret == -1) {
1304 ERR(!do_quiet, "WARNING: failed to open the progress status "
1305 "socket at %s: %s. Progress cannot be queried\n",
1306 sock_path[0] ? sock_path : SCRUB_PROGRESS_SOCKET_PATH,
1307 strerror(errno));
1308 if (prg_fd != -1) {
1309 close(prg_fd);
1310 prg_fd = -1;
1311 if (sock_path[0])
1312 unlink(sock_path);
1316 if (do_record) {
1317 /* write all-zero progress file for a start */
1318 ret = scrub_write_progress(&spc_write_mutex, fsid, sp,
1319 fi_args.num_devices);
1320 if (ret) {
1321 ERR(!do_quiet, "WARNING: failed to write the progress "
1322 "status file: %s. Status recording disabled\n",
1323 strerror(-ret));
1324 do_record = 0;
1328 if (do_background) {
1329 pid = fork();
1330 if (pid == -1) {
1331 ERR(!do_quiet, "ERROR: cannot scrub, fork failed: "
1332 "%s\n", strerror(errno));
1333 err = 1;
1334 goto out;
1337 if (pid) {
1338 int stat;
1339 scrub_handle_sigint_parent();
1340 if (!do_quiet)
1341 printf("scrub %s on %s, fsid %s (pid=%d)\n",
1342 n_start ? "started" : "resumed",
1343 path, fsid, pid);
1344 if (!do_wait) {
1345 err = 0;
1346 goto out;
1348 ret = wait(&stat);
1349 if (ret != pid) {
1350 ERR(!do_quiet, "ERROR: wait failed: (ret=%d) "
1351 "%s\n", ret, strerror(errno));
1352 err = 1;
1353 goto out;
1355 if (!WIFEXITED(stat) || WEXITSTATUS(stat)) {
1356 ERR(!do_quiet, "ERROR: scrub process failed\n");
1357 err = WIFEXITED(stat) ? WEXITSTATUS(stat) : -1;
1358 goto out;
1360 err = 0;
1361 goto out;
1365 scrub_handle_sigint_child(fdmnt);
1367 for (i = 0; i < fi_args.num_devices; ++i) {
1368 if (sp[i].skip) {
1369 sp[i].scrub_args.progress = sp[i].resumed->p;
1370 sp[i].stats = sp[i].resumed->stats;
1371 sp[i].ret = 0;
1372 sp[i].stats.finished = 1;
1373 continue;
1375 devid = di_args[i].devid;
1376 gettimeofday(&tv, NULL);
1377 sp[i].stats.t_start = tv.tv_sec;
1378 ret = pthread_create(&t_devs[i], &t_attr,
1379 scrub_one_dev, &sp[i]);
1380 if (ret) {
1381 if (do_print)
1382 fprintf(stderr, "ERROR: creating "
1383 "scrub_one_dev[%llu] thread failed: "
1384 "%s\n", devid, strerror(ret));
1385 err = 1;
1386 goto out;
1390 spc.fdmnt = fdmnt;
1391 spc.prg_fd = prg_fd;
1392 spc.do_record = do_record;
1393 spc.write_mutex = &spc_write_mutex;
1394 spc.shared_progress = sp;
1395 spc.fi = &fi_args;
1396 ret = pthread_create(&t_prog, &t_attr, scrub_progress_cycle, &spc);
1397 if (ret) {
1398 if (do_print)
1399 fprintf(stderr, "ERROR: creating progress thread "
1400 "failed: %s\n", strerror(ret));
1401 err = 1;
1402 goto out;
1405 err = 0;
1406 for (i = 0; i < fi_args.num_devices; ++i) {
1407 if (sp[i].skip)
1408 continue;
1409 devid = di_args[i].devid;
1410 ret = pthread_join(t_devs[i], NULL);
1411 if (ret) {
1412 if (do_print)
1413 fprintf(stderr, "ERROR: pthread_join failed "
1414 "for scrub_one_dev[%llu]: %s\n", devid,
1415 strerror(ret));
1416 ++err;
1417 continue;
1419 if (sp[i].ret && sp[i].ioctl_errno == ENODEV) {
1420 if (do_print)
1421 fprintf(stderr, "WARNING: device %lld not "
1422 "present\n", devid);
1423 continue;
1425 if (sp[i].ret && sp[i].ioctl_errno == ECANCELED) {
1426 ++err;
1427 } else if (sp[i].ret) {
1428 if (do_print)
1429 fprintf(stderr, "ERROR: scrubbing %s failed "
1430 "for device id %lld (%s)\n", path,
1431 devid, strerror(sp[i].ioctl_errno));
1432 ++err;
1433 continue;
1435 if (sp[i].scrub_args.progress.uncorrectable_errors > 0)
1436 e_uncorrectable++;
1437 if (sp[i].scrub_args.progress.corrected_errors > 0
1438 || sp[i].scrub_args.progress.unverified_errors > 0)
1439 e_correctable++;
1442 if (do_print) {
1443 const char *append = "done";
1444 if (!do_stats_per_dev)
1445 init_fs_stat(&fs_stat);
1446 for (i = 0; i < fi_args.num_devices; ++i) {
1447 if (do_stats_per_dev) {
1448 print_scrub_dev(&di_args[i],
1449 &sp[i].scrub_args.progress,
1450 print_raw,
1451 sp[i].ret ? "canceled" : "done",
1452 &sp[i].stats);
1453 } else {
1454 if (sp[i].ret)
1455 append = "canceled";
1456 add_to_fs_stat(&sp[i].scrub_args.progress,
1457 &sp[i].stats, &fs_stat);
1460 if (!do_stats_per_dev) {
1461 printf("scrub %s for %s\n", append, fsid);
1462 print_fs_stat(&fs_stat, print_raw);
1466 ret = pthread_cancel(t_prog);
1467 if (!ret)
1468 ret = pthread_join(t_prog, &terr);
1470 /* check for errors from the handling of the progress thread */
1471 if (do_print && ret) {
1472 fprintf(stderr, "ERROR: progress thread handling failed: %s\n",
1473 strerror(ret));
1476 /* check for errors returned from the progress thread itself */
1477 if (do_print && terr && terr != PTHREAD_CANCELED) {
1478 fprintf(stderr, "ERROR: recording progress "
1479 "failed: %s\n", strerror(-PTR_ERR(terr)));
1482 if (do_record) {
1483 ret = scrub_write_progress(&spc_write_mutex, fsid, sp,
1484 fi_args.num_devices);
1485 if (ret && do_print) {
1486 fprintf(stderr, "ERROR: failed to record the result: "
1487 "%s\n", strerror(-ret));
1491 scrub_handle_sigint_child(-1);
1493 out:
1494 free_history(past_scrubs);
1495 free(di_args);
1496 free(t_devs);
1497 free(sp);
1498 free(spc.progress);
1499 if (prg_fd > -1) {
1500 close(prg_fd);
1501 if (sock_path[0])
1502 unlink(sock_path);
1504 close_file_or_dir(fdmnt, dirstream);
1506 if (nothing_to_resume)
1507 return 2;
1508 if (err)
1509 return 1;
1510 if (e_correctable)
1511 return 3;
1512 if (e_uncorrectable)
1513 return 4;
1514 return 0;
1517 static const char * const cmd_scrub_start_usage[] = {
1518 "btrfs scrub start [-BdqrRf] [-c ioprio_class -n ioprio_classdata] <path>|<device>",
1519 "Start a new scrub",
1521 "-B do not background",
1522 "-d stats per device (-B only)",
1523 "-q be quiet",
1524 "-r read only mode",
1525 "-R raw print mode, print full data instead of summary"
1526 "-c set ioprio class (see ionice(1) manpage)",
1527 "-n set ioprio classdata (see ionice(1) manpage)",
1528 "-f force to skip checking whether scrub has started/resumed in userspace ",
1529 " this is useful when scrub stats record file is damaged",
1530 NULL
1533 static int cmd_scrub_start(int argc, char **argv)
1535 return scrub_start(argc, argv, 0);
1538 static const char * const cmd_scrub_cancel_usage[] = {
1539 "btrfs scrub cancel <path>|<device>",
1540 "Cancel a running scrub",
1541 NULL
1544 static int cmd_scrub_cancel(int argc, char **argv)
1546 char *path;
1547 int ret;
1548 int fdmnt = -1;
1549 DIR *dirstream = NULL;
1551 if (check_argc_exact(argc, 2))
1552 usage(cmd_scrub_cancel_usage);
1554 path = argv[1];
1556 fdmnt = open_path_or_dev_mnt(path, &dirstream);
1557 if (fdmnt < 0) {
1558 fprintf(stderr, "ERROR: could not open %s: %s\n",
1559 path, strerror(errno));
1560 ret = 1;
1561 goto out;
1564 ret = ioctl(fdmnt, BTRFS_IOC_SCRUB_CANCEL, NULL);
1566 if (ret < 0) {
1567 fprintf(stderr, "ERROR: scrub cancel failed on %s: %s\n", path,
1568 errno == ENOTCONN ? "not running" : strerror(errno));
1569 if (errno == ENOTCONN)
1570 ret = 2;
1571 else
1572 ret = 1;
1573 goto out;
1576 ret = 0;
1577 printf("scrub cancelled\n");
1579 out:
1580 close_file_or_dir(fdmnt, dirstream);
1581 return ret;
1584 static const char * const cmd_scrub_resume_usage[] = {
1585 "btrfs scrub resume [-BdqrR] [-c ioprio_class -n ioprio_classdata] <path>|<device>",
1586 "Resume previously canceled or interrupted scrub",
1588 "-B do not background",
1589 "-d stats per device (-B only)",
1590 "-q be quiet",
1591 "-r read only mode",
1592 "-c set ioprio class (see ionice(1) manpage)",
1593 "-n set ioprio classdata (see ionice(1) manpage)",
1594 NULL
1597 static int cmd_scrub_resume(int argc, char **argv)
1599 return scrub_start(argc, argv, 1);
1602 static const char * const cmd_scrub_status_usage[] = {
1603 "btrfs scrub status [-dR] <path>|<device>",
1604 "Show status of running or finished scrub",
1606 "-d stats per device",
1607 "-R print raw stats",
1608 NULL
1611 static int cmd_scrub_status(int argc, char **argv)
1613 char *path;
1614 struct btrfs_ioctl_fs_info_args fi_args;
1615 struct btrfs_ioctl_dev_info_args *di_args = NULL;
1616 struct scrub_file_record **past_scrubs = NULL;
1617 struct scrub_file_record *last_scrub;
1618 struct scrub_fs_stat fs_stat;
1619 struct sockaddr_un addr = {
1620 .sun_family = AF_UNIX,
1622 int ret;
1623 int i;
1624 int fdmnt;
1625 int print_raw = 0;
1626 int do_stats_per_dev = 0;
1627 int c;
1628 char fsid[BTRFS_UUID_UNPARSED_SIZE];
1629 int fdres = -1;
1630 int err = 0;
1631 DIR *dirstream = NULL;
1633 optind = 1;
1634 while ((c = getopt(argc, argv, "dR")) != -1) {
1635 switch (c) {
1636 case 'd':
1637 do_stats_per_dev = 1;
1638 break;
1639 case 'R':
1640 print_raw = 1;
1641 break;
1642 case '?':
1643 default:
1644 usage(cmd_scrub_status_usage);
1648 if (check_argc_exact(argc - optind, 1))
1649 usage(cmd_scrub_status_usage);
1651 path = argv[optind];
1653 fdmnt = open_path_or_dev_mnt(path, &dirstream);
1655 if (fdmnt < 0) {
1656 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
1657 return 1;
1660 ret = get_fs_info(path, &fi_args, &di_args);
1661 if (ret) {
1662 fprintf(stderr, "ERROR: getting dev info for scrub failed: "
1663 "%s\n", strerror(-ret));
1664 err = 1;
1665 goto out;
1667 if (!fi_args.num_devices) {
1668 fprintf(stderr, "ERROR: no devices found\n");
1669 err = 1;
1670 goto out;
1673 uuid_unparse(fi_args.fsid, fsid);
1675 fdres = socket(AF_UNIX, SOCK_STREAM, 0);
1676 if (fdres == -1) {
1677 fprintf(stderr, "ERROR: failed to create socket to "
1678 "receive progress information: %s\n",
1679 strerror(errno));
1680 err = 1;
1681 goto out;
1683 scrub_datafile(SCRUB_PROGRESS_SOCKET_PATH, fsid,
1684 NULL, addr.sun_path, sizeof(addr.sun_path));
1685 /* ignore EOVERFLOW, just use shorter name and hope for the best */
1686 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
1687 ret = connect(fdres, (struct sockaddr *)&addr, sizeof(addr));
1688 if (ret == -1) {
1689 close(fdres);
1690 fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid);
1691 if (fdres < 0 && fdres != -ENOENT) {
1692 fprintf(stderr, "WARNING: failed to open status file: "
1693 "%s\n", strerror(-fdres));
1694 err = 1;
1695 goto out;
1699 if (fdres >= 0) {
1700 past_scrubs = scrub_read_file(fdres, 1);
1701 if (IS_ERR(past_scrubs))
1702 fprintf(stderr, "WARNING: failed to read status: %s\n",
1703 strerror(-PTR_ERR(past_scrubs)));
1706 printf("scrub status for %s\n", fsid);
1708 if (do_stats_per_dev) {
1709 for (i = 0; i < fi_args.num_devices; ++i) {
1710 last_scrub = last_dev_scrub(past_scrubs,
1711 di_args[i].devid);
1712 if (!last_scrub) {
1713 print_scrub_dev(&di_args[i], NULL, print_raw,
1714 NULL, NULL);
1715 continue;
1717 print_scrub_dev(&di_args[i], &last_scrub->p, print_raw,
1718 last_scrub->stats.finished ?
1719 "history" : "status",
1720 &last_scrub->stats);
1722 } else {
1723 init_fs_stat(&fs_stat);
1724 for (i = 0; i < fi_args.num_devices; ++i) {
1725 last_scrub = last_dev_scrub(past_scrubs,
1726 di_args[i].devid);
1727 if (!last_scrub)
1728 continue;
1729 add_to_fs_stat(&last_scrub->p, &last_scrub->stats,
1730 &fs_stat);
1732 print_fs_stat(&fs_stat, print_raw);
1735 out:
1736 free_history(past_scrubs);
1737 free(di_args);
1738 if (fdres > -1)
1739 close(fdres);
1740 close_file_or_dir(fdmnt, dirstream);
1742 return !!err;
1745 const struct cmd_group scrub_cmd_group = {
1746 scrub_cmd_group_usage, NULL, {
1747 { "start", cmd_scrub_start, cmd_scrub_start_usage, NULL, 0 },
1748 { "cancel", cmd_scrub_cancel, cmd_scrub_cancel_usage, NULL, 0 },
1749 { "resume", cmd_scrub_resume, cmd_scrub_resume_usage, NULL, 0 },
1750 { "status", cmd_scrub_status, cmd_scrub_status_usage, NULL, 0 },
1751 NULL_CMD_STRUCT
1755 int cmd_scrub(int argc, char **argv)
1757 return handle_command_group(&scrub_cmd_group, argc, argv);