]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sbin/geom/class/journal/geom_journal.c
MFC r368207,368607:
[FreeBSD/stable/10.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         bzero(sector, sizeof(sector));
146         nargs = gctl_get_int(req, "nargs");
147         str = NULL;     /* gcc */
148
149         strlcpy(md.md_magic, G_JOURNAL_MAGIC, sizeof(md.md_magic));
150         md.md_version = G_JOURNAL_VERSION;
151         md.md_id = arc4random();
152         md.md_joffset = 0;
153         md.md_jid = 0;
154         md.md_flags = GJ_FLAG_CLEAN;
155         checksum = gctl_get_int(req, "checksum");
156         if (checksum)
157                 md.md_flags |= GJ_FLAG_CHECKSUM;
158         force = gctl_get_int(req, "force");
159         hardcode = gctl_get_int(req, "hardcode");
160
161         if (nargs != 1 && nargs != 2) {
162                 gctl_error(req, "Invalid number of arguments.");
163                 return;
164         }
165
166         /* Verify the given providers. */
167         for (i = 0; i < nargs; i++) {
168                 str = gctl_get_ascii(req, "arg%d", i);
169                 if (g_get_mediasize(str) == 0) {
170                         gctl_error(req, "Invalid provider %s.", str);
171                         return;
172                 }
173         }
174
175         data = gctl_get_ascii(req, "arg0");
176         jsize = gctl_get_intmax(req, "jsize");
177         journal = NULL;
178         switch (nargs) {
179         case 1:
180                 if (!force && g_journal_fs_exists(data)) {
181                         gctl_error(req, "File system exists on %s and this "
182                             "operation would destroy it.\nUse -f if you "
183                             "really want to do it.", data);
184                         return;
185                 }
186                 journal = data;
187                 msize = g_get_mediasize(data);
188                 ssize = g_get_sectorsize(data);
189                 if (jsize == -1) {
190                         /*
191                          * No journal size specified. 1GB should be safe
192                          * default.
193                          */
194                         jsize = 1073741824ULL;
195                 } else {
196                         if (jsize < 104857600) {
197                                 gctl_error(req, "Journal too small.");
198                                 return;
199                         }
200                         if ((jsize % ssize) != 0) {
201                                 gctl_error(req, "Invalid journal size.");
202                                 return;
203                         }
204                 }
205                 if (jsize + ssize >= msize) {
206                         gctl_error(req, "Provider too small for journalling. "
207                             "You can try smaller jsize (default is %jd).",
208                             jsize);
209                         return;
210                 }
211                 md.md_jstart = msize - ssize - jsize;
212                 md.md_jend = msize - ssize;
213                 break;
214         case 2:
215                 if (!force && g_journal_fs_using_last_sector(data)) {
216                         gctl_error(req, "File system on %s is using the last "
217                             "sector and this operation is going to overwrite "
218                             "it. Use -f if you really want to do it.", data);
219                         return;
220                 }
221                 journal = gctl_get_ascii(req, "arg1");
222                 if (jsize != -1) {
223                         gctl_error(req, "jsize argument is valid only for "
224                             "all-in-one configuration.");
225                         return;
226                 }
227                 msize = g_get_mediasize(journal);
228                 ssize = g_get_sectorsize(journal);
229                 md.md_jstart = 0;
230                 md.md_jend = msize - ssize;
231                 break;
232         }
233
234         if (g_get_sectorsize(data) != g_get_sectorsize(journal)) {
235                 gctl_error(req, "Not equal sector sizes.");
236                 return;
237         }
238
239         /*
240          * Clear last sector first, to spoil all components if device exists.
241          */
242         for (i = 0; i < nargs; i++) {
243                 str = gctl_get_ascii(req, "arg%d", i);
244                 error = g_metadata_clear(str, NULL);
245                 if (error != 0) {
246                         gctl_error(req, "Cannot clear metadata on %s: %s.", str,
247                             strerror(error));
248                         return;
249                 }
250         }
251
252         /*
253          * Ok, store metadata.
254          */
255         for (i = 0; i < nargs; i++) {
256                 switch (i) {
257                 case 0:
258                         str = data;
259                         md.md_type = GJ_TYPE_DATA;
260                         if (nargs == 1)
261                                 md.md_type |= GJ_TYPE_JOURNAL;
262                         break;
263                 case 1:
264                         str = journal;
265                         md.md_type = GJ_TYPE_JOURNAL;
266                         break;
267                 }
268                 md.md_provsize = g_get_mediasize(str);
269                 assert(md.md_provsize != 0);
270                 if (!hardcode)
271                         bzero(md.md_provider, sizeof(md.md_provider));
272                 else {
273                         if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
274                                 str += sizeof(_PATH_DEV) - 1;
275                         strlcpy(md.md_provider, str, sizeof(md.md_provider));
276                 }
277                 journal_metadata_encode(&md, sector);
278                 error = g_metadata_store(str, sector, sizeof(sector));
279                 if (error != 0) {
280                         fprintf(stderr, "Cannot store metadata on %s: %s.\n",
281                             str, strerror(error));
282                         gctl_error(req, "Not fully done.");
283                         continue;
284                 }
285                 if (verbose)
286                         printf("Metadata value stored on %s.\n", str);
287         }
288 }
289
290 static void
291 journal_clear(struct gctl_req *req)
292 {
293         const char *name;
294         int error, i, nargs;
295
296         nargs = gctl_get_int(req, "nargs");
297         if (nargs < 1) {
298                 gctl_error(req, "Too few arguments.");
299                 return;
300         }
301
302         for (i = 0; i < nargs; i++) {
303                 name = gctl_get_ascii(req, "arg%d", i);
304                 error = g_metadata_clear(name, G_JOURNAL_MAGIC);
305                 if (error != 0) {
306                         fprintf(stderr, "Cannot clear metadata on %s: %s.\n",
307                             name, strerror(error));
308                         gctl_error(req, "Not fully done.");
309                         continue;
310                 }
311                 if (verbose)
312                         printf("Metadata cleared on %s.\n", name);
313         }
314 }
315
316 static void
317 journal_dump(struct gctl_req *req)
318 {
319         struct g_journal_metadata md, tmpmd;
320         const char *name;
321         int error, i, nargs;
322
323         nargs = gctl_get_int(req, "nargs");
324         if (nargs < 1) {
325                 gctl_error(req, "Too few arguments.");
326                 return;
327         }
328
329         for (i = 0; i < nargs; i++) {
330                 name = gctl_get_ascii(req, "arg%d", i);
331                 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
332                     G_JOURNAL_MAGIC);
333                 if (error != 0) {
334                         fprintf(stderr, "Cannot read metadata from %s: %s.\n",
335                             name, strerror(error));
336                         gctl_error(req, "Not fully done.");
337                         continue;
338                 }
339                 if (journal_metadata_decode((u_char *)&tmpmd, &md) != 0) {
340                         fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n",
341                             name);
342                         gctl_error(req, "Not fully done.");
343                         continue;
344                 }
345                 printf("Metadata on %s:\n", name);
346                 journal_metadata_dump(&md);
347                 printf("\n");
348         }
349 }