1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 /* 30 * University Copyright- Copyright (c) 1982, 1986, 1988 31 * The Regents of the University of California 32 * All Rights Reserved 33 * 34 * University Acknowledgment- Portions of this document are derived from 35 * software developed by the University of California, Berkeley, and its 36 * contributors. 37 */ 38 39 #pragma ident "%Z%%M% %I% %E% SMI" 40 41 42 /* 43 * The maximum supported file system size (in sectors) is the 44 * number of frags that can be represented in an int32_t field 45 * (INT_MAX) times the maximum number of sectors per frag. Since 46 * the maximum frag size is MAXBSIZE, the maximum number of sectors 47 * per frag is MAXBSIZE/DEV_BSIZE. 48 */ 49 #define FS_MAX (((diskaddr_t)INT_MAX) * (MAXBSIZE/DEV_BSIZE)) 50 51 /* 52 * make file system for cylinder-group style file systems 53 * 54 * usage: 55 * 56 * mkfs [-F FSType] [-V] [-G [-P]] [-M dirname] [-m] [options] 57 * [-o specific_options] special size 58 * [nsect ntrack bsize fsize cpg minfree rps nbpi opt apc rotdelay 59 * 2 3 4 5 6 7 8 9 10 11 12 60 * nrpos maxcontig mtb] 61 * 13 14 15 62 * 63 * where specific_options are: 64 * N - no create 65 * nsect - The number of sectors per track 66 * ntrack - The number of tracks per cylinder 67 * bsize - block size 68 * fragsize - fragment size 69 * cgsize - The number of disk cylinders per cylinder group. 70 * free - minimum free space 71 * rps - rotational speed (rev/sec). 72 * nbpi - number of data bytes per allocated inode 73 * opt - optimization (space, time) 74 * apc - number of alternates 75 * gap - gap size 76 * nrpos - number of rotational positions 77 * maxcontig - maximum number of logical blocks that will be 78 * allocated contiguously before inserting rotational delay 79 * mtb - if "y", set up file system for eventual growth to over a 80 * a terabyte 81 * -P Do not grow the file system, but print on stdout the maximal 82 * size in sectors to which the file system can be increased. The calculated 83 * size is limited by the value provided by the operand size. 84 * 85 * Note that -P is a project-private interface and together with -G intended 86 * to be used only by the growfs script. It is therefore purposely not 87 * documented in the man page. 88 * The -P option is covered by PSARC case 2003/422. 89 */ 90 91 /* 92 * The following constants set the defaults used for the number 93 * of sectors/track (fs_nsect), and number of tracks/cyl (fs_ntrak). 94 * 95 * NSECT NTRAK 96 * 72MB CDC 18 9 97 * 30MB CDC 18 5 98 * 720KB Diskette 9 2 99 * 100 * However the defaults will be different for disks larger than CHSLIMIT. 101 */ 102 103 #define DFLNSECT 32 104 #define DFLNTRAK 16 105 106 /* 107 * The following default sectors and tracks values are used for 108 * non-efi disks that are larger than the CHS addressing limit. The 109 * existing default cpg of 16 (DESCPG) holds good for larger disks too. 110 */ 111 #define DEF_SECTORS_EFI 128 112 #define DEF_TRACKS_EFI 48 113 114 /* 115 * The maximum number of cylinders in a group depends upon how much 116 * information can be stored on a single cylinder. The default is to 117 * use 16 cylinders per group. This is effectively tradition - it was 118 * the largest value acceptable under SunOs 4.1 119 */ 120 #define DESCPG 16 /* desired fs_cpg */ 121 122 /* 123 * The following two constants set the default block and fragment sizes. 124 * Both constants must be a power of 2 and meet the following constraints: 125 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 126 * DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE 127 * DESBLKSIZE / DESFRAGSIZE <= 8 128 */ 129 #define DESBLKSIZE 8192 130 #define DESFRAGSIZE 1024 131 132 /* 133 * MINFREE gives the minimum acceptable percentage of file system 134 * blocks which may be free. If the freelist drops below this level 135 * only the superuser may continue to allocate blocks. This may 136 * be set to 0 if no reserve of free blocks is deemed necessary, 137 * however throughput drops by fifty percent if the file system 138 * is run at between 90% and 100% full; thus the default value of 139 * fs_minfree is 10%. With 10% free space, fragmentation is not a 140 * problem, so we choose to optimize for time. 141 */ 142 #define MINFREE 10 143 #define DEFAULTOPT FS_OPTTIME 144 145 /* 146 * ROTDELAY gives the minimum number of milliseconds to initiate 147 * another disk transfer on the same cylinder. It is no longer used 148 * and will always default to 0. 149 */ 150 #define ROTDELAY 0 151 152 /* 153 * MAXBLKPG determines the maximum number of data blocks which are 154 * placed in a single cylinder group. The default is one indirect 155 * block worth of data blocks. 156 */ 157 #define MAXBLKPG(bsize) ((bsize) / sizeof (daddr32_t)) 158 159 /* 160 * Each file system has a number of inodes statically allocated. 161 * We allocate one inode slot per NBPI bytes, expecting this 162 * to be far more than we will ever need. 163 */ 164 #define NBPI 2048 /* Number Bytes Per Inode */ 165 #define MTB_NBPI (MB) /* Number Bytes Per Inode for multi-terabyte */ 166 167 /* 168 * Disks are assumed to rotate at 60HZ, unless otherwise specified. 169 */ 170 #define DEFHZ 60 171 172 /* 173 * Cylinder group related limits. 174 * 175 * For each cylinder we keep track of the availability of blocks at different 176 * rotational positions, so that we can lay out the data to be picked 177 * up with minimum rotational latency. NRPOS is the number of rotational 178 * positions which we distinguish. With NRPOS 8 the resolution of our 179 * summary information is 2ms for a typical 3600 rpm drive. 180 */ 181 #define NRPOS 8 /* number distinct rotational positions */ 182 183 #ifdef DEBUG 184 #define dprintf(x) printf x 185 #else 186 #define dprintf(x) 187 #endif 188 189 /* 190 * For the -N option, when calculating the backup superblocks, do not print 191 * them if we are not really sure. We may have to try an alternate method of 192 * arriving at the superblocks. So defer printing till a handful of superblocks 193 * look good. 194 */ 195 #define tprintf(x) if (Nflag && retry) \ 196 strncat(tmpbuf, x, strlen(x)); \ 197 else \ 198 (void) fprintf(stderr, x); 199 200 #define ALTSB 32 /* Location of first backup superblock */ 201 202 /* 203 * range_check "user_supplied" flag values. 204 */ 205 #define RC_DEFAULT 0 206 #define RC_KEYWORD 1 207 #define RC_POSITIONAL 2 208 209 /* 210 * ufs hole 211 */ 212 #define UFS_HOLE -1 213 214 #ifndef STANDALONE 215 #include <stdio.h> 216 #include <sys/mnttab.h> 217 #endif 218 219 #include <stdlib.h> 220 #include <unistd.h> 221 #include <malloc.h> 222 #include <string.h> 223 #include <strings.h> 224 #include <ctype.h> 225 #include <errno.h> 226 #include <sys/param.h> 227 #include <time.h> 228 #include <sys/types.h> 229 #include <sys/sysmacros.h> 230 #include <sys/vnode.h> 231 #include <sys/fs/ufs_fsdir.h> 232 #include <sys/fs/ufs_inode.h> 233 #include <sys/fs/ufs_fs.h> 234 #include <sys/fs/ufs_log.h> 235 #include <sys/mntent.h> 236 #include <sys/filio.h> 237 #include <limits.h> 238 #include <sys/int_const.h> 239 #include <signal.h> 240 #include <sys/efi_partition.h> 241 #include "roll_log.h" 242 243 #define bcopy(f, t, n) (void) memcpy(t, f, n) 244 #define bzero(s, n) (void) memset(s, 0, n) 245 #define bcmp(s, d, n) memcmp(s, d, n) 246 247 #define index(s, r) strchr(s, r) 248 #define rindex(s, r) strrchr(s, r) 249 250 #include <sys/stat.h> 251 #include <sys/statvfs.h> 252 #include <locale.h> 253 #include <fcntl.h> 254 #include <sys/isa_defs.h> /* for ENDIAN defines */ 255 #include <sys/vtoc.h> 256 257 #include <sys/dkio.h> 258 #include <sys/asynch.h> 259 260 extern offset_t llseek(); 261 extern char *getfullblkname(); 262 extern long lrand48(); 263 264 extern int optind; 265 extern char *optarg; 266 267 268 /* 269 * The size of a cylinder group is calculated by CGSIZE. The maximum size 270 * is limited by the fact that cylinder groups are at most one block. 271 * Its size is derived from the size of the maps maintained in the 272 * cylinder group and the (struct cg) size. 273 */ 274 #define CGSIZE(fs) \ 275 /* base cg */ (sizeof (struct cg) + \ 276 /* blktot size */ (fs)->fs_cpg * sizeof (long) + \ 277 /* blks size */ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof (short) + \ 278 /* inode map */ howmany((fs)->fs_ipg, NBBY) + \ 279 /* block map */ howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY)) 280 281 /* 282 * We limit the size of the inode map to be no more than a 283 * third of the cylinder group space, since we must leave at 284 * least an equal amount of space for the block map. 285 * 286 * N.B.: MAXIpG must be a multiple of INOPB(fs). 287 */ 288 #define MAXIpG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs)) 289 290 /* 291 * Same as MAXIpG, but parameterized by the block size (b) and the 292 * cylinder group divisor (d), which is the reciprocal of the fraction of the 293 * cylinder group overhead block that is used for the inode map. So for 294 * example, if d = 5, the macro's computation assumes that 1/5 of the 295 * cylinder group overhead block can be dedicated to the inode map. 296 */ 297 #define MAXIpG_B(b, d) roundup((b) * NBBY / (d), (b) / sizeof (struct dinode)) 298 299 #define UMASK 0755 300 #define MAXINOPB (MAXBSIZE / sizeof (struct dinode)) 301 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 302 #define MB (1024*1024) 303 #define BETWEEN(x, l, h) ((x) >= (l) && (x) <= (h)) 304 305 /* 306 * Used to set the inode generation number. Since both inodes and dinodes 307 * are dealt with, we really need a pointer to an icommon here. 308 */ 309 #define IRANDOMIZE(icp) (icp)->ic_gen = lrand48(); 310 311 /* 312 * Flags for number() 313 */ 314 #define ALLOW_PERCENT 0x01 /* allow trailing `%' on number */ 315 #define ALLOW_MS1 0x02 /* allow trailing `ms', state 1 */ 316 #define ALLOW_MS2 0x04 /* allow trailing `ms', state 2 */ 317 #define ALLOW_END_ONLY 0x08 /* must be at end of number & suffixes */ 318 319 #define MAXAIO 1000 /* maximum number of outstanding I/O's we'll manage */ 320 #define BLOCK 1 /* block in aiowait */ 321 #define NOBLOCK 0 /* don't block in aiowait */ 322 323 #define RELEASE 1 /* free an aio buffer after use */ 324 #define SAVE 0 /* don't free the buffer */ 325 326 typedef struct aio_trans { 327 aio_result_t resultbuf; 328 diskaddr_t bno; 329 char *buffer; 330 int size; 331 int release; 332 struct aio_trans *next; 333 } aio_trans; 334 335 typedef struct aio_results { 336 int max; 337 int outstanding; 338 int maxpend; 339 aio_trans *trans; 340 } aio_results; 341 342 int aio_inited = 0; 343 aio_results results; 344 345 /* 346 * Allow up to MAXBUF aio requests that each have a unique buffer. 347 * More aio's might be done, but not using memory through the getbuf() 348 * interface. This can be raised, but you run into the potential of 349 * using more memory than is physically available on the machine, 350 * and if you start swapping, you can forget about performance. 351 * To prevent this, we also limit the total memory used for a given 352 * type of buffer to MAXBUFMEM. 353 * 354 * Tests indicate a cylinder group's worth of inodes takes: 355 * 356 * NBPI Size of Inode Buffer 357 * 2k 1688k 358 * 8k 424k 359 * 360 * initcg() stores all the inodes for a cylinder group in one buffer, 361 * so allowing 20 buffers could take 32 MB if not limited by MAXBUFMEM. 362 */ 363 #define MAXBUF 20 364 #define MAXBUFMEM (8 * 1024 * 1024) 365 366 /* 367 * header information for buffers managed by getbuf() and freebuf() 368 */ 369 typedef struct bufhdr { 370 struct bufhdr *head; 371 struct bufhdr *next; 372 } bufhdr; 373 374 int bufhdrsize; 375 376 bufhdr inodebuf = { NULL, NULL }; 377 bufhdr cgsumbuf = { NULL, NULL }; 378 379 #define SECTORS_PER_TERABYTE (1LL << 31) 380 /* 381 * The following constant specifies an upper limit for file system size 382 * that is actually a lot bigger than we expect to support with UFS. (Since 383 * it's specified in sectors, the file system size would be 2**44 * 512, 384 * which is 2**53, which is 8192 Terabytes.) However, it's useful 385 * for checking the basic sanity of a size value that is input on the 386 * command line. 387 */ 388 #define FS_SIZE_UPPER_LIMIT 0x100000000000LL 389 390 /* 391 * Forward declarations 392 */ 393 static char *getbuf(bufhdr *bufhead, int size); 394 static void freebuf(char *buf); 395 static void freetrans(aio_trans *transp); 396 static aio_trans *get_aiop(); 397 static aio_trans *wait_for_write(int block); 398 static void initcg(int cylno); 399 static void fsinit(); 400 static int makedir(struct direct *protodir, int entries); 401 static void iput(struct inode *ip); 402 static void rdfs(diskaddr_t bno, int size, char *bf); 403 static void wtfs(diskaddr_t bno, int size, char *bf); 404 static void awtfs(diskaddr_t bno, int size, char *bf, int release); 405 static void wtfs_breakup(diskaddr_t bno, int size, char *bf); 406 static int isblock(struct fs *fs, unsigned char *cp, int h); 407 static void clrblock(struct fs *fs, unsigned char *cp, int h); 408 static void setblock(struct fs *fs, unsigned char *cp, int h); 409 static void usage(); 410 static void dump_fscmd(char *fsys, int fsi); 411 static uint64_t number(uint64_t d_value, char *param, int flags); 412 static int match(char *s); 413 static char checkopt(char *optim); 414 static char checkmtb(char *mtbarg); 415 static void range_check(long *varp, char *name, long minimum, 416 long maximum, long def_val, int user_supplied); 417 static void range_check_64(uint64_t *varp, char *name, uint64_t minimum, 418 uint64_t maximum, uint64_t def_val, int user_supplied); 419 static daddr32_t alloc(int size, int mode); 420 static diskaddr_t get_max_size(int fd); 421 static long get_max_track_size(int fd); 422 static void block_sigint(sigset_t *old_mask); 423 static void unblock_sigint(sigset_t *old_mask); 424 static void recover_from_sigint(int signum); 425 static int confirm_abort(void); 426 static int getline(FILE *fp, char *loc, int maxlen); 427 static void flush_writes(void); 428 static long compute_maxcpg(long, long, long, long, long); 429 static int in_64bit_mode(void); 430 static int validate_size(int fd, diskaddr_t size); 431 static void dump_sblock(void); 432 433 union { 434 struct fs fs; 435 char pad[SBSIZE]; 436 } fsun, altfsun; 437 #define sblock fsun.fs 438 #define altsblock altfsun.fs 439 440 struct csum *fscs; 441 442 union cgun { 443 struct cg cg; 444 char pad[MAXBSIZE]; 445 } cgun; 446 447 #define acg cgun.cg 448 /* 449 * Size of screen in cols in which to fit output 450 */ 451 #define WIDTH 80 452 453 struct dinode zino[MAXBSIZE / sizeof (struct dinode)]; 454 455 /* 456 * file descriptors used for rdfs(fsi) and wtfs(fso). 457 * Initialized to an illegal file descriptor number. 458 */ 459 int fsi = -1; 460 int fso = -1; 461 462 /* 463 * The BIG parameter is machine dependent. It should be a longlong integer 464 * constant that can be used by the number parser to check the validity 465 * of numeric parameters. 466 */ 467 468 #define BIG 0x7fffffffffffffffLL 469 470 /* Used to indicate to number() that a bogus value should cause us to exit */ 471 #define NO_DEFAULT LONG_MIN 472 473 /* 474 * INVALIDSBLIMIT is the number of bad backup superblocks that will be 475 * tolerated before we decide to try arriving at a different set of them 476 * using a different logic. This is applicable for non-EFI disks only. 477 */ 478 #define INVALIDSBLIMIT 10 479 480 /* 481 * The *_flag variables are used to indicate that the user specified 482 * the values, rather than that we made them up ourselves. We can 483 * complain about the user giving us bogus values. 484 */ 485 486 /* semi-constants */ 487 long sectorsize = DEV_BSIZE; /* bytes/sector from param.h */ 488 long bbsize = BBSIZE; /* boot block size */ 489 long sbsize = SBSIZE; /* superblock size */ 490 491 /* parameters */ 492 diskaddr_t fssize_db; /* file system size in disk blocks */ 493 diskaddr_t fssize_frag; /* file system size in frags */ 494 long cpg; /* cylinders/cylinder group */ 495 int cpg_flag = RC_DEFAULT; 496 long rotdelay = -1; /* rotational delay between blocks */ 497 int rotdelay_flag = RC_DEFAULT; 498 long maxcontig; /* max contiguous blocks to allocate */ 499 int maxcontig_flag = RC_DEFAULT; 500 long nsect = DFLNSECT; /* sectors per track */ 501 int nsect_flag = RC_DEFAULT; 502 long ntrack = DFLNTRAK; /* tracks per cylinder group */ 503 int ntrack_flag = RC_DEFAULT; 504 long bsize = DESBLKSIZE; /* filesystem block size */ 505 int bsize_flag = RC_DEFAULT; 506 long fragsize = DESFRAGSIZE; /* filesystem fragment size */ 507 int fragsize_flag = RC_DEFAULT; 508 long minfree = MINFREE; /* fs_minfree */ 509 int minfree_flag = RC_DEFAULT; 510 long rps = DEFHZ; /* revolutions/second of drive */ 511 int rps_flag = RC_DEFAULT; 512 long nbpi = NBPI; /* number of bytes per inode */ 513 int nbpi_flag = RC_DEFAULT; 514 long nrpos = NRPOS; /* number of rotational positions */ 515 int nrpos_flag = RC_DEFAULT; 516 long apc = 0; /* alternate sectors per cylinder */ 517 int apc_flag = RC_DEFAULT; 518 char opt = 't'; /* optimization style, `t' or `s' */ 519 char mtb = 'n'; /* multi-terabyte format, 'y' or 'n' */ 520 521 long debug = 0; /* enable debugging output */ 522 523 int spc_flag = 0; /* alternate sectors specified or */ 524 /* found */ 525 526 /* global state */ 527 int Nflag; /* do not write to disk */ 528 int mflag; /* return the command line used to create this FS */ 529 int rflag; /* report the superblock in an easily-parsed form */ 530 int Rflag; /* dump the superblock in binary */ 531 char *fsys; 532 time_t mkfstime; 533 char *string; 534 int label_type; 535 536 /* 537 * logging support 538 */ 539 int ismdd; /* true if device is a SVM device */ 540 int islog; /* true if ufs or SVM logging is enabled */ 541 int islogok; /* true if ufs/SVM log state is good */ 542 543 static int isufslog; /* true if ufs logging is enabled */ 544 static int waslog; /* true when ufs logging disabled during grow */ 545 546 /* 547 * growfs defines, globals, and forward references 548 */ 549 #define NOTENOUGHSPACE 33 550 int grow; 551 static int Pflag; /* probe to which size the fs can be grown */ 552 int ismounted; 553 char *directory; 554 diskaddr_t grow_fssize; 555 long grow_fs_size; 556 long grow_fs_ncg; 557 diskaddr_t grow_fs_csaddr; 558 long grow_fs_cssize; 559 int grow_fs_clean; 560 struct csum *grow_fscs; 561 diskaddr_t grow_sifrag; 562 int test; 563 int testforce; 564 diskaddr_t testfrags; 565 int inlockexit; 566 int isbad; 567 568 void lockexit(int); 569 void randomgeneration(void); 570 void checksummarysize(void); 571 int checksblock(struct fs, int); 572 void growinit(char *); 573 void checkdev(char *, char *); 574 void checkmount(struct mnttab *, char *); 575 struct dinode *gdinode(ino_t); 576 int csfraginrange(daddr32_t); 577 struct csfrag *findcsfrag(daddr32_t, struct csfrag **); 578 void checkindirect(ino_t, daddr32_t *, daddr32_t, int); 579 void addcsfrag(ino_t, daddr32_t, struct csfrag **); 580 void delcsfrag(daddr32_t, struct csfrag **); 581 void checkdirect(ino_t, daddr32_t *, daddr32_t *, int); 582 void findcsfragino(void); 583 void fixindirect(daddr32_t, int); 584 void fixdirect(caddr_t, daddr32_t, daddr32_t *, int); 585 void fixcsfragino(void); 586 void extendsummaryinfo(void); 587 int notenoughspace(void); 588 void unalloccsfragino(void); 589 void unalloccsfragfree(void); 590 void findcsfragfree(void); 591 void copycsfragino(void); 592 void rdcg(long); 593 void wtcg(void); 594 void flcg(void); 595 void allocfrags(long, daddr32_t *, long *); 596 void alloccsfragino(void); 597 void alloccsfragfree(void); 598 void freefrags(daddr32_t, long, long); 599 int findfreerange(long *, long *); 600 void resetallocinfo(void); 601 void extendcg(long); 602 void ulockfs(void); 603 void wlockfs(void); 604 void clockfs(void); 605 void wtsb(void); 606 static int64_t checkfragallocated(daddr32_t); 607 static struct csum *read_summaryinfo(struct fs *); 608 static diskaddr_t probe_summaryinfo(); 609 610 int 611 main(int argc, char *argv[]) 612 { 613 long i, mincpc, mincpg, ibpcl; 614 long cylno, rpos, blk, j, warn = 0; 615 long mincpgcnt, maxcpg; 616 uint64_t used, bpcg, inospercg; 617 long mapcramped, inodecramped; 618 long postblsize, rotblsize, totalsbsize; 619 FILE *mnttab; 620 struct mnttab mntp; 621 char *special; 622 struct statvfs64 fs; 623 struct dk_geom dkg; 624 struct dk_cinfo dkcinfo; 625 char pbuf[sizeof (uint64_t) * 3 + 1]; 626 char *tmpbuf; 627 int width, plen; 628 uint64_t num; 629 int c, saverr; 630 diskaddr_t max_fssize; 631 long tmpmaxcontig = -1; 632 struct sigaction sigact; 633 uint64_t nbytes64; 634 int remaining_cg; 635 int do_dot = 0; 636 int use_efi_dflts = 0, retry = 0; 637 int invalid_sb_cnt, ret, skip_this_sb; 638 int save_nsect, save_ntrack, save_cpg; 639 640 (void) setlocale(LC_ALL, ""); 641 642 #if !defined(TEXT_DOMAIN) 643 #define TEXT_DOMAIN "SYS_TEST" 644 #endif 645 (void) textdomain(TEXT_DOMAIN); 646 647 while ((c = getopt(argc, argv, "F:bmo:VPGM:T:t:")) != EOF) { 648 switch (c) { 649 650 case 'F': 651 string = optarg; 652 if (strcmp(string, "ufs") != 0) 653 usage(); 654 break; 655 656 case 'm': /* return command line used to create this FS */ 657 mflag++; 658 break; 659 660 case 'o': 661 /* 662 * ufs specific options. 663 */ 664 string = optarg; 665 while (*string != '\0') { 666 if (match("nsect=")) { 667 nsect = number(DFLNSECT, "nsect", 0); 668 nsect_flag = RC_KEYWORD; 669 } else if (match("ntrack=")) { 670 ntrack = number(DFLNTRAK, "ntrack", 0); 671 ntrack_flag = RC_KEYWORD; 672 } else if (match("bsize=")) { 673 bsize = number(DESBLKSIZE, "bsize", 0); 674 bsize_flag = RC_KEYWORD; 675 } else if (match("fragsize=")) { 676 fragsize = number(DESFRAGSIZE, 677 "fragsize", 0); 678 fragsize_flag = RC_KEYWORD; 679 } else if (match("cgsize=")) { 680 cpg = number(DESCPG, "cgsize", 0); 681 cpg_flag = RC_KEYWORD; 682 } else if (match("free=")) { 683 minfree = number(MINFREE, "free", 684 ALLOW_PERCENT); 685 minfree_flag = RC_KEYWORD; 686 } else if (match("maxcontig=")) { 687 tmpmaxcontig = 688 number(-1, "maxcontig", 0); 689 maxcontig_flag = RC_KEYWORD; 690 } else if (match("nrpos=")) { 691 nrpos = number(NRPOS, "nrpos", 0); 692 nrpos_flag = RC_KEYWORD; 693 } else if (match("rps=")) { 694 rps = number(DEFHZ, "rps", 0); 695 rps_flag = RC_KEYWORD; 696 } else if (match("nbpi=")) { 697 nbpi = number(NBPI, "nbpi", 0); 698 nbpi_flag = RC_KEYWORD; 699 } else if (match("opt=")) { 700 opt = checkopt(string); 701 } else if (match("mtb=")) { 702 mtb = checkmtb(string); 703 } else if (match("apc=")) { 704 apc = number(0, "apc", 0); 705 apc_flag = RC_KEYWORD; 706 } else if (match("gap=")) { 707 (void) number(0, "gap", ALLOW_MS1); 708 rotdelay = ROTDELAY; 709 rotdelay_flag = RC_DEFAULT; 710 } else if (match("debug=")) { 711 debug = number(0, "debug", 0); 712 } else if (match("N")) { 713 Nflag++; 714 } else if (match("calcsb")) { 715 rflag++; 716 Nflag++; 717 } else if (match("calcbinsb")) { 718 rflag++; 719 Rflag++; 720 Nflag++; 721 } else if (*string == '\0') { 722 break; 723 } else { 724 (void) fprintf(stderr, gettext( 725 "illegal option: %s\n"), 726 string); 727 usage(); 728 } 729 730 if (*string == ',') string++; 731 if (*string == ' ') string++; 732 } 733 break; 734 735 case 'V': 736 { 737 char *opt_text; 738 int opt_count; 739 740 (void) fprintf(stdout, gettext("mkfs -F ufs ")); 741 for (opt_count = 1; opt_count < argc; 742 opt_count++) { 743 opt_text = argv[opt_count]; 744 if (opt_text) 745 (void) fprintf(stdout, " %s ", 746 opt_text); 747 } 748 (void) fprintf(stdout, "\n"); 749 } 750 break; 751 752 case 'b': /* do nothing for this */ 753 break; 754 755 case 'M': /* grow the mounted file system */ 756 directory = optarg; 757 758 /* FALLTHROUGH */ 759 case 'G': /* grow the file system */ 760 grow = 1; 761 break; 762 case 'P': /* probe the file system growing size */ 763 Pflag = 1; 764 grow = 1; /* probe mode implies fs growing */ 765 break; 766 case 'T': /* For testing */ 767 testforce = 1; 768 769 /* FALLTHROUGH */ 770 case 't': 771 test = 1; 772 string = optarg; 773 testfrags = number(NO_DEFAULT, "testfrags", 0); 774 break; 775 776 case '?': 777 usage(); 778 break; 779 } 780 } 781 #ifdef MKFS_DEBUG 782 /* 783 * Turning on MKFS_DEBUG causes mkfs to produce a filesystem 784 * that can be reproduced by setting the time to 0 and seeding 785 * the random number generator to a constant. 786 */ 787 mkfstime = 0; /* reproducible results */ 788 #else 789 (void) time(&mkfstime); 790 #endif 791 792 if (optind >= (argc - 1)) { 793 if (optind > (argc - 1)) { 794 (void) fprintf(stderr, 795 gettext("special not specified\n")); 796 usage(); 797 } else if (mflag == 0) { 798 (void) fprintf(stderr, 799 gettext("size not specified\n")); 800 usage(); 801 } 802 } 803 argc -= optind; 804 argv = &argv[optind]; 805 806 fsys = argv[0]; 807 fsi = open64(fsys, O_RDONLY); 808 if (fsi < 0) { 809 (void) fprintf(stderr, gettext("%s: cannot open\n"), fsys); 810 lockexit(32); 811 } 812 813 if (mflag) { 814 dump_fscmd(fsys, fsi); 815 lockexit(0); 816 } 817 818 /* 819 * The task of setting all of the configuration parameters for a 820 * UFS file system is basically a matter of solving n equations 821 * in m variables. Typically, m is greater than n, so there is 822 * usually more than one valid solution. Since this is usually 823 * an under-constrained problem, it's not always obvious what the 824 * "best" configuration is. 825 * 826 * In general, the approach is to 827 * 1. Determine the values for the file system parameters 828 * that are externally contrained and therefore not adjustable 829 * by mkfs (such as the device's size and maxtransfer size). 830 * 2. Acquire the user's requested setting for all configuration 831 * values that can be set on the command line. 832 * 3. Determine the final value of all configuration values, by 833 * the following approach: 834 * - set the file system block size (fs_bsize). Although 835 * this could be regarded as an adjustable parameter, in 836 * fact, it's pretty much a constant. At this time, it's 837 * generally set to 8k (with older hardware, it can 838 * sometimes make sense to set it to 4k, but those 839 * situations are pretty rare now). 840 * - re-adjust the maximum file system size based on the 841 * value of the file system block size. Since the 842 * frag size can't be any larger than a file system 843 * block, and the number of frags in the file system 844 * has to fit into 31 bits, the file system block size 845 * affects the maximum file system size. 846 * - now that the real maximum file system is known, set the 847 * actual size of the file system to be created to 848 * MIN(requested size, maximum file system size). 849 * - now validate, and if necessary, adjust the following 850 * values: 851 * rotdelay 852 * nsect 853 * maxcontig 854 * apc 855 * frag_size 856 * rps 857 * minfree 858 * nrpos 859 * nrack 860 * nbpi 861 * - calculate maxcpg (the maximum value of the cylinders-per- 862 * cylinder-group configuration parameters). There are two 863 * algorithms for calculating maxcpg: an old one, which is 864 * used for file systems of less than 1 terabyte, and a 865 * new one, implemented in the function compute_maxcpg(), 866 * which is used for file systems of greater than 1 TB. 867 * The difference between them is that compute_maxcpg() 868 * really tries to maximize the cpg value. The old 869 * algorithm fails to take advantage of smaller frags and 870 * lower inode density when determining the maximum cpg, 871 * and thus comes up with much lower numbers in some 872 * configurations. At some point, we might use the 873 * new algorithm for determining maxcpg for all file 874 * systems, but at this time, the changes implemented for 875 * multi-terabyte UFS are NOT being automatically applied 876 * to UFS file systems of less than a terabyte (in the 877 * interest of not changing existing UFS policy too much 878 * until the ramifications of the changes are well-understood 879 * and have been evaluated for their effects on performance.) 880 * - check the current values of the configuration parameters 881 * against the various constraints imposed by UFS. These 882 * include: 883 * * There must be at least one inode in each 884 * cylinder group. 885 * * The cylinder group overhead block, which 886 * contains the inode and frag bigmaps, must fit 887 * within one file system block. 888 * * The space required for inode maps should 889 * occupy no more than a third of the cylinder 890 * group overhead block. 891 * * The rotational position tables have to fit 892 * within the available space in the super block. 893 * Adjust the configuration values that can be adjusted 894 * so that these constraints are satisfied. The 895 * configuration values that are adjustable are: 896 * * frag size 897 * * cylinders per group 898 * * inode density (can be increased) 899 * * number of rotational positions (the rotational 900 * position tables are eliminated altogether if 901 * there isn't enough room for them.) 902 * 4. Set the values for all the dependent configuration 903 * values (those that aren't settable on the command 904 * line and which are completely dependent on the 905 * adjustable parameters). This include cpc (cycles 906 * per cylinder, spc (sectors-per-cylinder), and many others. 907 */ 908 909 max_fssize = get_max_size(fsi); 910 911 /* 912 * Get and check positional arguments, if any. 913 */ 914 switch (argc - 1) { 915 default: 916 usage(); 917 /*NOTREACHED*/ 918 case 15: 919 mtb = checkmtb(argv[15]); 920 /* FALLTHROUGH */ 921 case 14: 922 string = argv[14]; 923 tmpmaxcontig = number(-1, "maxcontig", 0); 924 maxcontig_flag = RC_POSITIONAL; 925 /* FALLTHROUGH */ 926 case 13: 927 string = argv[13]; 928 nrpos = number(NRPOS, "nrpos", 0); 929 nrpos_flag = RC_POSITIONAL; 930 /* FALLTHROUGH */ 931 case 12: 932 string = argv[12]; 933 rotdelay = ROTDELAY; 934 rotdelay_flag = RC_DEFAULT; 935 /* FALLTHROUGH */ 936 case 11: 937 string = argv[11]; 938 apc = number(0, "apc", 0); 939 apc_flag = RC_POSITIONAL; 940 /* FALLTHROUGH */ 941 case 10: 942 opt = checkopt(argv[10]); 943 /* FALLTHROUGH */ 944 case 9: 945 string = argv[9]; 946 nbpi = number(NBPI, "nbpi", 0); 947 nbpi_flag = RC_POSITIONAL; 948 /* FALLTHROUGH */ 949 case 8: 950 string = argv[8]; 951 rps = number(DEFHZ, "rps", 0); 952 rps_flag = RC_POSITIONAL; 953 /* FALLTHROUGH */ 954 case 7: 955 string = argv[7]; 956 minfree = number(MINFREE, "free", ALLOW_PERCENT); 957 minfree_flag = RC_POSITIONAL; 958 /* FALLTHROUGH */ 959 case 6: 960 string = argv[6]; 961 cpg = number(DESCPG, "cgsize", 0); 962 cpg_flag = RC_POSITIONAL; 963 /* FALLTHROUGH */ 964 case 5: 965 string = argv[5]; 966 fragsize = number(DESFRAGSIZE, "fragsize", 0); 967 fragsize_flag = RC_POSITIONAL; 968 /* FALLTHROUGH */ 969 case 4: 970 string = argv[4]; 971 bsize = number(DESBLKSIZE, "bsize", 0); 972 bsize_flag = RC_POSITIONAL; 973 /* FALLTHROUGH */ 974 case 3: 975 string = argv[3]; 976 ntrack = number(DFLNTRAK, "ntrack", 0); 977 ntrack_flag = RC_POSITIONAL; 978 /* FALLTHROUGH */ 979 case 2: 980 string = argv[2]; 981 nsect = number(DFLNSECT, "nsect", 0); 982 nsect_flag = RC_POSITIONAL; 983 /* FALLTHROUGH */ 984 case 1: 985 string = argv[1]; 986 fssize_db = number(max_fssize, "size", 0); 987 } 988 989 990 if ((maxcontig_flag == RC_DEFAULT) || (tmpmaxcontig == -1) || 991 (maxcontig == -1)) { 992 long maxtrax = get_max_track_size(fsi); 993 maxcontig = maxtrax / bsize; 994 995 } else { 996 maxcontig = tmpmaxcontig; 997 } 998 dprintf(("DeBuG maxcontig : %ld\n", maxcontig)); 999 1000 if (rotdelay == -1) { /* default by newfs and mkfs */ 1001 rotdelay = ROTDELAY; 1002 } 1003 1004 if (cpg_flag == RC_DEFAULT) { /* If not explicity set, use default */ 1005 cpg = DESCPG; 1006 } 1007 dprintf(("DeBuG cpg : %ld\n", cpg)); 1008 1009 /* 1010 * Now that we have the semi-sane args, either positional, via -o, 1011 * or by defaulting, handle inter-dependencies and range checks. 1012 */ 1013 1014 /* 1015 * Settle the file system block size first, since it's a fixed 1016 * parameter once set and so many other parameters, including 1017 * max_fssize, depend on it. 1018 */ 1019 range_check(&bsize, "bsize", MINBSIZE, MAXBSIZE, DESBLKSIZE, 1020 bsize_flag); 1021 1022 if (!POWEROF2(bsize)) { 1023 (void) fprintf(stderr, 1024 gettext("block size must be a power of 2, not %ld\n"), 1025 bsize); 1026 bsize = DESBLKSIZE; 1027 (void) fprintf(stderr, 1028 gettext("mkfs: bsize reset to default %ld\n"), 1029 bsize); 1030 } 1031 1032 if (fssize_db > max_fssize && validate_size(fsi, fssize_db)) { 1033 (void) fprintf(stderr, gettext( 1034 "Warning: the requested size of this file system\n" 1035 "(%lld sectors) is greater than the size of the\n" 1036 "device reported by the driver (%lld sectors).\n" 1037 "However, a read of the device at the requested size\n" 1038 "does succeed, so the requested size will be used.\n"), 1039 fssize_db, max_fssize); 1040 max_fssize = fssize_db; 1041 } 1042 /* 1043 * Since the maximum allocatable unit (the frag) must be less than 1044 * or equal to bsize, and the number of frags must be less than or 1045 * equal to INT_MAX, the total size of the file system (in 1046 * bytes) must be less than or equal to bsize * INT_MAX. 1047 */ 1048 1049 if (max_fssize > ((diskaddr_t)bsize/DEV_BSIZE) * INT_MAX) 1050 max_fssize = ((diskaddr_t)bsize/DEV_BSIZE) * INT_MAX; 1051 range_check_64(&fssize_db, "size", 1024LL, max_fssize, max_fssize, 1); 1052 1053 if (fssize_db >= SECTORS_PER_TERABYTE) { 1054 mtb = 'y'; 1055 if (!in_64bit_mode()) { 1056 (void) fprintf(stderr, gettext( 1057 "mkfs: Warning: Creating a file system greater than 1 terabyte on a\n" 1058 " system running a 32-bit kernel. This file system will not be\n" 1059 " accessible until the system is rebooted with a 64-bit kernel.\n")); 1060 } 1061 } 1062 1063 /* 1064 * With newer and much larger disks, the newfs(1M) and mkfs_ufs(1M) 1065 * commands had problems in correctly handling the "native" geometries 1066 * for various storage devices. 1067 * 1068 * To handle the new age disks, mkfs_ufs(1M) will use the EFI style 1069 * for non-EFI disks that are larger than the CHS addressing limit 1070 * ( > 8GB approx ) and ignore the disk geometry information for 1071 * these drives. This is what is currently done for multi-terrabyte 1072 * filesystems on EFI disks. 1073 * 1074 * However if the user asked for a specific layout by supplying values 1075 * for these parameters, honour the user supplied parameters. 1076 */ 1077 1078 if (mtb != 'y' && label_type == LABEL_TYPE_VTOC && 1079 ((nsect == -1 && ntrack == -1) || 1080 (grow && ntrack_flag == RC_DEFAULT))) { 1081 /* 1082 * "-1" indicates that we were called from newfs and these 1083 * arguments were not passed in command line. Calculate nsect 1084 * and ntrack in the same manner as newfs. 1085 * 1086 * This is required because, the defaults for nsect and ntrack 1087 * is hardcoded in mkfs, whereas to generate the alternate 1088 * superblock locations for the -N option, there is a need for 1089 * the geometry based values that newfs would have arrived at. 1090 * Newfs would have arrived at these values as below. 1091 */ 1092 1093 if (ioctl(fsi, DKIOCGGEOM, &dkg)) { 1094 dprintf(("%s: Unable to read Disk geometry", fsys)); 1095 perror(gettext("Unable to read Disk geometry")); 1096 lockexit(32); 1097 } else { 1098 nsect = dkg.dkg_nsect; 1099 ntrack = dkg.dkg_nhead; 1100 #ifdef i386 /* Bug 1170182 */ 1101 if (ntrack > 32 && (ntrack % 16) != 0) { 1102 ntrack -= (ntrack % 16); 1103 } 1104 #endif 1105 if ((dkg.dkg_ncyl * dkg.dkg_nhead * dkg.dkg_nsect) 1106 > CHSLIMIT) { 1107 use_efi_dflts = 1; 1108 retry = 1; 1109 } 1110 } 1111 dprintf(("DeBuG mkfs: geom = %ld CHSLIMIT = %d\n", 1112 dkg.dkg_ncyl * dkg.dkg_nhead * dkg.dkg_nsect, 1113 CHSLIMIT)); 1114 } 1115 1116 /* 1117 * For the newfs -N case, even if the disksize is > CHSLIMIT, do not 1118 * blindly follow EFI style. If the fs_version indicates a geometry 1119 * based layout, try that one first. If it fails we can always try the 1120 * other logic. 1121 * 1122 * If we were called from growfs, we will have a problem if we mix 1123 * and match the filesystem creation and growth styles. For example, 1124 * if we create using EFI style and we have to also grow using EFI 1125 * style. So follow the style indicated by the fs_version. 1126 * 1127 * Read and verify the primary superblock. If it looks sane, use the 1128 * fs_version from the superblock. If the primary superblock does 1129 * not look good, read and verify the first alternate superblock at 1130 * ALTSB. Use the fs_version to decide whether to use the 1131 * EFI style logic or the old geometry based logic to calculate 1132 * the alternate superblock locations. 1133 */ 1134 if ((Nflag && use_efi_dflts) || (grow)) { 1135 if (grow && ntrack_flag != RC_DEFAULT) 1136 goto retry_alternate_logic; 1137 rdfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, 1138 (char *)&altsblock); 1139 ret = checksblock(altsblock, 1); 1140 1141 if (!ret) { 1142 if (altsblock.fs_magic == MTB_UFS_MAGIC) { 1143 mtb = 'y'; 1144 goto retry_alternate_logic; 1145 } 1146 use_efi_dflts = (altsblock.fs_version == 1147 UFS_EFISTYLE4NONEFI_VERSION_2) ? 1 : 0; 1148 } else { 1149 /* 1150 * The primary superblock didn't help in determining 1151 * the fs_version. Try the first alternate superblock. 1152 */ 1153 dprintf(("DeBuG checksblock() failed - error : %d" 1154 " for sb : %d\n", ret, SBOFF/sectorsize)); 1155 rdfs((diskaddr_t)ALTSB, (int)sbsize, 1156 (char *)&altsblock); 1157 ret = checksblock(altsblock, 1); 1158 1159 if (!ret) { 1160 if (altsblock.fs_magic == MTB_UFS_MAGIC) { 1161 mtb = 'y'; 1162 goto retry_alternate_logic; 1163 } 1164 use_efi_dflts = (altsblock.fs_version == 1165 UFS_EFISTYLE4NONEFI_VERSION_2) ? 1 : 0; 1166 } else 1167 dprintf(("DeBuG checksblock() failed - error : %d" 1168 " for sb : %d\n", ret, ALTSB)); 1169 } 1170 } 1171 1172 retry_alternate_logic: 1173 invalid_sb_cnt = 0; 1174 if (use_efi_dflts) { 1175 save_nsect = nsect; 1176 save_ntrack = ntrack; 1177 save_cpg = cpg; 1178 1179 nsect = DEF_SECTORS_EFI; 1180 ntrack = DEF_TRACKS_EFI; 1181 cpg = DESCPG; 1182 1183 dprintf(("\nDeBuG Using EFI defaults\n")); 1184 dprintf(("DeBuG save_nsect=%d, save_ntrack=%d, save_cpg=%d\n", 1185 save_nsect, save_ntrack, save_cpg)); 1186 } else { 1187 save_nsect = DEF_SECTORS_EFI; 1188 save_ntrack = DEF_TRACKS_EFI; 1189 save_cpg = DESCPG; 1190 dprintf(("\n\nDeBuG mkfs: Using Geometry\n")); 1191 dprintf(("DeBuG save_nsect=%d, save_ntrack=%d, save_cpg=%d\n", 1192 save_nsect, save_ntrack, save_cpg)); 1193 /* 1194 * 32K based on max block size of 64K, and rotational layout 1195 * test of nsect <= (256 * sectors/block). Current block size 1196 * limit is not 64K, but it's growing soon. 1197 */ 1198 range_check(&nsect, "nsect", 1, 32768, DFLNSECT, nsect_flag); 1199 /* 1200 * ntrack is the number of tracks per cylinder. 1201 * The ntrack value must be between 1 and the total number of 1202 * sectors in the file system. 1203 */ 1204 range_check(&ntrack, "ntrack", 1, 1205 fssize_db > INT_MAX ? INT_MAX : (uint32_t)fssize_db, 1206 DFLNTRAK, ntrack_flag); 1207 } 1208 1209 range_check(&apc, "apc", 0, nsect - 1, 0, apc_flag); 1210 1211 if (mtb == 'y') 1212 fragsize = bsize; 1213 1214 range_check(&fragsize, "fragsize", sectorsize, bsize, 1215 MAX(bsize / MAXFRAG, MIN(DESFRAGSIZE, bsize)), fragsize_flag); 1216 1217 if ((bsize / MAXFRAG) > fragsize) { 1218 (void) fprintf(stderr, gettext( 1219 "fragment size %ld is too small, minimum with block size %ld is %ld\n"), 1220 fragsize, bsize, bsize / MAXFRAG); 1221 (void) fprintf(stderr, 1222 gettext("mkfs: fragsize reset to minimum %ld\n"), 1223 bsize / MAXFRAG); 1224 fragsize = bsize / MAXFRAG; 1225 } 1226 1227 if (!POWEROF2(fragsize)) { 1228 (void) fprintf(stderr, 1229 gettext("fragment size must be a power of 2, not %ld\n"), 1230 fragsize); 1231 fragsize = MAX(bsize / MAXFRAG, MIN(DESFRAGSIZE, bsize)); 1232 (void) fprintf(stderr, 1233 gettext("mkfs: fragsize reset to %ld\n"), 1234 fragsize); 1235 } 1236 1237 /* At this point, bsize must be >= fragsize, so no need to check it */ 1238 1239 if (bsize < PAGESIZE) { 1240 (void) fprintf(stderr, gettext( 1241 "WARNING: filesystem block size (%ld) is smaller than " 1242 "memory page size (%ld).\nResulting filesystem can not be " 1243 "mounted on this system.\n\n"), 1244 bsize, (long)PAGESIZE); 1245 } 1246 1247 range_check(&rps, "rps", 1, 1000, DEFHZ, rps_flag); 1248 range_check(&minfree, "free", 0, 99, MINFREE, minfree_flag); 1249 range_check(&nrpos, "nrpos", 1, nsect, MIN(nsect, NRPOS), nrpos_flag); 1250 1251 /* 1252 * nbpi is variable, but 2MB seems a reasonable upper limit, 1253 * as 4MB tends to cause problems (using otherwise-default 1254 * parameters). The true limit is where we end up with one 1255 * inode per cylinder group. If this file system is being 1256 * configured for multi-terabyte access, nbpi must be at least 1MB. 1257 */ 1258 if (mtb == 'y' && nbpi < MTB_NBPI) { 1259 (void) fprintf(stderr, gettext("mkfs: bad value for nbpi: " 1260 "must be at least 1048576 for multi-terabyte, " 1261 "nbpi reset to default 1048576\n")); 1262 nbpi = MTB_NBPI; 1263 } 1264 1265 if (mtb == 'y') 1266 range_check(&nbpi, "nbpi", MTB_NBPI, 2 * MB, MTB_NBPI, 1267 nbpi_flag); 1268 else 1269 range_check(&nbpi, "nbpi", DEV_BSIZE, 2 * MB, NBPI, nbpi_flag); 1270 1271 /* 1272 * maxcpg is another variably-limited parameter. Calculate 1273 * the limit based on what we've got for its dependent 1274 * variables. Effectively, it's how much space is left in the 1275 * superblock after all the other bits are accounted for. We 1276 * only fill in sblock fields so we can use MAXIpG. 1277 * 1278 * If the calculation of maxcpg below (for the mtb == 'n' 1279 * case) is changed, update newfs as well. 1280 * 1281 * For old-style, non-MTB format file systems, use the old 1282 * algorithm for calculating the maximum cylinder group size, 1283 * even though it limits the cylinder group more than necessary. 1284 * Since layout can affect performance, we don't want to change 1285 * the default layout for non-MTB file systems at this time. 1286 * However, for MTB file systems, use the new maxcpg calculation, 1287 * which really maxes out the cylinder group size. 1288 */ 1289 1290 sblock.fs_bsize = bsize; 1291 sblock.fs_inopb = sblock.fs_bsize / sizeof (struct dinode); 1292 1293 if (mtb == 'n') { 1294 maxcpg = (bsize - sizeof (struct cg) - 1295 howmany(MAXIpG(&sblock), NBBY)) / 1296 (sizeof (long) + nrpos * sizeof (short) + 1297 nsect / (MAXFRAG * NBBY)); 1298 } else { 1299 maxcpg = compute_maxcpg(bsize, fragsize, nbpi, nrpos, 1300 nsect * ntrack); 1301 } 1302 1303 dprintf(("DeBuG cpg : %ld\n", cpg)); 1304 if (cpg == -1) 1305 cpg = maxcpg; 1306 dprintf(("DeBuG cpg : %ld\n", cpg)); 1307 1308 /* 1309 * mincpg is variable in complex ways, so we really can't 1310 * do a sane lower-end limit check at this point. 1311 */ 1312 range_check(&cpg, "cgsize", 1, maxcpg, MIN(maxcpg, DESCPG), cpg_flag); 1313 1314 /* 1315 * get the controller info 1316 */ 1317 ismdd = 0; 1318 islog = 0; 1319 islogok = 0; 1320 waslog = 0; 1321 1322 if (ioctl(fsi, DKIOCINFO, &dkcinfo) == 0) 1323 /* 1324 * if it is an MDD (disksuite) device 1325 */ 1326 if (dkcinfo.dki_ctype == DKC_MD) { 1327 ismdd++; 1328 /* 1329 * check the logging device 1330 */ 1331 if (ioctl(fsi, _FIOISLOG, NULL) == 0) { 1332 islog++; 1333 if (ioctl(fsi, _FIOISLOGOK, NULL) == 0) 1334 islogok++; 1335 } 1336 } 1337 1338 /* 1339 * Do not grow the file system, but print on stdout the maximum 1340 * size in sectors to which the file system can be increased. 1341 * The calculated size is limited by fssize_db. 1342 * Note that we don't lock the filesystem and therefore under rare 1343 * conditions (the filesystem is mounted, the free block count is 1344 * almost zero, and the superuser is still changing it) the calculated 1345 * size can be imprecise. 1346 */ 1347 if (Pflag) { 1348 (void) printf("%llu\n", probe_summaryinfo()); 1349 exit(0); 1350 } 1351 1352 /* 1353 * If we're growing an existing filesystem, then we're about 1354 * to start doing things that can require recovery efforts if 1355 * we get interrupted, so make sure we get a chance to do so. 1356 */ 1357 if (grow) { 1358 sigact.sa_handler = recover_from_sigint; 1359 sigemptyset(&sigact.sa_mask); 1360 sigact.sa_flags = SA_RESTART; 1361 1362 if (sigaction(SIGINT, &sigact, (struct sigaction *)NULL) < 0) { 1363 perror(gettext("Could not register SIGINT handler")); 1364 lockexit(3); 1365 } 1366 } 1367 1368 if (!Nflag) { 1369 /* 1370 * Check if MNTTAB is trustable 1371 */ 1372 if (statvfs64(MNTTAB, &fs) < 0) { 1373 (void) fprintf(stderr, gettext("can't statvfs %s\n"), 1374 MNTTAB); 1375 exit(32); 1376 } 1377 1378 if (strcmp(MNTTYPE_MNTFS, fs.f_basetype) != 0) { 1379 (void) fprintf(stderr, gettext( 1380 "%s file system type is not %s, can't mkfs\n"), 1381 MNTTAB, MNTTYPE_MNTFS); 1382 exit(32); 1383 } 1384 1385 special = getfullblkname(fsys); 1386 checkdev(fsys, special); 1387 1388 /* 1389 * If we found the block device name, 1390 * then check the mount table. 1391 * if mounted, and growing write lock the file system 1392 * 1393 */ 1394 if ((special != NULL) && (*special != '\0')) { 1395 if ((mnttab = fopen(MNTTAB, "r")) == NULL) { 1396 (void) fprintf(stderr, gettext( 1397 "can't open %s\n"), MNTTAB); 1398 exit(32); 1399 } 1400 while ((getmntent(mnttab, &mntp)) == NULL) { 1401 if (grow) { 1402 checkmount(&mntp, special); 1403 continue; 1404 } 1405 if (strcmp(special, mntp.mnt_special) == 0) { 1406 (void) fprintf(stderr, gettext( 1407 "%s is mounted, can't mkfs\n"), 1408 special); 1409 exit(32); 1410 } 1411 } 1412 (void) fclose(mnttab); 1413 } 1414 1415 if (directory && (ismounted == 0)) { 1416 (void) fprintf(stderr, gettext("%s is not mounted\n"), 1417 special); 1418 lockexit(32); 1419 } 1420 1421 fso = (grow) ? open64(fsys, O_WRONLY) : creat64(fsys, 0666); 1422 if (fso < 0) { 1423 saverr = errno; 1424 (void) fprintf(stderr, 1425 gettext("%s: cannot create: %s\n"), 1426 fsys, strerror(saverr)); 1427 lockexit(32); 1428 } 1429 1430 } else { 1431 1432 /* 1433 * For the -N case, a file descriptor is needed for the llseek() 1434 * in wtfs(). See the comment in wtfs() for more information. 1435 * 1436 * Get a file descriptor that's read-only so that this code 1437 * doesn't accidentally write to the file. 1438 */ 1439 fso = open64(fsys, O_RDONLY); 1440 if (fso < 0) { 1441 saverr = errno; 1442 (void) fprintf(stderr, gettext("%s: cannot open: %s\n"), 1443 fsys, strerror(saverr)); 1444 lockexit(32); 1445 } 1446 } 1447 1448 /* 1449 * seed random # generator (for ic_generation) 1450 */ 1451 #ifdef MKFS_DEBUG 1452 srand48(12962); /* reproducible results */ 1453 #else 1454 srand48((long)(time((time_t *)NULL) + getpid())); 1455 #endif 1456 1457 if (grow) { 1458 growinit(fsys); 1459 goto grow00; 1460 } 1461 1462 /* 1463 * Validate the given file system size. 1464 * Verify that its last block can actually be accessed. 1465 * 1466 * Note: it's ok to use sblock as a buffer because it is immediately 1467 * overwritten by the rdfs() of the superblock in the next line. 1468 * 1469 * ToDo: Because the size checking is done in rdfs()/wtfs(), the 1470 * error message for specifying an illegal size is very unfriendly. 1471 * In the future, one could replace the rdfs()/wtfs() calls 1472 * below with in-line calls to read() or write(). This allows better 1473 * error messages to be put in place. 1474 */ 1475 rdfs(fssize_db - 1, (int)sectorsize, (char *)&sblock); 1476 1477 /* 1478 * make the fs unmountable 1479 */ 1480 rdfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, (char *)&sblock); 1481 sblock.fs_magic = -1; 1482 sblock.fs_clean = FSBAD; 1483 sblock.fs_state = FSOKAY - sblock.fs_time; 1484 wtfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, (char *)&sblock); 1485 bzero(&sblock, (size_t)sbsize); 1486 1487 sblock.fs_nsect = nsect; 1488 sblock.fs_ntrak = ntrack; 1489 1490 /* 1491 * Validate specified/determined spc 1492 * and calculate minimum cylinders per group. 1493 */ 1494 1495 /* 1496 * sectors/cyl = tracks/cyl * sectors/track 1497 */ 1498 sblock.fs_spc = sblock.fs_ntrak * sblock.fs_nsect; 1499 1500 grow00: 1501 if (apc_flag) { 1502 sblock.fs_spc -= apc; 1503 } 1504 /* 1505 * Have to test for this separately from apc_flag, due to 1506 * the growfs case.... 1507 */ 1508 if (sblock.fs_spc != sblock.fs_ntrak * sblock.fs_nsect) { 1509 spc_flag = 1; 1510 } 1511 if (grow) 1512 goto grow10; 1513 1514 sblock.fs_nrpos = nrpos; 1515 sblock.fs_bsize = bsize; 1516 sblock.fs_fsize = fragsize; 1517 sblock.fs_minfree = minfree; 1518 1519 grow10: 1520 if (nbpi < sblock.fs_fsize) { 1521 (void) fprintf(stderr, gettext( 1522 "warning: wasteful data byte allocation / inode (nbpi):\n")); 1523 (void) fprintf(stderr, gettext( 1524 "%ld smaller than allocatable fragment size of %d\n"), 1525 nbpi, sblock.fs_fsize); 1526 } 1527 if (grow) 1528 goto grow20; 1529 1530 if (opt == 's') 1531 sblock.fs_optim = FS_OPTSPACE; 1532 else 1533 sblock.fs_optim = FS_OPTTIME; 1534 1535 sblock.fs_bmask = ~(sblock.fs_bsize - 1); 1536 sblock.fs_fmask = ~(sblock.fs_fsize - 1); 1537 /* 1538 * Planning now for future expansion. 1539 */ 1540 #if defined(_BIG_ENDIAN) 1541 sblock.fs_qbmask.val[0] = 0; 1542 sblock.fs_qbmask.val[1] = ~sblock.fs_bmask; 1543 sblock.fs_qfmask.val[0] = 0; 1544 sblock.fs_qfmask.val[1] = ~sblock.fs_fmask; 1545 #endif 1546 #if defined(_LITTLE_ENDIAN) 1547 sblock.fs_qbmask.val[0] = ~sblock.fs_bmask; 1548 sblock.fs_qbmask.val[1] = 0; 1549 sblock.fs_qfmask.val[0] = ~sblock.fs_fmask; 1550 sblock.fs_qfmask.val[1] = 0; 1551 #endif 1552 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1) 1553 sblock.fs_bshift++; 1554 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1) 1555 sblock.fs_fshift++; 1556 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); 1557 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1) 1558 sblock.fs_fragshift++; 1559 if (sblock.fs_frag > MAXFRAG) { 1560 (void) fprintf(stderr, gettext( 1561 "fragment size %d is too small, minimum with block size %d is %d\n"), 1562 sblock.fs_fsize, sblock.fs_bsize, 1563 sblock.fs_bsize / MAXFRAG); 1564 lockexit(32); 1565 } 1566 sblock.fs_nindir = sblock.fs_bsize / sizeof (daddr32_t); 1567 sblock.fs_inopb = sblock.fs_bsize / sizeof (struct dinode); 1568 sblock.fs_nspf = sblock.fs_fsize / sectorsize; 1569 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1) 1570 sblock.fs_fsbtodb++; 1571 1572 /* 1573 * Compute the super-block, cylinder group, and inode blocks. 1574 * Note that these "blkno" are really fragment addresses. 1575 * For example, on an 8K/1K (block/fragment) system, fs_sblkno is 16, 1576 * fs_cblkno is 24, and fs_iblkno is 32. This is why CGSIZE is so 1577 * important: only 1 FS block is allocated for the cg struct (fragment 1578 * numbers 24 through 31). 1579 */ 1580 sblock.fs_sblkno = 1581 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag); 1582 sblock.fs_cblkno = (daddr32_t)(sblock.fs_sblkno + 1583 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag)); 1584 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; 1585 1586 sblock.fs_cgoffset = roundup( 1587 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag); 1588 for (sblock.fs_cgmask = -1, i = sblock.fs_ntrak; i > 1; i >>= 1) 1589 sblock.fs_cgmask <<= 1; 1590 if (!POWEROF2(sblock.fs_ntrak)) 1591 sblock.fs_cgmask <<= 1; 1592 /* 1593 * Validate specified/determined spc 1594 * and calculate minimum cylinders per group. 1595 */ 1596 1597 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc; 1598 sblock.fs_cpc > 1 && (i & 1) == 0; 1599 sblock.fs_cpc >>= 1, i >>= 1) 1600 /* void */; 1601 mincpc = sblock.fs_cpc; 1602 1603 /* if these calculations are changed, check dump_fscmd also */ 1604 bpcg = (uint64_t)sblock.fs_spc * sectorsize; 1605 inospercg = (uint64_t)roundup(bpcg / sizeof (struct dinode), 1606 INOPB(&sblock)); 1607 if (inospercg > MAXIpG(&sblock)) 1608 inospercg = MAXIpG(&sblock); 1609 used = (uint64_t)(sblock.fs_iblkno + inospercg / 1610 INOPF(&sblock)) * NSPF(&sblock); 1611 mincpgcnt = (long)howmany((uint64_t)sblock.fs_cgoffset * 1612 (~sblock.fs_cgmask) + used, sblock.fs_spc); 1613 mincpg = roundup(mincpgcnt, mincpc); 1614 /* 1615 * Insure that cylinder group with mincpg has enough space 1616 * for block maps 1617 */ 1618 sblock.fs_cpg = mincpg; 1619 sblock.fs_ipg = (int32_t)inospercg; 1620 mapcramped = 0; 1621 1622 /* 1623 * Make sure the cg struct fits within the file system block. 1624 * Use larger block sizes until it fits 1625 */ 1626 while (CGSIZE(&sblock) > sblock.fs_bsize) { 1627 mapcramped = 1; 1628 if (sblock.fs_bsize < MAXBSIZE) { 1629 sblock.fs_bsize <<= 1; 1630 if ((i & 1) == 0) { 1631 i >>= 1; 1632 } else { 1633 sblock.fs_cpc <<= 1; 1634 mincpc <<= 1; 1635 mincpg = roundup(mincpgcnt, mincpc); 1636 sblock.fs_cpg = mincpg; 1637 } 1638 sblock.fs_frag <<= 1; 1639 sblock.fs_fragshift += 1; 1640 if (sblock.fs_frag <= MAXFRAG) 1641 continue; 1642 } 1643 1644 /* 1645 * Looped far enough. The fragment is now as large as the 1646 * filesystem block! 1647 */ 1648 if (sblock.fs_fsize == sblock.fs_bsize) { 1649 (void) fprintf(stderr, gettext( 1650 "There is no block size that can support this disk\n")); 1651 lockexit(32); 1652 } 1653 1654 /* 1655 * Try a larger fragment. Double the fragment size. 1656 */ 1657 sblock.fs_frag >>= 1; 1658 sblock.fs_fragshift -= 1; 1659 sblock.fs_fsize <<= 1; 1660 sblock.fs_nspf <<= 1; 1661 } 1662 /* 1663 * Insure that cylinder group with mincpg has enough space for inodes 1664 */ 1665 inodecramped = 0; 1666 used *= sectorsize; 1667 nbytes64 = (uint64_t)mincpg * bpcg - used; 1668 inospercg = (uint64_t)roundup((nbytes64 / nbpi), INOPB(&sblock)); 1669 sblock.fs_ipg = (int32_t)inospercg; 1670 while (inospercg > MAXIpG(&sblock)) { 1671 inodecramped = 1; 1672 if (mincpc == 1 || sblock.fs_frag == 1 || 1673 sblock.fs_bsize == MINBSIZE) 1674 break; 1675 nbytes64 = (uint64_t)mincpg * bpcg - used; 1676 (void) fprintf(stderr, 1677 gettext("With a block size of %d %s %lu\n"), 1678 sblock.fs_bsize, gettext("minimum bytes per inode is"), 1679 (uint32_t)(nbytes64 / MAXIpG(&sblock) + 1)); 1680 sblock.fs_bsize >>= 1; 1681 sblock.fs_frag >>= 1; 1682 sblock.fs_fragshift -= 1; 1683 mincpc >>= 1; 1684 sblock.fs_cpg = roundup(mincpgcnt, mincpc); 1685 if (CGSIZE(&sblock) > sblock.fs_bsize) { 1686 sblock.fs_bsize <<= 1; 1687 break; 1688 } 1689 mincpg = sblock.fs_cpg; 1690 nbytes64 = (uint64_t)mincpg * bpcg - used; 1691 inospercg = (uint64_t)roundup((nbytes64 / nbpi), 1692 INOPB(&sblock)); 1693 sblock.fs_ipg = (int32_t)inospercg; 1694 } 1695 if (inodecramped) { 1696 if (inospercg > MAXIpG(&sblock)) { 1697 nbytes64 = (uint64_t)mincpg * bpcg - used; 1698 (void) fprintf(stderr, gettext( 1699 "Minimum bytes per inode is %d\n"), 1700 (uint32_t)(nbytes64 / MAXIpG(&sblock) + 1)); 1701 } else if (!mapcramped) { 1702 (void) fprintf(stderr, gettext( 1703 "With %ld bytes per inode, minimum cylinders per group is %ld\n"), 1704 nbpi, mincpg); 1705 } 1706 } 1707 if (mapcramped) { 1708 (void) fprintf(stderr, gettext( 1709 "With %d sectors per cylinder, minimum cylinders " 1710 "per group is %ld\n"), 1711 sblock.fs_spc, mincpg); 1712 } 1713 if (inodecramped || mapcramped) { 1714 /* 1715 * To make this at least somewhat comprehensible in 1716 * the world of i18n, figure out what we're going to 1717 * say and then say it all at one time. The days of 1718 * needing to scrimp on string space are behind us.... 1719 */ 1720 if ((sblock.fs_bsize != bsize) && 1721 (sblock.fs_fsize != fragsize)) { 1722 (void) fprintf(stderr, gettext( 1723 "This requires the block size to be changed from %ld to %d\n" 1724 "and the fragment size to be changed from %ld to %d\n"), 1725 bsize, sblock.fs_bsize, 1726 fragsize, sblock.fs_fsize); 1727 } else if (sblock.fs_bsize != bsize) { 1728 (void) fprintf(stderr, gettext( 1729 "This requires the block size to be changed from %ld to %d\n"), 1730 bsize, sblock.fs_bsize); 1731 } else if (sblock.fs_fsize != fragsize) { 1732 (void) fprintf(stderr, gettext( 1733 "This requires the fragment size to be changed from %ld to %d\n"), 1734 fragsize, sblock.fs_fsize); 1735 } else { 1736 (void) fprintf(stderr, gettext( 1737 "Unable to make filesystem fit with the given constraints\n")); 1738 } 1739 (void) fprintf(stderr, gettext( 1740 "Please re-run mkfs with corrected parameters\n")); 1741 lockexit(32); 1742 } 1743 /* 1744 * Calculate the number of cylinders per group 1745 */ 1746 sblock.fs_cpg = cpg; 1747 if (sblock.fs_cpg % mincpc != 0) { 1748 (void) fprintf(stderr, gettext( 1749 "Warning: cylinder groups must have a multiple " 1750 "of %ld cylinders with the given\n parameters\n"), 1751 mincpc); 1752 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc); 1753 (void) fprintf(stderr, gettext("Rounded cgsize up to %d\n"), 1754 sblock.fs_cpg); 1755 } 1756 /* 1757 * Must insure there is enough space for inodes 1758 */ 1759 /* if these calculations are changed, check dump_fscmd also */ 1760 nbytes64 = (uint64_t)sblock.fs_cpg * bpcg - used; 1761 sblock.fs_ipg = roundup((uint32_t)(nbytes64 / nbpi), INOPB(&sblock)); 1762 1763 /* 1764 * Slim down cylinders per group, until the inodes can fit. 1765 */ 1766 while (sblock.fs_ipg > MAXIpG(&sblock)) { 1767 inodecramped = 1; 1768 sblock.fs_cpg -= mincpc; 1769 nbytes64 = (uint64_t)sblock.fs_cpg * bpcg - used; 1770 sblock.fs_ipg = roundup((uint32_t)(nbytes64 / nbpi), 1771 INOPB(&sblock)); 1772 } 1773 /* 1774 * Must insure there is enough space to hold block map. 1775 * Cut down on cylinders per group, until the cg struct fits in a 1776 * filesystem block. 1777 */ 1778 while (CGSIZE(&sblock) > sblock.fs_bsize) { 1779 mapcramped = 1; 1780 sblock.fs_cpg -= mincpc; 1781 nbytes64 = (uint64_t)sblock.fs_cpg * bpcg - used; 1782 sblock.fs_ipg = roundup((uint32_t)(nbytes64 / nbpi), 1783 INOPB(&sblock)); 1784 } 1785 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock); 1786 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) { 1787 (void) fprintf(stderr, 1788 gettext("newfs: panic (fs_cpg * fs_spc) %% NSPF != 0\n")); 1789 lockexit(32); 1790 } 1791 if (sblock.fs_cpg < mincpg) { 1792 (void) fprintf(stderr, gettext( 1793 "With the given parameters, cgsize must be at least %ld; please re-run mkfs\n"), 1794 mincpg); 1795 lockexit(32); 1796 } 1797 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); 1798 grow20: 1799 /* 1800 * Now have size for file system and nsect and ntrak. 1801 * Determine number of cylinders and blocks in the file system. 1802 */ 1803 fssize_frag = (int64_t)dbtofsb(&sblock, fssize_db); 1804 if (fssize_frag > INT_MAX) { 1805 (void) fprintf(stderr, gettext( 1806 "There are too many fragments in the system, increase fragment size\n"), 1807 mincpg); 1808 lockexit(32); 1809 } 1810 sblock.fs_size = (int32_t)fssize_frag; 1811 sblock.fs_ncyl = (int32_t)(fssize_frag * NSPF(&sblock) / sblock.fs_spc); 1812 if (fssize_frag * NSPF(&sblock) > 1813 (uint64_t)sblock.fs_ncyl * sblock.fs_spc) { 1814 sblock.fs_ncyl++; 1815 warn = 1; 1816 } 1817 if (sblock.fs_ncyl < 1) { 1818 (void) fprintf(stderr, gettext( 1819 "file systems must have at least one cylinder\n")); 1820 lockexit(32); 1821 } 1822 if (grow) 1823 goto grow30; 1824 /* 1825 * Determine feasability/values of rotational layout tables. 1826 * 1827 * The size of the rotational layout tables is limited by the size 1828 * of the file system block, fs_bsize. The amount of space 1829 * available for tables is calculated as (fs_bsize - sizeof (struct 1830 * fs)). The size of these tables is inversely proportional to the 1831 * block size of the file system. The size increases if sectors per 1832 * track are not powers of two, because more cylinders must be 1833 * described by the tables before the rotational pattern repeats 1834 * (fs_cpc). 1835 */ 1836 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 1837 sblock.fs_sbsize = fragroundup(&sblock, sizeof (struct fs)); 1838 sblock.fs_npsect = sblock.fs_nsect; 1839 if (sblock.fs_ntrak == 1) { 1840 sblock.fs_cpc = 0; 1841 goto next; 1842 } 1843 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof (short); 1844 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock); 1845 totalsbsize = sizeof (struct fs) + rotblsize; 1846 1847 /* do static allocation if nrpos == 8 and fs_cpc == 16 */ 1848 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) { 1849 /* use old static table space */ 1850 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) - 1851 (char *)(&sblock.fs_link); 1852 sblock.fs_rotbloff = &sblock.fs_space[0] - 1853 (uchar_t *)(&sblock.fs_link); 1854 } else { 1855 /* use 4.3 dynamic table space */ 1856 sblock.fs_postbloff = &sblock.fs_space[0] - 1857 (uchar_t *)(&sblock.fs_link); 1858 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize; 1859 totalsbsize += postblsize; 1860 } 1861 if (totalsbsize > sblock.fs_bsize || 1862 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) { 1863 (void) fprintf(stderr, gettext( 1864 "Warning: insufficient space in super block for\n" 1865 "rotational layout tables with nsect %d, ntrack %d, " 1866 "and nrpos %d.\nOmitting tables - file system " 1867 "performance may be impaired.\n"), 1868 sblock.fs_nsect, sblock.fs_ntrak, sblock.fs_nrpos); 1869 1870 /* 1871 * Setting fs_cpc to 0 tells alloccgblk() in ufs_alloc.c to 1872 * ignore the positional layout table and rotational 1873 * position table. 1874 */ 1875 sblock.fs_cpc = 0; 1876 goto next; 1877 } 1878 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize); 1879 1880 1881 /* 1882 * calculate the available blocks for each rotational position 1883 */ 1884 for (cylno = 0; cylno < sblock.fs_cpc; cylno++) 1885 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++) 1886 fs_postbl(&sblock, cylno)[rpos] = -1; 1887 for (i = (rotblsize - 1) * sblock.fs_frag; 1888 i >= 0; i -= sblock.fs_frag) { 1889 cylno = cbtocylno(&sblock, i); 1890 rpos = cbtorpos(&sblock, i); 1891 blk = fragstoblks(&sblock, i); 1892 if (fs_postbl(&sblock, cylno)[rpos] == -1) 1893 fs_rotbl(&sblock)[blk] = 0; 1894 else 1895 fs_rotbl(&sblock)[blk] = 1896 fs_postbl(&sblock, cylno)[rpos] - blk; 1897 fs_postbl(&sblock, cylno)[rpos] = blk; 1898 } 1899 next: 1900 grow30: 1901 /* 1902 * Compute/validate number of cylinder groups. 1903 * Note that if an excessively large filesystem is specified 1904 * (e.g., more than 16384 cylinders for an 8K filesystem block), it 1905 * does not get detected until checksummarysize() 1906 */ 1907 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg; 1908 if (sblock.fs_ncyl % sblock.fs_cpg) 1909 sblock.fs_ncg++; 1910 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); 1911 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1); 1912 ibpcl = cgdmin(&sblock, i) - cgbase(&sblock, i); 1913 if (ibpcl >= sblock.fs_fpg) { 1914 (void) fprintf(stderr, gettext( 1915 "inode blocks/cyl group (%d) >= data blocks (%d)\n"), 1916 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag, 1917 sblock.fs_fpg / sblock.fs_frag); 1918 if ((ibpcl < 0) || (sblock.fs_fpg < 0)) { 1919 (void) fprintf(stderr, gettext( 1920 "number of cylinders per cylinder group (%d) must be decreased.\n"), 1921 sblock.fs_cpg); 1922 } else { 1923 (void) fprintf(stderr, gettext( 1924 "number of cylinders per cylinder group (%d) must be increased.\n"), 1925 sblock.fs_cpg); 1926 } 1927 (void) fprintf(stderr, gettext( 1928 "Note that cgsize may have been adjusted to allow struct cg to fit.\n")); 1929 lockexit(32); 1930 } 1931 j = sblock.fs_ncg - 1; 1932 if ((i = fssize_frag - j * sblock.fs_fpg) < sblock.fs_fpg && 1933 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) { 1934 (void) fprintf(stderr, gettext( 1935 "Warning: inode blocks/cyl group (%d) >= data " 1936 "blocks (%ld) in last\n cylinder group. This " 1937 "implies %ld sector(s) cannot be allocated.\n"), 1938 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag, 1939 i / sblock.fs_frag, i * NSPF(&sblock)); 1940 sblock.fs_ncg--; 1941 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg; 1942 sblock.fs_size = fssize_frag = 1943 (int64_t)sblock.fs_ncyl * (int64_t)sblock.fs_spc / 1944 (int64_t)NSPF(&sblock); 1945 warn = 0; 1946 } 1947 if (warn && !spc_flag) { 1948 (void) fprintf(stderr, gettext( 1949 "Warning: %d sector(s) in last cylinder unallocated\n"), 1950 sblock.fs_spc - (uint32_t)(fssize_frag * NSPF(&sblock) - 1951 (uint64_t)(sblock.fs_ncyl - 1) * sblock.fs_spc)); 1952 } 1953 /* 1954 * fill in remaining fields of the super block 1955 */ 1956 1957 /* 1958 * The csum records are stored in cylinder group 0, starting at 1959 * cgdmin, the first data block. 1960 */ 1961 sblock.fs_csaddr = cgdmin(&sblock, 0); 1962 sblock.fs_cssize = 1963 fragroundup(&sblock, sblock.fs_ncg * sizeof (struct csum)); 1964 i = sblock.fs_bsize / sizeof (struct csum); 1965 sblock.fs_csmask = ~(i - 1); 1966 for (sblock.fs_csshift = 0; i > 1; i >>= 1) 1967 sblock.fs_csshift++; 1968 fscs = (struct csum *)calloc(1, sblock.fs_cssize); 1969 1970 checksummarysize(); 1971 if (mtb == 'y') { 1972 sblock.fs_magic = MTB_UFS_MAGIC; 1973 sblock.fs_version = MTB_UFS_VERSION_1; 1974 } else { 1975 sblock.fs_magic = FS_MAGIC; 1976 if (use_efi_dflts) 1977 sblock.fs_version = UFS_EFISTYLE4NONEFI_VERSION_2; 1978 else 1979 sblock.fs_version = UFS_VERSION_MIN; 1980 } 1981 1982 if (grow) { 1983 bcopy((caddr_t)grow_fscs, (caddr_t)fscs, (int)grow_fs_cssize); 1984 extendsummaryinfo(); 1985 goto grow40; 1986 } 1987 sblock.fs_rotdelay = rotdelay; 1988 sblock.fs_maxcontig = maxcontig; 1989 sblock.fs_maxbpg = MAXBLKPG(sblock.fs_bsize); 1990 1991 sblock.fs_rps = rps; 1992 sblock.fs_cgrotor = 0; 1993 sblock.fs_cstotal.cs_ndir = 0; 1994 sblock.fs_cstotal.cs_nbfree = 0; 1995 sblock.fs_cstotal.cs_nifree = 0; 1996 sblock.fs_cstotal.cs_nffree = 0; 1997 sblock.fs_fmod = 0; 1998 sblock.fs_ronly = 0; 1999 sblock.fs_time = mkfstime; 2000 sblock.fs_state = FSOKAY - sblock.fs_time; 2001 sblock.fs_clean = FSCLEAN; 2002 grow40: 2003 2004 /* 2005 * If all that's needed is a dump of the superblock we 2006 * would use by default, we've got it now. So, splat it 2007 * out and leave. 2008 */ 2009 if (rflag) { 2010 dump_sblock(); 2011 lockexit(0); 2012 } 2013 /* 2014 * Dump out summary information about file system. 2015 */ 2016 (void) fprintf(stderr, gettext( 2017 "%s:\t%lld sectors in %d cylinders of %d tracks, %d sectors\n"), 2018 fsys, (uint64_t)sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl, 2019 sblock.fs_ntrak, sblock.fs_nsect); 2020 (void) fprintf(stderr, gettext( 2021 "\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n"), 2022 (float)sblock.fs_size * sblock.fs_fsize / MB, sblock.fs_ncg, 2023 sblock.fs_cpg, (float)sblock.fs_fpg * sblock.fs_fsize / MB, 2024 sblock.fs_ipg); 2025 2026 tmpbuf = calloc(sblock.fs_ncg / 50 + 500, 1); 2027 if (tmpbuf == NULL) { 2028 perror("calloc"); 2029 lockexit(32); 2030 } 2031 /* 2032 * Now build the cylinders group blocks and 2033 * then print out indices of cylinder groups. 2034 */ 2035 tprintf(gettext( 2036 "super-block backups (for fsck -F ufs -o b=#) at:\n")); 2037 for (width = cylno = 0; cylno < sblock.fs_ncg && cylno < 10; cylno++) { 2038 if ((grow == 0) || (cylno >= grow_fs_ncg)) 2039 initcg(cylno); 2040 num = fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno)); 2041 /* 2042 * If Nflag and if the disk is larger than the CHSLIMIT, 2043 * then sanity test the superblocks before reporting. If there 2044 * are too many superblocks which look insane, we probably 2045 * have to retry with alternate logic. If we are already 2046 * retrying, then our efforts to arrive at alternate 2047 * superblocks failed, so complain and exit. 2048 */ 2049 if (Nflag && retry) { 2050 skip_this_sb = 0; 2051 rdfs((diskaddr_t)num, sbsize, (char *)&altsblock); 2052 ret = checksblock(altsblock, 1); 2053 if (ret) { 2054 skip_this_sb = 1; 2055 invalid_sb_cnt++; 2056 dprintf(("DeBuG checksblock() failed - error : %d" 2057 " for sb : %llu invalid_sb_cnt : %d\n", 2058 ret, num, invalid_sb_cnt)); 2059 } else { 2060 /* 2061 * Though the superblock looks sane, verify if the 2062 * fs_version in the superblock and the logic that 2063 * we are using to arrive at the superblocks match. 2064 */ 2065 if (use_efi_dflts && altsblock.fs_version 2066 != UFS_EFISTYLE4NONEFI_VERSION_2) { 2067 skip_this_sb = 1; 2068 invalid_sb_cnt++; 2069 } 2070 } 2071 if (invalid_sb_cnt >= INVALIDSBLIMIT) { 2072 if (retry > 1) { 2073 (void) fprintf(stderr, gettext( 2074 "Error determining alternate " 2075 "superblock locations\n")); 2076 free(tmpbuf); 2077 lockexit(32); 2078 } 2079 retry++; 2080 use_efi_dflts = !use_efi_dflts; 2081 nsect = save_nsect; 2082 ntrack = save_ntrack; 2083 cpg = save_cpg; 2084 free(tmpbuf); 2085 goto retry_alternate_logic; 2086 } 2087 if (skip_this_sb) 2088 continue; 2089 } 2090 (void) sprintf(pbuf, " %llu,", num); 2091 plen = strlen(pbuf); 2092 if ((width + plen) > (WIDTH - 1)) { 2093 width = plen; 2094 tprintf("\n"); 2095 } else { 2096 width += plen; 2097 } 2098 if (Nflag && retry) 2099 strncat(tmpbuf, pbuf, strlen(pbuf)); 2100 else 2101 (void) fprintf(stderr, "%s", pbuf); 2102 } 2103 tprintf("\n"); 2104 2105 remaining_cg = sblock.fs_ncg - cylno; 2106 2107 /* 2108 * If there are more than 300 cylinder groups still to be 2109 * initialized, print a "." for every 50 cylinder groups. 2110 */ 2111 if (remaining_cg > 300) { 2112 tprintf(gettext("Initializing cylinder groups:\n")); 2113 do_dot = 1; 2114 } 2115 2116 /* 2117 * Now initialize all cylinder groups between the first ten 2118 * and the last ten. 2119 * 2120 * If the number of cylinder groups was less than 10, all of the 2121 * cylinder group offsets would have printed in the last loop 2122 * and cylno will already be equal to sblock.fs_ncg and so this 2123 * loop will not be entered. If there are less than 20 cylinder 2124 * groups, cylno is already less than fs_ncg - 10, so this loop 2125 * won't be entered in that case either. 2126 */ 2127 2128 i = 0; 2129 for (; cylno < sblock.fs_ncg - 10; cylno++) { 2130 if ((grow == 0) || (cylno >= grow_fs_ncg)) 2131 initcg(cylno); 2132 if (do_dot && cylno % 50 == 0) { 2133 tprintf("."); 2134 i++; 2135 if (i == WIDTH - 1) { 2136 tprintf("\n"); 2137 i = 0; 2138 } 2139 } 2140 } 2141 2142 /* 2143 * Now print the cylinder group offsets for the last 10 2144 * cylinder groups, if any are left. 2145 */ 2146 2147 if (do_dot) { 2148 tprintf(gettext( 2149 "\nsuper-block backups for last 10 cylinder groups at:\n")); 2150 } 2151 for (width = 0; cylno < sblock.fs_ncg; cylno++) { 2152 if ((grow == 0) || (cylno >= grow_fs_ncg)) 2153 initcg(cylno); 2154 num = fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno)); 2155 if (Nflag && retry) { 2156 skip_this_sb = 0; 2157 rdfs((diskaddr_t)num, sbsize, (char *)&altsblock); 2158 ret = checksblock(altsblock, 1); 2159 if (ret) { 2160 skip_this_sb = 1; 2161 invalid_sb_cnt++; 2162 dprintf(("DeBuG checksblock() failed - error : %d" 2163 " for sb : %llu invalid_sb_cnt : %d\n", 2164 ret, num, invalid_sb_cnt)); 2165 } else { 2166 /* 2167 * Though the superblock looks sane, verify if the 2168 * fs_version in the superblock and the logic that 2169 * we are using to arrive at the superblocks match. 2170 */ 2171 if (use_efi_dflts && altsblock.fs_version 2172 != UFS_EFISTYLE4NONEFI_VERSION_2) { 2173 skip_this_sb = 1; 2174 invalid_sb_cnt++; 2175 } 2176 } 2177 if (invalid_sb_cnt >= INVALIDSBLIMIT) { 2178 if (retry > 1) { 2179 (void) fprintf(stderr, gettext( 2180 "Error determining alternate " 2181 "superblock locations\n")); 2182 free(tmpbuf); 2183 lockexit(32); 2184 } 2185 retry++; 2186 use_efi_dflts = !use_efi_dflts; 2187 nsect = save_nsect; 2188 ntrack = save_ntrack; 2189 cpg = save_cpg; 2190 free(tmpbuf); 2191 goto retry_alternate_logic; 2192 } 2193 if (skip_this_sb) 2194 continue; 2195 } 2196 /* Don't print ',' for the last superblock */ 2197 if (cylno == sblock.fs_ncg-1) 2198 (void) sprintf(pbuf, " %llu", num); 2199 else 2200 (void) sprintf(pbuf, " %llu,", num); 2201 plen = strlen(pbuf); 2202 if ((width + plen) > (WIDTH - 1)) { 2203 width = plen; 2204 tprintf("\n"); 2205 } else { 2206 width += plen; 2207 } 2208 if (Nflag && retry) 2209 strncat(tmpbuf, pbuf, strlen(pbuf)); 2210 else 2211 (void) fprintf(stderr, "%s", pbuf); 2212 } 2213 tprintf("\n"); 2214 if (Nflag) { 2215 if (retry) 2216 fprintf(stderr, "%s", tmpbuf); 2217 free(tmpbuf); 2218 lockexit(0); 2219 } 2220 2221 free(tmpbuf); 2222 if (grow) 2223 goto grow50; 2224 2225 /* 2226 * Now construct the initial file system, 2227 * then write out the super-block. 2228 */ 2229 fsinit(); 2230 grow50: 2231 /* 2232 * write the superblock and csum information 2233 */ 2234 wtsb(); 2235 2236 /* 2237 * extend the last cylinder group in the original file system 2238 */ 2239 if (grow) { 2240 extendcg(grow_fs_ncg-1); 2241 wtsb(); 2242 } 2243 2244 /* 2245 * Write out the duplicate super blocks to the first 10 2246 * cylinder groups (or fewer, if there are fewer than 10 2247 * cylinder groups). 2248 */ 2249 for (cylno = 0; cylno < sblock.fs_ncg && cylno < 10; cylno++) 2250 awtfs(fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno)), 2251 (int)sbsize, (char *)&sblock, SAVE); 2252 2253 /* 2254 * Now write out duplicate super blocks to the remaining 2255 * cylinder groups. In the case of multi-terabyte file 2256 * systems, just write out the super block to the last ten 2257 * cylinder groups (or however many are left). 2258 */ 2259 if (mtb == 'y') { 2260 if (sblock.fs_ncg <= 10) 2261 cylno = sblock.fs_ncg; 2262 else if (sblock.fs_ncg <= 20) 2263 cylno = 10; 2264 else 2265 cylno = sblock.fs_ncg - 10; 2266 } 2267 2268 for (; cylno < sblock.fs_ncg; cylno++) 2269 awtfs(fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno)), 2270 (int)sbsize, (char *)&sblock, SAVE); 2271 2272 /* 2273 * Flush out all the AIO writes we've done. It's not 2274 * necessary to do this explicitly, but it's the only 2275 * way to report any errors from those writes. 2276 */ 2277 flush_writes(); 2278 2279 /* 2280 * set clean flag 2281 */ 2282 if (grow) 2283 sblock.fs_clean = grow_fs_clean; 2284 else 2285 sblock.fs_clean = FSCLEAN; 2286 sblock.fs_time = mkfstime; 2287 sblock.fs_state = FSOKAY - sblock.fs_time; 2288 wtfs((diskaddr_t)(SBOFF / sectorsize), sbsize, (char *)&sblock); 2289 isbad = 0; 2290 2291 if (fsync(fso) == -1) { 2292 saverr = errno; 2293 (void) fprintf(stderr, 2294 gettext("mkfs: fsync failed on write disk: %s\n"), 2295 strerror(saverr)); 2296 /* we're just cleaning up, so keep going */ 2297 } 2298 if (close(fsi) == -1) { 2299 saverr = errno; 2300 (void) fprintf(stderr, 2301 gettext("mkfs: close failed on read disk: %s\n"), 2302 strerror(saverr)); 2303 /* we're just cleaning up, so keep going */ 2304 } 2305 if (close(fso) == -1) { 2306 saverr = errno; 2307 (void) fprintf(stderr, 2308 gettext("mkfs: close failed on write disk: %s\n"), 2309 strerror(saverr)); 2310 /* we're just cleaning up, so keep going */ 2311 } 2312 fsi = fso = -1; 2313 return (0); 2314 #ifndef STANDALONE 2315 lockexit(0); 2316 #endif 2317 } 2318 2319 /* 2320 * Figure out how big the partition we're dealing with is. 2321 * The value returned is in disk blocks (sectors); 2322 */ 2323 static diskaddr_t 2324 get_max_size(int fd) 2325 { 2326 struct vtoc vtoc; 2327 dk_gpt_t *efi_vtoc; 2328 diskaddr_t slicesize; 2329 2330 int index = read_vtoc(fd, &vtoc); 2331 2332 if (index >= 0) { 2333 label_type = LABEL_TYPE_VTOC; 2334 } else { 2335 if (index == VT_ENOTSUP || index == VT_ERROR) { 2336 /* it might be an EFI label */ 2337 index = efi_alloc_and_read(fd, &efi_vtoc); 2338 label_type = LABEL_TYPE_EFI; 2339 } 2340 } 2341 2342 if (index < 0) { 2343 switch (index) { 2344 case VT_ERROR: 2345 break; 2346 case VT_EIO: 2347 errno = EIO; 2348 break; 2349 case VT_EINVAL: 2350 errno = EINVAL; 2351 } 2352 perror(gettext("Can not determine partition size")); 2353 lockexit(32); 2354 } 2355 2356 if (label_type == LABEL_TYPE_EFI) { 2357 slicesize = efi_vtoc->efi_parts[index].p_size; 2358 efi_free(efi_vtoc); 2359 } else { 2360 /* 2361 * In the vtoc struct, p_size is a 32-bit signed quantity. 2362 * In the dk_gpt struct (efi's version of the vtoc), p_size 2363 * is an unsigned 64-bit quantity. By casting the vtoc's 2364 * psize to an unsigned 32-bit quantity, it will be copied 2365 * to 'slicesize' (an unsigned 64-bit diskaddr_t) without 2366 * sign extension. 2367 */ 2368 2369 slicesize = (uint32_t)vtoc.v_part[index].p_size; 2370 } 2371 2372 dprintf(("DeBuG get_max_size index = %d, p_size = %lld, dolimit = %d\n", 2373 index, slicesize, (slicesize > FS_MAX))); 2374 2375 /* 2376 * The next line limits a UFS file system to the maximum 2377 * supported size. 2378 */ 2379 2380 if (slicesize > FS_MAX) 2381 return (FS_MAX); 2382 return (slicesize); 2383 } 2384 2385 static long 2386 get_max_track_size(int fd) 2387 { 2388 struct dk_cinfo ci; 2389 long track_size = -1; 2390 2391 if (ioctl(fd, DKIOCINFO, &ci) == 0) { 2392 track_size = ci.dki_maxtransfer * DEV_BSIZE; 2393 } 2394 2395 if ((track_size < 0)) { 2396 int error = 0; 2397 int maxphys; 2398 int gotit = 0; 2399 2400 gotit = fsgetmaxphys(&maxphys, &error); 2401 if (gotit) { 2402 track_size = MIN(MB, maxphys); 2403 } else { 2404 (void) fprintf(stderr, gettext( 2405 "Warning: Could not get system value for maxphys. The value for\n" 2406 "maxcontig will default to 1MB.\n")); 2407 track_size = MB; 2408 } 2409 } 2410 return (track_size); 2411 } 2412 2413 /* 2414 * Initialize a cylinder group. 2415 */ 2416 static void 2417 initcg(int cylno) 2418 { 2419 diskaddr_t cbase, d; 2420 diskaddr_t dlower; /* last data block before cg metadata */ 2421 diskaddr_t dupper; /* first data block after cg metadata */ 2422 diskaddr_t dmax; 2423 int64_t i; 2424 struct csum *cs; 2425 struct dinode *inode_buffer; 2426 int size; 2427 2428 /* 2429 * Variables used to store intermediate results as a part of 2430 * the internal implementation of the cbtocylno() macros. 2431 */ 2432 diskaddr_t bno; /* UFS block number (not sector number) */ 2433 int cbcylno; /* current cylinder number */ 2434 int cbcylno_sect; /* sector offset within cylinder */ 2435 int cbsect_incr; /* amount to increment sector offset */ 2436 2437 /* 2438 * Variables used to store intermediate results as a part of 2439 * the internal implementation of the cbtorpos() macros. 2440 */ 2441 short *cgblks; /* pointer to array of free blocks in cg */ 2442 int trackrpos; /* tmp variable for rotation position */ 2443 int trackoff; /* offset within a track */ 2444 int trackoff_incr; /* amount to increment trackoff */ 2445 int rpos; /* rotation position of current block */ 2446 int rpos_incr; /* amount to increment rpos per block */ 2447 2448 union cgun *icgun; /* local pointer to a cg summary block */ 2449 #define icg (icgun->cg) 2450 2451 icgun = (union cgun *)getbuf(&cgsumbuf, sizeof (union cgun)); 2452 2453 /* 2454 * Determine block bounds for cylinder group. 2455 * Allow space for super block summary information in first 2456 * cylinder group. 2457 */ 2458 cbase = cgbase(&sblock, cylno); 2459 dmax = cbase + sblock.fs_fpg; 2460 if (dmax > sblock.fs_size) /* last cg may be smaller than normal */ 2461 dmax = sblock.fs_size; 2462 dlower = cgsblock(&sblock, cylno) - cbase; 2463 dupper = cgdmin(&sblock, cylno) - cbase; 2464 if (cylno == 0) 2465 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 2466 cs = fscs + cylno; 2467 icg.cg_time = mkfstime; 2468 icg.cg_magic = CG_MAGIC; 2469 icg.cg_cgx = cylno; 2470 /* last one gets whatever's left */ 2471 if (cylno == sblock.fs_ncg - 1) 2472 icg.cg_ncyl = sblock.fs_ncyl - (sblock.fs_cpg * cylno); 2473 else 2474 icg.cg_ncyl = sblock.fs_cpg; 2475 icg.cg_niblk = sblock.fs_ipg; 2476 icg.cg_ndblk = dmax - cbase; 2477 icg.cg_cs.cs_ndir = 0; 2478 icg.cg_cs.cs_nffree = 0; 2479 icg.cg_cs.cs_nbfree = 0; 2480 icg.cg_cs.cs_nifree = 0; 2481 icg.cg_rotor = 0; 2482 icg.cg_frotor = 0; 2483 icg.cg_irotor = 0; 2484 icg.cg_btotoff = &icg.cg_space[0] - (uchar_t *)(&icg.cg_link); 2485 icg.cg_boff = icg.cg_btotoff + sblock.fs_cpg * sizeof (long); 2486 icg.cg_iusedoff = icg.cg_boff + 2487 sblock.fs_cpg * sblock.fs_nrpos * sizeof (short); 2488 icg.cg_freeoff = icg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY); 2489 icg.cg_nextfreeoff = icg.cg_freeoff + 2490 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY); 2491 for (i = 0; i < sblock.fs_frag; i++) { 2492 icg.cg_frsum[i] = 0; 2493 } 2494 bzero((caddr_t)cg_inosused(&icg), icg.cg_freeoff - icg.cg_iusedoff); 2495 icg.cg_cs.cs_nifree += sblock.fs_ipg; 2496 if (cylno == 0) 2497 for (i = 0; i < UFSROOTINO; i++) { 2498 setbit(cg_inosused(&icg), i); 2499 icg.cg_cs.cs_nifree--; 2500 } 2501 2502 /* 2503 * Initialize all the inodes in the cylinder group using 2504 * random numbers. 2505 */ 2506 size = sblock.fs_ipg * sizeof (struct dinode); 2507 inode_buffer = (struct dinode *)getbuf(&inodebuf, size); 2508 2509 for (i = 0; i < sblock.fs_ipg; i++) { 2510 IRANDOMIZE(&(inode_buffer[i].di_ic)); 2511 } 2512 2513 /* 2514 * Write all inodes in a single write for performance. 2515 */ 2516 awtfs(fsbtodb(&sblock, (uint64_t)cgimin(&sblock, cylno)), (int)size, 2517 (char *)inode_buffer, RELEASE); 2518 2519 bzero((caddr_t)cg_blktot(&icg), icg.cg_boff - icg.cg_btotoff); 2520 bzero((caddr_t)cg_blks(&sblock, &icg, 0), 2521 icg.cg_iusedoff - icg.cg_boff); 2522 bzero((caddr_t)cg_blksfree(&icg), icg.cg_nextfreeoff - icg.cg_freeoff); 2523 2524 if (cylno > 0) { 2525 for (d = 0; d < dlower; d += sblock.fs_frag) { 2526 setblock(&sblock, cg_blksfree(&icg), d/sblock.fs_frag); 2527 icg.cg_cs.cs_nbfree++; 2528 cg_blktot(&icg)[cbtocylno(&sblock, d)]++; 2529 cg_blks(&sblock, &icg, cbtocylno(&sblock, d)) 2530 [cbtorpos(&sblock, d)]++; 2531 } 2532 sblock.fs_dsize += dlower; 2533 } 2534 sblock.fs_dsize += icg.cg_ndblk - dupper; 2535 if ((i = dupper % sblock.fs_frag) != 0) { 2536 icg.cg_frsum[sblock.fs_frag - i]++; 2537 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 2538 setbit(cg_blksfree(&icg), dupper); 2539 icg.cg_cs.cs_nffree++; 2540 } 2541 } 2542 2543 /* 2544 * WARNING: The following code is somewhat confusing, but 2545 * results in a substantial performance improvement in mkfs. 2546 * 2547 * Instead of using cbtocylno() and cbtorpos() macros, we 2548 * keep track of all the intermediate state of those macros 2549 * in some variables. This allows simple addition to be 2550 * done to calculate the results as we step through the 2551 * blocks in an orderly fashion instead of the slower 2552 * multiplication and division the macros are forced to 2553 * used so they can support random input. (Multiplication, 2554 * division, and remainder operations typically take about 2555 * 10x as many processor cycles as other operations.) 2556 * 2557 * The basic idea is to take code: 2558 * 2559 * for (x = starting_x; x < max; x++) 2560 * y = (x * c) / z 2561 * 2562 * and rewrite it to take advantage of the fact that 2563 * the variable x is incrementing in an orderly way: 2564 * 2565 * intermediate = starting_x * c 2566 * yval = intermediate / z 2567 * for (x = starting_x; x < max; x++) { 2568 * y = yval; 2569 * intermediate += c 2570 * if (intermediate > z) { 2571 * yval++; 2572 * intermediate -= z 2573 * } 2574 * } 2575 * 2576 * Performance has improved as much as 4X using this code. 2577 */ 2578 2579 /* 2580 * Initialize the starting points for all the cbtocylno() 2581 * macro variables and figure out the increments needed each 2582 * time through the loop. 2583 */ 2584 cbcylno_sect = dupper * NSPF(&sblock); 2585 cbsect_incr = sblock.fs_frag * NSPF(&sblock); 2586 cbcylno = cbcylno_sect / sblock.fs_spc; 2587 cbcylno_sect %= sblock.fs_spc; 2588 cgblks = cg_blks(&sblock, &icg, cbcylno); 2589 bno = dupper / sblock.fs_frag; 2590 2591 /* 2592 * Initialize the starting points for all the cbtorpos() 2593 * macro variables and figure out the increments needed each 2594 * time through the loop. 2595 * 2596 * It's harder to simplify the cbtorpos() macro if there were 2597 * alternate sectors specified (or if they previously existed 2598 * in the growfs case). Since this is rare, we just revert to 2599 * using the macros in this case and skip the variable setup. 2600 */ 2601 if (!spc_flag) { 2602 trackrpos = (cbcylno_sect % sblock.fs_nsect) * sblock.fs_nrpos; 2603 rpos = trackrpos / sblock.fs_nsect; 2604 trackoff = trackrpos % sblock.fs_nsect; 2605 trackoff_incr = cbsect_incr * sblock.fs_nrpos; 2606 rpos_incr = (trackoff_incr / sblock.fs_nsect) % sblock.fs_nrpos; 2607 trackoff_incr = trackoff_incr % sblock.fs_nsect; 2608 } 2609 2610 /* 2611 * Loop through all the blocks, marking them free and 2612 * updating totals kept in the superblock and cg summary. 2613 */ 2614 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) { 2615 setblock(&sblock, cg_blksfree(&icg), bno); 2616 icg.cg_cs.cs_nbfree++; 2617 2618 cg_blktot(&icg)[cbcylno]++; 2619 2620 if (!spc_flag) 2621 cgblks[rpos]++; 2622 else 2623 cg_blks(&sblock, &icg, cbtocylno(&sblock, d)) 2624 [cbtorpos(&sblock, d)]++; 2625 2626 d += sblock.fs_frag; 2627 bno++; 2628 2629 /* 2630 * Increment the sector offset within the cylinder 2631 * for the cbtocylno() macro reimplementation. If 2632 * we're beyond the end of the cylinder, update the 2633 * cylinder number, calculate the offset in the 2634 * new cylinder, and update the cgblks pointer 2635 * to the next rotational position. 2636 */ 2637 cbcylno_sect += cbsect_incr; 2638 if (cbcylno_sect >= sblock.fs_spc) { 2639 cbcylno++; 2640 cbcylno_sect -= sblock.fs_spc; 2641 cgblks += sblock.fs_nrpos; 2642 } 2643 2644 /* 2645 * If there aren't alternate sectors, increment the 2646 * rotational position variables for the cbtorpos() 2647 * reimplementation. Note that we potentially 2648 * increment rpos twice. Once by rpos_incr, and one 2649 * more time when we wrap to a new track because 2650 * trackoff >= fs_nsect. 2651 */ 2652 if (!spc_flag) { 2653 trackoff += trackoff_incr; 2654 rpos += rpos_incr; 2655 if (trackoff >= sblock.fs_nsect) { 2656 trackoff -= sblock.fs_nsect; 2657 rpos++; 2658 } 2659 if (rpos >= sblock.fs_nrpos) 2660 rpos -= sblock.fs_nrpos; 2661 } 2662 } 2663 2664 if (d < dmax - cbase) { 2665 icg.cg_frsum[dmax - cbase - d]++; 2666 for (; d < dmax - cbase; d++) { 2667 setbit(cg_blksfree(&icg), d); 2668 icg.cg_cs.cs_nffree++; 2669 } 2670 } 2671 sblock.fs_cstotal.cs_ndir += icg.cg_cs.cs_ndir; 2672 sblock.fs_cstotal.cs_nffree += icg.cg_cs.cs_nffree; 2673 sblock.fs_cstotal.cs_nbfree += icg.cg_cs.cs_nbfree; 2674 sblock.fs_cstotal.cs_nifree += icg.cg_cs.cs_nifree; 2675 *cs = icg.cg_cs; 2676 awtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, cylno)), 2677 sblock.fs_bsize, (char *)&icg, RELEASE); 2678 } 2679 2680 /* 2681 * initialize the file system 2682 */ 2683 struct inode node; 2684 2685 #define LOSTDIR 2686 #ifdef LOSTDIR 2687 #define PREDEFDIR 3 2688 #else 2689 #define PREDEFDIR 2 2690 #endif 2691 2692 struct direct root_dir[] = { 2693 { UFSROOTINO, sizeof (struct direct), 1, "." }, 2694 { UFSROOTINO, sizeof (struct direct), 2, ".." }, 2695 #ifdef LOSTDIR 2696 { LOSTFOUNDINO, sizeof (struct direct), 10, "lost+found" }, 2697 #endif 2698 }; 2699 #ifdef LOSTDIR 2700 struct direct lost_found_dir[] = { 2701 { LOSTFOUNDINO, sizeof (struct direct), 1, "." }, 2702 { UFSROOTINO, sizeof (struct direct), 2, ".." }, 2703 { 0, DIRBLKSIZ, 0, 0 }, 2704 }; 2705 #endif 2706 char buf[MAXBSIZE]; 2707 2708 static void 2709 fsinit() 2710 { 2711 int i; 2712 2713 2714 /* 2715 * initialize the node 2716 */ 2717 node.i_atime = mkfstime; 2718 node.i_mtime = mkfstime; 2719 node.i_ctime = mkfstime; 2720 #ifdef LOSTDIR 2721 /* 2722 * create the lost+found directory 2723 */ 2724 (void) makedir(lost_found_dir, 2); 2725 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) { 2726 bcopy(&lost_found_dir[2], &buf[i], DIRSIZ(&lost_found_dir[2])); 2727 } 2728 node.i_number = LOSTFOUNDINO; 2729 node.i_smode = node.i_mode = IFDIR | 0700; 2730 node.i_nlink = 2; 2731 node.i_size = sblock.fs_bsize; 2732 node.i_db[0] = alloc((int)node.i_size, node.i_mode); 2733 node.i_blocks = btodb(fragroundup(&sblock, (int)node.i_size)); 2734 IRANDOMIZE(&node.i_ic); 2735 wtfs(fsbtodb(&sblock, (uint64_t)node.i_db[0]), (int)node.i_size, buf); 2736 iput(&node); 2737 #endif 2738 /* 2739 * create the root directory 2740 */ 2741 node.i_number = UFSROOTINO; 2742 node.i_mode = node.i_smode = IFDIR | UMASK; 2743 node.i_nlink = PREDEFDIR; 2744 node.i_size = makedir(root_dir, PREDEFDIR); 2745 node.i_db[0] = alloc(sblock.fs_fsize, node.i_mode); 2746 /* i_size < 2GB because we are initializing the file system */ 2747 node.i_blocks = btodb(fragroundup(&sblock, (int)node.i_size)); 2748 IRANDOMIZE(&node.i_ic); 2749 wtfs(fsbtodb(&sblock, (uint64_t)node.i_db[0]), sblock.fs_fsize, buf); 2750 iput(&node); 2751 } 2752 2753 /* 2754 * construct a set of directory entries in "buf". 2755 * return size of directory. 2756 */ 2757 static int 2758 makedir(struct direct *protodir, int entries) 2759 { 2760 char *cp; 2761 int i; 2762 ushort_t spcleft; 2763 2764 spcleft = DIRBLKSIZ; 2765 for (cp = buf, i = 0; i < entries - 1; i++) { 2766 protodir[i].d_reclen = DIRSIZ(&protodir[i]); 2767 bcopy(&protodir[i], cp, protodir[i].d_reclen); 2768 cp += protodir[i].d_reclen; 2769 spcleft -= protodir[i].d_reclen; 2770 } 2771 protodir[i].d_reclen = spcleft; 2772 bcopy(&protodir[i], cp, DIRSIZ(&protodir[i])); 2773 return (DIRBLKSIZ); 2774 } 2775 2776 /* 2777 * allocate a block or frag 2778 */ 2779 static daddr32_t 2780 alloc(int size, int mode) 2781 { 2782 int i, frag; 2783 daddr32_t d; 2784 2785 rdfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, 0)), sblock.fs_cgsize, 2786 (char *)&acg); 2787 if (acg.cg_magic != CG_MAGIC) { 2788 (void) fprintf(stderr, gettext("cg 0: bad magic number\n")); 2789 lockexit(32); 2790 } 2791 if (acg.cg_cs.cs_nbfree == 0) { 2792 (void) fprintf(stderr, 2793 gettext("first cylinder group ran out of space\n")); 2794 lockexit(32); 2795 } 2796 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag) 2797 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) 2798 goto goth; 2799 (void) fprintf(stderr, 2800 gettext("internal error: can't find block in cyl 0\n")); 2801 lockexit(32); 2802 goth: 2803 clrblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag); 2804 acg.cg_cs.cs_nbfree--; 2805 sblock.fs_cstotal.cs_nbfree--; 2806 fscs[0].cs_nbfree--; 2807 if (mode & IFDIR) { 2808 acg.cg_cs.cs_ndir++; 2809 sblock.fs_cstotal.cs_ndir++; 2810 fscs[0].cs_ndir++; 2811 } 2812 cg_blktot(&acg)[cbtocylno(&sblock, d)]--; 2813 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--; 2814 if (size != sblock.fs_bsize) { 2815 frag = howmany(size, sblock.fs_fsize); 2816 fscs[0].cs_nffree += sblock.fs_frag - frag; 2817 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; 2818 acg.cg_cs.cs_nffree += sblock.fs_frag - frag; 2819 acg.cg_frsum[sblock.fs_frag - frag]++; 2820 for (i = frag; i < sblock.fs_frag; i++) 2821 setbit(cg_blksfree(&acg), d + i); 2822 } 2823 wtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, 0)), sblock.fs_cgsize, 2824 (char *)&acg); 2825 return (d); 2826 } 2827 2828 /* 2829 * Allocate an inode on the disk 2830 */ 2831 static void 2832 iput(struct inode *ip) 2833 { 2834 struct dinode buf[MAXINOPB]; 2835 diskaddr_t d; 2836 2837 rdfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, 0)), sblock.fs_cgsize, 2838 (char *)&acg); 2839 if (acg.cg_magic != CG_MAGIC) { 2840 (void) fprintf(stderr, gettext("cg 0: bad magic number\n")); 2841 lockexit(32); 2842 } 2843 acg.cg_cs.cs_nifree--; 2844 setbit(cg_inosused(&acg), ip->i_number); 2845 wtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, 0)), sblock.fs_cgsize, 2846 (char *)&acg); 2847 sblock.fs_cstotal.cs_nifree--; 2848 fscs[0].cs_nifree--; 2849 if ((int)ip->i_number >= sblock.fs_ipg * sblock.fs_ncg) { 2850 (void) fprintf(stderr, 2851 gettext("fsinit: inode value out of range (%d).\n"), 2852 ip->i_number); 2853 lockexit(32); 2854 } 2855 d = fsbtodb(&sblock, (uint64_t)itod(&sblock, (int)ip->i_number)); 2856 rdfs(d, sblock.fs_bsize, (char *)buf); 2857 buf[itoo(&sblock, (int)ip->i_number)].di_ic = ip->i_ic; 2858 wtfs(d, sblock.fs_bsize, (char *)buf); 2859 } 2860 2861 /* 2862 * getbuf() -- Get a buffer for use in an AIO operation. Buffer 2863 * is zero'd the first time returned, left with whatever 2864 * was in memory after that. This function actually gets 2865 * enough memory the first time it's called to support 2866 * MAXBUF buffers like a slab allocator. When all the 2867 * buffers are in use, it waits for an aio to complete 2868 * and make a buffer available. 2869 * 2870 * Never returns an error. Either succeeds or exits. 2871 */ 2872 static char * 2873 getbuf(bufhdr *bufhead, int size) 2874 { 2875 bufhdr *pbuf; 2876 bufhdr *prev; 2877 int i; 2878 int buf_size, max_bufs; 2879 2880 /* 2881 * Initialize all the buffers 2882 */ 2883 if (bufhead->head == NULL) { 2884 /* 2885 * round up the size of our buffer header to a 2886 * 16 byte boundary so the address we return to 2887 * the caller is "suitably aligned". 2888 */ 2889 bufhdrsize = (sizeof (bufhdr) + 15) & ~15; 2890 2891 /* 2892 * Add in our header to the buffer and round it all up to 2893 * a 16 byte boundry so each member of the slab is aligned. 2894 */ 2895 buf_size = (size + bufhdrsize + 15) & ~15; 2896 2897 /* 2898 * Limit number of buffers to lesser of MAXBUFMEM's worth 2899 * or MAXBUF, whichever is less. 2900 */ 2901 max_bufs = MAXBUFMEM / buf_size; 2902 if (max_bufs > MAXBUF) 2903 max_bufs = MAXBUF; 2904 2905 pbuf = (bufhdr *)calloc(max_bufs, buf_size); 2906 if (pbuf == NULL) { 2907 perror("calloc"); 2908 lockexit(32); 2909 } 2910 2911 bufhead->head = bufhead; 2912 prev = bufhead; 2913 for (i = 0; i < max_bufs; i++) { 2914 pbuf->head = bufhead; 2915 prev->next = pbuf; 2916 prev = pbuf; 2917 pbuf = (bufhdr *)((char *)pbuf + buf_size); 2918 } 2919 } 2920 2921 /* 2922 * Get an available buffer, waiting for I/O if necessary 2923 */ 2924 wait_for_write(NOBLOCK); 2925 while (bufhead->next == NULL) 2926 wait_for_write(BLOCK); 2927 2928 /* 2929 * Take the buffer off the list 2930 */ 2931 pbuf = bufhead->next; 2932 bufhead->next = pbuf->next; 2933 pbuf->next = NULL; 2934 2935 /* 2936 * return the empty buffer space just past the header 2937 */ 2938 return ((char *)pbuf + bufhdrsize); 2939 } 2940 2941 /* 2942 * freebuf() -- Free a buffer gotten previously through getbuf. 2943 * Puts the buffer back on the appropriate list for 2944 * later use. Never calls free(). 2945 * 2946 * Assumes that SIGINT is blocked. 2947 */ 2948 static void 2949 freebuf(char *buf) 2950 { 2951 bufhdr *pbuf; 2952 bufhdr *bufhead; 2953 2954 /* 2955 * get the header for this buffer 2956 */ 2957 pbuf = (bufhdr *)(buf - bufhdrsize); 2958 2959 /* 2960 * Put it back on the list of available buffers 2961 */ 2962 bufhead = pbuf->head; 2963 pbuf->next = bufhead->next; 2964 bufhead->next = pbuf; 2965 } 2966 2967 /* 2968 * freetrans() -- Free a transaction gotten previously through getaiop. 2969 * Puts the transaction struct back on the appropriate list for 2970 * later use. Never calls free(). 2971 * 2972 * Assumes that SIGINT is blocked. 2973 */ 2974 static void 2975 freetrans(aio_trans *transp) 2976 { 2977 /* 2978 * free the buffer associated with this AIO if needed 2979 */ 2980 if (transp->release == RELEASE) 2981 freebuf(transp->buffer); 2982 2983 /* 2984 * Put transaction on the free list 2985 */ 2986 transp->next = results.trans; 2987 results.trans = transp; 2988 } 2989 2990 /* 2991 * wait_for_write() -- Wait for an aio write to complete. Return 2992 * the transaction structure for that write. 2993 * 2994 * Blocks SIGINT if necessary. 2995 */ 2996 aio_trans * 2997 wait_for_write(int block) 2998 { 2999 aio_trans *transp; 3000 aio_result_t *resultp; 3001 static struct timeval zero_wait = { 0, 0 }; 3002 sigset_t old_mask; 3003 3004 /* 3005 * If we know there aren't any outstanding transactions, just return 3006 */ 3007 if (results.outstanding == 0) 3008 return ((aio_trans *) 0); 3009 3010 block_sigint(&old_mask); 3011 3012 resultp = aiowait(block ? NULL : &zero_wait); 3013 if (resultp == NULL || 3014 (resultp == (aio_result_t *)-1 && errno == EINVAL)) { 3015 unblock_sigint(&old_mask); 3016 return ((aio_trans *) 0); 3017 } 3018 3019 results.outstanding--; 3020 transp = (aio_trans *)resultp; 3021 3022 if (resultp->aio_return != transp->size) { 3023 if (resultp->aio_return == -1) { 3024 /* 3025 * The aiowrite() may have failed because the 3026 * kernel didn't have enough memory to do the job. 3027 * Flush all pending writes and try a normal 3028 * write(). wtfs_breakup() will call exit if it 3029 * fails, so we don't worry about errors here. 3030 */ 3031 flush_writes(); 3032 wtfs_breakup(transp->bno, transp->size, transp->buffer); 3033 } else { 3034 (void) fprintf(stderr, gettext( 3035 "short write (%d of %d bytes) on sector %lld\n"), 3036 resultp->aio_return, transp->size, 3037 transp->bno); 3038 /* 3039 * Don't unblock SIGINT, to avoid potential 3040 * looping due to queued interrupts and 3041 * error handling. 3042 */ 3043 lockexit(32); 3044 } 3045 } 3046 3047 resultp->aio_return = 0; 3048 freetrans(transp); 3049 unblock_sigint(&old_mask); 3050 return (transp); 3051 } 3052 3053 /* 3054 * flush_writes() -- flush all the outstanding aio writes. 3055 */ 3056 static void 3057 flush_writes(void) 3058 { 3059 while (wait_for_write(BLOCK)) 3060 ; 3061 } 3062 3063 /* 3064 * get_aiop() -- find and return an aio_trans structure on which a new 3065 * aio can be done. Blocks on aiowait() if needed. Reaps 3066 * all outstanding completed aio's. 3067 * 3068 * Assumes that SIGINT is blocked. 3069 */ 3070 aio_trans * 3071 get_aiop() 3072 { 3073 int i; 3074 aio_trans *transp; 3075 aio_trans *prev; 3076 3077 /* 3078 * initialize aio stuff 3079 */ 3080 if (!aio_inited) { 3081 aio_inited = 1; 3082 3083 results.maxpend = 0; 3084 results.outstanding = 0; 3085 results.max = MAXAIO; 3086 3087 results.trans = (aio_trans *)calloc(results.max, 3088 sizeof (aio_trans)); 3089 if (results.trans == NULL) { 3090 perror("calloc"); 3091 lockexit(32); 3092 } 3093 3094 /* 3095 * Initialize the linked list of aio transaction 3096 * structures. Note that the final "next" pointer 3097 * will be NULL since we got the buffer from calloc(). 3098 */ 3099 prev = results.trans; 3100 for (i = 1; i < results.max; i++) { 3101 prev->next = &(results.trans[i]); 3102 prev = prev->next; 3103 } 3104 } 3105 3106 wait_for_write(NOBLOCK); 3107 while (results.trans == NULL) 3108 wait_for_write(BLOCK); 3109 transp = results.trans; 3110 results.trans = results.trans->next; 3111 3112 transp->next = 0; 3113 transp->resultbuf.aio_return = AIO_INPROGRESS; 3114 return (transp); 3115 } 3116 3117 /* 3118 * read a block from the file system 3119 */ 3120 static void 3121 rdfs(diskaddr_t bno, int size, char *bf) 3122 { 3123 int n, saverr; 3124 3125 /* 3126 * In case we need any data that's pending in an aiowrite(), 3127 * we wait for them all to complete before doing a read. 3128 */ 3129 flush_writes(); 3130 3131 /* 3132 * Note: the llseek() can succeed, even if the offset is out of range. 3133 * It's not until the file i/o operation (the read()) that one knows 3134 * for sure if the raw device can handle the offset. 3135 */ 3136 if (llseek(fsi, (offset_t)bno * sectorsize, 0) < 0) { 3137 saverr = errno; 3138 (void) fprintf(stderr, 3139 gettext("seek error on sector %lld: %s\n"), 3140 bno, strerror(saverr)); 3141 lockexit(32); 3142 } 3143 n = read(fsi, bf, size); 3144 if (n != size) { 3145 saverr = errno; 3146 if (n == -1) 3147 (void) fprintf(stderr, 3148 gettext("read error on sector %lld: %s\n"), 3149 bno, strerror(saverr)); 3150 else 3151 (void) fprintf(stderr, gettext( 3152 "short read (%d of %d bytes) on sector %lld\n"), 3153 n, size, bno); 3154 lockexit(32); 3155 } 3156 } 3157 3158 /* 3159 * write a block to the file system 3160 */ 3161 static void 3162 wtfs(diskaddr_t bno, int size, char *bf) 3163 { 3164 int n, saverr; 3165 3166 if (fso == -1) 3167 return; 3168 3169 /* 3170 * Note: the llseek() can succeed, even if the offset is out of range. 3171 * It's not until the file i/o operation (the write()) that one knows 3172 * for sure if the raw device can handle the offset. 3173 */ 3174 if (llseek(fso, (offset_t)bno * sectorsize, 0) < 0) { 3175 saverr = errno; 3176 (void) fprintf(stderr, 3177 gettext("seek error on sector %lld: %s\n"), 3178 bno, strerror(saverr)); 3179 lockexit(32); 3180 } 3181 if (Nflag) 3182 return; 3183 n = write(fso, bf, size); 3184 if (n != size) { 3185 saverr = errno; 3186 if (n == -1) 3187 (void) fprintf(stderr, 3188 gettext("write error on sector %lld: %s\n"), 3189 bno, strerror(saverr)); 3190 else 3191 (void) fprintf(stderr, gettext( 3192 "short write (%d of %d bytes) on sector %lld\n"), 3193 n, size, bno); 3194 lockexit(32); 3195 } 3196 } 3197 3198 /* 3199 * write a block to the file system -- buffered with aio 3200 */ 3201 static void 3202 awtfs(diskaddr_t bno, int size, char *bf, int release) 3203 { 3204 int n; 3205 aio_trans *transp; 3206 sigset_t old_mask; 3207 3208 if (fso == -1) 3209 return; 3210 3211 /* 3212 * We need to keep things consistent if we get interrupted, 3213 * so defer any expected interrupts for the time being. 3214 */ 3215 block_sigint(&old_mask); 3216 3217 if (Nflag) { 3218 if (release == RELEASE) 3219 freebuf(bf); 3220 } else { 3221 transp = get_aiop(); 3222 transp->bno = bno; 3223 transp->buffer = bf; 3224 transp->size = size; 3225 transp->release = release; 3226 3227 n = aiowrite(fso, bf, size, (off_t)bno * sectorsize, 3228 SEEK_SET, &transp->resultbuf); 3229 3230 if (n < 0) { 3231 /* 3232 * The aiowrite() may have failed because the 3233 * kernel didn't have enough memory to do the job. 3234 * Flush all pending writes and try a normal 3235 * write(). wtfs_breakup() will call exit if it 3236 * fails, so we don't worry about errors here. 3237 */ 3238 flush_writes(); 3239 wtfs_breakup(transp->bno, transp->size, transp->buffer); 3240 freetrans(transp); 3241 } else { 3242 /* 3243 * Keep track of our pending writes. 3244 */ 3245 results.outstanding++; 3246 if (results.outstanding > results.maxpend) 3247 results.maxpend = results.outstanding; 3248 } 3249 } 3250 3251 unblock_sigint(&old_mask); 3252 } 3253 3254 3255 /* 3256 * write a block to the file system, but break it up into sbsize 3257 * chunks to avoid forcing a large amount of memory to be locked down. 3258 * Only used as a fallback when an aio write has failed. 3259 */ 3260 static void 3261 wtfs_breakup(diskaddr_t bno, int size, char *bf) 3262 { 3263 int n, saverr; 3264 int wsize; 3265 int block_incr = sbsize / sectorsize; 3266 3267 if (size < sbsize) 3268 wsize = size; 3269 else 3270 wsize = sbsize; 3271 3272 n = 0; 3273 while (size) { 3274 /* 3275 * Note: the llseek() can succeed, even if the offset is 3276 * out of range. It's not until the file i/o operation 3277 * (the write()) that one knows for sure if the raw device 3278 * can handle the offset. 3279 */ 3280 if (llseek(fso, (offset_t)bno * sectorsize, 0) < 0) { 3281 saverr = errno; 3282 (void) fprintf(stderr, 3283 gettext("seek error on sector %lld: %s\n"), 3284 bno, strerror(saverr)); 3285 lockexit(32); 3286 } 3287 3288 n = write(fso, bf, wsize); 3289 if (n == -1) { 3290 saverr = errno; 3291 (void) fprintf(stderr, 3292 gettext("write error on sector %lld: %s\n"), 3293 bno, strerror(saverr)); 3294 lockexit(32); 3295 } 3296 if (n != wsize) { 3297 saverr = errno; 3298 (void) fprintf(stderr, gettext( 3299 "short write (%d of %d bytes) on sector %lld\n"), 3300 n, size, bno); 3301 lockexit(32); 3302 } 3303 3304 bno += block_incr; 3305 bf += wsize; 3306 size -= wsize; 3307 if (size < wsize) 3308 wsize = size; 3309 } 3310 } 3311 3312 3313 /* 3314 * check if a block is available 3315 */ 3316 static int 3317 isblock(struct fs *fs, unsigned char *cp, int h) 3318 { 3319 unsigned char mask; 3320 3321 switch (fs->fs_frag) { 3322 case 8: 3323 return (cp[h] == 0xff); 3324 case 4: 3325 mask = 0x0f << ((h & 0x1) << 2); 3326 return ((cp[h >> 1] & mask) == mask); 3327 case 2: 3328 mask = 0x03 << ((h & 0x3) << 1); 3329 return ((cp[h >> 2] & mask) == mask); 3330 case 1: 3331 mask = 0x01 << (h & 0x7); 3332 return ((cp[h >> 3] & mask) == mask); 3333 default: 3334 (void) fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag); 3335 return (0); 3336 } 3337 } 3338 3339 /* 3340 * take a block out of the map 3341 */ 3342 static void 3343 clrblock(struct fs *fs, unsigned char *cp, int h) 3344 { 3345 switch ((fs)->fs_frag) { 3346 case 8: 3347 cp[h] = 0; 3348 return; 3349 case 4: 3350 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 3351 return; 3352 case 2: 3353 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 3354 return; 3355 case 1: 3356 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 3357 return; 3358 default: 3359 (void) fprintf(stderr, 3360 gettext("clrblock: bad fs_frag value %d\n"), fs->fs_frag); 3361 return; 3362 } 3363 } 3364 3365 /* 3366 * put a block into the map 3367 */ 3368 static void 3369 setblock(struct fs *fs, unsigned char *cp, int h) 3370 { 3371 switch (fs->fs_frag) { 3372 case 8: 3373 cp[h] = 0xff; 3374 return; 3375 case 4: 3376 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 3377 return; 3378 case 2: 3379 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 3380 return; 3381 case 1: 3382 cp[h >> 3] |= (0x01 << (h & 0x7)); 3383 return; 3384 default: 3385 (void) fprintf(stderr, 3386 gettext("setblock: bad fs_frag value %d\n"), fs->fs_frag); 3387 return; 3388 } 3389 } 3390 3391 static void 3392 usage() 3393 { 3394 (void) fprintf(stderr, 3395 gettext("ufs usage: mkfs [-F FSType] [-V] [-m] [-o options] " 3396 "special " /* param 0 */ 3397 "size(sectors) \\ \n")); /* param 1 */ 3398 (void) fprintf(stderr, 3399 "[nsect " /* param 2 */ 3400 "ntrack " /* param 3 */ 3401 "bsize " /* param 4 */ 3402 "fragsize " /* param 5 */ 3403 "cpg " /* param 6 */ 3404 "free " /* param 7 */ 3405 "rps " /* param 8 */ 3406 "nbpi " /* param 9 */ 3407 "opt " /* param 10 */ 3408 "apc " /* param 11 */ 3409 "gap " /* param 12 */ 3410 "nrpos " /* param 13 */ 3411 "maxcontig " /* param 14 */ 3412 "mtb]\n"); /* param 15 */ 3413 (void) fprintf(stderr, 3414 gettext(" -m : dump fs cmd line used to make this partition\n" 3415 " -V :print this command line and return\n" 3416 " -o :ufs options: :nsect=%d,ntrack=%d,bsize=%d,fragsize=%d\n" 3417 " -o :ufs options: :cgsize=%d,free=%d,rps=%d,nbpi=%d,opt=%c\n" 3418 " -o :ufs options: :apc=%d,gap=%d,nrpos=%d,maxcontig=%d\n" 3419 " -o :ufs options: :mtb=%c,calcsb,calcbinsb\n" 3420 "NOTE that all -o suboptions: must be separated only by commas so as to\n" 3421 "be parsed as a single argument\n"), 3422 nsect, ntrack, bsize, fragsize, cpg, sblock.fs_minfree, rps, 3423 nbpi, opt, apc, (rotdelay == -1) ? 0 : rotdelay, 3424 sblock.fs_nrpos, maxcontig, mtb); 3425 lockexit(32); 3426 } 3427 3428 /*ARGSUSED*/ 3429 static void 3430 dump_fscmd(char *fsys, int fsi) 3431 { 3432 int64_t used, bpcg, inospercg; 3433 int64_t nbpi; 3434 uint64_t nbytes64; 3435 3436 bzero((char *)&sblock, sizeof (sblock)); 3437 rdfs((diskaddr_t)SBLOCK, SBSIZE, (char *)&sblock); 3438 3439 /* 3440 * ensure a valid file system and if not, exit with error or else 3441 * we will end up computing block numbers etc and dividing by zero 3442 * which will cause floating point errors in this routine. 3443 */ 3444 3445 if ((sblock.fs_magic != FS_MAGIC) && 3446 (sblock.fs_magic != MTB_UFS_MAGIC)) { 3447 (void) fprintf(stderr, gettext( 3448 "[not currently a valid file system - bad superblock]\n")); 3449 lockexit(32); 3450 } 3451 3452 if (sblock.fs_magic == FS_MAGIC && 3453 (sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 && 3454 sblock.fs_version != UFS_VERSION_MIN)) { 3455 (void) fprintf(stderr, gettext( 3456 "Unknown version of UFS format: %d\n"), sblock.fs_version); 3457 lockexit(32); 3458 } 3459 3460 if (sblock.fs_magic == MTB_UFS_MAGIC && 3461 (sblock.fs_version > MTB_UFS_VERSION_1 || 3462 sblock.fs_version < MTB_UFS_VERSION_MIN)) { 3463 (void) fprintf(stderr, gettext( 3464 "Unknown version of UFS format: %d\n"), sblock.fs_version); 3465 lockexit(32); 3466 } 3467 3468 /* 3469 * Compute a reasonable nbpi value. 3470 * The algorithm for "used" is copied from code 3471 * in main() verbatim. 3472 * The nbpi equation is taken from main where the 3473 * fs_ipg value is set for the last time. The INOPB(...) - 1 3474 * is used to account for the roundup. 3475 * The problem is that a range of nbpi values map to 3476 * the same file system layout. So it is not possible 3477 * to calculate the exact value specified when the file 3478 * system was created. So instead we determine the top 3479 * end of the range of values. 3480 */ 3481 bpcg = sblock.fs_spc * sectorsize; 3482 inospercg = (int64_t)roundup(bpcg / sizeof (struct dinode), 3483 INOPB(&sblock)); 3484 if (inospercg > MAXIpG(&sblock)) 3485 inospercg = MAXIpG(&sblock); 3486 used = (int64_t) 3487 (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock); 3488 used *= sectorsize; 3489 nbytes64 = (uint64_t)sblock.fs_cpg * bpcg - used; 3490 3491 /* 3492 * The top end of the range of values for nbpi may not be 3493 * a valid command line value for mkfs. Report the bottom 3494 * end instead. 3495 */ 3496 nbpi = (int64_t)(nbytes64 / (sblock.fs_ipg)); 3497 3498 (void) fprintf(stdout, gettext("mkfs -F ufs -o "), fsys); 3499 (void) fprintf(stdout, "nsect=%d,ntrack=%d,", 3500 sblock.fs_nsect, sblock.fs_ntrak); 3501 (void) fprintf(stdout, "bsize=%d,fragsize=%d,cgsize=%d,free=%d,", 3502 sblock.fs_bsize, sblock.fs_fsize, sblock.fs_cpg, sblock.fs_minfree); 3503 (void) fprintf(stdout, "rps=%d,nbpi=%lld,opt=%c,apc=%d,gap=%d,", 3504 sblock.fs_rps, nbpi, (sblock.fs_optim == FS_OPTSPACE) ? 's' : 't', 3505 (sblock.fs_ntrak * sblock.fs_nsect) - sblock.fs_spc, 3506 sblock.fs_rotdelay); 3507 (void) fprintf(stdout, "nrpos=%d,maxcontig=%d,mtb=%c ", 3508 sblock.fs_nrpos, sblock.fs_maxcontig, 3509 ((sblock.fs_magic == MTB_UFS_MAGIC) ? 'y' : 'n')); 3510 (void) fprintf(stdout, "%s %lld\n", fsys, 3511 fsbtodb(&sblock, sblock.fs_size)); 3512 3513 bzero((char *)&sblock, sizeof (sblock)); 3514 } 3515 3516 /* number ************************************************************* */ 3517 /* */ 3518 /* Convert a numeric string arg to binary */ 3519 /* */ 3520 /* Args: d_value - default value, if have parse error */ 3521 /* param - the name of the argument, for error messages */ 3522 /* flags - parser state and what's allowed in the arg */ 3523 /* Global arg: string - pointer to command arg */ 3524 /* */ 3525 /* Valid forms: 123 | 123k | 123*123 | 123x123 */ 3526 /* */ 3527 /* Return: converted number */ 3528 /* */ 3529 /* ******************************************************************** */ 3530 3531 static uint64_t 3532 number(uint64_t d_value, char *param, int flags) 3533 { 3534 char *cs; 3535 uint64_t n, t; 3536 uint64_t cut = BIG / 10; /* limit to avoid overflow */ 3537 int minus = 0; 3538 3539 cs = string; 3540 if (*cs == '-') { 3541 minus = 1; 3542 cs += 1; 3543 } 3544 if ((*cs < '0') || (*cs > '9')) { 3545 goto bail_out; 3546 } 3547 n = 0; 3548 while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) { 3549 n = n*10 + *cs++ - '0'; 3550 } 3551 if (minus) 3552 n = -n; 3553 for (;;) { 3554 switch (*cs++) { 3555 case 'k': 3556 if (flags & ALLOW_END_ONLY) 3557 goto bail_out; 3558 if (n > (BIG / 1024)) 3559 goto overflow; 3560 n *= 1024; 3561 continue; 3562 3563 case '*': 3564 case 'x': 3565 if (flags & ALLOW_END_ONLY) 3566 goto bail_out; 3567 string = cs; 3568 t = number(d_value, param, flags); 3569 if (n > (BIG / t)) 3570 goto overflow; 3571 n *= t; 3572 cs = string + 1; /* adjust for -- below */ 3573 3574 /* recursion has read rest of expression */ 3575 /* FALLTHROUGH */ 3576 3577 case ',': 3578 case '\0': 3579 cs--; 3580 string = cs; 3581 return (n); 3582 3583 case '%': 3584 if (flags & ALLOW_END_ONLY) 3585 goto bail_out; 3586 if (flags & ALLOW_PERCENT) { 3587 flags &= ~ALLOW_PERCENT; 3588 flags |= ALLOW_END_ONLY; 3589 continue; 3590 } 3591 goto bail_out; 3592 3593 case 'm': 3594 if (flags & ALLOW_END_ONLY) 3595 goto bail_out; 3596 if (flags & ALLOW_MS1) { 3597 flags &= ~ALLOW_MS1; 3598 flags |= ALLOW_MS2; 3599 continue; 3600 } 3601 goto bail_out; 3602 3603 case 's': 3604 if (flags & ALLOW_END_ONLY) 3605 goto bail_out; 3606 if (flags & ALLOW_MS2) { 3607 flags &= ~ALLOW_MS2; 3608 flags |= ALLOW_END_ONLY; 3609 continue; 3610 } 3611 goto bail_out; 3612 3613 case '0': case '1': case '2': case '3': case '4': 3614 case '5': case '6': case '7': case '8': case '9': 3615 overflow: 3616 (void) fprintf(stderr, 3617 gettext("mkfs: value for %s overflowed\n"), 3618 param); 3619 while ((*cs != '\0') && (*cs != ',')) 3620 cs++; 3621 string = cs; 3622 return (BIG); 3623 3624 default: 3625 bail_out: 3626 (void) fprintf(stderr, gettext( 3627 "mkfs: bad numeric arg for %s: \"%s\"\n"), 3628 param, string); 3629 while ((*cs != '\0') && (*cs != ',')) 3630 cs++; 3631 string = cs; 3632 if (d_value != NO_DEFAULT) { 3633 (void) fprintf(stderr, 3634 gettext("mkfs: %s reset to default %lld\n"), 3635 param, d_value); 3636 return (d_value); 3637 } 3638 lockexit(2); 3639 3640 } 3641 } /* never gets here */ 3642 } 3643 3644 /* match ************************************************************** */ 3645 /* */ 3646 /* Compare two text strings for equality */ 3647 /* */ 3648 /* Arg: s - pointer to string to match with a command arg */ 3649 /* Global arg: string - pointer to command arg */ 3650 /* */ 3651 /* Return: 1 if match, 0 if no match */ 3652 /* If match, also reset `string' to point to the text */ 3653 /* that follows the matching text. */ 3654 /* */ 3655 /* ******************************************************************** */ 3656 3657 static int 3658 match(char *s) 3659 { 3660 char *cs; 3661 3662 cs = string; 3663 while (*cs++ == *s) { 3664 if (*s++ == '\0') { 3665 goto true; 3666 } 3667 } 3668 if (*s != '\0') { 3669 return (0); 3670 } 3671 3672 true: 3673 cs--; 3674 string = cs; 3675 return (1); 3676 } 3677 3678 /* 3679 * GROWFS ROUTINES 3680 */ 3681 3682 /* ARGSUSED */ 3683 void 3684 lockexit(int exitstatus) 3685 { 3686 if (Pflag) { 3687 /* the probe mode neither changes nor locks the filesystem */ 3688 exit(exitstatus); 3689 } 3690 3691 /* 3692 * flush the dirty cylinder group 3693 */ 3694 if (inlockexit == 0) { 3695 inlockexit = 1; 3696 flcg(); 3697 } 3698 3699 if (aio_inited) { 3700 flush_writes(); 3701 } 3702 3703 /* 3704 * make sure the file system is unlocked before exiting 3705 */ 3706 if ((inlockexit == 1) && (!isbad)) { 3707 inlockexit = 2; 3708 ulockfs(); 3709 /* 3710 * if logging was enabled, then re-enable it 3711 */ 3712 if (waslog) { 3713 if (rl_log_control(fsys, _FIOLOGENABLE) != RL_SUCCESS) { 3714 (void) fprintf(stderr, gettext( 3715 "failed to re-enable logging\n")); 3716 } 3717 } 3718 } else if (grow) { 3719 if (isbad) { 3720 (void) fprintf(stderr, gettext( 3721 "Filesystem is currently inconsistent. It " 3722 "must be repaired with fsck(1M)\nbefore being " 3723 "used. Use the following command to " 3724 "do this:\n\n\tfsck %s\n\n"), 3725 fsys); 3726 3727 if (ismounted) { 3728 (void) fprintf(stderr, gettext( 3729 "You will be told that the filesystem " 3730 "is already mounted, and asked if you\n" 3731 "wish to continue. Answer `yes' to " 3732 "this question.\n\n")); 3733 } 3734 3735 (void) fprintf(stderr, gettext( 3736 "One problem should be reported, that " 3737 "the summary information is bad.\n" 3738 "You will then be asked if it " 3739 "should be salvaged. Answer `yes' " 3740 "to\nthis question.\n\n")); 3741 } 3742 3743 if (ismounted) { 3744 /* 3745 * In theory, there's no way to get here without 3746 * isbad also being set, but be robust in the 3747 * face of future code changes. 3748 */ 3749 (void) fprintf(stderr, gettext( 3750 "The filesystem is currently mounted " 3751 "read-only and write-locked. ")); 3752 if (isbad) { 3753 (void) fprintf(stderr, gettext( 3754 "After\nrunning fsck, unlock the " 3755 "filesystem and ")); 3756 } else { 3757 (void) fprintf(stderr, gettext( 3758 "Unlock the filesystem\nand ")); 3759 } 3760 3761 (void) fprintf(stderr, gettext( 3762 "re-enable writing with\nthe following " 3763 "command:\n\n\tlockfs -u %s\n\n"), 3764 directory); 3765 } 3766 } 3767 3768 exit(exitstatus); 3769 } 3770 3771 void 3772 randomgeneration() 3773 { 3774 int i; 3775 struct dinode *dp; 3776 3777 /* 3778 * always perform fsirand(1) function... newfs will notice that 3779 * the inodes have been randomized and will not call fsirand itself 3780 */ 3781 for (i = 0, dp = zino; i < sblock.fs_inopb; ++i, ++dp) 3782 IRANDOMIZE(&dp->di_ic); 3783 } 3784 3785 /* 3786 * Check the size of the summary information. 3787 * Fields in sblock are not changed in this function. 3788 * 3789 * For an 8K filesystem block, the maximum number of cylinder groups is 16384. 3790 * MAXCSBUFS {32} * 8K {FS block size} 3791 * divided by (sizeof csum) {16} 3792 * 3793 * Note that MAXCSBUFS is not used in the kernel; as of Solaris 2.6 build 32, 3794 * this is the only place where it's referenced. 3795 */ 3796 void 3797 checksummarysize() 3798 { 3799 diskaddr_t dmax; 3800 diskaddr_t dmin; 3801 int64_t cg0frags; 3802 int64_t cg0blocks; 3803 int64_t maxncg; 3804 int64_t maxfrags; 3805 uint64_t fs_size; 3806 uint64_t maxfs_blocks; /* filesystem blocks for max filesystem size */ 3807 3808 /* 3809 * compute the maximum summary info size 3810 */ 3811 dmin = cgdmin(&sblock, 0); 3812 dmax = cgbase(&sblock, 0) + sblock.fs_fpg; 3813 fs_size = (grow) ? grow_fs_size : sblock.fs_size; 3814 if (dmax > fs_size) 3815 dmax = fs_size; 3816 cg0frags = dmax - dmin; 3817 cg0blocks = cg0frags / sblock.fs_frag; 3818 cg0frags = cg0blocks * sblock.fs_frag; 3819 maxncg = (longlong_t)cg0blocks * 3820 (longlong_t)(sblock.fs_bsize / sizeof (struct csum)); 3821 3822 maxfs_blocks = FS_MAX; 3823 3824 if (maxncg > ((longlong_t)maxfs_blocks / (longlong_t)sblock.fs_fpg) + 1) 3825 maxncg = ((longlong_t)maxfs_blocks / 3826 (longlong_t)sblock.fs_fpg) + 1; 3827 3828 maxfrags = maxncg * (longlong_t)sblock.fs_fpg; 3829 3830 if (maxfrags > maxfs_blocks) 3831 maxfrags = maxfs_blocks; 3832 3833 3834 /* 3835 * remember for later processing in extendsummaryinfo() 3836 */ 3837 if (test) 3838 grow_sifrag = dmin + (cg0blocks * sblock.fs_frag); 3839 if (testfrags == 0) 3840 testfrags = cg0frags; 3841 if (testforce) 3842 if (testfrags > cg0frags) { 3843 (void) fprintf(stderr, 3844 gettext("Too many test frags (%lld); " 3845 "try %lld\n"), testfrags, cg0frags); 3846 lockexit(32); 3847 } 3848 3849 /* 3850 * if summary info is too large (too many cg's) tell the user and exit 3851 */ 3852 if ((longlong_t)sblock.fs_size > maxfrags) { 3853 (void) fprintf(stderr, gettext( 3854 "Too many cylinder groups with %llu sectors;\n try " 3855 "increasing cgsize, or decreasing fssize to %llu\n"), 3856 fsbtodb(&sblock, (uint64_t)sblock.fs_size), 3857 fsbtodb(&sblock, (uint64_t)maxfrags)); 3858 lockexit(32); 3859 } 3860 } 3861 3862 /* 3863 * checksblock() has two uses: 3864 * - One is to sanity test the superblock and is used when newfs(1M) 3865 * is invoked with the "-N" option. If any discrepancy was found, 3866 * just return whatever error was found and do not exit. 3867 * - the other use of it is in places where you expect the superblock 3868 * to be sane, and if it isn't, then we exit. 3869 * Which of the above two actions to take is indicated with the second argument. 3870 */ 3871 3872 int 3873 checksblock(struct fs sb, int proceed) 3874 { 3875 int err = 0; 3876 char *errmsg; 3877 3878 if ((sb.fs_magic != FS_MAGIC) && (sb.fs_magic != MTB_UFS_MAGIC)) { 3879 err = 1; 3880 errmsg = gettext("Bad superblock; magic number wrong\n"); 3881 } else if ((sb.fs_magic == FS_MAGIC && 3882 (sb.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 && 3883 sb.fs_version != UFS_VERSION_MIN)) || 3884 (sb.fs_magic == MTB_UFS_MAGIC && 3885 (sb.fs_version > MTB_UFS_VERSION_1 || 3886 sb.fs_version < MTB_UFS_VERSION_MIN))) { 3887 err = 2; 3888 errmsg = gettext("Unrecognized version of UFS\n"); 3889 } else if (sb.fs_ncg < 1) { 3890 err = 3; 3891 errmsg = gettext("Bad superblock; ncg out of range\n"); 3892 } else if (sb.fs_cpg < 1) { 3893 err = 4; 3894 errmsg = gettext("Bad superblock; cpg out of range\n"); 3895 } else if (sb.fs_ncg * sb.fs_cpg < sb.fs_ncyl || 3896 (sb.fs_ncg - 1) * sb.fs_cpg >= sb.fs_ncyl) { 3897 err = 5; 3898 errmsg = gettext("Bad superblock; ncyl out of range\n"); 3899 } else if (sb.fs_sbsize <= 0 || sb.fs_sbsize > sb.fs_bsize) { 3900 err = 6; 3901 errmsg = gettext("Bad superblock; superblock size out of range\n"); 3902 } 3903 3904 3905 if (proceed) { 3906 if (err) dprintf(("%s", errmsg)); 3907 return (err); 3908 } 3909 3910 if (err) { 3911 fprintf(stderr, "%s", errmsg); 3912 lockexit(32); 3913 } 3914 return (32); 3915 } 3916 3917 /* 3918 * Roll the embedded log, if any, and set up the global variables 3919 * islog, islogok and isufslog. 3920 */ 3921 static void 3922 logsetup(char *devstr) 3923 { 3924 void *buf, *ud_buf; 3925 extent_block_t *ebp; 3926 ml_unit_t *ul; 3927 ml_odunit_t *ud; 3928 3929 /* 3930 * Does the superblock indicate that we are supposed to have a log ? 3931 */ 3932 if (sblock.fs_logbno == 0) { 3933 /* 3934 * No log present, nothing to do. 3935 */ 3936 islogok = 0; 3937 islog = 0; 3938 isufslog = 0; 3939 return; 3940 } else { 3941 /* 3942 * There's a log in a yet unknown state, attempt to roll it. 3943 */ 3944 islog = 1; 3945 islogok = 0; 3946 isufslog = 0; 3947 3948 /* 3949 * We failed to roll the log, bail out. 3950 */ 3951 if (rl_roll_log(devstr) != RL_SUCCESS) 3952 return; 3953 3954 isufslog = 1; 3955 3956 /* log is not okay; check the fs */ 3957 if ((FSOKAY != (sblock.fs_state + sblock.fs_time)) || 3958 (sblock.fs_clean != FSLOG)) 3959 return; 3960 3961 /* get the log allocation block */ 3962 buf = (void *)malloc(DEV_BSIZE); 3963 if (buf == (void *) NULL) 3964 return; 3965 3966 ud_buf = (void *)malloc(DEV_BSIZE); 3967 if (ud_buf == (void *) NULL) { 3968 free(buf); 3969 return; 3970 } 3971 3972 rdfs((diskaddr_t)logbtodb(&sblock, sblock.fs_logbno), 3973 DEV_BSIZE, buf); 3974 ebp = (extent_block_t *)buf; 3975 3976 /* log allocation block is not okay; check the fs */ 3977 if (ebp->type != LUFS_EXTENTS) { 3978 free(buf); 3979 free(ud_buf); 3980 return; 3981 } 3982 3983 /* get the log state block(s) */ 3984 rdfs((diskaddr_t)logbtodb(&sblock, ebp->extents[0].pbno), 3985 DEV_BSIZE, ud_buf); 3986 ud = (ml_odunit_t *)ud_buf; 3987 ul = (ml_unit_t *)malloc(sizeof (*ul)); 3988 ul->un_ondisk = *ud; 3989 3990 /* log state is okay */ 3991 if ((ul->un_chksum == ul->un_head_ident + ul->un_tail_ident) && 3992 (ul->un_version == LUFS_VERSION_LATEST) && 3993 (ul->un_badlog == 0)) 3994 islogok = 1; 3995 free(ud_buf); 3996 free(buf); 3997 free(ul); 3998 } 3999 } 4000 4001 void 4002 growinit(char *devstr) 4003 { 4004 int i; 4005 char buf[DEV_BSIZE]; 4006 4007 /* 4008 * Read and verify the superblock 4009 */ 4010 rdfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, (char *)&sblock); 4011 (void) checksblock(sblock, 0); 4012 if (sblock.fs_postblformat != FS_DYNAMICPOSTBLFMT) { 4013 (void) fprintf(stderr, 4014 gettext("old file system format; can't growfs\n")); 4015 lockexit(32); 4016 } 4017 4018 /* 4019 * can't shrink a file system 4020 */ 4021 grow_fssize = fsbtodb(&sblock, (uint64_t)sblock.fs_size); 4022 if (fssize_db < grow_fssize) { 4023 (void) fprintf(stderr, 4024 gettext("%lld sectors < current size of %lld sectors\n"), 4025 fssize_db, grow_fssize); 4026 lockexit(32); 4027 } 4028 4029 /* 4030 * can't grow a system to over a terabyte unless it was set up 4031 * as an MTB UFS file system. 4032 */ 4033 if (mtb == 'y' && sblock.fs_magic != MTB_UFS_MAGIC) { 4034 if (fssize_db >= SECTORS_PER_TERABYTE) { 4035 (void) fprintf(stderr, gettext( 4036 "File system was not set up with the multi-terabyte format.\n")); 4037 (void) fprintf(stderr, gettext( 4038 "Its size cannot be increased to a terabyte or more.\n")); 4039 } else { 4040 (void) fprintf(stderr, gettext( 4041 "Cannot convert file system to multi-terabyte format.\n")); 4042 } 4043 lockexit(32); 4044 } 4045 4046 logsetup(devstr); 4047 4048 /* 4049 * can't growfs when logging device has errors 4050 */ 4051 if ((islog && !islogok) || 4052 ((FSOKAY == (sblock.fs_state + sblock.fs_time)) && 4053 (sblock.fs_clean == FSLOG && !islog))) { 4054 (void) fprintf(stderr, 4055 gettext("logging device has errors; can't growfs\n")); 4056 lockexit(32); 4057 } 4058 4059 /* 4060 * disable ufs logging for growing 4061 */ 4062 if (isufslog) { 4063 if (rl_log_control(devstr, _FIOLOGDISABLE) != RL_SUCCESS) { 4064 (void) fprintf(stderr, gettext( 4065 "failed to disable logging\n")); 4066 lockexit(32); 4067 } 4068 islog = 0; 4069 waslog = 1; 4070 } 4071 4072 /* 4073 * if mounted write lock the file system to be grown 4074 */ 4075 if (ismounted) 4076 wlockfs(); 4077 4078 /* 4079 * refresh dynamic superblock state - disabling logging will have 4080 * changed the amount of free space available in the file system 4081 */ 4082 rdfs((diskaddr_t)(SBOFF / sectorsize), sbsize, (char *)&sblock); 4083 4084 /* 4085 * make sure device is big enough 4086 */ 4087 rdfs((diskaddr_t)fssize_db - 1, DEV_BSIZE, buf); 4088 wtfs((diskaddr_t)fssize_db - 1, DEV_BSIZE, buf); 4089 4090 /* 4091 * read current summary information 4092 */ 4093 grow_fscs = read_summaryinfo(&sblock); 4094 4095 /* 4096 * save some current size related fields from the superblock 4097 * These are used in extendsummaryinfo() 4098 */ 4099 grow_fs_size = sblock.fs_size; 4100 grow_fs_ncg = sblock.fs_ncg; 4101 grow_fs_csaddr = (diskaddr_t)sblock.fs_csaddr; 4102 grow_fs_cssize = sblock.fs_cssize; 4103 4104 /* 4105 * save and reset the clean flag 4106 */ 4107 if (FSOKAY == (sblock.fs_state + sblock.fs_time)) 4108 grow_fs_clean = sblock.fs_clean; 4109 else 4110 grow_fs_clean = FSBAD; 4111 sblock.fs_clean = FSBAD; 4112 sblock.fs_state = FSOKAY - sblock.fs_time; 4113 isbad = 1; 4114 wtfs((diskaddr_t)(SBOFF / sectorsize), sbsize, (char *)&sblock); 4115 } 4116 4117 void 4118 checkdev(char *rdev, char *bdev) 4119 { 4120 struct stat64 statarea; 4121 4122 if (stat64(bdev, &statarea) < 0) { 4123 (void) fprintf(stderr, gettext("can't check mount point; ")); 4124 (void) fprintf(stderr, gettext("can't stat %s\n"), bdev); 4125 lockexit(32); 4126 } 4127 if ((statarea.st_mode & S_IFMT) != S_IFBLK) { 4128 (void) fprintf(stderr, gettext( 4129 "can't check mount point; %s is not a block device\n"), 4130 bdev); 4131 lockexit(32); 4132 } 4133 if (stat64(rdev, &statarea) < 0) { 4134 (void) fprintf(stderr, gettext("can't stat %s\n"), rdev); 4135 lockexit(32); 4136 } 4137 if ((statarea.st_mode & S_IFMT) != S_IFCHR) { 4138 (void) fprintf(stderr, 4139 gettext("%s is not a character device\n"), rdev); 4140 lockexit(32); 4141 } 4142 } 4143 4144 void 4145 checkmount(struct mnttab *mntp, char *bdevname) 4146 { 4147 struct stat64 statdir; 4148 struct stat64 statdev; 4149 4150 if (strcmp(bdevname, mntp->mnt_special) == 0) { 4151 if (stat64(mntp->mnt_mountp, &statdir) == -1) { 4152 (void) fprintf(stderr, gettext("can't stat %s\n"), 4153 mntp->mnt_mountp); 4154 lockexit(32); 4155 } 4156 if (stat64(mntp->mnt_special, &statdev) == -1) { 4157 (void) fprintf(stderr, gettext("can't stat %s\n"), 4158 mntp->mnt_special); 4159 lockexit(32); 4160 } 4161 if (statdir.st_dev != statdev.st_rdev) { 4162 (void) fprintf(stderr, gettext( 4163 "%s is not mounted on %s; mnttab(4) wrong\n"), 4164 mntp->mnt_special, mntp->mnt_mountp); 4165 lockexit(32); 4166 } 4167 ismounted = 1; 4168 if (directory) { 4169 if (strcmp(mntp->mnt_mountp, directory) != 0) { 4170 (void) fprintf(stderr, 4171 gettext("%s is mounted on %s, not %s\n"), 4172 bdevname, mntp->mnt_mountp, directory); 4173 lockexit(32); 4174 } 4175 } else { 4176 if (grow) 4177 (void) fprintf(stderr, gettext( 4178 "%s is mounted on %s; can't growfs\n"), 4179 bdevname, mntp->mnt_mountp); 4180 else 4181 (void) fprintf(stderr, 4182 gettext("%s is mounted, can't mkfs\n"), 4183 bdevname); 4184 lockexit(32); 4185 } 4186 } 4187 } 4188 4189 struct dinode *dibuf = 0; 4190 diskaddr_t difrag = 0; 4191 4192 struct dinode * 4193 gdinode(ino_t ino) 4194 { 4195 /* 4196 * read the block of inodes containing inode number ino 4197 */ 4198 if (dibuf == 0) 4199 dibuf = (struct dinode *)malloc((unsigned)sblock.fs_bsize); 4200 if (itod(&sblock, ino) != difrag) { 4201 difrag = itod(&sblock, ino); 4202 rdfs(fsbtodb(&sblock, (uint64_t)difrag), (int)sblock.fs_bsize, 4203 (char *)dibuf); 4204 } 4205 return (dibuf + (ino % INOPB(&sblock))); 4206 } 4207 4208 /* 4209 * structure that manages the frags we need for extended summary info 4210 * These frags can be: 4211 * free 4212 * data block 4213 * alloc block 4214 */ 4215 struct csfrag { 4216 struct csfrag *next; /* next entry */ 4217 daddr32_t ofrag; /* old frag */ 4218 daddr32_t nfrag; /* new frag */ 4219 long cylno; /* cylno of nfrag */ 4220 long frags; /* number of frags */ 4221 long size; /* size in bytes */ 4222 ino_t ino; /* inode number */ 4223 long fixed; /* Boolean - Already fixed? */ 4224 }; 4225 struct csfrag *csfrag; /* state unknown */ 4226 struct csfrag *csfragino; /* frags belonging to an inode */ 4227 struct csfrag *csfragfree; /* frags that are free */ 4228 4229 daddr32_t maxcsfrag = 0; /* maximum in range */ 4230 daddr32_t mincsfrag = 0x7fffffff; /* minimum in range */ 4231 4232 int 4233 csfraginrange(daddr32_t frag) 4234 { 4235 return ((frag >= mincsfrag) && (frag <= maxcsfrag)); 4236 } 4237 4238 struct csfrag * 4239 findcsfrag(daddr32_t frag, struct csfrag **cfap) 4240 { 4241 struct csfrag *cfp; 4242 4243 if (!csfraginrange(frag)) 4244 return (NULL); 4245 4246 for (cfp = *cfap; cfp; cfp = cfp->next) 4247 if (cfp->ofrag == frag) 4248 return (cfp); 4249 return (NULL); 4250 } 4251 4252 void 4253 checkindirect(ino_t ino, daddr32_t *fragsp, daddr32_t frag, int level) 4254 { 4255 int i; 4256 int ne = sblock.fs_bsize / sizeof (daddr32_t); 4257 daddr32_t fsb[MAXBSIZE / sizeof (daddr32_t)]; 4258 4259 if (frag == 0) 4260 return; 4261 4262 rdfs(fsbtodb(&sblock, frag), (int)sblock.fs_bsize, 4263 (char *)fsb); 4264 4265 checkdirect(ino, fragsp, fsb, sblock.fs_bsize / sizeof (daddr32_t)); 4266 4267 if (level) 4268 for (i = 0; i < ne && *fragsp; ++i) 4269 checkindirect(ino, fragsp, fsb[i], level-1); 4270 } 4271 4272 void 4273 addcsfrag(ino_t ino, daddr32_t frag, struct csfrag **cfap) 4274 { 4275 struct csfrag *cfp, *curr, *prev; 4276 4277 /* 4278 * establish a range for faster checking in csfraginrange() 4279 */ 4280 if (frag > maxcsfrag) 4281 maxcsfrag = frag; 4282 if (frag < mincsfrag) 4283 mincsfrag = frag; 4284 4285 /* 4286 * if this frag belongs to an inode and is not the start of a block 4287 * then see if it is part of a frag range for this inode 4288 */ 4289 if (ino && (frag % sblock.fs_frag)) 4290 for (cfp = *cfap; cfp; cfp = cfp->next) { 4291 if (ino != cfp->ino) 4292 continue; 4293 if (frag != cfp->ofrag + cfp->frags) 4294 continue; 4295 cfp->frags++; 4296 cfp->size += sblock.fs_fsize; 4297 return; 4298 } 4299 /* 4300 * allocate a csfrag entry and insert it in an increasing order into the 4301 * specified list 4302 */ 4303 cfp = (struct csfrag *)calloc(1, sizeof (struct csfrag)); 4304 cfp->ino = ino; 4305 cfp->ofrag = frag; 4306 cfp->frags = 1; 4307 cfp->size = sblock.fs_fsize; 4308 for (prev = NULL, curr = *cfap; curr != NULL; 4309 prev = curr, curr = curr->next) { 4310 if (frag < curr->ofrag) { 4311 cfp->next = curr; 4312 if (prev) 4313 prev->next = cfp; /* middle element */ 4314 else 4315 *cfap = cfp; /* first element */ 4316 break; 4317 } 4318 if (curr->next == NULL) { 4319 curr->next = cfp; /* last element */ 4320 break; 4321 } 4322 } 4323 if (*cfap == NULL) /* will happen only once */ 4324 *cfap = cfp; 4325 } 4326 4327 void 4328 delcsfrag(daddr32_t frag, struct csfrag **cfap) 4329 { 4330 struct csfrag *cfp; 4331 struct csfrag **cfpp; 4332 4333 /* 4334 * free up entry whose beginning frag matches 4335 */ 4336 for (cfpp = cfap; *cfpp; cfpp = &(*cfpp)->next) { 4337 if (frag == (*cfpp)->ofrag) { 4338 cfp = *cfpp; 4339 *cfpp = (*cfpp)->next; 4340 free((char *)cfp); 4341 return; 4342 } 4343 } 4344 } 4345 4346 /* 4347 * See whether any of the direct blocks in the array pointed by "db" and of 4348 * length "ne" are within the range of frags needed to extend the cylinder 4349 * summary. If so, remove those frags from the "as-yet-unclassified" list 4350 * (csfrag) and add them to the "owned-by-inode" list (csfragino). 4351 * For each such frag found, decrement the frag count pointed to by fragsp. 4352 * "ino" is the inode that contains (either directly or indirectly) the frags 4353 * being checked. 4354 */ 4355 void 4356 checkdirect(ino_t ino, daddr32_t *fragsp, daddr32_t *db, int ne) 4357 { 4358 int i; 4359 int j; 4360 int found; 4361 diskaddr_t frag; 4362 4363 /* 4364 * scan for allocation within the new summary info range 4365 */ 4366 for (i = 0; i < ne && *fragsp; ++i) { 4367 if ((frag = *db++) != 0) { 4368 found = 0; 4369 for (j = 0; j < sblock.fs_frag && *fragsp; ++j) { 4370 if (found || (found = csfraginrange(frag))) { 4371 addcsfrag(ino, frag, &csfragino); 4372 delcsfrag(frag, &csfrag); 4373 } 4374 ++frag; 4375 --(*fragsp); 4376 } 4377 } 4378 } 4379 } 4380 4381 void 4382 findcsfragino() 4383 { 4384 int i; 4385 int j; 4386 daddr32_t frags; 4387 struct dinode *dp; 4388 4389 /* 4390 * scan all old inodes looking for allocations in the new 4391 * summary info range. Move the affected frag from the 4392 * generic csfrag list onto the `owned-by-inode' list csfragino. 4393 */ 4394 for (i = UFSROOTINO; i < grow_fs_ncg*sblock.fs_ipg && csfrag; ++i) { 4395 dp = gdinode((ino_t)i); 4396 switch (dp->di_mode & IFMT) { 4397 case IFSHAD : 4398 case IFLNK : 4399 case IFDIR : 4400 case IFREG : break; 4401 default : continue; 4402 } 4403 4404 frags = dbtofsb(&sblock, dp->di_blocks); 4405 4406 checkdirect((ino_t)i, &frags, &dp->di_db[0], NDADDR+NIADDR); 4407 for (j = 0; j < NIADDR && frags; ++j) { 4408 /* Negate the block if its an fallocate'd block */ 4409 if (dp->di_ib[j] < 0 && dp->di_ib[j] != UFS_HOLE) 4410 checkindirect((ino_t)i, &frags, 4411 -(dp->di_ib[j]), j); 4412 else 4413 checkindirect((ino_t)i, &frags, 4414 dp->di_ib[j], j); 4415 } 4416 } 4417 } 4418 4419 void 4420 fixindirect(daddr32_t frag, int level) 4421 { 4422 int i; 4423 int ne = sblock.fs_bsize / sizeof (daddr32_t); 4424 daddr32_t fsb[MAXBSIZE / sizeof (daddr32_t)]; 4425 4426 if (frag == 0) 4427 return; 4428 4429 rdfs(fsbtodb(&sblock, (uint64_t)frag), (int)sblock.fs_bsize, 4430 (char *)fsb); 4431 4432 fixdirect((caddr_t)fsb, frag, fsb, ne); 4433 4434 if (level) 4435 for (i = 0; i < ne; ++i) 4436 fixindirect(fsb[i], level-1); 4437 } 4438 4439 void 4440 fixdirect(caddr_t bp, daddr32_t frag, daddr32_t *db, int ne) 4441 { 4442 int i; 4443 struct csfrag *cfp; 4444 4445 for (i = 0; i < ne; ++i, ++db) { 4446 if (*db == 0) 4447 continue; 4448 if ((cfp = findcsfrag(*db, &csfragino)) == NULL) 4449 continue; 4450 *db = cfp->nfrag; 4451 cfp->fixed = 1; 4452 wtfs(fsbtodb(&sblock, (uint64_t)frag), (int)sblock.fs_bsize, 4453 bp); 4454 } 4455 } 4456 4457 void 4458 fixcsfragino() 4459 { 4460 int i; 4461 struct dinode *dp; 4462 struct csfrag *cfp; 4463 4464 for (cfp = csfragino; cfp; cfp = cfp->next) { 4465 if (cfp->fixed) 4466 continue; 4467 dp = gdinode((ino_t)cfp->ino); 4468 fixdirect((caddr_t)dibuf, difrag, dp->di_db, NDADDR+NIADDR); 4469 for (i = 0; i < NIADDR; ++i) 4470 fixindirect(dp->di_ib[i], i); 4471 } 4472 } 4473 4474 /* 4475 * Read the cylinders summary information specified by settings in the 4476 * passed 'fs' structure into a new allocated array of csum structures. 4477 * The caller is responsible for freeing the returned array. 4478 * Return a pointer to an array of csum structures. 4479 */ 4480 static struct csum * 4481 read_summaryinfo(struct fs *fsp) 4482 { 4483 struct csum *csp; 4484 int i; 4485 4486 if ((csp = malloc((size_t)fsp->fs_cssize)) == NULL) { 4487 (void) fprintf(stderr, gettext("cannot create csum list," 4488 " not enough memory\n")); 4489 exit(32); 4490 } 4491 4492 for (i = 0; i < fsp->fs_cssize; i += fsp->fs_bsize) { 4493 rdfs(fsbtodb(fsp, 4494 (uint64_t)(fsp->fs_csaddr + numfrags(fsp, i))), 4495 (int)(fsp->fs_cssize - i < fsp->fs_bsize ? 4496 fsp->fs_cssize - i : fsp->fs_bsize), 4497 ((caddr_t)csp) + i); 4498 } 4499 4500 return (csp); 4501 } 4502 4503 /* 4504 * Check the allocation of fragments that are to be made part of a csum block. 4505 * A fragment is allocated if it is either in the csfragfree list or, it is 4506 * in the csfragino list and has new frags associated with it. 4507 * Return the number of allocated fragments. 4508 */ 4509 int64_t 4510 checkfragallocated(daddr32_t frag) 4511 { 4512 struct csfrag *cfp; 4513 /* 4514 * Since the lists are sorted we can break the search if the asked 4515 * frag is smaller then the one in the list. 4516 */ 4517 for (cfp = csfragfree; cfp != NULL && frag >= cfp->ofrag; 4518 cfp = cfp->next) { 4519 if (frag == cfp->ofrag) 4520 return (1); 4521 } 4522 for (cfp = csfragino; cfp != NULL && frag >= cfp->ofrag; 4523 cfp = cfp->next) { 4524 if (frag == cfp->ofrag && cfp->nfrag != 0) 4525 return (cfp->frags); 4526 } 4527 4528 return (0); 4529 } 4530 4531 /* 4532 * Figure out how much the filesystem can be grown. The limiting factor is 4533 * the available free space needed to extend the cg summary info block. 4534 * The free space is determined in three steps: 4535 * - Try to extend the cg summary block to the required size. 4536 * - Find free blocks in last cg. 4537 * - Find free space in the last already allocated fragment of the summary info 4538 * block, and use it for additional csum structures. 4539 * Return the maximum size of the new filesystem or 0 if it can't be grown. 4540 * Please note that this function leaves the global list pointers csfrag, 4541 * csfragfree, and csfragino initialized, and the caller is responsible for 4542 * freeing the lists. 4543 */ 4544 diskaddr_t 4545 probe_summaryinfo() 4546 { 4547 /* fragments by which the csum block can be extended. */ 4548 int64_t growth_csum_frags = 0; 4549 /* fragments by which the filesystem can be extended. */ 4550 int64_t growth_fs_frags = 0; 4551 int64_t new_fs_cssize; /* size of csum blk in the new FS */ 4552 int64_t new_fs_ncg; /* number of cg in the new FS */ 4553 int64_t spare_csum; 4554 daddr32_t oldfrag_daddr; 4555 daddr32_t newfrag_daddr; 4556 daddr32_t daddr; 4557 int i; 4558 4559 /* 4560 * read and verify the superblock 4561 */ 4562 rdfs((diskaddr_t)(SBOFF / sectorsize), (int)sbsize, (char *)&sblock); 4563 (void) checksblock(sblock, 0); 4564 4565 /* 4566 * check how much we can extend the cg summary info block 4567 */ 4568 4569 /* 4570 * read current summary information 4571 */ 4572 fscs = read_summaryinfo(&sblock); 4573 4574 /* 4575 * build list of frags needed for cg summary info block extension 4576 */ 4577 oldfrag_daddr = howmany(sblock.fs_cssize, sblock.fs_fsize) + 4578 sblock.fs_csaddr; 4579 new_fs_ncg = howmany(dbtofsb(&sblock, fssize_db), sblock.fs_fpg); 4580 new_fs_cssize = fragroundup(&sblock, new_fs_ncg * sizeof (struct csum)); 4581 newfrag_daddr = howmany(new_fs_cssize, sblock.fs_fsize) + 4582 sblock.fs_csaddr; 4583 /* 4584 * add all of the frags that are required to grow the cyl summary to the 4585 * csfrag list, which is the generic/unknown list, since at this point 4586 * we don't yet know the state of those frags. 4587 */ 4588 for (daddr = oldfrag_daddr; daddr < newfrag_daddr; daddr++) 4589 addcsfrag((ino_t)0, daddr, &csfrag); 4590 4591 /* 4592 * filter free fragments and allocate them. Note that the free frags 4593 * must be allocated first otherwise they could be grabbed by 4594 * alloccsfragino() for data frags. 4595 */ 4596 findcsfragfree(); 4597 alloccsfragfree(); 4598 4599 /* 4600 * filter fragments owned by inodes and allocate them 4601 */ 4602 grow_fs_ncg = sblock.fs_ncg; /* findcsfragino() needs this glob. var. */ 4603 findcsfragino(); 4604 alloccsfragino(); 4605 4606 if (notenoughspace()) { 4607 /* 4608 * check how many consecutive fragments could be allocated 4609 * in both lists. 4610 */ 4611 int64_t tmp_frags; 4612 for (daddr = oldfrag_daddr; daddr < newfrag_daddr; 4613 daddr += tmp_frags) { 4614 if ((tmp_frags = checkfragallocated(daddr)) > 0) 4615 growth_csum_frags += tmp_frags; 4616 else 4617 break; 4618 } 4619 } else { 4620 /* 4621 * We have all we need for the new desired size, 4622 * so clean up and report back. 4623 */ 4624 return (fssize_db); 4625 } 4626 4627 /* 4628 * given the number of fragments by which the csum block can be grown 4629 * compute by how many new fragments the FS can be increased. 4630 * It is the number of csum instances per fragment multiplied by 4631 * `growth_csum_frags' and the number of fragments per cylinder group. 4632 */ 4633 growth_fs_frags = howmany(sblock.fs_fsize, sizeof (struct csum)) * 4634 growth_csum_frags * sblock.fs_fpg; 4635 4636 /* 4637 * compute free fragments in the last cylinder group 4638 */ 4639 rdcg(sblock.fs_ncg - 1); 4640 growth_fs_frags += sblock.fs_fpg - acg.cg_ndblk; 4641 4642 /* 4643 * compute how many csum instances are unused in the old csum block. 4644 * For each unused csum instance the FS can be grown by one cylinder 4645 * group without extending the csum block. 4646 */ 4647 spare_csum = howmany(sblock.fs_cssize, sizeof (struct csum)) - 4648 sblock.fs_ncg; 4649 if (spare_csum > 0) 4650 growth_fs_frags += spare_csum * sblock.fs_fpg; 4651 4652 /* 4653 * recalculate the new filesystem size in sectors, shorten it by 4654 * the requested size `fssize_db' if necessary. 4655 */ 4656 if (growth_fs_frags > 0) { 4657 diskaddr_t sect; 4658 sect = (sblock.fs_size + growth_fs_frags) * sblock.fs_nspf; 4659 return ((sect > fssize_db) ? fssize_db : sect); 4660 } 4661 4662 return (0); 4663 } 4664 4665 void 4666 extendsummaryinfo() 4667 { 4668 int64_t i; 4669 int localtest = test; 4670 int64_t frags; 4671 daddr32_t oldfrag; 4672 daddr32_t newfrag; 4673 4674 /* 4675 * if no-write (-N), don't bother 4676 */ 4677 if (Nflag) 4678 return; 4679 4680 again: 4681 flcg(); 4682 /* 4683 * summary info did not change size -- do nothing unless in test mode 4684 */ 4685 if (grow_fs_cssize == sblock.fs_cssize) 4686 if (!localtest) 4687 return; 4688 4689 /* 4690 * build list of frags needed for additional summary information 4691 */ 4692 oldfrag = howmany(grow_fs_cssize, sblock.fs_fsize) + grow_fs_csaddr; 4693 newfrag = howmany(sblock.fs_cssize, sblock.fs_fsize) + grow_fs_csaddr; 4694 /* 4695 * add all of the frags that are required to grow the cyl summary to the 4696 * csfrag list, which is the generic/unknown list, since at this point 4697 * we don't yet know the state of those frags. 4698 */ 4699 for (i = oldfrag, frags = 0; i < newfrag; ++i, ++frags) 4700 addcsfrag((ino_t)0, (diskaddr_t)i, &csfrag); 4701 /* 4702 * reduce the number of data blocks in the file system (fs_dsize) by 4703 * the number of frags that need to be added to the cyl summary 4704 */ 4705 sblock.fs_dsize -= (newfrag - oldfrag); 4706 4707 /* 4708 * In test mode, we move more data than necessary from 4709 * cylinder group 0. The lookup/allocate/move code can be 4710 * better stressed without having to create HUGE file systems. 4711 */ 4712 if (localtest) 4713 for (i = newfrag; i < grow_sifrag; ++i) { 4714 if (frags >= testfrags) 4715 break; 4716 frags++; 4717 addcsfrag((ino_t)0, (diskaddr_t)i, &csfrag); 4718 } 4719 4720 /* 4721 * move frags to free or inode lists, depending on owner 4722 */ 4723 findcsfragfree(); 4724 findcsfragino(); 4725 4726 /* 4727 * if not all frags can be located, file system must be inconsistent 4728 */ 4729 if (csfrag) { 4730 isbad = 1; /* should already be set, but make sure */ 4731 lockexit(32); 4732 } 4733 4734 /* 4735 * allocate the free frags. Note that the free frags must be allocated 4736 * first otherwise they could be grabbed by alloccsfragino() for data 4737 * frags. 4738 */ 4739 alloccsfragfree(); 4740 /* 4741 * allocate extra space for inode frags 4742 */ 4743 alloccsfragino(); 4744 4745 /* 4746 * not enough space 4747 */ 4748 if (notenoughspace()) { 4749 unalloccsfragfree(); 4750 unalloccsfragino(); 4751 if (localtest && !testforce) { 4752 localtest = 0; 4753 goto again; 4754 } 4755 (void) fprintf(stderr, gettext("Not enough free space\n")); 4756 lockexit(NOTENOUGHSPACE); 4757 } 4758 4759 /* 4760 * copy the data from old frags to new frags 4761 */ 4762 copycsfragino(); 4763 4764 /* 4765 * fix the inodes to point to the new frags 4766 */ 4767 fixcsfragino(); 4768 4769 /* 4770 * We may have moved more frags than we needed. Free them. 4771 */ 4772 rdcg((long)0); 4773 for (i = newfrag; i <= maxcsfrag; ++i) 4774 setbit(cg_blksfree(&acg), i-cgbase(&sblock, 0)); 4775 wtcg(); 4776 4777 flcg(); 4778 } 4779 4780 /* 4781 * Check if all fragments in the `csfragino' list were reallocated. 4782 */ 4783 int 4784 notenoughspace() 4785 { 4786 struct csfrag *cfp; 4787 4788 /* 4789 * If any element in the csfragino array has a "new frag location" 4790 * of 0, the allocfrags() function was unsuccessful in allocating 4791 * space for moving the frag represented by this array element. 4792 */ 4793 for (cfp = csfragino; cfp; cfp = cfp->next) 4794 if (cfp->nfrag == 0) 4795 return (1); 4796 return (0); 4797 } 4798 4799 void 4800 unalloccsfragino() 4801 { 4802 struct csfrag *cfp; 4803 4804 while ((cfp = csfragino) != NULL) { 4805 if (cfp->nfrag) 4806 freefrags(cfp->nfrag, cfp->frags, cfp->cylno); 4807 delcsfrag(cfp->ofrag, &csfragino); 4808 } 4809 } 4810 4811 void 4812 unalloccsfragfree() 4813 { 4814 struct csfrag *cfp; 4815 4816 while ((cfp = csfragfree) != NULL) { 4817 freefrags(cfp->ofrag, cfp->frags, cfp->cylno); 4818 delcsfrag(cfp->ofrag, &csfragfree); 4819 } 4820 } 4821 4822 /* 4823 * For each frag in the "as-yet-unclassified" list (csfrag), see if 4824 * it's free (i.e., its bit is set in the free frag bit map). If so, 4825 * move it from the "as-yet-unclassified" list to the csfragfree list. 4826 */ 4827 void 4828 findcsfragfree() 4829 { 4830 struct csfrag *cfp; 4831 struct csfrag *cfpnext; 4832 4833 /* 4834 * move free frags onto the free-frag list 4835 */ 4836 rdcg((long)0); 4837 for (cfp = csfrag; cfp; cfp = cfpnext) { 4838 cfpnext = cfp->next; 4839 if (isset(cg_blksfree(&acg), cfp->ofrag - cgbase(&sblock, 0))) { 4840 addcsfrag(cfp->ino, cfp->ofrag, &csfragfree); 4841 delcsfrag(cfp->ofrag, &csfrag); 4842 } 4843 } 4844 } 4845 4846 void 4847 copycsfragino() 4848 { 4849 struct csfrag *cfp; 4850 char buf[MAXBSIZE]; 4851 4852 /* 4853 * copy data from old frags to newly allocated frags 4854 */ 4855 for (cfp = csfragino; cfp; cfp = cfp->next) { 4856 rdfs(fsbtodb(&sblock, (uint64_t)cfp->ofrag), (int)cfp->size, 4857 buf); 4858 wtfs(fsbtodb(&sblock, (uint64_t)cfp->nfrag), (int)cfp->size, 4859 buf); 4860 } 4861 } 4862 4863 long curcylno = -1; 4864 int cylnodirty = 0; 4865 4866 void 4867 rdcg(long cylno) 4868 { 4869 if (cylno != curcylno) { 4870 flcg(); 4871 curcylno = cylno; 4872 rdfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, curcylno)), 4873 (int)sblock.fs_cgsize, (char *)&acg); 4874 } 4875 } 4876 4877 void 4878 flcg() 4879 { 4880 if (cylnodirty) { 4881 if (debug && Pflag) { 4882 (void) fprintf(stderr, 4883 "Assert: cylnodirty set in probe mode\n"); 4884 return; 4885 } 4886 resetallocinfo(); 4887 wtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, curcylno)), 4888 (int)sblock.fs_cgsize, (char *)&acg); 4889 cylnodirty = 0; 4890 } 4891 curcylno = -1; 4892 } 4893 4894 void 4895 wtcg() 4896 { 4897 if (!Pflag) { 4898 /* probe mode should never write to disk */ 4899 cylnodirty = 1; 4900 } 4901 } 4902 4903 void 4904 allocfrags(long frags, daddr32_t *fragp, long *cylnop) 4905 { 4906 int i; 4907 int j; 4908 long bits; 4909 long bit; 4910 4911 /* 4912 * Allocate a free-frag range in an old cylinder group 4913 */ 4914 for (i = 0, *fragp = 0; i < grow_fs_ncg; ++i) { 4915 if (((fscs+i)->cs_nffree < frags) && ((fscs+i)->cs_nbfree == 0)) 4916 continue; 4917 rdcg((long)i); 4918 bit = bits = 0; 4919 while (findfreerange(&bit, &bits)) { 4920 if (frags <= bits) { 4921 for (j = 0; j < frags; ++j) 4922 clrbit(cg_blksfree(&acg), bit+j); 4923 wtcg(); 4924 *cylnop = i; 4925 *fragp = bit + cgbase(&sblock, i); 4926 return; 4927 } 4928 bit += bits; 4929 } 4930 } 4931 } 4932 4933 /* 4934 * Allocate space for frags that need to be moved in order to free up space for 4935 * expanding the cylinder summary info. 4936 * For each frag that needs to be moved (each frag or range of frags in 4937 * the csfragino list), allocate a new location and store the frag number 4938 * of that new location in the nfrag field of the csfrag struct. 4939 * If a new frag can't be allocated for any element in the csfragino list, 4940 * set the new frag number for that element to 0 and return immediately. 4941 * The notenoughspace() function will detect this condition. 4942 */ 4943 void 4944 alloccsfragino() 4945 { 4946 struct csfrag *cfp; 4947 4948 /* 4949 * allocate space for inode frag ranges 4950 */ 4951 for (cfp = csfragino; cfp; cfp = cfp->next) { 4952 allocfrags(cfp->frags, &cfp->nfrag, &cfp->cylno); 4953 if (cfp->nfrag == 0) 4954 break; 4955 } 4956 } 4957 4958 void 4959 alloccsfragfree() 4960 { 4961 struct csfrag *cfp; 4962 4963 /* 4964 * allocate the free frags needed for extended summary info 4965 */ 4966 rdcg((long)0); 4967 4968 for (cfp = csfragfree; cfp; cfp = cfp->next) 4969 clrbit(cg_blksfree(&acg), cfp->ofrag - cgbase(&sblock, 0)); 4970 4971 wtcg(); 4972 } 4973 4974 void 4975 freefrags(daddr32_t frag, long frags, long cylno) 4976 { 4977 int i; 4978 4979 /* 4980 * free frags 4981 */ 4982 rdcg(cylno); 4983 for (i = 0; i < frags; ++i) { 4984 setbit(cg_blksfree(&acg), (frag+i) - cgbase(&sblock, cylno)); 4985 } 4986 wtcg(); 4987 } 4988 4989 int 4990 findfreerange(long *bitp, long *bitsp) 4991 { 4992 long bit; 4993 4994 /* 4995 * find a range of free bits in a cylinder group bit map 4996 */ 4997 for (bit = *bitp, *bitsp = 0; bit < acg.cg_ndblk; ++bit) 4998 if (isset(cg_blksfree(&acg), bit)) 4999 break; 5000 5001 if (bit >= acg.cg_ndblk) 5002 return (0); 5003 5004 *bitp = bit; 5005 *bitsp = 1; 5006 for (++bit; bit < acg.cg_ndblk; ++bit, ++(*bitsp)) { 5007 if ((bit % sblock.fs_frag) == 0) 5008 break; 5009 if (isclr(cg_blksfree(&acg), bit)) 5010 break; 5011 } 5012 return (1); 5013 } 5014 5015 void 5016 resetallocinfo() 5017 { 5018 long cno; 5019 long bit; 5020 long bits; 5021 5022 /* 5023 * Compute the free blocks/frags info and update the appropriate 5024 * inmemory superblock, summary info, and cylinder group fields 5025 */ 5026 sblock.fs_cstotal.cs_nffree -= acg.cg_cs.cs_nffree; 5027 sblock.fs_cstotal.cs_nbfree -= acg.cg_cs.cs_nbfree; 5028 5029 acg.cg_cs.cs_nffree = 0; 5030 acg.cg_cs.cs_nbfree = 0; 5031 5032 bzero((caddr_t)acg.cg_frsum, sizeof (acg.cg_frsum)); 5033 bzero((caddr_t)cg_blktot(&acg), (int)(acg.cg_iusedoff-acg.cg_btotoff)); 5034 5035 bit = bits = 0; 5036 while (findfreerange(&bit, &bits)) { 5037 if (bits == sblock.fs_frag) { 5038 acg.cg_cs.cs_nbfree++; 5039 cno = cbtocylno(&sblock, bit); 5040 cg_blktot(&acg)[cno]++; 5041 cg_blks(&sblock, &acg, cno)[cbtorpos(&sblock, bit)]++; 5042 } else { 5043 acg.cg_cs.cs_nffree += bits; 5044 acg.cg_frsum[bits]++; 5045 } 5046 bit += bits; 5047 } 5048 5049 *(fscs + acg.cg_cgx) = acg.cg_cs; 5050 5051 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree; 5052 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree; 5053 } 5054 5055 void 5056 extendcg(long cylno) 5057 { 5058 int i; 5059 diskaddr_t dupper; 5060 diskaddr_t cbase; 5061 diskaddr_t dmax; 5062 5063 /* 5064 * extend the cylinder group at the end of the old file system 5065 * if it was partially allocated becase of lack of space 5066 */ 5067 flcg(); 5068 rdcg(cylno); 5069 5070 dupper = acg.cg_ndblk; 5071 if (cylno == sblock.fs_ncg - 1) 5072 acg.cg_ncyl = sblock.fs_ncyl - (sblock.fs_cpg * cylno); 5073 else 5074 acg.cg_ncyl = sblock.fs_cpg; 5075 cbase = cgbase(&sblock, cylno); 5076 dmax = cbase + sblock.fs_fpg; 5077 if (dmax > sblock.fs_size) 5078 dmax = sblock.fs_size; 5079 acg.cg_ndblk = dmax - cbase; 5080 5081 for (i = dupper; i < acg.cg_ndblk; ++i) 5082 setbit(cg_blksfree(&acg), i); 5083 5084 sblock.fs_dsize += (acg.cg_ndblk - dupper); 5085 5086 wtcg(); 5087 flcg(); 5088 } 5089 5090 struct lockfs lockfs; 5091 int lockfd; 5092 int islocked; 5093 int lockfskey; 5094 char lockfscomment[128]; 5095 5096 void 5097 ulockfs() 5098 { 5099 /* 5100 * if the file system was locked, unlock it before exiting 5101 */ 5102 if (islocked == 0) 5103 return; 5104 5105 /* 5106 * first, check if the lock held 5107 */ 5108 lockfs.lf_flags = LOCKFS_MOD; 5109 if (ioctl(lockfd, _FIOLFSS, &lockfs) == -1) { 5110 perror(directory); 5111 lockexit(32); 5112 } 5113 5114 if (LOCKFS_IS_MOD(&lockfs)) { 5115 (void) fprintf(stderr, 5116 gettext("FILE SYSTEM CHANGED DURING GROWFS!\n")); 5117 (void) fprintf(stderr, 5118 gettext(" See lockfs(1), umount(1), and fsck(1)\n")); 5119 lockexit(32); 5120 } 5121 /* 5122 * unlock the file system 5123 */ 5124 lockfs.lf_lock = LOCKFS_ULOCK; 5125 lockfs.lf_flags = 0; 5126 lockfs.lf_key = lockfskey; 5127 clockfs(); 5128 if (ioctl(lockfd, _FIOLFS, &lockfs) == -1) { 5129 perror(directory); 5130 lockexit(32); 5131 } 5132 } 5133 5134 void 5135 wlockfs() 5136 { 5137 5138 /* 5139 * if no-write (-N), don't bother 5140 */ 5141 if (Nflag) 5142 return; 5143 /* 5144 * open the mountpoint, and write lock the file system 5145 */ 5146 if ((lockfd = open64(directory, O_RDONLY)) == -1) { 5147 perror(directory); 5148 lockexit(32); 5149 } 5150 5151 /* 5152 * check if it is already locked 5153 */ 5154 if (ioctl(lockfd, _FIOLFSS, &lockfs) == -1) { 5155 perror(directory); 5156 lockexit(32); 5157 } 5158 5159 if (lockfs.lf_lock != LOCKFS_WLOCK) { 5160 lockfs.lf_lock = LOCKFS_WLOCK; 5161 lockfs.lf_flags = 0; 5162 lockfs.lf_key = 0; 5163 clockfs(); 5164 if (ioctl(lockfd, _FIOLFS, &lockfs) == -1) { 5165 perror(directory); 5166 lockexit(32); 5167 } 5168 } 5169 islocked = 1; 5170 lockfskey = lockfs.lf_key; 5171 } 5172 5173 void 5174 clockfs() 5175 { 5176 time_t t; 5177 char *ct; 5178 5179 (void) time(&t); 5180 ct = ctime(&t); 5181 ct[strlen(ct)-1] = '\0'; 5182 5183 (void) sprintf(lockfscomment, "%s -- mkfs pid %d", ct, getpid()); 5184 lockfs.lf_comlen = strlen(lockfscomment)+1; 5185 lockfs.lf_comment = lockfscomment; 5186 } 5187 5188 /* 5189 * Write the csum records and the superblock 5190 */ 5191 void 5192 wtsb() 5193 { 5194 long i; 5195 5196 /* 5197 * write summary information 5198 */ 5199 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) 5200 wtfs(fsbtodb(&sblock, (uint64_t)(sblock.fs_csaddr + 5201 numfrags(&sblock, i))), 5202 (int)(sblock.fs_cssize - i < sblock.fs_bsize ? 5203 sblock.fs_cssize - i : sblock.fs_bsize), 5204 ((char *)fscs) + i); 5205 5206 /* 5207 * write superblock 5208 */ 5209 sblock.fs_time = mkfstime; 5210 wtfs((diskaddr_t)(SBOFF / sectorsize), sbsize, (char *)&sblock); 5211 } 5212 5213 /* 5214 * Verify that the optimization selection is reasonable, and advance 5215 * the global "string" appropriately. 5216 */ 5217 static char 5218 checkopt(char *optim) 5219 { 5220 char opt; 5221 int limit = strcspn(optim, ","); 5222 5223 switch (limit) { 5224 case 0: /* missing indicator (have comma or nul) */ 5225 (void) fprintf(stderr, gettext( 5226 "mkfs: missing optimization flag reset to `t' (time)\n")); 5227 opt = 't'; 5228 break; 5229 5230 case 1: /* single-character indicator */ 5231 opt = *optim; 5232 if ((opt != 's') && (opt != 't')) { 5233 (void) fprintf(stderr, gettext( 5234 "mkfs: bad optimization value `%c' reset to `t' (time)\n"), 5235 opt); 5236 opt = 't'; 5237 } 5238 break; 5239 5240 default: /* multi-character indicator */ 5241 (void) fprintf(stderr, gettext( 5242 "mkfs: bad optimization value `%*.*s' reset to `t' (time)\n"), 5243 limit, limit, optim); 5244 opt = 't'; 5245 break; 5246 } 5247 5248 string += limit; 5249 5250 return (opt); 5251 } 5252 5253 /* 5254 * Verify that the mtb selection is reasonable, and advance 5255 * the global "string" appropriately. 5256 */ 5257 static char 5258 checkmtb(char *mtbarg) 5259 { 5260 char mtbc; 5261 int limit = strcspn(mtbarg, ","); 5262 5263 switch (limit) { 5264 case 0: /* missing indicator (have comma or nul) */ 5265 (void) fprintf(stderr, gettext( 5266 "mkfs: missing mtb flag reset to `n' (no mtb support)\n")); 5267 mtbc = 'n'; 5268 break; 5269 5270 case 1: /* single-character indicator */ 5271 mtbc = tolower(*mtbarg); 5272 if ((mtbc != 'y') && (mtbc != 'n')) { 5273 (void) fprintf(stderr, gettext( 5274 "mkfs: bad mtb value `%c' reset to `n' (no mtb support)\n"), 5275 mtbc); 5276 mtbc = 'n'; 5277 } 5278 break; 5279 5280 default: /* multi-character indicator */ 5281 (void) fprintf(stderr, gettext( 5282 "mkfs: bad mtb value `%*.*s' reset to `n' (no mtb support)\n"), 5283 limit, limit, mtbarg); 5284 opt = 'n'; 5285 break; 5286 } 5287 5288 string += limit; 5289 5290 return (mtbc); 5291 } 5292 5293 /* 5294 * Verify that a value is in a range. If it is not, resets it to 5295 * its default value if one is supplied, exits otherwise. 5296 * 5297 * When testing, can compare user_supplied to RC_KEYWORD or RC_POSITIONAL. 5298 */ 5299 static void 5300 range_check(long *varp, char *name, long minimum, long maximum, 5301 long def_val, int user_supplied) 5302 { 5303 dprintf(("DeBuG %s : %ld (%ld %ld %ld)\n", 5304 name, *varp, minimum, maximum, def_val)); 5305 5306 if ((*varp < minimum) || (*varp > maximum)) { 5307 if (user_supplied != RC_DEFAULT) { 5308 (void) fprintf(stderr, gettext( 5309 "mkfs: bad value for %s: %ld must be between %ld and %ld\n"), 5310 name, *varp, minimum, maximum); 5311 } 5312 if (def_val != NO_DEFAULT) { 5313 if (user_supplied) { 5314 (void) fprintf(stderr, 5315 gettext("mkfs: %s reset to default %ld\n"), 5316 name, def_val); 5317 } 5318 *varp = def_val; 5319 dprintf(("DeBuG %s : %ld\n", name, *varp)); 5320 return; 5321 } 5322 lockexit(2); 5323 /*NOTREACHED*/ 5324 } 5325 } 5326 5327 /* 5328 * Verify that a value is in a range. If it is not, resets it to 5329 * its default value if one is supplied, exits otherwise. 5330 * 5331 * When testing, can compare user_supplied to RC_KEYWORD or RC_POSITIONAL. 5332 */ 5333 static void 5334 range_check_64(uint64_t *varp, char *name, uint64_t minimum, uint64_t maximum, 5335 uint64_t def_val, int user_supplied) 5336 { 5337 if ((*varp < minimum) || (*varp > maximum)) { 5338 if (user_supplied != RC_DEFAULT) { 5339 (void) fprintf(stderr, gettext( 5340 "mkfs: bad value for %s: %lld must be between %lld and %lld\n"), 5341 name, *varp, minimum, maximum); 5342 } 5343 if (def_val != NO_DEFAULT) { 5344 if (user_supplied) { 5345 (void) fprintf(stderr, 5346 gettext("mkfs: %s reset to default %lld\n"), 5347 name, def_val); 5348 } 5349 *varp = def_val; 5350 return; 5351 } 5352 lockexit(2); 5353 /*NOTREACHED*/ 5354 } 5355 } 5356 5357 /* 5358 * Blocks SIGINT from delivery. Returns the previous mask in the 5359 * buffer provided, so that mask may be later restored. 5360 */ 5361 static void 5362 block_sigint(sigset_t *old_mask) 5363 { 5364 sigset_t block_mask; 5365 5366 if (sigemptyset(&block_mask) < 0) { 5367 fprintf(stderr, gettext("Could not clear signal mask\n")); 5368 lockexit(3); 5369 } 5370 if (sigaddset(&block_mask, SIGINT) < 0) { 5371 fprintf(stderr, gettext("Could not set signal mask\n")); 5372 lockexit(3); 5373 } 5374 if (sigprocmask(SIG_BLOCK, &block_mask, old_mask) < 0) { 5375 fprintf(stderr, gettext("Could not block SIGINT\n")); 5376 lockexit(3); 5377 } 5378 } 5379 5380 /* 5381 * Restores the signal mask that was in force before a call 5382 * to block_sigint(). This may actually still have SIGINT blocked, 5383 * if we've been recursively invoked. 5384 */ 5385 static void 5386 unblock_sigint(sigset_t *old_mask) 5387 { 5388 if (sigprocmask(SIG_UNBLOCK, old_mask, (sigset_t *)NULL) < 0) { 5389 fprintf(stderr, gettext("Could not restore signal mask\n")); 5390 lockexit(3); 5391 } 5392 } 5393 5394 /* 5395 * Attempt to be somewhat graceful about being interrupted, rather than 5396 * just silently leaving the filesystem in an unusable state. 5397 * 5398 * The kernel has blocked SIGINT upon entry, so we don't have to worry 5399 * about recursion if the user starts pounding on the keyboard. 5400 */ 5401 static void 5402 recover_from_sigint(int signum) 5403 { 5404 if (fso > -1) { 5405 if ((Nflag != 0) || confirm_abort()) { 5406 lockexit(4); 5407 } 5408 } 5409 } 5410 5411 static int 5412 confirm_abort(void) 5413 { 5414 char line[80]; 5415 5416 printf(gettext("\n\nAborting at this point will leave the filesystem " 5417 "in an inconsistent\nstate. If you do choose to stop, " 5418 "you will be given instructions on how to\nrecover " 5419 "the filesystem. Do you wish to cancel the filesystem " 5420 "grow\noperation (y/n)?")); 5421 if (getline(stdin, line, sizeof (line)) == EOF) 5422 line[0] = 'y'; 5423 5424 printf("\n"); 5425 if (line[0] == 'y' || line[0] == 'Y') 5426 return (1); 5427 else { 5428 return (0); 5429 } 5430 } 5431 5432 static int 5433 getline(FILE *fp, char *loc, int maxlen) 5434 { 5435 int n; 5436 char *p, *lastloc; 5437 5438 p = loc; 5439 lastloc = &p[maxlen-1]; 5440 while ((n = getc(fp)) != '\n') { 5441 if (n == EOF) 5442 return (EOF); 5443 if (!isspace(n) && p < lastloc) 5444 *p++ = n; 5445 } 5446 *p = 0; 5447 return (p - loc); 5448 } 5449 5450 /* 5451 * Calculate the maximum value of cylinders-per-group for a file 5452 * system with the characteristics: 5453 * 5454 * bsize - file system block size 5455 * fragsize - frag size 5456 * nbpi - number of bytes of disk space per inode 5457 * nrpos - number of rotational positions 5458 * spc - sectors per cylinder 5459 * 5460 * These five characteristic are not adjustable (by this function). 5461 * The only attribute of the file system which IS adjusted by this 5462 * function in order to maximize cylinders-per-group is the proportion 5463 * of the cylinder group overhead block used for the inode map. The 5464 * inode map cannot occupy more than one-third of the cylinder group 5465 * overhead block, but it's OK for it to occupy less than one-third 5466 * of the overhead block. 5467 * 5468 * The setting of nbpi determines one possible value for the maximum 5469 * size of a cylinder group. It does so because it determines the total 5470 * number of inodes in the file system (file system size is fixed, and 5471 * nbpi is fixed, so the total number of inodes is fixed too). The 5472 * cylinder group has to be small enough so that the number of inodes 5473 * in the cylinder group is less than or equal to the number of bits 5474 * in one-third (or whatever proportion is assumed) of a file system 5475 * block. The details of the calculation are: 5476 * 5477 * The macro MAXIpG_B(bsize, inode_divisor) determines the maximum 5478 * number of inodes that can be in a cylinder group, given the 5479 * proportion of the cylinder group overhead block used for the 5480 * inode bitmaps (an inode_divisor of 3 means that 1/3 of the 5481 * block is used for inode bitmaps; an inode_divisor of 12 means 5482 * that 1/12 of the block is used for inode bitmaps.) 5483 * 5484 * Once the number of inodes per cylinder group is known, the 5485 * maximum value of cylinders-per-group (determined by nbpi) 5486 * is calculated by the formula 5487 * 5488 * maxcpg_given_nbpi = (size of a cylinder group)/(size of a cylinder) 5489 * 5490 * = (inodes-per-cg * nbpi)/(spc * DEV_BSIZE) 5491 * 5492 * (Interestingly, the size of the file system never enters 5493 * into this calculation.) 5494 * 5495 * Another possible value for the maximum cylinder group size is determined 5496 * by frag_size and nrpos. The frags in the cylinder group must be 5497 * representable in the frag bitmaps in the cylinder overhead block and the 5498 * rotational positions for each cylinder must be represented in the 5499 * rotational position tables. The calculation of the maximum cpg 5500 * value, given the frag and nrpos vales, is: 5501 * 5502 * maxcpg_given_fragsize = 5503 * (available space in the overhead block) / (size of per-cylinder data) 5504 * 5505 * The available space in the overhead block = 5506 * bsize - sizeof (struct cg) - space_used_for_inode_bitmaps 5507 * 5508 * The size of the per-cylinder data is: 5509 * sizeof(long) # for the "blocks avail per cylinder" field 5510 * + nrpos * sizeof(short) # for the rotational position table entry 5511 * + frags-per-cylinder/NBBY # number of bytes to represent this 5512 * # cylinder in the frag bitmap 5513 * 5514 * The two calculated maximum values of cylinder-per-group will typically 5515 * turn out to be different, since they are derived from two different 5516 * constraints. Usually, maxcpg_given_nbpi is much bigger than 5517 * maxcpg_given_fragsize. But they can be brought together by 5518 * adjusting the proportion of the overhead block dedicated to 5519 * the inode bitmaps. Decreasing the proportion of the cylinder 5520 * group overhead block used for inode maps will decrease 5521 * maxcpg_given_nbpi and increase maxcpg_given_fragsize. 5522 * 5523 * This function calculates the initial values of maxcpg_given_nbpi 5524 * and maxcpg_given_fragsize assuming that 1/3 of the cg overhead 5525 * block is used for inode bitmaps. Then it decreases the proportion 5526 * of the cg overhead block used for inode bitmaps (by increasing 5527 * the value of inode_divisor) until maxcpg_given_nbpi and 5528 * maxcpg_given_fragsize are the same, or stop changing, or 5529 * maxcpg_given_nbpi is less than maxcpg_given_fragsize. 5530 * 5531 * The loop terminates when any of the following occur: 5532 * * maxcpg_given_fragsize is greater than or equal to 5533 * maxcpg_given_nbpi 5534 * * neither maxcpg_given_fragsize nor maxcpg_given_nbpi 5535 * change in the expected direction 5536 * 5537 * The loop is guaranteed to terminate because it only continues 5538 * while maxcpg_given_fragsize and maxcpg_given_nbpi are approaching 5539 * each other. As soon they cross each other, or neither one changes 5540 * in the direction of the other, or one of them moves in the wrong 5541 * direction, the loop completes. 5542 */ 5543 5544 static long 5545 compute_maxcpg(long bsize, long fragsize, long nbpi, long nrpos, long spc) 5546 { 5547 int maxcpg_given_nbpi; /* in cylinders */ 5548 int maxcpg_given_fragsize; /* in cylinders */ 5549 int spf; /* sectors per frag */ 5550 int inode_divisor; 5551 int old_max_given_frag = 0; 5552 int old_max_given_nbpi = INT_MAX; 5553 5554 spf = fragsize / DEV_BSIZE; 5555 inode_divisor = 3; 5556 5557 while (1) { 5558 maxcpg_given_nbpi = 5559 (((int64_t)(MAXIpG_B(bsize, inode_divisor))) * nbpi) / 5560 (DEV_BSIZE * ((int64_t)spc)); 5561 maxcpg_given_fragsize = 5562 (bsize - (sizeof (struct cg)) - (bsize / inode_divisor)) / 5563 (sizeof (long) + nrpos * sizeof (short) + 5564 (spc / spf) / NBBY); 5565 5566 if (maxcpg_given_fragsize >= maxcpg_given_nbpi) 5567 return (maxcpg_given_nbpi); 5568 5569 /* 5570 * If neither value moves toward the other, return the 5571 * least of the old values (we use the old instead of the 5572 * new because: if the old is the same as the new, it 5573 * doesn't matter which ones we use. If one of the 5574 * values changed, but in the wrong direction, the 5575 * new values are suspect. Better use the old. This 5576 * shouldn't happen, but it's best to check. 5577 */ 5578 5579 if (!(maxcpg_given_nbpi < old_max_given_nbpi) && 5580 !(maxcpg_given_fragsize > old_max_given_frag)) 5581 return (MIN(old_max_given_nbpi, old_max_given_frag)); 5582 5583 /* 5584 * This is probably impossible, but if one of the maxcpg 5585 * values moved in the "right" direction and one moved 5586 * in the "wrong" direction (that is, the two values moved 5587 * in the same direction), the previous conditional won't 5588 * recognize that the values aren't converging (since at 5589 * least one value moved in the "right" direction, the 5590 * last conditional says "keep going"). 5591 * 5592 * Just to make absolutely certain that the loop terminates, 5593 * check for one of the values moving in the "wrong" direction 5594 * and terminate the loop if it happens. 5595 */ 5596 5597 if (maxcpg_given_nbpi > old_max_given_nbpi || 5598 maxcpg_given_fragsize < old_max_given_frag) 5599 return (MIN(old_max_given_nbpi, old_max_given_frag)); 5600 5601 old_max_given_nbpi = maxcpg_given_nbpi; 5602 old_max_given_frag = maxcpg_given_fragsize; 5603 5604 inode_divisor++; 5605 } 5606 } 5607 5608 static int 5609 in_64bit_mode(void) 5610 { 5611 /* cmd must be an absolute path, for security */ 5612 char *cmd = "/usr/bin/isainfo -b"; 5613 char buf[BUFSIZ]; 5614 FILE *ptr; 5615 int retval = 0; 5616 5617 putenv("IFS= \t"); 5618 if ((ptr = popen(cmd, "r")) != NULL) { 5619 if (fgets(buf, BUFSIZ, ptr) != NULL && 5620 strncmp(buf, "64", 2) == 0) 5621 retval = 1; 5622 (void) pclose(ptr); 5623 } 5624 return (retval); 5625 } 5626 5627 /* 5628 * validate_size 5629 * 5630 * Return 1 if the device appears to be at least "size" sectors long. 5631 * Return 0 if it's shorter or we can't read it. 5632 */ 5633 5634 static int 5635 validate_size(int fd, diskaddr_t size) 5636 { 5637 char buf[DEV_BSIZE]; 5638 int rc; 5639 5640 if ((llseek(fd, (offset_t)((size - 1) * DEV_BSIZE), SEEK_SET) == -1) || 5641 (read(fd, buf, DEV_BSIZE)) != DEV_BSIZE) 5642 rc = 0; 5643 else 5644 rc = 1; 5645 return (rc); 5646 } 5647 5648 /* 5649 * Print every field of the calculated superblock, along with 5650 * its value. To make parsing easier on the caller, the value 5651 * is printed first, then the name. Additionally, there's only 5652 * one name/value pair per line. All values are reported in 5653 * hexadecimal (with the traditional 0x prefix), as that's slightly 5654 * easier for humans to read. Not that they're expected to, but 5655 * debugging happens. 5656 */ 5657 static void 5658 dump_sblock(void) 5659 { 5660 int row, column, pending, written; 5661 caddr_t source; 5662 5663 if (Rflag) { 5664 pending = sizeof (sblock); 5665 source = (caddr_t)&sblock; 5666 do { 5667 written = write(fileno(stdout), source, pending); 5668 pending -= written; 5669 source += written; 5670 } while ((pending > 0) && (written > 0)); 5671 5672 if (written < 0) { 5673 perror(gettext("Binary dump of superblock failed")); 5674 lockexit(1); 5675 } 5676 return; 5677 } else { 5678 printf("0x%x sblock.fs_link\n", sblock.fs_link); 5679 printf("0x%x sblock.fs_rolled\n", sblock.fs_rolled); 5680 printf("0x%x sblock.fs_sblkno\n", sblock.fs_sblkno); 5681 printf("0x%x sblock.fs_cblkno\n", sblock.fs_cblkno); 5682 printf("0x%x sblock.fs_iblkno\n", sblock.fs_iblkno); 5683 printf("0x%x sblock.fs_dblkno\n", sblock.fs_dblkno); 5684 printf("0x%x sblock.fs_cgoffset\n", sblock.fs_cgoffset); 5685 printf("0x%x sblock.fs_cgmask\n", sblock.fs_cgmask); 5686 printf("0x%x sblock.fs_time\n", sblock.fs_time); 5687 printf("0x%x sblock.fs_size\n", sblock.fs_size); 5688 printf("0x%x sblock.fs_dsize\n", sblock.fs_dsize); 5689 printf("0x%x sblock.fs_ncg\n", sblock.fs_ncg); 5690 printf("0x%x sblock.fs_bsize\n", sblock.fs_bsize); 5691 printf("0x%x sblock.fs_fsize\n", sblock.fs_fsize); 5692 printf("0x%x sblock.fs_frag\n", sblock.fs_frag); 5693 printf("0x%x sblock.fs_minfree\n", sblock.fs_minfree); 5694 printf("0x%x sblock.fs_rotdelay\n", sblock.fs_rotdelay); 5695 printf("0x%x sblock.fs_rps\n", sblock.fs_rps); 5696 printf("0x%x sblock.fs_bmask\n", sblock.fs_bmask); 5697 printf("0x%x sblock.fs_fmask\n", sblock.fs_fmask); 5698 printf("0x%x sblock.fs_bshift\n", sblock.fs_bshift); 5699 printf("0x%x sblock.fs_fshift\n", sblock.fs_fshift); 5700 printf("0x%x sblock.fs_maxcontig\n", sblock.fs_maxcontig); 5701 printf("0x%x sblock.fs_maxbpg\n", sblock.fs_maxbpg); 5702 printf("0x%x sblock.fs_fragshift\n", sblock.fs_fragshift); 5703 printf("0x%x sblock.fs_fsbtodb\n", sblock.fs_fsbtodb); 5704 printf("0x%x sblock.fs_sbsize\n", sblock.fs_sbsize); 5705 printf("0x%x sblock.fs_csmask\n", sblock.fs_csmask); 5706 printf("0x%x sblock.fs_csshift\n", sblock.fs_csshift); 5707 printf("0x%x sblock.fs_nindir\n", sblock.fs_nindir); 5708 printf("0x%x sblock.fs_inopb\n", sblock.fs_inopb); 5709 printf("0x%x sblock.fs_nspf\n", sblock.fs_nspf); 5710 printf("0x%x sblock.fs_optim\n", sblock.fs_optim); 5711 #ifdef _LITTLE_ENDIAN 5712 printf("0x%x sblock.fs_state\n", sblock.fs_state); 5713 #else 5714 printf("0x%x sblock.fs_npsect\n", sblock.fs_npsect); 5715 #endif 5716 printf("0x%x sblock.fs_si\n", sblock.fs_si); 5717 printf("0x%x sblock.fs_trackskew\n", sblock.fs_trackskew); 5718 printf("0x%x sblock.fs_id[0]\n", sblock.fs_id[0]); 5719 printf("0x%x sblock.fs_id[1]\n", sblock.fs_id[1]); 5720 printf("0x%x sblock.fs_csaddr\n", sblock.fs_csaddr); 5721 printf("0x%x sblock.fs_cssize\n", sblock.fs_cssize); 5722 printf("0x%x sblock.fs_cgsize\n", sblock.fs_cgsize); 5723 printf("0x%x sblock.fs_ntrak\n", sblock.fs_ntrak); 5724 printf("0x%x sblock.fs_nsect\n", sblock.fs_nsect); 5725 printf("0x%x sblock.fs_spc\n", sblock.fs_spc); 5726 printf("0x%x sblock.fs_ncyl\n", sblock.fs_ncyl); 5727 printf("0x%x sblock.fs_cpg\n", sblock.fs_cpg); 5728 printf("0x%x sblock.fs_ipg\n", sblock.fs_ipg); 5729 printf("0x%x sblock.fs_fpg\n", sblock.fs_fpg); 5730 printf("0x%x sblock.fs_cstotal\n", sblock.fs_cstotal); 5731 printf("0x%x sblock.fs_fmod\n", sblock.fs_fmod); 5732 printf("0x%x sblock.fs_clean\n", sblock.fs_clean); 5733 printf("0x%x sblock.fs_ronly\n", sblock.fs_ronly); 5734 printf("0x%x sblock.fs_flags\n", sblock.fs_flags); 5735 printf("0x%x sblock.fs_fsmnt\n", sblock.fs_fsmnt); 5736 printf("0x%x sblock.fs_cgrotor\n", sblock.fs_cgrotor); 5737 printf("0x%x sblock.fs_u.fs_csp\n", sblock.fs_u.fs_csp); 5738 printf("0x%x sblock.fs_cpc\n", sblock.fs_cpc); 5739 5740 /* 5741 * No macros are defined for the dimensions of the 5742 * opostbl array. 5743 */ 5744 for (row = 0; row < 16; row++) { 5745 for (column = 0; column < 8; column++) { 5746 printf("0x%x sblock.fs_opostbl[%d][%d]\n", 5747 sblock.fs_opostbl[row][column], 5748 row, column); 5749 } 5750 } 5751 5752 /* 5753 * Ditto the size of sparecon. 5754 */ 5755 for (row = 0; row < 51; row++) { 5756 printf("0x%x sblock.fs_sparecon[%d]\n", 5757 sblock.fs_sparecon[row], row); 5758 } 5759 5760 printf("0x%x sblock.fs_version\n", sblock.fs_version); 5761 printf("0x%x sblock.fs_logbno\n", sblock.fs_logbno); 5762 printf("0x%x sblock.fs_reclaim\n", sblock.fs_reclaim); 5763 printf("0x%x sblock.fs_sparecon2\n", sblock.fs_sparecon2); 5764 #ifdef _LITTLE_ENDIAN 5765 printf("0x%x sblock.fs_npsect\n", sblock.fs_npsect); 5766 #else 5767 printf("0x%x sblock.fs_state\n", sblock.fs_state); 5768 #endif 5769 printf("0x%llx sblock.fs_qbmask\n", sblock.fs_qbmask); 5770 printf("0x%llx sblock.fs_qfmask\n", sblock.fs_qfmask); 5771 printf("0x%x sblock.fs_postblformat\n", sblock.fs_postblformat); 5772 printf("0x%x sblock.fs_nrpos\n", sblock.fs_nrpos); 5773 printf("0x%x sblock.fs_postbloff\n", sblock.fs_postbloff); 5774 printf("0x%x sblock.fs_rotbloff\n", sblock.fs_rotbloff); 5775 printf("0x%x sblock.fs_magic\n", sblock.fs_magic); 5776 5777 /* 5778 * fs_space isn't of much use in this context, so we'll 5779 * just ignore it for now. 5780 */ 5781 } 5782 } 5783