]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/geom/class/journal/geom_journal.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sbin / geom / class / journal / geom_journal.c
1 /*-
2  * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <paths.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <assert.h>
39 #include <libgeom.h>
40 #include <geom/journal/g_journal.h>
41 #include <core/geom.h>
42 #include <misc/subr.h>
43
44 #include "geom_journal.h"
45
46
47 uint32_t lib_version = G_LIB_VERSION;
48 uint32_t version = G_JOURNAL_VERSION;
49
50 static intmax_t default_jsize = -1;
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, NULL,
59             "[-v] prov ..."
60         },
61         { "dump", 0, journal_main, G_NULL_OPTS, NULL,
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", &default_jsize, G_TYPE_NUMBER },
70                 G_OPT_SENTINEL
71             },
72             NULL, "[-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             NULL, "[-fv] name ..."
80         },
81         { "sync", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, NULL,
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         nargs = gctl_get_int(req, "nargs");
148         str = NULL;     /* gcc */
149
150         strlcpy(md.md_magic, G_JOURNAL_MAGIC, sizeof(md.md_magic));
151         md.md_version = G_JOURNAL_VERSION;
152         md.md_id = arc4random();
153         md.md_joffset = 0;
154         md.md_jid = 0;
155         md.md_flags = GJ_FLAG_CLEAN;
156         checksum = gctl_get_int(req, "checksum");
157         if (checksum)
158                 md.md_flags |= GJ_FLAG_CHECKSUM;
159         force = gctl_get_int(req, "force");
160         hardcode = gctl_get_int(req, "hardcode");
161
162         if (nargs != 1 && nargs != 2) {
163                 gctl_error(req, "Invalid number of arguments.");
164                 return;
165         }
166
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);
172                         return;
173                 }
174         }
175
176         data = gctl_get_ascii(req, "arg0");
177         jsize = gctl_get_intmax(req, "jsize");
178         journal = NULL;
179         switch (nargs) {
180         case 1:
181                 if (!force && g_journal_fs_exists(data)) {
182                         gctl_error(req, "File system exists on %s and this "
183                             "operation would destroy it.\nUse -f if you "
184                             "really want to do it.", data);
185                         return;
186                 }
187                 journal = data;
188                 msize = g_get_mediasize(data);
189                 ssize = g_get_sectorsize(data);
190                 if (jsize == -1) {
191                         /*
192                          * No journal size specified. 1GB should be safe
193                          * default.
194                          */
195                         jsize = 1073741824ULL;
196                 } else {
197                         if (jsize < 104857600) {
198                                 gctl_error(req, "Journal too small.");
199                                 return;
200                         }
201                         if ((jsize % ssize) != 0) {
202                                 gctl_error(req, "Invalid journal size.");
203                                 return;
204                         }
205                 }
206                 if (jsize + ssize >= msize) {
207                         gctl_error(req, "Provider too small for journalling. "
208                             "You can try smaller jsize (default is %jd).",
209                             jsize);
210                         return;
211                 }
212                 md.md_jstart = msize - ssize - jsize;
213                 md.md_jend = msize - ssize;
214                 break;
215         case 2:
216                 if (!force && g_journal_fs_using_last_sector(data)) {
217                         gctl_error(req, "File system on %s is using the last "
218                             "sector and this operation is going to overwrite "
219                             "it. Use -f if you really want to do it.", data);
220                         return;
221                 }
222                 journal = gctl_get_ascii(req, "arg1");
223                 if (jsize != -1) {
224                         gctl_error(req, "jsize argument is valid only for "
225                             "all-in-one configuration.");
226                         return;
227                 }
228                 msize = g_get_mediasize(journal);
229                 ssize = g_get_sectorsize(journal);
230                 md.md_jstart = 0;
231                 md.md_jend = msize - ssize;
232                 break;
233         }
234
235         if (g_get_sectorsize(data) != g_get_sectorsize(journal)) {
236                 gctl_error(req, "Not equal sector sizes.");
237                 return;
238         }
239
240         /*
241          * Clear last sector first, to spoil all components if device exists.
242          */
243         for (i = 0; i < nargs; i++) {
244                 str = gctl_get_ascii(req, "arg%d", i);
245                 error = g_metadata_clear(str, NULL);
246                 if (error != 0) {
247                         gctl_error(req, "Cannot clear metadata on %s: %s.", str,
248                             strerror(error));
249                         return;
250                 }
251         }
252
253         /*
254          * Ok, store metadata.
255          */
256         for (i = 0; i < nargs; i++) {
257                 switch (i) {
258                 case 0:
259                         str = data;
260                         md.md_type = GJ_TYPE_DATA;
261                         if (nargs == 1)
262                                 md.md_type |= GJ_TYPE_JOURNAL;
263                         break;
264                 case 1:
265                         str = journal;
266                         md.md_type = GJ_TYPE_JOURNAL;
267                         break;
268                 }
269                 md.md_provsize = g_get_mediasize(str);
270                 assert(md.md_provsize != 0);
271                 if (!hardcode)
272                         bzero(md.md_provider, sizeof(md.md_provider));
273                 else {
274                         if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0)
275                                 str += strlen(_PATH_DEV);
276                         strlcpy(md.md_provider, str, sizeof(md.md_provider));
277                 }
278                 journal_metadata_encode(&md, sector);
279                 error = g_metadata_store(str, sector, sizeof(sector));
280                 if (error != 0) {
281                         fprintf(stderr, "Cannot store metadata on %s: %s.\n",
282                             str, strerror(error));
283                         gctl_error(req, "Not fully done.");
284                         continue;
285                 }
286                 if (verbose)
287                         printf("Metadata value stored on %s.\n", str);
288         }
289 }
290
291 static void
292 journal_clear(struct gctl_req *req)
293 {
294         const char *name;
295         int error, i, nargs;
296
297         nargs = gctl_get_int(req, "nargs");
298         if (nargs < 1) {
299                 gctl_error(req, "Too few arguments.");
300                 return;
301         }
302
303         for (i = 0; i < nargs; i++) {
304                 name = gctl_get_ascii(req, "arg%d", i);
305                 error = g_metadata_clear(name, G_JOURNAL_MAGIC);
306                 if (error != 0) {
307                         fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
308                             name, strerror(error));
309                         gctl_error(req, "Not fully done.");
310                         continue;
311                 }
312                 if (verbose)
313                         printf("Metadata cleared on %s.\n", name);
314         }
315 }
316
317 static void
318 journal_dump(struct gctl_req *req)
319 {
320         struct g_journal_metadata md, tmpmd;
321         const char *name;
322         int error, i, nargs;
323
324         nargs = gctl_get_int(req, "nargs");
325         if (nargs < 1) {
326                 gctl_error(req, "Too few arguments.");
327                 return;
328         }
329
330         for (i = 0; i < nargs; i++) {
331                 name = gctl_get_ascii(req, "arg%d", i);
332                 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
333                     G_JOURNAL_MAGIC);
334                 if (error != 0) {
335                         fprintf(stderr, "Cannot read metadata from %s: %s.\n",
336                             name, strerror(error));
337                         gctl_error(req, "Not fully done.");
338                         continue;
339                 }
340                 if (journal_metadata_decode((u_char *)&tmpmd, &md) != 0) {
341                         fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
342                             name);
343                         gctl_error(req, "Not fully done.");
344                         continue;
345                 }
346                 printf("Metadata on %s:\n", name);
347                 journal_metadata_dump(&md);
348                 printf("\n");
349         }
350 }