newfs.c revision 1.25.6.2 1 1.25.6.2 yamt /* $NetBSD: newfs.c,v 1.25.6.2 2014/05/22 11:37:30 yamt Exp $ */
2 1.1 perseant
3 1.1 perseant /*-
4 1.1 perseant * Copyright (c) 1989, 1992, 1993
5 1.1 perseant * The Regents of the University of California. All rights reserved.
6 1.1 perseant *
7 1.1 perseant * Redistribution and use in source and binary forms, with or without
8 1.1 perseant * modification, are permitted provided that the following conditions
9 1.1 perseant * are met:
10 1.1 perseant * 1. Redistributions of source code must retain the above copyright
11 1.1 perseant * notice, this list of conditions and the following disclaimer.
12 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 perseant * notice, this list of conditions and the following disclaimer in the
14 1.1 perseant * documentation and/or other materials provided with the distribution.
15 1.12 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 perseant * may be used to endorse or promote products derived from this software
17 1.1 perseant * without specific prior written permission.
18 1.1 perseant *
19 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 perseant * SUCH DAMAGE.
30 1.1 perseant */
31 1.1 perseant
32 1.1 perseant #include <sys/cdefs.h>
33 1.1 perseant #ifndef lint
34 1.24 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1992, 1993\
35 1.24 lukem The Regents of the University of California. All rights reserved.");
36 1.1 perseant #endif /* not lint */
37 1.1 perseant
38 1.1 perseant #ifndef lint
39 1.1 perseant #if 0
40 1.1 perseant static char sccsid[] = "@(#)newfs.c 8.5 (Berkeley) 5/24/95";
41 1.1 perseant #else
42 1.25.6.2 yamt __RCSID("$NetBSD: newfs.c,v 1.25.6.2 2014/05/22 11:37:30 yamt Exp $");
43 1.1 perseant #endif
44 1.1 perseant #endif /* not lint */
45 1.1 perseant
46 1.1 perseant /*
47 1.1 perseant * newfs: friendly front end to mkfs
48 1.1 perseant */
49 1.1 perseant #include <sys/param.h>
50 1.1 perseant #include <sys/ucred.h>
51 1.1 perseant #include <sys/stat.h>
52 1.1 perseant #include <sys/ioctl.h>
53 1.1 perseant #include <sys/file.h>
54 1.1 perseant #include <sys/mount.h>
55 1.1 perseant #include <sys/sysctl.h>
56 1.6 perseant #include <sys/time.h>
57 1.23 riz #include <sys/disk.h>
58 1.1 perseant
59 1.6 perseant #include <ufs/lfs/lfs.h>
60 1.1 perseant
61 1.9 perseant #include <err.h>
62 1.1 perseant #include <errno.h>
63 1.1 perseant #include <unistd.h>
64 1.1 perseant #include <stdio.h>
65 1.1 perseant #include <stdlib.h>
66 1.1 perseant #include <ctype.h>
67 1.1 perseant #include <string.h>
68 1.1 perseant #include <paths.h>
69 1.1 perseant #include <util.h>
70 1.1 perseant #include "config.h"
71 1.1 perseant #include "extern.h"
72 1.16 perseant #include "bufcache.h"
73 1.23 riz #include "partutil.h"
74 1.1 perseant
75 1.1 perseant #define COMPAT /* allow non-labeled disks */
76 1.1 perseant
77 1.23 riz #ifdef COMPAT
78 1.23 riz const char lmsg[] = "%s: can't read disk label; disk type must be specified";
79 1.23 riz #else
80 1.23 riz const char lmsg[] = "%s: can't read disk label";
81 1.23 riz #endif
82 1.23 riz
83 1.7 perseant int Nflag = 0; /* run without writing file system */
84 1.1 perseant int fssize; /* file system size */
85 1.1 perseant int sectorsize; /* bytes/sector */
86 1.1 perseant int fsize = 0; /* fragment size */
87 1.1 perseant int bsize = 0; /* block size */
88 1.7 perseant int ibsize = 0; /* inode block size */
89 1.7 perseant int interleave = 0; /* segment interleave */
90 1.1 perseant int minfree = MINFREE; /* free space threshold */
91 1.20 perseant int minfreeseg = 0; /* segments not counted in bfree total */
92 1.20 perseant int resvseg = 0; /* free segments reserved for the cleaner */
93 1.7 perseant u_int32_t roll_id = 0; /* roll-forward id */
94 1.1 perseant u_long memleft; /* virtual memory available */
95 1.1 perseant caddr_t membase; /* start address of memory based filesystem */
96 1.1 perseant #ifdef COMPAT
97 1.1 perseant char *disktype;
98 1.1 perseant #endif
99 1.15 perseant int preen = 0; /* Coexistence with fsck_lfs */
100 1.1 perseant
101 1.1 perseant char device[MAXPATHLEN];
102 1.1 perseant char *progname, *special;
103 1.1 perseant
104 1.25 mlelstv extern long dev_bsize; /* device block size */
105 1.25 mlelstv
106 1.15 perseant static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
107 1.7 perseant static void usage(void);
108 1.1 perseant
109 1.7 perseant /* CHUNKSIZE should be larger than MAXPHYS */
110 1.7 perseant #define CHUNKSIZE (1024 * 1024)
111 1.6 perseant
112 1.6 perseant static size_t
113 1.6 perseant auto_segsize(int fd, off_t len, int version)
114 1.6 perseant {
115 1.6 perseant off_t off, bw;
116 1.6 perseant time_t start, finish;
117 1.6 perseant char buf[CHUNKSIZE];
118 1.6 perseant long seeks;
119 1.6 perseant size_t final;
120 1.6 perseant int i;
121 1.6 perseant
122 1.6 perseant /* First, get sequential access bandwidth */
123 1.6 perseant time(&start);
124 1.6 perseant finish = start;
125 1.6 perseant for (off = 0; finish - start < 10; off += CHUNKSIZE) {
126 1.6 perseant if (pread(fd, buf, CHUNKSIZE, off) < 0)
127 1.6 perseant break;
128 1.6 perseant time(&finish);
129 1.6 perseant }
130 1.6 perseant /* Bandwidth = bytes / sec */
131 1.6 perseant /* printf("%ld bytes in %ld seconds\n", (long)off, (long)(finish - start)); */
132 1.6 perseant bw = off / (finish - start);
133 1.6 perseant
134 1.6 perseant /* Second, seek time */
135 1.6 perseant time(&start);
136 1.6 perseant finish = start; /* structure copy */
137 1.6 perseant for (seeks = 0; finish - start < 10; ) {
138 1.19 jld off = (((double)rand()) * (btodb(len))) / ((off_t)RAND_MAX + 1);
139 1.19 jld if (pread(fd, buf, dbtob(1), dbtob(off)) < 0)
140 1.19 jld err(1, "pread");
141 1.6 perseant time(&finish);
142 1.6 perseant ++seeks;
143 1.6 perseant }
144 1.6 perseant /* printf("%ld seeks in %ld seconds\n", (long)seeks, (long)(finish - start)); */
145 1.6 perseant /* Seek time in units/sec */
146 1.6 perseant seeks /= (finish - start);
147 1.6 perseant if (seeks == 0)
148 1.6 perseant seeks = 1;
149 1.6 perseant
150 1.7 perseant printf("bw = %ld B/s, seek time %ld ms (%ld seeks/s)\n",
151 1.6 perseant (long)bw, 1000/seeks, seeks);
152 1.6 perseant final = dbtob(btodb(4 * bw / seeks));
153 1.6 perseant if (version == 1) {
154 1.6 perseant for (i = 0; final; final >>= 1, i++)
155 1.6 perseant ;
156 1.6 perseant final = 1 << i;
157 1.6 perseant }
158 1.6 perseant printf("using initial segment size %ld\n", (long)final);
159 1.6 perseant return final;
160 1.6 perseant }
161 1.6 perseant
162 1.1 perseant int
163 1.7 perseant main(int argc, char **argv)
164 1.1 perseant {
165 1.8 lukem int version, ch;
166 1.23 riz struct disk_geom geo;
167 1.23 riz struct dkwedge_info dkw;
168 1.1 perseant struct stat st;
169 1.25.6.2 yamt int debug, force, fsi, fso, lfs_segsize, maxpartitions;
170 1.13 dsl uint secsize = 0;
171 1.7 perseant daddr_t start;
172 1.17 christos const char *opstring;
173 1.15 perseant int byte_sized = 0;
174 1.16 perseant int r;
175 1.8 lukem
176 1.8 lukem version = DFL_VERSION; /* what version of lfs to make */
177 1.1 perseant
178 1.1 perseant if ((progname = strrchr(*argv, '/')) != NULL)
179 1.1 perseant ++progname;
180 1.1 perseant else
181 1.1 perseant progname = *argv;
182 1.1 perseant
183 1.1 perseant maxpartitions = getmaxpartitions();
184 1.1 perseant if (maxpartitions > 26)
185 1.1 perseant fatal("insane maxpartitions value %d", maxpartitions);
186 1.1 perseant
187 1.20 perseant opstring = "AB:b:DFf:I:i:LM:m:NO:R:r:S:s:v:";
188 1.1 perseant
189 1.25.6.2 yamt debug = force = lfs_segsize = start = 0;
190 1.1 perseant while ((ch = getopt(argc, argv, opstring)) != -1)
191 1.1 perseant switch(ch) {
192 1.6 perseant case 'A': /* Adaptively configure segment size */
193 1.25.6.2 yamt lfs_segsize = -1;
194 1.6 perseant break;
195 1.1 perseant case 'B': /* LFS segment size */
196 1.25.6.2 yamt lfs_segsize = strsuftoi64("segment size", optarg, LFS_MINSEGSIZE, INT64_MAX, NULL);
197 1.1 perseant break;
198 1.1 perseant case 'D':
199 1.1 perseant debug = 1;
200 1.1 perseant break;
201 1.3 perseant case 'F':
202 1.3 perseant force = 1;
203 1.3 perseant break;
204 1.7 perseant case 'I':
205 1.15 perseant interleave = strsuftoi64("interleave", optarg, 0, INT64_MAX, NULL);
206 1.7 perseant break;
207 1.6 perseant case 'L': /* Compatibility only */
208 1.1 perseant break;
209 1.4 perseant case 'M':
210 1.15 perseant minfreeseg = strsuftoi64("minfreeseg", optarg, 0, INT64_MAX, NULL);
211 1.4 perseant break;
212 1.1 perseant case 'N':
213 1.1 perseant Nflag++;
214 1.1 perseant break;
215 1.7 perseant case 'O':
216 1.15 perseant start = strsuftoi64("start", optarg, 0, INT64_MAX, NULL);
217 1.7 perseant break;
218 1.20 perseant case 'R':
219 1.20 perseant resvseg = strsuftoi64("resvseg", optarg, 0, INT64_MAX, NULL);
220 1.20 perseant break;
221 1.13 dsl case 'S':
222 1.15 perseant secsize = strsuftoi64("sector size", optarg, 1, INT64_MAX, NULL);
223 1.13 dsl if (secsize <= 0 || (secsize & (secsize - 1)))
224 1.13 dsl fatal("%s: bad sector size", optarg);
225 1.13 dsl break;
226 1.1 perseant #ifdef COMPAT
227 1.1 perseant case 'T':
228 1.1 perseant disktype = optarg;
229 1.2 perseant break;
230 1.2 perseant #endif
231 1.6 perseant case 'b':
232 1.15 perseant bsize = strsuftoi64("block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
233 1.1 perseant break;
234 1.1 perseant case 'f':
235 1.15 perseant fsize = strsuftoi64("fragment size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
236 1.1 perseant break;
237 1.7 perseant case 'i':
238 1.15 perseant ibsize = strsuftoi64("inode block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
239 1.7 perseant break;
240 1.2 perseant case 'm':
241 1.15 perseant minfree = strsuftoi64("free space %", optarg, 0, 99, NULL);
242 1.1 perseant break;
243 1.7 perseant case 'r':
244 1.15 perseant roll_id = strsuftoi64("roll-forward id", optarg, 1, UINT_MAX, NULL);
245 1.7 perseant break;
246 1.2 perseant case 's':
247 1.15 perseant fssize = strsuftoi64("file system size", optarg, 0, INT64_MAX, &byte_sized);
248 1.1 perseant break;
249 1.7 perseant case 'v':
250 1.15 perseant version = strsuftoi64("file system version", optarg, 1, LFS_VERSION, NULL);
251 1.7 perseant break;
252 1.1 perseant case '?':
253 1.1 perseant default:
254 1.1 perseant usage();
255 1.1 perseant }
256 1.1 perseant argc -= optind;
257 1.1 perseant argv += optind;
258 1.1 perseant
259 1.2 perseant if (argc != 2 && argc != 1)
260 1.1 perseant usage();
261 1.1 perseant
262 1.1 perseant /*
263 1.1 perseant * If the -N flag isn't specified, open the output file. If no path
264 1.1 perseant * prefix, try /dev/r%s and then /dev/%s.
265 1.1 perseant */
266 1.1 perseant special = argv[0];
267 1.1 perseant if (strchr(special, '/') == NULL) {
268 1.1 perseant (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
269 1.1 perseant special);
270 1.1 perseant if (stat(device, &st) == -1)
271 1.1 perseant (void)snprintf(device, sizeof(device), "%s%s",
272 1.1 perseant _PATH_DEV, special);
273 1.1 perseant special = device;
274 1.1 perseant }
275 1.1 perseant if (!Nflag) {
276 1.25.6.1 yamt fso = open(special, O_RDWR, DEFFILEMODE);
277 1.25.6.1 yamt if (debug && fso < 0) {
278 1.25.6.1 yamt /* Create a file of the requested size. */
279 1.25.6.1 yamt fso = open(special, O_CREAT | O_RDWR, DEFFILEMODE);
280 1.25.6.1 yamt if (fso >= 0) {
281 1.25.6.1 yamt char buf[512];
282 1.25.6.1 yamt int i;
283 1.25.6.1 yamt for (i = 0; i < fssize; i++)
284 1.25.6.1 yamt write(fso, buf, sizeof(buf));
285 1.25.6.1 yamt lseek(fso, 0, SEEK_SET);
286 1.25.6.1 yamt }
287 1.25.6.1 yamt }
288 1.1 perseant if (fso < 0)
289 1.1 perseant fatal("%s: %s", special, strerror(errno));
290 1.1 perseant } else
291 1.1 perseant fso = -1;
292 1.1 perseant
293 1.1 perseant /* Open the input file. */
294 1.1 perseant fsi = open(special, O_RDONLY);
295 1.1 perseant if (fsi < 0)
296 1.1 perseant fatal("%s: %s", special, strerror(errno));
297 1.1 perseant if (fstat(fsi, &st) < 0)
298 1.1 perseant fatal("%s: %s", special, strerror(errno));
299 1.1 perseant
300 1.6 perseant
301 1.13 dsl if (!S_ISCHR(st.st_mode)) {
302 1.21 perseant if (!S_ISREG(st.st_mode)) {
303 1.21 perseant fatal("%s: neither a character special device "
304 1.21 perseant "nor a regular file", special);
305 1.21 perseant }
306 1.23 riz (void)strcpy(dkw.dkw_ptype, DKW_PTYPE_LFS);
307 1.23 riz if (secsize == 0)
308 1.23 riz secsize = 512;
309 1.23 riz dkw.dkw_size = st.st_size / secsize;
310 1.13 dsl } else {
311 1.1 perseant #ifdef COMPAT
312 1.13 dsl if (disktype == NULL)
313 1.13 dsl disktype = argv[1];
314 1.1 perseant #endif
315 1.23 riz if (getdiskinfo(special, fsi, disktype, &geo, &dkw) == -1)
316 1.23 riz errx(1, lmsg, special);
317 1.1 perseant
318 1.23 riz if (dkw.dkw_size == 0)
319 1.23 riz fatal("%s: is zero sized", argv[0]);
320 1.23 riz if (!force && strcmp(dkw.dkw_ptype, DKW_PTYPE_LFS) != 0)
321 1.23 riz fatal("%s: is not `%s', but `%s'", argv[0],
322 1.23 riz DKW_PTYPE_LFS, dkw.dkw_ptype);
323 1.13 dsl }
324 1.13 dsl
325 1.13 dsl if (secsize == 0)
326 1.23 riz secsize = geo.dg_secsize;
327 1.3 perseant
328 1.25 mlelstv /* Make device block size available to low level routines */
329 1.25 mlelstv dev_bsize = secsize;
330 1.25 mlelstv
331 1.15 perseant /* From here on out fssize is in sectors */
332 1.15 perseant if (byte_sized) {
333 1.15 perseant fssize /= secsize;
334 1.15 perseant }
335 1.15 perseant
336 1.3 perseant /* If force, make the partition look like an LFS */
337 1.13 dsl if (force) {
338 1.23 riz (void)strcpy(dkw.dkw_ptype, DKW_PTYPE_LFS);
339 1.18 yamt if (fssize) {
340 1.23 riz dkw.dkw_size = fssize;
341 1.18 yamt }
342 1.13 dsl } else
343 1.23 riz if (fssize != 0 && fssize < dkw.dkw_size)
344 1.23 riz dkw.dkw_size = fssize;
345 1.1 perseant
346 1.6 perseant /* Try autoconfiguring segment size, if asked to */
347 1.25.6.2 yamt if (lfs_segsize == -1) {
348 1.9 perseant if (!S_ISCHR(st.st_mode)) {
349 1.9 perseant warnx("%s is not a character special device, ignoring -A", special);
350 1.25.6.2 yamt lfs_segsize = 0;
351 1.9 perseant } else
352 1.25.6.2 yamt lfs_segsize = auto_segsize(fsi, dkw.dkw_size / secsize,
353 1.23 riz version);
354 1.9 perseant }
355 1.6 perseant
356 1.1 perseant /* If we're making a LFS, we break out here */
357 1.25.6.2 yamt r = make_lfs(fso, secsize, &dkw, minfree, bsize, fsize, lfs_segsize,
358 1.23 riz minfreeseg, resvseg, version, start, ibsize, interleave, roll_id);
359 1.16 perseant if (debug)
360 1.16 perseant bufstats();
361 1.16 perseant exit(r);
362 1.1 perseant }
363 1.1 perseant
364 1.15 perseant static int64_t
365 1.15 perseant strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix)
366 1.15 perseant {
367 1.15 perseant int64_t result, r1;
368 1.15 perseant int shift = 0;
369 1.15 perseant char *ep;
370 1.15 perseant
371 1.15 perseant errno = 0;
372 1.15 perseant r1 = strtoll(arg, &ep, 10);
373 1.15 perseant if (ep[0] != '\0' && ep[1] != '\0')
374 1.15 perseant errx(1, "%s `%s' is not a valid number.", desc, arg);
375 1.15 perseant switch (ep[0]) {
376 1.15 perseant case '\0':
377 1.15 perseant case 's': case 'S':
378 1.15 perseant if (num_suffix != NULL)
379 1.15 perseant *num_suffix = 0;
380 1.15 perseant break;
381 1.15 perseant case 'g': case 'G':
382 1.15 perseant shift += 10;
383 1.15 perseant /* FALLTHROUGH */
384 1.15 perseant case 'm': case 'M':
385 1.15 perseant shift += 10;
386 1.15 perseant /* FALLTHROUGH */
387 1.15 perseant case 'k': case 'K':
388 1.15 perseant shift += 10;
389 1.15 perseant /* FALLTHROUGH */
390 1.15 perseant case 'b': case 'B':
391 1.15 perseant if (num_suffix != NULL)
392 1.15 perseant *num_suffix = 1;
393 1.15 perseant break;
394 1.15 perseant default:
395 1.15 perseant errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
396 1.15 perseant }
397 1.15 perseant result = r1 << shift;
398 1.15 perseant if (errno == ERANGE || result >> shift != r1)
399 1.15 perseant errx(1, "%s `%s' is too large to convert.", desc, arg);
400 1.15 perseant if (result < min)
401 1.15 perseant errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").",
402 1.15 perseant desc, arg, result, min);
403 1.15 perseant if (result > max)
404 1.15 perseant errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").",
405 1.15 perseant desc, arg, result, max);
406 1.15 perseant return result;
407 1.15 perseant }
408 1.15 perseant
409 1.1 perseant void
410 1.1 perseant usage()
411 1.1 perseant {
412 1.2 perseant fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
413 1.1 perseant fprintf(stderr, "where fsoptions are:\n");
414 1.6 perseant fprintf(stderr, "\t-A (autoconfigure segment size)\n");
415 1.6 perseant fprintf(stderr, "\t-B segment size in bytes\n");
416 1.7 perseant fprintf(stderr, "\t-D (debug)\n");
417 1.20 perseant fprintf(stderr, "\t-M count of segments not counted in bfree\n");
418 1.1 perseant fprintf(stderr,
419 1.6 perseant "\t-N (do not create file system, just print out parameters)\n");
420 1.7 perseant fprintf(stderr, "\t-O first segment offset in sectors\n");
421 1.20 perseant fprintf(stderr, "\t-R count of segments reserved for the cleaner\n");
422 1.6 perseant fprintf(stderr, "\t-b block size in bytes\n");
423 1.6 perseant fprintf(stderr, "\t-f frag size in bytes\n");
424 1.1 perseant fprintf(stderr, "\t-m minimum free space %%\n");
425 1.6 perseant fprintf(stderr, "\t-s file system size in sectors\n");
426 1.7 perseant fprintf(stderr, "\t-v version\n");
427 1.1 perseant exit(1);
428 1.1 perseant }
429