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