xref: /freebsd/sbin/newfs/newfs.c (revision 9fd69f37d28cfd7438cac3eeb45fe9dd46b4d7dd)
1 /*
2  * Copyright (c) 2002 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program.
10  *
11  * Copyright (c) 1983, 1989, 1993, 1994
12  *	The Regents of the University of California.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #if 0
40 #ifndef lint
41 static const char copyright[] =
42 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
43 	The Regents of the University of California.  All rights reserved.\n";
44 #endif /* not lint */
45 
46 #ifndef lint
47 static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
48 #endif /* not lint */
49 #endif
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52 
53 /*
54  * newfs: friendly front end to mkfs
55  */
56 #include <sys/param.h>
57 #include <sys/stat.h>
58 #include <sys/disk.h>
59 #include <sys/disklabel.h>
60 #include <sys/file.h>
61 #include <sys/mount.h>
62 
63 #include <ufs/ufs/dir.h>
64 #include <ufs/ufs/dinode.h>
65 #include <ufs/ffs/fs.h>
66 #include <ufs/ufs/ufsmount.h>
67 
68 #include <ctype.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <inttypes.h>
72 #include <paths.h>
73 #include <stdarg.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <syslog.h>
78 #include <unistd.h>
79 
80 #include "newfs.h"
81 
82 int	Eflag;			/* Erase previous disk contents */
83 int	Lflag;			/* add a volume label */
84 int	Nflag;			/* run without writing file system */
85 int	Oflag = 2;		/* file system format (1 => UFS1, 2 => UFS2) */
86 int	Rflag;			/* regression test */
87 int	Uflag;			/* enable soft updates for file system */
88 int	Xflag = 0;		/* exit in middle of newfs for testing */
89 int	Jflag;			/* enable gjournal for file system */
90 int	lflag;			/* enable multilabel for file system */
91 int	nflag;			/* do not create .snap directory */
92 intmax_t fssize;		/* file system size */
93 int	sectorsize;		/* bytes/sector */
94 int	realsectorsize;		/* bytes/sector in hardware */
95 int	fsize = 0;		/* fragment size */
96 int	bsize = 0;		/* block size */
97 int	maxbsize = 0;		/* maximum clustering */
98 int	maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
99 int	minfree = MINFREE;	/* free space threshold */
100 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
101 int	density;		/* number of bytes per inode */
102 int	maxcontig = 0;		/* max contiguous blocks to allocate */
103 int	maxbpg;			/* maximum blocks per file in a cyl group */
104 int	avgfilesize = AVFILESIZ;/* expected average file size */
105 int	avgfilesperdir = AFPDIR;/* expected number of files per directory */
106 u_char	*volumelabel = NULL;	/* volume label for filesystem */
107 struct uufsd disk;		/* libufs disk structure */
108 
109 static char	device[MAXPATHLEN];
110 static u_char   bootarea[BBSIZE];
111 static int	is_file;		/* work on a file, not a device */
112 static char	*dkname;
113 static char	*disktype;
114 static int	unlabeled;
115 
116 static void getfssize(intmax_t *, const char *p, intmax_t, intmax_t);
117 static struct disklabel *getdisklabel(char *s);
118 static void rewritelabel(char *s, struct disklabel *lp);
119 static void usage(void);
120 
121 ufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */
122 
123 int
124 main(int argc, char *argv[])
125 {
126 	struct partition *pp;
127 	struct disklabel *lp;
128 	struct partition oldpartition;
129 	struct stat st;
130 	char *cp, *special;
131 	intmax_t reserved;
132 	int ch, i;
133 	off_t mediasize;
134 	char part_name;		/* partition name, default to full disk */
135 
136 	part_name = 'c';
137 	reserved = 0;
138 	while ((ch = getopt(argc, argv,
139 	    "EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:lm:no:r:s:")) != -1)
140 		switch (ch) {
141 		case 'E':
142 			Eflag = 1;
143 			break;
144 		case 'J':
145 			Jflag = 1;
146 			break;
147 		case 'L':
148 			volumelabel = optarg;
149 			i = -1;
150 			while (isalnum(volumelabel[++i]));
151 			if (volumelabel[i] != '\0') {
152 				errx(1, "bad volume label. Valid characters are alphanumerics.");
153 			}
154 			if (strlen(volumelabel) >= MAXVOLLEN) {
155 				errx(1, "bad volume label. Length is longer than %d.",
156 				    MAXVOLLEN);
157 			}
158 			Lflag = 1;
159 			break;
160 		case 'N':
161 			Nflag = 1;
162 			break;
163 		case 'O':
164 			if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
165 				errx(1, "%s: bad file system format value",
166 				    optarg);
167 			break;
168 		case 'R':
169 			Rflag = 1;
170 			break;
171 		case 'S':
172 			if ((sectorsize = atoi(optarg)) <= 0)
173 				errx(1, "%s: bad sector size", optarg);
174 			break;
175 		case 'T':
176 			disktype = optarg;
177 			break;
178 		case 'U':
179 			Uflag = 1;
180 			break;
181 		case 'X':
182 			Xflag++;
183 			break;
184 		case 'a':
185 			if ((maxcontig = atoi(optarg)) <= 0)
186 				errx(1, "%s: bad maximum contiguous blocks",
187 				    optarg);
188 			break;
189 		case 'b':
190 			if ((bsize = atoi(optarg)) < MINBSIZE)
191 				errx(1, "%s: block size too small, min is %d",
192 				    optarg, MINBSIZE);
193 			if (bsize > MAXBSIZE)
194 				errx(1, "%s: block size too large, max is %d",
195 				    optarg, MAXBSIZE);
196 			break;
197 		case 'c':
198 			if ((maxblkspercg = atoi(optarg)) <= 0)
199 				errx(1, "%s: bad blocks per cylinder group",
200 				    optarg);
201 			break;
202 		case 'd':
203 			if ((maxbsize = atoi(optarg)) < MINBSIZE)
204 				errx(1, "%s: bad extent block size", optarg);
205 			break;
206 		case 'e':
207 			if ((maxbpg = atoi(optarg)) <= 0)
208 			  errx(1, "%s: bad blocks per file in a cylinder group",
209 				    optarg);
210 			break;
211 		case 'f':
212 			if ((fsize = atoi(optarg)) <= 0)
213 				errx(1, "%s: bad fragment size", optarg);
214 			break;
215 		case 'g':
216 			if ((avgfilesize = atoi(optarg)) <= 0)
217 				errx(1, "%s: bad average file size", optarg);
218 			break;
219 		case 'h':
220 			if ((avgfilesperdir = atoi(optarg)) <= 0)
221 			       errx(1, "%s: bad average files per dir", optarg);
222 			break;
223 		case 'i':
224 			if ((density = atoi(optarg)) <= 0)
225 				errx(1, "%s: bad bytes per inode", optarg);
226 			break;
227 		case 'l':
228 			lflag = 1;
229 			break;
230 		case 'm':
231 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
232 				errx(1, "%s: bad free space %%", optarg);
233 			break;
234 		case 'n':
235 			nflag = 1;
236 			break;
237 		case 'o':
238 			if (strcmp(optarg, "space") == 0)
239 				opt = FS_OPTSPACE;
240 			else if (strcmp(optarg, "time") == 0)
241 				opt = FS_OPTTIME;
242 			else
243 				errx(1,
244 		"%s: unknown optimization preference: use `space' or `time'",
245 				    optarg);
246 			break;
247 		case 'r':
248 			errno = 0;
249 			reserved = strtoimax(optarg, &cp, 0);
250 			if (errno != 0 || cp == optarg ||
251 			    *cp != '\0' || reserved < 0)
252 				errx(1, "%s: bad reserved size", optarg);
253 			break;
254 		case 'p':
255 			is_file = 1;
256 			part_name = optarg[0];
257 			break;
258 
259 		case 's':
260 			errno = 0;
261 			fssize = strtoimax(optarg, &cp, 0);
262 			if (errno != 0 || cp == optarg ||
263 			    *cp != '\0' || fssize < 0)
264 				errx(1, "%s: bad file system size", optarg);
265 			break;
266 		case '?':
267 		default:
268 			usage();
269 		}
270 	argc -= optind;
271 	argv += optind;
272 
273 	if (argc != 1)
274 		usage();
275 
276 	special = argv[0];
277 	if (!special[0])
278 		err(1, "empty file/special name");
279 	cp = strrchr(special, '/');
280 	if (cp == 0) {
281 		/*
282 		 * No path prefix; try prefixing _PATH_DEV.
283 		 */
284 		snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
285 		special = device;
286 	}
287 
288 	if (is_file) {
289 		/* bypass ufs_disk_fillout_blank */
290 		bzero( &disk, sizeof(disk));
291 		disk.d_bsize = 1;
292 		disk.d_name = special;
293 		disk.d_fd = open(special, O_RDONLY);
294 		if (disk.d_fd < 0 ||
295 		    (!Nflag && ufs_disk_write(&disk) == -1))
296 			errx(1, "%s: ", special);
297 	} else if (ufs_disk_fillout_blank(&disk, special) == -1 ||
298 	    (!Nflag && ufs_disk_write(&disk) == -1)) {
299 		if (disk.d_error != NULL)
300 			errx(1, "%s: %s", special, disk.d_error);
301 		else
302 			err(1, "%s", special);
303 	}
304 	if (fstat(disk.d_fd, &st) < 0)
305 		err(1, "%s", special);
306 	if ((st.st_mode & S_IFMT) != S_IFCHR) {
307 		warn("%s: not a character-special device", special);
308 		is_file = 1;	/* assume it is a file */
309 		dkname = special;
310 		if (sectorsize == 0)
311 			sectorsize = 512;
312 		mediasize = st.st_size;
313 		/* set fssize from the partition */
314 	} else {
315 	    if (sectorsize == 0)
316 		if (ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize) == -1)
317 		    sectorsize = 0;	/* back out on error for safety */
318 	    if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1)
319 		getfssize(&fssize, special, mediasize / sectorsize, reserved);
320 	}
321 	pp = NULL;
322 	lp = getdisklabel(special);
323 	if (lp != NULL) {
324 		if (!is_file) /* already set for files */
325 			part_name = special[strlen(special) - 1];
326 		if ((part_name < 'a' || part_name - 'a' >= MAXPARTITIONS) &&
327 				!isdigit(part_name))
328 			errx(1, "%s: can't figure out file system partition",
329 					special);
330 		cp = &part_name;
331 		if (isdigit(*cp))
332 			pp = &lp->d_partitions[RAW_PART];
333 		else
334 			pp = &lp->d_partitions[*cp - 'a'];
335 		oldpartition = *pp;
336 		if (pp->p_size == 0)
337 			errx(1, "%s: `%c' partition is unavailable",
338 			    special, *cp);
339 		if (pp->p_fstype == FS_BOOT)
340 			errx(1, "%s: `%c' partition overlaps boot program",
341 			    special, *cp);
342 		getfssize(&fssize, special, pp->p_size, reserved);
343 		if (sectorsize == 0)
344 			sectorsize = lp->d_secsize;
345 		if (fsize == 0)
346 			fsize = pp->p_fsize;
347 		if (bsize == 0)
348 			bsize = pp->p_frag * pp->p_fsize;
349 		if (is_file)
350 			part_ofs = pp->p_offset;
351 	}
352 	if (sectorsize <= 0)
353 		errx(1, "%s: no default sector size", special);
354 	if (fsize <= 0)
355 		fsize = MAX(DFL_FRAGSIZE, sectorsize);
356 	if (bsize <= 0)
357 		bsize = MIN(DFL_BLKSIZE, 8 * fsize);
358 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
359 		fprintf(stderr, "Warning: changing optimization to space ");
360 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
361 		opt = FS_OPTSPACE;
362 	}
363 	realsectorsize = sectorsize;
364 	if (sectorsize != DEV_BSIZE) {		/* XXX */
365 		int secperblk = sectorsize / DEV_BSIZE;
366 
367 		sectorsize = DEV_BSIZE;
368 		fssize *= secperblk;
369 		if (pp != NULL)
370 			pp->p_size *= secperblk;
371 	}
372 	mkfs(pp, special);
373 	if (!unlabeled) {
374 		if (realsectorsize != DEV_BSIZE)
375 			pp->p_size /= realsectorsize / DEV_BSIZE;
376 		if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
377 			rewritelabel(special, lp);
378 	}
379 	ufs_disk_close(&disk);
380 	exit(0);
381 }
382 
383 void
384 getfssize(intmax_t *fsz, const char *s, intmax_t disksize, intmax_t reserved)
385 {
386 	intmax_t available;
387 
388 	available = disksize - reserved;
389 	if (available <= 0)
390 		errx(1, "%s: reserved not less than device size %jd",
391 		    s, disksize);
392 	if (*fsz == 0)
393 		*fsz = available;
394 	else if (*fsz > available)
395 		errx(1, "%s: maximum file system size is %jd",
396 		    s, available);
397 }
398 
399 struct disklabel *
400 getdisklabel(char *s)
401 {
402 	static struct disklabel lab;
403 	struct disklabel *lp;
404 
405 	if (is_file) {
406 		if (read(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
407 			err(4, "cannot read bootarea");
408 		if (bsd_disklabel_le_dec(
409 		    bootarea + (0 /* labeloffset */ +
410 				1 /* labelsoffset */ * sectorsize),
411 		    &lab, MAXPARTITIONS))
412 			errx(1, "no valid label found");
413 
414 		lp = &lab;
415 		return &lab;
416 	}
417 
418 	if (ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab) != -1)
419 		return (&lab);
420 	unlabeled++;
421 	if (disktype) {
422 		lp = getdiskbyname(disktype);
423 		if (lp != NULL)
424 			return (lp);
425 	}
426 	return (NULL);
427 }
428 
429 void
430 rewritelabel(char *s, struct disklabel *lp)
431 {
432 	if (unlabeled)
433 		return;
434 	lp->d_checksum = 0;
435 	lp->d_checksum = dkcksum(lp);
436 	if (is_file) {
437 		bsd_disklabel_le_enc(bootarea + 0 /* labeloffset */ +
438 			1 /* labelsoffset */ * sectorsize, lp);
439 		lseek(disk.d_fd, 0, SEEK_SET);
440 		if (write(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
441 			errx(1, "cannot write label");
442 		return;
443 	}
444 	if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) == -1)
445 		warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
446 }
447 
448 static void
449 usage()
450 {
451 	fprintf(stderr,
452 	    "usage: %s [ -fsoptions ] special-device%s\n",
453 	    getprogname(),
454 	    " [device-type]");
455 	fprintf(stderr, "where fsoptions are:\n");
456 	fprintf(stderr, "\t-E Erase previous disk content\n");
457 	fprintf(stderr, "\t-J Enable journaling via gjournal\n");
458 	fprintf(stderr, "\t-L volume label to add to superblock\n");
459 	fprintf(stderr,
460 	    "\t-N do not create file system, just print out parameters\n");
461 	fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
462 	fprintf(stderr, "\t-R regression test, suppress random factors\n");
463 	fprintf(stderr, "\t-S sector size\n");
464 	fprintf(stderr, "\t-T disktype\n");
465 	fprintf(stderr, "\t-U enable soft updates\n");
466 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
467 	fprintf(stderr, "\t-b block size\n");
468 	fprintf(stderr, "\t-c blocks per cylinders group\n");
469 	fprintf(stderr, "\t-d maximum extent size\n");
470 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
471 	fprintf(stderr, "\t-f frag size\n");
472 	fprintf(stderr, "\t-g average file size\n");
473 	fprintf(stderr, "\t-h average files per directory\n");
474 	fprintf(stderr, "\t-i number of bytes per inode\n");
475 	fprintf(stderr, "\t-l enable multilabel MAC\n");
476 	fprintf(stderr, "\t-n do not create .snap directory\n");
477 	fprintf(stderr, "\t-m minimum free space %%\n");
478 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
479 	fprintf(stderr, "\t-p partition name (a..h)\n");
480 	fprintf(stderr, "\t-r reserved sectors at the end of device\n");
481 	fprintf(stderr, "\t-s file system size (sectors)\n");
482 	exit(1);
483 }
484