GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/newfs_ext2fs/newfs_ext2fs.c Lines: 0 211 0.0 %
Date: 2017-11-07 Branches: 0 170 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: newfs_ext2fs.c,v 1.25 2016/03/16 08:34:11 natano Exp $ */
2
/*	$NetBSD: newfs_ext2fs.c,v 1.8 2009/03/02 10:38:13 tsutsui Exp $	*/
3
4
/*
5
 * Copyright (c) 1983, 1989, 1993, 1994
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
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 the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
/*
34
 * newfs: friendly front end to mke2fs
35
 */
36
#include <sys/param.h>	/* powerof2 */
37
#include <sys/types.h>
38
#include <sys/ioctl.h>
39
#include <sys/dkio.h>
40
#include <sys/disklabel.h>
41
#include <sys/mount.h>
42
43
#include <ufs/ext2fs/ext2fs.h>
44
#include <ufs/ext2fs/ext2fs_dinode.h>
45
46
#include <ctype.h>
47
#include <err.h>
48
#include <errno.h>
49
#include <fcntl.h>
50
#include <inttypes.h>
51
#include <limits.h>
52
#include <paths.h>
53
#include <stdint.h>
54
#include <stdio.h>
55
#include <stdlib.h>
56
#include <string.h>
57
#include <unistd.h>
58
#include <util.h>
59
60
#define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
61
62
#include "extern.h"
63
64
static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
65
static void usage(void) __dead;
66
67
/*
68
 * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
69
 * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
70
 * L_DFL_*.
71
 */
72
#define SMALL_FSSIZE	((4 * 1024 * 1024) / sectorsize)	/* 4MB */
73
#define S_DFL_BSIZE	1024
74
#define MEDIUM_FSSIZE	((512 * 1024 * 1024) / sectorsize)	/* 512MB */
75
#define M_DFL_BSIZE	1024
76
#define L_DFL_BSIZE	4096
77
78
/*
79
 * Each file system has a number of inodes statically allocated.
80
 * We allocate one inode slot per 2, 4, or 8 blocks, expecting this
81
 * to be far more than we will ever need.
82
 */
83
#define S_DFL_NINODE(blocks)	((blocks) / 8)
84
#define M_DFL_NINODE(blocks)	((blocks) / 4)
85
#define L_DFL_NINODE(blocks)	((blocks) / 2)
86
87
/*
88
 * Default sector size.
89
 */
90
#define	DFL_SECSIZE	512
91
92
int	Nflag;			/* run without writing file system */
93
int	Oflag = 0;		/* format as conservative REV0 by default */
94
int	verbosity;		/* amount of printf() output */
95
#define DEFAULT_VERBOSITY 4	/* 4 is traditional behavior of newfs(8) */
96
int64_t fssize;			/* file system size */
97
uint	sectorsize;		/* bytes/sector */
98
uint16_t inodesize = EXT2_REV0_DINODE_SIZE;	/* inode size */
99
uint	fsize = 0;		/* fragment size */
100
uint	bsize = 0;		/* block size */
101
uint	minfree = MINFREE;	/* free space threshold */
102
uint	density;		/* number of bytes per inode */
103
uint	num_inodes;		/* number of inodes (overrides density) */
104
int	max_cols;
105
char	*volname = NULL;	/* volume name */
106
107
static char *disktype = NULL;
108
109
struct disklabel *getdisklabel(const char *, int);
110
struct partition *getpartition(int, const char *, char *[], struct disklabel **);
111
112
int
113
main(int argc, char *argv[])
114
{
115
	struct statfs *mp;
116
	struct stat sb;
117
	int ch, fd, len, n, Fflag, Iflag, Zflag;
118
	char *s1, *s2, *special;
119
	const char *opstring;
120
	int byte_sized, fl;
121
	uint blocks;			/* number of blocks */
122
	struct partition *pp = NULL;
123
	struct disklabel *lp;
124
	struct winsize winsize;
125
126
	/* Get terminal width */
127
	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) == 0)
128
		max_cols = winsize.ws_col;
129
	else
130
		max_cols = 80;
131
132
	if (pledge("stdio rpath wpath cpath disklabel flock", NULL) == -1)
133
		err(1, "pledge");
134
135
	Fflag = Iflag = Zflag = 0;
136
	verbosity = -1;
137
	opstring = "D:FINO:S:V:Zb:f:i:l:m:n:qs:t:v:";
138
	byte_sized = 0;
139
	while ((ch = getopt(argc, argv, opstring)) != -1)
