Merge branch 'vendor/LIBARCHIVE'
[dragonfly.git] / sbin / newfs_hammer2 / newfs_hammer2.c
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 #include <sys/param.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stddef.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <err.h>
44
45 #include "mkfs_hammer2.h"
46
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: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 'd':
107                         opt.DebugOpt = 1;
108                         break;
109                 default:
110                         usage();
111                         break;
112                 }
113         }
114
115         ac -= optind;
116         av += optind;
117
118         if (ac == 0)
119                 errx(1, "You must specify at least one disk device");
120         if (ac > HAMMER2_MAX_VOLUMES)
121                 errx(1, "The maximum number of volumes is %d",
122                      HAMMER2_MAX_VOLUMES);
123
124         /*
125          * Check default label type.
126          */
127         if (!label_specified) {
128                 char c = av[0][strlen(av[0]) - 1];
129                 if (c == 'a')
130                         opt.DefaultLabelType = HAMMER2_LABEL_BOOT;
131                 else if (c == 'd')
132                         opt.DefaultLabelType = HAMMER2_LABEL_ROOT;
133                 else
134                         opt.DefaultLabelType = HAMMER2_LABEL_DATA;
135         }
136
137         /*
138          * Create Hammer2 filesystem.
139          */
140         hammer2_mkfs(ac, av, &opt);
141
142         /*
143          * Cleanup option structure.
144          */
145         hammer2_mkfs_cleanup(&opt);
146
147         return(0);
148 }
149
150 static
151 void
152 usage(void)
153 {
154         fprintf(stderr,
155                 "usage: newfs_hammer2 [-b bootsize] [-r auxsize] "
156                 "[-V version] [-L label ...] special ...\n"
157         );
158         exit(1);
159 }