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