1 /* $NetBSD: newfs.c,v 1.29 2013/06/18 18:18:58 christos 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\ 35 The Regents of the University of California. All rights reserved."); 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.29 2013/06/18 18:18:58 christos 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/file.h> 54 #include <sys/mount.h> 55 #include <sys/sysctl.h> 56 #include <sys/time.h> 57 #include <sys/disk.h> 58 59 #include <ufs/lfs/lfs.h> 60 61 #include <err.h> 62 #include <errno.h> 63 #include <unistd.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <ctype.h> 67 #include <string.h> 68 #include <paths.h> 69 #include <util.h> 70 #include "config.h" 71 #include "extern.h" 72 #include "bufcache.h" 73 #include "partutil.h" 74 75 #define COMPAT /* allow non-labeled disks */ 76 77 #ifdef COMPAT 78 const char lmsg[] = "%s: can't read disk label; disk type must be specified"; 79 #else 80 const char lmsg[] = "%s: can't read disk label"; 81 #endif 82 83 int Nflag = 0; /* run without writing file system */ 84 int fssize; /* file system size */ 85 int sectorsize; /* bytes/sector */ 86 int fsize = 0; /* fragment size */ 87 int bsize = 0; /* block size */ 88 int ibsize = 0; /* inode block size */ 89 int interleave = 0; /* segment interleave */ 90 int minfree = MINFREE; /* free space threshold */ 91 int minfreeseg = 0; /* segments not counted in bfree total */ 92 int resvseg = 0; /* free segments reserved for the cleaner */ 93 u_int32_t roll_id = 0; /* roll-forward id */ 94 u_long memleft; /* virtual memory available */ 95 caddr_t membase; /* start address of memory based filesystem */ 96 #ifdef COMPAT 97 char *disktype; 98 #endif 99 int preen = 0; /* Coexistence with fsck_lfs */ 100 101 char device[MAXPATHLEN]; 102 char *progname, *special; 103 104 extern long dev_bsize; /* device block size */ 105 106 static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *); 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()) * (btodb(len))) / ((off_t)RAND_MAX + 1); 139 if (pread(fd, buf, dbtob(1), dbtob(off)) < 0) 140 err(1, "pread"); 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 version, ch; 166 struct disk_geom geo; 167 struct dkwedge_info dkw; 168 struct stat st; 169 int debug, force, fsi, fso, lfs_segsize, maxpartitions; 170 uint secsize = 0; 171 daddr_t start; 172 const char *opstring; 173 int byte_sized = 0; 174 int r; 175 176 version = DFL_VERSION; /* what version of lfs to make */ 177 178 if ((progname = strrchr(*argv, '/')) != NULL) 179 ++progname; 180 else 181 progname = *argv; 182 183 maxpartitions = getmaxpartitions(); 184 if (maxpartitions > 26) 185 fatal("insane maxpartitions value %d", maxpartitions); 186 187 opstring = "AB:b:DFf:I:i:LM:m:NO:R:r:S:s:v:"; 188 189 debug = force = lfs_segsize = start = 0; 190 while ((ch = getopt(argc, argv, opstring)) != -1) 191 switch(ch) { 192 case 'A': /* Adaptively configure segment size */ 193 lfs_segsize = -1; 194 break; 195 case 'B': /* LFS segment size */ 196 lfs_segsize = strsuftoi64("segment size", optarg, LFS_MINSEGSIZE, INT64_MAX, NULL); 197 break; 198 case 'D': 199 debug = 1; 200 break; 201 case 'F': 202 force = 1; 203 break; 204 case 'I': 205 interleave = strsuftoi64("interleave", optarg, 0, INT64_MAX, NULL); 206 break; 207 case 'L': /* Compatibility only */ 208 break; 209 case 'M': 210 minfreeseg = strsuftoi64("minfreeseg", optarg, 0, INT64_MAX, NULL); 211 break; 212 case 'N': 213 Nflag++; 214 break; 215 case 'O': 216 start = strsuftoi64("start", optarg, 0, INT64_MAX, NULL); 217 break; 218 case 'R': 219 resvseg = strsuftoi64("resvseg", optarg, 0, INT64_MAX, NULL); 220 break; 221 case 'S': 222 secsize = strsuftoi64("sector size", optarg, 1, INT64_MAX, NULL); 223 if (secsize <= 0 || (secsize & (secsize - 1))) 224 fatal("%s: bad sector size", optarg); 225 break; 226 #ifdef COMPAT 227 case 'T': 228 disktype = optarg; 229 break; 230 #endif 231 case 'b': 232 bsize = strsuftoi64("block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL); 233 break; 234 case 'f': 235 fsize = strsuftoi64("fragment size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL); 236 break; 237 case 'i': 238 ibsize = strsuftoi64("inode block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL); 239 break; 240 case 'm': 241 minfree = strsuftoi64("free space %", optarg, 0, 99, NULL); 242 break; 243 case 'r': 244 roll_id = strsuftoi64("roll-forward id", optarg, 1, UINT_MAX, NULL); 245 break; 246 case 's': 247 fssize = strsuftoi64("file system size", optarg, 0, INT64_MAX, &byte_sized); 248 break; 249 case 'v': 250 version = strsuftoi64("file system version", optarg, 1, LFS_VERSION, NULL); 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, O_RDWR, DEFFILEMODE); 277 if (debug && fso < 0) { 278 /* Create a file of the requested size. */ 279 fso = open(special, O_CREAT | O_RDWR, DEFFILEMODE); 280 if (fso >= 0) { 281 char buf[512]; 282 int i; 283 for (i = 0; i < fssize; i++) 284 write(fso, buf, sizeof(buf)); 285 lseek(fso, 0, SEEK_SET); 286 } 287 } 288 if (fso < 0) 289 fatal("%s: %s", special, strerror(errno)); 290 } else 291 fso = -1; 292 293 /* Open the input file. */ 294 fsi = open(special, O_RDONLY); 295 if (fsi < 0) 296 fatal("%s: %s", special, strerror(errno)); 297 if (fstat(fsi, &st) < 0) 298 fatal("%s: %s", special, strerror(errno)); 299 300 301 if (!S_ISCHR(st.st_mode)) { 302 if (!S_ISREG(st.st_mode)) { 303 fatal("%s: neither a character special device " 304 "nor a regular file", special); 305 } 306 (void)strcpy(dkw.dkw_ptype, DKW_PTYPE_LFS); 307 if (secsize == 0) 308 secsize = 512; 309 dkw.dkw_size = st.st_size / secsize; 310 } else { 311 #ifdef COMPAT 312 if (disktype == NULL) 313 disktype = argv[1]; 314 #endif 315 if (getdiskinfo(special, fsi, disktype, &geo, &dkw) == -1) 316 errx(1, lmsg, special); 317 318 if (dkw.dkw_size == 0) 319 fatal("%s: is zero sized", argv[0]); 320 if (!force && strcmp(dkw.dkw_ptype, DKW_PTYPE_LFS) != 0) 321 fatal("%s: is not `%s', but `%s'", argv[0], 322 DKW_PTYPE_LFS, dkw.dkw_ptype); 323 } 324 325 if (secsize == 0) 326 secsize = geo.dg_secsize; 327 328 /* Make device block size available to low level routines */ 329 dev_bsize = secsize; 330 331 /* From here on out fssize is in sectors */ 332 if (byte_sized) { 333 fssize /= secsize; 334 } 335 336 /* If force, make the partition look like an LFS */ 337 if (force) { 338 (void)strcpy(dkw.dkw_ptype, DKW_PTYPE_LFS); 339 if (fssize) { 340 dkw.dkw_size = fssize; 341 } 342 } else 343 if (fssize != 0 && fssize < dkw.dkw_size) 344 dkw.dkw_size = fssize; 345 346 /* Try autoconfiguring segment size, if asked to */ 347 if (lfs_segsize == -1) { 348 if (!S_ISCHR(st.st_mode)) { 349 warnx("%s is not a character special device, ignoring -A", special); 350 lfs_segsize = 0; 351 } else 352 lfs_segsize = auto_segsize(fsi, dkw.dkw_size / secsize, 353 version); 354 } 355 356 /* If we're making a LFS, we break out here */ 357 r = make_lfs(fso, secsize, &dkw, minfree, bsize, fsize, lfs_segsize, 358 minfreeseg, resvseg, version, start, ibsize, interleave, roll_id); 359 if (debug) 360 bufstats(); 361 exit(r); 362 } 363 364 static int64_t 365 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix) 366 { 367 int64_t result, r1; 368 int shift = 0; 369 char *ep; 370 371 errno = 0; 372 r1 = strtoll(arg, &ep, 10); 373 if (ep[0] != '\0' && ep[1] != '\0') 374 errx(1, "%s `%s' is not a valid number.", desc, arg); 375 switch (ep[0]) { 376 case '\0': 377 case 's': case 'S': 378 if (num_suffix != NULL) 379 *num_suffix = 0; 380 break; 381 case 'g': case 'G': 382 shift += 10; 383 /* FALLTHROUGH */ 384 case 'm': case 'M': 385 shift += 10; 386 /* FALLTHROUGH */ 387 case 'k': case 'K': 388 shift += 10; 389 /* FALLTHROUGH */ 390 case 'b': case 'B': 391 if (num_suffix != NULL) 392 *num_suffix = 1; 393 break; 394 default: 395 errx(1, "`%s' is not a valid suffix for %s.", ep, desc); 396 } 397 result = r1 << shift; 398 if (errno == ERANGE || result >> shift != r1) 399 errx(1, "%s `%s' is too large to convert.", desc, arg); 400 if (result < min) 401 errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").", 402 desc, arg, result, min); 403 if (result > max) 404 errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").", 405 desc, arg, result, max); 406 return result; 407 } 408 409 void 410 usage() 411 { 412 fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n"); 413 fprintf(stderr, "where fsoptions are:\n"); 414 fprintf(stderr, "\t-A (autoconfigure segment size)\n"); 415 fprintf(stderr, "\t-B segment size in bytes\n"); 416 fprintf(stderr, "\t-D (debug)\n"); 417 fprintf(stderr, "\t-M count of segments not counted in bfree\n"); 418 fprintf(stderr, 419 "\t-N (do not create file system, just print out parameters)\n"); 420 fprintf(stderr, "\t-O first segment offset in sectors\n"); 421 fprintf(stderr, "\t-R count of segments reserved for the cleaner\n"); 422 fprintf(stderr, "\t-b block size in bytes\n"); 423 fprintf(stderr, "\t-f frag size in bytes\n"); 424 fprintf(stderr, "\t-m minimum free space %%\n"); 425 fprintf(stderr, "\t-s file system size in sectors\n"); 426 fprintf(stderr, "\t-v version\n"); 427 exit(1); 428 } 429