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