xref: /freebsd/lib/geom/journal/geom_journal.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <errno.h>
34 #include <paths.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <assert.h>
41 #include <libgeom.h>
42 #include <geom/journal/g_journal.h>
43 #include <core/geom.h>
44 #include <misc/subr.h>
45 
46 #include "geom_journal.h"
47 
48 
49 uint32_t lib_version = G_LIB_VERSION;
50 uint32_t version = G_JOURNAL_VERSION;
51 
52 static void journal_main(struct gctl_req *req, unsigned flags);
53 static void journal_clear(struct gctl_req *req);
54 static void journal_dump(struct gctl_req *req);
55 static void journal_label(struct gctl_req *req);
56 
57 struct g_command class_commands[] = {
58 	{ "clear", G_FLAG_VERBOSE, journal_main, G_NULL_OPTS,
59 	    "[-v] prov ..."
60 	},
61 	{ "dump", 0, journal_main, G_NULL_OPTS,
62 	    "prov ..."
63 	},
64 	{ "label", G_FLAG_VERBOSE, journal_main,
65 	    {
66 		{ 'c', "checksum", NULL, G_TYPE_BOOL },
67 		{ 'f', "force", NULL, G_TYPE_BOOL },
68 		{ 'h', "hardcode", NULL, G_TYPE_BOOL },
69 		{ 's', "jsize", "-1", G_TYPE_NUMBER },
70 		G_OPT_SENTINEL
71 	    },
72 	    "[-cfhv] [-s jsize] dataprov [jprov]"
73 	},
74 	{ "stop", G_FLAG_VERBOSE, NULL,
75 	    {
76 		{ 'f', "force", NULL, G_TYPE_BOOL },
77 		G_OPT_SENTINEL
78 	    },
79 	    "[-fv] name ..."
80 	},
81 	{ "sync", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
82 	    "[-v]"
83 	},
84 	G_CMD_SENTINEL
85 };
86 
87 static int verbose = 0;
88 
89 static void
90 journal_main(struct gctl_req *req, unsigned flags)
91 {
92 	const char *name;
93 
94 	if ((flags & G_FLAG_VERBOSE) != 0)
95 		verbose = 1;
96 
97 	name = gctl_get_ascii(req, "verb");
98 	if (name == NULL) {
99 		gctl_error(req, "No '%s' argument.", "verb");
100 		return;
101 	}
102 	if (strcmp(name, "label") == 0)
103 		journal_label(req);
104 	else if (strcmp(name, "clear") == 0)
105 		journal_clear(req);
106 	else if (strcmp(name, "dump") == 0)
107 		journal_dump(req);
108 	else
109 		gctl_error(req, "Unknown command: %s.", name);
110 }
111 
112 static int
113 g_journal_fs_exists(const char *prov)
114 {
115 
116 	if (g_journal_ufs_exists(prov))
117 		return (1);
118 #if 0
119 	if (g_journal_otherfs_exists(prov))
120 		return (1);
121 #endif
122 	return (0);
123 }
124 
125 static int
126 g_journal_fs_using_last_sector(const char *prov)
127 {
128 
129 	if (g_journal_ufs_using_last_sector(prov))
130 		return (1);
131 #if 0
132 	if (g_journal_otherfs_using_last_sector(prov))
133 		return (1);
134 #endif
135 	return (0);
136 }
137 
138 static void
139 journal_label(struct gctl_req *req)
140 {
141 	struct g_journal_metadata md;
142 	const char *data, *journal, *str;
143 	u_char sector[512];
144 	intmax_t jsize, msize, ssize;
145 	int error, force, i, nargs, checksum, hardcode;
146 
147 	bzero(sector, sizeof(sector));
148 	nargs = gctl_get_int(req, "nargs");
149 	str = NULL;	/* gcc */
150 
151 	strlcpy(md.md_magic, G_JOURNAL_MAGIC, sizeof(md.md_magic));
152 	md.md_version = G_JOURNAL_VERSION;
153 	md.md_id = arc4random();
154 	md.md_joffset = 0;
155 	md.md_jid = 0;
156 	md.md_flags = GJ_FLAG_CLEAN;
157 	checksum = gctl_get_int(req, "checksum");
158 	if (checksum)
159 		md.md_flags |= GJ_FLAG_CHECKSUM;
160 	force = gctl_get_int(req, "force");
161 	hardcode = gctl_get_int(req, "hardcode");
162 
163 	if (nargs != 1 && nargs != 2) {
164 		gctl_error(req, "Invalid number of arguments.");
165 		return;
166 	}
167 
168 	/* Verify the given providers. */
169 	for (i = 0; i < nargs; i++) {
170 		str = gctl_get_ascii(req, "arg%d", i);
171 		if (g_get_mediasize(str) == 0) {
172 			gctl_error(req, "Invalid provider %s.", str);
173 			return;
174 		}
175 	}
176 
177 	data = gctl_get_ascii(req, "arg0");
178 	jsize = gctl_get_intmax(req, "jsize");
179 	journal = NULL;
180 	switch (nargs) {
181 	case 1:
182 		if (!force && g_journal_fs_exists(data)) {
183 			gctl_error(req, "File system exists on %s and this "
184 			    "operation would destroy it.\nUse -f if you "
185 			    "really want to do it.", data);
186 			return;
187 		}
188 		journal = data;
189 		msize = g_get_mediasize(data);
190 		ssize = g_get_sectorsize(data);
191 		if (jsize == -1) {
192 			/*
193 			 * No journal size specified. 1GB should be safe
194 			 * default.
195 			 */
196 			jsize = 1073741824ULL;
197 		} else {
198 			if (jsize < 104857600) {
199 				gctl_error(req, "Journal too small.");
200 				return;
201 			}
202 			if ((jsize % ssize) != 0) {
203 				gctl_error(req, "Invalid journal size.");
204 				return;
205 			}
206 		}
207 		if (jsize + ssize >= msize) {
208 			gctl_error(req, "Provider too small for journalling. "
209 			    "You can try smaller jsize (default is %jd).",
210 			    jsize);
211 			return;
212 		}
213 		md.md_jstart = msize - ssize - jsize;
214 		md.md_jend = msize - ssize;
215 		break;
216 	case 2:
217 		if (!force && g_journal_fs_using_last_sector(data)) {
218 			gctl_error(req, "File system on %s is using the last "
219 			    "sector and this operation is going to overwrite "
220 			    "it. Use -f if you really want to do it.", data);
221 			return;
222 		}
223 		journal = gctl_get_ascii(req, "arg1");
224 		if (jsize != -1) {
225 			gctl_error(req, "jsize argument is valid only for "
226 			    "all-in-one configuration.");
227 			return;
228 		}
229 		msize = g_get_mediasize(journal);
230 		ssize = g_get_sectorsize(journal);
231 		md.md_jstart = 0;
232 		md.md_jend = msize - ssize;
233 		break;
234 	}
235 
236 	if (g_get_sectorsize(data) != g_get_sectorsize(journal)) {
237 		gctl_error(req, "Not equal sector sizes.");
238 		return;
239 	}
240 
241 	/*
242 	 * Clear last sector first, to spoil all components if device exists.
243 	 */
244 	for (i = 0; i < nargs; i++) {
245 		str = gctl_get_ascii(req, "arg%d", i);
246 		error = g_metadata_clear(str, NULL);
247 		if (error != 0) {
248 			gctl_error(req, "Cannot clear metadata on %s: %s.", str,
249 			    strerror(error));
250 			return;
251 		}
252 	}
253 
254 	/*
255 	 * Ok, store metadata.
256 	 */
257 	for (i = 0; i < nargs; i++) {
258 		switch (i) {
259 		case 0:
260 			str = data;
261 			md.md_type = GJ_TYPE_DATA;
262 			if (nargs == 1)
263 				md.md_type |= GJ_TYPE_JOURNAL;
264 			break;
265 		case 1:
266 			str = journal;
267 			md.md_type = GJ_TYPE_JOURNAL;
268 			break;
269 		}
270 		md.md_provsize = g_get_mediasize(str);
271 		assert(md.md_provsize != 0);
272 		if (!hardcode)
273 			bzero(md.md_provider, sizeof(md.md_provider));
274 		else {
275 			if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
276 				str += sizeof(_PATH_DEV) - 1;
277 			strlcpy(md.md_provider, str, sizeof(md.md_provider));
278 		}
279 		journal_metadata_encode(&md, sector);
280 		error = g_metadata_store(str, sector, sizeof(sector));
281 		if (error != 0) {
282 			fprintf(stderr, "Cannot store metadata on %s: %s.\n",
283 			    str, strerror(error));
284 			gctl_error(req, "Not fully done.");
285 			continue;
286 		}
287 		if (verbose)
288 			printf("Metadata value stored on %s.\n", str);
289 	}
290 }
291 
292 static void
293 journal_clear(struct gctl_req *req)
294 {
295 	const char *name;
296 	int error, i, nargs;
297 
298 	nargs = gctl_get_int(req, "nargs");
299 	if (nargs < 1) {
300 		gctl_error(req, "Too few arguments.");
301 		return;
302 	}
303 
304 	for (i = 0; i < nargs; i++) {
305 		name = gctl_get_ascii(req, "arg%d", i);
306 		error = g_metadata_clear(name, G_JOURNAL_MAGIC);
307 		if (error != 0) {
308 			fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
309 			    name, strerror(error));
310 			gctl_error(req, "Not fully done.");
311 			continue;
312 		}
313 		if (verbose)
314 			printf("Metadata cleared on %s.\n", name);
315 	}
316 }
317 
318 static void
319 journal_dump(struct gctl_req *req)
320 {
321 	struct g_journal_metadata md, tmpmd;
322 	const char *name;
323 	int error, i, nargs;
324 
325 	nargs = gctl_get_int(req, "nargs");
326 	if (nargs < 1) {
327 		gctl_error(req, "Too few arguments.");
328 		return;
329 	}
330 
331 	for (i = 0; i < nargs; i++) {
332 		name = gctl_get_ascii(req, "arg%d", i);
333 		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
334 		    G_JOURNAL_MAGIC);
335 		if (error != 0) {
336 			fprintf(stderr, "Cannot read metadata from %s: %s.\n",
337 			    name, strerror(error));
338 			gctl_error(req, "Not fully done.");
339 			continue;
340 		}
341 		if (journal_metadata_decode((u_char *)&tmpmd, &md) != 0) {
342 			fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
343 			    name);
344 			gctl_error(req, "Not fully done.");
345 			continue;
346 		}
347 		printf("Metadata on %s:\n", name);
348 		journal_metadata_dump(&md);
349 		printf("\n");
350 	}
351 }
352