1 /*
2  * Copyright (c) 2011-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stddef.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <err.h>
43 
44 #include "mkfs_hammer2.h"
45 
46 static void parse_fs_size(hammer2_mkfs_options_t *, const char *);
47 static void usage(void);
48 
49 int
50 main(int ac, char **av)
51 {
52 	hammer2_mkfs_options_t opt;
53 	int ch;
54 	int label_specified = 0;
55 
56 	/*
57 	 * Initialize option structure.
58 	 */
59 	hammer2_mkfs_init(&opt);
60 
61 	/*
62 	 * Parse arguments.
63 	 */
64 	while ((ch = getopt(ac, av, "L:b:r:V:s:d")) != -1) {
65 		switch(ch) {
66 		case 'b':
67 			opt.BootAreaSize = getsize(optarg,
68 					 HAMMER2_NEWFS_ALIGN,
69 					 HAMMER2_BOOT_MAX_BYTES, 2);
70 			break;
71 		case 'r':
72 			opt.AuxAreaSize = getsize(optarg,
73 					 HAMMER2_NEWFS_ALIGN,
74 					 HAMMER2_AUX_MAX_BYTES, 2);
75 			break;
76 		case 'V':
77 			opt.Hammer2Version = strtol(optarg, NULL, 0);
78 			if (opt.Hammer2Version < HAMMER2_VOL_VERSION_MIN ||
79 			    opt.Hammer2Version >= HAMMER2_VOL_VERSION_WIP) {
80 				errx(1, "I don't understand how to format "
81 				     "HAMMER2 version %d",
82 				     opt.Hammer2Version);
83 			}
84 			break;
85 		case 'L':
86 			label_specified = 1;
87 			if (strcasecmp(optarg, "none") == 0) {
88 				break;
89 			}
90 			if (opt.NLabels >= MAXLABELS) {
91 				errx(1, "Limit of %d local labels",
92 				     MAXLABELS - 1);
93 			}
94 			if (strlen(optarg) == 0) {
95 				errx(1, "Volume label '%s' cannot be 0-length",
96 					optarg);
97 			}
98 			if (strlen(optarg) >= HAMMER2_INODE_MAXNAME) {
99 				errx(1, "Volume label '%s' is too long "
100 					"(%d chars max)",
101 					optarg,
102 					HAMMER2_INODE_MAXNAME - 1);
103 			}
104 			opt.Label[opt.NLabels++] = strdup(optarg);
105 			break;
106 		case 's':
107 			parse_fs_size(&opt, optarg);
108 			break;
109 		case 'd':
110 			opt.DebugOpt = 1;
111 			break;
112 		default:
113 			usage();
114 			break;
115 		}
116 	}
117 
118 	ac -= optind;
119 	av += optind;
120 
121 	if (ac == 0)
122 		errx(1, "You must specify at least one disk device");
123 	if (ac > HAMMER2_MAX_VOLUMES)
124 		errx(1, "The maximum number of volumes is %d",
125 		     HAMMER2_MAX_VOLUMES);
126 
127 	/*
128 	 * Check default label type.
129 	 */
130 	if (!label_specified) {
131 		char c = av[0][strlen(av[0]) - 1];
132 		if (c == 'a')
133 			opt.DefaultLabelType = HAMMER2_LABEL_BOOT;
134 		else if (c == 'd')
135 			opt.DefaultLabelType = HAMMER2_LABEL_ROOT;
136 		else
137 			opt.DefaultLabelType = HAMMER2_LABEL_DATA;
138 	}
139 
140 	/*
141 	 * Create Hammer2 filesystem.
142 	 */
143 	hammer2_mkfs(ac, av, &opt);
144 
145 	/*
146 	 * Cleanup option structure.
147 	 */
148 	hammer2_mkfs_cleanup(&opt);
149 
150 	return(0);
151 }
152 
153 static
154 void
155 parse_fs_size(hammer2_mkfs_options_t *opt, const char *arg)
156 {
157 	char *o, *p, *s;
158 
159 	opt->NFileSystemSizes = 0;
160 	o = p = strdup(arg);
161 
162 	while ((s = p) != NULL) {
163 		if ((p = strchr(p, ':')) != NULL)
164 			*p++ = 0;
165 		/* XXX 0x7fffffffffffffff isn't limitation of HAMMER2 */
166 		opt->FileSystemSize[opt->NFileSystemSizes++] = getsize(s,
167 				 HAMMER2_FREEMAP_LEVEL1_SIZE,
168 				 0x7fffffffffffffff, 2);
169 		if (opt->NFileSystemSizes >= HAMMER2_MAX_VOLUMES)
170 			break;
171 	}
172 	free(o);
173 }
174 
175 static
176 void
177 usage(void)
178 {
179 	fprintf(stderr,
180 		"usage: newfs_hammer2 [-b bootsize] [-r auxsize] "
181 		"[-V version] [-L label ...] [-s size] special ...\n"
182 	);
183 	exit(1);
184 }
185