140
		switch (ch) {
141
		case 'D':
142
			inodesize = (uint16_t)strtol(optarg, &s1, 0);
143
			if (*s1 || (inodesize != 128 && inodesize != 256))
144
				errx(1, "Bad inode size %d "
145
				    "(only 128 and 256 supported)", inodesize);
146
			break;
147
		case 'F':
148
			Fflag = 1;
149
			break;
150
		case 'I':
151
			Iflag = 1;
152
			break;
153
		case 'N':
154
			Nflag = 1;
155
			if (verbosity == -1)
156
				verbosity = DEFAULT_VERBOSITY;
157
			break;
158
		case 'O':
159
			Oflag = strsuftoi64("format", optarg, 0, 1, NULL);
160
			break;
161
		case 'S':
162
			/*
163
			 * XXX:
164
			 * non-512 byte sectors almost certainly don't work.
165
			 */
166
			sectorsize = strsuftoi64("sector size",
167
			    optarg, 512, 65536, NULL);
168
			if (!powerof2(sectorsize))
169
				errx(EXIT_FAILURE,
170
				    "sector size `%s' is not a power of 2.",
171
				    optarg);
172
			break;
173
		case 'V':
174
			verbosity = strsuftoi64("verbose", optarg, 0, 4, NULL);
175
			break;
176
		case 'Z':
177
			Zflag = 1;
178
			break;
179
		case 'b':
180
			bsize = strsuftoi64("block size",
181
			    optarg, MINBSIZE, EXT2_MAXBSIZE, NULL);
182
			break;
183
		case 'f':
184
			fsize = strsuftoi64("fragment size",
185
			    optarg, MINBSIZE, EXT2_MAXBSIZE, NULL);
186
			break;
187
		case 'i':
188
			density = strsuftoi64("bytes per inode",
189
			    optarg, 1, INT_MAX, NULL);
190
			break;
191
		case 'm':
192
			minfree = strsuftoi64("free space %",
193
			    optarg, 0, 99, NULL);
194
			break;
195
		case 'n':
196
			num_inodes = strsuftoi64("number of inodes",
197
			    optarg, 1, INT_MAX, NULL);
198
			break;
199
		case 'q':
200
			verbosity = 1;
201
			break;
202
		case 's':
203
			fssize = strsuftoi64("file system size",
204
			    optarg, INT64_MIN, INT64_MAX, &byte_sized);
205
			break;
206
		case 't':
207
			/* compat with newfs -t */
208
			break;
209
		case 'v':
210
			volname = optarg;
211
			if (volname[0] == '\0')
212
				errx(EXIT_FAILURE,
213
				    "Volume name cannot be zero length");
214
			break;
215
		case '?':
216
		default:
217
			usage();
218
		}
219
	argc -= optind;
220
	argv += optind;
221
222
	if (verbosity == -1)
223
		/* Default to showing cg info */
224
		verbosity = DEFAULT_VERBOSITY;
225
226
	if (argc != 1)
227
		usage();
228
229
	memset(&sb, 0, sizeof(sb));
230
	special = argv[0];
231
	fl = Nflag ? O_RDONLY : O_RDWR;
232
233
	if (Fflag) {
234
		/*
235
		 * It's a file system image
236
		 * no label, use fixed default for sectorsize.
237
		 */
238
		if (sectorsize == 0)
239
			sectorsize = DFL_SECSIZE;
240
241
		/* creating image in a regular file */
242
		if (!Nflag && fssize > 0)
243
			fl |= O_CREAT;
244
		fd = open(special, fl, 0666);
245
		if (fd == -1)
246
			err(EXIT_FAILURE, "can't open file %s", special);
247
		if (fstat(fd, &sb) == -1)
248
			err(EXIT_FAILURE, "can't fstat opened %s", special);
249
	} else {	/* !Fflag */
250
		fd = opendev(special, fl, 0, &special);
251
		if (fd < 0 || fstat(fd, &sb) == -1)
252
			err(EXIT_FAILURE, "%s: open", special);
253
254
		if (!Nflag) {
255
			/* Bail if target special is mounted */
256
			n = getmntinfo(&mp, MNT_NOWAIT);
257
			if (n == 0)
258
				err(EXIT_FAILURE, "%s: getmntinfo", special);
259
260
			len = sizeof(_PATH_DEV) - 1;
261
			s1 = special;
262
			if (strncmp(_PATH_DEV, s1, len) == 0)
263
				s1 += len;
264
265
			while (--n >= 0) {
266
				s2 = mp->f_mntfromname;
267
				if (strncmp(_PATH_DEV, s2, len) == 0) {
268
					s2 += len - 1;
269
					*s2 = 'r';
270
				}
271
				if (strcmp(s1, s2) == 0 ||
272
				    strcmp(s1, &s2[1]) == 0)
273
					errx(EXIT_FAILURE,
274
					    "%s is mounted on %s",
275
					    special, mp->f_mntonname);
276
				++mp;
277
			}
278
		}
279
280
		pp = getpartition(fd, special, argv, &lp);
281
		if (!Iflag) {
282
			static const char m[] =
283
			    "%s partition type is not `%s' (or use -I)";
284
			if (pp->p_fstype != FS_EXT2FS)
285
				errx(EXIT_FAILURE, m, special, "ext2fs");
286
		}
287
		if (sectorsize == 0) {
288
			sectorsize = lp->d_secsize;
289
			if (sectorsize <= 0)
290
				errx(EXIT_FAILURE, "no default sector size");
291
		}
292
	}
