newfs.c revision 1.10 1 1.10 scw /* $NetBSD: newfs.c,v 1.10 2002/12/12 11:45:17 scw Exp $ */
2 1.1 perseant
3 1.1 perseant /*-
4 1.1 perseant * Copyright (c) 1989, 1992, 1993
5 1.1 perseant * The Regents of the University of California. All rights reserved.
6 1.1 perseant *
7 1.1 perseant * Redistribution and use in source and binary forms, with or without
8 1.1 perseant * modification, are permitted provided that the following conditions
9 1.1 perseant * are met:
10 1.1 perseant * 1. Redistributions of source code must retain the above copyright
11 1.1 perseant * notice, this list of conditions and the following disclaimer.
12 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 perseant * notice, this list of conditions and the following disclaimer in the
14 1.1 perseant * documentation and/or other materials provided with the distribution.
15 1.1 perseant * 3. All advertising materials mentioning features or use of this software
16 1.1 perseant * must display the following acknowledgement:
17 1.1 perseant * This product includes software developed by the University of
18 1.1 perseant * California, Berkeley and its contributors.
19 1.1 perseant * 4. Neither the name of the University nor the names of its contributors
20 1.1 perseant * may be used to endorse or promote products derived from this software
21 1.1 perseant * without specific prior written permission.
22 1.1 perseant *
23 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 perseant * SUCH DAMAGE.
34 1.1 perseant */
35 1.1 perseant
36 1.1 perseant #include <sys/cdefs.h>
37 1.1 perseant #ifndef lint
38 1.1 perseant __COPYRIGHT("@(#) Copyright (c) 1989, 1992, 1993\n\
39 1.1 perseant The Regents of the University of California. All rights reserved.\n");
40 1.1 perseant #endif /* not lint */
41 1.1 perseant
42 1.1 perseant #ifndef lint
43 1.1 perseant #if 0
44 1.1 perseant static char sccsid[] = "@(#)newfs.c 8.5 (Berkeley) 5/24/95";
45 1.1 perseant #else
46 1.10 scw __RCSID("$NetBSD: newfs.c,v 1.10 2002/12/12 11:45:17 scw Exp $");
47 1.1 perseant #endif
48 1.1 perseant #endif /* not lint */
49 1.1 perseant
50 1.1 perseant /*
51 1.1 perseant * newfs: friendly front end to mkfs
52 1.1 perseant */
53 1.1 perseant #include <sys/param.h>
54 1.1 perseant #include <sys/ucred.h>
55 1.1 perseant #include <sys/stat.h>
56 1.1 perseant #include <sys/ioctl.h>
57 1.1 perseant #include <sys/disklabel.h>
58 1.1 perseant #include <sys/file.h>
59 1.1 perseant #include <sys/mount.h>
60 1.1 perseant #include <sys/sysctl.h>
61 1.6 perseant #include <sys/time.h>
62 1.1 perseant
63 1.1 perseant #include <ufs/ufs/dir.h>
64 1.1 perseant #include <ufs/ufs/dinode.h>
65 1.6 perseant #include <ufs/lfs/lfs.h>
66 1.1 perseant
67 1.1 perseant #include <disktab.h>
68 1.9 perseant #include <err.h>
69 1.1 perseant #include <errno.h>
70 1.1 perseant #include <unistd.h>
71 1.1 perseant #include <stdio.h>
72 1.1 perseant #include <stdlib.h>
73 1.1 perseant #include <ctype.h>
74 1.1 perseant #include <string.h>
75 1.1 perseant #include <paths.h>
76 1.1 perseant #include <util.h>
77 1.1 perseant #include "config.h"
78 1.1 perseant #include "extern.h"
79 1.1 perseant
80 1.1 perseant #define COMPAT /* allow non-labeled disks */
81 1.1 perseant
82 1.7 perseant int Nflag = 0; /* run without writing file system */
83 1.1 perseant int fssize; /* file system size */
84 1.1 perseant int sectorsize; /* bytes/sector */
85 1.1 perseant int fsize = 0; /* fragment size */
86 1.1 perseant int bsize = 0; /* block size */
87 1.7 perseant int ibsize = 0; /* inode block size */
88 1.7 perseant int interleave = 0; /* segment interleave */
89 1.1 perseant int minfree = MINFREE; /* free space threshold */
90 1.4 perseant int minfreeseg = 0; /* free segments reserved for the cleaner */
91 1.7 perseant u_int32_t roll_id = 0; /* roll-forward id */
92 1.1 perseant u_long memleft; /* virtual memory available */
93 1.1 perseant caddr_t membase; /* start address of memory based filesystem */
94 1.1 perseant #ifdef COMPAT
95 1.1 perseant char *disktype;
96 1.1 perseant int unlabeled;
97 1.1 perseant #endif
98 1.1 perseant
99 1.1 perseant char device[MAXPATHLEN];
100 1.1 perseant char *progname, *special;
101 1.1 perseant
102 1.7 perseant static struct disklabel *getdisklabel(char *, int);
103 1.7 perseant static struct disklabel *debug_readlabel(int);
104 1.1 perseant #ifdef notdef
105 1.7 perseant static void rewritelabel(char *, int, struct disklabel *);
106 1.1 perseant #endif
107 1.7 perseant static void usage(void);
108 1.1 perseant
109 1.7 perseant /* CHUNKSIZE should be larger than MAXPHYS */
110 1.7 perseant #define CHUNKSIZE (1024 * 1024)
111 1.6 perseant
112 1.6 perseant static size_t
113 1.6 perseant auto_segsize(int fd, off_t len, int version)
114 1.6 perseant {
115 1.6 perseant off_t off, bw;
116 1.6 perseant time_t start, finish;
117 1.6 perseant char buf[CHUNKSIZE];
118 1.6 perseant long seeks;
119 1.6 perseant size_t final;
120 1.6 perseant int i;
121 1.6 perseant
122 1.6 perseant /* First, get sequential access bandwidth */
123 1.6 perseant time(&start);
124 1.6 perseant finish = start;
125 1.6 perseant for (off = 0; finish - start < 10; off += CHUNKSIZE) {
126 1.6 perseant if (pread(fd, buf, CHUNKSIZE, off) < 0)
127 1.6 perseant break;
128 1.6 perseant time(&finish);
129 1.6 perseant }
130 1.6 perseant /* Bandwidth = bytes / sec */
131 1.6 perseant /* printf("%ld bytes in %ld seconds\n", (long)off, (long)(finish - start)); */
132 1.6 perseant bw = off / (finish - start);
133 1.6 perseant
134 1.6 perseant /* Second, seek time */
135 1.6 perseant time(&start);
136 1.6 perseant finish = start; /* structure copy */
137 1.6 perseant for (seeks = 0; finish - start < 10; ) {
138 1.6 perseant off = (((double)rand()) * len) / (off_t)RAND_MAX;
139 1.6 perseant if (pread(fd, buf, dbtob(1), off) < 0)
140 1.6 perseant break;
141 1.6 perseant time(&finish);
142 1.6 perseant ++seeks;
143 1.6 perseant }
144 1.6 perseant /* printf("%ld seeks in %ld seconds\n", (long)seeks, (long)(finish - start)); */
145 1.6 perseant /* Seek time in units/sec */
146 1.6 perseant seeks /= (finish - start);
147 1.6 perseant if (seeks == 0)
148 1.6 perseant seeks = 1;
149 1.6 perseant
150 1.7 perseant printf("bw = %ld B/s, seek time %ld ms (%ld seeks/s)\n",
151 1.6 perseant (long)bw, 1000/seeks, seeks);
152 1.6 perseant final = dbtob(btodb(4 * bw / seeks));
153 1.6 perseant if (version == 1) {
154 1.6 perseant for (i = 0; final; final >>= 1, i++)
155 1.6 perseant ;
156 1.6 perseant final = 1 << i;
157 1.6 perseant }
158 1.6 perseant printf("using initial segment size %ld\n", (long)final);
159 1.6 perseant return final;
160 1.6 perseant }
161 1.6 perseant
162 1.1 perseant int
163 1.7 perseant main(int argc, char **argv)
164 1.1 perseant {
165 1.8 lukem int version, ch;
166 1.1 perseant struct partition *pp;
167 1.1 perseant struct disklabel *lp;
168 1.1 perseant struct stat st;
169 1.6 perseant int debug, force, fsi, fso, segsize, maxpartitions;
170 1.7 perseant daddr_t start;
171 1.1 perseant char *cp, *opstring;
172 1.8 lukem
173 1.8 lukem version = DFL_VERSION; /* what version of lfs to make */
174 1.1 perseant
175 1.1 perseant if ((progname = strrchr(*argv, '/')) != NULL)
176 1.1 perseant ++progname;
177 1.1 perseant else
178 1.1 perseant progname = *argv;
179 1.1 perseant
180 1.1 perseant maxpartitions = getmaxpartitions();
181 1.1 perseant if (maxpartitions > 26)
182 1.1 perseant fatal("insane maxpartitions value %d", maxpartitions);
183 1.1 perseant
184 1.7 perseant opstring = "AB:b:DFf:I:i:LM:m:NO:r:s:v:";
185 1.1 perseant
186 1.7 perseant debug = force = segsize = start = 0;
187 1.1 perseant while ((ch = getopt(argc, argv, opstring)) != -1)
188 1.1 perseant switch(ch) {
189 1.6 perseant case 'A': /* Adaptively configure segment size */
190 1.6 perseant segsize = -1;
191 1.6 perseant break;
192 1.1 perseant case 'B': /* LFS segment size */
193 1.1 perseant if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
194 1.1 perseant fatal("%s: bad segment size", optarg);
195 1.1 perseant break;
196 1.1 perseant case 'D':
197 1.1 perseant debug = 1;
198 1.1 perseant break;
199 1.3 perseant case 'F':
200 1.3 perseant force = 1;
201 1.3 perseant break;
202 1.7 perseant case 'I':
203 1.7 perseant interleave = atoi(optarg);
204 1.7 perseant break;
205 1.6 perseant case 'L': /* Compatibility only */
206 1.1 perseant break;
207 1.4 perseant case 'M':
208 1.4 perseant minfreeseg = atoi(optarg);
209 1.4 perseant break;
210 1.1 perseant case 'N':
211 1.1 perseant Nflag++;
212 1.1 perseant break;
213 1.7 perseant case 'O':
214 1.7 perseant start = atoi(optarg);
215 1.7 perseant break;
216 1.1 perseant #ifdef COMPAT
217 1.1 perseant case 'T':
218 1.1 perseant disktype = optarg;
219 1.2 perseant break;
220 1.2 perseant #endif
221 1.6 perseant case 'b':
222 1.1 perseant if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
223 1.1 perseant fatal("%s: bad block size", optarg);
224 1.1 perseant break;
225 1.1 perseant case 'f':
226 1.1 perseant if ((fsize = atoi(optarg)) <= 0)
227 1.1 perseant fatal("%s: bad frag size", optarg);
228 1.1 perseant break;
229 1.7 perseant case 'i':
230 1.7 perseant if ((ibsize = atoi(optarg)) <= 0)
231 1.7 perseant fatal("%s: bad inode block size", optarg);
232 1.7 perseant break;
233 1.2 perseant case 'm':
234 1.1 perseant if ((minfree = atoi(optarg)) < 0 || minfree > 99)
235 1.1 perseant fatal("%s: bad free space %%\n", optarg);
236 1.1 perseant break;
237 1.7 perseant case 'r':
238 1.7 perseant if ((roll_id = strtoul(optarg, NULL, 0)) == 0)
239 1.7 perseant fatal("%s: bad roll-forward id\n", optarg);
240 1.7 perseant break;
241 1.2 perseant case 's':
242 1.1 perseant if ((fssize = atoi(optarg)) <= 0)
243 1.1 perseant fatal("%s: bad file system size", optarg);
244 1.1 perseant break;
245 1.7 perseant case 'v':
246 1.7 perseant version = atoi(optarg);
247 1.7 perseant if (version <= 0 || version > LFS_VERSION)
248 1.7 perseant fatal("%s: bad version", optarg);
249 1.7 perseant break;
250 1.1 perseant case '?':
251 1.1 perseant default:
252 1.1 perseant usage();
253 1.1 perseant }
254 1.1 perseant argc -= optind;
255 1.1 perseant argv += optind;
256 1.1 perseant
257 1.2 perseant if (argc != 2 && argc != 1)
258 1.1 perseant usage();
259 1.1 perseant
260 1.1 perseant /*
261 1.1 perseant * If the -N flag isn't specified, open the output file. If no path
262 1.1 perseant * prefix, try /dev/r%s and then /dev/%s.
263 1.1 perseant */
264 1.1 perseant special = argv[0];
265 1.1 perseant if (strchr(special, '/') == NULL) {
266 1.1 perseant (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
267 1.1 perseant special);
268 1.1 perseant if (stat(device, &st) == -1)
269 1.1 perseant (void)snprintf(device, sizeof(device), "%s%s",
270 1.1 perseant _PATH_DEV, special);
271 1.1 perseant special = device;
272 1.1 perseant }
273 1.1 perseant if (!Nflag) {
274 1.1 perseant fso = open(special,
275 1.1 perseant (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
276 1.1 perseant if (fso < 0)
277 1.1 perseant fatal("%s: %s", special, strerror(errno));
278 1.1 perseant } else
279 1.1 perseant fso = -1;
280 1.1 perseant
281 1.1 perseant /* Open the input file. */
282 1.1 perseant fsi = open(special, O_RDONLY);
283 1.1 perseant if (fsi < 0)
284 1.1 perseant fatal("%s: %s", special, strerror(errno));
285 1.1 perseant if (fstat(fsi, &st) < 0)
286 1.1 perseant fatal("%s: %s", special, strerror(errno));
287 1.1 perseant
288 1.6 perseant
289 1.2 perseant if (!debug && !S_ISCHR(st.st_mode))
290 1.1 perseant (void)printf("%s: %s: not a character-special device\n",
291 1.1 perseant progname, special);
292 1.1 perseant cp = strchr(argv[0], '\0') - 1;
293 1.1 perseant if (!debug
294 1.1 perseant && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
295 1.1 perseant && !isdigit(*cp))))
296 1.1 perseant fatal("%s: can't figure out file system partition", argv[0]);
297 1.1 perseant
298 1.1 perseant #ifdef COMPAT
299 1.2 perseant if (disktype == NULL)
300 1.1 perseant disktype = argv[1];
301 1.1 perseant #endif
302 1.1 perseant if (debug)
303 1.1 perseant lp = debug_readlabel(fsi);
304 1.1 perseant else
305 1.1 perseant lp = getdisklabel(special, fsi);
306 1.1 perseant
307 1.1 perseant if (isdigit(*cp))
308 1.1 perseant pp = &lp->d_partitions[0];
309 1.1 perseant else
310 1.1 perseant pp = &lp->d_partitions[*cp - 'a'];
311 1.1 perseant if (pp->p_size == 0)
312 1.1 perseant fatal("%s: `%c' partition is unavailable", argv[0], *cp);
313 1.3 perseant
314 1.3 perseant /* If force, make the partition look like an LFS */
315 1.3 perseant if(force) {
316 1.3 perseant pp->p_fstype = FS_BSDLFS;
317 1.3 perseant /* 0 means to use defaults */
318 1.3 perseant pp->p_fsize = 0;
319 1.3 perseant pp->p_frag = 0;
320 1.3 perseant pp->p_sgs = 0;
321 1.3 perseant }
322 1.1 perseant
323 1.6 perseant /* Try autoconfiguring segment size, if asked to */
324 1.9 perseant if (segsize == -1) {
325 1.9 perseant if (!S_ISCHR(st.st_mode)) {
326 1.9 perseant warnx("%s is not a character special device, ignoring -A", special);
327 1.9 perseant segsize = 0;
328 1.9 perseant } else
329 1.9 perseant segsize = auto_segsize(fsi, dbtob(pp->p_size), version);
330 1.9 perseant }
331 1.6 perseant
332 1.1 perseant /* If we're making a LFS, we break out here */
333 1.4 perseant exit(make_lfs(fso, lp, pp, minfree, bsize, fsize, segsize,
334 1.7 perseant minfreeseg, version, start, ibsize, interleave,
335 1.7 perseant roll_id));
336 1.1 perseant }
337 1.1 perseant
338 1.1 perseant #ifdef COMPAT
339 1.1 perseant char lmsg[] = "%s: can't read disk label; disk type must be specified";
340 1.1 perseant #else
341 1.1 perseant char lmsg[] = "%s: can't read disk label";
342 1.1 perseant #endif
343 1.1 perseant
344 1.1 perseant static struct disklabel *
345 1.7 perseant getdisklabel(char *s, int fd)
346 1.1 perseant {
347 1.1 perseant static struct disklabel lab;
348 1.1 perseant
349 1.1 perseant if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
350 1.1 perseant #ifdef COMPAT
351 1.1 perseant if (disktype) {
352 1.1 perseant struct disklabel *lp;
353 1.1 perseant
354 1.1 perseant unlabeled++;
355 1.1 perseant lp = getdiskbyname(disktype);
356 1.1 perseant if (lp == NULL)
357 1.1 perseant fatal("%s: unknown disk type", disktype);
358 1.1 perseant return (lp);
359 1.1 perseant }
360 1.1 perseant #endif
361 1.1 perseant (void)fprintf(stderr,
362 1.1 perseant "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
363 1.1 perseant fatal(lmsg, s);
364 1.1 perseant }
365 1.1 perseant return (&lab);
366 1.1 perseant }
367 1.1 perseant
368 1.1 perseant
369 1.1 perseant static struct disklabel *
370 1.7 perseant debug_readlabel(int fd)
371 1.1 perseant {
372 1.1 perseant static struct disklabel lab;
373 1.1 perseant int n;
374 1.1 perseant
375 1.1 perseant if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
376 1.1 perseant fatal("unable to read disk label: %s", strerror(errno));
377 1.1 perseant else if (n < sizeof(struct disklabel))
378 1.5 he fatal("short read of disklabel: %d of %ld bytes", n,
379 1.5 he (u_long) sizeof(struct disklabel));
380 1.1 perseant return(&lab);
381 1.1 perseant }
382 1.1 perseant
383 1.1 perseant #ifdef notdef
384 1.1 perseant static void
385 1.7 perseant rewritelabel(char *s, int fd, struct disklabel *lp)
386 1.1 perseant {
387 1.1 perseant #ifdef COMPAT
388 1.1 perseant if (unlabeled)
389 1.1 perseant return;
390 1.1 perseant #endif
391 1.1 perseant lp->d_checksum = 0;
392 1.1 perseant lp->d_checksum = dkcksum(lp);
393 1.1 perseant if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
394 1.1 perseant (void)fprintf(stderr,
395 1.1 perseant "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
396 1.1 perseant fatal("%s: can't rewrite disk label", s);
397 1.1 perseant }
398 1.1 perseant #if __vax__
399 1.1 perseant if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
400 1.1 perseant int i;
401 1.1 perseant int cfd;
402 1.1 perseant daddr_t alt;
403 1.10 scw off_t loff;
404 1.1 perseant char specname[64];
405 1.1 perseant char blk[1024];
406 1.1 perseant char *cp;
407 1.1 perseant
408 1.1 perseant /*
409 1.1 perseant * Make name for 'c' partition.
410 1.1 perseant */
411 1.1 perseant strcpy(specname, s);
412 1.1 perseant cp = specname + strlen(specname) - 1;
413 1.1 perseant if (!isdigit(*cp))
414 1.1 perseant *cp = 'c';
415 1.1 perseant cfd = open(specname, O_WRONLY);
416 1.1 perseant if (cfd < 0)
417 1.1 perseant fatal("%s: %s", specname, strerror(errno));
418 1.10 scw if ((loff = getlabeloffset()) < 0)
419 1.10 scw fatal("getlabeloffset: %s", strerror(errno));
420 1.1 perseant memset(blk, 0, sizeof(blk));
421 1.10 scw *(struct disklabel *)(blk + loff) = *lp;
422 1.1 perseant alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
423 1.1 perseant for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
424 1.1 perseant if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
425 1.1 perseant SEEK_SET) == -1)
426 1.1 perseant fatal("lseek to badsector area: %s",
427 1.1 perseant strerror(errno));
428 1.1 perseant if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
429 1.1 perseant fprintf(stderr,
430 1.1 perseant "%s: alternate label %d write: %s\n",
431 1.1 perseant progname, i/2, strerror(errno));
432 1.1 perseant }
433 1.1 perseant close(cfd);
434 1.1 perseant }
435 1.1 perseant #endif /* vax */
436 1.1 perseant }
437 1.1 perseant #endif /* notdef */
438 1.1 perseant
439 1.1 perseant void
440 1.1 perseant usage()
441 1.1 perseant {
442 1.2 perseant fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
443 1.1 perseant fprintf(stderr, "where fsoptions are:\n");
444 1.6 perseant fprintf(stderr, "\t-A (autoconfigure segment size)\n");
445 1.6 perseant fprintf(stderr, "\t-B segment size in bytes\n");
446 1.7 perseant fprintf(stderr, "\t-D (debug)\n");
447 1.1 perseant fprintf(stderr,
448 1.6 perseant "\t-N (do not create file system, just print out parameters)\n");
449 1.7 perseant fprintf(stderr, "\t-O first segment offset in sectors\n");
450 1.6 perseant fprintf(stderr, "\t-b block size in bytes\n");
451 1.6 perseant fprintf(stderr, "\t-f frag size in bytes\n");
452 1.1 perseant fprintf(stderr, "\t-m minimum free space %%\n");
453 1.6 perseant fprintf(stderr, "\t-s file system size in sectors\n");
454 1.7 perseant fprintf(stderr, "\t-v version\n");
455 1.1 perseant exit(1);
456 1.1 perseant }
457