2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
42 #include <geom/journal/g_journal.h>
43 #include <core/geom.h>
44 #include <misc/subr.h>
46 #include "geom_journal.h"
49 uint32_t lib_version = G_LIB_VERSION;
50 uint32_t version = G_JOURNAL_VERSION;
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);
57 struct g_command class_commands[] = {
58 { "clear", G_FLAG_VERBOSE, journal_main, G_NULL_OPTS,
61 { "dump", 0, journal_main, G_NULL_OPTS,
64 { "label", G_FLAG_VERBOSE, journal_main,
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 },
72 "[-cfhv] [-s jsize] dataprov [jprov]"
74 { "stop", G_FLAG_VERBOSE, NULL,
76 { 'f', "force", NULL, G_TYPE_BOOL },
81 { "sync", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
87 static int verbose = 0;
90 journal_main(struct gctl_req *req, unsigned flags)
94 if ((flags & G_FLAG_VERBOSE) != 0)
97 name = gctl_get_ascii(req, "verb");
99 gctl_error(req, "No '%s' argument.", "verb");
102 if (strcmp(name, "label") == 0)
104 else if (strcmp(name, "clear") == 0)
106 else if (strcmp(name, "dump") == 0)
109 gctl_error(req, "Unknown command: %s.", name);
113 g_journal_fs_exists(const char *prov)
116 if (g_journal_ufs_exists(prov))
119 if (g_journal_otherfs_exists(prov))
126 g_journal_fs_using_last_sector(const char *prov)
129 if (g_journal_ufs_using_last_sector(prov))
132 if (g_journal_otherfs_using_last_sector(prov))
139 journal_label(struct gctl_req *req)
141 struct g_journal_metadata md;
142 const char *data, *journal, *str;
144 intmax_t jsize, msize, ssize;
145 int error, force, i, nargs, checksum, hardcode;
147 bzero(sector, sizeof(sector));
148 nargs = gctl_get_int(req, "nargs");
149 str = NULL; /* gcc */
151 strlcpy(md.md_magic, G_JOURNAL_MAGIC, sizeof(md.md_magic));
152 md.md_version = G_JOURNAL_VERSION;
153 md.md_id = arc4random();
156 md.md_flags = GJ_FLAG_CLEAN;
157 checksum = gctl_get_int(req, "checksum");
159 md.md_flags |= GJ_FLAG_CHECKSUM;
160 force = gctl_get_int(req, "force");
161 hardcode = gctl_get_int(req, "hardcode");
163 if (nargs != 1 && nargs != 2) {
164 gctl_error(req, "Invalid number of arguments.");
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);
177 data = gctl_get_ascii(req, "arg0");
178 jsize = gctl_get_intmax(req, "jsize");
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);
189 msize = g_get_mediasize(data);
190 ssize = g_get_sectorsize(data);
193 * No journal size specified. 1GB should be safe
196 jsize = 1073741824ULL;
198 if (jsize < 104857600) {
199 gctl_error(req, "Journal too small.");
202 if ((jsize % ssize) != 0) {
203 gctl_error(req, "Invalid journal size.");
207 if (jsize + ssize >= msize) {
208 gctl_error(req, "Provider too small for journalling. "
209 "You can try smaller jsize (default is %jd).",
213 md.md_jstart = msize - ssize - jsize;
214 md.md_jend = msize - ssize;
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);
223 journal = gctl_get_ascii(req, "arg1");
225 gctl_error(req, "jsize argument is valid only for "
226 "all-in-one configuration.");
229 msize = g_get_mediasize(journal);
230 ssize = g_get_sectorsize(journal);
232 md.md_jend = msize - ssize;
236 if (g_get_sectorsize(data) != g_get_sectorsize(journal)) {
237 gctl_error(req, "Not equal sector sizes.");
242 * Clear last sector first, to spoil all components if device exists.
244 for (i = 0; i < nargs; i++) {
245 str = gctl_get_ascii(req, "arg%d", i);
246 error = g_metadata_clear(str, NULL);
248 gctl_error(req, "Cannot clear metadata on %s: %s.", str,
255 * Ok, store metadata.
257 for (i = 0; i < nargs; i++) {
261 md.md_type = GJ_TYPE_DATA;
263 md.md_type |= GJ_TYPE_JOURNAL;
267 md.md_type = GJ_TYPE_JOURNAL;
270 md.md_provsize = g_get_mediasize(str);
271 assert(md.md_provsize != 0);
273 bzero(md.md_provider, sizeof(md.md_provider));
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));
279 journal_metadata_encode(&md, sector);
280 error = g_metadata_store(str, sector, sizeof(sector));
282 fprintf(stderr, "Cannot store metadata on %s: %s.\n",
283 str, strerror(error));
284 gctl_error(req, "Not fully done.");
288 printf("Metadata value stored on %s.\n", str);
293 journal_clear(struct gctl_req *req)
298 nargs = gctl_get_int(req, "nargs");
300 gctl_error(req, "Too few arguments.");
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);
308 fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
309 name, strerror(error));
310 gctl_error(req, "Not fully done.");
314 printf("Metadata cleared on %s.\n", name);
319 journal_dump(struct gctl_req *req)
321 struct g_journal_metadata md, tmpmd;
325 nargs = gctl_get_int(req, "nargs");
327 gctl_error(req, "Too few arguments.");
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),
336 fprintf(stderr, "Cannot read metadata from %s: %s.\n",
337 name, strerror(error));
338 gctl_error(req, "Not fully done.");
341 if (journal_metadata_decode((u_char *)&tmpmd, &md) != 0) {
342 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
344 gctl_error(req, "Not fully done.");
347 printf("Metadata on %s:\n", name);
348 journal_metadata_dump(&md);