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 39 #ifndef _KERNEL 40 #include <stdio.h> 41 #include <string.h> 42 #include <stdlib.h> 43 #include <time.h> 44 #include <sys/errno.h> 45 #include <ufs/ufs/dinode.h> 46 #include <ufs/ffs/fs.h> 47 48 uint32_t calculate_crc32c(uint32_t, const void *, size_t); 49 uint32_t ffs_calc_sbhash(struct fs *); 50 struct malloc_type; 51 #define UFS_MALLOC(size, type, flags) malloc(size) 52 #define UFS_FREE(ptr, type) free(ptr) 53 54 #else /* _KERNEL */ 55 #include <sys/systm.h> 56 #include <sys/gsb_crc32.h> 57 #include <sys/lock.h> 58 #include <sys/malloc.h> 59 #include <sys/mount.h> 60 #include <sys/vnode.h> 61 #include <sys/bio.h> 62 #include <sys/buf.h> 63 #include <sys/ucred.h> 64 65 #include <ufs/ufs/quota.h> 66 #include <ufs/ufs/inode.h> 67 #include <ufs/ufs/extattr.h> 68 #include <ufs/ufs/ufsmount.h> 69 #include <ufs/ufs/ufs_extern.h> 70 #include <ufs/ffs/ffs_extern.h> 71 #include <ufs/ffs/fs.h> 72 73 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags) 74 #define UFS_FREE(ptr, type) free(ptr, type) 75 76 #endif /* _KERNEL */ 77 78 /* 79 * Verify an inode check-hash. 80 */ 81 int 82 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 83 { 84 uint32_t ckhash, save_ckhash; 85 86 /* 87 * Return success if unallocated or we are not doing inode check-hash. 88 */ 89 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 90 return (0); 91 /* 92 * Exclude di_ckhash from the crc32 calculation, e.g., always use 93 * a check-hash value of zero when calculating the check-hash. 94 */ 95 save_ckhash = dip->di_ckhash; 96 dip->di_ckhash = 0; 97 ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 98 dip->di_ckhash = save_ckhash; 99 if (save_ckhash == ckhash) 100 return (0); 101 return (EINVAL); 102 } 103 104 /* 105 * Update an inode check-hash. 106 */ 107 void 108 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 109 { 110 111 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 112 return; 113 /* 114 * Exclude old di_ckhash from the crc32 calculation, e.g., always use 115 * a check-hash value of zero when calculating the new check-hash. 116 */ 117 dip->di_ckhash = 0; 118 dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 119 } 120 121 /* 122 * These are the low-level functions that actually read and write 123 * the superblock and its associated data. 124 */ 125 static off_t sblock_try[] = SBLOCKSEARCH; 126 static int readsuper(void *, struct fs **, off_t, int, int, 127 int (*)(void *, off_t, void **, int)); 128 129 /* 130 * Read a superblock from the devfd device. 131 * 132 * If an alternate superblock is specified, it is read. Otherwise the 133 * set of locations given in the SBLOCKSEARCH list is searched for a 134 * superblock. Memory is allocated for the superblock by the readfunc and 135 * is returned. If filltype is non-NULL, additional memory is allocated 136 * of type filltype and filled in with the superblock summary information. 137 * All memory is freed when any error is returned. 138 * 139 * If a superblock is found, zero is returned. Otherwise one of the 140 * following error values is returned: 141 * EIO: non-existent or truncated superblock. 142 * EIO: error reading summary information. 143 * ENOENT: no usable known superblock found. 144 * ENOSPC: failed to allocate space for the superblock. 145 * EINVAL: The previous newfs operation on this volume did not complete. 146 * The administrator must complete newfs before using this volume. 147 */ 148 int 149 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock, 150 struct malloc_type *filltype, 151 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 152 { 153 struct fs *fs; 154 struct fs_summary_info *fs_si; 155 int i, error, size, blks; 156 uint8_t *space; 157 int32_t *lp; 158 char *buf; 159 160 fs = NULL; 161 *fsp = NULL; 162 if (altsblock >= 0) { 163 if ((error = readsuper(devfd, &fs, altsblock, 1, 0, 164 readfunc)) != 0) { 165 if (fs != NULL) 166 UFS_FREE(fs, filltype); 167 return (error); 168 } 169 } else { 170 for (i = 0; sblock_try[i] != -1; i++) { 171 if ((error = readsuper(devfd, &fs, sblock_try[i], 0, 172 altsblock, readfunc)) == 0) 173 break; 174 if (fs != NULL) { 175 UFS_FREE(fs, filltype); 176 fs = NULL; 177 } 178 if (error == ENOENT) 179 continue; 180 return (error); 181 } 182 if (sblock_try[i] == -1) 183 return (ENOENT); 184 } 185 /* 186 * Read in the superblock summary information. 187 */ 188 size = fs->fs_cssize; 189 blks = howmany(size, fs->fs_fsize); 190 if (fs->fs_contigsumsize > 0) 191 size += fs->fs_ncg * sizeof(int32_t); 192 size += fs->fs_ncg * sizeof(u_int8_t); 193 /* When running in libufs or libsa, UFS_MALLOC may fail */ 194 if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_WAITOK)) == NULL) { 195 UFS_FREE(fs, filltype); 196 return (ENOSPC); 197 } 198 bzero(fs_si, sizeof(*fs_si)); 199 fs->fs_si = fs_si; 200 if ((space = UFS_MALLOC(size, filltype, M_WAITOK)) == NULL) { 201 UFS_FREE(fs->fs_si, filltype); 202 UFS_FREE(fs, filltype); 203 return (ENOSPC); 204 } 205 fs->fs_csp = (struct csum *)space; 206 for (i = 0; i < blks; i += fs->fs_frag) { 207 size = fs->fs_bsize; 208 if (i + fs->fs_frag > blks) 209 size = (blks - i) * fs->fs_fsize; 210 buf = NULL; 211 error = (*readfunc)(devfd, 212 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size); 213 if (error) { 214 if (buf != NULL) 215 UFS_FREE(buf, filltype); 216 UFS_FREE(fs->fs_csp, filltype); 217 UFS_FREE(fs->fs_si, filltype); 218 UFS_FREE(fs, filltype); 219 return (error); 220 } 221 memcpy(space, buf, size); 222 UFS_FREE(buf, filltype); 223 space += size; 224 } 225 if (fs->fs_contigsumsize > 0) { 226 fs->fs_maxcluster = lp = (int32_t *)space; 227 for (i = 0; i < fs->fs_ncg; i++) 228 *lp++ = fs->fs_contigsumsize; 229 space = (uint8_t *)lp; 230 } 231 size = fs->fs_ncg * sizeof(u_int8_t); 232 fs->fs_contigdirs = (u_int8_t *)space; 233 bzero(fs->fs_contigdirs, size); 234 *fsp = fs; 235 return (0); 236 } 237 238 /* 239 * Try to read a superblock from the location specified by sblockloc. 240 * Return zero on success or an errno on failure. 241 */ 242 static int 243 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk, 244 int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 245 { 246 struct fs *fs; 247 int error, res; 248 uint32_t ckhash; 249 250 error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE); 251 if (error != 0) 252 return (error); 253 fs = *fsp; 254 if (fs->fs_magic == FS_BAD_MAGIC) 255 return (EINVAL); 256 if (!(((fs->fs_magic == FS_UFS1_MAGIC && (isaltsblk || 257 sblockloc <= SBLOCK_UFS1)) || 258 (fs->fs_magic == FS_UFS2_MAGIC && (isaltsblk || 259 sblockloc == fs->fs_sblockloc))) && 260 fs->fs_ncg >= 1 && 261 fs->fs_bsize >= MINBSIZE && 262 fs->fs_bsize <= MAXBSIZE && 263 fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE) && 264 fs->fs_sbsize <= SBLOCKSIZE)) 265 return (ENOENT); 266 /* 267 * If the filesystem has been run on a kernel without 268 * metadata check hashes, disable them. 269 */ 270 if ((fs->fs_flags & FS_METACKHASH) == 0) 271 fs->fs_metackhash = 0; 272 /* 273 * Clear any check-hashes that are not maintained 274 * by this kernel. Also clear any unsupported flags. 275 */ 276 fs->fs_metackhash &= CK_SUPPORTED; 277 fs->fs_flags &= FS_SUPPORTED; 278 if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) { 279 if (chkhash == STDSB_NOMSG) 280 return (EINTEGRITY); 281 if (chkhash == STDSB_NOHASHFAIL_NOMSG) 282 return (0); 283 #ifdef _KERNEL 284 res = uprintf("Superblock check-hash failed: recorded " 285 "check-hash 0x%x != computed check-hash 0x%x%s\n", 286 fs->fs_ckhash, ckhash, 287 chkhash == STDSB_NOHASHFAIL ? " (Ignored)" : ""); 288 #else 289 res = 0; 290 #endif 291 /* 292 * Print check-hash failure if no controlling terminal 293 * in kernel or always if in user-mode (libufs). 294 */ 295 if (res == 0) 296 printf("Superblock check-hash failed: recorded " 297 "check-hash 0x%x != computed check-hash " 298 "0x%x%s\n", fs->fs_ckhash, ckhash, 299 chkhash == STDSB_NOHASHFAIL ? 300 " (Ignored)" : ""); 301 if (chkhash == STDSB) 302 return (EINTEGRITY); 303 /* chkhash == STDSB_NOHASHFAIL */ 304 return (0); 305 } 306 /* Have to set for old filesystems that predate this field */ 307 fs->fs_sblockactualloc = sblockloc; 308 /* Not yet any summary information */ 309 fs->fs_si = NULL; 310 return (0); 311 } 312 313 /* 314 * Write a superblock to the devfd device from the memory pointed to by fs. 315 * Write out the superblock summary information if it is present. 316 * 317 * If the write is successful, zero is returned. Otherwise one of the 318 * following error values is returned: 319 * EIO: failed to write superblock. 320 * EIO: failed to write superblock summary information. 321 */ 322 int 323 ffs_sbput(void *devfd, struct fs *fs, off_t loc, 324 int (*writefunc)(void *devfd, off_t loc, void *buf, int size)) 325 { 326 int i, error, blks, size; 327 uint8_t *space; 328 329 /* 330 * If there is summary information, write it first, so if there 331 * is an error, the superblock will not be marked as clean. 332 */ 333 if (fs->fs_si != NULL && fs->fs_csp != NULL) { 334 blks = howmany(fs->fs_cssize, fs->fs_fsize); 335 space = (uint8_t *)fs->fs_csp; 336 for (i = 0; i < blks; i += fs->fs_frag) { 337 size = fs->fs_bsize; 338 if (i + fs->fs_frag > blks) 339 size = (blks - i) * fs->fs_fsize; 340 if ((error = (*writefunc)(devfd, 341 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), 342 space, size)) != 0) 343 return (error); 344 space += size; 345 } 346 } 347 fs->fs_fmod = 0; 348 #ifndef _KERNEL 349 { 350 struct fs_summary_info *fs_si; 351 352 fs->fs_time = time(NULL); 353 /* Clear the pointers for the duration of writing. */ 354 fs_si = fs->fs_si; 355 fs->fs_si = NULL; 356 fs->fs_ckhash = ffs_calc_sbhash(fs); 357 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 358 fs->fs_si = fs_si; 359 } 360 #else /* _KERNEL */ 361 fs->fs_time = time_second; 362 fs->fs_ckhash = ffs_calc_sbhash(fs); 363 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 364 #endif /* _KERNEL */ 365 return (error); 366 } 367 368 /* 369 * Calculate the check-hash for a superblock. 370 */ 371 uint32_t 372 ffs_calc_sbhash(struct fs *fs) 373 { 374 uint32_t ckhash, save_ckhash; 375 376 /* 377 * A filesystem that was using a superblock ckhash may be moved 378 * to an older kernel that does not support ckhashes. The 379 * older kernel will clear the FS_METACKHASH flag indicating 380 * that it does not update hashes. When the disk is moved back 381 * to a kernel capable of ckhashes it disables them on mount: 382 * 383 * if ((fs->fs_flags & FS_METACKHASH) == 0) 384 * fs->fs_metackhash = 0; 385 * 386 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an 387 * old stale value in the fs->fs_ckhash field. Thus the need to 388 * just accept what is there. 389 */ 390 if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0) 391 return (fs->fs_ckhash); 392 393 save_ckhash = fs->fs_ckhash; 394 fs->fs_ckhash = 0; 395 /* 396 * If newly read from disk, the caller is responsible for 397 * verifying that fs->fs_sbsize <= SBLOCKSIZE. 398 */ 399 ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize); 400 fs->fs_ckhash = save_ckhash; 401 return (ckhash); 402 } 403 404 /* 405 * Update the frsum fields to reflect addition or deletion 406 * of some frags. 407 */ 408 void 409 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 410 { 411 int inblk; 412 int field, subfield; 413 int siz, pos; 414 415 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 416 fragmap <<= 1; 417 for (siz = 1; siz < fs->fs_frag; siz++) { 418 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 419 continue; 420 field = around[siz]; 421 subfield = inside[siz]; 422 for (pos = siz; pos <= fs->fs_frag; pos++) { 423 if ((fragmap & field) == subfield) { 424 fraglist[siz] += cnt; 425 pos += siz; 426 field <<= siz; 427 subfield <<= siz; 428 } 429 field <<= 1; 430 subfield <<= 1; 431 } 432 } 433 } 434 435 /* 436 * block operations 437 * 438 * check if a block is available 439 */ 440 int 441 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 442 { 443 unsigned char mask; 444 445 switch ((int)fs->fs_frag) { 446 case 8: 447 return (cp[h] == 0xff); 448 case 4: 449 mask = 0x0f << ((h & 0x1) << 2); 450 return ((cp[h >> 1] & mask) == mask); 451 case 2: 452 mask = 0x03 << ((h & 0x3) << 1); 453 return ((cp[h >> 2] & mask) == mask); 454 case 1: 455 mask = 0x01 << (h & 0x7); 456 return ((cp[h >> 3] & mask) == mask); 457 default: 458 #ifdef _KERNEL 459 panic("ffs_isblock"); 460 #endif 461 break; 462 } 463 return (0); 464 } 465 466 /* 467 * check if a block is free 468 */ 469 int 470 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 471 { 472 473 switch ((int)fs->fs_frag) { 474 case 8: 475 return (cp[h] == 0); 476 case 4: 477 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 478 case 2: 479 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 480 case 1: 481 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 482 default: 483 #ifdef _KERNEL 484 panic("ffs_isfreeblock"); 485 #endif 486 break; 487 } 488 return (0); 489 } 490 491 /* 492 * take a block out of the map 493 */ 494 void 495 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 496 { 497 498 switch ((int)fs->fs_frag) { 499 case 8: 500 cp[h] = 0; 501 return; 502 case 4: 503 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 504 return; 505 case 2: 506 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 507 return; 508 case 1: 509 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 510 return; 511 default: 512 #ifdef _KERNEL 513 panic("ffs_clrblock"); 514 #endif 515 break; 516 } 517 } 518 519 /* 520 * put a block into the map 521 */ 522 void 523 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 524 { 525 526 switch ((int)fs->fs_frag) { 527 case 8: 528 cp[h] = 0xff; 529 return; 530 case 4: 531 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 532 return; 533 case 2: 534 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 535 return; 536 case 1: 537 cp[h >> 3] |= (0x01 << (h & 0x7)); 538 return; 539 default: 540 #ifdef _KERNEL 541 panic("ffs_setblock"); 542 #endif 543 break; 544 } 545 } 546 547 /* 548 * Update the cluster map because of an allocation or free. 549 * 550 * Cnt == 1 means free; cnt == -1 means allocating. 551 */ 552 void 553 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt) 554 { 555 int32_t *sump; 556 int32_t *lp; 557 u_char *freemapp, *mapp; 558 int i, start, end, forw, back, map; 559 u_int bit; 560 561 if (fs->fs_contigsumsize <= 0) 562 return; 563 freemapp = cg_clustersfree(cgp); 564 sump = cg_clustersum(cgp); 565 /* 566 * Allocate or clear the actual block. 567 */ 568 if (cnt > 0) 569 setbit(freemapp, blkno); 570 else 571 clrbit(freemapp, blkno); 572 /* 573 * Find the size of the cluster going forward. 574 */ 575 start = blkno + 1; 576 end = start + fs->fs_contigsumsize; 577 if (end >= cgp->cg_nclusterblks) 578 end = cgp->cg_nclusterblks; 579 mapp = &freemapp[start / NBBY]; 580 map = *mapp++; 581 bit = 1U << (start % NBBY); 582 for (i = start; i < end; i++) { 583 if ((map & bit) == 0) 584 break; 585 if ((i & (NBBY - 1)) != (NBBY - 1)) { 586 bit <<= 1; 587 } else { 588 map = *mapp++; 589 bit = 1; 590 } 591 } 592 forw = i - start; 593 /* 594 * Find the size of the cluster going backward. 595 */ 596 start = blkno - 1; 597 end = start - fs->fs_contigsumsize; 598 if (end < 0) 599 end = -1; 600 mapp = &freemapp[start / NBBY]; 601 map = *mapp--; 602 bit = 1U << (start % NBBY); 603 for (i = start; i > end; i--) { 604 if ((map & bit) == 0) 605 break; 606 if ((i & (NBBY - 1)) != 0) { 607 bit >>= 1; 608 } else { 609 map = *mapp--; 610 bit = 1U << (NBBY - 1); 611 } 612 } 613 back = start - i; 614 /* 615 * Account for old cluster and the possibly new forward and 616 * back clusters. 617 */ 618 i = back + forw + 1; 619 if (i > fs->fs_contigsumsize) 620 i = fs->fs_contigsumsize; 621 sump[i] += cnt; 622 if (back > 0) 623 sump[back] -= cnt; 624 if (forw > 0) 625 sump[forw] -= cnt; 626 /* 627 * Update cluster summary information. 628 */ 629 lp = &sump[fs->fs_contigsumsize]; 630 for (i = fs->fs_contigsumsize; i > 0; i--) 631 if (*lp-- > 0) 632 break; 633 fs->fs_maxcluster[cgp->cg_cgx] = i; 634 } 635