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