1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ffs_alloc.c 8.6 (Berkeley) 02/05/94 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/buf.h> 13 #include <sys/proc.h> 14 #include <sys/vnode.h> 15 #include <sys/mount.h> 16 #include <sys/kernel.h> 17 #include <sys/syslog.h> 18 19 #include <vm/vm.h> 20 21 #include <ufs/ufs/quota.h> 22 #include <ufs/ufs/inode.h> 23 24 #include <ufs/ffs/fs.h> 25 #include <ufs/ffs/ffs_extern.h> 26 27 extern u_long nextgennumber; 28 29 static daddr_t ffs_alloccg __P((struct inode *, int, daddr_t, int)); 30 static daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t)); 31 static daddr_t ffs_clusteralloc __P((struct inode *, int, daddr_t, int)); 32 static ino_t ffs_dirpref __P((struct fs *)); 33 static daddr_t ffs_fragextend __P((struct inode *, int, long, int, int)); 34 static void ffs_fserr __P((struct fs *, u_int, char *)); 35 static u_long ffs_hashalloc 36 __P((struct inode *, int, long, int, u_long (*)())); 37 static ino_t ffs_nodealloccg __P((struct inode *, int, daddr_t, int)); 38 static daddr_t ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int)); 39 40 /* 41 * Allocate a block in the file system. 42 * 43 * The size of the requested block is given, which must be some 44 * multiple of fs_fsize and <= fs_bsize. 45 * A preference may be optionally specified. If a preference is given 46 * the following hierarchy is used to allocate a block: 47 * 1) allocate the requested block. 48 * 2) allocate a rotationally optimal block in the same cylinder. 49 * 3) allocate a block in the same cylinder group. 50 * 4) quadradically rehash into other cylinder groups, until an 51 * available block is located. 52 * If no block preference is given the following heirarchy is used 53 * to allocate a block: 54 * 1) allocate a block in the cylinder group that contains the 55 * inode for the file. 56 * 2) quadradically rehash into other cylinder groups, until an 57 * available block is located. 58 */ 59 ffs_alloc(ip, lbn, bpref, size, cred, bnp) 60 register struct inode *ip; 61 daddr_t lbn, bpref; 62 int size; 63 struct ucred *cred; 64 daddr_t *bnp; 65 { 66 register struct fs *fs; 67 daddr_t bno; 68 int cg, error; 69 70 *bnp = 0; 71 fs = ip->i_fs; 72 #ifdef DIAGNOSTIC 73 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 74 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 75 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 76 panic("ffs_alloc: bad size"); 77 } 78 if (cred == NOCRED) 79 panic("ffs_alloc: missing credential\n"); 80 #endif /* DIAGNOSTIC */ 81 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 82 goto nospace; 83 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 84 goto nospace; 85 #ifdef QUOTA 86 if (error = chkdq(ip, (long)btodb(size), cred, 0)) 87 return (error); 88 #endif 89 if (bpref >= fs->fs_size) 90 bpref = 0; 91 if (bpref == 0) 92 cg = ino_to_cg(fs, ip->i_number); 93 else 94 cg = dtog(fs, bpref); 95 bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size, 96 (u_long (*)())ffs_alloccg); 97 if (bno > 0) { 98 ip->i_blocks += btodb(size); 99 ip->i_flag |= IN_CHANGE | IN_UPDATE; 100 *bnp = bno; 101 return (0); 102 } 103 #ifdef QUOTA 104 /* 105 * Restore user's disk quota because allocation failed. 106 */ 107 (void) chkdq(ip, (long)-btodb(size), cred, FORCE); 108 #endif 109 nospace: 110 ffs_fserr(fs, cred->cr_uid, "file system full"); 111 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 112 return (ENOSPC); 113 } 114 115 /* 116 * Reallocate a fragment to a bigger size 117 * 118 * The number and size of the old block is given, and a preference 119 * and new size is also specified. The allocator attempts to extend 120 * the original block. Failing that, the regular block allocator is 121 * invoked to get an appropriate block. 122 */ 123 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp) 124 register struct inode *ip; 125 daddr_t lbprev; 126 daddr_t bpref; 127 int osize, nsize; 128 struct ucred *cred; 129 struct buf **bpp; 130 { 131 register struct fs *fs; 132 struct buf *bp; 133 int cg, request, error; 134 daddr_t bprev, bno; 135 136 *bpp = 0; 137 fs = ip->i_fs; 138 #ifdef DIAGNOSTIC 139 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 140 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 141 printf( 142 "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", 143 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); 144 panic("ffs_realloccg: bad size"); 145 } 146 if (cred == NOCRED) 147 panic("ffs_realloccg: missing credential\n"); 148 #endif /* DIAGNOSTIC */ 149 if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) 150 goto nospace; 151 if ((bprev = ip->i_db[lbprev]) == 0) { 152 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", 153 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); 154 panic("ffs_realloccg: bad bprev"); 155 } 156 /* 157 * Allocate the extra space in the buffer. 158 */ 159 if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { 160 brelse(bp); 161 return (error); 162 } 163 #ifdef QUOTA 164 if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) { 165 brelse(bp); 166 return (error); 167 } 168 #endif 169 /* 170 * Check for extension in the existing location. 171 */ 172 cg = dtog(fs, bprev); 173 if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) { 174 if (bp->b_blkno != fsbtodb(fs, bno)) 175 panic("bad blockno"); 176 ip->i_blocks += btodb(nsize - osize); 177 ip->i_flag |= IN_CHANGE | IN_UPDATE; 178 allocbuf(bp, nsize); 179 bp->b_flags |= B_DONE; 180 bzero((char *)bp->b_data + osize, (u_int)nsize - osize); 181 *bpp = bp; 182 return (0); 183 } 184 /* 185 * Allocate a new disk location. 186 */ 187 if (bpref >= fs->fs_size) 188 bpref = 0; 189 switch ((int)fs->fs_optim) { 190 case FS_OPTSPACE: 191 /* 192 * Allocate an exact sized fragment. Although this makes 193 * best use of space, we will waste time relocating it if 194 * the file continues to grow. If the fragmentation is 195 * less than half of the minimum free reserve, we choose 196 * to begin optimizing for time. 197 */ 198 request = nsize; 199 if (fs->fs_minfree < 5 || 200 fs->fs_cstotal.cs_nffree > 201 fs->fs_dsize * fs->fs_minfree / (2 * 100)) 202 break; 203 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 204 fs->fs_fsmnt); 205 fs->fs_optim = FS_OPTTIME; 206 break; 207 case FS_OPTTIME: 208 /* 209 * At this point we have discovered a file that is trying to 210 * grow a small fragment to a larger fragment. To save time, 211 * we allocate a full sized block, then free the unused portion. 212 * If the file continues to grow, the `ffs_fragextend' call 213 * above will be able to grow it in place without further 214 * copying. If aberrant programs cause disk fragmentation to 215 * grow within 2% of the free reserve, we choose to begin 216 * optimizing for space. 217 */ 218 request = fs->fs_bsize; 219 if (fs->fs_cstotal.cs_nffree < 220 fs->fs_dsize * (fs->fs_minfree - 2) / 100) 221 break; 222 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 223 fs->fs_fsmnt); 224 fs->fs_optim = FS_OPTSPACE; 225 break; 226 default: 227 printf("dev = 0x%x, optim = %d, fs = %s\n", 228 ip->i_dev, fs->fs_optim, fs->fs_fsmnt); 229 panic("ffs_realloccg: bad optim"); 230 /* NOTREACHED */ 231 } 232 bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request, 233 (u_long (*)())ffs_alloccg); 234 if (bno > 0) { 235 bp->b_blkno = fsbtodb(fs, bno); 236 (void) vnode_pager_uncache(ITOV(ip)); 237 ffs_blkfree(ip, bprev, (long)osize); 238 if (nsize < request) 239 ffs_blkfree(ip, bno + numfrags(fs, nsize), 240 (long)(request - nsize)); 241 ip->i_blocks += btodb(nsize - osize); 242 ip->i_flag |= IN_CHANGE | IN_UPDATE; 243 allocbuf(bp, nsize); 244 bp->b_flags |= B_DONE; 245 bzero((char *)bp->b_data + osize, (u_int)nsize - osize); 246 *bpp = bp; 247 return (0); 248 } 249 #ifdef QUOTA 250 /* 251 * Restore user's disk quota because allocation failed. 252 */ 253 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE); 254 #endif 255 brelse(bp); 256 nospace: 257 /* 258 * no space available 259 */ 260 ffs_fserr(fs, cred->cr_uid, "file system full"); 261 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); 262 return (ENOSPC); 263 } 264 265 /* 266 * Reallocate a sequence of blocks into a contiguous sequence of blocks. 267 * 268 * The vnode and an array of buffer pointers for a range of sequential 269 * logical blocks to be made contiguous is given. The allocator attempts 270 * to find a range of sequential blocks starting as close as possible to 271 * an fs_rotdelay offset from the end of the allocation for the logical 272 * block immediately preceeding the current range. If successful, the 273 * physical block numbers in the buffer pointers and in the inode are 274 * changed to reflect the new allocation. If unsuccessful, the allocation 275 * is left unchanged. The success in doing the reallocation is returned. 276 * Note that the error return is not reflected back to the user. Rather 277 * the previous block allocation will be used. 278 */ 279 int 280 ffs_reallocblks(ap) 281 struct vop_reallocblks_args /* { 282 struct vnode *a_vp; 283 struct cluster_save *a_buflist; 284 } */ *ap; 285 { 286 struct fs *fs; 287 struct inode *ip; 288 struct vnode *vp; 289 struct buf *sbp, *ebp; 290 daddr_t *bap, *sbap, *ebap; 291 struct cluster_save *buflist; 292 daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno; 293 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 294 int i, len, start_lvl, end_lvl, pref, ssize; 295 296 vp = ap->a_vp; 297 ip = VTOI(vp); 298 fs = ip->i_fs; 299 if (fs->fs_contigsumsize <= 0) 300 return (ENOSPC); 301 buflist = ap->a_buflist; 302 len = buflist->bs_nchildren; 303 start_lbn = buflist->bs_children[0]->b_lblkno; 304 end_lbn = start_lbn + len - 1; 305 #ifdef DIAGNOSTIC 306 for (i = 1; i < len; i++) 307 if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 308 panic("ffs_reallocblks: non-cluster"); 309 #endif 310 /* 311 * If the latest allocation is in a new cylinder group, assume that 312 * the filesystem has decided to move and do not force it back to 313 * the previous cylinder group. 314 */ 315 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 316 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 317 return (ENOSPC); 318 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 319 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 320 return (ENOSPC); 321 /* 322 * Get the starting offset and block map for the first block. 323 */ 324 if (start_lvl == 0) { 325 sbap = &ip->i_db[0]; 326 soff = start_lbn; 327 } else { 328 idp = &start_ap[start_lvl - 1]; 329 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) { 330 brelse(sbp); 331 return (ENOSPC); 332 } 333 sbap = (daddr_t *)sbp->b_data; 334 soff = idp->in_off; 335 } 336 /* 337 * Find the preferred location for the cluster. 338 */ 339 pref = ffs_blkpref(ip, start_lbn, soff, sbap); 340 /* 341 * If the block range spans two block maps, get the second map. 342 */ 343 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off >= len) { 344 ssize = len; 345 } else { 346 ssize = len - (idp->in_off + 1); 347 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp)) 348 goto fail; 349 ebap = (daddr_t *)ebp->b_data; 350 } 351 /* 352 * Search the block map looking for an allocation of the desired size. 353 */ 354 if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref, 355 len, (u_long (*)())ffs_clusteralloc)) == 0) 356 goto fail; 357 /* 358 * We have found a new contiguous block. 359 * 360 * First we have to replace the old block pointers with the new 361 * block pointers in the inode and indirect blocks associated 362 * with the file. 363 */ 364 blkno = newblk; 365 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) { 366 if (i == ssize) 367 bap = ebap; 368 #ifdef DIAGNOSTIC 369 if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap)) 370 panic("ffs_reallocblks: alloc mismatch"); 371 #endif 372 *bap++ = blkno; 373 } 374 /* 375 * Next we must write out the modified inode and indirect blocks. 376 * The writes are synchronous since the old block values may have 377 * been written to disk. 378 * 379 * These writes should be changed to be bdwrites (and the VOP_UPDATE 380 * dropped) when a flag has been added to the buffers and inodes 381 * to detect when they have been written. It should be set when the 382 * cluster is started and cleared whenever the buffer or inode is 383 * flushed. We can then check below to see if it is set, and do 384 * the synchronous write only when it has been cleared. 385 */ 386 if (sbap != &ip->i_db[0]) { 387 bwrite(sbp); 388 } else { 389 ip->i_flag |= IN_CHANGE | IN_UPDATE; 390 VOP_UPDATE(vp, &time, &time, MNT_WAIT); 391 } 392 if (ssize < len) 393 bwrite(ebp); 394 /* 395 * Last, free the old blocks and assign the new blocks to the buffers. 396 */ 397 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) { 398 ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno), 399 fs->fs_bsize); 400 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 401 } 402 return (0); 403 404 fail: 405 if (ssize < len) 406 brelse(ebp); 407 if (sbap != &ip->i_db[0]) 408 brelse(sbp); 409 return (ENOSPC); 410 } 411 412 /* 413 * Allocate an inode in the file system. 414 * 415 * If allocating a directory, use ffs_dirpref to select the inode. 416 * If allocating in a directory, the following hierarchy is followed: 417 * 1) allocate the preferred inode. 418 * 2) allocate an inode in the same cylinder group. 419 * 3) quadradically rehash into other cylinder groups, until an 420 * available inode is located. 421 * If no inode preference is given the following heirarchy is used 422 * to allocate an inode: 423 * 1) allocate an inode in cylinder group 0. 424 * 2) quadradically rehash into other cylinder groups, until an 425 * available inode is located. 426 */ 427 ffs_valloc(ap) 428 struct vop_valloc_args /* { 429 struct vnode *a_pvp; 430 int a_mode; 431 struct ucred *a_cred; 432 struct vnode **a_vpp; 433 } */ *ap; 434 { 435 register struct vnode *pvp = ap->a_pvp; 436 register struct inode *pip; 437 register struct fs *fs; 438 register struct inode *ip; 439 mode_t mode = ap->a_mode; 440 ino_t ino, ipref; 441 int cg, error; 442 443 *ap->a_vpp = NULL; 444 pip = VTOI(pvp); 445 fs = pip->i_fs; 446 if (fs->fs_cstotal.cs_nifree == 0) 447 goto noinodes; 448 449 if ((mode & IFMT) == IFDIR) 450 ipref = ffs_dirpref(fs); 451 else 452 ipref = pip->i_number; 453 if (ipref >= fs->fs_ncg * fs->fs_ipg) 454 ipref = 0; 455 cg = ino_to_cg(fs, ipref); 456 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg); 457 if (ino == 0) 458 goto noinodes; 459 error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp); 460 if (error) { 461 VOP_VFREE(pvp, ino, mode); 462 return (error); 463 } 464 ip = VTOI(*ap->a_vpp); 465 if (ip->i_mode) { 466 printf("mode = 0%o, inum = %d, fs = %s\n", 467 ip->i_mode, ip->i_number, fs->fs_fsmnt); 468 panic("ffs_valloc: dup alloc"); 469 } 470 if (ip->i_blocks) { /* XXX */ 471 printf("free inode %s/%d had %d blocks\n", 472 fs->fs_fsmnt, ino, ip->i_blocks); 473 ip->i_blocks = 0; 474 } 475 ip->i_flags = 0; 476 /* 477 * Set up a new generation number for this inode. 478 */ 479 if (++nextgennumber < (u_long)time.tv_sec) 480 nextgennumber = time.tv_sec; 481 ip->i_gen = nextgennumber; 482 return (0); 483 noinodes: 484 ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes"); 485 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 486 return (ENOSPC); 487 } 488 489 /* 490 * Find a cylinder to place a directory. 491 * 492 * The policy implemented by this algorithm is to select from 493 * among those cylinder groups with above the average number of 494 * free inodes, the one with the smallest number of directories. 495 */ 496 static ino_t 497 ffs_dirpref(fs) 498 register struct fs *fs; 499 { 500 int cg, minndir, mincg, avgifree; 501 502 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 503 minndir = fs->fs_ipg; 504 mincg = 0; 505 for (cg = 0; cg < fs->fs_ncg; cg++) 506 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 507 fs->fs_cs(fs, cg).cs_nifree >= avgifree) { 508 mincg = cg; 509 minndir = fs->fs_cs(fs, cg).cs_ndir; 510 } 511 return ((ino_t)(fs->fs_ipg * mincg)); 512 } 513 514 /* 515 * Select the desired position for the next block in a file. The file is 516 * logically divided into sections. The first section is composed of the 517 * direct blocks. Each additional section contains fs_maxbpg blocks. 518 * 519 * If no blocks have been allocated in the first section, the policy is to 520 * request a block in the same cylinder group as the inode that describes 521 * the file. If no blocks have been allocated in any other section, the 522 * policy is to place the section in a cylinder group with a greater than 523 * average number of free blocks. An appropriate cylinder group is found 524 * by using a rotor that sweeps the cylinder groups. When a new group of 525 * blocks is needed, the sweep begins in the cylinder group following the 526 * cylinder group from which the previous allocation was made. The sweep 527 * continues until a cylinder group with greater than the average number 528 * of free blocks is found. If the allocation is for the first block in an 529 * indirect block, the information on the previous allocation is unavailable; 530 * here a best guess is made based upon the logical block number being 531 * allocated. 532 * 533 * If a section is already partially allocated, the policy is to 534 * contiguously allocate fs_maxcontig blocks. The end of one of these 535 * contiguous blocks and the beginning of the next is physically separated 536 * so that the disk head will be in transit between them for at least 537 * fs_rotdelay milliseconds. This is to allow time for the processor to 538 * schedule another I/O transfer. 539 */ 540 daddr_t 541 ffs_blkpref(ip, lbn, indx, bap) 542 struct inode *ip; 543 daddr_t lbn; 544 int indx; 545 daddr_t *bap; 546 { 547 register struct fs *fs; 548 register int cg; 549 int avgbfree, startcg; 550 daddr_t nextblk; 551 552 fs = ip->i_fs; 553 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 554 if (lbn < NDADDR) { 555 cg = ino_to_cg(fs, ip->i_number); 556 return (fs->fs_fpg * cg + fs->fs_frag); 557 } 558 /* 559 * Find a cylinder with greater than average number of 560 * unused data blocks. 561 */ 562 if (indx == 0 || bap[indx - 1] == 0) 563 startcg = 564 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 565 else 566 startcg = dtog(fs, bap[indx - 1]) + 1; 567 startcg %= fs->fs_ncg; 568 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 569 for (cg = startcg; cg < fs->fs_ncg; cg++) 570 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 571 fs->fs_cgrotor = cg; 572 return (fs->fs_fpg * cg + fs->fs_frag); 573 } 574 for (cg = 0; cg <= startcg; cg++) 575 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 576 fs->fs_cgrotor = cg; 577 return (fs->fs_fpg * cg + fs->fs_frag); 578 } 579 return (NULL); 580 } 581 /* 582 * One or more previous blocks have been laid out. If less 583 * than fs_maxcontig previous blocks are contiguous, the 584 * next block is requested contiguously, otherwise it is 585 * requested rotationally delayed by fs_rotdelay milliseconds. 586 */ 587 nextblk = bap[indx - 1] + fs->fs_frag; 588 if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] + 589 blkstofrags(fs, fs->fs_maxcontig) != nextblk) 590 return (nextblk); 591 if (fs->fs_rotdelay != 0) 592 /* 593 * Here we convert ms of delay to frags as: 594 * (frags) = (ms) * (rev/sec) * (sect/rev) / 595 * ((sect/frag) * (ms/sec)) 596 * then round up to the next block. 597 */ 598 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / 599 (NSPF(fs) * 1000), fs->fs_frag); 600 return (nextblk); 601 } 602 603 /* 604 * Implement the cylinder overflow algorithm. 605 * 606 * The policy implemented by this algorithm is: 607 * 1) allocate the block in its requested cylinder group. 608 * 2) quadradically rehash on the cylinder group number. 609 * 3) brute force search for a free block. 610 */ 611 /*VARARGS5*/ 612 static u_long 613 ffs_hashalloc(ip, cg, pref, size, allocator) 614 struct inode *ip; 615 int cg; 616 long pref; 617 int size; /* size for data blocks, mode for inodes */ 618 u_long (*allocator)(); 619 { 620 register struct fs *fs; 621 long result; 622 int i, icg = cg; 623 624 fs = ip->i_fs; 625 /* 626 * 1: preferred cylinder group 627 */ 628 result = (*allocator)(ip, cg, pref, size); 629 if (result) 630 return (result); 631 /* 632 * 2: quadratic rehash 633 */ 634 for (i = 1; i < fs->fs_ncg; i *= 2) { 635 cg += i; 636 if (cg >= fs->fs_ncg) 637 cg -= fs->fs_ncg; 638 result = (*allocator)(ip, cg, 0, size); 639 if (result) 640 return (result); 641 } 642 /* 643 * 3: brute force search 644 * Note that we start at i == 2, since 0 was checked initially, 645 * and 1 is always checked in the quadratic rehash. 646 */ 647 cg = (icg + 2) % fs->fs_ncg; 648 for (i = 2; i < fs->fs_ncg; i++) { 649 result = (*allocator)(ip, cg, 0, size); 650 if (result) 651 return (result); 652 cg++; 653 if (cg == fs->fs_ncg) 654 cg = 0; 655 } 656 return (NULL); 657 } 658 659 /* 660 * Determine whether a fragment can be extended. 661 * 662 * Check to see if the necessary fragments are available, and 663 * if they are, allocate them. 664 */ 665 static daddr_t 666 ffs_fragextend(ip, cg, bprev, osize, nsize) 667 struct inode *ip; 668 int cg; 669 long bprev; 670 int osize, nsize; 671 { 672 register struct fs *fs; 673 register struct cg *cgp; 674 struct buf *bp; 675 long bno; 676 int frags, bbase; 677 int i, error; 678 679 fs = ip->i_fs; 680 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 681 return (NULL); 682 frags = numfrags(fs, nsize); 683 bbase = fragnum(fs, bprev); 684 if (bbase > fragnum(fs, (bprev + frags - 1))) { 685 /* cannot extend across a block boundary */ 686 return (NULL); 687 } 688 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 689 (int)fs->fs_cgsize, NOCRED, &bp); 690 if (error) { 691 brelse(bp); 692 return (NULL); 693 } 694 cgp = (struct cg *)bp->b_data; 695 if (!cg_chkmagic(cgp)) { 696 brelse(bp); 697 return (NULL); 698 } 699 cgp->cg_time = time.tv_sec; 700 bno = dtogd(fs, bprev); 701 for (i = numfrags(fs, osize); i < frags; i++) 702 if (isclr(cg_blksfree(cgp), bno + i)) { 703 brelse(bp); 704 return (NULL); 705 } 706 /* 707 * the current fragment can be extended 708 * deduct the count on fragment being extended into 709 * increase the count on the remaining fragment (if any) 710 * allocate the extended piece 711 */ 712 for (i = frags; i < fs->fs_frag - bbase; i++) 713 if (isclr(cg_blksfree(cgp), bno + i)) 714 break; 715 cgp->cg_frsum[i - numfrags(fs, osize)]--; 716 if (i != frags) 717 cgp->cg_frsum[i - frags]++; 718 for (i = numfrags(fs, osize); i < frags; i++) { 719 clrbit(cg_blksfree(cgp), bno + i); 720 cgp->cg_cs.cs_nffree--; 721 fs->fs_cstotal.cs_nffree--; 722 fs->fs_cs(fs, cg).cs_nffree--; 723 } 724 fs->fs_fmod = 1; 725 bdwrite(bp); 726 return (bprev); 727 } 728 729 /* 730 * Determine whether a block can be allocated. 731 * 732 * Check to see if a block of the appropriate size is available, 733 * and if it is, allocate it. 734 */ 735 static daddr_t 736 ffs_alloccg(ip, cg, bpref, size) 737 struct inode *ip; 738 int cg; 739 daddr_t bpref; 740 int size; 741 { 742 register struct fs *fs; 743 register struct cg *cgp; 744 struct buf *bp; 745 register int i; 746 int error, bno, frags, allocsiz; 747 748 fs = ip->i_fs; 749 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 750 return (NULL); 751 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 752 (int)fs->fs_cgsize, NOCRED, &bp); 753 if (error) { 754 brelse(bp); 755 return (NULL); 756 } 757 cgp = (struct cg *)bp->b_data; 758 if (!cg_chkmagic(cgp) || 759 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 760 brelse(bp); 761 return (NULL); 762 } 763 cgp->cg_time = time.tv_sec; 764 if (size == fs->fs_bsize) { 765 bno = ffs_alloccgblk(fs, cgp, bpref); 766 bdwrite(bp); 767 return (bno); 768 } 769 /* 770 * check to see if any fragments are already available 771 * allocsiz is the size which will be allocated, hacking 772 * it down to a smaller size if necessary 773 */ 774 frags = numfrags(fs, size); 775 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 776 if (cgp->cg_frsum[allocsiz] != 0) 777 break; 778 if (allocsiz == fs->fs_frag) { 779 /* 780 * no fragments were available, so a block will be 781 * allocated, and hacked up 782 */ 783 if (cgp->cg_cs.cs_nbfree == 0) { 784 brelse(bp); 785 return (NULL); 786 } 787 bno = ffs_alloccgblk(fs, cgp, bpref); 788 bpref = dtogd(fs, bno); 789 for (i = frags; i < fs->fs_frag; i++) 790 setbit(cg_blksfree(cgp), bpref + i); 791 i = fs->fs_frag - frags; 792 cgp->cg_cs.cs_nffree += i; 793 fs->fs_cstotal.cs_nffree += i; 794 fs->fs_cs(fs, cg).cs_nffree += i; 795 fs->fs_fmod = 1; 796 cgp->cg_frsum[i]++; 797 bdwrite(bp); 798 return (bno); 799 } 800 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 801 if (bno < 0) { 802 brelse(bp); 803 return (NULL); 804 } 805 for (i = 0; i < frags; i++) 806 clrbit(cg_blksfree(cgp), bno + i); 807 cgp->cg_cs.cs_nffree -= frags; 808 fs->fs_cstotal.cs_nffree -= frags; 809 fs->fs_cs(fs, cg).cs_nffree -= frags; 810 fs->fs_fmod = 1; 811 cgp->cg_frsum[allocsiz]--; 812 if (frags != allocsiz) 813 cgp->cg_frsum[allocsiz - frags]++; 814 bdwrite(bp); 815 return (cg * fs->fs_fpg + bno); 816 } 817 818 /* 819 * Allocate a block in a cylinder group. 820 * 821 * This algorithm implements the following policy: 822 * 1) allocate the requested block. 823 * 2) allocate a rotationally optimal block in the same cylinder. 824 * 3) allocate the next available block on the block rotor for the 825 * specified cylinder group. 826 * Note that this routine only allocates fs_bsize blocks; these 827 * blocks may be fragmented by the routine that allocates them. 828 */ 829 static daddr_t 830 ffs_alloccgblk(fs, cgp, bpref) 831 register struct fs *fs; 832 register struct cg *cgp; 833 daddr_t bpref; 834 { 835 daddr_t bno, blkno; 836 int cylno, pos, delta; 837 short *cylbp; 838 register int i; 839 840 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) { 841 bpref = cgp->cg_rotor; 842 goto norot; 843 } 844 bpref = blknum(fs, bpref); 845 bpref = dtogd(fs, bpref); 846 /* 847 * if the requested block is available, use it 848 */ 849 if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { 850 bno = bpref; 851 goto gotit; 852 } 853 /* 854 * check for a block available on the same cylinder 855 */ 856 cylno = cbtocylno(fs, bpref); 857 if (cg_blktot(cgp)[cylno] == 0) 858 goto norot; 859 if (fs->fs_cpc == 0) { 860 /* 861 * Block layout information is not available. 862 * Leaving bpref unchanged means we take the 863 * next available free block following the one 864 * we just allocated. Hopefully this will at 865 * least hit a track cache on drives of unknown 866 * geometry (e.g. SCSI). 867 */ 868 goto norot; 869 } 870 /* 871 * check the summary information to see if a block is 872 * available in the requested cylinder starting at the 873 * requested rotational position and proceeding around. 874 */ 875 cylbp = cg_blks(fs, cgp, cylno); 876 pos = cbtorpos(fs, bpref); 877 for (i = pos; i < fs->fs_nrpos; i++) 878 if (cylbp[i] > 0) 879 break; 880 if (i == fs->fs_nrpos) 881 for (i = 0; i < pos; i++) 882 if (cylbp[i] > 0) 883 break; 884 if (cylbp[i] > 0) { 885 /* 886 * found a rotational position, now find the actual 887 * block. A panic if none is actually there. 888 */ 889 pos = cylno % fs->fs_cpc; 890 bno = (cylno - pos) * fs->fs_spc / NSPB(fs); 891 if (fs_postbl(fs, pos)[i] == -1) { 892 printf("pos = %d, i = %d, fs = %s\n", 893 pos, i, fs->fs_fsmnt); 894 panic("ffs_alloccgblk: cyl groups corrupted"); 895 } 896 for (i = fs_postbl(fs, pos)[i];; ) { 897 if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) { 898 bno = blkstofrags(fs, (bno + i)); 899 goto gotit; 900 } 901 delta = fs_rotbl(fs)[i]; 902 if (delta <= 0 || 903 delta + i > fragstoblks(fs, fs->fs_fpg)) 904 break; 905 i += delta; 906 } 907 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); 908 panic("ffs_alloccgblk: can't find blk in cyl"); 909 } 910 norot: 911 /* 912 * no blocks in the requested cylinder, so take next 913 * available one in this cylinder group. 914 */ 915 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 916 if (bno < 0) 917 return (NULL); 918 cgp->cg_rotor = bno; 919 gotit: 920 blkno = fragstoblks(fs, bno); 921 ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno); 922 ffs_clusteracct(fs, cgp, blkno, -1); 923 cgp->cg_cs.cs_nbfree--; 924 fs->fs_cstotal.cs_nbfree--; 925 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 926 cylno = cbtocylno(fs, bno); 927 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; 928 cg_blktot(cgp)[cylno]--; 929 fs->fs_fmod = 1; 930 return (cgp->cg_cgx * fs->fs_fpg + bno); 931 } 932 933 /* 934 * Determine whether a cluster can be allocated. 935 * 936 * We do not currently check for optimal rotational layout if there 937 * are multiple choices in the same cylinder group. Instead we just 938 * take the first one that we find following bpref. 939 */ 940 static daddr_t 941 ffs_clusteralloc(ip, cg, bpref, len) 942 struct inode *ip; 943 int cg; 944 daddr_t bpref; 945 int len; 946 { 947 register struct fs *fs; 948 register struct cg *cgp; 949 struct buf *bp; 950 int i, run, bno, bit, map; 951 u_char *mapp; 952 953 fs = ip->i_fs; 954 if (fs->fs_cs(fs, cg).cs_nbfree < len) 955 return (NULL); 956 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, 957 NOCRED, &bp)) 958 goto fail; 959 cgp = (struct cg *)bp->b_data; 960 if (!cg_chkmagic(cgp)) 961 goto fail; 962 /* 963 * Check to see if a cluster of the needed size (or bigger) is 964 * available in this cylinder group. 965 */ 966 for (i = len; i <= fs->fs_contigsumsize; i++) 967 if (cg_clustersum(cgp)[i] > 0) 968 break; 969 if (i > fs->fs_contigsumsize) 970 goto fail; 971 /* 972 * Search the cluster map to find a big enough cluster. 973 * We take the first one that we find, even if it is larger 974 * than we need as we prefer to get one close to the previous 975 * block allocation. We do not search before the current 976 * preference point as we do not want to allocate a block 977 * that is allocated before the previous one (as we will 978 * then have to wait for another pass of the elevator 979 * algorithm before it will be read). We prefer to fail and 980 * be recalled to try an allocation in the next cylinder group. 981 */ 982 if (dtog(fs, bpref) != cg) 983 bpref = 0; 984 else 985 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref))); 986 mapp = &cg_clustersfree(cgp)[bpref / NBBY]; 987 map = *mapp++; 988 bit = 1 << (bpref % NBBY); 989 for (run = 0, i = bpref; i < cgp->cg_nclusterblks; i++) { 990 if ((map & bit) == 0) { 991 run = 0; 992 } else { 993 run++; 994 if (run == len) 995 break; 996 } 997 if ((i & (NBBY - 1)) != (NBBY - 1)) { 998 bit <<= 1; 999 } else { 1000 map = *mapp++; 1001 bit = 1; 1002 } 1003 } 1004 if (i == cgp->cg_nclusterblks) 1005 goto fail; 1006 /* 1007 * Allocate the cluster that we have found. 1008 */ 1009 bno = cg * fs->fs_fpg + blkstofrags(fs, i - run + 1); 1010 len = blkstofrags(fs, len); 1011 for (i = 0; i < len; i += fs->fs_frag) 1012 if (ffs_alloccgblk(fs, cgp, bno + i) != bno + i) 1013 panic("ffs_clusteralloc: lost block"); 1014 brelse(bp); 1015 return (bno); 1016 1017 fail: 1018 brelse(bp); 1019 return (0); 1020 } 1021 1022 /* 1023 * Determine whether an inode can be allocated. 1024 * 1025 * Check to see if an inode is available, and if it is, 1026 * allocate it using the following policy: 1027 * 1) allocate the requested inode. 1028 * 2) allocate the next available inode after the requested 1029 * inode in the specified cylinder group. 1030 */ 1031 static ino_t 1032 ffs_nodealloccg(ip, cg, ipref, mode) 1033 struct inode *ip; 1034 int cg; 1035 daddr_t ipref; 1036 int mode; 1037 { 1038 register struct fs *fs; 1039 register struct cg *cgp; 1040 struct buf *bp; 1041 int error, start, len, loc, map, i; 1042 1043 fs = ip->i_fs; 1044 if (fs->fs_cs(fs, cg).cs_nifree == 0) 1045 return (NULL); 1046 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1047 (int)fs->fs_cgsize, NOCRED, &bp); 1048 if (error) { 1049 brelse(bp); 1050 return (NULL); 1051 } 1052 cgp = (struct cg *)bp->b_data; 1053 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 1054 brelse(bp); 1055 return (NULL); 1056 } 1057 cgp->cg_time = time.tv_sec; 1058 if (ipref) { 1059 ipref %= fs->fs_ipg; 1060 if (isclr(cg_inosused(cgp), ipref)) 1061 goto gotit; 1062 } 1063 start = cgp->cg_irotor / NBBY; 1064 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 1065 loc = skpc(0xff, len, &cg_inosused(cgp)[start]); 1066 if (loc == 0) { 1067 len = start + 1; 1068 start = 0; 1069 loc = skpc(0xff, len, &cg_inosused(cgp)[0]); 1070 if (loc == 0) { 1071 printf("cg = %d, irotor = %d, fs = %s\n", 1072 cg, cgp->cg_irotor, fs->fs_fsmnt); 1073 panic("ffs_nodealloccg: map corrupted"); 1074 /* NOTREACHED */ 1075 } 1076 } 1077 i = start + len - loc; 1078 map = cg_inosused(cgp)[i]; 1079 ipref = i * NBBY; 1080 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 1081 if ((map & i) == 0) { 1082 cgp->cg_irotor = ipref; 1083 goto gotit; 1084 } 1085 } 1086 printf("fs = %s\n", fs->fs_fsmnt); 1087 panic("ffs_nodealloccg: block not in map"); 1088 /* NOTREACHED */ 1089 gotit: 1090 setbit(cg_inosused(cgp), ipref); 1091 cgp->cg_cs.cs_nifree--; 1092 fs->fs_cstotal.cs_nifree--; 1093 fs->fs_cs(fs, cg).cs_nifree--; 1094 fs->fs_fmod = 1; 1095 if ((mode & IFMT) == IFDIR) { 1096 cgp->cg_cs.cs_ndir++; 1097 fs->fs_cstotal.cs_ndir++; 1098 fs->fs_cs(fs, cg).cs_ndir++; 1099 } 1100 bdwrite(bp); 1101 return (cg * fs->fs_ipg + ipref); 1102 } 1103 1104 /* 1105 * Free a block or fragment. 1106 * 1107 * The specified block or fragment is placed back in the 1108 * free map. If a fragment is deallocated, a possible 1109 * block reassembly is checked. 1110 */ 1111 ffs_blkfree(ip, bno, size) 1112 register struct inode *ip; 1113 daddr_t bno; 1114 long size; 1115 { 1116 register struct fs *fs; 1117 register struct cg *cgp; 1118 struct buf *bp; 1119 daddr_t blkno; 1120 int i, error, cg, blk, frags, bbase; 1121 1122 fs = ip->i_fs; 1123 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 1124 printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", 1125 ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); 1126 panic("blkfree: bad size"); 1127 } 1128 cg = dtog(fs, bno); 1129 if ((u_int)bno >= fs->fs_size) { 1130 printf("bad block %d, ino %d\n", bno, ip->i_number); 1131 ffs_fserr(fs, ip->i_uid, "bad block"); 1132 return; 1133 } 1134 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1135 (int)fs->fs_cgsize, NOCRED, &bp); 1136 if (error) { 1137 brelse(bp); 1138 return; 1139 } 1140 cgp = (struct cg *)bp->b_data; 1141 if (!cg_chkmagic(cgp)) { 1142 brelse(bp); 1143 return; 1144 } 1145 cgp->cg_time = time.tv_sec; 1146 bno = dtogd(fs, bno); 1147 if (size == fs->fs_bsize) { 1148 blkno = fragstoblks(fs, bno); 1149 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) { 1150 printf("dev = 0x%x, block = %d, fs = %s\n", 1151 ip->i_dev, bno, fs->fs_fsmnt); 1152 panic("blkfree: freeing free block"); 1153 } 1154 ffs_setblock(fs, cg_blksfree(cgp), blkno); 1155 ffs_clusteracct(fs, cgp, blkno, 1); 1156 cgp->cg_cs.cs_nbfree++; 1157 fs->fs_cstotal.cs_nbfree++; 1158 fs->fs_cs(fs, cg).cs_nbfree++; 1159 i = cbtocylno(fs, bno); 1160 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; 1161 cg_blktot(cgp)[i]++; 1162 } else { 1163 bbase = bno - fragnum(fs, bno); 1164 /* 1165 * decrement the counts associated with the old frags 1166 */ 1167 blk = blkmap(fs, cg_blksfree(cgp), bbase); 1168 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 1169 /* 1170 * deallocate the fragment 1171 */ 1172 frags = numfrags(fs, size); 1173 for (i = 0; i < frags; i++) { 1174 if (isset(cg_blksfree(cgp), bno + i)) { 1175 printf("dev = 0x%x, block = %d, fs = %s\n", 1176 ip->i_dev, bno + i, fs->fs_fsmnt); 1177 panic("blkfree: freeing free frag"); 1178 } 1179 setbit(cg_blksfree(cgp), bno + i); 1180 } 1181 cgp->cg_cs.cs_nffree += i; 1182 fs->fs_cstotal.cs_nffree += i; 1183 fs->fs_cs(fs, cg).cs_nffree += i; 1184 /* 1185 * add back in counts associated with the new frags 1186 */ 1187 blk = blkmap(fs, cg_blksfree(cgp), bbase); 1188 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 1189 /* 1190 * if a complete block has been reassembled, account for it 1191 */ 1192 blkno = fragstoblks(fs, bbase); 1193 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) { 1194 cgp->cg_cs.cs_nffree -= fs->fs_frag; 1195 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 1196 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 1197 ffs_clusteracct(fs, cgp, blkno, 1); 1198 cgp->cg_cs.cs_nbfree++; 1199 fs->fs_cstotal.cs_nbfree++; 1200 fs->fs_cs(fs, cg).cs_nbfree++; 1201 i = cbtocylno(fs, bbase); 1202 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; 1203 cg_blktot(cgp)[i]++; 1204 } 1205 } 1206 fs->fs_fmod = 1; 1207 bdwrite(bp); 1208 } 1209 1210 /* 1211 * Free an inode. 1212 * 1213 * The specified inode is placed back in the free map. 1214 */ 1215 int 1216 ffs_vfree(ap) 1217 struct vop_vfree_args /* { 1218 struct vnode *a_pvp; 1219 ino_t a_ino; 1220 int a_mode; 1221 } */ *ap; 1222 { 1223 register struct fs *fs; 1224 register struct cg *cgp; 1225 register struct inode *pip; 1226 ino_t ino = ap->a_ino; 1227 struct buf *bp; 1228 int error, cg; 1229 1230 pip = VTOI(ap->a_pvp); 1231 fs = pip->i_fs; 1232 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 1233 panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n", 1234 pip->i_dev, ino, fs->fs_fsmnt); 1235 cg = ino_to_cg(fs, ino); 1236 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1237 (int)fs->fs_cgsize, NOCRED, &bp); 1238 if (error) { 1239 brelse(bp); 1240 return (0); 1241 } 1242 cgp = (struct cg *)bp->b_data; 1243 if (!cg_chkmagic(cgp)) { 1244 brelse(bp); 1245 return (0); 1246 } 1247 cgp->cg_time = time.tv_sec; 1248 ino %= fs->fs_ipg; 1249 if (isclr(cg_inosused(cgp), ino)) { 1250 printf("dev = 0x%x, ino = %d, fs = %s\n", 1251 pip->i_dev, ino, fs->fs_fsmnt); 1252 if (fs->fs_ronly == 0) 1253 panic("ifree: freeing free inode"); 1254 } 1255 clrbit(cg_inosused(cgp), ino); 1256 if (ino < cgp->cg_irotor) 1257 cgp->cg_irotor = ino; 1258 cgp->cg_cs.cs_nifree++; 1259 fs->fs_cstotal.cs_nifree++; 1260 fs->fs_cs(fs, cg).cs_nifree++; 1261 if ((ap->a_mode & IFMT) == IFDIR) { 1262 cgp->cg_cs.cs_ndir--; 1263 fs->fs_cstotal.cs_ndir--; 1264 fs->fs_cs(fs, cg).cs_ndir--; 1265 } 1266 fs->fs_fmod = 1; 1267 bdwrite(bp); 1268 return (0); 1269 } 1270 1271 /* 1272 * Find a block of the specified size in the specified cylinder group. 1273 * 1274 * It is a panic if a request is made to find a block if none are 1275 * available. 1276 */ 1277 static daddr_t 1278 ffs_mapsearch(fs, cgp, bpref, allocsiz) 1279 register struct fs *fs; 1280 register struct cg *cgp; 1281 daddr_t bpref; 1282 int allocsiz; 1283 { 1284 daddr_t bno; 1285 int start, len, loc, i; 1286 int blk, field, subfield, pos; 1287 1288 /* 1289 * find the fragment by searching through the free block 1290 * map for an appropriate bit pattern 1291 */ 1292 if (bpref) 1293 start = dtogd(fs, bpref) / NBBY; 1294 else 1295 start = cgp->cg_frotor / NBBY; 1296 len = howmany(fs->fs_fpg, NBBY) - start; 1297 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start], 1298 (u_char *)fragtbl[fs->fs_frag], 1299 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1300 if (loc == 0) { 1301 len = start + 1; 1302 start = 0; 1303 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0], 1304 (u_char *)fragtbl[fs->fs_frag], 1305 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1306 if (loc == 0) { 1307 printf("start = %d, len = %d, fs = %s\n", 1308 start, len, fs->fs_fsmnt); 1309 panic("ffs_alloccg: map corrupted"); 1310 /* NOTREACHED */ 1311 } 1312 } 1313 bno = (start + len - loc) * NBBY; 1314 cgp->cg_frotor = bno; 1315 /* 1316 * found the byte in the map 1317 * sift through the bits to find the selected frag 1318 */ 1319 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 1320 blk = blkmap(fs, cg_blksfree(cgp), bno); 1321 blk <<= 1; 1322 field = around[allocsiz]; 1323 subfield = inside[allocsiz]; 1324 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 1325 if ((blk & field) == subfield) 1326 return (bno + pos); 1327 field <<= 1; 1328 subfield <<= 1; 1329 } 1330 } 1331 printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); 1332 panic("ffs_alloccg: block not in map"); 1333 return (-1); 1334 } 1335 1336 /* 1337 * Update the cluster map because of an allocation or free. 1338 * 1339 * Cnt == 1 means free; cnt == -1 means allocating. 1340 */ 1341 ffs_clusteracct(fs, cgp, blkno, cnt) 1342 struct fs *fs; 1343 struct cg *cgp; 1344 daddr_t blkno; 1345 int cnt; 1346 { 1347 long *sump; 1348 u_char *freemapp, *mapp; 1349 int i, start, end, forw, back, map, bit; 1350 1351 if (fs->fs_contigsumsize <= 0) 1352 return; 1353 freemapp = cg_clustersfree(cgp); 1354 sump = cg_clustersum(cgp); 1355 /* 1356 * Allocate or clear the actual block. 1357 */ 1358 if (cnt > 0) 1359 setbit(freemapp, blkno); 1360 else 1361 clrbit(freemapp, blkno); 1362 /* 1363 * Find the size of the cluster going forward. 1364 */ 1365 start = blkno + 1; 1366 end = start + fs->fs_contigsumsize; 1367 if (end >= cgp->cg_nclusterblks) 1368 end = cgp->cg_nclusterblks; 1369 mapp = &freemapp[start / NBBY]; 1370 map = *mapp++; 1371 bit = 1 << (start % NBBY); 1372 for (i = start; i < end; i++) { 1373 if ((map & bit) == 0) 1374 break; 1375 if ((i & (NBBY - 1)) != (NBBY - 1)) { 1376 bit <<= 1; 1377 } else { 1378 map = *mapp++; 1379 bit = 1; 1380 } 1381 } 1382 forw = i - start; 1383 /* 1384 * Find the size of the cluster going backward. 1385 */ 1386 start = blkno - 1; 1387 end = start - fs->fs_contigsumsize; 1388 if (end < 0) 1389 end = -1; 1390 mapp = &freemapp[start / NBBY]; 1391 map = *mapp--; 1392 bit = 1 << (start % NBBY); 1393 for (i = start; i > end; i--) { 1394 if ((map & bit) == 0) 1395 break; 1396 if ((i & (NBBY - 1)) != 0) { 1397 bit >>= 1; 1398 } else { 1399 map = *mapp--; 1400 bit = 1 << (NBBY - 1); 1401 } 1402 } 1403 back = start - i; 1404 /* 1405 * Account for old cluster and the possibly new forward and 1406 * back clusters. 1407 */ 1408 i = back + forw + 1; 1409 if (i > fs->fs_contigsumsize) 1410 i = fs->fs_contigsumsize; 1411 sump[i] += cnt; 1412 if (back > 0) 1413 sump[back] -= cnt; 1414 if (forw > 0) 1415 sump[forw] -= cnt; 1416 } 1417 1418 /* 1419 * Fserr prints the name of a file system with an error diagnostic. 1420 * 1421 * The form of the error message is: 1422 * fs: error message 1423 */ 1424 static void 1425 ffs_fserr(fs, uid, cp) 1426 struct fs *fs; 1427 u_int uid; 1428 char *cp; 1429 { 1430 1431 log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); 1432 } 1433