newfs.c revision 1.40 1 /* $NetBSD: newfs.c,v 1.40 2000/12/01 11:52:54 simonb Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1989, 1993, 1994
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) 1983, 1989, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
45 #else
46 __RCSID("$NetBSD: newfs.c,v 1.40 2000/12/01 11:52:54 simonb 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/stat.h>
55 #include <sys/ioctl.h>
56 #include <sys/disklabel.h>
57 #include <sys/file.h>
58 #include <sys/mount.h>
59 #include <sys/sysctl.h>
60 #include <sys/wait.h>
61
62 #include <ufs/ufs/dir.h>
63 #include <ufs/ufs/dinode.h>
64 #include <ufs/ufs/ufsmount.h>
65 #include <ufs/ffs/fs.h>
66
67 #include <ctype.h>
68 #include <disktab.h>
69 #include <errno.h>
70 #include <paths.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <syslog.h>
75 #include <unistd.h>
76 #include <signal.h>
77 #include <err.h>
78 #include <util.h>
79
80 #include "mntopts.h"
81 #include "dkcksum.h"
82 #include "extern.h"
83
84 struct mntopt mopts[] = {
85 MOPT_STDOPTS,
86 MOPT_ASYNC,
87 MOPT_UPDATE,
88 MOPT_NOATIME,
89 { NULL },
90 };
91
92 static struct disklabel *getdisklabel(char *, int);
93 static void rewritelabel(char *, int, struct disklabel *);
94 static void usage(void);
95 int main(int, char *[]);
96
97 #define COMPAT /* allow non-labeled disks */
98
99 /*
100 * The following two constants set the default block and fragment sizes.
101 * Both constants must be a power of 2 and meet the following constraints:
102 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
103 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
104 * DESBLKSIZE / DESFRAGSIZE <= 8
105 */
106 #define DFL_FRAGSIZE 1024
107 #define DFL_BLKSIZE 8192
108
109 /*
110 * Cylinder groups may have up to many cylinders. The actual
111 * number used depends upon how much information can be stored
112 * on a single cylinder. The default is to use 16 cylinders
113 * per group.
114 */
115 #define DESCPG 16 /* desired fs_cpg */
116
117 /*
118 * ROTDELAY gives the minimum number of milliseconds to initiate
119 * another disk transfer on the same cylinder. It is used in
120 * determining the rotationally optimal layout for disk blocks
121 * within a file; the default of fs_rotdelay is 0ms.
122 */
123 #define ROTDELAY 0
124
125 /*
126 * MAXBLKPG determines the maximum number of data blocks which are
127 * placed in a single cylinder group. The default is one indirect
128 * block worth of data blocks.
129 */
130 #define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t))
131
132 /*
133 * Each file system has a number of inodes statically allocated.
134 * We allocate one inode slot per NFPI fragments, expecting this
135 * to be far more than we will ever need.
136 */
137 #define NFPI 4
138
139 /*
140 * For each cylinder we keep track of the availability of blocks at different
141 * rotational positions, so that we can lay out the data to be picked
142 * up with minimum rotational latency. NRPOS is the default number of
143 * rotational positions that we distinguish. With NRPOS of 8 the resolution
144 * of our summary information is 2ms for a typical 3600 rpm drive. Caching
145 * and zoning pretty much defeats rotational optimization, so we now use a
146 * default of 1.
147 */
148 #define NRPOS 1 /* number distinct rotational positions */
149
150
151 int mfs; /* run as the memory based filesystem */
152 int Nflag; /* run without writing file system */
153 int Oflag; /* format as an 4.3BSD file system */
154 int fssize; /* file system size */
155 int ntracks; /* # tracks/cylinder */
156 int nsectors; /* # sectors/track */
157 int nphyssectors; /* # sectors/track including spares */
158 int secpercyl; /* sectors per cylinder */
159 int trackspares = -1; /* spare sectors per track */
160 int cylspares = -1; /* spare sectors per cylinder */
161 int sectorsize; /* bytes/sector */
162 int rpm; /* revolutions/minute of drive */
163 int interleave; /* hardware sector interleave */
164 int trackskew = -1; /* sector 0 skew, per track */
165 int headswitch; /* head switch time, usec */
166 int trackseek; /* track-to-track seek, usec */
167 int fsize = 0; /* fragment size */
168 int bsize = 0; /* block size */
169 int cpg = DESCPG; /* cylinders/cylinder group */
170 int cpgflg; /* cylinders/cylinder group flag was given */
171 int minfree = MINFREE; /* free space threshold */
172 int opt = DEFAULTOPT; /* optimization preference (space or time) */
173 int density; /* number of bytes per inode */
174 int maxcontig = 0; /* max contiguous blocks to allocate */
175 int rotdelay = ROTDELAY; /* rotational delay between blocks */
176 int maxbpg; /* maximum blocks per file in a cyl group */
177 int nrpos = NRPOS; /* # of distinguished rotational positions */
178 int bbsize = BBSIZE; /* boot block size */
179 int sbsize = SBSIZE; /* superblock size */
180 int mntflags = MNT_ASYNC; /* flags to be passed to mount */
181 u_long memleft; /* virtual memory available */
182 caddr_t membase; /* start address of memory based filesystem */
183 int needswap =0; /* Filesystem not in native byte order */
184 #ifdef COMPAT
185 char *disktype;
186 int unlabeled;
187 #endif
188
189 char device[MAXPATHLEN];
190 extern char *__progname;
191
192 int
193 main(int argc, char *argv[])
194 {
195 int ch;
196 struct partition *pp;
197 struct disklabel *lp;
198 struct disklabel mfsfakelabel;
199 struct partition oldpartition;
200 struct stat st;
201 struct statfs *mp;
202 int fsi = 0, fso, len, n, maxpartitions;
203 char *cp = NULL, *s1, *s2, *special, *opstring;
204 #ifdef MFS
205 char mountfromname[100];
206 pid_t pid, res;
207 struct statfs sf;
208 int status;
209 #endif
210
211 if (strstr(__progname, "mfs")) {
212 mfs = 1;
213 Nflag++;
214 }
215
216 maxpartitions = getmaxpartitions();
217 if (maxpartitions > 26)
218 errx(1, "insane maxpartitions value %d", maxpartitions);
219
220 opstring = mfs ?
221 "NT:a:b:c:d:e:f:i:m:o:s:" :
222 "B:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
223 while ((ch = getopt(argc, argv, opstring)) != -1)
224 switch (ch) {
225 case 'B':
226 if (strcmp(optarg, "be") == 0) {
227 #if BYTE_ORDER == LITTLE_ENDIAN
228 needswap = 1;
229 #endif
230 } else if (strcmp(optarg, "le") == 0) {
231 #if BYTE_ORDER == BIG_ENDIAN
232 needswap = 1;
233 #endif
234 } else
235 usage();
236 break;
237 case 'N':
238 Nflag = 1;
239 break;
240 case 'O':
241 Oflag = 1;
242 break;
243 case 'S':
244 if ((sectorsize = atoi(optarg)) <= 0)
245 errx(1, "%s: bad sector size", optarg);
246 break;
247 #ifdef COMPAT
248 case 'T':
249 disktype = optarg;
250 break;
251 #endif
252 case 'a':
253 if ((maxcontig = atoi(optarg)) <= 0)
254 errx(1, "%s: bad maximum contiguous blocks",
255 optarg);
256 break;
257 case 'b':
258 if ((bsize = atoi(optarg)) < MINBSIZE)
259 errx(1, "%s: bad block size", optarg);
260 break;
261 case 'c':
262 if ((cpg = atoi(optarg)) <= 0)
263 errx(1, "%s: bad cylinders/group", optarg);
264 cpgflg++;
265 break;
266 case 'd':
267 if ((rotdelay = atoi(optarg)) < 0)
268 errx(1, "%s: bad rotational delay", optarg);
269 break;
270 case 'e':
271 if ((maxbpg = atoi(optarg)) <= 0)
272 errx(1, "%s: bad blocks per file in a cylinder group",
273 optarg);
274 break;
275 case 'f':
276 if ((fsize = atoi(optarg)) <= 0)
277 errx(1, "%s: bad fragment size", optarg);
278 break;
279 case 'i':
280 if ((density = atoi(optarg)) <= 0)
281 errx(1, "%s: bad bytes per inode", optarg);
282 break;
283 case 'k':
284 if ((trackskew = atoi(optarg)) < 0)
285 errx(1, "%s: bad track skew", optarg);
286 break;
287 case 'l':
288 if ((interleave = atoi(optarg)) <= 0)
289 errx(1, "%s: bad interleave", optarg);
290 break;
291 case 'm':
292 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
293 errx(1, "%s: bad free space %%", optarg);
294 break;
295 case 'n':
296 if ((nrpos = atoi(optarg)) <= 0)
297 errx(1, "%s: bad rotational layout count",
298 optarg);
299 break;
300 case 'o':
301 if (mfs)
302 getmntopts(optarg, mopts, &mntflags, 0);
303 else {
304 if (strcmp(optarg, "space") == 0)
305 opt = FS_OPTSPACE;
306 else if (strcmp(optarg, "time") == 0)
307 opt = FS_OPTTIME;
308 else
309 errx(1, "%s %s",
310 "unknown optimization preference: ",
311 "use `space' or `time'.");
312 }
313 break;
314 case 'p':
315 if ((trackspares = atoi(optarg)) < 0)
316 errx(1, "%s: bad spare sectors per track",
317 optarg);
318 break;
319 case 'r':
320 if ((rpm = atoi(optarg)) <= 0)
321 errx(1, "%s: bad revolutions/minute", optarg);
322 break;
323 case 's':
324 if ((fssize = atoi(optarg)) <= 0)
325 errx(1, "%s: bad file system size", optarg);
326 break;
327 case 't':
328 if ((ntracks = atoi(optarg)) <= 0)
329 errx(1, "%s: bad total tracks", optarg);
330 break;
331 case 'u':
332 if ((nsectors = atoi(optarg)) <= 0)
333 errx(1, "%s: bad sectors/track", optarg);
334 break;
335 case 'x':
336 if ((cylspares = atoi(optarg)) < 0)
337 errx(1, "%s: bad spare sectors per cylinder",
338 optarg);
339 break;
340 case '?':
341 default:
342 usage();
343 }
344 argc -= optind;
345 argv += optind;
346
347 if (argc != 2 && (mfs || argc != 1))
348 usage();
349
350 special = argv[0];
351 if (mfs && !strcmp(special, "swap")) {
352 /*
353 * it's an MFS, mounted on "swap." fake up a label.
354 * XXX XXX XXX
355 */
356 fso = -1; /* XXX; normally done below. */
357
358 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
359 mfsfakelabel.d_secsize = 512;
360 mfsfakelabel.d_nsectors = 64;
361 mfsfakelabel.d_ntracks = 16;
362 mfsfakelabel.d_ncylinders = 16;
363 mfsfakelabel.d_secpercyl = 1024;
364 mfsfakelabel.d_secperunit = 16384;
365 mfsfakelabel.d_rpm = 3600;
366 mfsfakelabel.d_interleave = 1;
367 mfsfakelabel.d_npartitions = 1;
368 mfsfakelabel.d_partitions[0].p_size = 16384;
369 mfsfakelabel.d_partitions[0].p_fsize = 1024;
370 mfsfakelabel.d_partitions[0].p_frag = 8;
371 mfsfakelabel.d_partitions[0].p_cpg = 16;
372
373 lp = &mfsfakelabel;
374 pp = &mfsfakelabel.d_partitions[0];
375
376 goto havelabel;
377 }
378 cp = strrchr(special, '/');
379 if (cp == 0) {
380 /*
381 * No path prefix; try /dev/r%s then /dev/%s.
382 */
383 (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
384 special);
385 if (stat(device, &st) == -1)
386 (void)snprintf(device, sizeof(device), "%s%s",
387 _PATH_DEV, special);
388 special = device;
389 }
390 if (Nflag) {
391 fso = -1;
392 } else {
393 fso = open(special, O_WRONLY);
394 if (fso < 0)
395 err(1, "%s: open", special);
396
397 /* Bail if target special is mounted */
398 n = getmntinfo(&mp, MNT_NOWAIT);
399 if (n == 0)
400 err(1, "%s: getmntinfo", special);
401
402 len = sizeof(_PATH_DEV) - 1;
403 s1 = special;
404 if (strncmp(_PATH_DEV, s1, len) == 0)
405 s1 += len;
406
407 while (--n >= 0) {
408 s2 = mp->f_mntfromname;
409 if (strncmp(_PATH_DEV, s2, len) == 0) {
410 s2 += len - 1;
411 *s2 = 'r';
412 }
413 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
414 errx(1, "%s is mounted on %s",
415 special, mp->f_mntonname);
416 ++mp;
417 }
418 }
419 if (mfs && disktype != NULL) {
420 lp = (struct disklabel *)getdiskbyname(disktype);
421 if (lp == NULL)
422 errx(1, "%s: unknown disk type", disktype);
423 pp = &lp->d_partitions[1];
424 } else {
425 fsi = open(special, O_RDONLY);
426 if (fsi < 0)
427 err(1, "%s: open", special);
428 if (fstat(fsi, &st) < 0)
429 err(1, "%s: fstat", special);
430 if (!S_ISCHR(st.st_mode) && !mfs)
431 warnx("%s: not a character-special device", special);
432 cp = strchr(argv[0], '\0') - 1;
433 if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
434 && !isdigit(*cp)))
435 errx(1, "can't figure out file system partition");
436 #ifdef COMPAT
437 if (!mfs && disktype == NULL)
438 disktype = argv[1];
439 #endif
440 lp = getdisklabel(special, fsi);
441 if (isdigit(*cp))
442 pp = &lp->d_partitions[0];
443 else
444 pp = &lp->d_partitions[*cp - 'a'];
445 if (pp->p_size == 0)
446 errx(1, "`%c' partition is unavailable", *cp);
447 if (pp->p_fstype == FS_BOOT)
448 errx(1, "`%c' partition overlaps boot program", *cp);
449 }
450 havelabel:
451 if (fssize == 0)
452 fssize = pp->p_size;
453 if (fssize > pp->p_size && !mfs)
454 errx(1, "maximum file system size on the `%c' partition is %d",
455 *cp, pp->p_size);
456 if (rpm == 0) {
457 rpm = lp->d_rpm;
458 if (rpm <= 0)
459 rpm = 3600;
460 }
461 if (ntracks == 0) {
462 ntracks = lp->d_ntracks;
463 if (ntracks <= 0)
464 errx(1, "no default #tracks");
465 }
466 if (nsectors == 0) {
467 nsectors = lp->d_nsectors;
468 if (nsectors <= 0)
469 errx(1, "no default #sectors/track");
470 }
471 if (sectorsize == 0) {
472 sectorsize = lp->d_secsize;
473 if (sectorsize <= 0)
474 errx(1, "no default sector size");
475 }
476 if (trackskew == -1) {
477 trackskew = lp->d_trackskew;
478 if (trackskew < 0)
479 trackskew = 0;
480 }
481 if (interleave == 0) {
482 interleave = lp->d_interleave;
483 if (interleave <= 0)
484 interleave = 1;
485 }
486 if (fsize == 0) {
487 fsize = pp->p_fsize;
488 if (fsize <= 0)
489 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
490 }
491 if (bsize == 0) {
492 bsize = pp->p_frag * pp->p_fsize;
493 if (bsize <= 0)
494 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
495 }
496 if (cpgflg == 0) {
497 if (pp->p_cpg != 0)
498 cpg = pp->p_cpg;
499 }
500 /*
501 * Maxcontig sets the default for the maximum number of blocks
502 * that may be allocated sequentially. With filesystem clustering
503 * it is possible to allocate contiguous blocks up to the maximum
504 * transfer size permitted by the controller or buffering.
505 */
506 if (maxcontig == 0)
507 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
508 if (density == 0)
509 density = NFPI * fsize;
510 if (minfree < MINFREE && opt != FS_OPTSPACE) {
511 warnx("%s %s %d%%", "Warning: changing optimization to space",
512 "because minfree is less than", MINFREE);
513 opt = FS_OPTSPACE;
514 }
515 if (trackspares == -1) {
516 trackspares = lp->d_sparespertrack;
517 if (trackspares < 0)
518 trackspares = 0;
519 }
520 nphyssectors = nsectors + trackspares;
521 if (cylspares == -1) {
522 cylspares = lp->d_sparespercyl;
523 if (cylspares < 0)
524 cylspares = 0;
525 }
526 secpercyl = nsectors * ntracks - cylspares;
527 if (secpercyl != lp->d_secpercyl)
528 warnx("%s (%d) %s (%u)\n",
529 "Warning: calculated sectors per cylinder", secpercyl,
530 "disagrees with disk label", lp->d_secpercyl);
531 if (maxbpg == 0)
532 maxbpg = MAXBLKPG(bsize);
533 headswitch = lp->d_headswitch;
534 trackseek = lp->d_trkseek;
535 #ifdef notdef /* label may be 0 if faked up by kernel */
536 bbsize = lp->d_bbsize;
537 sbsize = lp->d_sbsize;
538 #endif
539 oldpartition = *pp;
540 mkfs(pp, special, fsi, fso);
541 if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
542 rewritelabel(special, fso, lp);
543 if (!Nflag)
544 close(fso);
545 close(fsi);
546 #ifdef MFS
547 if (mfs) {
548 struct mfs_args args;
549
550 switch (pid = fork()) {
551 case -1:
552 perror("mfs");
553 exit(10);
554 case 0:
555 (void)snprintf(mountfromname, sizeof(mountfromname),
556 "mfs:%d", getpid());
557 break;
558 default:
559 (void)snprintf(mountfromname, sizeof(mountfromname),
560 "mfs:%d", pid);
561 for (;;) {
562 /*
563 * spin until the mount succeeds
564 * or the child exits
565 */
566 usleep(1);
567
568 /*
569 * XXX Here is a race condition: another process
570 * can mount a filesystem which hides our
571 * ramdisk before we see the success.
572 */
573 if (statfs(argv[1], &sf) < 0)
574 err(88, "statfs %s", argv[1]);
575 if (!strcmp(sf.f_mntfromname, mountfromname) &&
576 !strncmp(sf.f_mntonname, argv[1],
577 MNAMELEN) &&
578 !strcmp(sf.f_fstypename, "mfs"))
579 exit(0);
580
581 res = waitpid(pid, &status, WNOHANG);
582 if (res == -1)
583 err(11, "waitpid");
584 if (res != pid)
585 continue;
586 if (WIFEXITED(status)) {
587 if (WEXITSTATUS(status) == 0)
588 exit(0);
589 errx(1, "%s: mount: %s", argv[1],
590 strerror(WEXITSTATUS(status)));
591 } else
592 errx(11, "abnormal termination");
593 }
594 /* NOTREACHED */
595 }
596
597 (void) setsid();
598 (void) close(0);
599 (void) close(1);
600 (void) close(2);
601 (void) chdir("/");
602
603 args.fspec = mountfromname;
604 args.export.ex_root = -2;
605 if (mntflags & MNT_RDONLY)
606 args.export.ex_flags = MNT_EXRDONLY;
607 else
608 args.export.ex_flags = 0;
609 args.base = membase;
610 args.size = fssize * sectorsize;
611 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
612 exit(errno); /* parent prints message */
613 }
614 #endif
615 exit(0);
616 }
617
618 #ifdef COMPAT
619 const char lmsg[] = "%s: can't read disk label; disk type must be specified";
620 #else
621 const char lmsg[] = "%s: can't read disk label";
622 #endif
623
624 static struct disklabel *
625 getdisklabel(char *s, volatile int fd)
626 /* XXX why is fs volatile?! */
627 {
628 static struct disklabel lab;
629
630 if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
631 #ifdef COMPAT
632 if (disktype) {
633 struct disklabel *lp;
634
635 unlabeled++;
636 lp = getdiskbyname(disktype);
637 if (lp == NULL)
638 errx(1, "%s: unknown disk type", disktype);
639 return (lp);
640 }
641 #endif
642 warn("ioctl (GDINFO)");
643 errx(1, lmsg, s);
644 }
645 return (&lab);
646 }
647
648 static void
649 rewritelabel(char *s, volatile int fd, struct disklabel *lp)
650 /* XXX why is fd volatile?! */
651 {
652 #ifdef COMPAT
653 if (unlabeled)
654 return;
655 #endif
656 lp->d_checksum = 0;
657 lp->d_checksum = dkcksum(lp);
658 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
659 warn("ioctl (WDINFO)");
660 errx(1, "%s: can't rewrite disk label", s);
661 }
662 #if __vax__
663 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
664 int i;
665 int cfd;
666 daddr_t alt;
667 char specname[64];
668 char blk[1024];
669 char *cp;
670
671 /*
672 * Make name for 'c' partition.
673 */
674 strcpy(specname, s);
675 cp = specname + strlen(specname) - 1;
676 if (!isdigit(*cp))
677 *cp = 'c';
678 cfd = open(specname, O_WRONLY);
679 if (cfd < 0)
680 err(1, "%s: open", specname);
681 memset(blk, 0, sizeof(blk));
682 *(struct disklabel *)(blk + LABELOFFSET) = *lp;
683 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
684 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
685 off_t offset;
686
687 offset = alt + i;
688 offset *= lp->d_secsize;
689 if (lseek(cfd, offset, SEEK_SET) == -1)
690 err(1, "lseek to badsector area: ");
691 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
692 warn("alternate label %d write", i/2);
693 }
694 close(cfd);
695 }
696 #endif
697 }
698
699 static void
700 usage(void)
701 {
702 if (mfs) {
703 fprintf(stderr,
704 "usage: %s [ -fsoptions ] special-device mount-point\n",
705 __progname);
706 } else
707 fprintf(stderr,
708 "usage: %s [ -fsoptions ] special-device%s\n",
709 __progname,
710 #ifdef COMPAT
711 " [device-type]");
712 #else
713 "");
714 #endif
715 fprintf(stderr, "where fsoptions are:\n");
716 fprintf(stderr, "\t-B byte order (`be' or `le')\n");
717 fprintf(stderr,
718 "\t-N do not create file system, just print out parameters\n");
719 fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
720 fprintf(stderr, "\t-S sector size\n");
721 #ifdef COMPAT
722 fprintf(stderr, "\t-T disktype\n");
723 #endif
724 fprintf(stderr, "\t-a maximum contiguous blocks\n");
725 fprintf(stderr, "\t-b block size\n");
726 fprintf(stderr, "\t-c cylinders/group\n");
727 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
728 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
729 fprintf(stderr, "\t-f frag size\n");
730 fprintf(stderr, "\t-i number of bytes per inode\n");
731 fprintf(stderr, "\t-k sector 0 skew, per track\n");
732 fprintf(stderr, "\t-l hardware sector interleave\n");
733 fprintf(stderr, "\t-m minimum free space %%\n");
734 fprintf(stderr, "\t-n number of distinguished rotational positions\n");
735 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
736 fprintf(stderr, "\t-p spare sectors per track\n");
737 fprintf(stderr, "\t-s file system size (sectors)\n");
738 fprintf(stderr, "\t-r revolutions/minute\n");
739 fprintf(stderr, "\t-t tracks/cylinder\n");
740 fprintf(stderr, "\t-u sectors/track\n");
741 fprintf(stderr, "\t-x spare sectors per cylinder\n");
742 exit(1);
743 }
744