2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
40 #include <geom/journal/g_journal.h>
41 #include <core/geom.h>
42 #include <misc/subr.h>
44 #include "geom_journal.h"
47 uint32_t lib_version = G_LIB_VERSION;
48 uint32_t version = G_JOURNAL_VERSION;
50 static void journal_main(struct gctl_req *req, unsigned flags);
51 static void journal_clear(struct gctl_req *req);
52 static void journal_dump(struct gctl_req *req);
53 static void journal_label(struct gctl_req *req);
55 struct g_command class_commands[] = {
56 { "clear", G_FLAG_VERBOSE, journal_main, G_NULL_OPTS,
59 { "dump", 0, journal_main, G_NULL_OPTS,
62 { "label", G_FLAG_VERBOSE, journal_main,
64 { 'c', "checksum", NULL, G_TYPE_BOOL },
65 { 'f', "force", NULL, G_TYPE_BOOL },
66 { 'h', "hardcode", NULL, G_TYPE_BOOL },
67 { 's', "jsize", "-1", G_TYPE_NUMBER },
70 "[-cfhv] [-s jsize] dataprov [jprov]"
72 { "stop", G_FLAG_VERBOSE, NULL,
74 { 'f', "force", NULL, G_TYPE_BOOL },
79 { "sync", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
85 static int verbose = 0;
88 journal_main(struct gctl_req *req, unsigned flags)
92 if ((flags & G_FLAG_VERBOSE) != 0)
95 name = gctl_get_ascii(req, "verb");
97 gctl_error(req, "No '%s' argument.", "verb");
100 if (strcmp(name, "label") == 0)
102 else if (strcmp(name, "clear") == 0)
104 else if (strcmp(name, "dump") == 0)
107 gctl_error(req, "Unknown command: %s.", name);
111 g_journal_fs_exists(const char *prov)
114 if (g_journal_ufs_exists(prov))
117 if (g_journal_otherfs_exists(prov))
124 g_journal_fs_using_last_sector(const char *prov)
127 if (g_journal_ufs_using_last_sector(prov))
130 if (g_journal_otherfs_using_last_sector(prov))
137 journal_label(struct gctl_req *req)
139 struct g_journal_metadata md;
140 const char *data, *journal, *str;
142 intmax_t jsize, msize, ssize;
143 int error, force, i, nargs, checksum, hardcode;
145 nargs = gctl_get_int(req, "nargs");
146 str = NULL; /* gcc */
148 strlcpy(md.md_magic, G_JOURNAL_MAGIC, sizeof(md.md_magic));
149 md.md_version = G_JOURNAL_VERSION;
150 md.md_id = arc4random();
153 md.md_flags = GJ_FLAG_CLEAN;
154 checksum = gctl_get_int(req, "checksum");
156 md.md_flags |= GJ_FLAG_CHECKSUM;
157 force = gctl_get_int(req, "force");
158 hardcode = gctl_get_int(req, "hardcode");
160 if (nargs != 1 && nargs != 2) {
161 gctl_error(req, "Invalid number of arguments.");
165 /* Verify the given providers. */
166 for (i = 0; i < nargs; i++) {
167 str = gctl_get_ascii(req, "arg%d", i);
168 if (g_get_mediasize(str) == 0) {
169 gctl_error(req, "Invalid provider %s.", str);
174 data = gctl_get_ascii(req, "arg0");
175 jsize = gctl_get_intmax(req, "jsize");
179 if (!force && g_journal_fs_exists(data)) {
180 gctl_error(req, "File system exists on %s and this "
181 "operation would destroy it.\nUse -f if you "
182 "really want to do it.", data);
186 msize = g_get_mediasize(data);
187 ssize = g_get_sectorsize(data);
190 * No journal size specified. 1GB should be safe
193 jsize = 1073741824ULL;
195 if (jsize < 104857600) {
196 gctl_error(req, "Journal too small.");
199 if ((jsize % ssize) != 0) {
200 gctl_error(req, "Invalid journal size.");
204 if (jsize + ssize >= msize) {
205 gctl_error(req, "Provider too small for journalling. "
206 "You can try smaller jsize (default is %jd).",
210 md.md_jstart = msize - ssize - jsize;
211 md.md_jend = msize - ssize;
214 if (!force && g_journal_fs_using_last_sector(data)) {
215 gctl_error(req, "File system on %s is using the last "
216 "sector and this operation is going to overwrite "
217 "it. Use -f if you really want to do it.", data);
220 journal = gctl_get_ascii(req, "arg1");
222 gctl_error(req, "jsize argument is valid only for "
223 "all-in-one configuration.");
226 msize = g_get_mediasize(journal);
227 ssize = g_get_sectorsize(journal);
229 md.md_jend = msize - ssize;
233 if (g_get_sectorsize(data) != g_get_sectorsize(journal)) {
234 gctl_error(req, "Not equal sector sizes.");
239 * Clear last sector first, to spoil all components if device exists.
241 for (i = 0; i < nargs; i++) {
242 str = gctl_get_ascii(req, "arg%d", i);
243 error = g_metadata_clear(str, NULL);
245 gctl_error(req, "Cannot clear metadata on %s: %s.", str,
252 * Ok, store metadata.
254 for (i = 0; i < nargs; i++) {
258 md.md_type = GJ_TYPE_DATA;
260 md.md_type |= GJ_TYPE_JOURNAL;
264 md.md_type = GJ_TYPE_JOURNAL;
267 md.md_provsize = g_get_mediasize(str);
268 assert(md.md_provsize != 0);
270 bzero(md.md_provider, sizeof(md.md_provider));
272 if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
273 str += sizeof(_PATH_DEV) - 1;
274 strlcpy(md.md_provider, str, sizeof(md.md_provider));
276 journal_metadata_encode(&md, sector);
277 error = g_metadata_store(str, sector, sizeof(sector));
279 fprintf(stderr, "Cannot store metadata on %s: %s.\n",
280 str, strerror(error));
281 gctl_error(req, "Not fully done.");
285 printf("Metadata value stored on %s.\n", str);
290 journal_clear(struct gctl_req *req)
295 nargs = gctl_get_int(req, "nargs");
297 gctl_error(req, "Too few arguments.");
301 for (i = 0; i < nargs; i++) {
302 name = gctl_get_ascii(req, "arg%d", i);
303 error = g_metadata_clear(name, G_JOURNAL_MAGIC);
305 fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
306 name, strerror(error));
307 gctl_error(req, "Not fully done.");
311 printf("Metadata cleared on %s.\n", name);
316 journal_dump(struct gctl_req *req)
318 struct g_journal_metadata md, tmpmd;
322 nargs = gctl_get_int(req, "nargs");
324 gctl_error(req, "Too few arguments.");
328 for (i = 0; i < nargs; i++) {
329 name = gctl_get_ascii(req, "arg%d", i);
330 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
333 fprintf(stderr, "Cannot read metadata from %s: %s.\n",
334 name, strerror(error));
335 gctl_error(req, "Not fully done.");
338 if (journal_metadata_decode((u_char *)&tmpmd, &md) != 0) {
339 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
341 gctl_error(req, "Not fully done.");
344 printf("Metadata on %s:\n", name);
345 journal_metadata_dump(&md);