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>
24 #include <sys/types.h>
25 #include <sys/socket.h>
29 #include <uuid/uuid.h>
45 static const char * const scrub_cmd_group_usage[] = {
46 "btrfs scrub <command> [options] <path>|<device>",
50 #define SCRUB_DATA_FILE "/var/lib/btrfs/scrub.status"
51 #define SCRUB_PROGRESS_SOCKET_PATH "/var/lib/btrfs/scrub.progress"
52 #define SCRUB_FILE_VERSION_PREFIX "scrub status"
53 #define SCRUB_FILE_VERSION "1"
63 struct scrub_progress {
64 struct btrfs_ioctl_scrub_args scrub_args;
68 struct scrub_stats stats;
69 struct scrub_file_record *resumed;
71 pthread_mutex_t progress_mutex;
74 struct scrub_file_record {
75 u8 fsid[BTRFS_FSID_SIZE];
77 struct scrub_stats stats;
78 struct btrfs_scrub_progress p;
81 struct scrub_progress_cycle {
85 struct btrfs_ioctl_fs_info_args *fi;
86 struct scrub_progress *progress;
87 struct scrub_progress *shared_progress;
88 pthread_mutex_t *write_mutex;
91 struct scrub_fs_stat {
92 struct btrfs_scrub_progress p;
97 static void print_scrub_full(struct btrfs_scrub_progress *sp)
99 printf("\tdata_extents_scrubbed: %lld\n", sp->data_extents_scrubbed);
100 printf("\ttree_extents_scrubbed: %lld\n", sp->tree_extents_scrubbed);
101 printf("\tdata_bytes_scrubbed: %lld\n", sp->data_bytes_scrubbed);
102 printf("\ttree_bytes_scrubbed: %lld\n", sp->tree_bytes_scrubbed);
103 printf("\tread_errors: %lld\n", sp->read_errors);
104 printf("\tcsum_errors: %lld\n", sp->csum_errors);
105 printf("\tverify_errors: %lld\n", sp->verify_errors);
106 printf("\tno_csum: %lld\n", sp->no_csum);
107 printf("\tcsum_discards: %lld\n", sp->csum_discards);
108 printf("\tsuper_errors: %lld\n", sp->super_errors);
109 printf("\tmalloc_errors: %lld\n", sp->malloc_errors);
110 printf("\tuncorrectable_errors: %lld\n", sp->uncorrectable_errors);
111 printf("\tunverified_errors: %lld\n", sp->unverified_errors);
112 printf("\tcorrected_errors: %lld\n", sp->corrected_errors);
113 printf("\tlast_physical: %lld\n", sp->last_physical);
116 #define ERR(test, ...) do { \
118 fprintf(stderr, __VA_ARGS__); \
121 #define PRINT_SCRUB_ERROR(test, desc) do { \
123 printf(" %s=%llu", desc, test); \
126 static void print_scrub_summary(struct btrfs_scrub_progress *p)
132 err_cnt = p->read_errors +
137 err_cnt2 = p->corrected_errors + p->uncorrectable_errors;
139 if (p->malloc_errors)
140 printf("*** WARNING: memory allocation failed while scrubbing. "
141 "results may be inaccurate\n");
142 bytes = pretty_sizes(p->data_bytes_scrubbed + p->tree_bytes_scrubbed);
143 printf("\ttotal bytes scrubbed: %s with %llu errors\n", bytes,
144 max(err_cnt, err_cnt2));
146 if (err_cnt || err_cnt2) {
147 printf("\terror details:");
148 PRINT_SCRUB_ERROR(p->read_errors, "read");
149 PRINT_SCRUB_ERROR(p->super_errors, "super");
150 PRINT_SCRUB_ERROR(p->verify_errors, "verify");
151 PRINT_SCRUB_ERROR(p->csum_errors, "csum");
153 printf("\tcorrected errors: %llu, uncorrectable errors: %llu, "
154 "unverified errors: %llu\n", p->corrected_errors,
155 p->uncorrectable_errors, p->unverified_errors);
159 #define _SCRUB_FS_STAT(p, name, fs_stat) do { \
160 fs_stat->p.name += p->name; \
163 #define _SCRUB_FS_STAT_MIN(ss, name, fs_stat) \
165 if (fs_stat->s.name > ss->name) { \
166 fs_stat->s.name = ss->name; \
170 #define _SCRUB_FS_STAT_ZMIN(ss, name, fs_stat) \
172 if (!fs_stat->s.name || fs_stat->s.name > ss->name) { \
173 fs_stat->s.name = ss->name; \
177 #define _SCRUB_FS_STAT_ZMAX(ss, name, fs_stat) \
179 if (!(fs_stat)->s.name || (fs_stat)->s.name < (ss)->name) { \
180 (fs_stat)->s.name = (ss)->name; \
184 static void add_to_fs_stat(struct btrfs_scrub_progress *p,
185 struct scrub_stats *ss,
186 struct scrub_fs_stat *fs_stat)
188 _SCRUB_FS_STAT(p, data_extents_scrubbed, fs_stat);
189 _SCRUB_FS_STAT(p, tree_extents_scrubbed, fs_stat);
190 _SCRUB_FS_STAT(p, data_bytes_scrubbed, fs_stat);
191 _SCRUB_FS_STAT(p, tree_bytes_scrubbed, fs_stat);
192 _SCRUB_FS_STAT(p, read_errors, fs_stat);
193 _SCRUB_FS_STAT(p, csum_errors, fs_stat);
194 _SCRUB_FS_STAT(p, verify_errors, fs_stat);
195 _SCRUB_FS_STAT(p, no_csum, fs_stat);
196 _SCRUB_FS_STAT(p, csum_discards, fs_stat);
197 _SCRUB_FS_STAT(p, super_errors, fs_stat);
198 _SCRUB_FS_STAT(p, malloc_errors, fs_stat);
199 _SCRUB_FS_STAT(p, uncorrectable_errors, fs_stat);
200 _SCRUB_FS_STAT(p, corrected_errors, fs_stat);
201 _SCRUB_FS_STAT(p, last_physical, fs_stat);
202 _SCRUB_FS_STAT_ZMIN(ss, t_start, fs_stat);
203 _SCRUB_FS_STAT_ZMIN(ss, t_resumed, fs_stat);
204 _SCRUB_FS_STAT_ZMAX(ss, duration, fs_stat);
205 _SCRUB_FS_STAT_ZMAX(ss, canceled, fs_stat);
206 _SCRUB_FS_STAT_MIN(ss, finished, fs_stat);
209 static void init_fs_stat(struct scrub_fs_stat *fs_stat)
211 memset(fs_stat, 0, sizeof(*fs_stat));
212 fs_stat->s.finished = 1;
215 static void _print_scrub_ss(struct scrub_stats *ss)
220 if (!ss || !ss->t_start) {
221 printf("\tno stats available\n");
225 localtime_r(&ss->t_resumed, &tm);
226 strftime(t, sizeof(t), "%c", &tm);
227 t[sizeof(t) - 1] = '\0';
228 printf("\tscrub resumed at %s", t);
230 localtime_r(&ss->t_start, &tm);
231 strftime(t, sizeof(t), "%c", &tm);
232 t[sizeof(t) - 1] = '\0';
233 printf("\tscrub started at %s", t);
235 if (ss->finished && !ss->canceled) {
236 printf(" and finished after %llu seconds\n",
238 } else if (ss->canceled) {
239 printf(" and was aborted after %llu seconds\n",
242 printf(", running for %llu seconds\n", ss->duration);
246 static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
247 struct btrfs_scrub_progress *p, int raw,
248 const char *append, struct scrub_stats *ss)
250 printf("scrub device %s (id %llu) %s\n", di->path, di->devid,
251 append ? append : "");
259 print_scrub_summary(p);
263 static void print_fs_stat(struct scrub_fs_stat *fs_stat, int raw)
265 _print_scrub_ss(&fs_stat->s);
268 print_scrub_full(&fs_stat->p);
270 print_scrub_summary(&fs_stat->p);
273 static void free_history(struct scrub_file_record **last_scrubs)
275 struct scrub_file_record **l = last_scrubs;
284 * cancels a running scrub and makes the master process record the current
285 * progress status before exiting.
287 static int cancel_fd = -1;
288 static void scrub_sigint_record_progress(int signal)
290 ioctl(cancel_fd, BTRFS_IOC_SCRUB_CANCEL, NULL);
293 static int scrub_handle_sigint_parent(void)
295 struct sigaction sa = {
296 .sa_handler = SIG_IGN,
297 .sa_flags = SA_RESTART,
300 return sigaction(SIGINT, &sa, NULL);
303 static int scrub_handle_sigint_child(int fd)
305 struct sigaction sa = {
306 .sa_handler = fd == -1 ? SIG_DFL : scrub_sigint_record_progress,
310 return sigaction(SIGINT, &sa, NULL);
313 static int scrub_datafile(const char *fn_base, const char *fn_local,
314 const char *fn_tmp, char *datafile, int size)
319 datafile[end + 1] = '\0';
320 strncpy(datafile, fn_base, end);
321 ret = strlen(datafile);
327 strncpy(datafile + ret + 1, fn_local, end - ret - 1);
328 ret = strlen(datafile);
335 strncpy(datafile + ret + 1, fn_tmp, end - ret - 1);
336 ret = strlen(datafile);
345 static int scrub_open_file(const char *datafile, int m)
350 fd = open(datafile, m, 0600);
354 ret = flock(fd, LOCK_EX|LOCK_NB);
364 static int scrub_open_file_r(const char *fn_base, const char *fn_local)
367 char datafile[BTRFS_PATH_NAME_MAX + 1];
368 ret = scrub_datafile(fn_base, fn_local, NULL,
369 datafile, sizeof(datafile));
372 return scrub_open_file(datafile, O_RDONLY);
375 static int scrub_open_file_w(const char *fn_base, const char *fn_local,
379 char datafile[BTRFS_PATH_NAME_MAX + 1];
380 ret = scrub_datafile(fn_base, fn_local, tmp,
381 datafile, sizeof(datafile));
384 return scrub_open_file(datafile, O_WRONLY|O_CREAT);
387 static int scrub_rename_file(const char *fn_base, const char *fn_local,
391 char datafile_old[BTRFS_PATH_NAME_MAX + 1];
392 char datafile_new[BTRFS_PATH_NAME_MAX + 1];
393 ret = scrub_datafile(fn_base, fn_local, tmp,
394 datafile_old, sizeof(datafile_old));
397 ret = scrub_datafile(fn_base, fn_local, NULL,
398 datafile_new, sizeof(datafile_new));
401 ret = rename(datafile_old, datafile_new);
402 return ret ? -errno : 0;
405 #define _SCRUB_KVREAD(ret, i, name, avail, l, dest) if (ret == 0) { \
406 ret = scrub_kvread(i, sizeof(#name), avail, l, #name, dest.name); \
410 * returns 0 if the key did not match (nothing was read)
411 * 1 if the key did match (success)
412 * -1 if the key did match and an error occured
414 static int scrub_kvread(int *i, int len, int avail, const char *buf,
415 const char *key, u64 *dest)
419 if (*i + len + 1 < avail && strncmp(&buf[*i], key, len - 1) == 0) {
424 for (j = 0; isdigit(buf[*i + j]) && *i + j < avail; ++j)
428 *dest = atoll(&buf[*i]);
436 #define _SCRUB_INVALID do { \
438 fprintf(stderr, "WARNING: invalid data in line %d pos " \
439 "%d state %d (near \"%.*s\") at %s:%d\n", \
440 lineno, i, state, 20 > avail ? avail : 20, \
441 l + i, __FILE__, __LINE__); \
445 static struct scrub_file_record **scrub_read_file(int fd, int report_errors)
458 char empty_uuid[BTRFS_FSID_SIZE] = {0};
459 struct scrub_file_record **p = NULL;
462 return ERR_PTR(-EINVAL);
465 old_avail = avail - i;
466 BUG_ON(old_avail < 0);
468 memmove(l, l + i, old_avail);
469 avail = read(fd, l + old_avail, sizeof(l) - old_avail);
472 if (avail == 0 && old_avail == 0) {
474 memcmp(p[curr]->fsid, empty_uuid, BTRFS_FSID_SIZE) == 0) {
476 } else if (curr == -1) {
477 p = ERR_PTR(-ENODATA);
482 return ERR_PTR(-errno);
488 case 0: /* start of file */
489 ret = scrub_kvread(&i,
490 sizeof(SCRUB_FILE_VERSION_PREFIX), avail, l,
491 SCRUB_FILE_VERSION_PREFIX, &version);
494 if (version != atoll(SCRUB_FILE_VERSION))
495 return ERR_PTR(-ENOTSUP);
498 case 1: /* start of line, alloc */
500 * this state makes sure we have a complete line in
501 * further processing, so we don't need wrap-tracking
504 if (!eof && !memchr(l + i, '\n', avail - i))
507 if (curr > -1 && memcmp(p[curr]->fsid, empty_uuid,
508 BTRFS_FSID_SIZE) == 0) {
513 p = realloc(p, (curr + 2) * sizeof(*p));
515 p[curr] = malloc(sizeof(**p));
517 return ERR_PTR(-errno);
518 memset(p[curr], 0, sizeof(**p));
522 case 2: /* start of line, skip space */
523 while (isspace(l[i]) && i < avail) {
529 (!eof && !memchr(l + i, '\n', avail - i)))
533 case 3: /* read fsid */
536 for (j = 0; l[i + j] != ':' && i + j < avail; ++j)
538 if (i + j + 1 >= avail)
543 ret = uuid_parse(l + i, p[curr]->fsid);
549 case 4: /* read dev id */
550 for (j = 0; isdigit(l[i + j]) && i+j < avail; ++j)
552 if (j == 0 || i + j + 1 >= avail)
554 p[curr]->devid = atoll(&l[i]);
558 case 5: /* read key/value pair */
560 _SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
562 _SCRUB_KVREAD(ret, &i, data_extents_scrubbed, avail, l,
564 _SCRUB_KVREAD(ret, &i, tree_extents_scrubbed, avail, l,
566 _SCRUB_KVREAD(ret, &i, data_bytes_scrubbed, avail, l,
568 _SCRUB_KVREAD(ret, &i, tree_bytes_scrubbed, avail, l,
570 _SCRUB_KVREAD(ret, &i, read_errors, avail, l,
572 _SCRUB_KVREAD(ret, &i, csum_errors, avail, l,
574 _SCRUB_KVREAD(ret, &i, verify_errors, avail, l,
576 _SCRUB_KVREAD(ret, &i, no_csum, avail, l,
578 _SCRUB_KVREAD(ret, &i, csum_discards, avail, l,
580 _SCRUB_KVREAD(ret, &i, super_errors, avail, l,
582 _SCRUB_KVREAD(ret, &i, malloc_errors, avail, l,
584 _SCRUB_KVREAD(ret, &i, uncorrectable_errors, avail, l,
586 _SCRUB_KVREAD(ret, &i, corrected_errors, avail, l,
588 _SCRUB_KVREAD(ret, &i, last_physical, avail, l,
590 _SCRUB_KVREAD(ret, &i, finished, avail, l,
592 _SCRUB_KVREAD(ret, &i, t_start, avail, l,
593 (u64 *)&p[curr]->stats);
594 _SCRUB_KVREAD(ret, &i, t_resumed, avail, l,
595 (u64 *)&p[curr]->stats);
596 _SCRUB_KVREAD(ret, &i, duration, avail, l,
597 (u64 *)&p[curr]->stats);
598 _SCRUB_KVREAD(ret, &i, canceled, avail, l,
604 case 6: /* after number */
607 else if (l[i] == '\n')
613 case 99: /* skip rest of line */
618 if (l[i - 1] == '\n') {
630 static int scrub_write_buf(int fd, const void *data, int len)
633 ret = write(fd, data, len);
637 static int scrub_writev(int fd, char *buf, int max, const char *fmt, ...)
638 __attribute__ ((format (printf, 4, 5)));
639 static int scrub_writev(int fd, char *buf, int max, const char *fmt, ...)
645 ret = vsnprintf(buf, max, fmt, args);
649 return scrub_write_buf(fd, buf, ret);
652 #define _SCRUB_SUM(dest, data, name) dest->scrub_args.progress.name = \
653 data->resumed->p.name + data->scrub_args.progress.name
655 static struct scrub_progress *scrub_resumed_stats(struct scrub_progress *data,
656 struct scrub_progress *dest)
658 if (!data->resumed || data->skip)
661 _SCRUB_SUM(dest, data, data_extents_scrubbed);
662 _SCRUB_SUM(dest, data, tree_extents_scrubbed);
663 _SCRUB_SUM(dest, data, data_bytes_scrubbed);
664 _SCRUB_SUM(dest, data, tree_bytes_scrubbed);
665 _SCRUB_SUM(dest, data, read_errors);
666 _SCRUB_SUM(dest, data, csum_errors);
667 _SCRUB_SUM(dest, data, verify_errors);
668 _SCRUB_SUM(dest, data, no_csum);
669 _SCRUB_SUM(dest, data, csum_discards);
670 _SCRUB_SUM(dest, data, super_errors);
671 _SCRUB_SUM(dest, data, malloc_errors);
672 _SCRUB_SUM(dest, data, uncorrectable_errors);
673 _SCRUB_SUM(dest, data, corrected_errors);
674 _SCRUB_SUM(dest, data, last_physical);
675 dest->stats.canceled = data->stats.canceled;
676 dest->stats.finished = data->stats.finished;
677 dest->stats.t_resumed = data->stats.t_start;
678 dest->stats.t_start = data->resumed->stats.t_start;
679 dest->stats.duration = data->resumed->stats.duration +
680 data->stats.duration;
681 dest->scrub_args.devid = data->scrub_args.devid;
685 #define _SCRUB_KVWRITE(fd, buf, name, use) \
686 scrub_kvwrite(fd, buf, sizeof(buf), #name, \
687 use->scrub_args.progress.name)
689 #define _SCRUB_KVWRITE_STATS(fd, buf, name, use) \
690 scrub_kvwrite(fd, buf, sizeof(buf), #name, \
693 static int scrub_kvwrite(int fd, char *buf, int max, const char *key, u64 val)
695 return scrub_writev(fd, buf, max, "|%s:%lld", key, val);
698 static int scrub_write_file(int fd, const char *fsid,
699 struct scrub_progress *data, int n)
704 struct scrub_progress local;
705 struct scrub_progress *use;
710 /* each -1 is to subtract one \0 byte, the + 2 is for ':' and '\n' */
711 ret = scrub_write_buf(fd, SCRUB_FILE_VERSION_PREFIX ":"
712 SCRUB_FILE_VERSION "\n",
713 (sizeof(SCRUB_FILE_VERSION_PREFIX) - 1) +
714 (sizeof(SCRUB_FILE_VERSION) - 1) + 2);
718 for (i = 0; i < n; ++i) {
719 use = scrub_resumed_stats(&data[i], &local);
720 if (scrub_write_buf(fd, fsid, strlen(fsid)) ||
721 scrub_write_buf(fd, ":", 1) ||
722 scrub_writev(fd, buf, sizeof(buf), "%lld",
723 use->scrub_args.devid) ||
724 scrub_write_buf(fd, buf, ret) ||
725 _SCRUB_KVWRITE(fd, buf, data_extents_scrubbed, use) ||
726 _SCRUB_KVWRITE(fd, buf, tree_extents_scrubbed, use) ||
727 _SCRUB_KVWRITE(fd, buf, data_bytes_scrubbed, use) ||
728 _SCRUB_KVWRITE(fd, buf, tree_bytes_scrubbed, use) ||
729 _SCRUB_KVWRITE(fd, buf, read_errors, use) ||
730 _SCRUB_KVWRITE(fd, buf, csum_errors, use) ||
731 _SCRUB_KVWRITE(fd, buf, verify_errors, use) ||
732 _SCRUB_KVWRITE(fd, buf, no_csum, use) ||
733 _SCRUB_KVWRITE(fd, buf, csum_discards, use) ||
734 _SCRUB_KVWRITE(fd, buf, super_errors, use) ||
735 _SCRUB_KVWRITE(fd, buf, malloc_errors, use) ||
736 _SCRUB_KVWRITE(fd, buf, uncorrectable_errors, use) ||
737 _SCRUB_KVWRITE(fd, buf, corrected_errors, use) ||
738 _SCRUB_KVWRITE(fd, buf, last_physical, use) ||
739 _SCRUB_KVWRITE_STATS(fd, buf, t_start, use) ||
740 _SCRUB_KVWRITE_STATS(fd, buf, t_resumed, use) ||
741 _SCRUB_KVWRITE_STATS(fd, buf, duration, use) ||
742 _SCRUB_KVWRITE_STATS(fd, buf, canceled, use) ||
743 _SCRUB_KVWRITE_STATS(fd, buf, finished, use) ||
744 scrub_write_buf(fd, "\n", 1)) {
752 static int scrub_write_progress(pthread_mutex_t *m, const char *fsid,
753 struct scrub_progress *data, int n)
760 ret = pthread_mutex_lock(m);
766 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
772 fd = scrub_open_file_w(SCRUB_DATA_FILE, fsid, "tmp");
777 err = scrub_write_file(fd, fsid, data, n);
780 err = scrub_rename_file(SCRUB_DATA_FILE, fsid, "tmp");
791 ret = pthread_mutex_unlock(m);
795 ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
802 static void *scrub_one_dev(void *ctx)
804 struct scrub_progress *sp = ctx;
808 sp->stats.canceled = 0;
809 sp->stats.duration = 0;
810 sp->stats.finished = 0;
812 ret = ioctl(sp->fd, BTRFS_IOC_SCRUB, &sp->scrub_args);
813 gettimeofday(&tv, NULL);
815 sp->stats.duration = tv.tv_sec - sp->stats.t_start;
816 sp->stats.canceled = !!ret;
817 sp->ioctl_errno = errno;
818 ret = pthread_mutex_lock(&sp->progress_mutex);
820 return ERR_PTR(-ret);
821 sp->stats.finished = 1;
822 ret = pthread_mutex_unlock(&sp->progress_mutex);
824 return ERR_PTR(-ret);
829 static void *progress_one_dev(void *ctx)
831 struct scrub_progress *sp = ctx;
833 sp->ret = ioctl(sp->fd, BTRFS_IOC_SCRUB_PROGRESS, &sp->scrub_args);
834 sp->ioctl_errno = errno;
839 static void *scrub_progress_cycle(void *ctx)
845 struct scrub_progress *sp;
846 struct scrub_progress *sp_last;
847 struct scrub_progress *sp_shared;
849 struct scrub_progress_cycle *spc = ctx;
850 int ndev = spc->fi->num_devices;
854 struct pollfd accept_poll_fd = {
859 struct pollfd write_poll_fd = {
863 struct sockaddr_un peer;
864 socklen_t peer_size = sizeof(peer);
866 ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
868 return ERR_PTR(-ret);
870 uuid_unparse(spc->fi->fsid, fsid);
872 for (i = 0; i < ndev; ++i) {
873 sp = &spc->progress[i];
874 sp_last = &spc->progress[i + ndev];
875 sp_shared = &spc->shared_progress[i];
876 sp->scrub_args.devid = sp_last->scrub_args.devid =
877 sp_shared->scrub_args.devid;
878 sp->fd = sp_last->fd = spc->fdmnt;
879 sp->stats.t_start = sp_last->stats.t_start =
880 sp_shared->stats.t_start;
881 sp->resumed = sp_last->resumed = sp_shared->resumed;
882 sp->skip = sp_last->skip = sp_shared->skip;
883 sp->stats.finished = sp_last->stats.finished =
884 sp_shared->stats.finished;
888 ret = poll(&accept_poll_fd, 1, 5 * 1000);
890 return ERR_PTR(-errno);
892 peer_fd = accept(spc->prg_fd, (struct sockaddr *)&peer,
894 gettimeofday(&tv, NULL);
897 for (i = 0; i < ndev; ++i) {
898 sp = &spc->progress[this * ndev + i];
899 sp_last = &spc->progress[last * ndev + i];
900 sp_shared = &spc->shared_progress[i];
901 if (sp->stats.finished)
903 progress_one_dev(sp);
904 sp->stats.duration = tv.tv_sec - sp->stats.t_start;
907 if (sp->ioctl_errno != ENOTCONN &&
908 sp->ioctl_errno != ENODEV)
909 return ERR_PTR(-sp->ioctl_errno);
911 * scrub finished or device removed, check the
912 * finished flag. if unset, just use the last
913 * result we got for the current write and go
914 * on. flag should be set on next cycle, then.
916 ret = pthread_mutex_lock(&sp_shared->progress_mutex);
918 return ERR_PTR(-ret);
919 if (!sp_shared->stats.finished) {
920 ret = pthread_mutex_unlock(
921 &sp_shared->progress_mutex);
923 return ERR_PTR(-ret);
924 memcpy(sp, sp_last, sizeof(*sp));
927 ret = pthread_mutex_unlock(&sp_shared->progress_mutex);
929 return ERR_PTR(-ret);
930 memcpy(sp, sp_shared, sizeof(*sp));
931 memcpy(sp_last, sp_shared, sizeof(*sp));
934 write_poll_fd.fd = peer_fd;
935 ret = poll(&write_poll_fd, 1, 0);
937 return ERR_PTR(-errno);
939 ret = scrub_write_file(
941 &spc->progress[this * ndev], ndev);
950 ret = scrub_write_progress(spc->write_mutex, fsid,
951 &spc->progress[this * ndev], ndev);
957 static struct scrub_file_record *last_dev_scrub(
958 struct scrub_file_record *const *const past_scrubs, u64 devid)
962 if (!past_scrubs || IS_ERR(past_scrubs))
965 for (i = 0; past_scrubs[i]; ++i)
966 if (past_scrubs[i]->devid == devid)
967 return past_scrubs[i];
972 int mkdir_p(char *path)
977 for (i = 1; i < strlen(path); ++i) {
981 ret = mkdir(path, 0777);
982 if (ret && errno != EEXIST)
990 static const char * const cmd_scrub_start_usage[];
991 static const char * const cmd_scrub_resume_usage[];
993 static int scrub_start(int argc, char **argv, int resume)
1003 int e_uncorrectable = 0;
1004 int e_correctable = 0;
1007 int do_background = 1;
1013 int do_stats_per_dev = 0;
1017 struct btrfs_ioctl_fs_info_args fi_args;
1018 struct btrfs_ioctl_dev_info_args *di_args = NULL;
1019 struct scrub_progress *sp = NULL;
1020 struct scrub_fs_stat fs_stat;
1022 struct sockaddr_un addr = {
1023 .sun_family = AF_UNIX,
1025 pthread_t *t_devs = NULL;
1027 pthread_attr_t t_attr;
1028 struct scrub_file_record **past_scrubs = NULL;
1029 struct scrub_file_record *last_scrub = NULL;
1030 char *datafile = strdup(SCRUB_DATA_FILE);
1032 char sock_path[BTRFS_PATH_NAME_MAX + 1] = "";
1033 struct scrub_progress_cycle spc;
1034 pthread_mutex_t spc_write_mutex = PTHREAD_MUTEX_INITIALIZER;
1039 while ((c = getopt(argc, argv, "BdqrR")) != -1) {
1047 do_stats_per_dev = 1;
1060 usage(resume ? cmd_scrub_resume_usage :
1061 cmd_scrub_start_usage);
1065 /* try to catch most error cases before forking */
1067 if (check_argc_exact(argc - optind, 1)) {
1068 usage(resume ? cmd_scrub_resume_usage :
1069 cmd_scrub_start_usage);
1072 spc.progress = NULL;
1073 if (do_quiet && do_print)
1076 if (mkdir_p(datafile)) {
1077 ERR(!do_quiet, "WARNING: cannot create scrub data "
1078 "file, mkdir %s failed: %s. Status recording "
1079 "disabled\n", datafile, strerror(errno));
1084 path = argv[optind];
1086 fdmnt = open_file_or_dir(path);
1088 ERR(!do_quiet, "ERROR: can't access '%s'\n", path);
1092 ret = get_fs_info(fdmnt, path, &fi_args, &di_args);
1094 ERR(!do_quiet, "ERROR: getting dev info for scrub failed: "
1095 "%s\n", strerror(-ret));
1099 if (!fi_args.num_devices) {
1100 ERR(!do_quiet, "ERROR: no devices found\n");
1105 uuid_unparse(fi_args.fsid, fsid);
1106 fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid);
1107 if (fdres < 0 && fdres != -ENOENT) {
1108 ERR(!do_quiet, "WARNING: failed to open status file: "
1109 "%s\n", strerror(-fdres));
1110 } else if (fdres >= 0) {
1111 past_scrubs = scrub_read_file(fdres, !do_quiet);
1112 if (IS_ERR(past_scrubs))
1113 ERR(!do_quiet, "WARNING: failed to read status file: "
1114 "%s\n", strerror(-PTR_ERR(past_scrubs)));
1118 t_devs = malloc(fi_args.num_devices * sizeof(*t_devs));
1119 sp = calloc(fi_args.num_devices, sizeof(*sp));
1120 spc.progress = calloc(fi_args.num_devices * 2, sizeof(*spc.progress));
1122 if (!t_devs || !sp || !spc.progress) {
1123 ERR(!do_quiet, "ERROR: scrub failed: %s", strerror(errno));
1128 ret = pthread_attr_init(&t_attr);
1130 ERR(!do_quiet, "ERROR: pthread_attr_init failed: %s\n",
1136 for (i = 0; i < fi_args.num_devices; ++i) {
1137 devid = di_args[i].devid;
1138 ret = pthread_mutex_init(&sp[i].progress_mutex, NULL);
1140 ERR(!do_quiet, "ERROR: pthread_mutex_init failed: "
1141 "%s\n", strerror(ret));
1145 last_scrub = last_dev_scrub(past_scrubs, devid);
1146 sp[i].scrub_args.devid = devid;
1148 if (resume && last_scrub && (last_scrub->stats.canceled ||
1149 !last_scrub->stats.finished)) {
1151 sp[i].scrub_args.start = last_scrub->p.last_physical;
1152 sp[i].resumed = last_scrub;
1153 } else if (resume) {
1156 sp[i].resumed = last_scrub;
1160 sp[i].scrub_args.start = 0ll;
1161 sp[i].resumed = NULL;
1164 sp[i].scrub_args.end = (u64)-1ll;
1165 sp[i].scrub_args.flags = readonly ? BTRFS_SCRUB_READONLY : 0;
1168 if (!n_start && !n_resume) {
1170 printf("scrub: nothing to resume for %s, fsid %s\n",
1176 ret = prg_fd = socket(AF_UNIX, SOCK_STREAM, 0);
1178 ret = scrub_datafile(SCRUB_PROGRESS_SOCKET_PATH, fsid, NULL,
1179 sock_path, sizeof(sock_path));
1180 /* ignore EOVERFLOW, try using a shorter path for the socket */
1181 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
1182 strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);
1183 ret = bind(prg_fd, (struct sockaddr *)&addr, sizeof(addr));
1184 if (ret != -1 || errno != EADDRINUSE)
1187 * bind failed with EADDRINUSE. so let's see if anyone answers
1188 * when we make a call to the socket ...
1190 ret = connect(prg_fd, (struct sockaddr *)&addr, sizeof(addr));
1191 if (!ret || errno != ECONNREFUSED) {
1192 /* ... yes, so scrub must be running. error out */
1193 fprintf(stderr, "ERROR: scrub already running\n");
1199 * ... no, this means someone left us alone with an unused
1200 * socket in the file system. remove it and try again.
1202 ret = unlink(sock_path);
1205 ret = listen(prg_fd, 100);
1207 ERR(!do_quiet, "WARNING: failed to open the progress status "
1208 "socket at %s: %s. Progress cannot be queried\n",
1209 sock_path[0] ? sock_path : SCRUB_PROGRESS_SOCKET_PATH,
1220 /* write all-zero progress file for a start */
1221 ret = scrub_write_progress(&spc_write_mutex, fsid, sp,
1222 fi_args.num_devices);
1224 ERR(!do_quiet, "WARNING: failed to write the progress "
1225 "status file: %s. Status recording disabled\n",
1231 if (do_background) {
1234 ERR(!do_quiet, "ERROR: cannot scrub, fork failed: "
1235 "%s\n", strerror(errno));
1242 scrub_handle_sigint_parent();
1244 printf("scrub %s on %s, fsid %s (pid=%d)\n",
1245 n_start ? "started" : "resumed",
1253 ERR(!do_quiet, "ERROR: wait failed: (ret=%d) "
1254 "%s\n", ret, strerror(errno));
1258 if (!WIFEXITED(stat) || WEXITSTATUS(stat)) {
1259 ERR(!do_quiet, "ERROR: scrub process failed\n");
1260 err = WIFEXITED(stat) ? WEXITSTATUS(stat) : -1;
1268 scrub_handle_sigint_child(fdmnt);
1270 for (i = 0; i < fi_args.num_devices; ++i) {
1272 sp[i].scrub_args.progress = sp[i].resumed->p;
1273 sp[i].stats = sp[i].resumed->stats;
1275 sp[i].stats.finished = 1;
1278 devid = di_args[i].devid;
1279 gettimeofday(&tv, NULL);
1280 sp[i].stats.t_start = tv.tv_sec;
1281 ret = pthread_create(&t_devs[i], &t_attr,
1282 scrub_one_dev, &sp[i]);
1285 fprintf(stderr, "ERROR: creating "
1286 "scrub_one_dev[%llu] thread failed: "
1287 "%s\n", devid, strerror(ret));
1294 spc.prg_fd = prg_fd;
1295 spc.do_record = do_record;
1296 spc.write_mutex = &spc_write_mutex;
1297 spc.shared_progress = sp;
1299 ret = pthread_create(&t_prog, &t_attr, scrub_progress_cycle, &spc);
1302 fprintf(stderr, "ERROR: creating progress thread "
1303 "failed: %s\n", strerror(ret));
1309 for (i = 0; i < fi_args.num_devices; ++i) {
1312 devid = di_args[i].devid;
1313 ret = pthread_join(t_devs[i], NULL);
1316 fprintf(stderr, "ERROR: pthread_join failed "
1317 "for scrub_one_dev[%llu]: %s\n", devid,
1322 if (sp[i].ret && sp[i].ioctl_errno == ENODEV) {
1324 fprintf(stderr, "WARNING: device %lld not "
1325 "present\n", devid);
1328 if (sp[i].ret && sp[i].ioctl_errno == ECANCELED) {
1330 } else if (sp[i].ret) {
1332 fprintf(stderr, "ERROR: scrubbing %s failed "
1333 "for device id %lld (%s)\n", path,
1334 devid, strerror(sp[i].ioctl_errno));
1338 if (sp[i].scrub_args.progress.uncorrectable_errors > 0)
1340 if (sp[i].scrub_args.progress.corrected_errors > 0
1341 || sp[i].scrub_args.progress.unverified_errors > 0)
1346 const char *append = "done";
1347 if (!do_stats_per_dev)
1348 init_fs_stat(&fs_stat);
1349 for (i = 0; i < fi_args.num_devices; ++i) {
1350 if (do_stats_per_dev) {
1351 print_scrub_dev(&di_args[i],
1352 &sp[i].scrub_args.progress,
1354 sp[i].ret ? "canceled" : "done",
1358 append = "canceled";
1359 add_to_fs_stat(&sp[i].scrub_args.progress,
1360 &sp[i].stats, &fs_stat);
1363 if (!do_stats_per_dev) {
1364 printf("scrub %s for %s\n", append, fsid);
1365 print_fs_stat(&fs_stat, print_raw);
1369 ret = pthread_cancel(t_prog);
1371 ret = pthread_join(t_prog, &terr);
1372 if (do_print && ret) {
1373 fprintf(stderr, "ERROR: progress thead handling failed: %s\n",
1377 if (do_print && terr && terr != PTHREAD_CANCELED) {
1378 fprintf(stderr, "ERROR: recording progress "
1379 "failed: %s\n", strerror(-PTR_ERR(terr)));
1383 ret = scrub_write_progress(&spc_write_mutex, fsid, sp,
1384 fi_args.num_devices);
1385 if (ret && do_print) {
1386 fprintf(stderr, "ERROR: failed to record the result: "
1387 "%s\n", strerror(-ret));
1391 scrub_handle_sigint_child(-1);
1394 free_history(past_scrubs);
1410 if (e_uncorrectable)
1415 static const char * const cmd_scrub_start_usage[] = {
1416 "btrfs scrub start [-Bdqr] <path>|<device>",
1417 "Start a new scrub",
1419 "-B do not background",
1420 "-d stats per device (-B only)",
1422 "-r read only mode",
1426 static int cmd_scrub_start(int argc, char **argv)
1428 return scrub_start(argc, argv, 0);
1431 static const char * const cmd_scrub_cancel_usage[] = {
1432 "btrfs scrub cancel <path>|<device>",
1433 "Cancel a running scrub",
1437 static int cmd_scrub_cancel(int argc, char **argv)
1443 char mp[BTRFS_PATH_NAME_MAX + 1];
1444 struct btrfs_fs_devices *fs_devices_mnt = NULL;
1446 if (check_argc_exact(argc, 2))
1447 usage(cmd_scrub_cancel_usage);
1451 fdmnt = open_file_or_dir(path);
1453 fprintf(stderr, "ERROR: scrub cancel failed\n");
1458 ret = ioctl(fdmnt, BTRFS_IOC_SCRUB_CANCEL, NULL);
1461 if (ret && err == EINVAL) {
1462 /* path is not a btrfs mount point. See if it's a device. */
1463 ret = check_mounted_where(fdmnt, path, mp, sizeof(mp),
1466 /* It is a device; open the mountpoint. */
1468 fdmnt = open_file_or_dir(mp);
1479 fprintf(stderr, "ERROR: scrub cancel failed on %s: %s\n", path,
1480 err == ENOTCONN ? "not running" : strerror(errno));
1484 printf("scrub cancelled\n");
1489 static const char * const cmd_scrub_resume_usage[] = {
1490 "btrfs scrub resume [-Bdqr] <path>|<device>",
1491 "Resume previously canceled or interrupted scrub",
1493 "-B do not background",
1494 "-d stats per device (-B only)",
1496 "-r read only mode",
1500 static int cmd_scrub_resume(int argc, char **argv)
1502 return scrub_start(argc, argv, 1);
1505 static const char * const cmd_scrub_status_usage[] = {
1506 "btrfs scrub status [-dR] <path>|<device>",
1507 "Show status of running or finished scrub",
1509 "-d stats per device",
1510 "-R print raw stats",
1514 static int cmd_scrub_status(int argc, char **argv)
1517 struct btrfs_ioctl_fs_info_args fi_args;
1518 struct btrfs_ioctl_dev_info_args *di_args = NULL;
1519 struct scrub_file_record **past_scrubs = NULL;
1520 struct scrub_file_record *last_scrub;
1521 struct scrub_fs_stat fs_stat;
1522 struct sockaddr_un addr = {
1523 .sun_family = AF_UNIX,
1529 int do_stats_per_dev = 0;
1536 while ((c = getopt(argc, argv, "dR")) != -1) {
1539 do_stats_per_dev = 1;
1546 usage(cmd_scrub_status_usage);
1550 if (check_argc_exact(argc - optind, 1))
1551 usage(cmd_scrub_status_usage);
1553 path = argv[optind];
1555 fdmnt = open_file_or_dir(path);
1557 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
1561 ret = get_fs_info(fdmnt, path, &fi_args, &di_args);
1563 fprintf(stderr, "ERROR: getting dev info for scrub failed: "
1564 "%s\n", strerror(-ret));
1568 if (!fi_args.num_devices) {
1569 fprintf(stderr, "ERROR: no devices found\n");
1574 uuid_unparse(fi_args.fsid, fsid);
1576 fdres = socket(AF_UNIX, SOCK_STREAM, 0);
1578 fprintf(stderr, "ERROR: failed to create socket to "
1579 "receive progress information: %s\n",
1584 scrub_datafile(SCRUB_PROGRESS_SOCKET_PATH, fsid,
1585 NULL, addr.sun_path, sizeof(addr.sun_path));
1586 /* ignore EOVERFLOW, just use shorter name and hope for the best */
1587 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
1588 ret = connect(fdres, (struct sockaddr *)&addr, sizeof(addr));
1591 fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid);
1592 if (fdres < 0 && fdres != -ENOENT) {
1593 fprintf(stderr, "WARNING: failed to open status file: "
1594 "%s\n", strerror(-fdres));
1601 past_scrubs = scrub_read_file(fdres, 1);
1602 if (IS_ERR(past_scrubs))
1603 fprintf(stderr, "WARNING: failed to read status: %s\n",
1604 strerror(-PTR_ERR(past_scrubs)));
1607 printf("scrub status for %s\n", fsid);
1609 if (do_stats_per_dev) {
1610 for (i = 0; i < fi_args.num_devices; ++i) {
1611 last_scrub = last_dev_scrub(past_scrubs,
1614 print_scrub_dev(&di_args[i], NULL, print_raw,
1618 print_scrub_dev(&di_args[i], &last_scrub->p, print_raw,
1619 last_scrub->stats.finished ?
1620 "history" : "status",
1621 &last_scrub->stats);
1624 init_fs_stat(&fs_stat);
1625 for (i = 0; i < fi_args.num_devices; ++i) {
1626 last_scrub = last_dev_scrub(past_scrubs,
1630 add_to_fs_stat(&last_scrub->p, &last_scrub->stats,
1633 print_fs_stat(&fs_stat, print_raw);
1637 free_history(past_scrubs);
1645 const struct cmd_group scrub_cmd_group = {
1646 scrub_cmd_group_usage, NULL, {
1647 { "start", cmd_scrub_start, cmd_scrub_start_usage, NULL, 0 },
1648 { "cancel", cmd_scrub_cancel, cmd_scrub_cancel_usage, NULL, 0 },
1649 { "resume", cmd_scrub_resume, cmd_scrub_resume_usage, NULL, 0 },
1650 { "status", cmd_scrub_status, cmd_scrub_status_usage, NULL, 0 },
1655 int cmd_scrub(int argc, char **argv)
1657 return handle_command_group(&scrub_cmd_group, argc, argv);