]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sbin/geom/class/journal/geom_journal.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 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);
54
55 struct g_command class_commands[] = {
56         { "clear", G_FLAG_VERBOSE, journal_main, G_NULL_OPTS,
57             "[-v] prov ..."
58         },
59         { "dump", 0, journal_main, G_NULL_OPTS,
60             "prov ..."
61         },
62         { "label", G_FLAG_VERBOSE, journal_main,
63             {
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 },
68                 G_OPT_SENTINEL
69             },
70             "[-cfhv] [-s jsize] dataprov [jprov]"
71         },
72         { "stop", G_FLAG_VERBOSE, NULL,
73             {
74                 { 'f', "force", NULL, G_TYPE_BOOL },
75                 G_OPT_SENTINEL
76             },
77             "[-fv] name ..."
78         },
79         { "sync", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
80             "[-v]"
81         },
82         G_CMD_SENTINEL
83 };
84
85 static int verbose = 0;
86
87 static void
88 journal_main(struct gctl_req *req, unsigned flags)
89 {
90         const char *name;
91
92         if ((flags & G_FLAG_VERBOSE) != 0)
93                 verbose = 1;
94
95         name = gctl_get_ascii(req, "verb");
96         if (name == NULL) {
97                 gctl_error(req, "No '%s' argument.", "verb");
98                 return;
99         }
100         if (strcmp(name, "label") == 0)
101                 journal_label(req);
102         else if (strcmp(name, "clear") == 0)
103                 journal_clear(req);
104         else if (strcmp(name, "dump") == 0)
105                 journal_dump(req);
106         else
107                 gctl_error(req, "Unknown command: %s.", name);
108 }
109
110 static int
111 g_journal_fs_exists(const char *prov)
112 {
113
114         if (g_journal_ufs_exists(prov))
115                 return (1);
116 #if 0
117         if (g_journal_otherfs_exists(prov))
118                 return (1);
119 #endif
120         return (0);
121 }
122
123 static int
124 g_journal_fs_using_last_sector(const char *prov)
125 {
126
127         if (g_journal_ufs_using_last_sector(prov))
128                 return (1);
129 #if 0
130         if (g_journal_otherfs_using_last_sector(prov))
131                 return (1);
132 #endif
133         return (0);
134 }
135
136 static void
137 journal_label(struct gctl_req *req)
138 {
139         struct g_journal_metadata md;
140         const char *data, *journal, *str;
141         u_char sector[512];
142         intmax_t jsize, msize, ssize;
143         int error, force, i, nargs, checksum, hardcode;
144
145         nargs = gctl_get_int(req, "nargs");
146         str = NULL;     /* gcc */
147
148         strlcpy(md.md_magic, G_JOURNAL_MAGIC, sizeof(md.md_magic));
149         md.md_version = G_JOURNAL_VERSION;
150         md.md_id = arc4random();
151         md.md_joffset = 0;
152         md.md_jid = 0;
153         md.md_flags = GJ_FLAG_CLEAN;
154         checksum = gctl_get_int(req, "checksum");
155         if (checksum)
156                 md.md_flags |= GJ_FLAG_CHECKSUM;
157         force = gctl_get_int(req, "force");
158         hardcode = gctl_get_int(req, "hardcode");
159
160         if (nargs != 1 && nargs != 2) {
161                 gctl_error(req, "Invalid number of arguments.");
162                 return;
163         }
164
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);
170                         return;
171                 }
172         }
173
174         data = gctl_get_ascii(req, "arg0");
175         jsize = gctl_get_intmax(req, "jsize");
176         journal = NULL;
177         switch (nargs) {
178         case 1:
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);
183                         return;
184                 }
185                 journal = data;
186                 msize = g_get_mediasize(data);
187                 ssize = g_get_sectorsize(data);
188                 if (jsize == -1) {
189                         /*
190                          * No journal size specified. 1GB should be safe
191                          * default.
192                          */
193                         jsize = 1073741824ULL;
194                 } else {
195                         if (jsize < 104857600) {
196                                 gctl_error(req, "Journal too small.");
197                                 return;
198                         }
199                         if ((jsize % ssize) != 0) {
200                                 gctl_error(req, "Invalid journal size.");
201                                 return;
202                         }
203                 }
204                 if (jsize + ssize >= msize) {
205                         gctl_error(req, "Provider too small for journalling. "
206                             "You can try smaller jsize (default is %jd).",
207                             jsize);
208                         return;
209                 }
210                 md.md_jstart = msize - ssize - jsize;
211                 md.md_jend = msize - ssize;
212                 break;
213         case 2:
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);
218                         return;
219                 }
220                 journal = gctl_get_ascii(req, "arg1");
221                 if (jsize != -1) {
222                         gctl_error(req, "jsize argument is valid only for "
223                             "all-in-one configuration.");
224                         return;
225                 }
226                 msize = g_get_mediasize(journal);
227                 ssize = g_get_sectorsize(journal);
228                 md.md_jstart = 0;
229                 md.md_jend = msize - ssize;
230                 break;
231         }
232
233         if (g_get_sectorsize(data) != g_get_sectorsize(journal)) {
234                 gctl_error(req, "Not equal sector sizes.");
235                 return;
236         }
237
238         /*
239          * Clear last sector first, to spoil all components if device exists.
240          */
241         for (i = 0; i < nargs; i++) {
242                 str = gctl_get_ascii(req, "arg%d", i);
243                 error = g_metadata_clear(str, NULL);
244                 if (error != 0) {
245                         gctl_error(req, "Cannot clear metadata on %s: %s.", str,
246                             strerror(error));
247                         return;
248                 }
249         }
250
251         /*
252          * Ok, store metadata.
253          */
254         for (i = 0; i < nargs; i++) {
255                 switch (i) {
256                 case 0:
257                         str = data;
258                         md.md_type = GJ_TYPE_DATA;
259                         if (nargs == 1)
260                                 md.md_type |= GJ_TYPE_JOURNAL;
261                         break;
262                 case 1:
263                         str = journal;
264                         md.md_type = GJ_TYPE_JOURNAL;
265                         break;
266                 }
267                 md.md_provsize = g_get_mediasize(str);
268                 assert(md.md_provsize != 0);
269                 if (!hardcode)
270                         bzero(md.md_provider, sizeof(md.md_provider));
271                 else {
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));
275                 }
276                 journal_metadata_encode(&md, sector);
277                 error = g_metadata_store(str, sector, sizeof(sector));
278                 if (error != 0) {
279                         fprintf(stderr, "Cannot store metadata on %s: %s.\n",
280                             str, strerror(error));
281                         gctl_error(req, "Not fully done.");
282                         continue;
283                 }
284                 if (verbose)
285                         printf("Metadata value stored on %s.\n", str);
286         }
287 }
288
289 static void
290 journal_clear(struct gctl_req *req)
291 {
292         const char *name;
293         int error, i, nargs;
294
295         nargs = gctl_get_int(req, "nargs");
296         if (nargs < 1) {
297                 gctl_error(req, "Too few arguments.");
298                 return;
299         }
300
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);
304                 if (error != 0) {
305                         fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
306                             name, strerror(error));
307                         gctl_error(req, "Not fully done.");
308                         continue;
309                 }
310                 if (verbose)
311                         printf("Metadata cleared on %s.\n", name);
312         }
313 }
314
315 static void
316 journal_dump(struct gctl_req *req)
317 {
318         struct g_journal_metadata md, tmpmd;
319         const char *name;
320         int error, i, nargs;
321
322         nargs = gctl_get_int(req, "nargs");
323         if (nargs < 1) {
324                 gctl_error(req, "Too few arguments.");
325                 return;
326         }
327
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),
331                     G_JOURNAL_MAGIC);
332                 if (error != 0) {
333                         fprintf(stderr, "Cannot read metadata from %s: %s.\n",
334                             name, strerror(error));
335                         gctl_error(req, "Not fully done.");
336                         continue;
337                 }
338                 if (journal_metadata_decode((u_char *)&tmpmd, &md) != 0) {
339                         fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
340                             name);
341                         gctl_error(req, "Not fully done.");
342                         continue;
343                 }
344                 printf("Metadata on %s:\n", name);
345                 journal_metadata_dump(&md);
346                 printf("\n");
347         }
348 }