newfs.c revision 1.6.2.1 1 /* $NetBSD: newfs.c,v 1.6.2.1 2001/06/27 03:49:42 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.1 2001/06/27 03:49:42 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 = 1; /* 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 int main __P((int, char **));
103 static struct disklabel *getdisklabel __P((char *, int));
104 static struct disklabel *debug_readlabel __P((int));
105 #ifdef notdef
106 static void rewritelabel __P((char *, int, struct disklabel *));
107 #endif
108 static void usage __P((void));
109
110 /* CHUNKSIZE should be larger than MAXPHYS */
111 #define CHUNKSIZE (1024 * 1024)
112
113 static size_t
114 auto_segsize(int fd, off_t len, int version)
115 {
116 off_t off, bw;
117 time_t start, finish;
118 char buf[CHUNKSIZE];
119 long seeks;
120 size_t final;
121 int i;
122
123 /* First, get sequential access bandwidth */
124 time(&start);
125 finish = start;
126 for (off = 0; finish - start < 10; off += CHUNKSIZE) {
127 if (pread(fd, buf, CHUNKSIZE, off) < 0)
128 break;
129 time(&finish);
130 }
131 /* Bandwidth = bytes / sec */
132 /* printf("%ld bytes in %ld seconds\n", (long)off, (long)(finish - start)); */
133 bw = off / (finish - start);
134
135 /* Second, seek time */
136 time(&start);
137 finish = start; /* structure copy */
138 for (seeks = 0; finish - start < 10; ) {
139 off = (((double)rand()) * len) / (off_t)RAND_MAX;
140 if (pread(fd, buf, dbtob(1), off) < 0)
141 break;
142 time(&finish);
143 ++seeks;
144 }
145 /* printf("%ld seeks in %ld seconds\n", (long)seeks, (long)(finish - start)); */
146 /* Seek time in units/sec */
147 seeks /= (finish - start);
148 if (seeks == 0)
149 seeks = 1;
150
151 printf("bw = %ld B/s, seek time %ld ms (%ld seeks/s)\n",
152 (long)bw, 1000/seeks, seeks);
153 final = dbtob(btodb(4 * bw / seeks));
154 if (version == 1) {
155 for (i = 0; final; final >>= 1, i++)
156 ;
157 final = 1 << i;
158 }
159 printf("using initial segment size %ld\n", (long)final);
160 return final;
161 }
162
163 int
164 main(argc, argv)
165 int argc;
166 char *argv[];
167 {
168 int ch;
169 struct partition *pp;
170 struct disklabel *lp;
171 struct stat st;
172 int debug, force, fsi, fso, segsize, maxpartitions;
173 daddr_t start;
174 char *cp, *opstring;
175
176 if ((progname = strrchr(*argv, '/')) != NULL)
177 ++progname;
178 else
179 progname = *argv;
180
181 maxpartitions = getmaxpartitions();
182 if (maxpartitions > 26)
183 fatal("insane maxpartitions value %d", maxpartitions);
184
185 opstring = "AB:DFI:LNb:f:M:O:i:m:r:s:v:";
186
187 debug = force = segsize = start = 0;
188 while ((ch = getopt(argc, argv, opstring)) != -1)
189 switch(ch) {
190 case 'A': /* Adaptively configure segment size */
191 segsize = -1;
192 break;
193 case 'B': /* LFS segment size */
194 if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
195 fatal("%s: bad segment size", optarg);
196 break;
197 case 'D':
198 debug = 1;
199 break;
200 case 'F':
201 force = 1;
202 break;
203 case 'I':
204 interleave = atoi(optarg);
205 break;
206 case 'L': /* Compatibility only */
207 break;
208 case 'M':
209 minfreeseg = atoi(optarg);
210 break;
211 case 'N':
212 Nflag++;
213 break;
214 case 'O':
215 start = atoi(optarg);
216 break;
217 #ifdef COMPAT
218 case 'T':
219 disktype = optarg;
220 break;
221 #endif
222 case 'b':
223 if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
224 fatal("%s: bad block size", optarg);
225 break;
226 case 'f':
227 if ((fsize = atoi(optarg)) <= 0)
228 fatal("%s: bad frag size", optarg);
229 break;
230 case 'i':
231 if ((ibsize = atoi(optarg)) <= 0)
232 fatal("%s: bad inode block size", optarg);
233 break;
234 case 'm':
235 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
236 fatal("%s: bad free space %%\n", optarg);
237 break;
238 case 'r':
239 if ((roll_id = strtoul(optarg, NULL, 0)) == 0)
240 fatal("%s: bad roll-forward id\n", optarg);
241 break;
242 case 's':
243 if ((fssize = atoi(optarg)) <= 0)
244 fatal("%s: bad file system size", optarg);
245 break;
246 case 'v':
247 version = atoi(optarg);
248 if (version <= 0 || version > LFS_VERSION)
249 fatal("%s: bad version", optarg);
250 break;
251 case '?':
252 default:
253 usage();
254 }
255 argc -= optind;
256 argv += optind;
257
258 if (argc != 2 && argc != 1)
259 usage();
260
261 /*
262 * If the -N flag isn't specified, open the output file. If no path
263 * prefix, try /dev/r%s and then /dev/%s.
264 */
265 special = argv[0];
266 if (strchr(special, '/') == NULL) {
267 (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
268 special);
269 if (stat(device, &st) == -1)
270 (void)snprintf(device, sizeof(device), "%s%s",
271 _PATH_DEV, special);
272 special = device;
273 }
274 if (!Nflag) {
275 fso = open(special,
276 (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
277 if (fso < 0)
278 fatal("%s: %s", special, strerror(errno));
279 } else
280 fso = -1;
281
282 /* Open the input file. */
283 fsi = open(special, O_RDONLY);
284 if (fsi < 0)
285 fatal("%s: %s", special, strerror(errno));
286 if (fstat(fsi, &st) < 0)
287 fatal("%s: %s", special, strerror(errno));
288
289
290 if (!debug && !S_ISCHR(st.st_mode))
291 (void)printf("%s: %s: not a character-special device\n",
292 progname, special);
293 cp = strchr(argv[0], '\0') - 1;
294 if (!debug
295 && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
296 && !isdigit(*cp))))
297 fatal("%s: can't figure out file system partition", argv[0]);
298
299 #ifdef COMPAT
300 if (disktype == NULL)
301 disktype = argv[1];
302 #endif
303 if (debug)
304 lp = debug_readlabel(fsi);
305 else
306 lp = getdisklabel(special, fsi);
307
308 if (isdigit(*cp))
309 pp = &lp->d_partitions[0];
310 else
311 pp = &lp->d_partitions[*cp - 'a'];
312 if (pp->p_size == 0)
313 fatal("%s: `%c' partition is unavailable", argv[0], *cp);
314
315 /* If force, make the partition look like an LFS */
316 if(force) {
317 pp->p_fstype = FS_BSDLFS;
318 /* 0 means to use defaults */
319 pp->p_fsize = 0;
320 pp->p_frag = 0;
321 pp->p_sgs = 0;
322 }
323
324 /* Try autoconfiguring segment size, if asked to */
325 if (segsize == -1)
326 segsize = auto_segsize(fsi, dbtob(pp->p_size), version);
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(s, fd)
342 char *s;
343 int fd;
344 {
345 static struct disklabel lab;
346
347 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
348 #ifdef COMPAT
349 if (disktype) {
350 struct disklabel *lp;
351
352 unlabeled++;
353 lp = getdiskbyname(disktype);
354 if (lp == NULL)
355 fatal("%s: unknown disk type", disktype);
356 return (lp);
357 }
358 #endif
359 (void)fprintf(stderr,
360 "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
361 fatal(lmsg, s);
362 }
363 return (&lab);
364 }
365
366
367 static struct disklabel *
368 debug_readlabel(fd)
369 int fd;
370 {
371 static struct disklabel lab;
372 int n;
373
374 if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
375 fatal("unable to read disk label: %s", strerror(errno));
376 else if (n < sizeof(struct disklabel))
377 fatal("short read of disklabel: %d of %ld bytes", n,
378 (u_long) sizeof(struct disklabel));
379 return(&lab);
380 }
381
382 #ifdef notdef
383 static void
384 rewritelabel(s, fd, lp)
385 char *s;
386 int fd;
387 struct disklabel *lp;
388 {
389 #ifdef COMPAT
390 if (unlabeled)
391 return;
392 #endif
393 lp->d_checksum = 0;
394 lp->d_checksum = dkcksum(lp);
395 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
396 (void)fprintf(stderr,
397 "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
398 fatal("%s: can't rewrite disk label", s);
399 }
400 #if __vax__
401 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
402 int i;
403 int cfd;
404 daddr_t alt;
405 char specname[64];
406 char blk[1024];
407 char *cp;
408
409 /*
410 * Make name for 'c' partition.
411 */
412 strcpy(specname, s);
413 cp = specname + strlen(specname) - 1;
414 if (!isdigit(*cp))
415 *cp = 'c';
416 cfd = open(specname, O_WRONLY);
417 if (cfd < 0)
418 fatal("%s: %s", specname, strerror(errno));
419 memset(blk, 0, sizeof(blk));
420 *(struct disklabel *)(blk + LABELOFFSET) = *lp;
421 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
422 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
423 if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
424 SEEK_SET) == -1)
425 fatal("lseek to badsector area: %s",
426 strerror(errno));
427 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
428 fprintf(stderr,
429 "%s: alternate label %d write: %s\n",
430 progname, i/2, strerror(errno));
431 }
432 close(cfd);
433 }
434 #endif /* vax */
435 }
436 #endif /* notdef */
437
438 void
439 usage()
440 {
441 fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
442 fprintf(stderr, "where fsoptions are:\n");
443 fprintf(stderr, "\t-A (autoconfigure segment size)\n");
444 fprintf(stderr, "\t-B segment size in bytes\n");
445 fprintf(stderr, "\t-D (debug)\n");
446 fprintf(stderr,
447 "\t-N (do not create file system, just print out parameters)\n");
448 fprintf(stderr, "\t-O first segment offset in sectors\n");
449 fprintf(stderr, "\t-b block size in bytes\n");
450 fprintf(stderr, "\t-f frag size in bytes\n");
451 fprintf(stderr, "\t-m minimum free space %%\n");
452 fprintf(stderr, "\t-s file system size in sectors\n");
453 fprintf(stderr, "\t-v version\n");
454 exit(1);
455 }
456