1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/limits.h> 39 40 #ifndef _KERNEL 41 #include <stdio.h> 42 #include <string.h> 43 #include <stdlib.h> 44 #include <time.h> 45 #include <sys/errno.h> 46 #include <ufs/ufs/dinode.h> 47 #include <ufs/ffs/fs.h> 48 49 uint32_t calculate_crc32c(uint32_t, const void *, size_t); 50 uint32_t ffs_calc_sbhash(struct fs *); 51 struct malloc_type; 52 #define UFS_MALLOC(size, type, flags) malloc(size) 53 #define UFS_FREE(ptr, type) free(ptr) 54 #define maxphys MAXPHYS 55 56 #else /* _KERNEL */ 57 #include <sys/systm.h> 58 #include <sys/gsb_crc32.h> 59 #include <sys/lock.h> 60 #include <sys/malloc.h> 61 #include <sys/mount.h> 62 #include <sys/vnode.h> 63 #include <sys/bio.h> 64 #include <sys/buf.h> 65 #include <sys/ucred.h> 66 67 #include <ufs/ufs/quota.h> 68 #include <ufs/ufs/inode.h> 69 #include <ufs/ufs/extattr.h> 70 #include <ufs/ufs/ufsmount.h> 71 #include <ufs/ufs/ufs_extern.h> 72 #include <ufs/ffs/ffs_extern.h> 73 #include <ufs/ffs/fs.h> 74 75 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags) 76 #define UFS_FREE(ptr, type) free(ptr, type) 77 78 #endif /* _KERNEL */ 79 80 /* 81 * Verify an inode check-hash. 82 */ 83 int 84 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 85 { 86 uint32_t ckhash, save_ckhash; 87 88 /* 89 * Return success if unallocated or we are not doing inode check-hash. 90 */ 91 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 92 return (0); 93 /* 94 * Exclude di_ckhash from the crc32 calculation, e.g., always use 95 * a check-hash value of zero when calculating the check-hash. 96 */ 97 save_ckhash = dip->di_ckhash; 98 dip->di_ckhash = 0; 99 ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 100 dip->di_ckhash = save_ckhash; 101 if (save_ckhash == ckhash) 102 return (0); 103 return (EINVAL); 104 } 105 106 /* 107 * Update an inode check-hash. 108 */ 109 void 110 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 111 { 112 113 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 114 return; 115 /* 116 * Exclude old di_ckhash from the crc32 calculation, e.g., always use 117 * a check-hash value of zero when calculating the new check-hash. 118 */ 119 dip->di_ckhash = 0; 120 dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 121 } 122 123 /* 124 * These are the low-level functions that actually read and write 125 * the superblock and its associated data. 126 */ 127 static off_t sblock_try[] = SBLOCKSEARCH; 128 static int readsuper(void *, struct fs **, off_t, int, int, 129 int (*)(void *, off_t, void **, int)); 130 static int validate_sblock(struct fs *, int); 131 132 /* 133 * Read a superblock from the devfd device. 134 * 135 * If an alternate superblock is specified, it is read. Otherwise the 136 * set of locations given in the SBLOCKSEARCH list is searched for a 137 * superblock. Memory is allocated for the superblock by the readfunc and 138 * is returned. If filltype is non-NULL, additional memory is allocated 139 * of type filltype and filled in with the superblock summary information. 140 * All memory is freed when any error is returned. 141 * 142 * If a superblock is found, zero is returned. Otherwise one of the 143 * following error values is returned: 144 * EIO: non-existent or truncated superblock. 145 * EIO: error reading summary information. 146 * ENOENT: no usable known superblock found. 147 * ENOMEM: failed to allocate space for the superblock. 148 * EINVAL: The previous newfs operation on this volume did not complete. 149 * The administrator must complete newfs before using this volume. 150 */ 151 int 152 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock, 153 struct malloc_type *filltype, 154 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 155 { 156 struct fs *fs; 157 struct fs_summary_info *fs_si; 158 int i, error; 159 uint64_t size, blks; 160 uint8_t *space; 161 int32_t *lp; 162 char *buf; 163 164 fs = NULL; 165 *fsp = NULL; 166 if (altsblock >= 0) { 167 if ((error = readsuper(devfd, &fs, altsblock, 1, 0, 168 readfunc)) != 0) { 169 if (fs != NULL) 170 UFS_FREE(fs, filltype); 171 return (error); 172 } 173 } else { 174 for (i = 0; sblock_try[i] != -1; i++) { 175 if ((error = readsuper(devfd, &fs, sblock_try[i], 0, 176 altsblock, readfunc)) == 0) 177 break; 178 if (fs != NULL) { 179 UFS_FREE(fs, filltype); 180 fs = NULL; 181 } 182 if (error == ENOENT) 183 continue; 184 return (error); 185 } 186 if (sblock_try[i] == -1) 187 return (ENOENT); 188 } 189 /* 190 * Read in the superblock summary information. 191 */ 192 size = fs->fs_cssize; 193 blks = howmany(size, fs->fs_fsize); 194 if (fs->fs_contigsumsize > 0) 195 size += fs->fs_ncg * sizeof(int32_t); 196 size += fs->fs_ncg * sizeof(u_int8_t); 197 if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) { 198 UFS_FREE(fs, filltype); 199 return (ENOMEM); 200 } 201 bzero(fs_si, sizeof(*fs_si)); 202 fs->fs_si = fs_si; 203 if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) { 204 UFS_FREE(fs->fs_si, filltype); 205 UFS_FREE(fs, filltype); 206 return (ENOMEM); 207 } 208 fs->fs_csp = (struct csum *)space; 209 for (i = 0; i < blks; i += fs->fs_frag) { 210 size = fs->fs_bsize; 211 if (i + fs->fs_frag > blks) 212 size = (blks - i) * fs->fs_fsize; 213 buf = NULL; 214 error = (*readfunc)(devfd, 215 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size); 216 if (error) { 217 if (buf != NULL) 218 UFS_FREE(buf, filltype); 219 UFS_FREE(fs->fs_csp, filltype); 220 UFS_FREE(fs->fs_si, filltype); 221 UFS_FREE(fs, filltype); 222 return (error); 223 } 224 memcpy(space, buf, size); 225 UFS_FREE(buf, filltype); 226 space += size; 227 } 228 if (fs->fs_contigsumsize > 0) { 229 fs->fs_maxcluster = lp = (int32_t *)space; 230 for (i = 0; i < fs->fs_ncg; i++) 231 *lp++ = fs->fs_contigsumsize; 232 space = (uint8_t *)lp; 233 } 234 size = fs->fs_ncg * sizeof(u_int8_t); 235 fs->fs_contigdirs = (u_int8_t *)space; 236 bzero(fs->fs_contigdirs, size); 237 *fsp = fs; 238 return (0); 239 } 240 241 /* 242 * Try to read a superblock from the location specified by sblockloc. 243 * Return zero on success or an errno on failure. 244 */ 245 static int 246 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk, 247 int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 248 { 249 struct fs *fs; 250 int error, res; 251 uint32_t ckhash; 252 253 error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE); 254 if (error != 0) 255 return (error); 256 fs = *fsp; 257 if (fs->fs_magic == FS_BAD_MAGIC) 258 return (EINVAL); 259 /* 260 * For UFS1 with a 65536 block size, the first backup superblock 261 * is at the same location as the UFS2 superblock. Since SBLOCK_UFS2 262 * is the first location checked, the first backup is the superblock 263 * that will be accessed. Here we fail the lookup so that we can 264 * retry with the correct location for the UFS1 superblock. 265 */ 266 if (fs->fs_magic == FS_UFS1_MAGIC && !isaltsblk && 267 fs->fs_bsize == SBLOCK_UFS2 && sblockloc == SBLOCK_UFS2) 268 return (ENOENT); 269 if ((error = validate_sblock(fs, isaltsblk)) != 0) 270 return (error); 271 /* 272 * If the filesystem has been run on a kernel without 273 * metadata check hashes, disable them. 274 */ 275 if ((fs->fs_flags & FS_METACKHASH) == 0) 276 fs->fs_metackhash = 0; 277 /* 278 * Clear any check-hashes that are not maintained 279 * by this kernel. Also clear any unsupported flags. 280 */ 281 fs->fs_metackhash &= CK_SUPPORTED; 282 fs->fs_flags &= FS_SUPPORTED; 283 if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) { 284 if (chkhash == STDSB_NOMSG) 285 return (EINTEGRITY); 286 if (chkhash == STDSB_NOHASHFAIL_NOMSG) 287 return (0); 288 #ifdef _KERNEL 289 res = uprintf("Superblock check-hash failed: recorded " 290 "check-hash 0x%x != computed check-hash 0x%x%s\n", 291 fs->fs_ckhash, ckhash, 292 chkhash == STDSB_NOHASHFAIL ? " (Ignored)" : ""); 293 #else 294 res = 0; 295 #endif 296 /* 297 * Print check-hash failure if no controlling terminal 298 * in kernel or always if in user-mode (libufs). 299 */ 300 if (res == 0) 301 printf("Superblock check-hash failed: recorded " 302 "check-hash 0x%x != computed check-hash " 303 "0x%x%s\n", fs->fs_ckhash, ckhash, 304 chkhash == STDSB_NOHASHFAIL ? 305 " (Ignored)" : ""); 306 if (chkhash == STDSB) 307 return (EINTEGRITY); 308 /* chkhash == STDSB_NOHASHFAIL */ 309 return (0); 310 } 311 /* Have to set for old filesystems that predate this field */ 312 fs->fs_sblockactualloc = sblockloc; 313 /* Not yet any summary information */ 314 fs->fs_si = NULL; 315 return (0); 316 } 317 318 /* 319 * Verify the filesystem values. 320 */ 321 #define ILOG2(num) (fls(num) - 1) 322 #undef CHK 323 #define CHK(lhs, op, rhs, fmt) \ 324 if (lhs op rhs) { \ 325 printf("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 326 #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, \ 327 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs); \ 328 return (ENOENT); \ 329 } 330 #define CHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt) \ 331 if (lhs1 op1 rhs1 && lhs2 op2 rhs2) { \ 332 printf("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 333 #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n", \ 334 fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, \ 335 (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2, \ 336 (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2); \ 337 return (ENOENT); \ 338 } 339 340 static int 341 validate_sblock(struct fs *fs, int isaltsblk) 342 { 343 u_long i, sectorsize, cgnum; 344 u_int64_t maxfilesize, sizepb; 345 346 sectorsize = dbtob(1); 347 if (fs->fs_magic == FS_UFS2_MAGIC) { 348 if (!isaltsblk) { 349 CHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx); 350 CHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2, 351 fs->fs_sblockactualloc, !=, 0, %jd); 352 } 353 CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 354 sizeof(ufs2_daddr_t)), %jd); 355 CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t), 356 %jd); 357 CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs2_dinode), 358 %jd); 359 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 360 if (!isaltsblk) { 361 CHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd); 362 CHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd); 363 } 364 CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t), 365 %jd); 366 CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs1_dinode), 367 %jd); 368 CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 369 sizeof(ufs1_daddr_t)), %jd); 370 CHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd); 371 CHK(fs->fs_old_cgoffset, !=, 0, %jd); 372 CHK(fs->fs_old_cgmask, !=, 0xffffffff, %#jx); 373 CHK(fs->fs_old_rotdelay, !=, 0, %jd); 374 CHK(fs->fs_old_rps, !=, 60, %jd); 375 CHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd); 376 CHK(fs->fs_old_cpg, !=, 1, %jd); 377 CHK(fs->fs_old_interleave, !=, 1, %jd); 378 CHK(fs->fs_old_trackskew, !=, 0, %jd); 379 CHK(fs->fs_old_cpc, !=, 0, %jd); 380 CHK(fs->fs_old_postblformat, !=, 1, %jd); 381 CHK(fs->fs_old_nrpos, !=, 1, %jd); 382 CHK(fs->fs_old_spc, !=, fs->fs_fpg * fs->fs_old_nspf, %jd); 383 CHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd); 384 CHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd); 385 CHK(fs->fs_old_ncyl, !=, fs->fs_ncg, %jd); 386 } else { 387 /* Bad magic number, so assume not a superblock */ 388 return (ENOENT); 389 } 390 CHK(fs->fs_bsize, <, MINBSIZE, %jd); 391 CHK(fs->fs_bsize, >, MAXBSIZE, %jd); 392 CHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd); 393 CHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd); 394 CHK(powerof2(fs->fs_bsize), ==, 0, %jd); 395 CHK(fs->fs_fsize, <, sectorsize, %jd); 396 CHK(fs->fs_fsize, >, fs->fs_bsize, %jd); 397 CHK(fs->fs_fsize * MAXFRAG, <, fs->fs_bsize, %jd); 398 CHK(powerof2(fs->fs_fsize), ==, 0, %jd); 399 CHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd); 400 CHK(powerof2(fs->fs_maxbsize), ==, 0, %jd); 401 CHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd); 402 CHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx); 403 CHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx); 404 CHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx); 405 CHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx); 406 CHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd); 407 CHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd); 408 CHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd); 409 CHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd); 410 CHK(fs->fs_frag, >, MAXFRAG, %jd); 411 CHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd); 412 CHK(fs->fs_sblkno, !=, roundup( 413 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 414 fs->fs_frag), %jd); 415 CHK(fs->fs_cblkno, !=, fs->fs_sblkno + 416 roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd); 417 CHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd); 418 CHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd); 419 CHK(fs->fs_cgsize, >, fs->fs_bsize, %jd); 420 CHK(fs->fs_cssize, !=, 421 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 422 /* 423 * This test is valid, however older versions of growfs failed 424 * to correctly update fs_dsize so will fail this test. Thus we 425 * exclude it from the requirements. 426 */ 427 #ifdef notdef 428 CHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno - 429 fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) - 430 howmany(fs->fs_cssize, fs->fs_fsize), %jd); 431 #endif 432 CHK(fs->fs_metaspace, <, 0, %jd); 433 CHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd); 434 CHK(fs->fs_minfree, >, 99, %jd%%); 435 maxfilesize = fs->fs_bsize * UFS_NDADDR - 1; 436 for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) { 437 sizepb *= NINDIR(fs); 438 maxfilesize += sizepb; 439 } 440 CHK(fs->fs_maxfilesize, !=, maxfilesize, %jd); 441 /* 442 * These values have a tight interaction with each other that 443 * makes it hard to tightly bound them. So we can only check 444 * that they are within a broader possible range. 445 * 446 * The size cannot always be accurately determined, but ensure 447 * that it is consistent with the number of cylinder groups (fs_ncg) 448 * and the number of fragments per cylinder group (fs_fpg). Ensure 449 * that the summary information size is correct and that it starts 450 * and ends in the data area of the same cylinder group. 451 */ 452 CHK(fs->fs_ncg, <, 1, %jd); 453 CHK(fs->fs_size, <, 8 * fs->fs_frag, %jd); 454 CHK(fs->fs_size, <=, (fs->fs_ncg - 1) * fs->fs_fpg, %jd); 455 CHK(fs->fs_size, >, fs->fs_ncg * fs->fs_fpg, %jd); 456 CHK(fs->fs_cssize, !=, 457 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 458 CHK(dtog(fs, fs->fs_csaddr), >, fs->fs_ncg, %jd); 459 cgnum = dtog(fs, fs->fs_csaddr); 460 CHK(fs->fs_csaddr, <, cgdmin(fs, cgnum), %jd); 461 CHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >, 462 cgnum, %jd); 463 CHK(fs->fs_ipg * fs->fs_ncg, >, (((int64_t)(1)) << 32) - INOPB(fs), 464 %jd); 465 /* 466 * With file system clustering it is possible to allocate 467 * many contiguous blocks. The kernel variable maxphys defines 468 * the maximum transfer size permitted by the controller and/or 469 * buffering. The fs_maxcontig parameter controls the maximum 470 * number of blocks that the filesystem will read or write 471 * in a single transfer. It is calculated when the filesystem 472 * is created as maxphys / fs_bsize. The loader uses a maxphys 473 * of 128K even when running on a system that supports larger 474 * values. If the filesystem was built on a system that supports 475 * a larger maxphys (1M is typical) it will have configured 476 * fs_maxcontig for that larger system. So we bound the upper 477 * allowable limit for fs_maxconfig to be able to at least 478 * work with a 1M maxphys on the smallest block size filesystem: 479 * 1M / 4096 == 256. There is no harm in allowing the mounting of 480 * filesystems that make larger than maxphys I/O requests because 481 * those (mostly 32-bit machines) can (very slowly) handle I/O 482 * requests that exceed maxphys. 483 */ 484 CHK(fs->fs_maxcontig, <, 0, %jd); 485 CHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd); 486 CHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd); 487 CHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=, 488 MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd); 489 return (0); 490 } 491 492 /* 493 * Write a superblock to the devfd device from the memory pointed to by fs. 494 * Write out the superblock summary information if it is present. 495 * 496 * If the write is successful, zero is returned. Otherwise one of the 497 * following error values is returned: 498 * EIO: failed to write superblock. 499 * EIO: failed to write superblock summary information. 500 */ 501 int 502 ffs_sbput(void *devfd, struct fs *fs, off_t loc, 503 int (*writefunc)(void *devfd, off_t loc, void *buf, int size)) 504 { 505 int i, error, blks, size; 506 uint8_t *space; 507 508 /* 509 * If there is summary information, write it first, so if there 510 * is an error, the superblock will not be marked as clean. 511 */ 512 if (fs->fs_si != NULL && fs->fs_csp != NULL) { 513 blks = howmany(fs->fs_cssize, fs->fs_fsize); 514 space = (uint8_t *)fs->fs_csp; 515 for (i = 0; i < blks; i += fs->fs_frag) { 516 size = fs->fs_bsize; 517 if (i + fs->fs_frag > blks) 518 size = (blks - i) * fs->fs_fsize; 519 if ((error = (*writefunc)(devfd, 520 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), 521 space, size)) != 0) 522 return (error); 523 space += size; 524 } 525 } 526 fs->fs_fmod = 0; 527 #ifndef _KERNEL 528 { 529 struct fs_summary_info *fs_si; 530 531 fs->fs_time = time(NULL); 532 /* Clear the pointers for the duration of writing. */ 533 fs_si = fs->fs_si; 534 fs->fs_si = NULL; 535 fs->fs_ckhash = ffs_calc_sbhash(fs); 536 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 537 fs->fs_si = fs_si; 538 } 539 #else /* _KERNEL */ 540 fs->fs_time = time_second; 541 fs->fs_ckhash = ffs_calc_sbhash(fs); 542 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 543 #endif /* _KERNEL */ 544 return (error); 545 } 546 547 /* 548 * Calculate the check-hash for a superblock. 549 */ 550 uint32_t 551 ffs_calc_sbhash(struct fs *fs) 552 { 553 uint32_t ckhash, save_ckhash; 554 555 /* 556 * A filesystem that was using a superblock ckhash may be moved 557 * to an older kernel that does not support ckhashes. The 558 * older kernel will clear the FS_METACKHASH flag indicating 559 * that it does not update hashes. When the disk is moved back 560 * to a kernel capable of ckhashes it disables them on mount: 561 * 562 * if ((fs->fs_flags & FS_METACKHASH) == 0) 563 * fs->fs_metackhash = 0; 564 * 565 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an 566 * old stale value in the fs->fs_ckhash field. Thus the need to 567 * just accept what is there. 568 */ 569 if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0) 570 return (fs->fs_ckhash); 571 572 save_ckhash = fs->fs_ckhash; 573 fs->fs_ckhash = 0; 574 /* 575 * If newly read from disk, the caller is responsible for 576 * verifying that fs->fs_sbsize <= SBLOCKSIZE. 577 */ 578 ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize); 579 fs->fs_ckhash = save_ckhash; 580 return (ckhash); 581 } 582 583 /* 584 * Update the frsum fields to reflect addition or deletion 585 * of some frags. 586 */ 587 void 588 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 589 { 590 int inblk; 591 int field, subfield; 592 int siz, pos; 593 594 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 595 fragmap <<= 1; 596 for (siz = 1; siz < fs->fs_frag; siz++) { 597 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 598 continue; 599 field = around[siz]; 600 subfield = inside[siz]; 601 for (pos = siz; pos <= fs->fs_frag; pos++) { 602 if ((fragmap & field) == subfield) { 603 fraglist[siz] += cnt; 604 pos += siz; 605 field <<= siz; 606 subfield <<= siz; 607 } 608 field <<= 1; 609 subfield <<= 1; 610 } 611 } 612 } 613 614 /* 615 * block operations 616 * 617 * check if a block is available 618 */ 619 int 620 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 621 { 622 unsigned char mask; 623 624 switch ((int)fs->fs_frag) { 625 case 8: 626 return (cp[h] == 0xff); 627 case 4: 628 mask = 0x0f << ((h & 0x1) << 2); 629 return ((cp[h >> 1] & mask) == mask); 630 case 2: 631 mask = 0x03 << ((h & 0x3) << 1); 632 return ((cp[h >> 2] & mask) == mask); 633 case 1: 634 mask = 0x01 << (h & 0x7); 635 return ((cp[h >> 3] & mask) == mask); 636 default: 637 #ifdef _KERNEL 638 panic("ffs_isblock"); 639 #endif 640 break; 641 } 642 return (0); 643 } 644 645 /* 646 * check if a block is free 647 */ 648 int 649 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 650 { 651 652 switch ((int)fs->fs_frag) { 653 case 8: 654 return (cp[h] == 0); 655 case 4: 656 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 657 case 2: 658 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 659 case 1: 660 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 661 default: 662 #ifdef _KERNEL 663 panic("ffs_isfreeblock"); 664 #endif 665 break; 666 } 667 return (0); 668 } 669 670 /* 671 * take a block out of the map 672 */ 673 void 674 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 675 { 676 677 switch ((int)fs->fs_frag) { 678 case 8: 679 cp[h] = 0; 680 return; 681 case 4: 682 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 683 return; 684 case 2: 685 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 686 return; 687 case 1: 688 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 689 return; 690 default: 691 #ifdef _KERNEL 692 panic("ffs_clrblock"); 693 #endif 694 break; 695 } 696 } 697 698 /* 699 * put a block into the map 700 */ 701 void 702 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 703 { 704 705 switch ((int)fs->fs_frag) { 706 case 8: 707 cp[h] = 0xff; 708 return; 709 case 4: 710 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 711 return; 712 case 2: 713 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 714 return; 715 case 1: 716 cp[h >> 3] |= (0x01 << (h & 0x7)); 717 return; 718 default: 719 #ifdef _KERNEL 720 panic("ffs_setblock"); 721 #endif 722 break; 723 } 724 } 725 726 /* 727 * Update the cluster map because of an allocation or free. 728 * 729 * Cnt == 1 means free; cnt == -1 means allocating. 730 */ 731 void 732 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt) 733 { 734 int32_t *sump; 735 int32_t *lp; 736 u_char *freemapp, *mapp; 737 int i, start, end, forw, back, map; 738 u_int bit; 739 740 if (fs->fs_contigsumsize <= 0) 741 return; 742 freemapp = cg_clustersfree(cgp); 743 sump = cg_clustersum(cgp); 744 /* 745 * Allocate or clear the actual block. 746 */ 747 if (cnt > 0) 748 setbit(freemapp, blkno); 749 else 750 clrbit(freemapp, blkno); 751 /* 752 * Find the size of the cluster going forward. 753 */ 754 start = blkno + 1; 755 end = start + fs->fs_contigsumsize; 756 if (end >= cgp->cg_nclusterblks) 757 end = cgp->cg_nclusterblks; 758 mapp = &freemapp[start / NBBY]; 759 map = *mapp++; 760 bit = 1U << (start % NBBY); 761 for (i = start; i < end; i++) { 762 if ((map & bit) == 0) 763 break; 764 if ((i & (NBBY - 1)) != (NBBY - 1)) { 765 bit <<= 1; 766 } else { 767 map = *mapp++; 768 bit = 1; 769 } 770 } 771 forw = i - start; 772 /* 773 * Find the size of the cluster going backward. 774 */ 775 start = blkno - 1; 776 end = start - fs->fs_contigsumsize; 777 if (end < 0) 778 end = -1; 779 mapp = &freemapp[start / NBBY]; 780 map = *mapp--; 781 bit = 1U << (start % NBBY); 782 for (i = start; i > end; i--) { 783 if ((map & bit) == 0) 784 break; 785 if ((i & (NBBY - 1)) != 0) { 786 bit >>= 1; 787 } else { 788 map = *mapp--; 789 bit = 1U << (NBBY - 1); 790 } 791 } 792 back = start - i; 793 /* 794 * Account for old cluster and the possibly new forward and 795 * back clusters. 796 */ 797 i = back + forw + 1; 798 if (i > fs->fs_contigsumsize) 799 i = fs->fs_contigsumsize; 800 sump[i] += cnt; 801 if (back > 0) 802 sump[back] -= cnt; 803 if (forw > 0) 804 sump[forw] -= cnt; 805 /* 806 * Update cluster summary information. 807 */ 808 lp = &sump[fs->fs_contigsumsize]; 809 for (i = fs->fs_contigsumsize; i > 0; i--) 810 if (*lp-- > 0) 811 break; 812 fs->fs_maxcluster[cgp->cg_cgx] = i; 813 } 814