summaryrefslogtreecommitdiff
path: root/src/reclaim-tail-waste.c
blob: 8f8c4e698359223b294ecacdac1a6c31e4e0906a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 *  Copyright (C) 2020 - Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include <iou.h>
#include <thunk.h>

#include "bootid.h"
#include "humane.h"
#include "journals.h"
#include "machid.h"
#include "reclaim-tail-waste.h"

#include "upstream/journal-def.h"


typedef struct tail_waste_t {
	unsigned	n_journals, n_reclaimed, n_ignored, n_errored, n_mules;
	uint64_t	reclaimed_bytes, ignored_bytes, errored_bytes;
} tail_waste_t;


THUNK_DEFINE_STATIC(reclaim_tail_waste, journal_t **, journal, Header *, journal_header, ObjectHeader *, tail_object_header, tail_waste_t *, tail_waste)
{
	uint64_t	sz, tail;
	struct stat	st;
	int		r;
	humane_t	h1;

	assert(journal);
	assert(journal_header);
	assert(tail_object_header);
	assert(tail_waste);

	r = fstat((*journal)->fd, &st);
	if (r < 0)
		return r;

	tail_waste->n_journals++;

	sz = st.st_size;
	tail = journal_header->tail_object_offset + ALIGN64(tail_object_header->size);

	if (sz == tail) {
		tail_waste->n_mules++;

		return 0;
	}

	if (journal_header->state != STATE_ARCHIVED) {
		printf("Ignoring %s of tail-waste on \"%s\" for not being archived (state=%s)\n",
			humane_bytes(&h1, sz - tail),
			(*journal)->name,
			journal_state_str(journal_header->state));

		tail_waste->n_ignored++;
		tail_waste->ignored_bytes += sz - tail;

		return 0;
	}

	if (ftruncate((*journal)->fd, tail) < 0) {
		fprintf(stderr, "Unable to truncate \"%s\" to %"PRIu64", ignoring: %s\n",
			(*journal)->name,
			tail,
			strerror(errno));

		tail_waste->n_errored++;
		tail_waste->errored_bytes += sz - tail;

		return 0;
	}

	tail_waste->n_reclaimed++;
	tail_waste->reclaimed_bytes += sz - tail;

	return 0;
}


THUNK_DEFINE_STATIC(per_journal_tail_waste, iou_t *, iou, journal_t **, journal_iter, tail_waste_t *, tail_waste)
{
	struct {
		journal_t	*journal;
		Header		header;
		ObjectHeader	tail_object_header;
	} *foo;

	thunk_t	*closure;

	assert(iou);
	assert(journal_iter);

	closure = THUNK_ALLOC(reclaim_tail_waste, (void **)&foo, sizeof(*foo));
	foo->journal = *journal_iter;

	return journal_get_header(iou, &foo->journal, &foo->header, THUNK(
			journal_get_object_header(iou, &foo->journal, &foo->header.tail_object_offset, &foo->tail_object_header, THUNK_INIT(
				reclaim_tail_waste(closure, &foo->journal, &foo->header, &foo->tail_object_header, tail_waste)))));
}


/* print the size of wasted space between each journal's tail object and EOF, and a sum total. */
int jio_reclaim_tail_waste(iou_t *iou, int argc, char *argv[])
{
	char		*machid;
	journals_t	*journals;
	journal_t	*journal_iter;
	tail_waste_t	tail_waste = {};
	int		r;
	humane_t	h1;

	printf("\nTemporarily disabled: https://github.com/systemd/systemd/pull/17876\n");
	return -ENOTSUP;

	r = machid_get(iou, &machid, THUNK(
		journals_open(iou, &machid, O_RDWR, &journals, THUNK(
			journals_for_each(&journals, &journal_iter, THUNK(
				per_journal_tail_waste(iou, &journal_iter, &tail_waste)))))));
	if (r < 0)
		return r;

	printf("\nReclaiming tail-waste...\n");
	r = iou_run(iou);
	if (r < 0)
		return r;

	printf("\nSummary:\n");
	if (!tail_waste.n_journals)
		printf("\tNo journal files opened!\n");

	if (tail_waste.n_mules)
		printf("\tSkipped %u journal files free of tail-waste\n",
			tail_waste.n_mules);

	if (tail_waste.n_ignored)
		printf("\tIgnored %u unarchived journal files totalling %s of tail-waste\n",
			tail_waste.n_ignored,
			humane_bytes(&h1, tail_waste.ignored_bytes));

	if (tail_waste.n_reclaimed)
		printf("\tReclaimed %s from %u journal files\n",
			humane_bytes(&h1, tail_waste.reclaimed_bytes),
			tail_waste.n_reclaimed);

	if (tail_waste.n_errored)
		printf("\tFailed to relcaim %s from %u journal files due to errors\n",
			humane_bytes(&h1, tail_waste.errored_bytes),
			tail_waste.n_errored);

	return 0;
}
© All Rights Reserved