1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. 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 * $Id: ffs_subr.c,v 1.11 1997/02/22 09:47:04 peter Exp $ 35 */ 36 37 #include <sys/param.h> 38 39 #ifndef KERNEL 40 #include <ufs/ufs/dinode.h> 41 #include <ufs/ffs/fs.h> 42 #else 43 #include "opt_ddb.h" 44 45 #include <sys/systm.h> 46 #include <sys/vnode.h> 47 #include <sys/buf.h> 48 #include <ufs/ufs/quota.h> 49 #include <ufs/ufs/inode.h> 50 #include <ufs/ufs/dinode.h> 51 #include <ufs/ffs/fs.h> 52 #include <ufs/ffs/ffs_extern.h> 53 54 /* 55 * Return buffer with the contents of block "offset" from the beginning of 56 * directory "ip". If "res" is non-zero, fill it in with a pointer to the 57 * remaining space in the directory. 58 */ 59 int 60 ffs_blkatoff(ap) 61 struct vop_blkatoff_args /* { 62 struct vnode *a_vp; 63 off_t a_offset; 64 char **a_res; 65 struct buf **a_bpp; 66 } */ *ap; 67 { 68 struct inode *ip; 69 register struct fs *fs; 70 struct buf *bp; 71 ufs_daddr_t lbn; 72 int bsize, error; 73 74 ip = VTOI(ap->a_vp); 75 fs = ip->i_fs; 76 lbn = lblkno(fs, ap->a_offset); 77 bsize = blksize(fs, ip, lbn); 78 79 *ap->a_bpp = NULL; 80 error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp); 81 if (error) { 82 brelse(bp); 83 return (error); 84 } 85 if (ap->a_res) 86 *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset); 87 *ap->a_bpp = bp; 88 return (0); 89 } 90 #endif 91 92 /* 93 * Update the frsum fields to reflect addition or deletion 94 * of some frags. 95 */ 96 void 97 ffs_fragacct(fs, fragmap, fraglist, cnt) 98 struct fs *fs; 99 int fragmap; 100 int32_t fraglist[]; 101 int cnt; 102 { 103 int inblk; 104 register int field, subfield; 105 register int siz, pos; 106 107 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 108 fragmap <<= 1; 109 for (siz = 1; siz < fs->fs_frag; siz++) { 110 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 111 continue; 112 field = around[siz]; 113 subfield = inside[siz]; 114 for (pos = siz; pos <= fs->fs_frag; pos++) { 115 if ((fragmap & field) == subfield) { 116 fraglist[siz] += cnt; 117 pos += siz; 118 field <<= siz; 119 subfield <<= siz; 120 } 121 field <<= 1; 122 subfield <<= 1; 123 } 124 } 125 } 126 127 #if defined(KERNEL) && defined(DIAGNOSTIC) && defined(DDB) 128 void 129 ffs_checkoverlap(bp, ip) 130 struct buf *bp; 131 struct inode *ip; 132 { 133 register struct buf *ebp, *ep; 134 register ufs_daddr_t start, last; 135 struct vnode *vp; 136 137 ebp = &buf[nbuf]; 138 start = bp->b_blkno; 139 last = start + btodb(bp->b_bcount) - 1; 140 for (ep = buf; ep < ebp; ep++) { 141 if (ep == bp || (ep->b_flags & B_INVAL) || 142 ep->b_vp == NULLVP) 143 continue; 144 if (VOP_BMAP(ep->b_vp, (ufs_daddr_t)0, &vp, (ufs_daddr_t)0, 145 NULL, NULL)) 146 continue; 147 if (vp != ip->i_devvp) 148 continue; 149 /* look for overlap */ 150 if (ep->b_bcount == 0 || ep->b_blkno > last || 151 ep->b_blkno + btodb(ep->b_bcount) <= start) 152 continue; 153 vprint("Disk overlap", vp); 154 (void)printf("\tstart %lu, end %lu overlap start %lu, end %lu\n", 155 (u_long)start, (u_long)last, (u_long)ep->b_blkno, 156 (u_long)(ep->b_blkno + btodb(ep->b_bcount) - 1)); 157 panic("ffs_checkoverlap: Disk buffer overlap"); 158 } 159 } 160 #endif /* DIAGNOSTIC */ 161 162 /* 163 * block operations 164 * 165 * check if a block is available 166 */ 167 int 168 ffs_isblock(fs, cp, h) 169 struct fs *fs; 170 unsigned char *cp; 171 ufs_daddr_t h; 172 { 173 unsigned char mask; 174 175 switch ((int)fs->fs_frag) { 176 case 8: 177 return (cp[h] == 0xff); 178 case 4: 179 mask = 0x0f << ((h & 0x1) << 2); 180 return ((cp[h >> 1] & mask) == mask); 181 case 2: 182 mask = 0x03 << ((h & 0x3) << 1); 183 return ((cp[h >> 2] & mask) == mask); 184 case 1: 185 mask = 0x01 << (h & 0x7); 186 return ((cp[h >> 3] & mask) == mask); 187 default: 188 panic("ffs_isblock"); 189 } 190 } 191 192 /* 193 * take a block out of the map 194 */ 195 void 196 ffs_clrblock(fs, cp, h) 197 struct fs *fs; 198 u_char *cp; 199 ufs_daddr_t h; 200 { 201 202 switch ((int)fs->fs_frag) { 203 case 8: 204 cp[h] = 0; 205 return; 206 case 4: 207 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 208 return; 209 case 2: 210 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 211 return; 212 case 1: 213 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 214 return; 215 default: 216 panic("ffs_clrblock"); 217 } 218 } 219 220 /* 221 * put a block into the map 222 */ 223 void 224 ffs_setblock(fs, cp, h) 225 struct fs *fs; 226 unsigned char *cp; 227 ufs_daddr_t h; 228 { 229 230 switch ((int)fs->fs_frag) { 231 232 case 8: 233 cp[h] = 0xff; 234 return; 235 case 4: 236 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 237 return; 238 case 2: 239 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 240 return; 241 case 1: 242 cp[h >> 3] |= (0x01 << (h & 0x7)); 243 return; 244 default: 245 panic("ffs_setblock"); 246 } 247 } 248