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