newfs.c revision 1.15.2.1 1 /* $NetBSD: newfs.c,v 1.15.2.1 2005/05/07 11:21:29 tron 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.15.2.1 2005/05/07 11:21:29 tron 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 #include "bufcache.h"
76
77 #define COMPAT /* allow non-labeled disks */
78
79 int Nflag = 0; /* run without writing file system */
80 int fssize; /* file system size */
81 int sectorsize; /* bytes/sector */
82 int fsize = 0; /* fragment size */
83 int bsize = 0; /* block size */
84 int ibsize = 0; /* inode block size */
85 int interleave = 0; /* segment interleave */
86 int minfree = MINFREE; /* free space threshold */
87 int minfreeseg = 0; /* free segments reserved for the cleaner */
88 u_int32_t roll_id = 0; /* roll-forward id */
89 u_long memleft; /* virtual memory available */
90 caddr_t membase; /* start address of memory based filesystem */
91 #ifdef COMPAT
92 char *disktype;
93 int unlabeled;
94 #endif
95 int preen = 0; /* Coexistence with fsck_lfs */
96
97 char device[MAXPATHLEN];
98 char *progname, *special;
99
100 static struct disklabel *getdisklabel(char *, int);
101 static struct disklabel *debug_readlabel(int);
102 #ifdef notdef
103 static void rewritelabel(char *, int, struct disklabel *);
104 #endif
105 static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
106 static void usage(void);
107
108 /* CHUNKSIZE should be larger than MAXPHYS */
109 #define CHUNKSIZE (1024 * 1024)
110
111 static size_t
112 auto_segsize(int fd, off_t len, int version)
113 {
114 off_t off, bw;
115 time_t start, finish;
116 char buf[CHUNKSIZE];
117 long seeks;
118 size_t final;
119 int i;
120
121 /* First, get sequential access bandwidth */
122 time(&start);
123 finish = start;
124 for (off = 0; finish - start < 10; off += CHUNKSIZE) {
125 if (pread(fd, buf, CHUNKSIZE, off) < 0)
126 break;
127 time(&finish);
128 }
129 /* Bandwidth = bytes / sec */
130 /* printf("%ld bytes in %ld seconds\n", (long)off, (long)(finish - start)); */
131 bw = off / (finish - start);
132
133 /* Second, seek time */
134 time(&start);
135 finish = start; /* structure copy */
136 for (seeks = 0; finish - start < 10; ) {
137 off = (((double)rand()) * len) / (off_t)RAND_MAX;
138 if (pread(fd, buf, dbtob(1), off) < 0)
139 break;
140 time(&finish);
141 ++seeks;
142 }
143 /* printf("%ld seeks in %ld seconds\n", (long)seeks, (long)(finish - start)); */
144 /* Seek time in units/sec */
145 seeks /= (finish - start);
146 if (seeks == 0)
147 seeks = 1;
148
149 printf("bw = %ld B/s, seek time %ld ms (%ld seeks/s)\n",
150 (long)bw, 1000/seeks, seeks);
151 final = dbtob(btodb(4 * bw / seeks));
152 if (version == 1) {
153 for (i = 0; final; final >>= 1, i++)
154 ;
155 final = 1 << i;
156 }
157 printf("using initial segment size %ld\n", (long)final);
158 return final;
159 }
160
161 int
162 main(int argc, char **argv)
163 {
164 int version, ch;
165 struct partition *pp;
166 struct disklabel *lp;
167 struct stat st;
168 int debug, force, fsi, fso, segsize, maxpartitions;
169 uint secsize = 0;
170 daddr_t start;
171 char *cp, *opstring;
172 int byte_sized = 0;
173 int r;
174
175 version = DFL_VERSION; /* what version of lfs to make */
176
177 if ((progname = strrchr(*argv, '/')) != NULL)
178 ++progname;
179 else
180 progname = *argv;
181
182 maxpartitions = getmaxpartitions();
183 if (maxpartitions > 26)
184 fatal("insane maxpartitions value %d", maxpartitions);
185
186 opstring = "AB:b:DFf:I:i:LM:m:NO:r:S:s:v:";
187
188 debug = force = segsize = start = 0;
189 while ((ch = getopt(argc, argv, opstring)) != -1)
190 switch(ch) {
191 case 'A': /* Adaptively configure segment size */
192 segsize = -1;
193 break;
194 case 'B': /* LFS segment size */
195 segsize = strsuftoi64("segment size", optarg, LFS_MINSEGSIZE, INT64_MAX, NULL);
196 break;
197 case 'D':
198 debug = 1;
199 break;
200 case 'F':
201 force = 1;
202 break;
203 case 'I':
204 interleave = strsuftoi64("interleave", optarg, 0, INT64_MAX, NULL);
205 break;
206 case 'L': /* Compatibility only */
207 break;
208 case 'M':
209 minfreeseg = strsuftoi64("minfreeseg", optarg, 0, INT64_MAX, NULL);
210 break;
211 case 'N':
212 Nflag++;
213 break;
214 case 'O':
215 start = strsuftoi64("start", optarg, 0, INT64_MAX, NULL);
216 break;
217 case 'S':
218 secsize = strsuftoi64("sector size", optarg, 1, INT64_MAX, NULL);
219 if (secsize <= 0 || (secsize & (secsize - 1)))
220 fatal("%s: bad sector size", optarg);
221 break;
222 #ifdef COMPAT
223 case 'T':
224 disktype = optarg;
225 break;
226 #endif
227 case 'b':
228 bsize = strsuftoi64("block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
229 break;
230 case 'f':
231 fsize = strsuftoi64("fragment size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
232 break;
233 case 'i':
234 ibsize = strsuftoi64("inode block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
235 break;
236 case 'm':
237 minfree = strsuftoi64("free space %", optarg, 0, 99, NULL);
238 break;
239 case 'r':
240 roll_id = strsuftoi64("roll-forward id", optarg, 1, UINT_MAX, NULL);
241 break;
242 case 's':
243 fssize = strsuftoi64("file system size", optarg, 0, INT64_MAX, &byte_sized);
244 break;
245 case 'v':
246 version = strsuftoi64("file system version", optarg, 1, LFS_VERSION, NULL);
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_RDWR, 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 (!S_ISCHR(st.st_mode)) {
288 if (debug) {
289 lp = debug_readlabel(fsi);
290 pp = &lp->d_partitions[0];
291 } else {
292 static struct partition dummy_pp;
293 lp = NULL;
294 pp = &dummy_pp;
295 pp->p_fstype = FS_BSDLFS;
296 if (secsize == 0)
297 secsize = 512;
298 pp->p_size = st.st_size / secsize;
299 }
300 } else {
301 cp = strchr(argv[0], '\0') - 1;
302 if (!debug
303 && ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
304 && !isdigit((unsigned char)*cp)))
305 fatal("%s: can't figure out file system partition", argv[0]);
306
307 #ifdef COMPAT
308 if (disktype == NULL)
309 disktype = argv[1];
310 #endif
311 lp = getdisklabel(special, fsi);
312
313 if (isdigit((unsigned char)*cp))
314 pp = &lp->d_partitions[0];
315 else
316 pp = &lp->d_partitions[*cp - 'a'];
317 if (pp->p_size == 0)
318 fatal("%s: `%c' partition is unavailable", argv[0], *cp);
319 }
320
321 if (secsize == 0)
322 secsize = lp->d_secsize;
323
324 /* From here on out fssize is in sectors */
325 if (byte_sized) {
326 fssize /= secsize;
327 }
328
329 /* If force, make the partition look like an LFS */
330 if (force) {
331 pp->p_fstype = FS_BSDLFS;
332 pp->p_size = fssize;
333 /* 0 means to use defaults */
334 pp->p_fsize = 0;
335 pp->p_frag = 0;
336 pp->p_sgs = 0;
337 } else
338 if (fssize != 0 && fssize < pp->p_size)
339 pp->p_size = fssize;
340
341 /* Try autoconfiguring segment size, if asked to */
342 if (segsize == -1) {
343 if (!S_ISCHR(st.st_mode)) {
344 warnx("%s is not a character special device, ignoring -A", special);
345 segsize = 0;
346 } else
347 segsize = auto_segsize(fsi, dbtob(pp->p_size), version);
348 }
349
350 /* If we're making a LFS, we break out here */
351 r = make_lfs(fso, secsize, pp, minfree, bsize, fsize, segsize,
352 minfreeseg, version, start, ibsize, interleave,
353 roll_id);
354 if (debug)
355 bufstats();
356 exit(r);
357 }
358
359 #ifdef COMPAT
360 char lmsg[] = "%s: can't read disk label; disk type must be specified";
361 #else
362 char lmsg[] = "%s: can't read disk label";
363 #endif
364
365 static struct disklabel *
366 getdisklabel(char *s, int fd)
367 {
368 static struct disklabel lab;
369
370 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
371 #ifdef COMPAT
372 if (disktype) {
373 struct disklabel *lp;
374
375 unlabeled++;
376 lp = getdiskbyname(disktype);
377 if (lp == NULL)
378 fatal("%s: unknown disk type", disktype);
379 return (lp);
380 }
381 #endif
382 (void)fprintf(stderr,
383 "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
384 fatal(lmsg, s);
385 }
386 return (&lab);
387 }
388
389
390 static struct disklabel *
391 debug_readlabel(int fd)
392 {
393 static struct disklabel lab;
394 int n;
395
396 if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
397 fatal("unable to read disk label: %s", strerror(errno));
398 else if (n < sizeof(struct disklabel))
399 fatal("short read of disklabel: %d of %ld bytes", n,
400 (u_long) sizeof(struct disklabel));
401 return(&lab);
402 }
403
404 #ifdef notdef
405 static void
406 rewritelabel(char *s, int fd, struct disklabel *lp)
407 {
408 #ifdef COMPAT
409 if (unlabeled)
410 return;
411 #endif
412 lp->d_checksum = 0;
413 lp->d_checksum = dkcksum(lp);
414 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
415 (void)fprintf(stderr,
416 "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
417 fatal("%s: can't rewrite disk label", s);
418 }
419 #if __vax__
420 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
421 int i;
422 int cfd;
423 daddr_t alt;
424 off_t loff;
425 char specname[64];
426 char blk[1024];
427 char *cp;
428
429 /*
430 * Make name for 'c' partition.
431 */
432 strlcpy(specname, s, sizeof(specname));
433 cp = specname + strlen(specname) - 1;
434 if (!isdigit(*cp))
435 *cp = 'c';
436 cfd = open(specname, O_WRONLY);
437 if (cfd < 0)
438 fatal("%s: %s", specname, strerror(errno));
439 if ((loff = getlabeloffset()) < 0)
440 fatal("getlabeloffset: %s", strerror(errno));
441 memset(blk, 0, sizeof(blk));
442 *(struct disklabel *)(blk + loff) = *lp;
443 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
444 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
445 if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
446 SEEK_SET) == -1)
447 fatal("lseek to badsector area: %s",
448 strerror(errno));
449 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
450 fprintf(stderr,
451 "%s: alternate label %d write: %s\n",
452 progname, i/2, strerror(errno));
453 }
454 close(cfd);
455 }
456 #endif /* vax */
457 }
458 #endif /* notdef */
459
460 static int64_t
461 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix)
462 {
463 int64_t result, r1;
464 int shift = 0;
465 char *ep;
466
467 errno = 0;
468 r1 = strtoll(arg, &ep, 10);
469 if (ep[0] != '\0' && ep[1] != '\0')
470 errx(1, "%s `%s' is not a valid number.", desc, arg);
471 switch (ep[0]) {
472 case '\0':
473 case 's': case 'S':
474 if (num_suffix != NULL)
475 *num_suffix = 0;
476 break;
477 case 'g': case 'G':
478 shift += 10;
479 /* FALLTHROUGH */
480 case 'm': case 'M':
481 shift += 10;
482 /* FALLTHROUGH */
483 case 'k': case 'K':
484 shift += 10;
485 /* FALLTHROUGH */
486 case 'b': case 'B':
487 if (num_suffix != NULL)
488 *num_suffix = 1;
489 break;
490 default:
491 errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
492 }
493 result = r1 << shift;
494 if (errno == ERANGE || result >> shift != r1)
495 errx(1, "%s `%s' is too large to convert.", desc, arg);
496 if (result < min)
497 errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").",
498 desc, arg, result, min);
499 if (result > max)
500 errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").",
501 desc, arg, result, max);
502 return result;
503 }
504
505 void
506 usage()
507 {
508 fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
509 fprintf(stderr, "where fsoptions are:\n");
510 fprintf(stderr, "\t-A (autoconfigure segment size)\n");
511 fprintf(stderr, "\t-B segment size in bytes\n");
512 fprintf(stderr, "\t-D (debug)\n");
513 fprintf(stderr,
514 "\t-N (do not create file system, just print out parameters)\n");
515 fprintf(stderr, "\t-O first segment offset in sectors\n");
516 fprintf(stderr, "\t-b block size in bytes\n");
517 fprintf(stderr, "\t-f frag size in bytes\n");
518 fprintf(stderr, "\t-m minimum free space %%\n");
519 fprintf(stderr, "\t-s file system size in sectors\n");
520 fprintf(stderr, "\t-v version\n");
521 exit(1);
522 }
523