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 intmax_t default_jsize = -1;
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", &default_jsize, 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 nargs = gctl_get_int(req, "nargs");
148 str = NULL; /* gcc */
150 strlcpy(md.md_magic, G_JOURNAL_MAGIC, sizeof(md.md_magic));
151 md.md_version = G_JOURNAL_VERSION;
152 md.md_id = arc4random();
155 md.md_flags = GJ_FLAG_CLEAN;
156 checksum = gctl_get_int(req, "checksum");
158 md.md_flags |= GJ_FLAG_CHECKSUM;
159 force = gctl_get_int(req, "force");
160 hardcode = gctl_get_int(req, "hardcode");
162 if (nargs != 1 && nargs != 2) {
163 gctl_error(req, "Invalid number of arguments.");
167 /* Verify the given providers. */
168 for (i = 0; i < nargs; i++) {
169 str = gctl_get_ascii(req, "arg%d", i);
170 if (g_get_mediasize(str) == 0) {
171 gctl_error(req, "Invalid provider %s.", str);
176 data = gctl_get_ascii(req, "arg0");
177 jsize = gctl_get_intmax(req, "jsize");
181 if (!force && g_journal_fs_exists(data)) {
182 gctl_error(req, "File system exists on %s and this "
183 "operation is going to destroy it. Use -f if you "
184 "really want to do it.", data);
190 * No journal size specified. 1GB should be safe
193 jsize = 1073741824ULL;
195 msize = g_get_mediasize(data);
196 ssize = g_get_sectorsize(data);
197 if (jsize + ssize >= msize) {
198 gctl_error(req, "Provider too small for journalling. "
199 "You can try smaller jsize (default is %jd).",
203 md.md_jstart = msize - ssize - jsize;
204 md.md_jend = msize - ssize;
207 if (!force && g_journal_fs_using_last_sector(data)) {
208 gctl_error(req, "File system on %s is using the last "
209 "sector and this operation is going to overwrite "
210 "it. Use -f if you really want to do it.", data);
213 journal = gctl_get_ascii(req, "arg1");
215 gctl_error(req, "jsize argument is valid only for "
216 "all-in-one configuration.");
219 msize = g_get_mediasize(journal);
220 ssize = g_get_sectorsize(journal);
222 md.md_jend = msize - ssize;
226 if (g_get_sectorsize(data) != g_get_sectorsize(journal)) {
227 gctl_error(req, "Not equal sector sizes.");
232 * Clear last sector first, to spoil all components if device exists.
234 for (i = 0; i < nargs; i++) {
235 str = gctl_get_ascii(req, "arg%d", i);
236 error = g_metadata_clear(str, NULL);
238 gctl_error(req, "Cannot clear metadata on %s: %s.", str,
245 * Ok, store metadata.
247 for (i = 0; i < nargs; i++) {
251 md.md_type = GJ_TYPE_DATA;
253 md.md_type |= GJ_TYPE_JOURNAL;
257 md.md_type = GJ_TYPE_JOURNAL;
260 md.md_provsize = g_get_mediasize(str);
261 assert(md.md_provsize != 0);
263 bzero(md.md_provider, sizeof(md.md_provider));
265 if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0)
266 str += strlen(_PATH_DEV);
267 strlcpy(md.md_provider, str, sizeof(md.md_provider));
269 journal_metadata_encode(&md, sector);
270 error = g_metadata_store(str, sector, sizeof(sector));
272 fprintf(stderr, "Cannot store metadata on %s: %s.\n",
273 str, strerror(error));
274 gctl_error(req, "Not fully done.");
278 printf("Metadata value stored on %s.\n", str);
283 journal_clear(struct gctl_req *req)
288 nargs = gctl_get_int(req, "nargs");
290 gctl_error(req, "Too few arguments.");
294 for (i = 0; i < nargs; i++) {
295 name = gctl_get_ascii(req, "arg%d", i);
296 error = g_metadata_clear(name, G_JOURNAL_MAGIC);
298 fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
299 name, strerror(error));
300 gctl_error(req, "Not fully done.");
304 printf("Metadata cleared on %s.\n", name);
309 journal_dump(struct gctl_req *req)
311 struct g_journal_metadata md, tmpmd;
315 nargs = gctl_get_int(req, "nargs");
317 gctl_error(req, "Too few arguments.");
321 for (i = 0; i < nargs; i++) {
322 name = gctl_get_ascii(req, "arg%d", i);
323 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
326 fprintf(stderr, "Cannot read metadata from %s: %s.\n",
327 name, strerror(error));
328 gctl_error(req, "Not fully done.");
331 if (journal_metadata_decode((u_char *)&tmpmd, &md) != 0) {
332 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
334 gctl_error(req, "Not fully done.");
337 printf("Metadata on %s:\n", name);
338 journal_metadata_dump(&md);