]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/descr.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include <sys/statvfs.h>
9 #include "platform_defs.h"
11 #include "libfrog/paths.h"
12 #include "libfrog/ptvar.h"
13 #include "xfs_scrub.h"
18 * Deferred String Description Renderer
19 * ====================================
20 * There are many places in xfs_scrub where some event occurred and we'd like
21 * to be able to print some sort of message describing what happened, and
22 * where. However, we don't know whether we're going to need the description
23 * of where ahead of time and there's little point in spending any time looking
24 * up gettext strings and formatting buffers until we actually need to.
26 * This code provides enough of a function closure that we are able to record
27 * some information about the program status but defer rendering the textual
28 * description until we know that we need it. Once we've rendered the string
29 * we can skip it for subsequent calls. We use per-thread storage for the
30 * message buffer to amortize the memory allocation across calls.
32 * On a clean filesystem this can reduce the xfs_scrub runtime by 7-10% by
33 * avoiding unnecessary work.
36 static struct ptvar
*descr_ptvar
;
38 /* Global buffer for when we aren't running in threaded mode. */
39 static char global_dsc_buf
[DESCR_BUFSZ
];
42 * Render a textual description string using the function and location stored
43 * in the description context.
55 dsc_buf
= ptvar_get(descr_ptvar
, &ret
);
57 return _("error finding description buffer");
59 dsc_buf
= global_dsc_buf
;
61 ret
= dsc
->fn(dsc
->ctx
, dsc_buf
, DESCR_BUFSZ
, dsc
->where
);
63 snprintf(dsc_buf
, DESCR_BUFSZ
,
64 _("error %d while rendering description at %s line %d\n"),
72 * Set a new location context for this deferred-rendering string.
73 * The caller is responsible for freeing the old context, if any.
83 /* Allocate all the description string buffers. */
86 struct scrub_ctx
*ctx
,
87 unsigned int nr_threads
)
91 assert(descr_ptvar
== NULL
);
92 ret
= -ptvar_alloc(nr_threads
, DESCR_BUFSZ
, &descr_ptvar
);
94 str_liberror(ctx
, ret
, _("creating description buffer"));
99 /* Free all the description string buffers. */
101 descr_end_phase(void)
104 ptvar_free(descr_ptvar
);