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 * $FreeBSD: src/sys/ufs/ffs/ffs_subr.c,v 1.25 1999/12/29 04:55:04 peter Exp $ 35 * $DragonFly: src/sys/vfs/ufs/ffs_subr.c,v 1.13 2006/05/26 19:57:33 dillon Exp $ 36 */ 37 38 #include <sys/param.h> 39 40 #ifndef _KERNEL 41 #include "dinode.h" 42 #include "fs.h" 43 #else 44 #include "opt_ddb.h" 45 46 #include <sys/systm.h> 47 #include <sys/lock.h> 48 #include <sys/vnode.h> 49 #include <sys/buf.h> 50 #include <sys/ucred.h> 51 #include <sys/mount.h> 52 53 #include "quota.h" 54 #include "inode.h" 55 #include "fs.h" 56 #include "ffs_extern.h" 57 58 /* 59 * Return buffer with the contents of block "offset" from the beginning of 60 * vnode "vp". If "res" is non-zero, fill it in with a pointer to the 61 * remaining space in the vnode. 62 */ 63 int 64 ffs_blkatoff(struct vnode *vp, off_t uoffset, char **res, struct buf **bpp) 65 { 66 struct inode *ip; 67 struct fs *fs; 68 struct buf *bp; 69 ufs_daddr_t lbn; 70 int bsize, error; 71 72 ip = VTOI(vp); 73 fs = ip->i_fs; 74 lbn = lblkno(fs, uoffset); 75 bsize = blksize(fs, ip, lbn); 76 77 *bpp = NULL; 78 error = bread(vp, lblktodoff(fs, lbn), bsize, &bp); 79 if (error) { 80 brelse(bp); 81 return (error); 82 } 83 if (res) 84 *res = (char *)bp->b_data + blkoff(fs, uoffset); 85 *bpp = bp; 86 return (0); 87 } 88 89 /* 90 * Return buffer with the contents of block "offset" from the beginning of 91 * vnode "vp". If "res" is non-zero, fill it in with a pointer to the 92 * remaining space in the vnode. 93 * 94 * This version includes a read-ahead optimization. 95 */ 96 int 97 ffs_blkatoff_ra(struct vnode *vp, off_t uoffset, char **res, struct buf **bpp, 98 int seqcount) 99 { 100 struct inode *ip; 101 struct fs *fs; 102 struct buf *bp; 103 ufs_daddr_t lbn; 104 ufs_daddr_t nextlbn; 105 off_t base_loffset; 106 off_t next_loffset; 107 int bsize, error; 108 int nextbsize; 109 110 ip = VTOI(vp); 111 fs = ip->i_fs; 112 lbn = lblkno(fs, uoffset); 113 base_loffset = lblktodoff(fs, lbn); 114 bsize = blksize(fs, ip, lbn); 115 116 nextlbn = lbn + 1; 117 next_loffset = lblktodoff(fs, nextlbn); 118 119 120 *bpp = NULL; 121 122 if (next_loffset >= ip->i_size) { 123 /* 124 * Do not do readahead if this is the last block 125 */ 126 error = bread(vp, base_loffset, bsize, &bp); 127 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 128 /* 129 * Try to cluster if we allowed to. 130 */ 131 error = cluster_read(vp, (off_t)ip->i_size, 132 base_loffset, bsize, 133 MAXBSIZE, seqcount, &bp); 134 } else if (seqcount > 1) { 135 /* 136 * Faked read ahead 137 */ 138 nextbsize = blksize(fs, ip, nextlbn); 139 error = breadn(vp, base_loffset, bsize, 140 &next_loffset, &nextbsize, 1, &bp); 141 } else { 142 /* 143 * Failing all of the above, just read what the 144 * user asked for. Interestingly, the same as 145 * the first option above. 146 */ 147 error = bread(vp, base_loffset, bsize, &bp); 148 } 149 if (error) { 150 brelse(bp); 151 return (error); 152 } 153 if (res) 154 *res = (char *)bp->b_data + (int)(uoffset - base_loffset); 155 *bpp = bp; 156 return (0); 157 } 158 159 #endif 160 161 /* 162 * Update the frsum fields to reflect addition or deletion 163 * of some frags. 164 */ 165 void 166 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt) 167 { 168 int inblk; 169 int field, subfield; 170 int siz, pos; 171 172 /* 173 * inblk represents a bitmap of fragment sizes which may be 174 * contained in the data 'fragmap'. e.g. if a fragment of size 175 * 1 is available, bit 0 would be set. inblk is shifted left 176 * by one so we do not have to calculate (1 << (siz - 1)). 177 * 178 * fragment represents the data pattern we are trying to decipher, 179 * we shift it left by one to align it with the 'around' and 'inside' 180 * masks. 181 * 182 * around represents the bits around the subfield and is a mask. 183 * inside represents what we must match within the mask, it is 184 * basically the mask with the first and last bit set to 0, allowing 185 * us to represent a whole fragment. 186 * 187 * When we find a match we bump our position by the size of the 188 * matching fragment, then bump the position again: 189 * 190 * 010101010 fragmap (shifted left by 1) 191 * 111 around mask 192 * 010 inside mask 193 * 111 (shifted by siz) 194 * 010 195 * 111 (shifted again) 196 * 010 197 */ 198 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1; 199 fragmap <<= 1; 200 for (siz = 1; siz < fs->fs_frag; siz++) { 201 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0) 202 continue; 203 field = around[siz]; 204 subfield = inside[siz]; 205 for (pos = siz; pos <= fs->fs_frag; pos++) { 206 if ((fragmap & field) == subfield) { 207 fraglist[siz] += cnt; 208 pos += siz; 209 field <<= siz; 210 subfield <<= siz; 211 } 212 field <<= 1; 213 subfield <<= 1; 214 } 215 } 216 } 217 218 /* 219 * block operations 220 * 221 * check if a block is available 222 */ 223 int 224 ffs_isblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h) 225 { 226 unsigned char mask; 227 228 switch ((int)fs->fs_frag) { 229 case 8: 230 return (cp[h] == 0xff); 231 case 4: 232 mask = 0x0f << ((h & 0x1) << 2); 233 return ((cp[h >> 1] & mask) == mask); 234 case 2: 235 mask = 0x03 << ((h & 0x3) << 1); 236 return ((cp[h >> 2] & mask) == mask); 237 case 1: 238 mask = 0x01 << (h & 0x7); 239 return ((cp[h >> 3] & mask) == mask); 240 default: 241 panic("ffs_isblock"); 242 } 243 } 244 245 /* 246 * check if a block is free 247 */ 248 int 249 ffs_isfreeblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h) 250 { 251 switch ((int)fs->fs_frag) { 252 case 8: 253 return (cp[h] == 0); 254 case 4: 255 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 256 case 2: 257 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 258 case 1: 259 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 260 default: 261 panic("ffs_isfreeblock"); 262 } 263 } 264 265 /* 266 * take a block out of the map 267 */ 268 void 269 ffs_clrblock(struct fs *fs, u_char *cp, ufs_daddr_t h) 270 { 271 switch ((int)fs->fs_frag) { 272 case 8: 273 cp[h] = 0; 274 return; 275 case 4: 276 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 277 return; 278 case 2: 279 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 280 return; 281 case 1: 282 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 283 return; 284 default: 285 panic("ffs_clrblock"); 286 } 287 } 288 289 /* 290 * put a block into the map 291 */ 292 void 293 ffs_setblock(struct fs *fs, unsigned char *cp, ufs_daddr_t h) 294 { 295 switch ((int)fs->fs_frag) { 296 case 8: 297 cp[h] = 0xff; 298 return; 299 case 4: 300 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 301 return; 302 case 2: 303 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 304 return; 305 case 1: 306 cp[h >> 3] |= (0x01 << (h & 0x7)); 307 return; 308 default: 309 panic("ffs_setblock"); 310 } 311 } 312