160727d8bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1515c377c3SEd Maste * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31996c772fSJohn Dyson * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34f4636c59SDavid E. O'Brien #include <sys/cdefs.h> 35f4636c59SDavid E. O'Brien __FBSDID("$FreeBSD$"); 36f4636c59SDavid E. O'Brien 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38076002f2SKirk McKusick #include <sys/limits.h> 39df8bae1dSRodney W. Grimes 40c4473420SPeter Wemm #ifndef _KERNEL 41dffce215SKirk McKusick #include <stdio.h> 42dffce215SKirk McKusick #include <string.h> 43dffce215SKirk McKusick #include <stdlib.h> 44dffce215SKirk McKusick #include <time.h> 45dffce215SKirk McKusick #include <sys/errno.h> 46996c772fSJohn Dyson #include <ufs/ufs/dinode.h> 471c85e6a3SKirk McKusick #include <ufs/ffs/fs.h> 48dffce215SKirk McKusick 49ec888383SKirk McKusick uint32_t calculate_crc32c(uint32_t, const void *, size_t); 50ade67b50SKirk McKusick uint32_t ffs_calc_sbhash(struct fs *); 51dffce215SKirk McKusick struct malloc_type; 52dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size) 53dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr) 54076002f2SKirk McKusick #define maxphys MAXPHYS 55dffce215SKirk McKusick 56dffce215SKirk McKusick #else /* _KERNEL */ 57df8bae1dSRodney W. Grimes #include <sys/systm.h> 58f89d2072SXin LI #include <sys/gsb_crc32.h> 591cd52ec3SBruce Evans #include <sys/lock.h> 601c85e6a3SKirk McKusick #include <sys/malloc.h> 611c85e6a3SKirk McKusick #include <sys/mount.h> 62df8bae1dSRodney W. Grimes #include <sys/vnode.h> 639626b608SPoul-Henning Kamp #include <sys/bio.h> 64df8bae1dSRodney W. Grimes #include <sys/buf.h> 6508637435SBruce Evans #include <sys/ucred.h> 6608637435SBruce Evans 67df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h> 68df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h> 691c85e6a3SKirk McKusick #include <ufs/ufs/extattr.h> 701c85e6a3SKirk McKusick #include <ufs/ufs/ufsmount.h> 711c85e6a3SKirk McKusick #include <ufs/ufs/ufs_extern.h> 72996c772fSJohn Dyson #include <ufs/ffs/ffs_extern.h> 731c85e6a3SKirk McKusick #include <ufs/ffs/fs.h> 74df8bae1dSRodney W. Grimes 75dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size, type, flags) 76dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr, type) 77dffce215SKirk McKusick 78fb14e73cSKirk McKusick #endif /* _KERNEL */ 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes /* 818f829a5cSKirk McKusick * Verify an inode check-hash. 828f829a5cSKirk McKusick */ 838f829a5cSKirk McKusick int 848f829a5cSKirk McKusick ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 858f829a5cSKirk McKusick { 86c8f55fc4SKirk McKusick uint32_t ckhash, save_ckhash; 878f829a5cSKirk McKusick 888f829a5cSKirk McKusick /* 898f829a5cSKirk McKusick * Return success if unallocated or we are not doing inode check-hash. 908f829a5cSKirk McKusick */ 918f829a5cSKirk McKusick if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 928f829a5cSKirk McKusick return (0); 938f829a5cSKirk McKusick /* 948f829a5cSKirk McKusick * Exclude di_ckhash from the crc32 calculation, e.g., always use 958f829a5cSKirk McKusick * a check-hash value of zero when calculating the check-hash. 968f829a5cSKirk McKusick */ 978f829a5cSKirk McKusick save_ckhash = dip->di_ckhash; 988f829a5cSKirk McKusick dip->di_ckhash = 0; 99c8f55fc4SKirk McKusick ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 1008f829a5cSKirk McKusick dip->di_ckhash = save_ckhash; 101c8f55fc4SKirk McKusick if (save_ckhash == ckhash) 1028f829a5cSKirk McKusick return (0); 103c8f55fc4SKirk McKusick return (EINVAL); 1048f829a5cSKirk McKusick } 1058f829a5cSKirk McKusick 1068f829a5cSKirk McKusick /* 1078f829a5cSKirk McKusick * Update an inode check-hash. 1088f829a5cSKirk McKusick */ 1098f829a5cSKirk McKusick void 1108f829a5cSKirk McKusick ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip) 1118f829a5cSKirk McKusick { 1128f829a5cSKirk McKusick 1138f829a5cSKirk McKusick if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0) 1148f829a5cSKirk McKusick return; 1158f829a5cSKirk McKusick /* 1168f829a5cSKirk McKusick * Exclude old di_ckhash from the crc32 calculation, e.g., always use 1178f829a5cSKirk McKusick * a check-hash value of zero when calculating the new check-hash. 1188f829a5cSKirk McKusick */ 1198f829a5cSKirk McKusick dip->di_ckhash = 0; 1208f829a5cSKirk McKusick dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip)); 1218f829a5cSKirk McKusick } 1228f829a5cSKirk McKusick 1238f829a5cSKirk McKusick /* 124dffce215SKirk McKusick * These are the low-level functions that actually read and write 125dffce215SKirk McKusick * the superblock and its associated data. 126dffce215SKirk McKusick */ 127dffce215SKirk McKusick static off_t sblock_try[] = SBLOCKSEARCH; 128fb14e73cSKirk McKusick static int readsuper(void *, struct fs **, off_t, int, int, 129dffce215SKirk McKusick int (*)(void *, off_t, void **, int)); 130076002f2SKirk McKusick static int validate_sblock(struct fs *, int); 131dffce215SKirk McKusick 132dffce215SKirk McKusick /* 133dffce215SKirk McKusick * Read a superblock from the devfd device. 134dffce215SKirk McKusick * 135dffce215SKirk McKusick * If an alternate superblock is specified, it is read. Otherwise the 136dffce215SKirk McKusick * set of locations given in the SBLOCKSEARCH list is searched for a 137dffce215SKirk McKusick * superblock. Memory is allocated for the superblock by the readfunc and 138dffce215SKirk McKusick * is returned. If filltype is non-NULL, additional memory is allocated 139dffce215SKirk McKusick * of type filltype and filled in with the superblock summary information. 140efbf3964SKirk McKusick * All memory is freed when any error is returned. 141dffce215SKirk McKusick * 142dffce215SKirk McKusick * If a superblock is found, zero is returned. Otherwise one of the 143dffce215SKirk McKusick * following error values is returned: 144dffce215SKirk McKusick * EIO: non-existent or truncated superblock. 145dffce215SKirk McKusick * EIO: error reading summary information. 146dffce215SKirk McKusick * ENOENT: no usable known superblock found. 147076002f2SKirk McKusick * ENOMEM: failed to allocate space for the superblock. 148dffce215SKirk McKusick * EINVAL: The previous newfs operation on this volume did not complete. 149dffce215SKirk McKusick * The administrator must complete newfs before using this volume. 150dffce215SKirk McKusick */ 151dffce215SKirk McKusick int 1524cbd996aSKirk McKusick ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock, 153dffce215SKirk McKusick struct malloc_type *filltype, 154dffce215SKirk McKusick int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 155dffce215SKirk McKusick { 156dffce215SKirk McKusick struct fs *fs; 15734816cb9SKirk McKusick struct fs_summary_info *fs_si; 158076002f2SKirk McKusick int i, error; 159076002f2SKirk McKusick uint64_t size, blks; 160dffce215SKirk McKusick uint8_t *space; 161dffce215SKirk McKusick int32_t *lp; 162dffce215SKirk McKusick char *buf; 163dffce215SKirk McKusick 164efbf3964SKirk McKusick fs = NULL; 16516759360SMark Johnston *fsp = NULL; 166fb14e73cSKirk McKusick if (altsblock >= 0) { 167996d40f9SKirk McKusick if ((error = readsuper(devfd, &fs, altsblock, 1, 0, 168efbf3964SKirk McKusick readfunc)) != 0) { 169efbf3964SKirk McKusick if (fs != NULL) 170efbf3964SKirk McKusick UFS_FREE(fs, filltype); 1714cbd996aSKirk McKusick return (error); 172efbf3964SKirk McKusick } 173dffce215SKirk McKusick } else { 174dffce215SKirk McKusick for (i = 0; sblock_try[i] != -1; i++) { 175efbf3964SKirk McKusick if ((error = readsuper(devfd, &fs, sblock_try[i], 0, 176f2b39152SKirk McKusick altsblock, readfunc)) == 0) 177dffce215SKirk McKusick break; 178efbf3964SKirk McKusick if (fs != NULL) { 179efbf3964SKirk McKusick UFS_FREE(fs, filltype); 180efbf3964SKirk McKusick fs = NULL; 181efbf3964SKirk McKusick } 1824cbd996aSKirk McKusick if (error == ENOENT) 183dffce215SKirk McKusick continue; 1844cbd996aSKirk McKusick return (error); 185dffce215SKirk McKusick } 186dffce215SKirk McKusick if (sblock_try[i] == -1) 187dffce215SKirk McKusick return (ENOENT); 188dffce215SKirk McKusick } 189dffce215SKirk McKusick /* 190dffce215SKirk McKusick * Read in the superblock summary information. 191dffce215SKirk McKusick */ 192dffce215SKirk McKusick size = fs->fs_cssize; 193dffce215SKirk McKusick blks = howmany(size, fs->fs_fsize); 194dffce215SKirk McKusick if (fs->fs_contigsumsize > 0) 195dffce215SKirk McKusick size += fs->fs_ncg * sizeof(int32_t); 196dffce215SKirk McKusick size += fs->fs_ncg * sizeof(u_int8_t); 197076002f2SKirk McKusick if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) { 19834816cb9SKirk McKusick UFS_FREE(fs, filltype); 199076002f2SKirk McKusick return (ENOMEM); 20034816cb9SKirk McKusick } 20134816cb9SKirk McKusick bzero(fs_si, sizeof(*fs_si)); 20234816cb9SKirk McKusick fs->fs_si = fs_si; 203076002f2SKirk McKusick if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) { 20434816cb9SKirk McKusick UFS_FREE(fs->fs_si, filltype); 205efbf3964SKirk McKusick UFS_FREE(fs, filltype); 206076002f2SKirk McKusick return (ENOMEM); 207efbf3964SKirk McKusick } 208dffce215SKirk McKusick fs->fs_csp = (struct csum *)space; 209dffce215SKirk McKusick for (i = 0; i < blks; i += fs->fs_frag) { 210dffce215SKirk McKusick size = fs->fs_bsize; 211dffce215SKirk McKusick if (i + fs->fs_frag > blks) 212dffce215SKirk McKusick size = (blks - i) * fs->fs_fsize; 21316759360SMark Johnston buf = NULL; 2144cbd996aSKirk McKusick error = (*readfunc)(devfd, 215dffce215SKirk McKusick dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size); 2164cbd996aSKirk McKusick if (error) { 217efbf3964SKirk McKusick if (buf != NULL) 21816759360SMark Johnston UFS_FREE(buf, filltype); 219dffce215SKirk McKusick UFS_FREE(fs->fs_csp, filltype); 22034816cb9SKirk McKusick UFS_FREE(fs->fs_si, filltype); 221efbf3964SKirk McKusick UFS_FREE(fs, filltype); 2224cbd996aSKirk McKusick return (error); 223dffce215SKirk McKusick } 224dffce215SKirk McKusick memcpy(space, buf, size); 225dffce215SKirk McKusick UFS_FREE(buf, filltype); 226dffce215SKirk McKusick space += size; 227dffce215SKirk McKusick } 228dffce215SKirk McKusick if (fs->fs_contigsumsize > 0) { 229dffce215SKirk McKusick fs->fs_maxcluster = lp = (int32_t *)space; 230dffce215SKirk McKusick for (i = 0; i < fs->fs_ncg; i++) 231dffce215SKirk McKusick *lp++ = fs->fs_contigsumsize; 232dffce215SKirk McKusick space = (uint8_t *)lp; 233dffce215SKirk McKusick } 234dffce215SKirk McKusick size = fs->fs_ncg * sizeof(u_int8_t); 235dffce215SKirk McKusick fs->fs_contigdirs = (u_int8_t *)space; 236dffce215SKirk McKusick bzero(fs->fs_contigdirs, size); 237efbf3964SKirk McKusick *fsp = fs; 238dffce215SKirk McKusick return (0); 239dffce215SKirk McKusick } 240dffce215SKirk McKusick 241dffce215SKirk McKusick /* 242dffce215SKirk McKusick * Try to read a superblock from the location specified by sblockloc. 243dffce215SKirk McKusick * Return zero on success or an errno on failure. 244dffce215SKirk McKusick */ 245dffce215SKirk McKusick static int 246528833faSKirk McKusick readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk, 247fb14e73cSKirk McKusick int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 248dffce215SKirk McKusick { 249dffce215SKirk McKusick struct fs *fs; 250ec888383SKirk McKusick int error, res; 251ec888383SKirk McKusick uint32_t ckhash; 252dffce215SKirk McKusick 253dffce215SKirk McKusick error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE); 254dffce215SKirk McKusick if (error != 0) 255dffce215SKirk McKusick return (error); 256dffce215SKirk McKusick fs = *fsp; 257dffce215SKirk McKusick if (fs->fs_magic == FS_BAD_MAGIC) 258dffce215SKirk McKusick return (EINVAL); 259*36e08b01SKirk McKusick /* 260*36e08b01SKirk McKusick * For UFS1 with a 65536 block size, the first backup superblock 261*36e08b01SKirk McKusick * is at the same location as the UFS2 superblock. Since SBLOCK_UFS2 262*36e08b01SKirk McKusick * is the first location checked, the first backup is the superblock 263*36e08b01SKirk McKusick * that will be accessed. Here we fail the lookup so that we can 264*36e08b01SKirk McKusick * retry with the correct location for the UFS1 superblock. 265*36e08b01SKirk McKusick */ 266*36e08b01SKirk McKusick if (fs->fs_magic == FS_UFS1_MAGIC && !isaltsblk && 267*36e08b01SKirk McKusick fs->fs_bsize == SBLOCK_UFS2 && sblockloc == SBLOCK_UFS2) 268*36e08b01SKirk McKusick return (ENOENT); 269076002f2SKirk McKusick if ((error = validate_sblock(fs, isaltsblk)) != 0) 270076002f2SKirk McKusick return (error); 271a02bd3e3SKirk McKusick /* 272a02bd3e3SKirk McKusick * If the filesystem has been run on a kernel without 273a02bd3e3SKirk McKusick * metadata check hashes, disable them. 274a02bd3e3SKirk McKusick */ 275a02bd3e3SKirk McKusick if ((fs->fs_flags & FS_METACKHASH) == 0) 276a02bd3e3SKirk McKusick fs->fs_metackhash = 0; 277996d40f9SKirk McKusick /* 278996d40f9SKirk McKusick * Clear any check-hashes that are not maintained 279996d40f9SKirk McKusick * by this kernel. Also clear any unsupported flags. 280996d40f9SKirk McKusick */ 281996d40f9SKirk McKusick fs->fs_metackhash &= CK_SUPPORTED; 282996d40f9SKirk McKusick fs->fs_flags &= FS_SUPPORTED; 283ade67b50SKirk McKusick if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) { 284f2b39152SKirk McKusick if (chkhash == STDSB_NOMSG) 285f2b39152SKirk McKusick return (EINTEGRITY); 2867ef56fb0SKirk McKusick if (chkhash == STDSB_NOHASHFAIL_NOMSG) 287f2b39152SKirk McKusick return (0); 288ec888383SKirk McKusick #ifdef _KERNEL 289ec888383SKirk McKusick res = uprintf("Superblock check-hash failed: recorded " 290fb14e73cSKirk McKusick "check-hash 0x%x != computed check-hash 0x%x%s\n", 291fb14e73cSKirk McKusick fs->fs_ckhash, ckhash, 2929b8eb1c5SKirk McKusick chkhash == STDSB_NOHASHFAIL ? " (Ignored)" : ""); 293ec888383SKirk McKusick #else 294ec888383SKirk McKusick res = 0; 295ec888383SKirk McKusick #endif 296ec888383SKirk McKusick /* 297ec888383SKirk McKusick * Print check-hash failure if no controlling terminal 298ec888383SKirk McKusick * in kernel or always if in user-mode (libufs). 299ec888383SKirk McKusick */ 300ec888383SKirk McKusick if (res == 0) 301ec888383SKirk McKusick printf("Superblock check-hash failed: recorded " 302ec888383SKirk McKusick "check-hash 0x%x != computed check-hash " 303fb14e73cSKirk McKusick "0x%x%s\n", fs->fs_ckhash, ckhash, 3049b8eb1c5SKirk McKusick chkhash == STDSB_NOHASHFAIL ? 3059b8eb1c5SKirk McKusick " (Ignored)" : ""); 306f2b39152SKirk McKusick if (chkhash == STDSB) 307f2b39152SKirk McKusick return (EINTEGRITY); 308f2b39152SKirk McKusick /* chkhash == STDSB_NOHASHFAIL */ 309fb14e73cSKirk McKusick return (0); 310fb14e73cSKirk McKusick } 311dffce215SKirk McKusick /* Have to set for old filesystems that predate this field */ 312dffce215SKirk McKusick fs->fs_sblockactualloc = sblockloc; 313efbf3964SKirk McKusick /* Not yet any summary information */ 31434816cb9SKirk McKusick fs->fs_si = NULL; 315dffce215SKirk McKusick return (0); 316dffce215SKirk McKusick } 317dffce215SKirk McKusick 318dffce215SKirk McKusick /* 319076002f2SKirk McKusick * Verify the filesystem values. 320076002f2SKirk McKusick */ 321076002f2SKirk McKusick #define ILOG2(num) (fls(num) - 1) 322ce6296caSKirk McKusick #undef CHK 32350dc4c7dSKirk McKusick #define CHK(lhs, op, rhs, fmt) \ 32450dc4c7dSKirk McKusick if (lhs op rhs) { \ 32550dc4c7dSKirk McKusick printf("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 32650dc4c7dSKirk McKusick #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, \ 32750dc4c7dSKirk McKusick #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs); \ 32850dc4c7dSKirk McKusick return (ENOENT); \ 32950dc4c7dSKirk McKusick } 33050dc4c7dSKirk McKusick #define CHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt) \ 33150dc4c7dSKirk McKusick if (lhs1 op1 rhs1 && lhs2 op2 rhs2) { \ 33250dc4c7dSKirk McKusick printf("UFS%d superblock failed: %s (" #fmt ") %s %s (" \ 33350dc4c7dSKirk McKusick #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n", \ 33450dc4c7dSKirk McKusick fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, \ 33550dc4c7dSKirk McKusick (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2, \ 33650dc4c7dSKirk McKusick (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2); \ 33750dc4c7dSKirk McKusick return (ENOENT); \ 33850dc4c7dSKirk McKusick } 339076002f2SKirk McKusick 340076002f2SKirk McKusick static int 341076002f2SKirk McKusick validate_sblock(struct fs *fs, int isaltsblk) 342076002f2SKirk McKusick { 343f3f5368dSKirk McKusick u_long i, sectorsize, cgnum; 344f3f5368dSKirk McKusick u_int64_t maxfilesize, sizepb; 345076002f2SKirk McKusick 346076002f2SKirk McKusick sectorsize = dbtob(1); 347076002f2SKirk McKusick if (fs->fs_magic == FS_UFS2_MAGIC) { 34850dc4c7dSKirk McKusick if (!isaltsblk) { 34950dc4c7dSKirk McKusick CHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx); 35050dc4c7dSKirk McKusick CHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2, 35150dc4c7dSKirk McKusick fs->fs_sblockactualloc, !=, 0, %jd); 35250dc4c7dSKirk McKusick } 35350dc4c7dSKirk McKusick CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 35450dc4c7dSKirk McKusick sizeof(ufs2_daddr_t)), %jd); 35550dc4c7dSKirk McKusick CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t), 35650dc4c7dSKirk McKusick %jd); 35750dc4c7dSKirk McKusick CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs2_dinode), 35850dc4c7dSKirk McKusick %jd); 359076002f2SKirk McKusick } else if (fs->fs_magic == FS_UFS1_MAGIC) { 36050dc4c7dSKirk McKusick if (!isaltsblk) { 3619e1f44d0SKirk McKusick CHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd); 362*36e08b01SKirk McKusick CHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd); 3639e1f44d0SKirk McKusick } 36450dc4c7dSKirk McKusick CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t), 36550dc4c7dSKirk McKusick %jd); 36650dc4c7dSKirk McKusick CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs1_dinode), 36750dc4c7dSKirk McKusick %jd); 36850dc4c7dSKirk McKusick CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) * 36950dc4c7dSKirk McKusick sizeof(ufs1_daddr_t)), %jd); 37050dc4c7dSKirk McKusick CHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd); 37150dc4c7dSKirk McKusick CHK(fs->fs_old_cgoffset, !=, 0, %jd); 37250dc4c7dSKirk McKusick CHK(fs->fs_old_cgmask, !=, 0xffffffff, %#jx); 37350dc4c7dSKirk McKusick CHK(fs->fs_old_rotdelay, !=, 0, %jd); 37450dc4c7dSKirk McKusick CHK(fs->fs_old_rps, !=, 60, %jd); 37550dc4c7dSKirk McKusick CHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd); 37650dc4c7dSKirk McKusick CHK(fs->fs_old_cpg, !=, 1, %jd); 37750dc4c7dSKirk McKusick CHK(fs->fs_old_interleave, !=, 1, %jd); 37850dc4c7dSKirk McKusick CHK(fs->fs_old_trackskew, !=, 0, %jd); 37950dc4c7dSKirk McKusick CHK(fs->fs_old_cpc, !=, 0, %jd); 38050dc4c7dSKirk McKusick CHK(fs->fs_old_postblformat, !=, 1, %jd); 38150dc4c7dSKirk McKusick CHK(fs->fs_old_nrpos, !=, 1, %jd); 38250dc4c7dSKirk McKusick CHK(fs->fs_old_spc, !=, fs->fs_fpg * fs->fs_old_nspf, %jd); 38350dc4c7dSKirk McKusick CHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd); 38450dc4c7dSKirk McKusick CHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd); 38550dc4c7dSKirk McKusick CHK(fs->fs_old_ncyl, !=, fs->fs_ncg, %jd); 386076002f2SKirk McKusick } else { 38750dc4c7dSKirk McKusick /* Bad magic number, so assume not a superblock */ 388076002f2SKirk McKusick return (ENOENT); 389076002f2SKirk McKusick } 39050dc4c7dSKirk McKusick CHK(fs->fs_bsize, <, MINBSIZE, %jd); 39150dc4c7dSKirk McKusick CHK(fs->fs_bsize, >, MAXBSIZE, %jd); 39250dc4c7dSKirk McKusick CHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd); 39350dc4c7dSKirk McKusick CHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd); 39450dc4c7dSKirk McKusick CHK(powerof2(fs->fs_bsize), ==, 0, %jd); 39550dc4c7dSKirk McKusick CHK(fs->fs_fsize, <, sectorsize, %jd); 39650dc4c7dSKirk McKusick CHK(fs->fs_fsize, >, fs->fs_bsize, %jd); 39750dc4c7dSKirk McKusick CHK(fs->fs_fsize * MAXFRAG, <, fs->fs_bsize, %jd); 39850dc4c7dSKirk McKusick CHK(powerof2(fs->fs_fsize), ==, 0, %jd); 39950dc4c7dSKirk McKusick CHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd); 40050dc4c7dSKirk McKusick CHK(powerof2(fs->fs_maxbsize), ==, 0, %jd); 40150dc4c7dSKirk McKusick CHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd); 40250dc4c7dSKirk McKusick CHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx); 40350dc4c7dSKirk McKusick CHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx); 40450dc4c7dSKirk McKusick CHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx); 40550dc4c7dSKirk McKusick CHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx); 40650dc4c7dSKirk McKusick CHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd); 40750dc4c7dSKirk McKusick CHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd); 40850dc4c7dSKirk McKusick CHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd); 40950dc4c7dSKirk McKusick CHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd); 41050dc4c7dSKirk McKusick CHK(fs->fs_frag, >, MAXFRAG, %jd); 41150dc4c7dSKirk McKusick CHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd); 41250dc4c7dSKirk McKusick CHK(fs->fs_sblkno, !=, roundup( 41350dc4c7dSKirk McKusick howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize), 41450dc4c7dSKirk McKusick fs->fs_frag), %jd); 41550dc4c7dSKirk McKusick CHK(fs->fs_cblkno, !=, fs->fs_sblkno + 41650dc4c7dSKirk McKusick roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd); 41750dc4c7dSKirk McKusick CHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd); 41850dc4c7dSKirk McKusick CHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd); 41950dc4c7dSKirk McKusick CHK(fs->fs_cgsize, >, fs->fs_bsize, %jd); 42050dc4c7dSKirk McKusick CHK(fs->fs_cssize, !=, 42150dc4c7dSKirk McKusick fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 4225bc926afSKirk McKusick /* 4235bc926afSKirk McKusick * This test is valid, however older versions of growfs failed 4245bc926afSKirk McKusick * to correctly update fs_dsize so will fail this test. Thus we 4255bc926afSKirk McKusick * exclude it from the requirements. 4265bc926afSKirk McKusick */ 4275bc926afSKirk McKusick #ifdef notdef 42850dc4c7dSKirk McKusick CHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno - 429076002f2SKirk McKusick fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) - 43050dc4c7dSKirk McKusick howmany(fs->fs_cssize, fs->fs_fsize), %jd); 4315bc926afSKirk McKusick #endif 43250dc4c7dSKirk McKusick CHK(fs->fs_metaspace, <, 0, %jd); 43350dc4c7dSKirk McKusick CHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd); 43450dc4c7dSKirk McKusick CHK(fs->fs_minfree, >, 99, %jd%%); 435076002f2SKirk McKusick maxfilesize = fs->fs_bsize * UFS_NDADDR - 1; 436076002f2SKirk McKusick for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) { 437076002f2SKirk McKusick sizepb *= NINDIR(fs); 438076002f2SKirk McKusick maxfilesize += sizepb; 439076002f2SKirk McKusick } 44050dc4c7dSKirk McKusick CHK(fs->fs_maxfilesize, !=, maxfilesize, %jd); 441076002f2SKirk McKusick /* 442076002f2SKirk McKusick * These values have a tight interaction with each other that 443076002f2SKirk McKusick * makes it hard to tightly bound them. So we can only check 444076002f2SKirk McKusick * that they are within a broader possible range. 445076002f2SKirk McKusick * 446f3f5368dSKirk McKusick * The size cannot always be accurately determined, but ensure 447f3f5368dSKirk McKusick * that it is consistent with the number of cylinder groups (fs_ncg) 448f3f5368dSKirk McKusick * and the number of fragments per cylinder group (fs_fpg). Ensure 449f3f5368dSKirk McKusick * that the summary information size is correct and that it starts 450f3f5368dSKirk McKusick * and ends in the data area of the same cylinder group. 451076002f2SKirk McKusick */ 45250dc4c7dSKirk McKusick CHK(fs->fs_ncg, <, 1, %jd); 45350dc4c7dSKirk McKusick CHK(fs->fs_size, <, 8 * fs->fs_frag, %jd); 45450dc4c7dSKirk McKusick CHK(fs->fs_size, <=, (fs->fs_ncg - 1) * fs->fs_fpg, %jd); 45550dc4c7dSKirk McKusick CHK(fs->fs_size, >, fs->fs_ncg * fs->fs_fpg, %jd); 456f3f5368dSKirk McKusick CHK(fs->fs_cssize, !=, 457f3f5368dSKirk McKusick fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd); 458904347a0SKirk McKusick CHK(dtog(fs, fs->fs_csaddr), >, fs->fs_ncg, %jd); 459f3f5368dSKirk McKusick cgnum = dtog(fs, fs->fs_csaddr); 460f3f5368dSKirk McKusick CHK(fs->fs_csaddr, <, cgdmin(fs, cgnum), %jd); 461f3f5368dSKirk McKusick CHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >, 462f3f5368dSKirk McKusick cgnum, %jd); 463f3f5368dSKirk McKusick CHK(fs->fs_ipg * fs->fs_ncg, >, (((int64_t)(1)) << 32) - INOPB(fs), 464f3f5368dSKirk McKusick %jd); 465076002f2SKirk McKusick /* 466bc218d89SKirk McKusick * With file system clustering it is possible to allocate 467bc218d89SKirk McKusick * many contiguous blocks. The kernel variable maxphys defines 468bc218d89SKirk McKusick * the maximum transfer size permitted by the controller and/or 469bc218d89SKirk McKusick * buffering. The fs_maxcontig parameter controls the maximum 470bc218d89SKirk McKusick * number of blocks that the filesystem will read or write 471bc218d89SKirk McKusick * in a single transfer. It is calculated when the filesystem 472bc218d89SKirk McKusick * is created as maxphys / fs_bsize. The loader uses a maxphys 473bc218d89SKirk McKusick * of 128K even when running on a system that supports larger 474bc218d89SKirk McKusick * values. If the filesystem was built on a system that supports 475bc218d89SKirk McKusick * a larger maxphys (1M is typical) it will have configured 476bc218d89SKirk McKusick * fs_maxcontig for that larger system. So we bound the upper 477bc218d89SKirk McKusick * allowable limit for fs_maxconfig to be able to at least 478bc218d89SKirk McKusick * work with a 1M maxphys on the smallest block size filesystem: 479bc218d89SKirk McKusick * 1M / 4096 == 256. There is no harm in allowing the mounting of 480bc218d89SKirk McKusick * filesystems that make larger than maxphys I/O requests because 481bc218d89SKirk McKusick * those (mostly 32-bit machines) can (very slowly) handle I/O 482bc218d89SKirk McKusick * requests that exceed maxphys. 483076002f2SKirk McKusick */ 48450dc4c7dSKirk McKusick CHK(fs->fs_maxcontig, <, 0, %jd); 485f3f5368dSKirk McKusick CHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd); 48650dc4c7dSKirk McKusick CHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd); 48750dc4c7dSKirk McKusick CHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=, 48850dc4c7dSKirk McKusick MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd); 489076002f2SKirk McKusick return (0); 490076002f2SKirk McKusick } 491076002f2SKirk McKusick 492076002f2SKirk McKusick /* 493dffce215SKirk McKusick * Write a superblock to the devfd device from the memory pointed to by fs. 494dffce215SKirk McKusick * Write out the superblock summary information if it is present. 495dffce215SKirk McKusick * 496dffce215SKirk McKusick * If the write is successful, zero is returned. Otherwise one of the 497dffce215SKirk McKusick * following error values is returned: 498dffce215SKirk McKusick * EIO: failed to write superblock. 499dffce215SKirk McKusick * EIO: failed to write superblock summary information. 500dffce215SKirk McKusick */ 501dffce215SKirk McKusick int 502dffce215SKirk McKusick ffs_sbput(void *devfd, struct fs *fs, off_t loc, 503dffce215SKirk McKusick int (*writefunc)(void *devfd, off_t loc, void *buf, int size)) 504dffce215SKirk McKusick { 505dffce215SKirk McKusick int i, error, blks, size; 506dffce215SKirk McKusick uint8_t *space; 507dffce215SKirk McKusick 508dffce215SKirk McKusick /* 509dffce215SKirk McKusick * If there is summary information, write it first, so if there 510dffce215SKirk McKusick * is an error, the superblock will not be marked as clean. 511dffce215SKirk McKusick */ 51234816cb9SKirk McKusick if (fs->fs_si != NULL && fs->fs_csp != NULL) { 513dffce215SKirk McKusick blks = howmany(fs->fs_cssize, fs->fs_fsize); 514dffce215SKirk McKusick space = (uint8_t *)fs->fs_csp; 515dffce215SKirk McKusick for (i = 0; i < blks; i += fs->fs_frag) { 516dffce215SKirk McKusick size = fs->fs_bsize; 517dffce215SKirk McKusick if (i + fs->fs_frag > blks) 518dffce215SKirk McKusick size = (blks - i) * fs->fs_fsize; 519dffce215SKirk McKusick if ((error = (*writefunc)(devfd, 520dffce215SKirk McKusick dbtob(fsbtodb(fs, fs->fs_csaddr + i)), 521dffce215SKirk McKusick space, size)) != 0) 522dffce215SKirk McKusick return (error); 523dffce215SKirk McKusick space += size; 524dffce215SKirk McKusick } 525dffce215SKirk McKusick } 526dffce215SKirk McKusick fs->fs_fmod = 0; 52793440bbeSKirk McKusick #ifndef _KERNEL 52893440bbeSKirk McKusick { 52993440bbeSKirk McKusick struct fs_summary_info *fs_si; 53093440bbeSKirk McKusick 53193440bbeSKirk McKusick fs->fs_time = time(NULL); 53293440bbeSKirk McKusick /* Clear the pointers for the duration of writing. */ 53393440bbeSKirk McKusick fs_si = fs->fs_si; 53493440bbeSKirk McKusick fs->fs_si = NULL; 535ade67b50SKirk McKusick fs->fs_ckhash = ffs_calc_sbhash(fs); 53693440bbeSKirk McKusick error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 53793440bbeSKirk McKusick fs->fs_si = fs_si; 53893440bbeSKirk McKusick } 53993440bbeSKirk McKusick #else /* _KERNEL */ 54093440bbeSKirk McKusick fs->fs_time = time_second; 54193440bbeSKirk McKusick fs->fs_ckhash = ffs_calc_sbhash(fs); 54293440bbeSKirk McKusick error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize); 54393440bbeSKirk McKusick #endif /* _KERNEL */ 544dffce215SKirk McKusick return (error); 545dffce215SKirk McKusick } 546dffce215SKirk McKusick 547dffce215SKirk McKusick /* 548ec888383SKirk McKusick * Calculate the check-hash for a superblock. 549ec888383SKirk McKusick */ 550ade67b50SKirk McKusick uint32_t 551ade67b50SKirk McKusick ffs_calc_sbhash(struct fs *fs) 552ec888383SKirk McKusick { 553ec888383SKirk McKusick uint32_t ckhash, save_ckhash; 554ec888383SKirk McKusick 555ec888383SKirk McKusick /* 556ec888383SKirk McKusick * A filesystem that was using a superblock ckhash may be moved 557ec888383SKirk McKusick * to an older kernel that does not support ckhashes. The 558ec888383SKirk McKusick * older kernel will clear the FS_METACKHASH flag indicating 559ec888383SKirk McKusick * that it does not update hashes. When the disk is moved back 560ec888383SKirk McKusick * to a kernel capable of ckhashes it disables them on mount: 561ec888383SKirk McKusick * 562ec888383SKirk McKusick * if ((fs->fs_flags & FS_METACKHASH) == 0) 563ec888383SKirk McKusick * fs->fs_metackhash = 0; 564ec888383SKirk McKusick * 565ec888383SKirk McKusick * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an 566ec888383SKirk McKusick * old stale value in the fs->fs_ckhash field. Thus the need to 567ec888383SKirk McKusick * just accept what is there. 568ec888383SKirk McKusick */ 569ec888383SKirk McKusick if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0) 570ec888383SKirk McKusick return (fs->fs_ckhash); 571ec888383SKirk McKusick 572ec888383SKirk McKusick save_ckhash = fs->fs_ckhash; 573ec888383SKirk McKusick fs->fs_ckhash = 0; 574ec888383SKirk McKusick /* 575ec888383SKirk McKusick * If newly read from disk, the caller is responsible for 576ec888383SKirk McKusick * verifying that fs->fs_sbsize <= SBLOCKSIZE. 577ec888383SKirk McKusick */ 578ec888383SKirk McKusick ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize); 579ec888383SKirk McKusick fs->fs_ckhash = save_ckhash; 580ec888383SKirk McKusick return (ckhash); 581ec888383SKirk McKusick } 582ec888383SKirk McKusick 583ec888383SKirk McKusick /* 584df8bae1dSRodney W. Grimes * Update the frsum fields to reflect addition or deletion 585df8bae1dSRodney W. Grimes * of some frags. 586df8bae1dSRodney W. Grimes */ 587df8bae1dSRodney W. Grimes void 58815c377c3SEd Maste ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 589df8bae1dSRodney W. Grimes { 590df8bae1dSRodney W. Grimes int inblk; 59105f4ff5dSPoul-Henning Kamp int field, subfield; 59205f4ff5dSPoul-Henning Kamp int siz, pos; 593df8bae1dSRodney W. Grimes 594df8bae1dSRodney W. Grimes inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 595df8bae1dSRodney W. Grimes fragmap <<= 1; 596df8bae1dSRodney W. Grimes for (siz = 1; siz < fs->fs_frag; siz++) { 597df8bae1dSRodney W. Grimes if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 598df8bae1dSRodney W. Grimes continue; 599df8bae1dSRodney W. Grimes field = around[siz]; 600df8bae1dSRodney W. Grimes subfield = inside[siz]; 601df8bae1dSRodney W. Grimes for (pos = siz; pos <= fs->fs_frag; pos++) { 602df8bae1dSRodney W. Grimes if ((fragmap & field) == subfield) { 603df8bae1dSRodney W. Grimes fraglist[siz] += cnt; 604df8bae1dSRodney W. Grimes pos += siz; 605df8bae1dSRodney W. Grimes field <<= siz; 606df8bae1dSRodney W. Grimes subfield <<= siz; 607df8bae1dSRodney W. Grimes } 608df8bae1dSRodney W. Grimes field <<= 1; 609df8bae1dSRodney W. Grimes subfield <<= 1; 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes } 613df8bae1dSRodney W. Grimes 614df8bae1dSRodney W. Grimes /* 615df8bae1dSRodney W. Grimes * block operations 616df8bae1dSRodney W. Grimes * 617df8bae1dSRodney W. Grimes * check if a block is available 618df8bae1dSRodney W. Grimes */ 619df8bae1dSRodney W. Grimes int 62015c377c3SEd Maste ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 621df8bae1dSRodney W. Grimes { 622df8bae1dSRodney W. Grimes unsigned char mask; 623df8bae1dSRodney W. Grimes 624df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) { 625df8bae1dSRodney W. Grimes case 8: 626df8bae1dSRodney W. Grimes return (cp[h] == 0xff); 627df8bae1dSRodney W. Grimes case 4: 628df8bae1dSRodney W. Grimes mask = 0x0f << ((h & 0x1) << 2); 629df8bae1dSRodney W. Grimes return ((cp[h >> 1] & mask) == mask); 630df8bae1dSRodney W. Grimes case 2: 631df8bae1dSRodney W. Grimes mask = 0x03 << ((h & 0x3) << 1); 632df8bae1dSRodney W. Grimes return ((cp[h >> 2] & mask) == mask); 633df8bae1dSRodney W. Grimes case 1: 634df8bae1dSRodney W. Grimes mask = 0x01 << (h & 0x7); 635df8bae1dSRodney W. Grimes return ((cp[h >> 3] & mask) == mask); 636df8bae1dSRodney W. Grimes default: 637113db2ddSJeff Roberson #ifdef _KERNEL 638df8bae1dSRodney W. Grimes panic("ffs_isblock"); 639113db2ddSJeff Roberson #endif 640113db2ddSJeff Roberson break; 641113db2ddSJeff Roberson } 642113db2ddSJeff Roberson return (0); 643113db2ddSJeff Roberson } 644113db2ddSJeff Roberson 645113db2ddSJeff Roberson /* 646113db2ddSJeff Roberson * check if a block is free 647113db2ddSJeff Roberson */ 648113db2ddSJeff Roberson int 64915c377c3SEd Maste ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 650113db2ddSJeff Roberson { 651113db2ddSJeff Roberson 652113db2ddSJeff Roberson switch ((int)fs->fs_frag) { 653113db2ddSJeff Roberson case 8: 654113db2ddSJeff Roberson return (cp[h] == 0); 655113db2ddSJeff Roberson case 4: 656113db2ddSJeff Roberson return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 657113db2ddSJeff Roberson case 2: 658113db2ddSJeff Roberson return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 659113db2ddSJeff Roberson case 1: 660113db2ddSJeff Roberson return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 661113db2ddSJeff Roberson default: 662113db2ddSJeff Roberson #ifdef _KERNEL 663113db2ddSJeff Roberson panic("ffs_isfreeblock"); 664113db2ddSJeff Roberson #endif 665113db2ddSJeff Roberson break; 666df8bae1dSRodney W. Grimes } 667589c7af9SKirk McKusick return (0); 668df8bae1dSRodney W. Grimes } 669df8bae1dSRodney W. Grimes 670df8bae1dSRodney W. Grimes /* 671df8bae1dSRodney W. Grimes * take a block out of the map 672df8bae1dSRodney W. Grimes */ 673df8bae1dSRodney W. Grimes void 67415c377c3SEd Maste ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 675df8bae1dSRodney W. Grimes { 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) { 678df8bae1dSRodney W. Grimes case 8: 679df8bae1dSRodney W. Grimes cp[h] = 0; 680df8bae1dSRodney W. Grimes return; 681df8bae1dSRodney W. Grimes case 4: 682df8bae1dSRodney W. Grimes cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 683df8bae1dSRodney W. Grimes return; 684df8bae1dSRodney W. Grimes case 2: 685df8bae1dSRodney W. Grimes cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 686df8bae1dSRodney W. Grimes return; 687df8bae1dSRodney W. Grimes case 1: 688df8bae1dSRodney W. Grimes cp[h >> 3] &= ~(0x01 << (h & 0x7)); 689df8bae1dSRodney W. Grimes return; 690df8bae1dSRodney W. Grimes default: 691113db2ddSJeff Roberson #ifdef _KERNEL 692df8bae1dSRodney W. Grimes panic("ffs_clrblock"); 693113db2ddSJeff Roberson #endif 694113db2ddSJeff Roberson break; 695df8bae1dSRodney W. Grimes } 696df8bae1dSRodney W. Grimes } 697df8bae1dSRodney W. Grimes 698df8bae1dSRodney W. Grimes /* 699df8bae1dSRodney W. Grimes * put a block into the map 700df8bae1dSRodney W. Grimes */ 701df8bae1dSRodney W. Grimes void 70215c377c3SEd Maste ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 703df8bae1dSRodney W. Grimes { 704df8bae1dSRodney W. Grimes 705df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) { 706df8bae1dSRodney W. Grimes case 8: 707df8bae1dSRodney W. Grimes cp[h] = 0xff; 708df8bae1dSRodney W. Grimes return; 709df8bae1dSRodney W. Grimes case 4: 710df8bae1dSRodney W. Grimes cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 711df8bae1dSRodney W. Grimes return; 712df8bae1dSRodney W. Grimes case 2: 713df8bae1dSRodney W. Grimes cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 714df8bae1dSRodney W. Grimes return; 715df8bae1dSRodney W. Grimes case 1: 716df8bae1dSRodney W. Grimes cp[h >> 3] |= (0x01 << (h & 0x7)); 717df8bae1dSRodney W. Grimes return; 718df8bae1dSRodney W. Grimes default: 719113db2ddSJeff Roberson #ifdef _KERNEL 720df8bae1dSRodney W. Grimes panic("ffs_setblock"); 721113db2ddSJeff Roberson #endif 722113db2ddSJeff Roberson break; 723df8bae1dSRodney W. Grimes } 724df8bae1dSRodney W. Grimes } 725113db2ddSJeff Roberson 726113db2ddSJeff Roberson /* 727113db2ddSJeff Roberson * Update the cluster map because of an allocation or free. 728113db2ddSJeff Roberson * 729113db2ddSJeff Roberson * Cnt == 1 means free; cnt == -1 means allocating. 730113db2ddSJeff Roberson */ 731113db2ddSJeff Roberson void 73215c377c3SEd Maste ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt) 733113db2ddSJeff Roberson { 734113db2ddSJeff Roberson int32_t *sump; 735113db2ddSJeff Roberson int32_t *lp; 736113db2ddSJeff Roberson u_char *freemapp, *mapp; 737e1c27cf7SKirk McKusick int i, start, end, forw, back, map; 738e1c27cf7SKirk McKusick u_int bit; 739113db2ddSJeff Roberson 740113db2ddSJeff Roberson if (fs->fs_contigsumsize <= 0) 741113db2ddSJeff Roberson return; 742113db2ddSJeff Roberson freemapp = cg_clustersfree(cgp); 743113db2ddSJeff Roberson sump = cg_clustersum(cgp); 744113db2ddSJeff Roberson /* 745113db2ddSJeff Roberson * Allocate or clear the actual block. 746113db2ddSJeff Roberson */ 747113db2ddSJeff Roberson if (cnt > 0) 748113db2ddSJeff Roberson setbit(freemapp, blkno); 749113db2ddSJeff Roberson else 750113db2ddSJeff Roberson clrbit(freemapp, blkno); 751113db2ddSJeff Roberson /* 752113db2ddSJeff Roberson * Find the size of the cluster going forward. 753113db2ddSJeff Roberson */ 754113db2ddSJeff Roberson start = blkno + 1; 755113db2ddSJeff Roberson end = start + fs->fs_contigsumsize; 756113db2ddSJeff Roberson if (end >= cgp->cg_nclusterblks) 757113db2ddSJeff Roberson end = cgp->cg_nclusterblks; 758113db2ddSJeff Roberson mapp = &freemapp[start / NBBY]; 759113db2ddSJeff Roberson map = *mapp++; 760e1c27cf7SKirk McKusick bit = 1U << (start % NBBY); 761113db2ddSJeff Roberson for (i = start; i < end; i++) { 762113db2ddSJeff Roberson if ((map & bit) == 0) 763113db2ddSJeff Roberson break; 764113db2ddSJeff Roberson if ((i & (NBBY - 1)) != (NBBY - 1)) { 765113db2ddSJeff Roberson bit <<= 1; 766113db2ddSJeff Roberson } else { 767113db2ddSJeff Roberson map = *mapp++; 768113db2ddSJeff Roberson bit = 1; 769113db2ddSJeff Roberson } 770113db2ddSJeff Roberson } 771113db2ddSJeff Roberson forw = i - start; 772113db2ddSJeff Roberson /* 773113db2ddSJeff Roberson * Find the size of the cluster going backward. 774113db2ddSJeff Roberson */ 775113db2ddSJeff Roberson start = blkno - 1; 776113db2ddSJeff Roberson end = start - fs->fs_contigsumsize; 777113db2ddSJeff Roberson if (end < 0) 778113db2ddSJeff Roberson end = -1; 779113db2ddSJeff Roberson mapp = &freemapp[start / NBBY]; 780113db2ddSJeff Roberson map = *mapp--; 781e1c27cf7SKirk McKusick bit = 1U << (start % NBBY); 782113db2ddSJeff Roberson for (i = start; i > end; i--) { 783113db2ddSJeff Roberson if ((map & bit) == 0) 784113db2ddSJeff Roberson break; 785113db2ddSJeff Roberson if ((i & (NBBY - 1)) != 0) { 786113db2ddSJeff Roberson bit >>= 1; 787113db2ddSJeff Roberson } else { 788113db2ddSJeff Roberson map = *mapp--; 789e1c27cf7SKirk McKusick bit = 1U << (NBBY - 1); 790113db2ddSJeff Roberson } 791113db2ddSJeff Roberson } 792113db2ddSJeff Roberson back = start - i; 793113db2ddSJeff Roberson /* 794113db2ddSJeff Roberson * Account for old cluster and the possibly new forward and 795113db2ddSJeff Roberson * back clusters. 796113db2ddSJeff Roberson */ 797113db2ddSJeff Roberson i = back + forw + 1; 798113db2ddSJeff Roberson if (i > fs->fs_contigsumsize) 799113db2ddSJeff Roberson i = fs->fs_contigsumsize; 800113db2ddSJeff Roberson sump[i] += cnt; 801113db2ddSJeff Roberson if (back > 0) 802113db2ddSJeff Roberson sump[back] -= cnt; 803113db2ddSJeff Roberson if (forw > 0) 804113db2ddSJeff Roberson sump[forw] -= cnt; 805113db2ddSJeff Roberson /* 806113db2ddSJeff Roberson * Update cluster summary information. 807113db2ddSJeff Roberson */ 808113db2ddSJeff Roberson lp = &sump[fs->fs_contigsumsize]; 809113db2ddSJeff Roberson for (i = fs->fs_contigsumsize; i > 0; i--) 810113db2ddSJeff Roberson if (*lp-- > 0) 811113db2ddSJeff Roberson break; 812113db2ddSJeff Roberson fs->fs_maxcluster[cgp->cg_cgx] = i; 813113db2ddSJeff Roberson } 814