]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/geom/class/journal/geom_journal.c
This commit was generated by cvs2svn to compensate for changes in r164146,
[FreeBSD/FreeBSD.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,
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", &default_jsize, 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         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 is going to destroy it. Use -f if you "
184                             "really want to do it.", data);
185                         return;
186                 }
187                 journal = data;
188                 if (jsize == -1) {
189                         /*
190                          * No journal size specified. 1GB should be safe
191                          * default.
192                          */
193                         jsize = 1073741824ULL;
194                 }
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).",
200                             jsize);
201                         return;
202                 }
203                 md.md_jstart = msize - ssize - jsize;
204                 md.md_jend = msize - ssize;
205                 break;
206         case 2:
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);
211                         return;
212                 }
213                 journal = gctl_get_ascii(req, "arg1");
214                 if (jsize != -1) {
215                         gctl_error(req, "jsize argument is valid only for "
216                             "all-in-one configuration.");
217                         return;
218                 }
219                 msize = g_get_mediasize(journal);
220                 ssize = g_get_sectorsize(journal);
221                 md.md_jstart = 0;
222                 md.md_jend = msize - ssize;
223                 break;
224         }
225
226         if (g_get_sectorsize(data) != g_get_sectorsize(journal)) {
227                 gctl_error(req, "Not equal sector sizes.");
228                 return;
229         }
230
231         /*
232          * Clear last sector first, to spoil all components if device exists.
233          */
234         for (i = 0; i < nargs; i++) {
235                 str = gctl_get_ascii(req, "arg%d", i);
236                 error = g_metadata_clear(str, NULL);
237                 if (error != 0) {
238                         gctl_error(req, "Cannot clear metadata on %s: %s.", str,
239                             strerror(error));
240                         return;
241                 }
242         }
243
244         /*
245          * Ok, store metadata.
246          */
247         for (i = 0; i < nargs; i++) {
248                 switch (i) {
249                 case 0:
250                         str = data;
251                         md.md_type = GJ_TYPE_DATA;
252                         if (nargs == 1)
253                                 md.md_type |= GJ_TYPE_JOURNAL;
254                         break;
255                 case 1:
256                         str = journal;
257                         md.md_type = GJ_TYPE_JOURNAL;
258                         break;
259                 }
260                 md.md_provsize = g_get_mediasize(str);
261                 assert(md.md_provsize != 0);
262                 if (!hardcode)
263                         bzero(md.md_provider, sizeof(md.md_provider));
264                 else {
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));
268                 }
269                 journal_metadata_encode(&md, sector);
270                 error = g_metadata_store(str, sector, sizeof(sector));
271                 if (error != 0) {
272                         fprintf(stderr, "Cannot store metadata on %s: %s.\n",
273                             str, strerror(error));
274                         gctl_error(req, "Not fully done.");
275                         continue;
276                 }
277                 if (verbose)
278                         printf("Metadata value stored on %s.\n", str);
279         }
280 }
281
282 static void
283 journal_clear(struct gctl_req *req)
284 {
285         const char *name;
286         int error, i, nargs;
287
288         nargs = gctl_get_int(req, "nargs");
289         if (nargs < 1) {
290                 gctl_error(req, "Too few arguments.");
291                 return;
292         }
293
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);
297                 if (error != 0) {
298                         fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
299                             name, strerror(error));
300                         gctl_error(req, "Not fully done.");
301                         continue;
302                 }
303                 if (verbose)
304                         printf("Metadata cleared on %s.\n", name);
305         }
306 }
307
308 static void
309 journal_dump(struct gctl_req *req)
310 {
311         struct g_journal_metadata md, tmpmd;
312         const char *name;
313         int error, i, nargs;
314
315         nargs = gctl_get_int(req, "nargs");
316         if (nargs < 1) {
317                 gctl_error(req, "Too few arguments.");
318                 return;
319         }
320
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),
324                     G_JOURNAL_MAGIC);
325                 if (error != 0) {
326                         fprintf(stderr, "Cannot read metadata from %s: %s.\n",
327                             name, strerror(error));
328                         gctl_error(req, "Not fully done.");
329                         continue;
330                 }
331                 if (journal_metadata_decode((u_char *)&tmpmd, &md) != 0) {
332                         fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
333                             name);
334                         gctl_error(req, "Not fully done.");
335                         continue;
336                 }
337                 printf("Metadata on %s:\n", name);
338                 journal_metadata_dump(&md);
339                 printf("\n");
340         }
341 }