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