1 /* $NetBSD: ffs_subr.c,v 1.32 2003/12/30 12:33:24 pk Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 1982, 1986, 1989, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/types.h> 41 42 #include <ufs/ufs/dinode.h> 43 #include <ufs/ffs/fs.h> 44 #include "ffs/ffs_extern.h" 45 #include "ffs/ufs_bswap.h" 46 47 /* 48 * Update the frsum fields to reflect addition or deletion 49 * of some frags. 50 */ 51 void 52 ffs_fragacct_swap(struct fs *fs, int fragmap, int32_t fraglist[], int cnt, int needswap) 53 { 54 int inblk; 55 int field, subfield; 56 int siz, pos; 57 58 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 59 fragmap <<= 1; 60 for (siz = 1; siz < fs->fs_frag; siz++) { 61 if ((inblk & (1 << (siz + (fs->fs_frag & (NBBY - 1))))) == 0) 62 continue; 63 field = around[siz]; 64 subfield = inside[siz]; 65 for (pos = siz; pos <= fs->fs_frag; pos++) { 66 if ((fragmap & field) == subfield) { 67 fraglist[siz] = ufs_rw32( 68 ufs_rw32(fraglist[siz], needswap) + cnt, 69 needswap); 70 pos += siz; 71 field <<= siz; 72 subfield <<= siz; 73 } 74 field <<= 1; 75 subfield <<= 1; 76 } 77 } 78 } 79 80 /* 81 * block operations 82 * 83 * check if a block is available 84 * returns true if all the corresponding bits in the free map are 1 85 * returns false if any corresponding bit in the free map is 0 86 */ 87 int 88 ffs_isblock(fs, cp, h) 89 struct fs *fs; 90 u_char *cp; 91 int32_t h; 92 { 93 u_char mask; 94 95 switch ((int)fs->fs_fragshift) { 96 case 3: 97 return (cp[h] == 0xff); 98 case 2: 99 mask = 0x0f << ((h & 0x1) << 2); 100 return ((cp[h >> 1] & mask) == mask); 101 case 1: 102 mask = 0x03 << ((h & 0x3) << 1); 103 return ((cp[h >> 2] & mask) == mask); 104 case 0: 105 mask = 0x01 << (h & 0x7); 106 return ((cp[h >> 3] & mask) == mask); 107 default: 108 panic("ffs_isblock: unknown fs_fragshift %d", 109 (int)fs->fs_fragshift); 110 } 111 } 112 113 /* 114 * check if a block is completely allocated 115 * returns true if all the corresponding bits in the free map are 0 116 * returns false if any corresponding bit in the free map is 1 117 */ 118 int 119 ffs_isfreeblock(fs, cp, h) 120 struct fs *fs; 121 u_char *cp; 122 int32_t h; 123 { 124 125 switch ((int)fs->fs_fragshift) { 126 case 3: 127 return (cp[h] == 0); 128 case 2: 129 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 130 case 1: 131 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 132 case 0: 133 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 134 default: 135 panic("ffs_isfreeblock: unknown fs_fragshift %d", 136 (int)fs->fs_fragshift); 137 } 138 } 139 140 /* 141 * take a block out of the map 142 */ 143 void 144 ffs_clrblock(fs, cp, h) 145 struct fs *fs; 146 u_char *cp; 147 int32_t h; 148 { 149 150 switch ((int)fs->fs_fragshift) { 151 case 3: 152 cp[h] = 0; 153 return; 154 case 2: 155 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 156 return; 157 case 1: 158 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 159 return; 160 case 0: 161 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 162 return; 163 default: 164 panic("ffs_clrblock: unknown fs_fragshift %d", 165 (int)fs->fs_fragshift); 166 } 167 } 168 169 /* 170 * put a block into the map 171 */ 172 void 173 ffs_setblock(fs, cp, h) 174 struct fs *fs; 175 u_char *cp; 176 int32_t h; 177 { 178 179 switch ((int)fs->fs_fragshift) { 180 case 3: 181 cp[h] = 0xff; 182 return; 183 case 2: 184 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 185 return; 186 case 1: 187 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 188 return; 189 case 0: 190 cp[h >> 3] |= (0x01 << (h & 0x7)); 191 return; 192 default: 193 panic("ffs_setblock: unknown fs_fragshift %d", 194 (int)fs->fs_fragshift); 195 } 196 } 197