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