]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame_incremental - scrub/common.c
xfs_scrub: fix weirdness in directory name check code
[thirdparty/xfsprogs-dev.git] / scrub / common.c
... / ...
CommitLineData
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6#include "xfs.h"
7#include <pthread.h>
8#include <sys/statvfs.h>
9#include <syslog.h>
10#include "platform_defs.h"
11#include "libfrog/paths.h"
12#include "xfs_scrub.h"
13#include "common.h"
14#include "progress.h"
15
16extern char *progname;
17
18/*
19 * Reporting Status to the Console
20 *
21 * We aim for a roughly standard reporting format -- the severity of the
22 * status being reported, a textual description of the object being
23 * reported, and whatever the status happens to be.
24 *
25 * Errors are the most severe and reflect filesystem corruption.
26 * Warnings indicate that something is amiss and needs the attention of
27 * the administrator, but does not constitute a corruption. Information
28 * is merely advisory.
29 */
30
31/* Too many errors? Bail out. */
32bool
33scrub_excessive_errors(
34 struct scrub_ctx *ctx)
35{
36 unsigned long long errors_seen;
37
38 /*
39 * We only set max_errors at the start of the program, so it's safe to
40 * access it locklessly.
41 */
42 if (ctx->max_errors == 0)
43 return false;
44
45 pthread_mutex_lock(&ctx->lock);
46 errors_seen = ctx->corruptions_found + ctx->unfixable_errors;
47 pthread_mutex_unlock(&ctx->lock);
48
49 return errors_seen >= ctx->max_errors;
50}
51
52static struct {
53 const char *string;
54 int loglevel;
55} err_levels[] = {
56 [S_ERROR] = {
57 .string = "Error",
58 .loglevel = LOG_ERR,
59 },
60 [S_CORRUPT] = {
61 .string = "Corruption",
62 .loglevel = LOG_ERR,
63 },
64 [S_UNFIXABLE] = {
65 .string = "Unfixable Error",
66 .loglevel = LOG_ERR,
67 },
68 [S_WARN] = {
69 .string = "Warning",
70 .loglevel = LOG_WARNING,
71 },
72 [S_INFO] = {
73 .string = "Info",
74 .loglevel = LOG_INFO,
75 },
76 [S_REPAIR] = {
77 .string = "Repaired",
78 .loglevel = LOG_INFO,
79 },
80 [S_PREEN] = {
81 .string = "Optimized",
82 .loglevel = LOG_INFO,
83 },
84};
85
86/* If stream is a tty, clear to end of line to clean up progress bar. */
87static inline const char *stream_start(FILE *stream)
88{
89 if (stream == stderr)
90 return stderr_isatty ? CLEAR_EOL : "";
91 return stdout_isatty ? CLEAR_EOL : "";
92}
93
94/* Print a warning string and some warning text. */
95void
96__str_out(
97 struct scrub_ctx *ctx,
98 const char *descr,
99 enum error_level level,
100 int error,
101 const char *file,
102 int line,
103 const char *format,
104 ...)
105{
106 FILE *stream = stderr;
107 va_list args;
108 char buf[DESCR_BUFSZ];
109
110 /* print strerror or format of choice but not both */
111 assert(!(error && format));
112
113 if (level >= S_INFO)
114 stream = stdout;
115
116 pthread_mutex_lock(&ctx->lock);
117
118 /* We only want to hear about optimizing when in debug/verbose mode. */
119 if (level == S_PREEN && !debug && !verbose)
120 goto out_record;
121
122 fprintf(stream, "%s%s: %s: ", stream_start(stream),
123 _(err_levels[level].string), descr);
124 if (error) {
125 fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
126 } else {
127 va_start(args, format);
128 vfprintf(stream, format, args);
129 va_end(args);
130 }
131
132 if (debug)
133 fprintf(stream, _(" (%s line %d)"), file, line);
134 fprintf(stream, "\n");
135 if (stream == stdout)
136 fflush(stream);
137
138out_record:
139 if (error || level == S_ERROR) /* A syscall failed */
140 ctx->runtime_errors++;
141 else if (level == S_CORRUPT)
142 ctx->corruptions_found++;
143 else if (level == S_UNFIXABLE)
144 ctx->unfixable_errors++;
145 else if (level == S_WARN)
146 ctx->warnings_found++;
147 else if (level == S_REPAIR)
148 ctx->repairs++;
149 else if (level == S_PREEN)
150 ctx->preens++;
151
152 pthread_mutex_unlock(&ctx->lock);
153}
154
155/* Log a message to syslog. */
156#define LOG_BUFSZ 4096
157#define LOGNAME_BUFSZ 256
158void
159__str_log(
160 struct scrub_ctx *ctx,
161 enum error_level level,
162 const char *format,
163 ...)
164{
165 va_list args;
166 char logname[LOGNAME_BUFSZ];
167 char buf[LOG_BUFSZ];
168 int sz;
169
170 /* We only want to hear about optimizing when in debug/verbose mode. */
171 if (level == S_PREEN && !debug && !verbose)
172 return;
173
174 /*
175 * Skip logging if we're being run as a service (presumably the
176 * service will log stdout/stderr); if we're being run in a non
177 * interactive manner (assume we're a service); or if we're in
178 * debug mode.
179 */
180 if (is_service || !isatty(fileno(stdin)) || debug)
181 return;
182
183 snprintf(logname, LOGNAME_BUFSZ, "%s@%s", progname, ctx->mntpoint);
184 openlog(logname, LOG_PID, LOG_DAEMON);
185
186 sz = snprintf(buf, LOG_BUFSZ, "%s: ", _(err_levels[level].string));
187 va_start(args, format);
188 vsnprintf(buf + sz, LOG_BUFSZ - sz, format, args);
189 va_end(args);
190 syslog(err_levels[level].loglevel, "%s", buf);
191
192 closelog();
193}
194
195double
196timeval_subtract(
197 struct timeval *tv1,
198 struct timeval *tv2)
199{
200 return ((tv1->tv_sec - tv2->tv_sec) +
201 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
202}
203
204/* Produce human readable disk space output. */
205double
206auto_space_units(
207 unsigned long long bytes,
208 char **units)
209{
210 if (debug > 1)
211 goto no_prefix;
212 if (bytes > (1ULL << 40)) {
213 *units = "TiB";
214 return (double)bytes / (1ULL << 40);
215 } else if (bytes > (1ULL << 30)) {
216 *units = "GiB";
217 return (double)bytes / (1ULL << 30);
218 } else if (bytes > (1ULL << 20)) {
219 *units = "MiB";
220 return (double)bytes / (1ULL << 20);
221 } else if (bytes > (1ULL << 10)) {
222 *units = "KiB";
223 return (double)bytes / (1ULL << 10);
224 }
225
226no_prefix:
227 *units = "B";
228 return bytes;
229}
230
231/* Produce human readable discrete number output. */
232double
233auto_units(
234 unsigned long long number,
235 char **units,
236 int *precision)
237{
238 if (debug > 1)
239 goto no_prefix;
240 *precision = 1;
241 if (number > 1000000000000ULL) {
242 *units = "T";
243 return number / 1000000000000.0;
244 } else if (number > 1000000000ULL) {
245 *units = "G";
246 return number / 1000000000.0;
247 } else if (number > 1000000ULL) {
248 *units = "M";
249 return number / 1000000.0;
250 } else if (number > 1000ULL) {
251 *units = "K";
252 return number / 1000.0;
253 }
254
255no_prefix:
256 *units = "";
257 *precision = 0;
258 return number;
259}
260
261/* How many threads to kick off? */
262unsigned int
263scrub_nproc(
264 struct scrub_ctx *ctx)
265{
266 if (force_nr_threads)
267 return force_nr_threads;
268 return ctx->nr_io_threads;
269}
270
271/*
272 * How many threads to kick off for a workqueue? If we only want one
273 * thread, save ourselves the overhead and just run it in the main thread.
274 */
275unsigned int
276scrub_nproc_workqueue(
277 struct scrub_ctx *ctx)
278{
279 unsigned int x;
280
281 x = scrub_nproc(ctx);
282 if (x == 1)
283 x = 0;
284 return x;
285}
286
287/*
288 * Sleep for 100us * however many -b we got past the initial one.
289 * This is an (albeit clumsy) way to throttle scrub activity.
290 */
291void
292background_sleep(void)
293{
294 unsigned long long time_ns;
295 struct timespec tv;
296
297 if (bg_mode < 2)
298 return;
299
300 time_ns = 100 * NSEC_PER_USEC * (bg_mode - 1);
301 tv.tv_sec = time_ns / NSEC_PER_SEC;
302 tv.tv_nsec = time_ns % NSEC_PER_SEC;
303 nanosleep(&tv, NULL);
304}
305
306/*
307 * Return the input string with non-printing bytes escaped.
308 * Caller must free the buffer.
309 */
310char *
311string_escape(
312 const char *in)
313{
314 char *str;
315 const char *p;
316 char *q;
317 int x;
318
319 str = malloc(strlen(in) * 4);
320 if (!str)
321 return NULL;
322 for (p = in, q = str; *p != '\0'; p++) {
323 if (isprint(*p)) {
324 *q = *p;
325 q++;
326 } else {
327 x = sprintf(q, "\\x%02x", *p);
328 q += x;
329 }
330 }
331 *q = '\0';
332 return str;
333}
334
335/*
336 * Record another naming warning, and decide if it's worth
337 * complaining about.
338 */
339bool
340should_warn_about_name(
341 struct scrub_ctx *ctx)
342{
343 bool whine;
344 bool res;
345
346 pthread_mutex_lock(&ctx->lock);
347 ctx->naming_warnings++;
348 whine = ctx->naming_warnings == TOO_MANY_NAME_WARNINGS;
349 res = ctx->naming_warnings < TOO_MANY_NAME_WARNINGS;
350 pthread_mutex_unlock(&ctx->lock);
351
352 if (whine && !(debug || verbose))
353 str_info(ctx, ctx->mntpoint,
354_("More than %u naming warnings, shutting up."),
355 TOO_MANY_NAME_WARNINGS);
356
357 return debug || verbose || res;
358}
359
360/* Decide if a value is within +/- (n/d) of a desired value. */
361bool
362within_range(
363 struct scrub_ctx *ctx,
364 unsigned long long value,
365 unsigned long long desired,
366 unsigned long long abs_threshold,
367 unsigned int n,
368 unsigned int d,
369 const char *descr)
370{
371 assert(n < d);
372
373 /* Don't complain if difference does not exceed an absolute value. */
374 if (value < desired && desired - value < abs_threshold)
375 return true;
376 if (value > desired && value - desired < abs_threshold)
377 return true;
378
379 /* Complain if the difference exceeds a certain percentage. */
380 if (value < desired * (d - n) / d)
381 return false;
382 if (value > desired * (d + n) / d)
383 return false;
384
385 return true;
386}
387
388/*
389 * Render an inode number into a buffer in a format suitable for use in
390 * log messages. The buffer will be filled with:
391 * "inode <inode number> (<ag number>/<ag inode number>)"
392 * If the @format argument is non-NULL, it will be rendered into the buffer
393 * after the inode representation and a single space.
394 */
395int
396scrub_render_ino_descr(
397 const struct scrub_ctx *ctx,
398 char *buf,
399 size_t buflen,
400 uint64_t ino,
401 uint32_t gen,
402 const char *format,
403 ...)
404{
405 va_list args;
406 uint32_t agno;
407 uint32_t agino;
408 int ret;
409
410 agno = cvt_ino_to_agno(&ctx->mnt, ino);
411 agino = cvt_ino_to_agino(&ctx->mnt, ino);
412 ret = snprintf(buf, buflen, _("inode %"PRIu64" (%"PRIu32"/%"PRIu32")%s"),
413 ino, agno, agino, format ? " " : "");
414 if (ret < 0 || ret >= buflen || format == NULL)
415 return ret;
416
417 va_start(args, format);
418 ret += vsnprintf(buf + ret, buflen - ret, format, args);
419 va_end(args);
420 return ret;
421}