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