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> 38df8bae1dSRodney W. Grimes 39c4473420SPeter Wemm #ifndef _KERNEL 40dffce215SKirk McKusick #include <stdio.h> 41dffce215SKirk McKusick #include <string.h> 42dffce215SKirk McKusick #include <stdlib.h> 43dffce215SKirk McKusick #include <time.h> 44dffce215SKirk McKusick #include <sys/errno.h> 45996c772fSJohn Dyson #include <ufs/ufs/dinode.h> 461c85e6a3SKirk McKusick #include <ufs/ffs/fs.h> 47dffce215SKirk McKusick 48ec888383SKirk McKusick uint32_t calculate_crc32c(uint32_t, const void *, size_t); 49ade67b50SKirk McKusick uint32_t ffs_calc_sbhash(struct fs *); 50dffce215SKirk McKusick struct malloc_type; 51dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size) 52dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr) 53dffce215SKirk McKusick #define UFS_TIME time(NULL) 54dffce215SKirk McKusick 55dffce215SKirk McKusick #else /* _KERNEL */ 56df8bae1dSRodney W. Grimes #include <sys/systm.h> 571cd52ec3SBruce Evans #include <sys/lock.h> 581c85e6a3SKirk McKusick #include <sys/malloc.h> 591c85e6a3SKirk McKusick #include <sys/mount.h> 60df8bae1dSRodney W. Grimes #include <sys/vnode.h> 619626b608SPoul-Henning Kamp #include <sys/bio.h> 62df8bae1dSRodney W. Grimes #include <sys/buf.h> 6308637435SBruce Evans #include <sys/ucred.h> 6408637435SBruce Evans 65df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h> 66df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h> 671c85e6a3SKirk McKusick #include <ufs/ufs/extattr.h> 681c85e6a3SKirk McKusick #include <ufs/ufs/ufsmount.h> 691c85e6a3SKirk McKusick #include <ufs/ufs/ufs_extern.h> 70996c772fSJohn Dyson #include <ufs/ffs/ffs_extern.h> 711c85e6a3SKirk McKusick #include <ufs/ffs/fs.h> 72df8bae1dSRodney W. Grimes 73dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size, type, flags) 74dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr, type) 75dffce215SKirk McKusick #define UFS_TIME time_second 76dffce215SKirk McKusick 77df8bae1dSRodney W. Grimes /* 78df8bae1dSRodney W. Grimes * Return buffer with the contents of block "offset" from the beginning of 79df8bae1dSRodney W. Grimes * directory "ip". If "res" is non-zero, fill it in with a pointer to the 80df8bae1dSRodney W. Grimes * remaining space in the directory. 81df8bae1dSRodney W. Grimes */ 82df8bae1dSRodney W. Grimes int 8315c377c3SEd Maste ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp) 84df8bae1dSRodney W. Grimes { 85df8bae1dSRodney W. Grimes struct inode *ip; 8605f4ff5dSPoul-Henning Kamp struct fs *fs; 87df8bae1dSRodney W. Grimes struct buf *bp; 881c85e6a3SKirk McKusick ufs_lbn_t lbn; 89df8bae1dSRodney W. Grimes int bsize, error; 90df8bae1dSRodney W. Grimes 91cec0f20cSPoul-Henning Kamp ip = VTOI(vp); 92e1db6897SKonstantin Belousov fs = ITOFS(ip); 93cec0f20cSPoul-Henning Kamp lbn = lblkno(fs, offset); 94df8bae1dSRodney W. Grimes bsize = blksize(fs, ip, lbn); 95df8bae1dSRodney W. Grimes 96cec0f20cSPoul-Henning Kamp *bpp = NULL; 97cec0f20cSPoul-Henning Kamp error = bread(vp, lbn, bsize, NOCRED, &bp); 98c1d9efcbSPoul-Henning Kamp if (error) { 99df8bae1dSRodney W. Grimes brelse(bp); 100df8bae1dSRodney W. Grimes return (error); 101df8bae1dSRodney W. Grimes } 102cec0f20cSPoul-Henning Kamp if (res) 103cec0f20cSPoul-Henning Kamp *res = (char *)bp->b_data + blkoff(fs, offset); 104cec0f20cSPoul-Henning Kamp *bpp = bp; 105df8bae1dSRodney W. Grimes return (0); 106df8bae1dSRodney W. Grimes } 1071c85e6a3SKirk McKusick 1081c85e6a3SKirk McKusick /* 1091c85e6a3SKirk McKusick * Load up the contents of an inode and copy the appropriate pieces 1101c85e6a3SKirk McKusick * to the incore copy. 1111c85e6a3SKirk McKusick */ 1129fc5d538SKirk McKusick int 11315c377c3SEd Maste ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino) 1141c85e6a3SKirk McKusick { 1159fc5d538SKirk McKusick struct ufs1_dinode *dip1; 1169fc5d538SKirk McKusick struct ufs2_dinode *dip2; 1171c85e6a3SKirk McKusick 118e1db6897SKonstantin Belousov if (I_IS_UFS1(ip)) { 1199fc5d538SKirk McKusick dip1 = ip->i_din1; 1209fc5d538SKirk McKusick *dip1 = 1211c85e6a3SKirk McKusick *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino)); 1229fc5d538SKirk McKusick ip->i_mode = dip1->di_mode; 1239fc5d538SKirk McKusick ip->i_nlink = dip1->di_nlink; 1249fc5d538SKirk McKusick ip->i_size = dip1->di_size; 1259fc5d538SKirk McKusick ip->i_flags = dip1->di_flags; 1269fc5d538SKirk McKusick ip->i_gen = dip1->di_gen; 1279fc5d538SKirk McKusick ip->i_uid = dip1->di_uid; 1289fc5d538SKirk McKusick ip->i_gid = dip1->di_gid; 1299fc5d538SKirk McKusick return (0); 1301c85e6a3SKirk McKusick } 1319fc5d538SKirk McKusick dip2 = ip->i_din2; 1329fc5d538SKirk McKusick *dip2 = *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino)); 1339fc5d538SKirk McKusick ip->i_mode = dip2->di_mode; 1349fc5d538SKirk McKusick ip->i_nlink = dip2->di_nlink; 1359fc5d538SKirk McKusick ip->i_size = dip2->di_size; 1369fc5d538SKirk McKusick ip->i_flags = dip2->di_flags; 1379fc5d538SKirk McKusick ip->i_gen = dip2->di_gen; 1389fc5d538SKirk McKusick ip->i_uid = dip2->di_uid; 1399fc5d538SKirk McKusick ip->i_gid = dip2->di_gid; 1409fc5d538SKirk McKusick return (0); 1411c85e6a3SKirk McKusick } 1421c85e6a3SKirk McKusick #endif /* KERNEL */ 143df8bae1dSRodney W. Grimes 144df8bae1dSRodney W. Grimes /* 145dffce215SKirk McKusick * These are the low-level functions that actually read and write 146dffce215SKirk McKusick * the superblock and its associated data. 147dffce215SKirk McKusick */ 148dffce215SKirk McKusick static off_t sblock_try[] = SBLOCKSEARCH; 149528833faSKirk McKusick static int readsuper(void *, struct fs **, off_t, int, 150dffce215SKirk McKusick int (*)(void *, off_t, void **, int)); 151dffce215SKirk McKusick 152dffce215SKirk McKusick /* 153dffce215SKirk McKusick * Read a superblock from the devfd device. 154dffce215SKirk McKusick * 155dffce215SKirk McKusick * If an alternate superblock is specified, it is read. Otherwise the 156dffce215SKirk McKusick * set of locations given in the SBLOCKSEARCH list is searched for a 157dffce215SKirk McKusick * superblock. Memory is allocated for the superblock by the readfunc and 158dffce215SKirk McKusick * is returned. If filltype is non-NULL, additional memory is allocated 159dffce215SKirk McKusick * of type filltype and filled in with the superblock summary information. 160efbf3964SKirk McKusick * All memory is freed when any error is returned. 161dffce215SKirk McKusick * 162dffce215SKirk McKusick * If a superblock is found, zero is returned. Otherwise one of the 163dffce215SKirk McKusick * following error values is returned: 164dffce215SKirk McKusick * EIO: non-existent or truncated superblock. 165dffce215SKirk McKusick * EIO: error reading summary information. 166dffce215SKirk McKusick * ENOENT: no usable known superblock found. 167dffce215SKirk McKusick * ENOSPC: failed to allocate space for the superblock. 168dffce215SKirk McKusick * EINVAL: The previous newfs operation on this volume did not complete. 169dffce215SKirk McKusick * The administrator must complete newfs before using this volume. 170dffce215SKirk McKusick */ 171dffce215SKirk McKusick int 1724cbd996aSKirk McKusick ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock, 173dffce215SKirk McKusick struct malloc_type *filltype, 174dffce215SKirk McKusick int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 175dffce215SKirk McKusick { 176dffce215SKirk McKusick struct fs *fs; 1774cbd996aSKirk McKusick int i, error, size, blks; 178dffce215SKirk McKusick uint8_t *space; 179dffce215SKirk McKusick int32_t *lp; 180dffce215SKirk McKusick char *buf; 181dffce215SKirk McKusick 182efbf3964SKirk McKusick fs = NULL; 18316759360SMark Johnston *fsp = NULL; 1844cbd996aSKirk McKusick if (altsblock != -1) { 185efbf3964SKirk McKusick if ((error = readsuper(devfd, &fs, altsblock, 1, 186efbf3964SKirk McKusick readfunc)) != 0) { 187efbf3964SKirk McKusick if (fs != NULL) 188efbf3964SKirk McKusick UFS_FREE(fs, filltype); 1894cbd996aSKirk McKusick return (error); 190efbf3964SKirk McKusick } 191dffce215SKirk McKusick } else { 192dffce215SKirk McKusick for (i = 0; sblock_try[i] != -1; i++) { 193efbf3964SKirk McKusick if ((error = readsuper(devfd, &fs, sblock_try[i], 0, 194f686b171SKirk McKusick readfunc)) == 0) 195dffce215SKirk McKusick break; 196efbf3964SKirk McKusick if (fs != NULL) { 197efbf3964SKirk McKusick UFS_FREE(fs, filltype); 198efbf3964SKirk McKusick fs = NULL; 199efbf3964SKirk McKusick } 2004cbd996aSKirk McKusick if (error == ENOENT) 201dffce215SKirk McKusick continue; 2024cbd996aSKirk McKusick return (error); 203dffce215SKirk McKusick } 204dffce215SKirk McKusick if (sblock_try[i] == -1) 205dffce215SKirk McKusick return (ENOENT); 206dffce215SKirk McKusick } 207dffce215SKirk McKusick /* 208dffce215SKirk McKusick * Read in the superblock summary information. 209dffce215SKirk McKusick */ 210dffce215SKirk McKusick size = fs->fs_cssize; 211dffce215SKirk McKusick blks = howmany(size, fs->fs_fsize); 212dffce215SKirk McKusick if (fs->fs_contigsumsize > 0) 213dffce215SKirk McKusick size += fs->fs_ncg * sizeof(int32_t); 214dffce215SKirk McKusick size += fs->fs_ncg * sizeof(u_int8_t); 215efbf3964SKirk McKusick /* When running in libufs or libsa, UFS_MALLOC may fail */ 216efbf3964SKirk McKusick if ((space = UFS_MALLOC(size, filltype, M_WAITOK)) == NULL) { 217efbf3964SKirk McKusick UFS_FREE(fs, filltype); 218efbf3964SKirk McKusick return (ENOSPC); 219efbf3964SKirk McKusick } 220dffce215SKirk McKusick fs->fs_csp = (struct csum *)space; 221dffce215SKirk McKusick for (i = 0; i < blks; i += fs->fs_frag) { 222dffce215SKirk McKusick size = fs->fs_bsize; 223dffce215SKirk McKusick if (i + fs->fs_frag > blks) 224dffce215SKirk McKusick size = (blks - i) * fs->fs_fsize; 22516759360SMark Johnston buf = NULL; 2264cbd996aSKirk McKusick error = (*readfunc)(devfd, 227dffce215SKirk McKusick dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size); 2284cbd996aSKirk McKusick if (error) { 229efbf3964SKirk McKusick if (buf != NULL) 23016759360SMark Johnston UFS_FREE(buf, filltype); 231dffce215SKirk McKusick UFS_FREE(fs->fs_csp, filltype); 232efbf3964SKirk McKusick UFS_FREE(fs, filltype); 2334cbd996aSKirk McKusick return (error); 234dffce215SKirk McKusick } 235dffce215SKirk McKusick memcpy(space, buf, size); 236dffce215SKirk McKusick UFS_FREE(buf, filltype); 237dffce215SKirk McKusick space += size; 238dffce215SKirk McKusick } 239dffce215SKirk McKusick if (fs->fs_contigsumsize > 0) { 240dffce215SKirk McKusick fs->fs_maxcluster = lp = (int32_t *)space; 241dffce215SKirk McKusick for (i = 0; i < fs->fs_ncg; i++) 242dffce215SKirk McKusick *lp++ = fs->fs_contigsumsize; 243dffce215SKirk McKusick space = (uint8_t *)lp; 244dffce215SKirk McKusick } 245dffce215SKirk McKusick size = fs->fs_ncg * sizeof(u_int8_t); 246dffce215SKirk McKusick fs->fs_contigdirs = (u_int8_t *)space; 247dffce215SKirk McKusick bzero(fs->fs_contigdirs, size); 248efbf3964SKirk McKusick *fsp = fs; 249dffce215SKirk McKusick return (0); 250dffce215SKirk McKusick } 251dffce215SKirk McKusick 252dffce215SKirk McKusick /* 253dffce215SKirk McKusick * Try to read a superblock from the location specified by sblockloc. 254dffce215SKirk McKusick * Return zero on success or an errno on failure. 255dffce215SKirk McKusick */ 256dffce215SKirk McKusick static int 257528833faSKirk McKusick readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk, 258dffce215SKirk McKusick int (*readfunc)(void *devfd, off_t loc, void **bufp, int size)) 259dffce215SKirk McKusick { 260dffce215SKirk McKusick struct fs *fs; 261ec888383SKirk McKusick int error, res; 262ec888383SKirk McKusick uint32_t ckhash; 263dffce215SKirk McKusick 264dffce215SKirk McKusick error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE); 265dffce215SKirk McKusick if (error != 0) 266dffce215SKirk McKusick return (error); 267dffce215SKirk McKusick fs = *fsp; 268dffce215SKirk McKusick if (fs->fs_magic == FS_BAD_MAGIC) 269dffce215SKirk McKusick return (EINVAL); 270528833faSKirk McKusick if (((fs->fs_magic == FS_UFS1_MAGIC && (isaltsblk || 2714cbd996aSKirk McKusick sblockloc <= SBLOCK_UFS1)) || 272528833faSKirk McKusick (fs->fs_magic == FS_UFS2_MAGIC && (isaltsblk || 2734cbd996aSKirk McKusick sblockloc == fs->fs_sblockloc))) && 274dffce215SKirk McKusick fs->fs_ncg >= 1 && 275dffce215SKirk McKusick fs->fs_bsize >= MINBSIZE && 276dffce215SKirk McKusick fs->fs_bsize <= MAXBSIZE && 277ec888383SKirk McKusick fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE) && 278ec888383SKirk McKusick fs->fs_sbsize <= SBLOCKSIZE) { 279*a02bd3e3SKirk McKusick /* 280*a02bd3e3SKirk McKusick * If the filesystem has been run on a kernel without 281*a02bd3e3SKirk McKusick * metadata check hashes, disable them. 282*a02bd3e3SKirk McKusick */ 283*a02bd3e3SKirk McKusick if ((fs->fs_flags & FS_METACKHASH) == 0) 284*a02bd3e3SKirk McKusick fs->fs_metackhash = 0; 285ade67b50SKirk McKusick if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) { 286ec888383SKirk McKusick #ifdef _KERNEL 287ec888383SKirk McKusick res = uprintf("Superblock check-hash failed: recorded " 288ec888383SKirk McKusick "check-hash 0x%x != computed check-hash 0x%x\n", 289ec888383SKirk McKusick fs->fs_ckhash, ckhash); 290ec888383SKirk McKusick #else 291ec888383SKirk McKusick res = 0; 292ec888383SKirk McKusick #endif 293ec888383SKirk McKusick /* 294ec888383SKirk McKusick * Print check-hash failure if no controlling terminal 295ec888383SKirk McKusick * in kernel or always if in user-mode (libufs). 296ec888383SKirk McKusick */ 297ec888383SKirk McKusick if (res == 0) 298ec888383SKirk McKusick printf("Superblock check-hash failed: recorded " 299ec888383SKirk McKusick "check-hash 0x%x != computed check-hash " 300ec888383SKirk McKusick "0x%x\n", fs->fs_ckhash, ckhash); 301ec888383SKirk McKusick return (EINVAL); 302ec888383SKirk McKusick } 303dffce215SKirk McKusick /* Have to set for old filesystems that predate this field */ 304dffce215SKirk McKusick fs->fs_sblockactualloc = sblockloc; 305efbf3964SKirk McKusick /* Not yet any summary information */ 306efbf3964SKirk McKusick fs->fs_csp = NULL; 307dffce215SKirk McKusick return (0); 308dffce215SKirk McKusick } 309dffce215SKirk McKusick return (ENOENT); 310dffce215SKirk McKusick } 311dffce215SKirk McKusick 312dffce215SKirk McKusick /* 313dffce215SKirk McKusick * Write a superblock to the devfd device from the memory pointed to by fs. 314dffce215SKirk McKusick * Write out the superblock summary information if it is present. 315dffce215SKirk McKusick * 316dffce215SKirk McKusick * If the write is successful, zero is returned. Otherwise one of the 317dffce215SKirk McKusick * following error values is returned: 318dffce215SKirk McKusick * EIO: failed to write superblock. 319dffce215SKirk McKusick * EIO: failed to write superblock summary information. 320dffce215SKirk McKusick */ 321dffce215SKirk McKusick int 322dffce215SKirk McKusick ffs_sbput(void *devfd, struct fs *fs, off_t loc, 323dffce215SKirk McKusick int (*writefunc)(void *devfd, off_t loc, void *buf, int size)) 324dffce215SKirk McKusick { 325dffce215SKirk McKusick int i, error, blks, size; 326dffce215SKirk McKusick uint8_t *space; 327dffce215SKirk McKusick 328dffce215SKirk McKusick /* 329dffce215SKirk McKusick * If there is summary information, write it first, so if there 330dffce215SKirk McKusick * is an error, the superblock will not be marked as clean. 331dffce215SKirk McKusick */ 332dffce215SKirk McKusick if (fs->fs_csp != NULL) { 333dffce215SKirk McKusick blks = howmany(fs->fs_cssize, fs->fs_fsize); 334dffce215SKirk McKusick space = (uint8_t *)fs->fs_csp; 335dffce215SKirk McKusick for (i = 0; i < blks; i += fs->fs_frag) { 336dffce215SKirk McKusick size = fs->fs_bsize; 337dffce215SKirk McKusick if (i + fs->fs_frag > blks) 338dffce215SKirk McKusick size = (blks - i) * fs->fs_fsize; 339dffce215SKirk McKusick if ((error = (*writefunc)(devfd, 340dffce215SKirk McKusick dbtob(fsbtodb(fs, fs->fs_csaddr + i)), 341dffce215SKirk McKusick space, size)) != 0) 342dffce215SKirk McKusick return (error); 343dffce215SKirk McKusick space += size; 344dffce215SKirk McKusick } 345dffce215SKirk McKusick } 346dffce215SKirk McKusick fs->fs_fmod = 0; 347dffce215SKirk McKusick fs->fs_time = UFS_TIME; 348ade67b50SKirk McKusick fs->fs_ckhash = ffs_calc_sbhash(fs); 349dffce215SKirk McKusick if ((error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize)) != 0) 350dffce215SKirk McKusick return (error); 351dffce215SKirk McKusick return (0); 352dffce215SKirk McKusick } 353dffce215SKirk McKusick 354dffce215SKirk McKusick /* 355ec888383SKirk McKusick * Calculate the check-hash for a superblock. 356ec888383SKirk McKusick */ 357ade67b50SKirk McKusick uint32_t 358ade67b50SKirk McKusick ffs_calc_sbhash(struct fs *fs) 359ec888383SKirk McKusick { 360ec888383SKirk McKusick uint32_t ckhash, save_ckhash; 361ec888383SKirk McKusick 362ec888383SKirk McKusick /* 363ec888383SKirk McKusick * A filesystem that was using a superblock ckhash may be moved 364ec888383SKirk McKusick * to an older kernel that does not support ckhashes. The 365ec888383SKirk McKusick * older kernel will clear the FS_METACKHASH flag indicating 366ec888383SKirk McKusick * that it does not update hashes. When the disk is moved back 367ec888383SKirk McKusick * to a kernel capable of ckhashes it disables them on mount: 368ec888383SKirk McKusick * 369ec888383SKirk McKusick * if ((fs->fs_flags & FS_METACKHASH) == 0) 370ec888383SKirk McKusick * fs->fs_metackhash = 0; 371ec888383SKirk McKusick * 372ec888383SKirk McKusick * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an 373ec888383SKirk McKusick * old stale value in the fs->fs_ckhash field. Thus the need to 374ec888383SKirk McKusick * just accept what is there. 375ec888383SKirk McKusick */ 376ec888383SKirk McKusick if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0) 377ec888383SKirk McKusick return (fs->fs_ckhash); 378ec888383SKirk McKusick 379ec888383SKirk McKusick save_ckhash = fs->fs_ckhash; 380ec888383SKirk McKusick fs->fs_ckhash = 0; 381ec888383SKirk McKusick /* 382ec888383SKirk McKusick * If newly read from disk, the caller is responsible for 383ec888383SKirk McKusick * verifying that fs->fs_sbsize <= SBLOCKSIZE. 384ec888383SKirk McKusick */ 385ec888383SKirk McKusick ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize); 386ec888383SKirk McKusick fs->fs_ckhash = save_ckhash; 387ec888383SKirk McKusick return (ckhash); 388ec888383SKirk McKusick } 389ec888383SKirk McKusick 390ec888383SKirk McKusick /* 391df8bae1dSRodney W. Grimes * Update the frsum fields to reflect addition or deletion 392df8bae1dSRodney W. Grimes * of some frags. 393df8bae1dSRodney W. Grimes */ 394df8bae1dSRodney W. Grimes void 39515c377c3SEd Maste ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 396df8bae1dSRodney W. Grimes { 397df8bae1dSRodney W. Grimes int inblk; 39805f4ff5dSPoul-Henning Kamp int field, subfield; 39905f4ff5dSPoul-Henning Kamp int siz, pos; 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 402df8bae1dSRodney W. Grimes fragmap <<= 1; 403df8bae1dSRodney W. Grimes for (siz = 1; siz < fs->fs_frag; siz++) { 404df8bae1dSRodney W. Grimes if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 405df8bae1dSRodney W. Grimes continue; 406df8bae1dSRodney W. Grimes field = around[siz]; 407df8bae1dSRodney W. Grimes subfield = inside[siz]; 408df8bae1dSRodney W. Grimes for (pos = siz; pos <= fs->fs_frag; pos++) { 409df8bae1dSRodney W. Grimes if ((fragmap & field) == subfield) { 410df8bae1dSRodney W. Grimes fraglist[siz] += cnt; 411df8bae1dSRodney W. Grimes pos += siz; 412df8bae1dSRodney W. Grimes field <<= siz; 413df8bae1dSRodney W. Grimes subfield <<= siz; 414df8bae1dSRodney W. Grimes } 415df8bae1dSRodney W. Grimes field <<= 1; 416df8bae1dSRodney W. Grimes subfield <<= 1; 417df8bae1dSRodney W. Grimes } 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes } 420df8bae1dSRodney W. Grimes 421df8bae1dSRodney W. Grimes /* 422df8bae1dSRodney W. Grimes * block operations 423df8bae1dSRodney W. Grimes * 424df8bae1dSRodney W. Grimes * check if a block is available 425df8bae1dSRodney W. Grimes */ 426df8bae1dSRodney W. Grimes int 42715c377c3SEd Maste ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 428df8bae1dSRodney W. Grimes { 429df8bae1dSRodney W. Grimes unsigned char mask; 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) { 432df8bae1dSRodney W. Grimes case 8: 433df8bae1dSRodney W. Grimes return (cp[h] == 0xff); 434df8bae1dSRodney W. Grimes case 4: 435df8bae1dSRodney W. Grimes mask = 0x0f << ((h & 0x1) << 2); 436df8bae1dSRodney W. Grimes return ((cp[h >> 1] & mask) == mask); 437df8bae1dSRodney W. Grimes case 2: 438df8bae1dSRodney W. Grimes mask = 0x03 << ((h & 0x3) << 1); 439df8bae1dSRodney W. Grimes return ((cp[h >> 2] & mask) == mask); 440df8bae1dSRodney W. Grimes case 1: 441df8bae1dSRodney W. Grimes mask = 0x01 << (h & 0x7); 442df8bae1dSRodney W. Grimes return ((cp[h >> 3] & mask) == mask); 443df8bae1dSRodney W. Grimes default: 444113db2ddSJeff Roberson #ifdef _KERNEL 445df8bae1dSRodney W. Grimes panic("ffs_isblock"); 446113db2ddSJeff Roberson #endif 447113db2ddSJeff Roberson break; 448113db2ddSJeff Roberson } 449113db2ddSJeff Roberson return (0); 450113db2ddSJeff Roberson } 451113db2ddSJeff Roberson 452113db2ddSJeff Roberson /* 453113db2ddSJeff Roberson * check if a block is free 454113db2ddSJeff Roberson */ 455113db2ddSJeff Roberson int 45615c377c3SEd Maste ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 457113db2ddSJeff Roberson { 458113db2ddSJeff Roberson 459113db2ddSJeff Roberson switch ((int)fs->fs_frag) { 460113db2ddSJeff Roberson case 8: 461113db2ddSJeff Roberson return (cp[h] == 0); 462113db2ddSJeff Roberson case 4: 463113db2ddSJeff Roberson return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 464113db2ddSJeff Roberson case 2: 465113db2ddSJeff Roberson return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 466113db2ddSJeff Roberson case 1: 467113db2ddSJeff Roberson return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 468113db2ddSJeff Roberson default: 469113db2ddSJeff Roberson #ifdef _KERNEL 470113db2ddSJeff Roberson panic("ffs_isfreeblock"); 471113db2ddSJeff Roberson #endif 472113db2ddSJeff Roberson break; 473df8bae1dSRodney W. Grimes } 474589c7af9SKirk McKusick return (0); 475df8bae1dSRodney W. Grimes } 476df8bae1dSRodney W. Grimes 477df8bae1dSRodney W. Grimes /* 478df8bae1dSRodney W. Grimes * take a block out of the map 479df8bae1dSRodney W. Grimes */ 480df8bae1dSRodney W. Grimes void 48115c377c3SEd Maste ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h) 482df8bae1dSRodney W. Grimes { 483df8bae1dSRodney W. Grimes 484df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) { 485df8bae1dSRodney W. Grimes case 8: 486df8bae1dSRodney W. Grimes cp[h] = 0; 487df8bae1dSRodney W. Grimes return; 488df8bae1dSRodney W. Grimes case 4: 489df8bae1dSRodney W. Grimes cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 490df8bae1dSRodney W. Grimes return; 491df8bae1dSRodney W. Grimes case 2: 492df8bae1dSRodney W. Grimes cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 493df8bae1dSRodney W. Grimes return; 494df8bae1dSRodney W. Grimes case 1: 495df8bae1dSRodney W. Grimes cp[h >> 3] &= ~(0x01 << (h & 0x7)); 496df8bae1dSRodney W. Grimes return; 497df8bae1dSRodney W. Grimes default: 498113db2ddSJeff Roberson #ifdef _KERNEL 499df8bae1dSRodney W. Grimes panic("ffs_clrblock"); 500113db2ddSJeff Roberson #endif 501113db2ddSJeff Roberson break; 502df8bae1dSRodney W. Grimes } 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes 505df8bae1dSRodney W. Grimes /* 506df8bae1dSRodney W. Grimes * put a block into the map 507df8bae1dSRodney W. Grimes */ 508df8bae1dSRodney W. Grimes void 50915c377c3SEd Maste ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 510df8bae1dSRodney W. Grimes { 511df8bae1dSRodney W. Grimes 512df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) { 513df8bae1dSRodney W. Grimes 514df8bae1dSRodney W. Grimes case 8: 515df8bae1dSRodney W. Grimes cp[h] = 0xff; 516df8bae1dSRodney W. Grimes return; 517df8bae1dSRodney W. Grimes case 4: 518df8bae1dSRodney W. Grimes cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 519df8bae1dSRodney W. Grimes return; 520df8bae1dSRodney W. Grimes case 2: 521df8bae1dSRodney W. Grimes cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 522df8bae1dSRodney W. Grimes return; 523df8bae1dSRodney W. Grimes case 1: 524df8bae1dSRodney W. Grimes cp[h >> 3] |= (0x01 << (h & 0x7)); 525df8bae1dSRodney W. Grimes return; 526df8bae1dSRodney W. Grimes default: 527113db2ddSJeff Roberson #ifdef _KERNEL 528df8bae1dSRodney W. Grimes panic("ffs_setblock"); 529113db2ddSJeff Roberson #endif 530113db2ddSJeff Roberson break; 531df8bae1dSRodney W. Grimes } 532df8bae1dSRodney W. Grimes } 533113db2ddSJeff Roberson 534113db2ddSJeff Roberson /* 535113db2ddSJeff Roberson * Update the cluster map because of an allocation or free. 536113db2ddSJeff Roberson * 537113db2ddSJeff Roberson * Cnt == 1 means free; cnt == -1 means allocating. 538113db2ddSJeff Roberson */ 539113db2ddSJeff Roberson void 54015c377c3SEd Maste ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt) 541113db2ddSJeff Roberson { 542113db2ddSJeff Roberson int32_t *sump; 543113db2ddSJeff Roberson int32_t *lp; 544113db2ddSJeff Roberson u_char *freemapp, *mapp; 545e1c27cf7SKirk McKusick int i, start, end, forw, back, map; 546e1c27cf7SKirk McKusick u_int bit; 547113db2ddSJeff Roberson 548113db2ddSJeff Roberson if (fs->fs_contigsumsize <= 0) 549113db2ddSJeff Roberson return; 550113db2ddSJeff Roberson freemapp = cg_clustersfree(cgp); 551113db2ddSJeff Roberson sump = cg_clustersum(cgp); 552113db2ddSJeff Roberson /* 553113db2ddSJeff Roberson * Allocate or clear the actual block. 554113db2ddSJeff Roberson */ 555113db2ddSJeff Roberson if (cnt > 0) 556113db2ddSJeff Roberson setbit(freemapp, blkno); 557113db2ddSJeff Roberson else 558113db2ddSJeff Roberson clrbit(freemapp, blkno); 559113db2ddSJeff Roberson /* 560113db2ddSJeff Roberson * Find the size of the cluster going forward. 561113db2ddSJeff Roberson */ 562113db2ddSJeff Roberson start = blkno + 1; 563113db2ddSJeff Roberson end = start + fs->fs_contigsumsize; 564113db2ddSJeff Roberson if (end >= cgp->cg_nclusterblks) 565113db2ddSJeff Roberson end = cgp->cg_nclusterblks; 566113db2ddSJeff Roberson mapp = &freemapp[start / NBBY]; 567113db2ddSJeff Roberson map = *mapp++; 568e1c27cf7SKirk McKusick bit = 1U << (start % NBBY); 569113db2ddSJeff Roberson for (i = start; i < end; i++) { 570113db2ddSJeff Roberson if ((map & bit) == 0) 571113db2ddSJeff Roberson break; 572113db2ddSJeff Roberson if ((i & (NBBY - 1)) != (NBBY - 1)) { 573113db2ddSJeff Roberson bit <<= 1; 574113db2ddSJeff Roberson } else { 575113db2ddSJeff Roberson map = *mapp++; 576113db2ddSJeff Roberson bit = 1; 577113db2ddSJeff Roberson } 578113db2ddSJeff Roberson } 579113db2ddSJeff Roberson forw = i - start; 580113db2ddSJeff Roberson /* 581113db2ddSJeff Roberson * Find the size of the cluster going backward. 582113db2ddSJeff Roberson */ 583113db2ddSJeff Roberson start = blkno - 1; 584113db2ddSJeff Roberson end = start - fs->fs_contigsumsize; 585113db2ddSJeff Roberson if (end < 0) 586113db2ddSJeff Roberson end = -1; 587113db2ddSJeff Roberson mapp = &freemapp[start / NBBY]; 588113db2ddSJeff Roberson map = *mapp--; 589e1c27cf7SKirk McKusick bit = 1U << (start % NBBY); 590113db2ddSJeff Roberson for (i = start; i > end; i--) { 591113db2ddSJeff Roberson if ((map & bit) == 0) 592113db2ddSJeff Roberson break; 593113db2ddSJeff Roberson if ((i & (NBBY - 1)) != 0) { 594113db2ddSJeff Roberson bit >>= 1; 595113db2ddSJeff Roberson } else { 596113db2ddSJeff Roberson map = *mapp--; 597e1c27cf7SKirk McKusick bit = 1U << (NBBY - 1); 598113db2ddSJeff Roberson } 599113db2ddSJeff Roberson } 600113db2ddSJeff Roberson back = start - i; 601113db2ddSJeff Roberson /* 602113db2ddSJeff Roberson * Account for old cluster and the possibly new forward and 603113db2ddSJeff Roberson * back clusters. 604113db2ddSJeff Roberson */ 605113db2ddSJeff Roberson i = back + forw + 1; 606113db2ddSJeff Roberson if (i > fs->fs_contigsumsize) 607113db2ddSJeff Roberson i = fs->fs_contigsumsize; 608113db2ddSJeff Roberson sump[i] += cnt; 609113db2ddSJeff Roberson if (back > 0) 610113db2ddSJeff Roberson sump[back] -= cnt; 611113db2ddSJeff Roberson if (forw > 0) 612113db2ddSJeff Roberson sump[forw] -= cnt; 613113db2ddSJeff Roberson /* 614113db2ddSJeff Roberson * Update cluster summary information. 615113db2ddSJeff Roberson */ 616113db2ddSJeff Roberson lp = &sump[fs->fs_contigsumsize]; 617113db2ddSJeff Roberson for (i = fs->fs_contigsumsize; i > 0; i--) 618113db2ddSJeff Roberson if (*lp-- > 0) 619113db2ddSJeff Roberson break; 620113db2ddSJeff Roberson fs->fs_maxcluster[cgp->cg_cgx] = i; 621113db2ddSJeff Roberson } 622