newfs.c revision 1.42 1 /* $NetBSD: newfs.c,v 1.42 2001/02/19 22:56:21 cgd 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.42 2001/02/19 22:56:21 cgd 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
191 int
192 main(int argc, char *argv[])
193 {
194 struct partition *pp;
195 struct disklabel *lp;
196 struct disklabel mfsfakelabel;
197 struct partition oldpartition;
198 struct stat st;
199 struct statfs *mp;
200 int ch, fsi = 0, fso, len, maxpartitions, n;
201 char *cp = NULL, *endp, *opstring, *s1, *s2, *special;
202 #ifdef MFS
203 char mountfromname[100];
204 pid_t pid, res;
205 struct statfs sf;
206 int status;
207 #endif
208
209 if (strstr(getprogname(), "mfs")) {
210 mfs = 1;
211 Nflag++;
212 }
213
214 maxpartitions = getmaxpartitions();
215 if (maxpartitions > 26)
216 errx(1, "insane maxpartitions value %d", maxpartitions);
217
218 opstring = mfs ?
219 "NT:a:b:c:d:e:f:i:m:o:s:" :
220 "B:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
221 while ((ch = getopt(argc, argv, opstring)) != -1)
222 switch (ch) {
223 case 'B':
224 if (strcmp(optarg, "be") == 0) {
225 #if BYTE_ORDER == LITTLE_ENDIAN
226 needswap = 1;
227 #endif
228 } else if (strcmp(optarg, "le") == 0) {
229 #if BYTE_ORDER == BIG_ENDIAN
230 needswap = 1;
231 #endif
232 } else
233 usage();
234 break;
235 case 'N':
236 Nflag = 1;
237 break;
238 case 'O':
239 Oflag = 1;
240 break;
241 case 'S':
242 if ((sectorsize = atoi(optarg)) <= 0)
243 errx(1, "%s: bad sector size", optarg);
244 break;
245 #ifdef COMPAT
246 case 'T':
247 disktype = optarg;
248 break;
249 #endif
250 case 'a':
251 if ((maxcontig = atoi(optarg)) <= 0)
252 errx(1, "%s: bad maximum contiguous blocks",
253 optarg);
254 break;
255 case 'b':
256 if ((bsize = atoi(optarg)) < MINBSIZE)
257 errx(1, "%s: bad block size", optarg);
258 break;
259 case 'c':
260 if ((cpg = atoi(optarg)) <= 0)
261 errx(1, "%s: bad cylinders/group", optarg);
262 cpgflg++;
263 break;
264 case 'd':
265 if ((rotdelay = atoi(optarg)) < 0)
266 errx(1, "%s: bad rotational delay", optarg);
267 break;
268 case 'e':
269 if ((maxbpg = atoi(optarg)) <= 0)
270 errx(1, "%s: bad blocks per file in a cylinder group",
271 optarg);
272 break;
273 case 'f':
274 if ((fsize = atoi(optarg)) <= 0)
275 errx(1, "%s: bad fragment size", optarg);
276 break;
277 case 'i':
278 if ((density = atoi(optarg)) <= 0)
279 errx(1, "%s: bad bytes per inode", optarg);
280 break;
281 case 'k':
282 if ((trackskew = atoi(optarg)) < 0)
283 errx(1, "%s: bad track skew", optarg);
284 break;
285 case 'l':
286 if ((interleave = atoi(optarg)) <= 0)
287 errx(1, "%s: bad interleave", optarg);
288 break;
289 case 'm':
290 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
291 errx(1, "%s: bad free space %%", optarg);
292 break;
293 case 'n':
294 if ((nrpos = atoi(optarg)) <= 0)
295 errx(1, "%s: bad rotational layout count",
296 optarg);
297 break;
298 case 'o':
299 if (mfs)
300 getmntopts(optarg, mopts, &mntflags, 0);
301 else {
302 if (strcmp(optarg, "space") == 0)
303 opt = FS_OPTSPACE;
304 else if (strcmp(optarg, "time") == 0)
305 opt = FS_OPTTIME;
306 else
307 errx(1, "%s %s",
308 "unknown optimization preference: ",
309 "use `space' or `time'.");
310 }
311 break;
312 case 'p':
313 if ((trackspares = atoi(optarg)) < 0)
314 errx(1, "%s: bad spare sectors per track",
315 optarg);
316 break;
317 case 'r':
318 if ((rpm = atoi(optarg)) <= 0)
319 errx(1, "%s: bad revolutions/minute", optarg);
320 break;
321 case 's':
322 fssize = (int)strtol(optarg, &endp, 10);
323 if (*endp) {
324 if (mfs) { /* Only do 'm' for mfs */
325 if (*endp == 'm' || *endp == 'M')
326 fssize *= 1024 * 1024 / 512;
327 else
328 fssize = -1; /* error */
329 }
330 else
331 fssize = -1; /* error */
332 }
333 if (fssize <= 0)
334 errx(1, "%s: bad file system size", optarg);
335 break;
336 case 't':
337 if ((ntracks = atoi(optarg)) <= 0)
338 errx(1, "%s: bad total tracks", optarg);
339 break;
340 case 'u':
341 if ((nsectors = atoi(optarg)) <= 0)
342 errx(1, "%s: bad sectors/track", optarg);
343 break;
344 case 'x':
345 if ((cylspares = atoi(optarg)) < 0)
346 errx(1, "%s: bad spare sectors per cylinder",
347 optarg);
348 break;
349 case '?':
350 default:
351 usage();
352 }
353 argc -= optind;
354 argv += optind;
355
356 if (argc != 2 && (mfs || argc != 1))
357 usage();
358
359 special = argv[0];
360 if (mfs && !strcmp(special, "swap")) {
361 /*
362 * it's an MFS, mounted on "swap." fake up a label.
363 * XXX XXX XXX
364 */
365 fso = -1; /* XXX; normally done below. */
366
367 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
368 mfsfakelabel.d_secsize = 512;
369 mfsfakelabel.d_nsectors = 64;
370 mfsfakelabel.d_ntracks = 16;
371 mfsfakelabel.d_ncylinders = 16;
372 mfsfakelabel.d_secpercyl = 1024;
373 mfsfakelabel.d_secperunit = 16384;
374 mfsfakelabel.d_rpm = 3600;
375 mfsfakelabel.d_interleave = 1;
376 mfsfakelabel.d_npartitions = 1;
377 mfsfakelabel.d_partitions[0].p_size = 16384;
378 mfsfakelabel.d_partitions[0].p_fsize = 1024;
379 mfsfakelabel.d_partitions[0].p_frag = 8;
380 mfsfakelabel.d_partitions[0].p_cpg = 16;
381
382 lp = &mfsfakelabel;
383 pp = &mfsfakelabel.d_partitions[0];
384
385 goto havelabel;
386 }
387 cp = strrchr(special, '/');
388 if (cp == 0) {
389 /*
390 * No path prefix; try /dev/r%s then /dev/%s.
391 */
392 (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
393 special);
394 if (stat(device, &st) == -1)
395 (void)snprintf(device, sizeof(device), "%s%s",
396 _PATH_DEV, special);
397 special = device;
398 }
399 if (Nflag) {
400 fso = -1;
401 } else {
402 fso = open(special, O_WRONLY);
403 if (fso < 0)
404 err(1, "%s: open", special);
405
406 /* Bail if target special is mounted */
407 n = getmntinfo(&mp, MNT_NOWAIT);
408 if (n == 0)
409 err(1, "%s: getmntinfo", special);
410
411 len = sizeof(_PATH_DEV) - 1;
412 s1 = special;
413 if (strncmp(_PATH_DEV, s1, len) == 0)
414 s1 += len;
415
416 while (--n >= 0) {
417 s2 = mp->f_mntfromname;
418 if (strncmp(_PATH_DEV, s2, len) == 0) {
419 s2 += len - 1;
420 *s2 = 'r';
421 }
422 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
423 errx(1, "%s is mounted on %s",
424 special, mp->f_mntonname);
425 ++mp;
426 }
427 }
428 if (mfs && disktype != NULL) {
429 lp = (struct disklabel *)getdiskbyname(disktype);
430 if (lp == NULL)
431 errx(1, "%s: unknown disk type", disktype);
432 pp = &lp->d_partitions[1];
433 } else {
434 fsi = open(special, O_RDONLY);
435 if (fsi < 0)
436 err(1, "%s: open", special);
437 if (fstat(fsi, &st) < 0)
438 err(1, "%s: fstat", special);
439 if (!S_ISCHR(st.st_mode) && !mfs)
440 warnx("%s: not a character-special device", special);
441 cp = strchr(argv[0], '\0') - 1;
442 if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
443 && !isdigit(*cp)))
444 errx(1, "can't figure out file system partition");
445 #ifdef COMPAT
446 if (!mfs && disktype == NULL)
447 disktype = argv[1];
448 #endif
449 lp = getdisklabel(special, fsi);
450 if (isdigit(*cp))
451 pp = &lp->d_partitions[0];
452 else
453 pp = &lp->d_partitions[*cp - 'a'];
454 if (pp->p_size == 0)
455 errx(1, "`%c' partition is unavailable", *cp);
456 if (pp->p_fstype == FS_BOOT)
457 errx(1, "`%c' partition overlaps boot program", *cp);
458 }
459 havelabel:
460 if (fssize == 0)
461 fssize = pp->p_size;
462 if (fssize > pp->p_size && !mfs)
463 errx(1, "maximum file system size on the `%c' partition is %d",
464 *cp, pp->p_size);
465 if (rpm == 0) {
466 rpm = lp->d_rpm;
467 if (rpm <= 0)
468 rpm = 3600;
469 }
470 if (ntracks == 0) {
471 ntracks = lp->d_ntracks;
472 if (ntracks <= 0)
473 errx(1, "no default #tracks");
474 }
475 if (nsectors == 0) {
476 nsectors = lp->d_nsectors;
477 if (nsectors <= 0)
478 errx(1, "no default #sectors/track");
479 }
480 if (sectorsize == 0) {
481 sectorsize = lp->d_secsize;
482 if (sectorsize <= 0)
483 errx(1, "no default sector size");
484 }
485 if (trackskew == -1) {
486 trackskew = lp->d_trackskew;
487 if (trackskew < 0)
488 trackskew = 0;
489 }
490 if (interleave == 0) {
491 interleave = lp->d_interleave;
492 if (interleave <= 0)
493 interleave = 1;
494 }
495 if (fsize == 0) {
496 fsize = pp->p_fsize;
497 if (fsize <= 0)
498 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
499 }
500 if (bsize == 0) {
501 bsize = pp->p_frag * pp->p_fsize;
502 if (bsize <= 0)
503 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
504 }
505 if (cpgflg == 0) {
506 if (pp->p_cpg != 0)
507 cpg = pp->p_cpg;
508 }
509 /*
510 * Maxcontig sets the default for the maximum number of blocks
511 * that may be allocated sequentially. With filesystem clustering
512 * it is possible to allocate contiguous blocks up to the maximum
513 * transfer size permitted by the controller or buffering.
514 */
515 if (maxcontig == 0)
516 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
517 if (density == 0)
518 density = NFPI * fsize;
519 if (minfree < MINFREE && opt != FS_OPTSPACE) {
520 warnx("%s %s %d%%", "Warning: changing optimization to space",
521 "because minfree is less than", MINFREE);
522 opt = FS_OPTSPACE;
523 }
524 if (trackspares == -1) {
525 trackspares = lp->d_sparespertrack;
526 if (trackspares < 0)
527 trackspares = 0;
528 }
529 nphyssectors = nsectors + trackspares;
530 if (cylspares == -1) {
531 cylspares = lp->d_sparespercyl;
532 if (cylspares < 0)
533 cylspares = 0;
534 }
535 secpercyl = nsectors * ntracks - cylspares;
536 if (secpercyl != lp->d_secpercyl)
537 warnx("%s (%d) %s (%u)\n",
538 "Warning: calculated sectors per cylinder", secpercyl,
539 "disagrees with disk label", lp->d_secpercyl);
540 if (maxbpg == 0)
541 maxbpg = MAXBLKPG(bsize);
542 headswitch = lp->d_headswitch;
543 trackseek = lp->d_trkseek;
544 #ifdef notdef /* label may be 0 if faked up by kernel */
545 bbsize = lp->d_bbsize;
546 sbsize = lp->d_sbsize;
547 #endif
548 oldpartition = *pp;
549 mkfs(pp, special, fsi, fso);
550 if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
551 rewritelabel(special, fso, lp);
552 if (!Nflag)
553 close(fso);
554 close(fsi);
555 #ifdef MFS
556 if (mfs) {
557 struct mfs_args args;
558
559 switch (pid = fork()) {
560 case -1:
561 perror("mfs");
562 exit(10);
563 case 0:
564 (void)snprintf(mountfromname, sizeof(mountfromname),
565 "mfs:%d", getpid());
566 break;
567 default:
568 (void)snprintf(mountfromname, sizeof(mountfromname),
569 "mfs:%d", pid);
570 for (;;) {
571 /*
572 * spin until the mount succeeds
573 * or the child exits
574 */
575 usleep(1);
576
577 /*
578 * XXX Here is a race condition: another process
579 * can mount a filesystem which hides our
580 * ramdisk before we see the success.
581 */
582 if (statfs(argv[1], &sf) < 0)
583 err(88, "statfs %s", argv[1]);
584 if (!strcmp(sf.f_mntfromname, mountfromname) &&
585 !strncmp(sf.f_mntonname, argv[1],
586 MNAMELEN) &&
587 !strcmp(sf.f_fstypename, "mfs"))
588 exit(0);
589
590 res = waitpid(pid, &status, WNOHANG);
591 if (res == -1)
592 err(11, "waitpid");
593 if (res != pid)
594 continue;
595 if (WIFEXITED(status)) {
596 if (WEXITSTATUS(status) == 0)
597 exit(0);
598 errx(1, "%s: mount: %s", argv[1],
599 strerror(WEXITSTATUS(status)));
600 } else
601 errx(11, "abnormal termination");
602 }
603 /* NOTREACHED */
604 }
605
606 (void) setsid();
607 (void) close(0);
608 (void) close(1);
609 (void) close(2);
610 (void) chdir("/");
611
612 args.fspec = mountfromname;
613 args.export.ex_root = -2;
614 if (mntflags & MNT_RDONLY)
615 args.export.ex_flags = MNT_EXRDONLY;
616 else
617 args.export.ex_flags = 0;
618 args.base = membase;
619 args.size = fssize * sectorsize;
620 if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
621 exit(errno); /* parent prints message */
622 }
623 #endif
624 exit(0);
625 }
626
627 #ifdef COMPAT
628 const char lmsg[] = "%s: can't read disk label; disk type must be specified";
629 #else
630 const char lmsg[] = "%s: can't read disk label";
631 #endif
632
633 static struct disklabel *
634 getdisklabel(char *s, volatile int fd)
635 /* XXX why is fs volatile?! */
636 {
637 static struct disklabel lab;
638
639 if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
640 #ifdef COMPAT
641 if (disktype) {
642 struct disklabel *lp;
643
644 unlabeled++;
645 lp = getdiskbyname(disktype);
646 if (lp == NULL)
647 errx(1, "%s: unknown disk type", disktype);
648 return (lp);
649 }
650 #endif
651 warn("ioctl (GDINFO)");
652 errx(1, lmsg, s);
653 }
654 return (&lab);
655 }
656
657 static void
658 rewritelabel(char *s, volatile int fd, struct disklabel *lp)
659 /* XXX why is fd volatile?! */
660 {
661 #ifdef COMPAT
662 if (unlabeled)
663 return;
664 #endif
665 lp->d_checksum = 0;
666 lp->d_checksum = dkcksum(lp);
667 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
668 warn("ioctl (WDINFO)");
669 errx(1, "%s: can't rewrite disk label", s);
670 }
671 #if __vax__
672 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
673 int i;
674 int cfd;
675 daddr_t alt;
676 char specname[64];
677 char blk[1024];
678 char *cp;
679
680 /*
681 * Make name for 'c' partition.
682 */
683 strcpy(specname, s);
684 cp = specname + strlen(specname) - 1;
685 if (!isdigit(*cp))
686 *cp = 'c';
687 cfd = open(specname, O_WRONLY);
688 if (cfd < 0)
689 err(1, "%s: open", specname);
690 memset(blk, 0, sizeof(blk));
691 *(struct disklabel *)(blk + LABELOFFSET) = *lp;
692 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
693 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
694 off_t offset;
695
696 offset = alt + i;
697 offset *= lp->d_secsize;
698 if (lseek(cfd, offset, SEEK_SET) == -1)
699 err(1, "lseek to badsector area: ");
700 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
701 warn("alternate label %d write", i/2);
702 }
703 close(cfd);
704 }
705 #endif
706 }
707
708 static void
709 usage(void)
710 {
711 if (mfs) {
712 fprintf(stderr,
713 "usage: %s [ -fsoptions ] special-device mount-point\n",
714 getprogname());
715 } else
716 fprintf(stderr,
717 "usage: %s [ -fsoptions ] special-device%s\n",
718 getprogname(),
719 #ifdef COMPAT
720 " [device-type]");
721 #else
722 "");
723 #endif
724 fprintf(stderr, "where fsoptions are:\n");
725 fprintf(stderr, "\t-B byte order (`be' or `le')\n");
726 fprintf(stderr,
727 "\t-N do not create file system, just print out parameters\n");
728 fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
729 fprintf(stderr, "\t-S sector size\n");
730 #ifdef COMPAT
731 fprintf(stderr, "\t-T disktype\n");
732 #endif
733 fprintf(stderr, "\t-a maximum contiguous blocks\n");
734 fprintf(stderr, "\t-b block size\n");
735 fprintf(stderr, "\t-c cylinders/group\n");
736 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
737 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
738 fprintf(stderr, "\t-f frag size\n");
739 fprintf(stderr, "\t-i number of bytes per inode\n");
740 fprintf(stderr, "\t-k sector 0 skew, per track\n");
741 fprintf(stderr, "\t-l hardware sector interleave\n");
742 fprintf(stderr, "\t-m minimum free space %%\n");
743 fprintf(stderr, "\t-n number of distinguished rotational positions\n");
744 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
745 fprintf(stderr, "\t-p spare sectors per track\n");
746 fprintf(stderr, "\t-s file system size (sectors)\n");
747 fprintf(stderr, "\t-r revolutions/minute\n");
748 fprintf(stderr, "\t-t tracks/cylinder\n");
749 fprintf(stderr, "\t-u sectors/track\n");
750 fprintf(stderr, "\t-x spare sectors per cylinder\n");
751 exit(1);
752 }
753