xref: /minix/sbin/newfs_ext2fs/newfs_ext2fs.c (revision 84d9c625)
1 /*	$NetBSD: newfs_ext2fs.c,v 1.9 2013/10/19 01:09:59 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1989, 1993, 1994
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) 1983, 1989, 1993, 1994\
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.13 (Berkeley) 5/1/95";
41 #else
42 __RCSID("$NetBSD: newfs_ext2fs.c,v 1.9 2013/10/19 01:09:59 christos Exp $");
43 #endif
44 #endif /* not lint */
45 
46 /*
47  * newfs: friendly front end to mke2fs
48  */
49 #include <sys/param.h>
50 #include <sys/ioctl.h>
51 #include <sys/disklabel.h>
52 #include <sys/disk.h>
53 #include <sys/file.h>
54 #include <sys/mount.h>
55 
56 #include <ufs/ext2fs/ext2fs.h>
57 #include <ufs/ext2fs/ext2fs_dinode.h>
58 
59 #include <disktab.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <limits.h>
63 #include <paths.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <util.h>
69 #include <mntopts.h>
70 
71 #if defined(__minix)
72 #include <minix/partition.h>
73 #endif /* !defined(__minix) */
74 
75 #include "extern.h"
76 #include "partutil.h"
77 
78 static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
79 static void usage(void) __dead;
80 
81 /*
82  * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
83  * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
84  * L_DFL_*.
85  */
86 #define SMALL_FSSIZE	((4 * 1024 * 1024) / sectorsize)	/* 4MB */
87 #if !defined(__minix)
88 #define S_DFL_BSIZE	1024
89 #else
90 #define S_DFL_BSIZE	4096
91 #endif /* !defined(__minix) */
92 #define MEDIUM_FSSIZE	((512 * 1024 * 1024) / sectorsize)	/* 512MB */
93 #if !defined(__minix)
94 #define M_DFL_BSIZE	1024
95 #else
96 #define M_DFL_BSIZE	4096
97 #endif /* !defined(__minix) */
98 #define L_DFL_BSIZE	4096
99 
100 /*
101  * Each file system has a number of inodes statically allocated.
102  * We allocate one inode slot per 2, 4, or 8 blocks, expecting this
103  * to be far more than we will ever need.
104  */
105 #define S_DFL_NINODE(blocks)	((blocks) / 8)
106 #define M_DFL_NINODE(blocks)	((blocks) / 4)
107 #define L_DFL_NINODE(blocks)	((blocks) / 2)
108 
109 /*
110  * Default sector size.
111  */
112 #define	DFL_SECSIZE	512
113 
114 int	Nflag;			/* run without writing file system */
115 int	Oflag = 0;		/* format as conservative REV0 by default */
116 int	verbosity;		/* amount of printf() output */
117 #define DEFAULT_VERBOSITY 3	/* 4 is traditional behavior of newfs(8) */
118 int64_t fssize;			/* file system size */
119 uint	sectorsize;		/* bytes/sector */
120 uint16_t inodesize = EXT2_REV0_DINODE_SIZE;	/* inode size */
121 uint	fsize = 0;		/* fragment size */
122 uint	bsize = 0;		/* block size */
123 uint	minfree = MINFREE;	/* free space threshold */
124 uint	density;		/* number of bytes per inode */
125 uint	num_inodes;		/* number of inodes (overrides density) */
126 char	*volname = NULL;	/* volume name */
127 
128 #if !defined(__minix)
129 static char *disktype = NULL;
130 #endif /* !defined(__minix) */
131 static char device[MAXPATHLEN];
132 
133 static const char lmsg[] = "%s: can't read disk label";
134 
135 int
136 main(int argc, char *argv[])
137 {
138 #if !defined(__minix)
139 	struct disk_geom geo;
140 	struct dkwedge_info dkw;
141 #else
142 	u64_t minix_fssize;
143 #endif /* !defined(__minix) */
144 	struct statvfs *mp;
145 	struct stat sb;
146 	int ch, fsi, fso, len, n, Fflag, Iflag, Zflag;
147 	char *s1, *s2, *special;
148 	const char *opstring;
149 	int byte_sized;
150 	uint blocks;			/* number of blocks */
151 
152 	fsi = fso = -1;
153 	Fflag = Iflag = Zflag = 0;
154 	verbosity = -1;
155 #if !defined(__minix)
156 	opstring = "D:FINO:S:V:Zb:f:i:l:m:n:s:v:";
157 #else
158 	opstring = "D:FINO:S:V:Zb:f:i:l:m:n:s:v:B:";
159 #endif /* !defined(__minix) */
160 	byte_sized = 0;
161 	while ((ch = getopt(argc, argv, opstring)) != -1)
162 		switch (ch) {
163 		case 'D':
164 			inodesize = (uint16_t)strtol(optarg, &s1, 0);
165 			if (*s1 || (inodesize != 128 && inodesize != 256))
166 				errx(1, "Bad inode size %d "
167 				    "(only 128 and 256 supported)", inodesize);
168 			break;
169 		case 'F':
170 			Fflag = 1;
171 			break;
172 #if !defined(__minix)
173 		case 'I':
174 			Iflag = 1;
175 			break;
176 #endif /* !defined(__minix) */
177 		case 'N':
178 			Nflag = 1;
179 			if (verbosity == -1)
180 				verbosity = DEFAULT_VERBOSITY;
181 			break;
182 		case 'O':
183 			Oflag = strsuftoi64("format", optarg, 0, 1, NULL);
184 			break;
185 		case 'S':
186 			/*
187 			 * XXX:
188 			 * non-512 byte sectors almost certainly don't work.
189 			 */
190 			sectorsize = strsuftoi64("sector size",
191 			    optarg, 512, 65536, NULL);
192 			if (!powerof2(sectorsize))
193 				errx(EXIT_FAILURE,
194 				    "sector size `%s' is not a power of 2.",
195 				    optarg);
196 			break;
197 		case 'V':
198 			verbosity = strsuftoi64("verbose", optarg, 0, 4, NULL);
199 			break;
200 #if !defined(__minix)
201 		case 'Z':
202 			Zflag = 1;
203 			break;
204 #else
205 		case 'B':
206 #endif /* !defined(__minix) */
207 		case 'b':
208 			bsize = strsuftoi64("block size",
209 			    optarg, MINBSIZE, EXT2_MAXBSIZE, NULL);
210 			break;
211 		case 'f':
212 			fsize = strsuftoi64("fragment size",
213 			    optarg, MINBSIZE, EXT2_MAXBSIZE, NULL);
214 			break;
215 		case 'i':
216 			density = strsuftoi64("bytes per inode",
217 			    optarg, 1, INT_MAX, NULL);
218 			break;
219 		case 'm':
220 			minfree = strsuftoi64("free space %",
221 			    optarg, 0, 99, NULL);
222 			break;
223 		case 'n':
224 			num_inodes = strsuftoi64("number of inodes",
225 			    optarg, 1, INT_MAX, NULL);
226 			break;
227 		case 's':
228 			fssize = strsuftoi64("file system size",
229 			    optarg, INT64_MIN, INT64_MAX, &byte_sized);
230 			break;
231 		case 'v':
232 			volname = optarg;
233 			if (volname[0] == '\0')
234 				errx(EXIT_FAILURE,
235 				    "Volume name cannot be zero length");
236 			break;
237 		case '?':
238 		default:
239 			usage();
240 		}
241 	argc -= optind;
242 	argv += optind;
243 
244 	if (verbosity == -1)
245 		/* Default to showing cg info */
246 		verbosity = DEFAULT_VERBOSITY;
247 
248 	if (argc != 1)
249 		usage();
250 
251 	memset(&sb, 0, sizeof(sb));
252 #if !defined(__minix)
253 	memset(&dkw, 0, sizeof(dkw));
254 #endif /* !defined(__minix) */
255 	special = argv[0];
256 	if (Fflag) {
257 		int fl;
258 		/*
259 		 * It's a file system image
260 		 * no label, use fixed default for sectorsize.
261 		 */
262 		if (sectorsize == 0)
263 			sectorsize = DFL_SECSIZE;
264 
265 		/* creating image in a regular file */
266 		if (Nflag)
267 			fl = O_RDONLY;
268 		else {
269 			if (fssize > 0)
270 				fl = O_RDWR | O_CREAT;
271 			else
272 				fl = O_RDWR;
273 		}
274 		fsi = open(special, fl, 0777);
275 		if (fsi == -1)
276 			err(EXIT_FAILURE, "can't open file %s", special);
277 		if (fstat(fsi, &sb) == -1)
278 			err(EXIT_FAILURE, "can't fstat opened %s", special);
279 		if (!Nflag)
280 			fso = fsi;
281 	} else {	/* !Fflag */
282 		fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0);
283 		special = device;
284 		if (fsi < 0 || fstat(fsi, &sb) == -1)
285 			err(EXIT_FAILURE, "%s: open for read", special);
286 
287 		if (!Nflag) {
288 			fso = open(special, O_WRONLY, 0);
289 			if (fso < 0)
290 				err(EXIT_FAILURE,
291 				    "%s: open for write", special);
292 
293 			/* Bail if target special is mounted */
294 			n = getmntinfo(&mp, MNT_NOWAIT);
295 			if (n == 0)
296 				err(EXIT_FAILURE, "%s: getmntinfo", special);
297 
298 			len = sizeof(_PATH_DEV) - 1;
299 			s1 = special;
300 			if (strncmp(_PATH_DEV, s1, len) == 0)
301 				s1 += len;
302 
303 			while (--n >= 0) {
304 				s2 = mp->f_mntfromname;
305 				if (strncmp(_PATH_DEV, s2, len) == 0) {
306 					s2 += len - 1;
307 					*s2 = 'r';
308 				}
309 				if (strcmp(s1, s2) == 0 ||
310 				    strcmp(s1, &s2[1]) == 0)
311 					errx(EXIT_FAILURE,
312 					    "%s is mounted on %s",
313 					    special, mp->f_mntonname);
314 				++mp;
315 			}
316 		}
317 
318 #if !defined(__minix)
319 		if (getdiskinfo(special, fsi, disktype, &geo, &dkw) == -1)
320 			errx(EXIT_FAILURE, lmsg, special);
321 
322 		if (sectorsize == 0) {
323 			sectorsize = geo.dg_secsize;
324 			if (sectorsize <= 0)
325 				errx(EXIT_FAILURE, "no default sector size");
326 		}
327 
328 		if (dkw.dkw_parent[0]) {
329 			if (dkw.dkw_size == 0)
330 				errx(EXIT_FAILURE,
331 				    "%s partition is unavailable", special);
332 
333 			if (!Iflag) {
334 				static const char m[] =
335 				    "%s partition type is not `%s' (or use -I)";
336 				if (strcmp(dkw.dkw_ptype, DKW_PTYPE_EXT2FS))
337 					errx(EXIT_FAILURE, m,
338 					    special, "Linux Ext2");
339 			}
340 		}
341 #else
342 		if(minix_sizeup(special, &minix_fssize) < 0)
343 			errx(EXIT_FAILURE, "minix_sizeup failed");
344 
345 		fssize = minix_fssize;
346 		byte_sized = 1;
347 
348 		if (sectorsize == 0)
349 			sectorsize = 512;
350 #endif /* !defined(__minix) */
351 	}
352 
353 	if (byte_sized)
354 		fssize /= sectorsize;
355 #if !defined(__minix)
356 	if (fssize <= 0) {
357 		if (sb.st_size != 0)
358 			fssize += sb.st_size / sectorsize;
359 		else
360 			fssize += dkw.dkw_size;
361 		if (fssize <= 0)
362 			errx(EXIT_FAILURE,
363 			    "Unable to determine file system size");
364 	}
365 
366 	if (dkw.dkw_parent[0] && fssize > dkw.dkw_size)
367 		errx(EXIT_FAILURE,
368 		    "size %" PRIu64 " exceeds maximum file system size on "
369 		    "`%s' of %" PRIu64 " sectors",
370 		    fssize, special, dkw.dkw_size);
371 #endif /* !defined(__minix) */
372 
373 	/* XXXLUKEM: only ftruncate() regular files ? (dsl: or at all?) */
374 	if (Fflag && fso != -1
375 	    && ftruncate(fso, (off_t)fssize * sectorsize) == -1)
376 		err(1, "can't ftruncate %s to %" PRId64, special, fssize);
377 
378 #if !defined(__minix)
379 	if (Zflag && fso != -1) {	/* pre-zero (and de-sparce) the file */
380 		char *buf;
381 		int bufsize, i;
382 		off_t bufrem;
383 		struct statvfs sfs;
384 
385 		if (fstatvfs(fso, &sfs) == -1) {
386 			warn("can't fstatvfs `%s'", special);
387 			bufsize = 8192;
388 		} else
389 			bufsize = sfs.f_iosize;
390 
391 		if ((buf = calloc(1, bufsize)) == NULL)
392 			err(1, "can't malloc buffer of %d",
393 			bufsize);
394 		bufrem = fssize * sectorsize;
395 		if (verbosity > 0)
396 			printf("Creating file system image in `%s', "
397 			    "size %" PRId64 " bytes, in %d byte chunks.\n",
398 			    special, bufrem, bufsize);
399 		while (bufrem > 0) {
400 			i = write(fso, buf, MIN(bufsize, bufrem));
401 			if (i == -1)
402 				err(1, "writing image");
403 			bufrem -= i;
404 		}
405 		free(buf);
406 	}
407 #endif /* !defined(__minix) */
408 
409 	/* Sort out fragment and block sizes */
410 	if (bsize == 0) {
411 		bsize = fsize;
412 		if (bsize == 0) {
413 			if (fssize < SMALL_FSSIZE)
414 				bsize = S_DFL_BSIZE;
415 			else if (fssize < MEDIUM_FSSIZE)
416 				bsize = M_DFL_BSIZE;
417 			else
418 				bsize = L_DFL_BSIZE;
419 		}
420 	}
421 	if (fsize == 0)
422 		fsize = bsize;
423 
424 	blocks = fssize * sectorsize / bsize;
425 
426 	if (num_inodes == 0) {
427 		if (density != 0)
428 			num_inodes = fssize / density;
429 		else {
430 			if (fssize < SMALL_FSSIZE)
431 				num_inodes = S_DFL_NINODE(blocks);
432 			else if (fssize < MEDIUM_FSSIZE)
433 				num_inodes = M_DFL_NINODE(blocks);
434 			else
435 				num_inodes = L_DFL_NINODE(blocks);
436 		}
437 	}
438 	mke2fs(special, fsi, fso);
439 
440 	if (fsi != -1)
441 		close(fsi);
442 	if (fso != -1 && fso != fsi)
443 		close(fso);
444 	exit(EXIT_SUCCESS);
445 }
446 
447 static int64_t
448 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max,
449     int *num_suffix)
450 {
451 	int64_t result, r1;
452 	int shift = 0;
453 	char *ep;
454 
455 	errno = 0;
456 	r1 = strtoll(arg, &ep, 10);
457 	if (ep[0] != '\0' && ep[1] != '\0')
458 		errx(EXIT_FAILURE,
459 		    "%s `%s' is not a valid number.", desc, arg);
460 	switch (ep[0]) {
461 	case '\0':
462 	case 's':
463 	case 'S':
464 		if (num_suffix != NULL)
465 			*num_suffix = 0;
466 		break;
467 	case 'g':
468 	case 'G':
469 		shift += 10;
470 		/* FALLTHROUGH */
471 	case 'm':
472 	case 'M':
473 		shift += 10;
474 		/* FALLTHROUGH */
475 	case 'k':
476 	case 'K':
477 		shift += 10;
478 		/* FALLTHROUGH */
479 	case 'b':
480 	case 'B':
481 		if (num_suffix != NULL)
482 			*num_suffix = 1;
483 		break;
484 	default:
485 		errx(EXIT_FAILURE,
486 		    "`%s' is not a valid suffix for %s.", ep, desc);
487 	}
488 	result = r1 << shift;
489 	if (errno == ERANGE || result >> shift != r1)
490 		errx(EXIT_FAILURE,
491 		    "%s `%s' is too large to convert.", desc, arg);
492 	if (result < min)
493 		errx(EXIT_FAILURE,
494 		    "%s `%s' (%" PRId64 ") is less than the minimum (%"
495 		    PRId64 ").", desc, arg, result, min);
496 	if (result > max)
497 		errx(EXIT_FAILURE,
498 		    "%s `%s' (%" PRId64 ") is greater than the maximum (%"
499 		    PRId64 ").", desc, arg, result, max);
500 	return result;
501 }
502 
503 static const char help_strings[] =
504 	"\t-b bsize\tblock size\n"
505 	"\t-D inodesize\tsize of an inode in bytes (128 or 256)\n"
506 	"\t-F \t\tcreate file system image in regular file\n"
507 	"\t-f fsize\tfragment size\n"
508 	"\t-I \t\tdo not check that the file system type is `Linux Ext2'\n"
509 	"\t-i density\tnumber of bytes per inode\n"
510 	"\t-m minfree\tminimum free space %\n"
511 	"\t-N \t\tdo not create file system, just print out parameters\n"
512 	"\t-n inodes\tnumber of inodes (overrides -i density)\n"
513 	"\t-O N\t\tfilesystem revision: 0 ==> REV0, 1 ==> REV1 (default 0)\n"
514 	"\t-S secsize\tsector size\n"
515 	"\t-s fssize\tfile system size (sectors)\n"
516 	"\t-V verbose\toutput verbosity: 0 ==> none, 4 ==> max\n"
517 	"\t-v volname\text2fs volume name\n"
518 	"\t-Z \t\tpre-zero the image file\n";
519 
520 static void
521 usage(void)
522 {
523 
524 	fprintf(stderr,
525 	    "usage: %s [ fsoptions ] special-device\n", getprogname());
526 	fprintf(stderr, "where fsoptions are:\n");
527 	fprintf(stderr, "%s", help_strings);
528 
529 	exit(EXIT_FAILURE);
530 }
531