newfs.c revision 1.6 1 1.6 perseant /* $NetBSD: newfs.c,v 1.6 2000/12/05 19:51:15 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 perseant __RCSID("$NetBSD: newfs.c,v 1.6 2000/12/05 19:51:15 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.1 perseant int Nflag; /* run without writing file system */
82 1.1 perseant int fssize; /* file system size */
83 1.1 perseant int sectorsize; /* bytes/sector */
84 1.1 perseant int fsize = 0; /* fragment size */
85 1.1 perseant int bsize = 0; /* block size */
86 1.1 perseant int minfree = MINFREE; /* free space threshold */
87 1.4 perseant int minfreeseg = 0; /* free segments reserved for the cleaner */
88 1.1 perseant int bbsize = BBSIZE; /* boot block size */
89 1.1 perseant int sbsize = SBSIZE; /* superblock size */
90 1.1 perseant u_long memleft; /* virtual memory available */
91 1.1 perseant caddr_t membase; /* start address of memory based filesystem */
92 1.1 perseant #ifdef COMPAT
93 1.1 perseant char *disktype;
94 1.1 perseant int unlabeled;
95 1.1 perseant #endif
96 1.1 perseant
97 1.1 perseant char device[MAXPATHLEN];
98 1.1 perseant char *progname, *special;
99 1.1 perseant
100 1.1 perseant int main __P((int, char **));
101 1.1 perseant static struct disklabel *getdisklabel __P((char *, int));
102 1.1 perseant static struct disklabel *debug_readlabel __P((int));
103 1.1 perseant #ifdef notdef
104 1.1 perseant static void rewritelabel __P((char *, int, struct disklabel *));
105 1.1 perseant #endif
106 1.1 perseant static void usage __P((void));
107 1.1 perseant
108 1.6 perseant #define CHUNKSIZE 65536
109 1.6 perseant
110 1.6 perseant static size_t
111 1.6 perseant auto_segsize(int fd, off_t len, int version)
112 1.6 perseant {
113 1.6 perseant off_t off, bw;
114 1.6 perseant time_t start, finish;
115 1.6 perseant char buf[CHUNKSIZE];
116 1.6 perseant long seeks;
117 1.6 perseant size_t final;
118 1.6 perseant int i;
119 1.6 perseant
120 1.6 perseant /* First, get sequential access bandwidth */
121 1.6 perseant time(&start);
122 1.6 perseant finish = start;
123 1.6 perseant for (off = 0; finish - start < 10; off += CHUNKSIZE) {
124 1.6 perseant if (pread(fd, buf, CHUNKSIZE, off) < 0)
125 1.6 perseant break;
126 1.6 perseant time(&finish);
127 1.6 perseant }
128 1.6 perseant /* Bandwidth = bytes / sec */
129 1.6 perseant /* printf("%ld bytes in %ld seconds\n", (long)off, (long)(finish - start)); */
130 1.6 perseant bw = off / (finish - start);
131 1.6 perseant
132 1.6 perseant /* Second, seek time */
133 1.6 perseant time(&start);
134 1.6 perseant finish = start; /* structure copy */
135 1.6 perseant for (seeks = 0; finish - start < 10; ) {
136 1.6 perseant off = (((double)rand()) * len) / (off_t)RAND_MAX;
137 1.6 perseant if (pread(fd, buf, dbtob(1), off) < 0)
138 1.6 perseant break;
139 1.6 perseant time(&finish);
140 1.6 perseant ++seeks;
141 1.6 perseant }
142 1.6 perseant /* printf("%ld seeks in %ld seconds\n", (long)seeks, (long)(finish - start)); */
143 1.6 perseant /* Seek time in units/sec */
144 1.6 perseant seeks /= (finish - start);
145 1.6 perseant if (seeks == 0)
146 1.6 perseant seeks = 1;
147 1.6 perseant
148 1.6 perseant printf("bandwidth %ld B/s, seek time %ld ms (%ld seeks/s)\n",
149 1.6 perseant (long)bw, 1000/seeks, seeks);
150 1.6 perseant final = dbtob(btodb(4 * bw / seeks));
151 1.6 perseant
152 1.6 perseant /* Version 1 filesystems have po2 segment sizes */
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
159 1.6 perseant printf("using initial segment size %ld\n", (long)final);
160 1.6 perseant return final;
161 1.6 perseant }
162 1.6 perseant
163 1.1 perseant int
164 1.1 perseant main(argc, argv)
165 1.1 perseant int argc;
166 1.1 perseant char *argv[];
167 1.1 perseant {
168 1.1 perseant int ch;
169 1.1 perseant struct partition *pp;
170 1.1 perseant struct disklabel *lp;
171 1.1 perseant struct stat st;
172 1.6 perseant int debug, force, fsi, fso, segsize, maxpartitions;
173 1.1 perseant char *cp, *opstring;
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.6 perseant opstring = "AB:DFLNb:f:M:m:s:";
185 1.1 perseant
186 1.6 perseant debug = force = segsize = 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.6 perseant case 'L': /* Compatibility only */
203 1.1 perseant break;
204 1.4 perseant case 'M':
205 1.4 perseant minfreeseg = atoi(optarg);
206 1.4 perseant break;
207 1.1 perseant case 'N':
208 1.1 perseant Nflag++;
209 1.1 perseant break;
210 1.1 perseant #ifdef COMPAT
211 1.1 perseant case 'T':
212 1.1 perseant disktype = optarg;
213 1.2 perseant break;
214 1.2 perseant #endif
215 1.6 perseant case 'b':
216 1.1 perseant if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
217 1.1 perseant fatal("%s: bad block size", optarg);
218 1.1 perseant break;
219 1.1 perseant case 'f':
220 1.1 perseant if ((fsize = atoi(optarg)) <= 0)
221 1.1 perseant fatal("%s: bad frag size", optarg);
222 1.1 perseant break;
223 1.2 perseant case 'm':
224 1.1 perseant if ((minfree = atoi(optarg)) < 0 || minfree > 99)
225 1.1 perseant fatal("%s: bad free space %%\n", optarg);
226 1.1 perseant break;
227 1.2 perseant case 's':
228 1.1 perseant if ((fssize = atoi(optarg)) <= 0)
229 1.1 perseant fatal("%s: bad file system size", optarg);
230 1.1 perseant break;
231 1.1 perseant case '?':
232 1.1 perseant default:
233 1.1 perseant usage();
234 1.1 perseant }
235 1.1 perseant argc -= optind;
236 1.1 perseant argv += optind;
237 1.1 perseant
238 1.2 perseant if (argc != 2 && argc != 1)
239 1.1 perseant usage();
240 1.1 perseant
241 1.1 perseant /*
242 1.1 perseant * If the -N flag isn't specified, open the output file. If no path
243 1.1 perseant * prefix, try /dev/r%s and then /dev/%s.
244 1.1 perseant */
245 1.1 perseant special = argv[0];
246 1.1 perseant if (strchr(special, '/') == NULL) {
247 1.1 perseant (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
248 1.1 perseant special);
249 1.1 perseant if (stat(device, &st) == -1)
250 1.1 perseant (void)snprintf(device, sizeof(device), "%s%s",
251 1.1 perseant _PATH_DEV, special);
252 1.1 perseant special = device;
253 1.1 perseant }
254 1.1 perseant if (!Nflag) {
255 1.1 perseant fso = open(special,
256 1.1 perseant (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
257 1.1 perseant if (fso < 0)
258 1.1 perseant fatal("%s: %s", special, strerror(errno));
259 1.1 perseant } else
260 1.1 perseant fso = -1;
261 1.1 perseant
262 1.1 perseant /* Open the input file. */
263 1.1 perseant fsi = open(special, O_RDONLY);
264 1.1 perseant if (fsi < 0)
265 1.1 perseant fatal("%s: %s", special, strerror(errno));
266 1.1 perseant if (fstat(fsi, &st) < 0)
267 1.1 perseant fatal("%s: %s", special, strerror(errno));
268 1.1 perseant
269 1.6 perseant
270 1.2 perseant if (!debug && !S_ISCHR(st.st_mode))
271 1.1 perseant (void)printf("%s: %s: not a character-special device\n",
272 1.1 perseant progname, special);
273 1.1 perseant cp = strchr(argv[0], '\0') - 1;
274 1.1 perseant if (!debug
275 1.1 perseant && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
276 1.1 perseant && !isdigit(*cp))))
277 1.1 perseant fatal("%s: can't figure out file system partition", argv[0]);
278 1.1 perseant
279 1.1 perseant #ifdef COMPAT
280 1.2 perseant if (disktype == NULL)
281 1.1 perseant disktype = argv[1];
282 1.1 perseant #endif
283 1.1 perseant if (debug)
284 1.1 perseant lp = debug_readlabel(fsi);
285 1.1 perseant else
286 1.1 perseant lp = getdisklabel(special, fsi);
287 1.1 perseant
288 1.1 perseant if (isdigit(*cp))
289 1.1 perseant pp = &lp->d_partitions[0];
290 1.1 perseant else
291 1.1 perseant pp = &lp->d_partitions[*cp - 'a'];
292 1.1 perseant if (pp->p_size == 0)
293 1.1 perseant fatal("%s: `%c' partition is unavailable", argv[0], *cp);
294 1.3 perseant
295 1.3 perseant /* If force, make the partition look like an LFS */
296 1.3 perseant if(force) {
297 1.3 perseant pp->p_fstype = FS_BSDLFS;
298 1.3 perseant /* 0 means to use defaults */
299 1.3 perseant pp->p_fsize = 0;
300 1.3 perseant pp->p_frag = 0;
301 1.3 perseant pp->p_sgs = 0;
302 1.3 perseant }
303 1.1 perseant
304 1.6 perseant /* Try autoconfiguring segment size, if asked to */
305 1.6 perseant if (segsize == -1)
306 1.6 perseant segsize = auto_segsize(fsi, dbtob(pp->p_size), 1);
307 1.6 perseant
308 1.1 perseant /* If we're making a LFS, we break out here */
309 1.4 perseant exit(make_lfs(fso, lp, pp, minfree, bsize, fsize, segsize,
310 1.4 perseant minfreeseg));
311 1.1 perseant }
312 1.1 perseant
313 1.1 perseant #ifdef COMPAT
314 1.1 perseant char lmsg[] = "%s: can't read disk label; disk type must be specified";
315 1.1 perseant #else
316 1.1 perseant char lmsg[] = "%s: can't read disk label";
317 1.1 perseant #endif
318 1.1 perseant
319 1.1 perseant static struct disklabel *
320 1.1 perseant getdisklabel(s, fd)
321 1.1 perseant char *s;
322 1.1 perseant int fd;
323 1.1 perseant {
324 1.1 perseant static struct disklabel lab;
325 1.1 perseant
326 1.1 perseant if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
327 1.1 perseant #ifdef COMPAT
328 1.1 perseant if (disktype) {
329 1.1 perseant struct disklabel *lp;
330 1.1 perseant
331 1.1 perseant unlabeled++;
332 1.1 perseant lp = getdiskbyname(disktype);
333 1.1 perseant if (lp == NULL)
334 1.1 perseant fatal("%s: unknown disk type", disktype);
335 1.1 perseant return (lp);
336 1.1 perseant }
337 1.1 perseant #endif
338 1.1 perseant (void)fprintf(stderr,
339 1.1 perseant "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
340 1.1 perseant fatal(lmsg, s);
341 1.1 perseant }
342 1.1 perseant return (&lab);
343 1.1 perseant }
344 1.1 perseant
345 1.1 perseant
346 1.1 perseant static struct disklabel *
347 1.1 perseant debug_readlabel(fd)
348 1.1 perseant int fd;
349 1.1 perseant {
350 1.1 perseant static struct disklabel lab;
351 1.1 perseant int n;
352 1.1 perseant
353 1.1 perseant if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
354 1.1 perseant fatal("unable to read disk label: %s", strerror(errno));
355 1.1 perseant else if (n < sizeof(struct disklabel))
356 1.5 he fatal("short read of disklabel: %d of %ld bytes", n,
357 1.5 he (u_long) sizeof(struct disklabel));
358 1.1 perseant return(&lab);
359 1.1 perseant }
360 1.1 perseant
361 1.1 perseant #ifdef notdef
362 1.1 perseant static void
363 1.1 perseant rewritelabel(s, fd, lp)
364 1.1 perseant char *s;
365 1.1 perseant int fd;
366 1.1 perseant struct disklabel *lp;
367 1.1 perseant {
368 1.1 perseant #ifdef COMPAT
369 1.1 perseant if (unlabeled)
370 1.1 perseant return;
371 1.1 perseant #endif
372 1.1 perseant lp->d_checksum = 0;
373 1.1 perseant lp->d_checksum = dkcksum(lp);
374 1.1 perseant if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
375 1.1 perseant (void)fprintf(stderr,
376 1.1 perseant "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
377 1.1 perseant fatal("%s: can't rewrite disk label", s);
378 1.1 perseant }
379 1.1 perseant #if __vax__
380 1.1 perseant if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
381 1.1 perseant int i;
382 1.1 perseant int cfd;
383 1.1 perseant daddr_t alt;
384 1.1 perseant char specname[64];
385 1.1 perseant char blk[1024];
386 1.1 perseant char *cp;
387 1.1 perseant
388 1.1 perseant /*
389 1.1 perseant * Make name for 'c' partition.
390 1.1 perseant */
391 1.1 perseant strcpy(specname, s);
392 1.1 perseant cp = specname + strlen(specname) - 1;
393 1.1 perseant if (!isdigit(*cp))
394 1.1 perseant *cp = 'c';
395 1.1 perseant cfd = open(specname, O_WRONLY);
396 1.1 perseant if (cfd < 0)
397 1.1 perseant fatal("%s: %s", specname, strerror(errno));
398 1.1 perseant memset(blk, 0, sizeof(blk));
399 1.1 perseant *(struct disklabel *)(blk + LABELOFFSET) = *lp;
400 1.1 perseant alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
401 1.1 perseant for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
402 1.1 perseant if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
403 1.1 perseant SEEK_SET) == -1)
404 1.1 perseant fatal("lseek to badsector area: %s",
405 1.1 perseant strerror(errno));
406 1.1 perseant if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
407 1.1 perseant fprintf(stderr,
408 1.1 perseant "%s: alternate label %d write: %s\n",
409 1.1 perseant progname, i/2, strerror(errno));
410 1.1 perseant }
411 1.1 perseant close(cfd);
412 1.1 perseant }
413 1.1 perseant #endif /* vax */
414 1.1 perseant }
415 1.1 perseant #endif /* notdef */
416 1.1 perseant
417 1.1 perseant void
418 1.1 perseant usage()
419 1.1 perseant {
420 1.2 perseant fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
421 1.1 perseant fprintf(stderr, "where fsoptions are:\n");
422 1.6 perseant fprintf(stderr, "\t-A (autoconfigure segment size)\n");
423 1.6 perseant fprintf(stderr, "\t-B segment size in bytes\n");
424 1.1 perseant fprintf(stderr, "\t-D debug\n");
425 1.1 perseant fprintf(stderr,
426 1.6 perseant "\t-N (do not create file system, just print out parameters)\n");
427 1.6 perseant fprintf(stderr, "\t-b block size in bytes\n");
428 1.6 perseant fprintf(stderr, "\t-f frag size in bytes\n");
429 1.1 perseant fprintf(stderr, "\t-m minimum free space %%\n");
430 1.6 perseant fprintf(stderr, "\t-s file system size in sectors\n");
431 1.1 perseant exit(1);
432 1.1 perseant }
433