checkpoint of hacking for mail.cs.berkeley.edu
[unix-history] / usr / src / sbin / newfs / newfs.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1983, 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8#ifndef lint
9static char sccsid[] = "@(#)newfs.c 6.29 (Berkeley) %G%";
10#endif /* not lint */
11
12#ifndef lint
13char copyright[] =
14"@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
15 All rights reserved.\n";
16#endif /* not lint */
17
18/*
19 * newfs: friendly front end to mkfs
20 */
21#include <sys/param.h>
22#include <sys/stat.h>
23#include <sys/ioctl.h>
24#include <sys/disklabel.h>
25#include <sys/file.h>
26#include <sys/mount.h>
27#include <ufs/ufs/dir.h>
28#include <ufs/ffs/fs.h>
29
30#include <errno.h>
31#if __STDC__
32#include <stdarg.h>
33#else
34#include <varargs.h>
35#endif
36#include <stdio.h>
37#include <ctype.h>
38#include <string.h>
39#include <stdlib.h>
40#include <paths.h>
41
42#if __STDC__
43void fatal(const char *fmt, ...);
44#else
45void fatal();
46#endif
47
48#define COMPAT /* allow non-labeled disks */
49
50/*
51 * The following two constants set the default block and fragment sizes.
52 * Both constants must be a power of 2 and meet the following constraints:
53 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
54 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
55 * DESBLKSIZE / DESFRAGSIZE <= 8
56 */
57#define DFL_FRAGSIZE 1024
58#define DFL_BLKSIZE 8192
59
60/*
61 * Cylinder groups may have up to many cylinders. The actual
62 * number used depends upon how much information can be stored
63 * on a single cylinder. The default is to use 16 cylinders
64 * per group.
65 */
66#define DESCPG 16 /* desired fs_cpg */
67
68/*
69 * MINFREE gives the minimum acceptable percentage of file system
70 * blocks which may be free. If the freelist drops below this level
71 * only the superuser may continue to allocate blocks. This may
72 * be set to 0 if no reserve of free blocks is deemed necessary,
73 * however throughput drops by fifty percent if the file system
74 * is run at between 90% and 100% full; thus the default value of
75 * fs_minfree is 10%. With 10% free space, fragmentation is not a
76 * problem, so we choose to optimize for time.
77 */
78#define MINFREE 10
79#define DEFAULTOPT FS_OPTTIME
80
81/*
82 * ROTDELAY gives the minimum number of milliseconds to initiate
83 * another disk transfer on the same cylinder. It is used in
84 * determining the rotationally optimal layout for disk blocks
85 * within a file; the default of fs_rotdelay is 4ms.
86 */
87#define ROTDELAY 4
88
89/*
90 * MAXCONTIG sets the default for the maximum number of blocks
91 * that may be allocated sequentially. Since UNIX drivers are
92 * not capable of scheduling multi-block transfers, this defaults
93 * to 1 (ie no contiguous blocks are allocated).
94 */
95#define MAXCONTIG 1
96
97/*
98 * MAXBLKPG determines the maximum number of data blocks which are
99 * placed in a single cylinder group. The default is one indirect
100 * block worth of data blocks.
101 */
102#define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t))
103
104/*
105 * Each file system has a number of inodes statically allocated.
106 * We allocate one inode slot per NFPI fragments, expecting this
107 * to be far more than we will ever need.
108 */
109#define NFPI 4
110
111/*
112 * For each cylinder we keep track of the availability of blocks at different
113 * rotational positions, so that we can lay out the data to be picked
114 * up with minimum rotational latency. NRPOS is the default number of
115 * rotational positions that we distinguish. With NRPOS of 8 the resolution
116 * of our summary information is 2ms for a typical 3600 rpm drive.
117 */
118#define NRPOS 8 /* number distinct rotational positions */
119
120
121int mfs; /* run as the memory based filesystem */
122int Nflag; /* run without writing file system */
123int fssize; /* file system size */
124int ntracks; /* # tracks/cylinder */
125int nsectors; /* # sectors/track */
126int nphyssectors; /* # sectors/track including spares */
127int secpercyl; /* sectors per cylinder */
128int trackspares = -1; /* spare sectors per track */
129int cylspares = -1; /* spare sectors per cylinder */
130int sectorsize; /* bytes/sector */
131#ifdef tahoe
132int realsectorsize; /* bytes/sector in hardware */
133#endif
134int rpm; /* revolutions/minute of drive */
135int interleave; /* hardware sector interleave */
136int trackskew = -1; /* sector 0 skew, per track */
137int headswitch; /* head switch time, usec */
138int trackseek; /* track-to-track seek, usec */
139int fsize = 0; /* fragment size */
140int bsize = 0; /* block size */
141int cpg = DESCPG; /* cylinders/cylinder group */
142int cpgflg; /* cylinders/cylinder group flag was given */
143int minfree = MINFREE; /* free space threshold */
144int opt = DEFAULTOPT; /* optimization preference (space or time) */
145int density; /* number of bytes per inode */
146int maxcontig = MAXCONTIG; /* max contiguous blocks to allocate */
147int rotdelay = ROTDELAY; /* rotational delay between blocks */
148int maxbpg; /* maximum blocks per file in a cyl group */
149int nrpos = NRPOS; /* # of distinguished rotational positions */
150int bbsize = BBSIZE; /* boot block size */
151int sbsize = SBSIZE; /* superblock size */
152int mntflags; /* flags to be passed to mount */
153u_long memleft; /* virtual memory available */
154caddr_t membase; /* start address of memory based filesystem */
155#ifdef COMPAT
156char *disktype;
157int unlabeled;
158#endif
159
160char device[MAXPATHLEN];
161char *progname;
162
163main(argc, argv)
164 int argc;
165 char *argv[];
166{
167 extern char *optarg;
168 extern int optind;
169 register int ch;
170 register struct partition *pp;
171 register struct disklabel *lp;
172 struct disklabel *getdisklabel();
173 struct partition oldpartition;
174 struct stat st;
175 int fsi, fso;
176 char *cp, *special, *opstring, buf[BUFSIZ];
177
178 if (progname = rindex(*argv, '/'))
179 ++progname;
180 else
181 progname = *argv;
182
183 if (strstr(progname, "mfs")) {
184 mfs = 1;
185 Nflag++;
186 }
187
188 opstring = "F:NS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
189 if (!mfs)
190 opstring += 2; /* -F is mfs only */
191
192 while ((ch = getopt(argc, argv, opstring)) != EOF)
193 switch(ch) {
194 case 'F':
195 if ((mntflags = atoi(optarg)) == 0)
196 fatal("%s: bad mount flags", optarg);
197 break;
198 case 'N':
199 Nflag++;
200 break;
201 case 'S':
202 if ((sectorsize = atoi(optarg)) <= 0)
203 fatal("%s: bad sector size", optarg);
204 break;
205#ifdef COMPAT
206 case 'T':
207 disktype = optarg;
208 break;
209#endif
210 case 'a':
211 if ((maxcontig = atoi(optarg)) <= 0)
212 fatal("%s: bad max contiguous blocks\n",
213 optarg);
214 break;
215 case 'b':
216 if ((bsize = atoi(optarg)) < MINBSIZE)
217 fatal("%s: bad block size", optarg);
218 break;
219 case 'c':
220 if ((cpg = atoi(optarg)) <= 0)
221 fatal("%s: bad cylinders/group", optarg);
222 cpgflg++;
223 break;
224 case 'd':
225 if ((rotdelay = atoi(optarg)) < 0)
226 fatal("%s: bad rotational delay\n", optarg);
227 break;
228 case 'e':
229 if ((maxbpg = atoi(optarg)) <= 0)
230 fatal("%s: bad blocks per file in a cyl group\n",
231 optarg);
232 break;
233 case 'f':
234 if ((fsize = atoi(optarg)) <= 0)
235 fatal("%s: bad frag size", optarg);
236 break;
237 case 'i':
238 if ((density = atoi(optarg)) <= 0)
239 fatal("%s: bad bytes per inode\n", optarg);
240 break;
241 case 'k':
242 if ((trackskew = atoi(optarg)) < 0)
243 fatal("%s: bad track skew", optarg);
244 break;
245 case 'l':
246 if ((interleave = atoi(optarg)) <= 0)
247 fatal("%s: bad interleave", optarg);
248 break;
249 case 'm':
250 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
251 fatal("%s: bad free space %%\n", optarg);
252 break;
253 case 'n':
254 if ((nrpos = atoi(optarg)) <= 0)
255 fatal("%s: bad rotational layout count\n",
256 optarg);
257 break;
258 case 'o':
259 if (strcmp(optarg, "space") == 0)
260 opt = FS_OPTSPACE;
261 else if (strcmp(optarg, "time") == 0)
262 opt = FS_OPTTIME;
263 else
264 fatal("%s: bad optimization preference %s",
265 optarg, "(options are `space' or `time')");
266 break;
267 case 'p':
268 if ((trackspares = atoi(optarg)) < 0)
269 fatal("%s: bad spare sectors per track",
270 optarg);
271 break;
272 case 'r':
273 if ((rpm = atoi(optarg)) <= 0)
274 fatal("%s: bad revs/minute\n", optarg);
275 break;
276 case 's':
277 if ((fssize = atoi(optarg)) <= 0)
278 fatal("%s: bad file system size", optarg);
279 break;
280 case 't':
281 if ((ntracks = atoi(optarg)) <= 0)
282 fatal("%s: bad total tracks", optarg);
283 break;
284 case 'u':
285 if ((nsectors = atoi(optarg)) <= 0)
286 fatal("%s: bad sectors/track", optarg);
287 break;
288 case 'x':
289 if ((cylspares = atoi(optarg)) < 0)
290 fatal("%s: bad spare sectors per cylinder",
291 optarg);
292 break;
293 case '?':
294 default:
295 usage();
296 }
297 argc -= optind;
298 argv += optind;
299
300 if (argc != 2 && (mfs || argc != 1))
301 usage();
302
303 special = argv[0];
304 cp = rindex(special, '/');
305 if (cp == 0) {
306 /*
307 * No path prefix; try /dev/r%s then /dev/%s.
308 */
309 (void)sprintf(device, "%sr%s", _PATH_DEV, special);
310 if (stat(device, &st) == -1)
311 (void)sprintf(device, "%s%s", _PATH_DEV, special);
312 special = device;
313 }
314 if (!Nflag) {
315 fso = open(special, O_WRONLY);
316 if (fso < 0)
317 fatal("%s: %s", special, strerror(errno));
318 } else
319 fso = -1;
320 fsi = open(special, O_RDONLY);
321 if (fsi < 0)
322 fatal("%s: %s", special, strerror(errno));
323 if (fstat(fsi, &st) < 0)
324 fatal("%s: %s", special, strerror(errno));
325 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
326 printf("%s: %s: not a character-special device\n",
327 progname, special);
328 cp = index(argv[0], '\0') - 1;
329 if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
330 fatal("%s: can't figure out file system partition", argv[0]);
331#ifdef COMPAT
332 if (!mfs && disktype == NULL)
333 disktype = argv[1];
334#endif
335 lp = getdisklabel(special, fsi);
336 if (isdigit(*cp))
337 pp = &lp->d_partitions[0];
338 else
339 pp = &lp->d_partitions[*cp - 'a'];
340 if (pp->p_size == 0)
341 fatal("%s: `%c' partition is unavailable", argv[0], *cp);
342 if (fssize == 0)
343 fssize = pp->p_size;
344 if (fssize > pp->p_size && !mfs)
345 fatal("%s: maximum file system size on the `%c' partition is %d",
346 argv[0], *cp, pp->p_size);
347 if (rpm == 0) {
348 rpm = lp->d_rpm;
349 if (rpm <= 0)
350 rpm = 3600;
351 }
352 if (ntracks == 0) {
353 ntracks = lp->d_ntracks;
354 if (ntracks <= 0)
355 fatal("%s: no default #tracks", argv[0]);
356 }
357 if (nsectors == 0) {
358 nsectors = lp->d_nsectors;
359 if (nsectors <= 0)
360 fatal("%s: no default #sectors/track", argv[0]);
361 }
362 if (sectorsize == 0) {
363 sectorsize = lp->d_secsize;
364 if (sectorsize <= 0)
365 fatal("%s: no default sector size", argv[0]);
366 }
367 if (trackskew == -1) {
368 trackskew = lp->d_trackskew;
369 if (trackskew < 0)
370 trackskew = 0;
371 }
372 if (interleave == 0) {
373 interleave = lp->d_interleave;
374 if (interleave <= 0)
375 interleave = 1;
376 }
377 if (fsize == 0) {
378 fsize = pp->p_fsize;
379 if (fsize <= 0)
380 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
381 }
382 if (bsize == 0) {
383 bsize = pp->p_frag * pp->p_fsize;
384 if (bsize <= 0)
385 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
386 }
387 if (density == 0)
388 density = NFPI * fsize;
389 if (minfree < 10 && opt != FS_OPTSPACE) {
390 fprintf(stderr, "Warning: changing optimization to space ");
391 fprintf(stderr, "because minfree is less than 10%%\n");
392 opt = FS_OPTSPACE;
393 }
394 if (trackspares == -1) {
395 trackspares = lp->d_sparespertrack;
396 if (trackspares < 0)
397 trackspares = 0;
398 }
399 nphyssectors = nsectors + trackspares;
400 if (cylspares == -1) {
401 cylspares = lp->d_sparespercyl;
402 if (cylspares < 0)
403 cylspares = 0;
404 }
405 secpercyl = nsectors * ntracks - cylspares;
406 if (secpercyl != lp->d_secpercyl)
407 fprintf(stderr, "%s (%d) %s (%lu)\n",
408 "Warning: calculated sectors per cylinder", secpercyl,
409 "disagrees with disk label", lp->d_secpercyl);
410 if (maxbpg == 0)
411 maxbpg = MAXBLKPG(bsize);
412 headswitch = lp->d_headswitch;
413 trackseek = lp->d_trkseek;
414 /* Reno fix: label may be 0 if faked up by kernel */
415#ifdef notdef
416#ifdef notdef /* label may be 0 if faked up by kernel */
417 bbsize = lp->d_bbsize;
418 sbsize = lp->d_sbsize;
419#endif
420#endif
421 oldpartition = *pp;
422#ifdef tahoe
423 realsectorsize = sectorsize;
424 if (sectorsize != DEV_BSIZE) { /* XXX */
425 int secperblk = DEV_BSIZE / sectorsize;
426
427 sectorsize = DEV_BSIZE;
428 nsectors /= secperblk;
429 nphyssectors /= secperblk;
430 secpercyl /= secperblk;
431 fssize /= secperblk;
432 pp->p_size /= secperblk;
433 }
434#endif
435 mkfs(pp, special, fsi, fso);
436#ifdef tahoe
437 if (realsectorsize != DEV_BSIZE)
438 pp->p_size *= DEV_BSIZE / realsectorsize;
439#endif
440 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
441 rewritelabel(special, fso, lp);
442 if (!Nflag)
443 close(fso);
444 close(fsi);
445#ifdef MFS
446 if (mfs) {
447 struct mfs_args args;
448
449 sprintf(buf, "mfs:%d", getpid());
450 args.name = buf;
451 args.base = membase;
452 args.size = fssize * sectorsize;
453 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
454 fatal("%s: %s", argv[1], strerror(errno));
455 }
456#endif
457 exit(0);
458}
459
460#ifdef COMPAT
461char lmsg[] = "%s: can't read disk label; disk type must be specified";
462#else
463char lmsg[] = "%s: can't read disk label";
464#endif
465
466struct disklabel *
467getdisklabel(s, fd)
468 char *s;
469 int fd;
470{
471 static struct disklabel lab;
472
473 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
474#ifdef COMPAT
475 if (disktype) {
476 struct disklabel *lp, *getdiskbyname();
477
478 unlabeled++;
479 lp = getdiskbyname(disktype);
480 if (lp == NULL)
481 fatal("%s: unknown disk type", disktype);
482 return (lp);
483 }
484#endif
485 (void)fprintf(stderr,
486 "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
487 fatal(lmsg, s);
488 }
489 return (&lab);
490}
491
492rewritelabel(s, fd, lp)
493 char *s;
494 int fd;
495 register struct disklabel *lp;
496{
497#ifdef COMPAT
498 if (unlabeled)
499 return;
500#endif
501 lp->d_checksum = 0;
502 lp->d_checksum = dkcksum(lp);
503 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
504 (void)fprintf(stderr,
505 "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
506 fatal("%s: can't rewrite disk label", s);
507 }
508#if vax
509 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
510 register i;
511 int cfd;
512 daddr_t alt;
513 char specname[64];
514 char blk[1024];
515 char *cp;
516
517 /*
518 * Make name for 'c' partition.
519 */
520 strcpy(specname, s);
521 cp = specname + strlen(specname) - 1;
522 if (!isdigit(*cp))
523 *cp = 'c';
524 cfd = open(specname, O_WRONLY);
525 if (cfd < 0)
526 fatal("%s: %s", specname, strerror(errno));
527 bzero(blk, sizeof(blk));
528 *(struct disklabel *)(blk + LABELOFFSET) = *lp;
529 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
530 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
531 if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
532 L_SET) == -1)
533 fatal("lseek to badsector area: %s",
534 strerror(errno));
535 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
536 fprintf(stderr,
537 "%s: alternate label %d write: %s\n",
538 progname, i/2, strerror(errno));
539 }
540 close(cfd);
541 }
542#endif
543}
544
545/*VARARGS*/
546void
547#if __STDC__
548fatal(const char *fmt, ...)
549#else
550fatal(fmt, va_alist)
551 char *fmt;
552 va_dcl
553#endif
554{
555 va_list ap;
556
557#if __STDC__
558 va_start(ap, fmt);
559#else
560 va_start(ap);
561#endif
562 fprintf(stderr, "%s: ", progname);
563 (void)vfprintf(stderr, fmt, ap);
564 va_end(ap);
565 putc('\n', stderr);
566 exit(1);
567}
568
569usage()
570{
571 if (mfs) {
572 fprintf(stderr,
573 "usage: mfs [ -fsoptions ] special-device mount-point\n");
574 } else
575 fprintf(stderr,
576 "usage: newfs [ -fsoptions ] special-device%s\n",
577#ifdef COMPAT
578 " [device-type]");
579#else
580 "");
581#endif
582 fprintf(stderr, "where fsoptions are:\n");
583 fprintf(stderr,
584 "\t-N do not create file system, just print out parameters\n");
585 fprintf(stderr, "\t-S sector size\n");
586#ifdef COMPAT
587 fprintf(stderr, "\t-T disktype\n");
588#endif
589 fprintf(stderr, "\t-a maximum contiguous blocks\n");
590 fprintf(stderr, "\t-b block size\n");
591 fprintf(stderr, "\t-c cylinders/group\n");
592 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
593 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
594 fprintf(stderr, "\t-f frag size\n");
595 fprintf(stderr, "\t-i number of bytes per inode\n");
596 fprintf(stderr, "\t-k sector 0 skew, per track\n");
597 fprintf(stderr, "\t-l hardware sector interleave\n");
598 fprintf(stderr, "\t-m minimum free space %%\n");
599 fprintf(stderr, "\t-n number of distinguished rotational positions\n");
600 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
601 fprintf(stderr, "\t-p spare sectors per track\n");
602 fprintf(stderr, "\t-s file system size (sectors)\n");
603 fprintf(stderr, "\t-r revolutions/minute\n");
604 fprintf(stderr, "\t-t tracks/cylinder\n");
605 fprintf(stderr, "\t-u sectors/track\n");
606 fprintf(stderr, "\t-x spare sectors per cylinder\n");
607 exit(1);
608}