Home | History | Annotate | Line # | Download | only in newfs_lfs
newfs.c revision 1.3.4.2
      1  1.3.4.2        tv /*	$NetBSD: newfs.c,v 1.3.4.2 2000/10/16 22:28:37 tv 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.1  perseant  * 3. All advertising materials mentioning features or use of this software
     16      1.1  perseant  *    must display the following acknowledgement:
     17      1.1  perseant  *	This product includes software developed by the University of
     18      1.1  perseant  *	California, Berkeley and its contributors.
     19      1.1  perseant  * 4. Neither the name of the University nor the names of its contributors
     20      1.1  perseant  *    may be used to endorse or promote products derived from this software
     21      1.1  perseant  *    without specific prior written permission.
     22      1.1  perseant  *
     23      1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24      1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25      1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26      1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27      1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28      1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29      1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30      1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31      1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.1  perseant  * SUCH DAMAGE.
     34      1.1  perseant  */
     35      1.1  perseant 
     36      1.1  perseant #include <sys/cdefs.h>
     37      1.1  perseant #ifndef lint
     38      1.1  perseant __COPYRIGHT("@(#) Copyright (c) 1989, 1992, 1993\n\
     39      1.1  perseant 	The Regents of the University of California.  All rights reserved.\n");
     40      1.1  perseant #endif /* not lint */
     41      1.1  perseant 
     42      1.1  perseant #ifndef lint
     43      1.1  perseant #if 0
     44      1.1  perseant static char sccsid[] = "@(#)newfs.c	8.5 (Berkeley) 5/24/95";
     45      1.1  perseant #else
     46  1.3.4.2        tv __RCSID("$NetBSD: newfs.c,v 1.3.4.2 2000/10/16 22:28:37 tv Exp $");
     47      1.1  perseant #endif
     48      1.1  perseant #endif /* not lint */
     49      1.1  perseant 
     50      1.1  perseant /*
     51      1.1  perseant  * newfs: friendly front end to mkfs
     52      1.1  perseant  */
     53      1.1  perseant #include <sys/param.h>
     54      1.1  perseant #include <sys/ucred.h>
     55      1.1  perseant #include <sys/stat.h>
     56      1.1  perseant #include <sys/ioctl.h>
     57      1.1  perseant #include <sys/disklabel.h>
     58      1.1  perseant #include <sys/file.h>
     59      1.1  perseant #include <sys/mount.h>
     60      1.1  perseant #include <sys/sysctl.h>
     61      1.1  perseant 
     62      1.1  perseant #include <ufs/ufs/dir.h>
     63      1.1  perseant #include <ufs/ufs/dinode.h>
     64      1.1  perseant 
     65      1.1  perseant #include <disktab.h>
     66      1.1  perseant #include <errno.h>
     67      1.1  perseant #include <unistd.h>
     68      1.1  perseant #include <stdio.h>
     69      1.1  perseant #include <stdlib.h>
     70      1.1  perseant #include <ctype.h>
     71      1.1  perseant #include <string.h>
     72      1.1  perseant #include <paths.h>
     73      1.1  perseant #include <util.h>
     74      1.1  perseant #include "config.h"
     75      1.1  perseant #include "extern.h"
     76      1.1  perseant 
     77      1.1  perseant #define	COMPAT			/* allow non-labeled disks */
     78      1.1  perseant 
     79      1.1  perseant int	Nflag;			/* run without writing file system */
     80      1.1  perseant int	fssize;			/* file system size */
     81      1.1  perseant int	sectorsize;		/* bytes/sector */
     82      1.1  perseant int	fsize = 0;		/* fragment size */
     83      1.1  perseant int	bsize = 0;		/* block size */
     84      1.1  perseant int	minfree = MINFREE;	/* free space threshold */
     85  1.3.4.1  perseant int     minfreeseg = 0;         /* free segments reserved for the cleaner */
     86      1.1  perseant int	bbsize = BBSIZE;	/* boot block size */
     87      1.1  perseant int	sbsize = SBSIZE;	/* superblock size */
     88      1.1  perseant u_long	memleft;		/* virtual memory available */
     89      1.1  perseant caddr_t	membase;		/* start address of memory based filesystem */
     90      1.1  perseant #ifdef COMPAT
     91      1.1  perseant char	*disktype;
     92      1.1  perseant int	unlabeled;
     93      1.1  perseant #endif
     94      1.1  perseant 
     95      1.1  perseant char	device[MAXPATHLEN];
     96      1.1  perseant char	*progname, *special;
     97      1.1  perseant 
     98      1.1  perseant int main __P((int, char **));
     99      1.1  perseant static struct disklabel *getdisklabel __P((char *, int));
    100      1.1  perseant static struct disklabel *debug_readlabel __P((int));
    101      1.1  perseant #ifdef notdef
    102      1.1  perseant static void rewritelabel __P((char *, int, struct disklabel *));
    103      1.1  perseant #endif
    104      1.1  perseant static void usage __P((void));
    105      1.1  perseant 
    106      1.1  perseant int
    107      1.1  perseant main(argc, argv)
    108      1.1  perseant 	int argc;
    109      1.1  perseant 	char *argv[];
    110      1.1  perseant {
    111      1.1  perseant 	int ch;
    112      1.1  perseant 	struct partition *pp;
    113      1.1  perseant 	struct disklabel *lp;
    114      1.1  perseant 	struct stat st;
    115      1.3  perseant 	int debug, force, lfs, fsi, fso, segsize, maxpartitions;
    116      1.1  perseant 	char *cp, *opstring;
    117      1.1  perseant 
    118      1.1  perseant 	if ((progname = strrchr(*argv, '/')) != NULL)
    119      1.1  perseant 		++progname;
    120      1.1  perseant 	else
    121      1.1  perseant 		progname = *argv;
    122      1.1  perseant 
    123      1.1  perseant 	maxpartitions = getmaxpartitions();
    124      1.1  perseant 	if (maxpartitions > 26)
    125      1.1  perseant 		fatal("insane maxpartitions value %d", maxpartitions);
    126      1.1  perseant 
    127  1.3.4.1  perseant 	opstring = "B:DFLNb:f:M:m:s:";
    128      1.1  perseant 
    129      1.3  perseant 	debug = force = lfs = segsize = 0;
    130      1.1  perseant 	while ((ch = getopt(argc, argv, opstring)) != -1)
    131      1.1  perseant 		switch(ch) {
    132      1.1  perseant 		case 'B':	/* LFS segment size */
    133      1.1  perseant 			if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
    134      1.1  perseant 				fatal("%s: bad segment size", optarg);
    135      1.1  perseant 			break;
    136      1.1  perseant 		case 'D':
    137      1.1  perseant 			debug = 1;
    138      1.1  perseant 			break;
    139      1.3  perseant 		case 'F':
    140      1.3  perseant 			force = 1;
    141      1.3  perseant 			break;
    142      1.1  perseant 		case 'L':	/* Create lfs */
    143      1.1  perseant 			lfs = 1;
    144      1.1  perseant 			break;
    145  1.3.4.1  perseant 		case 'M':
    146  1.3.4.1  perseant 			minfreeseg = atoi(optarg);
    147  1.3.4.1  perseant 			break;
    148      1.1  perseant 		case 'N':
    149      1.1  perseant 			Nflag++;
    150      1.1  perseant 			break;
    151      1.1  perseant #ifdef COMPAT
    152      1.1  perseant 		case 'T':
    153      1.1  perseant 			disktype = optarg;
    154      1.2  perseant 			break;
    155      1.2  perseant #endif
    156      1.1  perseant 		case 'b':	/* used for LFS */
    157      1.1  perseant 			if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
    158      1.1  perseant 				fatal("%s: bad block size", optarg);
    159      1.1  perseant 			break;
    160      1.1  perseant 		case 'f':
    161      1.1  perseant 			if ((fsize = atoi(optarg)) <= 0)
    162      1.1  perseant 				fatal("%s: bad frag size", optarg);
    163      1.1  perseant 			break;
    164      1.2  perseant 		case 'm':
    165      1.1  perseant 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    166      1.1  perseant 				fatal("%s: bad free space %%\n", optarg);
    167      1.1  perseant 			break;
    168      1.2  perseant 		case 's':
    169      1.1  perseant 			if ((fssize = atoi(optarg)) <= 0)
    170      1.1  perseant 				fatal("%s: bad file system size", optarg);
    171      1.1  perseant 			break;
    172      1.1  perseant 		case '?':
    173      1.1  perseant 		default:
    174      1.1  perseant 			usage();
    175      1.1  perseant 		}
    176      1.1  perseant 	argc -= optind;
    177      1.1  perseant 	argv += optind;
    178      1.1  perseant 
    179      1.2  perseant 	if (argc != 2 && argc != 1)
    180      1.1  perseant 		usage();
    181      1.1  perseant 
    182      1.1  perseant 	/*
    183      1.1  perseant 	 * If the -N flag isn't specified, open the output file.  If no path
    184      1.1  perseant 	 * prefix, try /dev/r%s and then /dev/%s.
    185      1.1  perseant 	 */
    186      1.1  perseant 	special = argv[0];
    187      1.1  perseant 	if (strchr(special, '/') == NULL) {
    188      1.1  perseant 		(void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
    189      1.1  perseant 		    special);
    190      1.1  perseant 		if (stat(device, &st) == -1)
    191      1.1  perseant 			(void)snprintf(device, sizeof(device), "%s%s",
    192      1.1  perseant 			    _PATH_DEV, special);
    193      1.1  perseant 		special = device;
    194      1.1  perseant 	}
    195      1.1  perseant 	if (!Nflag) {
    196      1.1  perseant 		fso = open(special,
    197      1.1  perseant 		    (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
    198      1.1  perseant 		if (fso < 0)
    199      1.1  perseant 			fatal("%s: %s", special, strerror(errno));
    200      1.1  perseant 	} else
    201      1.1  perseant 		fso = -1;
    202      1.1  perseant 
    203      1.1  perseant 	/* Open the input file. */
    204      1.1  perseant 	fsi = open(special, O_RDONLY);
    205      1.1  perseant 	if (fsi < 0)
    206      1.1  perseant 		fatal("%s: %s", special, strerror(errno));
    207      1.1  perseant 	if (fstat(fsi, &st) < 0)
    208      1.1  perseant 		fatal("%s: %s", special, strerror(errno));
    209      1.1  perseant 
    210      1.2  perseant 	if (!debug && !S_ISCHR(st.st_mode))
    211      1.1  perseant 		(void)printf("%s: %s: not a character-special device\n",
    212      1.1  perseant 		    progname, special);
    213      1.1  perseant 	cp = strchr(argv[0], '\0') - 1;
    214      1.1  perseant 	if (!debug
    215      1.1  perseant 	    && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    216      1.1  perseant 	    && !isdigit(*cp))))
    217      1.1  perseant 		fatal("%s: can't figure out file system partition", argv[0]);
    218      1.1  perseant 
    219      1.1  perseant #ifdef COMPAT
    220      1.2  perseant 	if (disktype == NULL)
    221      1.1  perseant 		disktype = argv[1];
    222      1.1  perseant #endif
    223      1.1  perseant 	if (debug)
    224      1.1  perseant 		lp = debug_readlabel(fsi);
    225      1.1  perseant 	else
    226      1.1  perseant 		lp = getdisklabel(special, fsi);
    227      1.1  perseant 
    228      1.1  perseant 	if (isdigit(*cp))
    229      1.1  perseant 		pp = &lp->d_partitions[0];
    230      1.1  perseant 	else
    231      1.1  perseant 		pp = &lp->d_partitions[*cp - 'a'];
    232      1.1  perseant 	if (pp->p_size == 0)
    233      1.1  perseant 		fatal("%s: `%c' partition is unavailable", argv[0], *cp);
    234      1.3  perseant 
    235      1.3  perseant 	/* If force, make the partition look like an LFS */
    236      1.3  perseant 	if(force) {
    237      1.3  perseant 		pp->p_fstype = FS_BSDLFS;
    238      1.3  perseant 		/* 0 means to use defaults */
    239      1.3  perseant 		pp->p_fsize  = 0;
    240      1.3  perseant 		pp->p_frag   = 0;
    241      1.3  perseant 		pp->p_sgs    = 0;
    242      1.3  perseant 	}
    243      1.1  perseant 
    244      1.1  perseant 	/* If we're making a LFS, we break out here */
    245  1.3.4.1  perseant 	exit(make_lfs(fso, lp, pp, minfree, bsize, fsize, segsize,
    246  1.3.4.1  perseant 		      minfreeseg));
    247      1.1  perseant }
    248      1.1  perseant 
    249      1.1  perseant #ifdef COMPAT
    250      1.1  perseant char lmsg[] = "%s: can't read disk label; disk type must be specified";
    251      1.1  perseant #else
    252      1.1  perseant char lmsg[] = "%s: can't read disk label";
    253      1.1  perseant #endif
    254      1.1  perseant 
    255      1.1  perseant static struct disklabel *
    256      1.1  perseant getdisklabel(s, fd)
    257      1.1  perseant 	char *s;
    258      1.1  perseant 	int fd;
    259      1.1  perseant {
    260      1.1  perseant 	static struct disklabel lab;
    261      1.1  perseant 
    262      1.1  perseant 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    263      1.1  perseant #ifdef COMPAT
    264      1.1  perseant 		if (disktype) {
    265      1.1  perseant 			struct disklabel *lp;
    266      1.1  perseant 
    267      1.1  perseant 			unlabeled++;
    268      1.1  perseant 			lp = getdiskbyname(disktype);
    269      1.1  perseant 			if (lp == NULL)
    270      1.1  perseant 				fatal("%s: unknown disk type", disktype);
    271      1.1  perseant 			return (lp);
    272      1.1  perseant 		}
    273      1.1  perseant #endif
    274      1.1  perseant 		(void)fprintf(stderr,
    275      1.1  perseant 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
    276      1.1  perseant 		fatal(lmsg, s);
    277      1.1  perseant 	}
    278      1.1  perseant 	return (&lab);
    279      1.1  perseant }
    280      1.1  perseant 
    281      1.1  perseant 
    282      1.1  perseant static struct disklabel *
    283      1.1  perseant debug_readlabel(fd)
    284      1.1  perseant 	int fd;
    285      1.1  perseant {
    286      1.1  perseant 	static struct disklabel lab;
    287      1.1  perseant 	int n;
    288      1.1  perseant 
    289      1.1  perseant 	if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
    290      1.1  perseant 		fatal("unable to read disk label: %s", strerror(errno));
    291      1.1  perseant 	else if (n < sizeof(struct disklabel))
    292  1.3.4.2        tv 		fatal("short read of disklabel: %d of %ld bytes", n,
    293  1.3.4.2        tv 			(u_long) sizeof(struct disklabel));
    294      1.1  perseant 	return(&lab);
    295      1.1  perseant }
    296      1.1  perseant 
    297      1.1  perseant #ifdef notdef
    298      1.1  perseant static void
    299      1.1  perseant rewritelabel(s, fd, lp)
    300      1.1  perseant 	char *s;
    301      1.1  perseant 	int fd;
    302      1.1  perseant 	struct disklabel *lp;
    303      1.1  perseant {
    304      1.1  perseant #ifdef COMPAT
    305      1.1  perseant 	if (unlabeled)
    306      1.1  perseant 		return;
    307      1.1  perseant #endif
    308      1.1  perseant 	lp->d_checksum = 0;
    309      1.1  perseant 	lp->d_checksum = dkcksum(lp);
    310      1.1  perseant 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    311      1.1  perseant 		(void)fprintf(stderr,
    312      1.1  perseant 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
    313      1.1  perseant 		fatal("%s: can't rewrite disk label", s);
    314      1.1  perseant 	}
    315      1.1  perseant #if __vax__
    316      1.1  perseant 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    317      1.1  perseant 		int i;
    318      1.1  perseant 		int cfd;
    319      1.1  perseant 		daddr_t alt;
    320      1.1  perseant 		char specname[64];
    321      1.1  perseant 		char blk[1024];
    322      1.1  perseant 		char *cp;
    323      1.1  perseant 
    324      1.1  perseant 		/*
    325      1.1  perseant 		 * Make name for 'c' partition.
    326      1.1  perseant 		 */
    327      1.1  perseant 		strcpy(specname, s);
    328      1.1  perseant 		cp = specname + strlen(specname) - 1;
    329      1.1  perseant 		if (!isdigit(*cp))
    330      1.1  perseant 			*cp = 'c';
    331      1.1  perseant 		cfd = open(specname, O_WRONLY);
    332      1.1  perseant 		if (cfd < 0)
    333      1.1  perseant 			fatal("%s: %s", specname, strerror(errno));
    334      1.1  perseant 		memset(blk, 0, sizeof(blk));
    335      1.1  perseant 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    336      1.1  perseant 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    337      1.1  perseant 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    338      1.1  perseant 			if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
    339      1.1  perseant 			    SEEK_SET) == -1)
    340      1.1  perseant 				fatal("lseek to badsector area: %s",
    341      1.1  perseant 				    strerror(errno));
    342      1.1  perseant 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    343      1.1  perseant 				fprintf(stderr,
    344      1.1  perseant 				    "%s: alternate label %d write: %s\n",
    345      1.1  perseant 				    progname, i/2, strerror(errno));
    346      1.1  perseant 		}
    347      1.1  perseant 		close(cfd);
    348      1.1  perseant 	}
    349      1.1  perseant #endif /* vax */
    350      1.1  perseant }
    351      1.1  perseant #endif /* notdef */
    352      1.1  perseant 
    353      1.1  perseant void
    354      1.1  perseant usage()
    355      1.1  perseant {
    356      1.2  perseant 	fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
    357      1.1  perseant 	fprintf(stderr, "where fsoptions are:\n");
    358      1.1  perseant 	fprintf(stderr, "\t-B LFS segment size\n");
    359      1.1  perseant 	fprintf(stderr, "\t-D debug\n");
    360      1.2  perseant 	/* fprintf(stderr, "\t-L create LFS file system\n"); */
    361      1.1  perseant 	fprintf(stderr,
    362      1.1  perseant 	    "\t-N do not create file system, just print out parameters\n");
    363      1.1  perseant 	fprintf(stderr, "\t-b block size\n");
    364      1.1  perseant 	fprintf(stderr, "\t-f frag size\n");
    365      1.1  perseant 	fprintf(stderr, "\t-m minimum free space %%\n");
    366      1.1  perseant 	fprintf(stderr, "\t-s file system size (sectors)\n");
    367      1.1  perseant 	exit(1);
    368      1.1  perseant }
    369