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