1 /* $NetBSD: ffs_alloc.c,v 1.14 2004/06/20 22:20:18 jmc Exp $ */ 2 /* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem Exp */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-3-Clause 6 * 7 * Copyright (c) 2002 Networks Associates Technology, Inc. 8 * All rights reserved. 9 * 10 * This software was developed for the FreeBSD Project by Marshall 11 * Kirk McKusick and Network Associates Laboratories, the Security 12 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 13 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 14 * research program 15 * 16 * Copyright (c) 1982, 1986, 1989, 1993 17 * The Regents of the University of California. All rights reserved. 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 3. Neither the name of the University nor the names of its contributors 28 * may be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41 * SUCH DAMAGE. 42 * 43 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95 44 */ 45 46 #include <sys/cdefs.h> 47 #include <sys/param.h> 48 #include <sys/time.h> 49 50 #include <errno.h> 51 #include <stdint.h> 52 53 #include "makefs.h" 54 55 #include <ufs/ufs/dinode.h> 56 #include <ufs/ffs/fs.h> 57 58 #include "ffs/ufs_bswap.h" 59 #include "ffs/buf.h" 60 #include "ffs/ufs_inode.h" 61 #include "ffs/ffs_extern.h" 62 63 static int scanc(u_int, const u_char *, const u_char *, int); 64 65 static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int); 66 static daddr_t ffs_alloccgblk(struct inode *, struct m_buf *, daddr_t); 67 static daddr_t ffs_hashalloc(struct inode *, u_int, daddr_t, int, 68 daddr_t (*)(struct inode *, int, daddr_t, int)); 69 static int32_t ffs_mapsearch(struct fs *, struct cg *, daddr_t, int); 70 71 /* 72 * Allocate a block in the file system. 73 * 74 * The size of the requested block is given, which must be some 75 * multiple of fs_fsize and <= fs_bsize. 76 * A preference may be optionally specified. If a preference is given 77 * the following hierarchy is used to allocate a block: 78 * 1) allocate the requested block. 79 * 2) allocate a rotationally optimal block in the same cylinder. 80 * 3) allocate a block in the same cylinder group. 81 * 4) quadratically rehash into other cylinder groups, until an 82 * available block is located. 83 * If no block preference is given the following hierarchy is used 84 * to allocate a block: 85 * 1) allocate a block in the cylinder group that contains the 86 * inode for the file. 87 * 2) quadratically rehash into other cylinder groups, until an 88 * available block is located. 89 */ 90 int 91 ffs_alloc(struct inode *ip, daddr_t lbn __unused, daddr_t bpref, int size, 92 daddr_t *bnp) 93 { 94 struct fs *fs = ip->i_fs; 95 daddr_t bno; 96 int cg; 97 98 *bnp = 0; 99 if (size > fs->fs_bsize || fragoff(fs, size) != 0) { 100 errx(1, "ffs_alloc: bad size: bsize %d size %d", 101 fs->fs_bsize, size); 102 } 103 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 104 goto nospace; 105 if (bpref >= fs->fs_size) 106 bpref = 0; 107 if (bpref == 0) 108 cg = ino_to_cg(fs, ip->i_number); 109 else 110 cg = dtog(fs, bpref); 111 bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg); 112 if (bno > 0) { 113 if (ip->i_fs->fs_magic == FS_UFS1_MAGIC) 114 ip->i_ffs1_blocks += size / DEV_BSIZE; 115 else 116 ip->i_ffs2_blocks += size / DEV_BSIZE; 117 *bnp = bno; 118 return (0); 119 } 120 nospace: 121 return (ENOSPC); 122 } 123 124 /* 125 * Select the desired position for the next block in a file. The file is 126 * logically divided into sections. The first section is composed of the 127 * direct blocks. Each additional section contains fs_maxbpg blocks. 128 * 129 * If no blocks have been allocated in the first section, the policy is to 130 * request a block in the same cylinder group as the inode that describes 131 * the file. If no blocks have been allocated in any other section, the 132 * policy is to place the section in a cylinder group with a greater than 133 * average number of free blocks. An appropriate cylinder group is found 134 * by using a rotor that sweeps the cylinder groups. When a new group of 135 * blocks is needed, the sweep begins in the cylinder group following the 136 * cylinder group from which the previous allocation was made. The sweep 137 * continues until a cylinder group with greater than the average number 138 * of free blocks is found. If the allocation is for the first block in an 139 * indirect block, the information on the previous allocation is unavailable; 140 * here a best guess is made based upon the logical block number being 141 * allocated. 142 * 143 * If a section is already partially allocated, the policy is to 144 * contiguously allocate fs_maxcontig blocks. The end of one of these 145 * contiguous blocks and the beginning of the next is physically separated 146 * so that the disk head will be in transit between them for at least 147 * fs_rotdelay milliseconds. This is to allow time for the processor to 148 * schedule another I/O transfer. 149 */ 150 /* XXX ondisk32 */ 151 daddr_t 152 ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int32_t *bap) 153 { 154 struct fs *fs; 155 u_int cg, startcg; 156 int avgbfree; 157 158 fs = ip->i_fs; 159 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 160 if (lbn < UFS_NDADDR + NINDIR(fs)) { 161 cg = ino_to_cg(fs, ip->i_number); 162 return (fs->fs_fpg * cg + fs->fs_frag); 163 } 164 /* 165 * Find a cylinder with greater than average number of 166 * unused data blocks. 167 */ 168 if (indx == 0 || bap[indx - 1] == 0) 169 startcg = 170 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 171 else 172 startcg = dtog(fs, 173 ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1); 174 startcg %= fs->fs_ncg; 175 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 176 for (cg = startcg; cg < fs->fs_ncg; cg++) 177 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) 178 return (fs->fs_fpg * cg + fs->fs_frag); 179 for (cg = 0; cg <= startcg; cg++) 180 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) 181 return (fs->fs_fpg * cg + fs->fs_frag); 182 return (0); 183 } 184 /* 185 * We just always try to lay things out contiguously. 186 */ 187 return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag; 188 } 189 190 daddr_t 191 ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int64_t *bap) 192 { 193 struct fs *fs; 194 u_int cg, startcg; 195 int avgbfree; 196 197 fs = ip->i_fs; 198 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 199 if (lbn < UFS_NDADDR + NINDIR(fs)) { 200 cg = ino_to_cg(fs, ip->i_number); 201 return (fs->fs_fpg * cg + fs->fs_frag); 202 } 203 /* 204 * Find a cylinder with greater than average number of 205 * unused data blocks. 206 */ 207 if (indx == 0 || bap[indx - 1] == 0) 208 startcg = 209 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 210 else 211 startcg = dtog(fs, 212 ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1); 213 startcg %= fs->fs_ncg; 214 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 215 for (cg = startcg; cg < fs->fs_ncg; cg++) 216 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 217 return (fs->fs_fpg * cg + fs->fs_frag); 218 } 219 for (cg = 0; cg < startcg; cg++) 220 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 221 return (fs->fs_fpg * cg + fs->fs_frag); 222 } 223 return (0); 224 } 225 /* 226 * We just always try to lay things out contiguously. 227 */ 228 return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag; 229 } 230 231 /* 232 * Implement the cylinder overflow algorithm. 233 * 234 * The policy implemented by this algorithm is: 235 * 1) allocate the block in its requested cylinder group. 236 * 2) quadratically rehash on the cylinder group number. 237 * 3) brute force search for a free block. 238 * 239 * `size': size for data blocks, mode for inodes 240 */ 241 /*VARARGS5*/ 242 static daddr_t 243 ffs_hashalloc(struct inode *ip, u_int cg, daddr_t pref, int size, 244 daddr_t (*allocator)(struct inode *, int, daddr_t, int)) 245 { 246 struct fs *fs; 247 daddr_t result; 248 u_int i, icg = cg; 249 250 fs = ip->i_fs; 251 /* 252 * 1: preferred cylinder group 253 */ 254 result = (*allocator)(ip, cg, pref, size); 255 if (result) 256 return (result); 257 /* 258 * 2: quadratic rehash 259 */ 260 for (i = 1; i < fs->fs_ncg; i *= 2) { 261 cg += i; 262 if (cg >= fs->fs_ncg) 263 cg -= fs->fs_ncg; 264 result = (*allocator)(ip, cg, 0, size); 265 if (result) 266 return (result); 267 } 268 /* 269 * 3: brute force search 270 * Note that we start at i == 2, since 0 was checked initially, 271 * and 1 is always checked in the quadratic rehash. 272 */ 273 cg = (icg + 2) % fs->fs_ncg; 274 for (i = 2; i < fs->fs_ncg; i++) { 275 result = (*allocator)(ip, cg, 0, size); 276 if (result) 277 return (result); 278 cg++; 279 if (cg == fs->fs_ncg) 280 cg = 0; 281 } 282 return (0); 283 } 284 285 /* 286 * Determine whether a block can be allocated. 287 * 288 * Check to see if a block of the appropriate size is available, 289 * and if it is, allocate it. 290 */ 291 static daddr_t 292 ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size) 293 { 294 struct cg *cgp; 295 struct m_buf *bp; 296 daddr_t bno, blkno; 297 int error, frags, allocsiz, i; 298 struct fs *fs = ip->i_fs; 299 const int needswap = UFS_FSNEEDSWAP(fs); 300 301 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 302 return (0); 303 error = bread((void *)ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 304 (int)fs->fs_cgsize, NULL, &bp); 305 if (error) { 306 return (0); 307 } 308 cgp = (struct cg *)bp->b_data; 309 if (!cg_chkmagic_swap(cgp, needswap) || 310 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 311 brelse(bp); 312 return (0); 313 } 314 if (size == fs->fs_bsize) { 315 bno = ffs_alloccgblk(ip, bp, bpref); 316 bdwrite(bp); 317 return (bno); 318 } 319 /* 320 * check to see if any fragments are already available 321 * allocsiz is the size which will be allocated, hacking 322 * it down to a smaller size if necessary 323 */ 324 frags = numfrags(fs, size); 325 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 326 if (cgp->cg_frsum[allocsiz] != 0) 327 break; 328 if (allocsiz == fs->fs_frag) { 329 /* 330 * no fragments were available, so a block will be 331 * allocated, and hacked up 332 */ 333 if (cgp->cg_cs.cs_nbfree == 0) { 334 brelse(bp); 335 return (0); 336 } 337 bno = ffs_alloccgblk(ip, bp, bpref); 338 bpref = dtogd(fs, bno); 339 for (i = frags; i < fs->fs_frag; i++) 340 setbit(cg_blksfree_swap(cgp, needswap), bpref + i); 341 i = fs->fs_frag - frags; 342 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap); 343 fs->fs_cstotal.cs_nffree += i; 344 fs->fs_cs(fs, cg).cs_nffree += i; 345 fs->fs_fmod = 1; 346 ufs_add32(cgp->cg_frsum[i], 1, needswap); 347 bdwrite(bp); 348 return (bno); 349 } 350 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 351 for (i = 0; i < frags; i++) 352 clrbit(cg_blksfree_swap(cgp, needswap), bno + i); 353 ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap); 354 fs->fs_cstotal.cs_nffree -= frags; 355 fs->fs_cs(fs, cg).cs_nffree -= frags; 356 fs->fs_fmod = 1; 357 ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap); 358 if (frags != allocsiz) 359 ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap); 360 blkno = cg * fs->fs_fpg + bno; 361 bdwrite(bp); 362 return blkno; 363 } 364 365 /* 366 * Allocate a block in a cylinder group. 367 * 368 * This algorithm implements the following policy: 369 * 1) allocate the requested block. 370 * 2) allocate a rotationally optimal block in the same cylinder. 371 * 3) allocate the next available block on the block rotor for the 372 * specified cylinder group. 373 * Note that this routine only allocates fs_bsize blocks; these 374 * blocks may be fragmented by the routine that allocates them. 375 */ 376 static daddr_t 377 ffs_alloccgblk(struct inode *ip, struct m_buf *bp, daddr_t bpref) 378 { 379 struct cg *cgp; 380 daddr_t blkno; 381 int32_t bno; 382 struct fs *fs = ip->i_fs; 383 const int needswap = UFS_FSNEEDSWAP(fs); 384 u_int8_t *blksfree_swap; 385 386 cgp = (struct cg *)bp->b_data; 387 blksfree_swap = cg_blksfree_swap(cgp, needswap); 388 if (bpref == 0 || (uint32_t)dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) { 389 bpref = ufs_rw32(cgp->cg_rotor, needswap); 390 } else { 391 bpref = blknum(fs, bpref); 392 bno = dtogd(fs, bpref); 393 /* 394 * if the requested block is available, use it 395 */ 396 if (ffs_isblock(fs, blksfree_swap, fragstoblks(fs, bno))) 397 goto gotit; 398 } 399 /* 400 * Take the next available one in this cylinder group. 401 */ 402 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 403 if (bno < 0) 404 return (0); 405 cgp->cg_rotor = ufs_rw32(bno, needswap); 406 gotit: 407 blkno = fragstoblks(fs, bno); 408 ffs_clrblock(fs, blksfree_swap, (long)blkno); 409 ffs_clusteracct(fs, cgp, blkno, -1); 410 ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap); 411 fs->fs_cstotal.cs_nbfree--; 412 fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--; 413 fs->fs_fmod = 1; 414 blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno; 415 return (blkno); 416 } 417 418 /* 419 * Free a block or fragment. 420 * 421 * The specified block or fragment is placed back in the 422 * free map. If a fragment is deallocated, a possible 423 * block reassembly is checked. 424 */ 425 void 426 ffs_blkfree(struct inode *ip, daddr_t bno, long size) 427 { 428 struct cg *cgp; 429 struct m_buf *bp; 430 int32_t fragno, cgbno; 431 int i, error, cg, blk, frags, bbase; 432 struct fs *fs = ip->i_fs; 433 const int needswap = UFS_FSNEEDSWAP(fs); 434 435 if (size > fs->fs_bsize || fragoff(fs, size) != 0 || 436 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) { 437 errx(1, "blkfree: bad size: bno %lld bsize %d size %ld", 438 (long long)bno, fs->fs_bsize, size); 439 } 440 cg = dtog(fs, bno); 441 if (bno >= fs->fs_size) { 442 warnx("bad block %lld, ino %ju", (long long)bno, 443 (uintmax_t)ip->i_number); 444 return; 445 } 446 error = bread((void *)ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 447 (int)fs->fs_cgsize, NULL, &bp); 448 if (error) { 449 return; 450 } 451 cgp = (struct cg *)bp->b_data; 452 if (!cg_chkmagic_swap(cgp, needswap)) { 453 brelse(bp); 454 return; 455 } 456 cgbno = dtogd(fs, bno); 457 if (size == fs->fs_bsize) { 458 fragno = fragstoblks(fs, cgbno); 459 if (!ffs_isfreeblock(fs, cg_blksfree_swap(cgp, needswap), fragno)) { 460 errx(1, "blkfree: freeing free block %lld", 461 (long long)bno); 462 } 463 ffs_setblock(fs, cg_blksfree_swap(cgp, needswap), fragno); 464 ffs_clusteracct(fs, cgp, fragno, 1); 465 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap); 466 fs->fs_cstotal.cs_nbfree++; 467 fs->fs_cs(fs, cg).cs_nbfree++; 468 } else { 469 bbase = cgbno - fragnum(fs, cgbno); 470 /* 471 * decrement the counts associated with the old frags 472 */ 473 blk = blkmap(fs, cg_blksfree_swap(cgp, needswap), bbase); 474 ffs_fragacct_swap(fs, blk, cgp->cg_frsum, -1, needswap); 475 /* 476 * deallocate the fragment 477 */ 478 frags = numfrags(fs, size); 479 for (i = 0; i < frags; i++) { 480 if (isset(cg_blksfree_swap(cgp, needswap), cgbno + i)) { 481 errx(1, "blkfree: freeing free frag: block %lld", 482 (long long)(cgbno + i)); 483 } 484 setbit(cg_blksfree_swap(cgp, needswap), cgbno + i); 485 } 486 ufs_add32(cgp->cg_cs.cs_nffree, i, needswap); 487 fs->fs_cstotal.cs_nffree += i; 488 fs->fs_cs(fs, cg).cs_nffree += i; 489 /* 490 * add back in counts associated with the new frags 491 */ 492 blk = blkmap(fs, cg_blksfree_swap(cgp, needswap), bbase); 493 ffs_fragacct_swap(fs, blk, cgp->cg_frsum, 1, needswap); 494 /* 495 * if a complete block has been reassembled, account for it 496 */ 497 fragno = fragstoblks(fs, bbase); 498 if (ffs_isblock(fs, cg_blksfree_swap(cgp, needswap), fragno)) { 499 ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap); 500 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 501 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 502 ffs_clusteracct(fs, cgp, fragno, 1); 503 ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap); 504 fs->fs_cstotal.cs_nbfree++; 505 fs->fs_cs(fs, cg).cs_nbfree++; 506 } 507 } 508 fs->fs_fmod = 1; 509 bdwrite(bp); 510 } 511 512 513 static int 514 scanc(u_int size, const u_char *cp, const u_char table[], int mask) 515 { 516 const u_char *end = &cp[size]; 517 518 while (cp < end && (table[*cp] & mask) == 0) 519 cp++; 520 return (end - cp); 521 } 522 523 /* 524 * Find a block of the specified size in the specified cylinder group. 525 * 526 * It is a panic if a request is made to find a block if none are 527 * available. 528 */ 529 static int32_t 530 ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz) 531 { 532 int32_t bno; 533 int start, len, loc, i; 534 int blk, field, subfield, pos; 535 int ostart, olen; 536 const int needswap = UFS_FSNEEDSWAP(fs); 537 538 /* 539 * find the fragment by searching through the free block 540 * map for an appropriate bit pattern 541 */ 542 if (bpref) 543 start = dtogd(fs, bpref) / NBBY; 544 else 545 start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY; 546 len = howmany(fs->fs_fpg, NBBY) - start; 547 ostart = start; 548 olen = len; 549 loc = scanc((u_int)len, 550 (const u_char *)&cg_blksfree_swap(cgp, needswap)[start], 551 (const u_char *)fragtbl[fs->fs_frag], 552 (1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 553 if (loc == 0) { 554 len = start + 1; 555 start = 0; 556 loc = scanc((u_int)len, 557 (const u_char *)&cg_blksfree_swap(cgp, needswap)[0], 558 (const u_char *)fragtbl[fs->fs_frag], 559 (1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 560 if (loc == 0) { 561 errx(1, 562 "ffs_alloccg: map corrupted: start %d len %d offset %d %ld", 563 ostart, olen, 564 ufs_rw32(cgp->cg_freeoff, needswap), 565 (long)cg_blksfree_swap(cgp, needswap) - (long)cgp); 566 /* NOTREACHED */ 567 } 568 } 569 bno = (start + len - loc) * NBBY; 570 cgp->cg_frotor = ufs_rw32(bno, needswap); 571 /* 572 * found the byte in the map 573 * sift through the bits to find the selected frag 574 */ 575 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 576 blk = blkmap(fs, cg_blksfree_swap(cgp, needswap), bno); 577 blk <<= 1; 578 field = around[allocsiz]; 579 subfield = inside[allocsiz]; 580 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 581 if ((blk & field) == subfield) 582 return (bno + pos); 583 field <<= 1; 584 subfield <<= 1; 585 } 586 } 587 errx(1, "ffs_alloccg: block not in map: bno %lld", (long long)bno); 588 return (-1); 589 } 590 591 /* 592 * Update the cluster map because of an allocation or free. 593 * 594 * Cnt == 1 means free; cnt == -1 means allocating. 595 */ 596 void 597 ffs_clusteracct(struct fs *fs, struct cg *cgp, int32_t blkno, int cnt) 598 { 599 int32_t *sump; 600 int32_t *lp; 601 u_char *freemapp, *mapp; 602 int i, start, end, forw, back, map, bit; 603 const int needswap = UFS_FSNEEDSWAP(fs); 604 605 if (fs->fs_contigsumsize <= 0) 606 return; 607 freemapp = cg_clustersfree_swap(cgp, needswap); 608 sump = cg_clustersum_swap(cgp, needswap); 609 /* 610 * Allocate or clear the actual block. 611 */ 612 if (cnt > 0) 613 setbit(freemapp, blkno); 614 else 615 clrbit(freemapp, blkno); 616 /* 617 * Find the size of the cluster going forward. 618 */ 619 start = blkno + 1; 620 end = start + fs->fs_contigsumsize; 621 if ((unsigned)end >= ufs_rw32(cgp->cg_nclusterblks, needswap)) 622 end = ufs_rw32(cgp->cg_nclusterblks, needswap); 623 mapp = &freemapp[start / NBBY]; 624 map = *mapp++; 625 bit = 1 << (start % NBBY); 626 for (i = start; i < end; i++) { 627 if ((map & bit) == 0) 628 break; 629 if ((i & (NBBY - 1)) != (NBBY - 1)) { 630 bit <<= 1; 631 } else { 632 map = *mapp++; 633 bit = 1; 634 } 635 } 636 forw = i - start; 637 /* 638 * Find the size of the cluster going backward. 639 */ 640 start = blkno - 1; 641 end = start - fs->fs_contigsumsize; 642 if (end < 0) 643 end = -1; 644 mapp = &freemapp[start / NBBY]; 645 map = *mapp--; 646 bit = 1 << (start % NBBY); 647 for (i = start; i > end; i--) { 648 if ((map & bit) == 0) 649 break; 650 if ((i & (NBBY - 1)) != 0) { 651 bit >>= 1; 652 } else { 653 map = *mapp--; 654 bit = 1 << (NBBY - 1); 655 } 656 } 657 back = start - i; 658 /* 659 * Account for old cluster and the possibly new forward and 660 * back clusters. 661 */ 662 i = back + forw + 1; 663 if (i > fs->fs_contigsumsize) 664 i = fs->fs_contigsumsize; 665 ufs_add32(sump[i], cnt, needswap); 666 if (back > 0) 667 ufs_add32(sump[back], -cnt, needswap); 668 if (forw > 0) 669 ufs_add32(sump[forw], -cnt, needswap); 670 671 /* 672 * Update cluster summary information. 673 */ 674 lp = &sump[fs->fs_contigsumsize]; 675 for (i = fs->fs_contigsumsize; i > 0; i--) 676 if (ufs_rw32(*lp--, needswap) > 0) 677 break; 678 fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i; 679 } 680