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