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