newfs.c revision 1.6.2.3 1 1.6.2.3 perseant /* $NetBSD: newfs.c,v 1.6.2.3 2001/07/13 05:14:23 perseant 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.6.2.3 perseant __RCSID("$NetBSD: newfs.c,v 1.6.2.3 2001/07/13 05:14:23 perseant 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.1 perseant #include <errno.h>
69 1.1 perseant #include <unistd.h>
70 1.1 perseant #include <stdio.h>
71 1.1 perseant #include <stdlib.h>
72 1.1 perseant #include <ctype.h>
73 1.1 perseant #include <string.h>
74 1.1 perseant #include <paths.h>
75 1.1 perseant #include <util.h>
76 1.1 perseant #include "config.h"
77 1.1 perseant #include "extern.h"
78 1.1 perseant
79 1.1 perseant #define COMPAT /* allow non-labeled disks */
80 1.1 perseant
81 1.6.2.3 perseant int version = DFL_VERSION; /* what version of lfs to make */
82 1.6.2.1 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.6.2.1 perseant int ibsize = 0; /* inode block size */
88 1.6.2.1 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.6.2.1 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.6.2.2 perseant static struct disklabel *getdisklabel(char *, int);
103 1.6.2.2 perseant static struct disklabel *debug_readlabel(int);
104 1.1 perseant #ifdef notdef
105 1.6.2.2 perseant static void rewritelabel(char *, int, struct disklabel *);
106 1.1 perseant #endif
107 1.6.2.2 perseant static void usage(void);
108 1.1 perseant
109 1.6.2.1 perseant /* CHUNKSIZE should be larger than MAXPHYS */
110 1.6.2.1 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.6.2.1 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.6.2.2 perseant main(int argc, char **argv)
164 1.1 perseant {
165 1.1 perseant int 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.6.2.1 perseant daddr_t start;
171 1.1 perseant char *cp, *opstring;
172 1.1 perseant
173 1.1 perseant if ((progname = strrchr(*argv, '/')) != NULL)
174 1.1 perseant ++progname;
175 1.1 perseant else
176 1.1 perseant progname = *argv;
177 1.1 perseant
178 1.1 perseant maxpartitions = getmaxpartitions();
179 1.1 perseant if (maxpartitions > 26)
180 1.1 perseant fatal("insane maxpartitions value %d", maxpartitions);
181 1.1 perseant
182 1.6.2.3 perseant opstring = "AB:b:DFf:I:i:LM:m:NO:r:s:v:";
183 1.1 perseant
184 1.6.2.1 perseant debug = force = segsize = start = 0;
185 1.1 perseant while ((ch = getopt(argc, argv, opstring)) != -1)
186 1.1 perseant switch(ch) {
187 1.6 perseant case 'A': /* Adaptively configure segment size */
188 1.6 perseant segsize = -1;
189 1.6 perseant break;
190 1.1 perseant case 'B': /* LFS segment size */
191 1.1 perseant if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
192 1.1 perseant fatal("%s: bad segment size", optarg);
193 1.1 perseant break;
194 1.1 perseant case 'D':
195 1.1 perseant debug = 1;
196 1.1 perseant break;
197 1.3 perseant case 'F':
198 1.3 perseant force = 1;
199 1.3 perseant break;
200 1.6.2.1 perseant case 'I':
201 1.6.2.1 perseant interleave = atoi(optarg);
202 1.6.2.1 perseant break;
203 1.6 perseant case 'L': /* Compatibility only */
204 1.1 perseant break;
205 1.4 perseant case 'M':
206 1.4 perseant minfreeseg = atoi(optarg);
207 1.4 perseant break;
208 1.1 perseant case 'N':
209 1.1 perseant Nflag++;
210 1.1 perseant break;
211 1.6.2.1 perseant case 'O':
212 1.6.2.1 perseant start = atoi(optarg);
213 1.6.2.1 perseant break;
214 1.1 perseant #ifdef COMPAT
215 1.1 perseant case 'T':
216 1.1 perseant disktype = optarg;
217 1.2 perseant break;
218 1.2 perseant #endif
219 1.6 perseant case 'b':
220 1.1 perseant if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
221 1.1 perseant fatal("%s: bad block size", optarg);
222 1.1 perseant break;
223 1.1 perseant case 'f':
224 1.1 perseant if ((fsize = atoi(optarg)) <= 0)
225 1.1 perseant fatal("%s: bad frag size", optarg);
226 1.1 perseant break;
227 1.6.2.1 perseant case 'i':
228 1.6.2.1 perseant if ((ibsize = atoi(optarg)) <= 0)
229 1.6.2.1 perseant fatal("%s: bad inode block size", optarg);
230 1.6.2.1 perseant break;
231 1.2 perseant case 'm':
232 1.1 perseant if ((minfree = atoi(optarg)) < 0 || minfree > 99)
233 1.1 perseant fatal("%s: bad free space %%\n", optarg);
234 1.1 perseant break;
235 1.6.2.1 perseant case 'r':
236 1.6.2.1 perseant if ((roll_id = strtoul(optarg, NULL, 0)) == 0)
237 1.6.2.1 perseant fatal("%s: bad roll-forward id\n", optarg);
238 1.6.2.1 perseant break;
239 1.2 perseant case 's':
240 1.1 perseant if ((fssize = atoi(optarg)) <= 0)
241 1.1 perseant fatal("%s: bad file system size", optarg);
242 1.1 perseant break;
243 1.6.2.1 perseant case 'v':
244 1.6.2.1 perseant version = atoi(optarg);
245 1.6.2.1 perseant if (version <= 0 || version > LFS_VERSION)
246 1.6.2.1 perseant fatal("%s: bad version", optarg);
247 1.6.2.1 perseant break;
248 1.1 perseant case '?':
249 1.1 perseant default:
250 1.1 perseant usage();
251 1.1 perseant }
252 1.1 perseant argc -= optind;
253 1.1 perseant argv += optind;
254 1.1 perseant
255 1.2 perseant if (argc != 2 && argc != 1)
256 1.1 perseant usage();
257 1.1 perseant
258 1.1 perseant /*
259 1.1 perseant * If the -N flag isn't specified, open the output file. If no path
260 1.1 perseant * prefix, try /dev/r%s and then /dev/%s.
261 1.1 perseant */
262 1.1 perseant special = argv[0];
263 1.1 perseant if (strchr(special, '/') == NULL) {
264 1.1 perseant (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
265 1.1 perseant special);
266 1.1 perseant if (stat(device, &st) == -1)
267 1.1 perseant (void)snprintf(device, sizeof(device), "%s%s",
268 1.1 perseant _PATH_DEV, special);
269 1.1 perseant special = device;
270 1.1 perseant }
271 1.1 perseant if (!Nflag) {
272 1.1 perseant fso = open(special,
273 1.1 perseant (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
274 1.1 perseant if (fso < 0)
275 1.1 perseant fatal("%s: %s", special, strerror(errno));
276 1.1 perseant } else
277 1.1 perseant fso = -1;
278 1.1 perseant
279 1.1 perseant /* Open the input file. */
280 1.1 perseant fsi = open(special, O_RDONLY);
281 1.1 perseant if (fsi < 0)
282 1.1 perseant fatal("%s: %s", special, strerror(errno));
283 1.1 perseant if (fstat(fsi, &st) < 0)
284 1.1 perseant fatal("%s: %s", special, strerror(errno));
285 1.1 perseant
286 1.6 perseant
287 1.2 perseant if (!debug && !S_ISCHR(st.st_mode))
288 1.1 perseant (void)printf("%s: %s: not a character-special device\n",
289 1.1 perseant progname, special);
290 1.1 perseant cp = strchr(argv[0], '\0') - 1;
291 1.1 perseant if (!debug
292 1.1 perseant && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
293 1.1 perseant && !isdigit(*cp))))
294 1.1 perseant fatal("%s: can't figure out file system partition", argv[0]);
295 1.1 perseant
296 1.1 perseant #ifdef COMPAT
297 1.2 perseant if (disktype == NULL)
298 1.1 perseant disktype = argv[1];
299 1.1 perseant #endif
300 1.1 perseant if (debug)
301 1.1 perseant lp = debug_readlabel(fsi);
302 1.1 perseant else
303 1.1 perseant lp = getdisklabel(special, fsi);
304 1.1 perseant
305 1.1 perseant if (isdigit(*cp))
306 1.1 perseant pp = &lp->d_partitions[0];
307 1.1 perseant else
308 1.1 perseant pp = &lp->d_partitions[*cp - 'a'];
309 1.1 perseant if (pp->p_size == 0)
310 1.1 perseant fatal("%s: `%c' partition is unavailable", argv[0], *cp);
311 1.3 perseant
312 1.3 perseant /* If force, make the partition look like an LFS */
313 1.3 perseant if(force) {
314 1.3 perseant pp->p_fstype = FS_BSDLFS;
315 1.3 perseant /* 0 means to use defaults */
316 1.3 perseant pp->p_fsize = 0;
317 1.3 perseant pp->p_frag = 0;
318 1.3 perseant pp->p_sgs = 0;
319 1.3 perseant }
320 1.1 perseant
321 1.6 perseant /* Try autoconfiguring segment size, if asked to */
322 1.6 perseant if (segsize == -1)
323 1.6.2.1 perseant segsize = auto_segsize(fsi, dbtob(pp->p_size), version);
324 1.6 perseant
325 1.1 perseant /* If we're making a LFS, we break out here */
326 1.4 perseant exit(make_lfs(fso, lp, pp, minfree, bsize, fsize, segsize,
327 1.6.2.1 perseant minfreeseg, version, start, ibsize, interleave,
328 1.6.2.1 perseant roll_id));
329 1.1 perseant }
330 1.1 perseant
331 1.1 perseant #ifdef COMPAT
332 1.1 perseant char lmsg[] = "%s: can't read disk label; disk type must be specified";
333 1.1 perseant #else
334 1.1 perseant char lmsg[] = "%s: can't read disk label";
335 1.1 perseant #endif
336 1.1 perseant
337 1.1 perseant static struct disklabel *
338 1.6.2.2 perseant getdisklabel(char *s, int fd)
339 1.1 perseant {
340 1.1 perseant static struct disklabel lab;
341 1.1 perseant
342 1.1 perseant if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
343 1.1 perseant #ifdef COMPAT
344 1.1 perseant if (disktype) {
345 1.1 perseant struct disklabel *lp;
346 1.1 perseant
347 1.1 perseant unlabeled++;
348 1.1 perseant lp = getdiskbyname(disktype);
349 1.1 perseant if (lp == NULL)
350 1.1 perseant fatal("%s: unknown disk type", disktype);
351 1.1 perseant return (lp);
352 1.1 perseant }
353 1.1 perseant #endif
354 1.1 perseant (void)fprintf(stderr,
355 1.1 perseant "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
356 1.1 perseant fatal(lmsg, s);
357 1.1 perseant }
358 1.1 perseant return (&lab);
359 1.1 perseant }
360 1.1 perseant
361 1.1 perseant
362 1.1 perseant static struct disklabel *
363 1.6.2.2 perseant debug_readlabel(int fd)
364 1.1 perseant {
365 1.1 perseant static struct disklabel lab;
366 1.1 perseant int n;
367 1.1 perseant
368 1.1 perseant if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
369 1.1 perseant fatal("unable to read disk label: %s", strerror(errno));
370 1.1 perseant else if (n < sizeof(struct disklabel))
371 1.5 he fatal("short read of disklabel: %d of %ld bytes", n,
372 1.5 he (u_long) sizeof(struct disklabel));
373 1.1 perseant return(&lab);
374 1.1 perseant }
375 1.1 perseant
376 1.1 perseant #ifdef notdef
377 1.1 perseant static void
378 1.6.2.2 perseant rewritelabel(char *s, int fd, struct disklabel *lp)
379 1.1 perseant {
380 1.1 perseant #ifdef COMPAT
381 1.1 perseant if (unlabeled)
382 1.1 perseant return;
383 1.1 perseant #endif
384 1.1 perseant lp->d_checksum = 0;
385 1.1 perseant lp->d_checksum = dkcksum(lp);
386 1.1 perseant if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
387 1.1 perseant (void)fprintf(stderr,
388 1.1 perseant "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
389 1.1 perseant fatal("%s: can't rewrite disk label", s);
390 1.1 perseant }
391 1.1 perseant #if __vax__
392 1.1 perseant if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
393 1.1 perseant int i;
394 1.1 perseant int cfd;
395 1.1 perseant daddr_t alt;
396 1.1 perseant char specname[64];
397 1.1 perseant char blk[1024];
398 1.1 perseant char *cp;
399 1.1 perseant
400 1.1 perseant /*
401 1.1 perseant * Make name for 'c' partition.
402 1.1 perseant */
403 1.1 perseant strcpy(specname, s);
404 1.1 perseant cp = specname + strlen(specname) - 1;
405 1.1 perseant if (!isdigit(*cp))
406 1.1 perseant *cp = 'c';
407 1.1 perseant cfd = open(specname, O_WRONLY);
408 1.1 perseant if (cfd < 0)
409 1.1 perseant fatal("%s: %s", specname, strerror(errno));
410 1.1 perseant memset(blk, 0, sizeof(blk));
411 1.1 perseant *(struct disklabel *)(blk + LABELOFFSET) = *lp;
412 1.1 perseant alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
413 1.1 perseant for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
414 1.1 perseant if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
415 1.1 perseant SEEK_SET) == -1)
416 1.1 perseant fatal("lseek to badsector area: %s",
417 1.1 perseant strerror(errno));
418 1.1 perseant if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
419 1.1 perseant fprintf(stderr,
420 1.1 perseant "%s: alternate label %d write: %s\n",
421 1.1 perseant progname, i/2, strerror(errno));
422 1.1 perseant }
423 1.1 perseant close(cfd);
424 1.1 perseant }
425 1.1 perseant #endif /* vax */
426 1.1 perseant }
427 1.1 perseant #endif /* notdef */
428 1.1 perseant
429 1.1 perseant void
430 1.1 perseant usage()
431 1.1 perseant {
432 1.2 perseant fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
433 1.1 perseant fprintf(stderr, "where fsoptions are:\n");
434 1.6 perseant fprintf(stderr, "\t-A (autoconfigure segment size)\n");
435 1.6 perseant fprintf(stderr, "\t-B segment size in bytes\n");
436 1.6.2.1 perseant fprintf(stderr, "\t-D (debug)\n");
437 1.1 perseant fprintf(stderr,
438 1.6 perseant "\t-N (do not create file system, just print out parameters)\n");
439 1.6.2.1 perseant fprintf(stderr, "\t-O first segment offset in sectors\n");
440 1.6 perseant fprintf(stderr, "\t-b block size in bytes\n");
441 1.6 perseant fprintf(stderr, "\t-f frag size in bytes\n");
442 1.1 perseant fprintf(stderr, "\t-m minimum free space %%\n");
443 1.6 perseant fprintf(stderr, "\t-s file system size in sectors\n");
444 1.6.2.1 perseant fprintf(stderr, "\t-v version\n");
445 1.1 perseant exit(1);
446 1.1 perseant }
447