293
294
	if (byte_sized)
295
		fssize /= sectorsize;
296
	if (fssize <= 0) {
297
		if (sb.st_size != 0)
298
			fssize += sb.st_size / sectorsize;
299
		else if (pp)
300
			fssize += DL_GETPSIZE(pp);
301
		if (fssize <= 0)
302
			errx(EXIT_FAILURE,
303
			    "Unable to determine file system size");
304
	}
305
306
	/* XXXLUKEM: only ftruncate() regular files ? (dsl: or at all?) */
307
	if (Fflag && !Nflag
308
	    && ftruncate(fd, (off_t)fssize * sectorsize) == -1)
309
		err(1, "can't ftruncate %s to %" PRId64, special, fssize);
310
311
	if (Zflag && !Nflag) {	/* pre-zero (and de-sparce) the file */
312
		char *buf;
313
		int bufsize, i;
314
		off_t bufrem;
315
		struct statfs sfs;
316
317
		if (fstatfs(fd, &sfs) == -1) {
318
			warn("can't fstatvfs `%s'", special);
319
			bufsize = 8192;
320
		} else
321
			bufsize = sfs.f_iosize;
322
323
		if ((buf = calloc(1, bufsize)) == NULL)
324
			err(1, "can't allocate buffer of %d",
325
			bufsize);
326
		bufrem = fssize * sectorsize;
327
		if (verbosity > 0)
328
			printf("Creating file system image in `%s', "
329
			    "size %" PRId64 " bytes, in %d byte chunks.\n",
330
			    special, bufrem, bufsize);
331
		while (bufrem > 0) {
332
			i = write(fd, buf, MINIMUM(bufsize, bufrem));
333
			if (i == -1)
334
				err(1, "writing image");
335
			bufrem -= i;
336
		}
337
		free(buf);
338
	}
339
340
	/* Sort out fragment and block sizes */
341
	if (bsize == 0) {
342
		bsize = fsize;
343
		if (bsize == 0) {
344
			if (fssize < SMALL_FSSIZE)
345
				bsize = S_DFL_BSIZE;
346
			else if (fssize < MEDIUM_FSSIZE)
347
				bsize = M_DFL_BSIZE;
348
			else
349
				bsize = L_DFL_BSIZE;
350
		}
351
	}
352
	if (fsize == 0)
353
		fsize = bsize;
354
355
	blocks = fssize * sectorsize / bsize;
356
357
	if (num_inodes == 0) {
358
		if (density != 0)
359
			num_inodes = fssize / density;
360
		else {
361
			if (fssize < SMALL_FSSIZE)
362
				num_inodes = S_DFL_NINODE(blocks);
363
			else if (fssize < MEDIUM_FSSIZE)
364
				num_inodes = M_DFL_NINODE(blocks);
365
			else
366
				num_inodes = L_DFL_NINODE(blocks);
367
		}
368
	}
369
	mke2fs(special, fd);
370
371
	close(fd);
372
	exit(EXIT_SUCCESS);
373
}
374
375
static int64_t
376
strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max,
377
    int *num_suffix)
