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