378
{
379
	int64_t result, r1;
380
	int shift = 0;
381
	char *ep;
382
383
	errno = 0;
384
	r1 = strtoll(arg, &ep, 10);
385
	if (ep[0] != '\0' && ep[1] != '\0')
386
		errx(EXIT_FAILURE,
387
		    "%s `%s' is not a valid number.", desc, arg);
388
	switch (ep[0]) {
389
	case '\0':
390
	case 's':
391
	case 'S':
392
		if (num_suffix != NULL)
393
			*num_suffix = 0;
394
		break;
395
	case 'g':
396
	case 'G':
397
		shift += 10;
398
		/* FALLTHROUGH */
399
	case 'm':
400
	case 'M':
401
		shift += 10;
402
		/* FALLTHROUGH */
403
	case 'k':
404
	case 'K':
405
		shift += 10;
406
		/* FALLTHROUGH */
407
	case 'b':
408
	case 'B':
409
		if (num_suffix != NULL)
410
			*num_suffix = 1;
411
		break;
412
	default:
413
		errx(EXIT_FAILURE,
414
		    "`%s' is not a valid suffix for %s.", ep, desc);
415
	}
416
	result = r1 << shift;
417
	if (errno == ERANGE || result >> shift != r1)
418
		errx(EXIT_FAILURE,
419
		    "%s `%s' is too large to convert.", desc, arg);
420
	if (result < min)
421
		errx(EXIT_FAILURE,
422
		    "%s `%s' (%" PRId64 ") is less than the minimum (%"
423
		    PRId64 ").", desc, arg, result, min);
424
	if (result > max)
425
		errx(EXIT_FAILURE,
426
		    "%s `%s' (%" PRId64 ") is greater than the maximum (%"
427
		    PRId64 ").", desc, arg, result, max);
428
	return result;
429
}
430
431
static const char help_strings[] =
432
	"\t-b bsize\tblock size\n"
433
	"\t-D inodesize\tsize of an inode in bytes (128 or 256)\n"
434
	"\t-F \t\tcreate file system image in regular file\n"
435
	"\t-f fsize\tfragment size\n"
436
	"\t-I \t\tdo not check that the file system type is `ext2fs'\n"
437
	"\t-i density\tnumber of bytes per inode\n"
438
	"\t-m minfree\tminimum free space %\n"
439
	"\t-N \t\tdo not create file system, just print out parameters\n"
440
	"\t-n inodes\tnumber of inodes (overrides -i density)\n"
441
	"\t-O N\t\tfilesystem revision: 0 ==> REV0, 1 ==> REV1 (default 0)\n"
442
	"\t-S secsize\tsector size\n"
443
	"\t-s fssize\tfile system size (sectors)\n"
444
	"\t-V verbose\toutput verbosity: 0 ==> none, 4 ==> max\n"
445
	"\t-v volname\text2fs volume name\n"
446
	"\t-Z \t\tpre-zero the image file\n";
447
448
static void
449
usage(void)
450
{
451
452
	extern char *__progname;
453
454
	fprintf(stderr,
455
	    "usage: %s [ fsoptions ] special-device\n", __progname);
456
	fprintf(stderr, "where fsoptions are:\n");
457
	fprintf(stderr, "%s", help_strings);
458
459
	exit(EXIT_FAILURE);
460
}
461
462
struct disklabel *
463
getdisklabel(const char *s, int fd)
464
{
465
	static struct disklabel lab;
466
467
	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
468
		if (disktype != NULL) {
469
			struct disklabel *lp;
470
471
			//unlabeled++;
472
			lp = getdiskbyname(disktype);
473
			if (lp == NULL)
474
				errx(EXIT_FAILURE, "%s: unknown disk type",
475
				    disktype);
476
			return (lp);
477
		}
478
		warn("ioctl (GDINFO)");
479
		errx(EXIT_FAILURE,
480
		    "%s: can't read disk label; disk type must be specified",
481
		    s);
482
	}
483
	return (&lab);
484
}
485
486
struct partition *
487
getpartition(int fsi, const char *special, char *argv[], struct disklabel **dl)
488
{
489
	struct stat st;
490
	const char *cp;
491
	struct disklabel *lp;
492
	struct partition *pp;
493
494
	if (fstat(fsi, &st) < 0)
495
		err(EXIT_FAILURE, "%s", special);
496
	if (S_ISBLK(st.st_mode))
497
		errx(EXIT_FAILURE, "%s: block device", special);
498
	if (!S_ISCHR(st.st_mode))
499
		warnx("%s: not a character-special device", special);
500
	if (*argv[0] == '\0')
501
		errx(EXIT_FAILURE, "empty partition name supplied");
502
	cp = argv[0] + strlen(argv[0]) - 1;
503
	if ((*cp < 'a' || *cp > ('a' + getmaxpartitions() - 1))
504
	    && !isdigit((unsigned char)*cp))
505
		errx(EXIT_FAILURE, "%s: can't figure out file system partition", argv[0]);
506
	lp = getdisklabel(special, fsi);
507
	if (isdigit((unsigned char)*cp))
508
		pp = &lp->d_partitions[0];
509
	else
510
		pp = &lp->d_partitions[*cp - 'a'];
511
	if (DL_GETPSIZE(pp) == 0)
512
		errx(EXIT_FAILURE, "%s: `%c' partition is unavailable", argv[0], *cp);
513
	if (pp->p_fstype == FS_BOOT)
514
			errx(EXIT_FAILURE, "%s: `%c' partition overlaps boot program",
515
			      argv[0], *cp);
516
	*dl = lp;
517
	return pp;
518
}
519