1 /* 2 * Copyright (c) 2002 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by Marshall 6 * Kirk McKusick and Network Associates Laboratories, the Security 7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 9 * research program 10 * 11 * Copyright (c) 1982, 1989, 1993 12 * The Regents of the University of California. All rights reserved. 13 * (c) UNIX System Laboratories, Inc. 14 * Copyright (c) 1982, 1986, 1989, 1993 15 * The Regents of the University of California. All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 3. All advertising materials mentioning features or use of this software 26 * must display the following acknowledgement: 27 * This product includes software developed by the University of 28 * California, Berkeley and its contributors. 29 * 4. Neither the name of the University nor the names of its contributors 30 * may be used to endorse or promote products derived from this software 31 * without specific prior written permission. 32 * 33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 * SUCH DAMAGE. 44 * 45 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95 46 * $FreeBSD$ 47 */ 48 49 #include "opt_quota.h" 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/bio.h> 54 #include <sys/buf.h> 55 #include <sys/conf.h> 56 #include <sys/file.h> 57 #include <sys/proc.h> 58 #include <sys/vnode.h> 59 #include <sys/mount.h> 60 #include <sys/kernel.h> 61 #include <sys/stdint.h> 62 #include <sys/sysctl.h> 63 #include <sys/syslog.h> 64 65 #include <ufs/ufs/extattr.h> 66 #include <ufs/ufs/quota.h> 67 #include <ufs/ufs/inode.h> 68 #include <ufs/ufs/ufs_extern.h> 69 #include <ufs/ufs/ufsmount.h> 70 71 #include <ufs/ffs/fs.h> 72 #include <ufs/ffs/ffs_extern.h> 73 74 typedef ufs2_daddr_t allocfcn_t(struct inode *ip, int cg, ufs2_daddr_t bpref, 75 int size); 76 77 static ufs2_daddr_t ffs_alloccg(struct inode *, int, ufs2_daddr_t, int); 78 static ufs2_daddr_t 79 ffs_alloccgblk(struct inode *, struct buf *, ufs2_daddr_t); 80 #ifdef DIAGNOSTIC 81 static int ffs_checkblk(struct inode *, ufs2_daddr_t, long); 82 #endif 83 static ufs2_daddr_t ffs_clusteralloc(struct inode *, int, ufs2_daddr_t, int); 84 static ino_t ffs_dirpref(struct inode *); 85 static ufs2_daddr_t ffs_fragextend(struct inode *, int, ufs2_daddr_t, int, int); 86 static void ffs_fserr(struct fs *, ino_t, char *); 87 static ufs2_daddr_t ffs_hashalloc 88 (struct inode *, int, ufs2_daddr_t, int, allocfcn_t *); 89 static ufs2_daddr_t ffs_nodealloccg(struct inode *, int, ufs2_daddr_t, int); 90 static ufs1_daddr_t ffs_mapsearch(struct fs *, struct cg *, ufs2_daddr_t, int); 91 static int ffs_reallocblks_ufs1(struct vop_reallocblks_args *); 92 static int ffs_reallocblks_ufs2(struct vop_reallocblks_args *); 93 94 /* 95 * Allocate a block in the filesystem. 96 * 97 * The size of the requested block is given, which must be some 98 * multiple of fs_fsize and <= fs_bsize. 99 * A preference may be optionally specified. If a preference is given 100 * the following hierarchy is used to allocate a block: 101 * 1) allocate the requested block. 102 * 2) allocate a rotationally optimal block in the same cylinder. 103 * 3) allocate a block in the same cylinder group. 104 * 4) quadradically rehash into other cylinder groups, until an 105 * available block is located. 106 * If no block preference is given the following heirarchy is used 107 * to allocate a block: 108 * 1) allocate a block in the cylinder group that contains the 109 * inode for the file. 110 * 2) quadradically rehash into other cylinder groups, until an 111 * available block is located. 112 */ 113 int 114 ffs_alloc(ip, lbn, bpref, size, cred, bnp) 115 struct inode *ip; 116 ufs2_daddr_t lbn, bpref; 117 int size; 118 struct ucred *cred; 119 ufs2_daddr_t *bnp; 120 { 121 struct fs *fs; 122 ufs2_daddr_t bno; 123 int cg, reclaimed; 124 #ifdef QUOTA 125 int error; 126 #endif 127 128 *bnp = 0; 129 fs = ip->i_fs; 130 #ifdef DIAGNOSTIC 131 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 132 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n", 133 devtoname(ip->i_dev), (long)fs->fs_bsize, size, 134 fs->fs_fsmnt); 135 panic("ffs_alloc: bad size"); 136 } 137 if (cred == NOCRED) 138 panic("ffs_alloc: missing credential"); 139 #endif /* DIAGNOSTIC */ 140 reclaimed = 0; 141 retry: 142 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) 143 goto nospace; 144 if (suser_cred(cred, PRISON_ROOT) && 145 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0) 146 goto nospace; 147 #ifdef QUOTA 148 error = chkdq(ip, btodb(size), cred, 0); 149 if (error) 150 return (error); 151 #endif 152 if (bpref >= fs->fs_size) 153 bpref = 0; 154 if (bpref == 0) 155 cg = ino_to_cg(fs, ip->i_number); 156 else 157 cg = dtog(fs, bpref); 158 bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg); 159 if (bno > 0) { 160 DIP(ip, i_blocks) += btodb(size); 161 ip->i_flag |= IN_CHANGE | IN_UPDATE; 162 *bnp = bno; 163 return (0); 164 } 165 #ifdef QUOTA 166 /* 167 * Restore user's disk quota because allocation failed. 168 */ 169 (void) chkdq(ip, -btodb(size), cred, FORCE); 170 #endif 171 nospace: 172 if (fs->fs_pendingblocks > 0 && reclaimed == 0) { 173 reclaimed = 1; 174 softdep_request_cleanup(fs, ITOV(ip)); 175 goto retry; 176 } 177 ffs_fserr(fs, ip->i_number, "filesystem full"); 178 uprintf("\n%s: write failed, filesystem is full\n", fs->fs_fsmnt); 179 return (ENOSPC); 180 } 181 182 /* 183 * Reallocate a fragment to a bigger size 184 * 185 * The number and size of the old block is given, and a preference 186 * and new size is also specified. The allocator attempts to extend 187 * the original block. Failing that, the regular block allocator is 188 * invoked to get an appropriate block. 189 */ 190 int 191 ffs_realloccg(ip, lbprev, bprev, bpref, osize, nsize, cred, bpp) 192 struct inode *ip; 193 ufs2_daddr_t lbprev; 194 ufs2_daddr_t bprev; 195 ufs2_daddr_t bpref; 196 int osize, nsize; 197 struct ucred *cred; 198 struct buf **bpp; 199 { 200 struct vnode *vp; 201 struct fs *fs; 202 struct buf *bp; 203 int cg, request, error, reclaimed; 204 ufs2_daddr_t bno; 205 206 *bpp = 0; 207 vp = ITOV(ip); 208 fs = ip->i_fs; 209 #ifdef DIAGNOSTIC 210 if (vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) 211 panic("ffs_realloccg: allocation on suspended filesystem"); 212 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || 213 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { 214 printf( 215 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n", 216 devtoname(ip->i_dev), (long)fs->fs_bsize, osize, 217 nsize, fs->fs_fsmnt); 218 panic("ffs_realloccg: bad size"); 219 } 220 if (cred == NOCRED) 221 panic("ffs_realloccg: missing credential"); 222 #endif /* DIAGNOSTIC */ 223 reclaimed = 0; 224 retry: 225 if (suser_cred(cred, PRISON_ROOT) && 226 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0) 227 goto nospace; 228 if (bprev == 0) { 229 printf("dev = %s, bsize = %ld, bprev = %jd, fs = %s\n", 230 devtoname(ip->i_dev), (long)fs->fs_bsize, (intmax_t)bprev, 231 fs->fs_fsmnt); 232 panic("ffs_realloccg: bad bprev"); 233 } 234 /* 235 * Allocate the extra space in the buffer. 236 */ 237 error = bread(vp, lbprev, osize, NOCRED, &bp); 238 if (error) { 239 brelse(bp); 240 return (error); 241 } 242 243 if (bp->b_blkno == bp->b_lblkno) { 244 if (lbprev >= NDADDR) 245 panic("ffs_realloccg: lbprev out of range"); 246 bp->b_blkno = fsbtodb(fs, bprev); 247 } 248 249 #ifdef QUOTA 250 error = chkdq(ip, btodb(nsize - osize), cred, 0); 251 if (error) { 252 brelse(bp); 253 return (error); 254 } 255 #endif 256 /* 257 * Check for extension in the existing location. 258 */ 259 cg = dtog(fs, bprev); 260 bno = ffs_fragextend(ip, cg, bprev, osize, nsize); 261 if (bno) { 262 if (bp->b_blkno != fsbtodb(fs, bno)) 263 panic("ffs_realloccg: bad blockno"); 264 DIP(ip, i_blocks) += btodb(nsize - osize); 265 ip->i_flag |= IN_CHANGE | IN_UPDATE; 266 allocbuf(bp, nsize); 267 bp->b_flags |= B_DONE; 268 bzero((char *)bp->b_data + osize, (u_int)nsize - osize); 269 *bpp = bp; 270 return (0); 271 } 272 /* 273 * Allocate a new disk location. 274 */ 275 if (bpref >= fs->fs_size) 276 bpref = 0; 277 switch ((int)fs->fs_optim) { 278 case FS_OPTSPACE: 279 /* 280 * Allocate an exact sized fragment. Although this makes 281 * best use of space, we will waste time relocating it if 282 * the file continues to grow. If the fragmentation is 283 * less than half of the minimum free reserve, we choose 284 * to begin optimizing for time. 285 */ 286 request = nsize; 287 if (fs->fs_minfree <= 5 || 288 fs->fs_cstotal.cs_nffree > 289 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100)) 290 break; 291 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", 292 fs->fs_fsmnt); 293 fs->fs_optim = FS_OPTTIME; 294 break; 295 case FS_OPTTIME: 296 /* 297 * At this point we have discovered a file that is trying to 298 * grow a small fragment to a larger fragment. To save time, 299 * we allocate a full sized block, then free the unused portion. 300 * If the file continues to grow, the `ffs_fragextend' call 301 * above will be able to grow it in place without further 302 * copying. If aberrant programs cause disk fragmentation to 303 * grow within 2% of the free reserve, we choose to begin 304 * optimizing for space. 305 */ 306 request = fs->fs_bsize; 307 if (fs->fs_cstotal.cs_nffree < 308 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100) 309 break; 310 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", 311 fs->fs_fsmnt); 312 fs->fs_optim = FS_OPTSPACE; 313 break; 314 default: 315 printf("dev = %s, optim = %ld, fs = %s\n", 316 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt); 317 panic("ffs_realloccg: bad optim"); 318 /* NOTREACHED */ 319 } 320 bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg); 321 if (bno > 0) { 322 bp->b_blkno = fsbtodb(fs, bno); 323 if (!DOINGSOFTDEP(vp)) 324 ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize, 325 ip->i_number); 326 if (nsize < request) 327 ffs_blkfree(fs, ip->i_devvp, bno + numfrags(fs, nsize), 328 (long)(request - nsize), ip->i_number); 329 DIP(ip, i_blocks) += btodb(nsize - osize); 330 ip->i_flag |= IN_CHANGE | IN_UPDATE; 331 allocbuf(bp, nsize); 332 bp->b_flags |= B_DONE; 333 bzero((char *)bp->b_data + osize, (u_int)nsize - osize); 334 *bpp = bp; 335 return (0); 336 } 337 #ifdef QUOTA 338 /* 339 * Restore user's disk quota because allocation failed. 340 */ 341 (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE); 342 #endif 343 brelse(bp); 344 nospace: 345 /* 346 * no space available 347 */ 348 if (fs->fs_pendingblocks > 0 && reclaimed == 0) { 349 reclaimed = 1; 350 softdep_request_cleanup(fs, vp); 351 goto retry; 352 } 353 ffs_fserr(fs, ip->i_number, "filesystem full"); 354 uprintf("\n%s: write failed, filesystem is full\n", fs->fs_fsmnt); 355 return (ENOSPC); 356 } 357 358 /* 359 * Reallocate a sequence of blocks into a contiguous sequence of blocks. 360 * 361 * The vnode and an array of buffer pointers for a range of sequential 362 * logical blocks to be made contiguous is given. The allocator attempts 363 * to find a range of sequential blocks starting as close as possible 364 * from the end of the allocation for the logical block immediately 365 * preceding the current range. If successful, the physical block numbers 366 * in the buffer pointers and in the inode are changed to reflect the new 367 * allocation. If unsuccessful, the allocation is left unchanged. The 368 * success in doing the reallocation is returned. Note that the error 369 * return is not reflected back to the user. Rather the previous block 370 * allocation will be used. 371 */ 372 373 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem"); 374 375 static int doasyncfree = 1; 376 SYSCTL_INT(_vfs_ffs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, ""); 377 378 static int doreallocblks = 1; 379 SYSCTL_INT(_vfs_ffs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, ""); 380 381 #ifdef DEBUG 382 static volatile int prtrealloc = 0; 383 #endif 384 385 int 386 ffs_reallocblks(ap) 387 struct vop_reallocblks_args /* { 388 struct vnode *a_vp; 389 struct cluster_save *a_buflist; 390 } */ *ap; 391 { 392 393 if (doreallocblks == 0) 394 return (ENOSPC); 395 if (VTOI(ap->a_vp)->i_ump->um_fstype == UFS1) 396 return (ffs_reallocblks_ufs1(ap)); 397 return (ffs_reallocblks_ufs2(ap)); 398 } 399 400 static int 401 ffs_reallocblks_ufs1(ap) 402 struct vop_reallocblks_args /* { 403 struct vnode *a_vp; 404 struct cluster_save *a_buflist; 405 } */ *ap; 406 { 407 struct fs *fs; 408 struct inode *ip; 409 struct vnode *vp; 410 struct buf *sbp, *ebp; 411 ufs1_daddr_t *bap, *sbap, *ebap = 0; 412 struct cluster_save *buflist; 413 ufs_lbn_t start_lbn, end_lbn; 414 ufs1_daddr_t soff, newblk, blkno; 415 ufs2_daddr_t pref; 416 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 417 int i, len, start_lvl, end_lvl, ssize; 418 419 vp = ap->a_vp; 420 ip = VTOI(vp); 421 fs = ip->i_fs; 422 if (fs->fs_contigsumsize <= 0) 423 return (ENOSPC); 424 buflist = ap->a_buflist; 425 len = buflist->bs_nchildren; 426 start_lbn = buflist->bs_children[0]->b_lblkno; 427 end_lbn = start_lbn + len - 1; 428 #ifdef DIAGNOSTIC 429 for (i = 0; i < len; i++) 430 if (!ffs_checkblk(ip, 431 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 432 panic("ffs_reallocblks: unallocated block 1"); 433 for (i = 1; i < len; i++) 434 if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 435 panic("ffs_reallocblks: non-logical cluster"); 436 blkno = buflist->bs_children[0]->b_blkno; 437 ssize = fsbtodb(fs, fs->fs_frag); 438 for (i = 1; i < len - 1; i++) 439 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize)) 440 panic("ffs_reallocblks: non-physical cluster %d", i); 441 #endif 442 /* 443 * If the latest allocation is in a new cylinder group, assume that 444 * the filesystem has decided to move and do not force it back to 445 * the previous cylinder group. 446 */ 447 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 448 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 449 return (ENOSPC); 450 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 451 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 452 return (ENOSPC); 453 /* 454 * Get the starting offset and block map for the first block. 455 */ 456 if (start_lvl == 0) { 457 sbap = &ip->i_din1->di_db[0]; 458 soff = start_lbn; 459 } else { 460 idp = &start_ap[start_lvl - 1]; 461 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) { 462 brelse(sbp); 463 return (ENOSPC); 464 } 465 sbap = (ufs1_daddr_t *)sbp->b_data; 466 soff = idp->in_off; 467 } 468 /* 469 * Find the preferred location for the cluster. 470 */ 471 pref = ffs_blkpref_ufs1(ip, start_lbn, soff, sbap); 472 /* 473 * If the block range spans two block maps, get the second map. 474 */ 475 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) { 476 ssize = len; 477 } else { 478 #ifdef DIAGNOSTIC 479 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn) 480 panic("ffs_reallocblk: start == end"); 481 #endif 482 ssize = len - (idp->in_off + 1); 483 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp)) 484 goto fail; 485 ebap = (ufs1_daddr_t *)ebp->b_data; 486 } 487 /* 488 * Search the block map looking for an allocation of the desired size. 489 */ 490 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref, 491 len, ffs_clusteralloc)) == 0) 492 goto fail; 493 /* 494 * We have found a new contiguous block. 495 * 496 * First we have to replace the old block pointers with the new 497 * block pointers in the inode and indirect blocks associated 498 * with the file. 499 */ 500 #ifdef DEBUG 501 if (prtrealloc) 502 printf("realloc: ino %d, lbns %lld-%lld\n\told:", ip->i_number, 503 (intmax_t)start_lbn, (intmax_t)end_lbn); 504 #endif 505 blkno = newblk; 506 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) { 507 if (i == ssize) { 508 bap = ebap; 509 soff = -i; 510 } 511 #ifdef DIAGNOSTIC 512 if (!ffs_checkblk(ip, 513 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 514 panic("ffs_reallocblks: unallocated block 2"); 515 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap) 516 panic("ffs_reallocblks: alloc mismatch"); 517 #endif 518 #ifdef DEBUG 519 if (prtrealloc) 520 printf(" %d,", *bap); 521 #endif 522 if (DOINGSOFTDEP(vp)) { 523 if (sbap == &ip->i_din1->di_db[0] && i < ssize) 524 softdep_setup_allocdirect(ip, start_lbn + i, 525 blkno, *bap, fs->fs_bsize, fs->fs_bsize, 526 buflist->bs_children[i]); 527 else 528 softdep_setup_allocindir_page(ip, start_lbn + i, 529 i < ssize ? sbp : ebp, soff + i, blkno, 530 *bap, buflist->bs_children[i]); 531 } 532 *bap++ = blkno; 533 } 534 /* 535 * Next we must write out the modified inode and indirect blocks. 536 * For strict correctness, the writes should be synchronous since 537 * the old block values may have been written to disk. In practise 538 * they are almost never written, but if we are concerned about 539 * strict correctness, the `doasyncfree' flag should be set to zero. 540 * 541 * The test on `doasyncfree' should be changed to test a flag 542 * that shows whether the associated buffers and inodes have 543 * been written. The flag should be set when the cluster is 544 * started and cleared whenever the buffer or inode is flushed. 545 * We can then check below to see if it is set, and do the 546 * synchronous write only when it has been cleared. 547 */ 548 if (sbap != &ip->i_din1->di_db[0]) { 549 if (doasyncfree) 550 bdwrite(sbp); 551 else 552 bwrite(sbp); 553 } else { 554 ip->i_flag |= IN_CHANGE | IN_UPDATE; 555 if (!doasyncfree) 556 UFS_UPDATE(vp, 1); 557 } 558 if (ssize < len) { 559 if (doasyncfree) 560 bdwrite(ebp); 561 else 562 bwrite(ebp); 563 } 564 /* 565 * Last, free the old blocks and assign the new blocks to the buffers. 566 */ 567 #ifdef DEBUG 568 if (prtrealloc) 569 printf("\n\tnew:"); 570 #endif 571 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) { 572 if (!DOINGSOFTDEP(vp)) 573 ffs_blkfree(fs, ip->i_devvp, 574 dbtofsb(fs, buflist->bs_children[i]->b_blkno), 575 fs->fs_bsize, ip->i_number); 576 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 577 #ifdef DIAGNOSTIC 578 if (!ffs_checkblk(ip, 579 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 580 panic("ffs_reallocblks: unallocated block 3"); 581 #endif 582 #ifdef DEBUG 583 if (prtrealloc) 584 printf(" %d,", blkno); 585 #endif 586 } 587 #ifdef DEBUG 588 if (prtrealloc) { 589 prtrealloc--; 590 printf("\n"); 591 } 592 #endif 593 return (0); 594 595 fail: 596 if (ssize < len) 597 brelse(ebp); 598 if (sbap != &ip->i_din1->di_db[0]) 599 brelse(sbp); 600 return (ENOSPC); 601 } 602 603 static int 604 ffs_reallocblks_ufs2(ap) 605 struct vop_reallocblks_args /* { 606 struct vnode *a_vp; 607 struct cluster_save *a_buflist; 608 } */ *ap; 609 { 610 struct fs *fs; 611 struct inode *ip; 612 struct vnode *vp; 613 struct buf *sbp, *ebp; 614 ufs2_daddr_t *bap, *sbap, *ebap = 0; 615 struct cluster_save *buflist; 616 ufs_lbn_t start_lbn, end_lbn; 617 ufs2_daddr_t soff, newblk, blkno, pref; 618 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp; 619 int i, len, start_lvl, end_lvl, ssize; 620 621 vp = ap->a_vp; 622 ip = VTOI(vp); 623 fs = ip->i_fs; 624 if (fs->fs_contigsumsize <= 0) 625 return (ENOSPC); 626 buflist = ap->a_buflist; 627 len = buflist->bs_nchildren; 628 start_lbn = buflist->bs_children[0]->b_lblkno; 629 end_lbn = start_lbn + len - 1; 630 #ifdef DIAGNOSTIC 631 for (i = 0; i < len; i++) 632 if (!ffs_checkblk(ip, 633 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 634 panic("ffs_reallocblks: unallocated block 1"); 635 for (i = 1; i < len; i++) 636 if (buflist->bs_children[i]->b_lblkno != start_lbn + i) 637 panic("ffs_reallocblks: non-logical cluster"); 638 blkno = buflist->bs_children[0]->b_blkno; 639 ssize = fsbtodb(fs, fs->fs_frag); 640 for (i = 1; i < len - 1; i++) 641 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize)) 642 panic("ffs_reallocblks: non-physical cluster %d", i); 643 #endif 644 /* 645 * If the latest allocation is in a new cylinder group, assume that 646 * the filesystem has decided to move and do not force it back to 647 * the previous cylinder group. 648 */ 649 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) != 650 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno))) 651 return (ENOSPC); 652 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) || 653 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl)) 654 return (ENOSPC); 655 /* 656 * Get the starting offset and block map for the first block. 657 */ 658 if (start_lvl == 0) { 659 sbap = &ip->i_din2->di_db[0]; 660 soff = start_lbn; 661 } else { 662 idp = &start_ap[start_lvl - 1]; 663 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) { 664 brelse(sbp); 665 return (ENOSPC); 666 } 667 sbap = (ufs2_daddr_t *)sbp->b_data; 668 soff = idp->in_off; 669 } 670 /* 671 * Find the preferred location for the cluster. 672 */ 673 pref = ffs_blkpref_ufs2(ip, start_lbn, soff, sbap); 674 /* 675 * If the block range spans two block maps, get the second map. 676 */ 677 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) { 678 ssize = len; 679 } else { 680 #ifdef DIAGNOSTIC 681 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn) 682 panic("ffs_reallocblk: start == end"); 683 #endif 684 ssize = len - (idp->in_off + 1); 685 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp)) 686 goto fail; 687 ebap = (ufs2_daddr_t *)ebp->b_data; 688 } 689 /* 690 * Search the block map looking for an allocation of the desired size. 691 */ 692 if ((newblk = ffs_hashalloc(ip, dtog(fs, pref), pref, 693 len, ffs_clusteralloc)) == 0) 694 goto fail; 695 /* 696 * We have found a new contiguous block. 697 * 698 * First we have to replace the old block pointers with the new 699 * block pointers in the inode and indirect blocks associated 700 * with the file. 701 */ 702 #ifdef DEBUG 703 if (prtrealloc) 704 printf("realloc: ino %d, lbns %lld-%lld\n\told:", ip->i_number, 705 (intmax_t)start_lbn, (intmax_t)end_lbn); 706 #endif 707 blkno = newblk; 708 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) { 709 if (i == ssize) { 710 bap = ebap; 711 soff = -i; 712 } 713 #ifdef DIAGNOSTIC 714 if (!ffs_checkblk(ip, 715 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 716 panic("ffs_reallocblks: unallocated block 2"); 717 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap) 718 panic("ffs_reallocblks: alloc mismatch"); 719 #endif 720 #ifdef DEBUG 721 if (prtrealloc) 722 printf(" %lld,", (intmax_t)*bap); 723 #endif 724 if (DOINGSOFTDEP(vp)) { 725 if (sbap == &ip->i_din2->di_db[0] && i < ssize) 726 softdep_setup_allocdirect(ip, start_lbn + i, 727 blkno, *bap, fs->fs_bsize, fs->fs_bsize, 728 buflist->bs_children[i]); 729 else 730 softdep_setup_allocindir_page(ip, start_lbn + i, 731 i < ssize ? sbp : ebp, soff + i, blkno, 732 *bap, buflist->bs_children[i]); 733 } 734 *bap++ = blkno; 735 } 736 /* 737 * Next we must write out the modified inode and indirect blocks. 738 * For strict correctness, the writes should be synchronous since 739 * the old block values may have been written to disk. In practise 740 * they are almost never written, but if we are concerned about 741 * strict correctness, the `doasyncfree' flag should be set to zero. 742 * 743 * The test on `doasyncfree' should be changed to test a flag 744 * that shows whether the associated buffers and inodes have 745 * been written. The flag should be set when the cluster is 746 * started and cleared whenever the buffer or inode is flushed. 747 * We can then check below to see if it is set, and do the 748 * synchronous write only when it has been cleared. 749 */ 750 if (sbap != &ip->i_din2->di_db[0]) { 751 if (doasyncfree) 752 bdwrite(sbp); 753 else 754 bwrite(sbp); 755 } else { 756 ip->i_flag |= IN_CHANGE | IN_UPDATE; 757 if (!doasyncfree) 758 UFS_UPDATE(vp, 1); 759 } 760 if (ssize < len) { 761 if (doasyncfree) 762 bdwrite(ebp); 763 else 764 bwrite(ebp); 765 } 766 /* 767 * Last, free the old blocks and assign the new blocks to the buffers. 768 */ 769 #ifdef DEBUG 770 if (prtrealloc) 771 printf("\n\tnew:"); 772 #endif 773 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) { 774 if (!DOINGSOFTDEP(vp)) 775 ffs_blkfree(fs, ip->i_devvp, 776 dbtofsb(fs, buflist->bs_children[i]->b_blkno), 777 fs->fs_bsize, ip->i_number); 778 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno); 779 #ifdef DIAGNOSTIC 780 if (!ffs_checkblk(ip, 781 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize)) 782 panic("ffs_reallocblks: unallocated block 3"); 783 #endif 784 #ifdef DEBUG 785 if (prtrealloc) 786 printf(" %jd,", (intmax_t)blkno); 787 #endif 788 } 789 #ifdef DEBUG 790 if (prtrealloc) { 791 prtrealloc--; 792 printf("\n"); 793 } 794 #endif 795 return (0); 796 797 fail: 798 if (ssize < len) 799 brelse(ebp); 800 if (sbap != &ip->i_din2->di_db[0]) 801 brelse(sbp); 802 return (ENOSPC); 803 } 804 805 /* 806 * Allocate an inode in the filesystem. 807 * 808 * If allocating a directory, use ffs_dirpref to select the inode. 809 * If allocating in a directory, the following hierarchy is followed: 810 * 1) allocate the preferred inode. 811 * 2) allocate an inode in the same cylinder group. 812 * 3) quadradically rehash into other cylinder groups, until an 813 * available inode is located. 814 * If no inode preference is given the following heirarchy is used 815 * to allocate an inode: 816 * 1) allocate an inode in cylinder group 0. 817 * 2) quadradically rehash into other cylinder groups, until an 818 * available inode is located. 819 */ 820 int 821 ffs_valloc(pvp, mode, cred, vpp) 822 struct vnode *pvp; 823 int mode; 824 struct ucred *cred; 825 struct vnode **vpp; 826 { 827 struct inode *pip; 828 struct fs *fs; 829 struct inode *ip; 830 struct timespec ts; 831 ino_t ino, ipref; 832 int cg, error; 833 834 *vpp = NULL; 835 pip = VTOI(pvp); 836 fs = pip->i_fs; 837 if (fs->fs_cstotal.cs_nifree == 0) 838 goto noinodes; 839 840 if ((mode & IFMT) == IFDIR) 841 ipref = ffs_dirpref(pip); 842 else 843 ipref = pip->i_number; 844 if (ipref >= fs->fs_ncg * fs->fs_ipg) 845 ipref = 0; 846 cg = ino_to_cg(fs, ipref); 847 /* 848 * Track number of dirs created one after another 849 * in a same cg without intervening by files. 850 */ 851 if ((mode & IFMT) == IFDIR) { 852 if (fs->fs_contigdirs[cg] < 255) 853 fs->fs_contigdirs[cg]++; 854 } else { 855 if (fs->fs_contigdirs[cg] > 0) 856 fs->fs_contigdirs[cg]--; 857 } 858 ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, 859 (allocfcn_t *)ffs_nodealloccg); 860 if (ino == 0) 861 goto noinodes; 862 error = VFS_VGET(pvp->v_mount, ino, LK_EXCLUSIVE, vpp); 863 if (error) { 864 UFS_VFREE(pvp, ino, mode); 865 return (error); 866 } 867 ip = VTOI(*vpp); 868 if (ip->i_mode) { 869 printf("mode = 0%o, inum = %lu, fs = %s\n", 870 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt); 871 panic("ffs_valloc: dup alloc"); 872 } 873 if (DIP(ip, i_blocks) && (fs->fs_flags & FS_UNCLEAN) == 0) { /* XXX */ 874 printf("free inode %s/%lu had %ld blocks\n", 875 fs->fs_fsmnt, (u_long)ino, (long)DIP(ip, i_blocks)); 876 DIP(ip, i_blocks) = 0; 877 } 878 ip->i_flags = 0; 879 DIP(ip, i_flags) = 0; 880 /* 881 * Set up a new generation number for this inode. 882 */ 883 if (ip->i_gen == 0 || ++ip->i_gen == 0) 884 ip->i_gen = random() / 2 + 1; 885 DIP(ip, i_gen) = ip->i_gen; 886 if (fs->fs_magic == FS_UFS2_MAGIC) { 887 vfs_timestamp(&ts); 888 ip->i_din2->di_birthtime = ts.tv_sec; 889 ip->i_din2->di_birthnsec = ts.tv_nsec; 890 } 891 return (0); 892 noinodes: 893 ffs_fserr(fs, pip->i_number, "out of inodes"); 894 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); 895 return (ENOSPC); 896 } 897 898 /* 899 * Find a cylinder group to place a directory. 900 * 901 * The policy implemented by this algorithm is to allocate a 902 * directory inode in the same cylinder group as its parent 903 * directory, but also to reserve space for its files inodes 904 * and data. Restrict the number of directories which may be 905 * allocated one after another in the same cylinder group 906 * without intervening allocation of files. 907 * 908 * If we allocate a first level directory then force allocation 909 * in another cylinder group. 910 */ 911 static ino_t 912 ffs_dirpref(pip) 913 struct inode *pip; 914 { 915 struct fs *fs; 916 int cg, prefcg, dirsize, cgsize; 917 int avgifree, avgbfree, avgndir, curdirsize; 918 int minifree, minbfree, maxndir; 919 int mincg, minndir; 920 int maxcontigdirs; 921 922 fs = pip->i_fs; 923 924 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; 925 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 926 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg; 927 928 /* 929 * Force allocation in another cg if creating a first level dir. 930 */ 931 ASSERT_VOP_LOCKED(ITOV(pip), "ffs_dirpref"); 932 if (ITOV(pip)->v_vflag & VV_ROOT) { 933 prefcg = arc4random() % fs->fs_ncg; 934 mincg = prefcg; 935 minndir = fs->fs_ipg; 936 for (cg = prefcg; cg < fs->fs_ncg; cg++) 937 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 938 fs->fs_cs(fs, cg).cs_nifree >= avgifree && 939 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 940 mincg = cg; 941 minndir = fs->fs_cs(fs, cg).cs_ndir; 942 } 943 for (cg = 0; cg < prefcg; cg++) 944 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 945 fs->fs_cs(fs, cg).cs_nifree >= avgifree && 946 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 947 mincg = cg; 948 minndir = fs->fs_cs(fs, cg).cs_ndir; 949 } 950 return ((ino_t)(fs->fs_ipg * mincg)); 951 } 952 953 /* 954 * Count various limits which used for 955 * optimal allocation of a directory inode. 956 */ 957 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg); 958 minifree = avgifree - fs->fs_ipg / 4; 959 if (minifree < 0) 960 minifree = 0; 961 minbfree = avgbfree - fs->fs_fpg / fs->fs_frag / 4; 962 if (minbfree < 0) 963 minbfree = 0; 964 cgsize = fs->fs_fsize * fs->fs_fpg; 965 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir; 966 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0; 967 if (dirsize < curdirsize) 968 dirsize = curdirsize; 969 maxcontigdirs = min(cgsize / dirsize, 255); 970 if (fs->fs_avgfpdir > 0) 971 maxcontigdirs = min(maxcontigdirs, 972 fs->fs_ipg / fs->fs_avgfpdir); 973 if (maxcontigdirs == 0) 974 maxcontigdirs = 1; 975 976 /* 977 * Limit number of dirs in one cg and reserve space for 978 * regular files, but only if we have no deficit in 979 * inodes or space. 980 */ 981 prefcg = ino_to_cg(fs, pip->i_number); 982 for (cg = prefcg; cg < fs->fs_ncg; cg++) 983 if (fs->fs_cs(fs, cg).cs_ndir < maxndir && 984 fs->fs_cs(fs, cg).cs_nifree >= minifree && 985 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { 986 if (fs->fs_contigdirs[cg] < maxcontigdirs) 987 return ((ino_t)(fs->fs_ipg * cg)); 988 } 989 for (cg = 0; cg < prefcg; cg++) 990 if (fs->fs_cs(fs, cg).cs_ndir < maxndir && 991 fs->fs_cs(fs, cg).cs_nifree >= minifree && 992 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { 993 if (fs->fs_contigdirs[cg] < maxcontigdirs) 994 return ((ino_t)(fs->fs_ipg * cg)); 995 } 996 /* 997 * This is a backstop when we have deficit in space. 998 */ 999 for (cg = prefcg; cg < fs->fs_ncg; cg++) 1000 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) 1001 return ((ino_t)(fs->fs_ipg * cg)); 1002 for (cg = 0; cg < prefcg; cg++) 1003 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) 1004 break; 1005 return ((ino_t)(fs->fs_ipg * cg)); 1006 } 1007 1008 /* 1009 * Select the desired position for the next block in a file. The file is 1010 * logically divided into sections. The first section is composed of the 1011 * direct blocks. Each additional section contains fs_maxbpg blocks. 1012 * 1013 * If no blocks have been allocated in the first section, the policy is to 1014 * request a block in the same cylinder group as the inode that describes 1015 * the file. If no blocks have been allocated in any other section, the 1016 * policy is to place the section in a cylinder group with a greater than 1017 * average number of free blocks. An appropriate cylinder group is found 1018 * by using a rotor that sweeps the cylinder groups. When a new group of 1019 * blocks is needed, the sweep begins in the cylinder group following the 1020 * cylinder group from which the previous allocation was made. The sweep 1021 * continues until a cylinder group with greater than the average number 1022 * of free blocks is found. If the allocation is for the first block in an 1023 * indirect block, the information on the previous allocation is unavailable; 1024 * here a best guess is made based upon the logical block number being 1025 * allocated. 1026 * 1027 * If a section is already partially allocated, the policy is to 1028 * contiguously allocate fs_maxcontig blocks. The end of one of these 1029 * contiguous blocks and the beginning of the next is laid out 1030 * contiguously if possible. 1031 */ 1032 ufs2_daddr_t 1033 ffs_blkpref_ufs1(ip, lbn, indx, bap) 1034 struct inode *ip; 1035 ufs_lbn_t lbn; 1036 int indx; 1037 ufs1_daddr_t *bap; 1038 { 1039 struct fs *fs; 1040 int cg; 1041 int avgbfree, startcg; 1042 1043 fs = ip->i_fs; 1044 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 1045 if (lbn < NDADDR + NINDIR(fs)) { 1046 cg = ino_to_cg(fs, ip->i_number); 1047 return (fs->fs_fpg * cg + fs->fs_frag); 1048 } 1049 /* 1050 * Find a cylinder with greater than average number of 1051 * unused data blocks. 1052 */ 1053 if (indx == 0 || bap[indx - 1] == 0) 1054 startcg = 1055 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 1056 else 1057 startcg = dtog(fs, bap[indx - 1]) + 1; 1058 startcg %= fs->fs_ncg; 1059 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 1060 for (cg = startcg; cg < fs->fs_ncg; cg++) 1061 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1062 fs->fs_cgrotor = cg; 1063 return (fs->fs_fpg * cg + fs->fs_frag); 1064 } 1065 for (cg = 0; cg <= startcg; cg++) 1066 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1067 fs->fs_cgrotor = cg; 1068 return (fs->fs_fpg * cg + fs->fs_frag); 1069 } 1070 return (0); 1071 } 1072 /* 1073 * We just always try to lay things out contiguously. 1074 */ 1075 return (bap[indx - 1] + fs->fs_frag); 1076 } 1077 1078 /* 1079 * Same as above, but for UFS2 1080 */ 1081 ufs2_daddr_t 1082 ffs_blkpref_ufs2(ip, lbn, indx, bap) 1083 struct inode *ip; 1084 ufs_lbn_t lbn; 1085 int indx; 1086 ufs2_daddr_t *bap; 1087 { 1088 struct fs *fs; 1089 int cg; 1090 int avgbfree, startcg; 1091 1092 fs = ip->i_fs; 1093 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 1094 if (lbn < NDADDR + NINDIR(fs)) { 1095 cg = ino_to_cg(fs, ip->i_number); 1096 return (fs->fs_fpg * cg + fs->fs_frag); 1097 } 1098 /* 1099 * Find a cylinder with greater than average number of 1100 * unused data blocks. 1101 */ 1102 if (indx == 0 || bap[indx - 1] == 0) 1103 startcg = 1104 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 1105 else 1106 startcg = dtog(fs, bap[indx - 1]) + 1; 1107 startcg %= fs->fs_ncg; 1108 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 1109 for (cg = startcg; cg < fs->fs_ncg; cg++) 1110 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1111 fs->fs_cgrotor = cg; 1112 return (fs->fs_fpg * cg + fs->fs_frag); 1113 } 1114 for (cg = 0; cg <= startcg; cg++) 1115 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1116 fs->fs_cgrotor = cg; 1117 return (fs->fs_fpg * cg + fs->fs_frag); 1118 } 1119 return (0); 1120 } 1121 /* 1122 * We just always try to lay things out contiguously. 1123 */ 1124 return (bap[indx - 1] + fs->fs_frag); 1125 } 1126 1127 /* 1128 * Implement the cylinder overflow algorithm. 1129 * 1130 * The policy implemented by this algorithm is: 1131 * 1) allocate the block in its requested cylinder group. 1132 * 2) quadradically rehash on the cylinder group number. 1133 * 3) brute force search for a free block. 1134 */ 1135 /*VARARGS5*/ 1136 static ufs2_daddr_t 1137 ffs_hashalloc(ip, cg, pref, size, allocator) 1138 struct inode *ip; 1139 int cg; 1140 ufs2_daddr_t pref; 1141 int size; /* size for data blocks, mode for inodes */ 1142 allocfcn_t *allocator; 1143 { 1144 struct fs *fs; 1145 ufs2_daddr_t result; 1146 int i, icg = cg; 1147 1148 #ifdef DIAGNOSTIC 1149 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED) 1150 panic("ffs_hashalloc: allocation on suspended filesystem"); 1151 #endif 1152 fs = ip->i_fs; 1153 /* 1154 * 1: preferred cylinder group 1155 */ 1156 result = (*allocator)(ip, cg, pref, size); 1157 if (result) 1158 return (result); 1159 /* 1160 * 2: quadratic rehash 1161 */ 1162 for (i = 1; i < fs->fs_ncg; i *= 2) { 1163 cg += i; 1164 if (cg >= fs->fs_ncg) 1165 cg -= fs->fs_ncg; 1166 result = (*allocator)(ip, cg, 0, size); 1167 if (result) 1168 return (result); 1169 } 1170 /* 1171 * 3: brute force search 1172 * Note that we start at i == 2, since 0 was checked initially, 1173 * and 1 is always checked in the quadratic rehash. 1174 */ 1175 cg = (icg + 2) % fs->fs_ncg; 1176 for (i = 2; i < fs->fs_ncg; i++) { 1177 result = (*allocator)(ip, cg, 0, size); 1178 if (result) 1179 return (result); 1180 cg++; 1181 if (cg == fs->fs_ncg) 1182 cg = 0; 1183 } 1184 return (0); 1185 } 1186 1187 /* 1188 * Determine whether a fragment can be extended. 1189 * 1190 * Check to see if the necessary fragments are available, and 1191 * if they are, allocate them. 1192 */ 1193 static ufs2_daddr_t 1194 ffs_fragextend(ip, cg, bprev, osize, nsize) 1195 struct inode *ip; 1196 int cg; 1197 ufs2_daddr_t bprev; 1198 int osize, nsize; 1199 { 1200 struct fs *fs; 1201 struct cg *cgp; 1202 struct buf *bp; 1203 long bno; 1204 int frags, bbase; 1205 int i, error; 1206 u_int8_t *blksfree; 1207 1208 fs = ip->i_fs; 1209 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 1210 return (0); 1211 frags = numfrags(fs, nsize); 1212 bbase = fragnum(fs, bprev); 1213 if (bbase > fragnum(fs, (bprev + frags - 1))) { 1214 /* cannot extend across a block boundary */ 1215 return (0); 1216 } 1217 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1218 (int)fs->fs_cgsize, NOCRED, &bp); 1219 if (error) { 1220 brelse(bp); 1221 return (0); 1222 } 1223 cgp = (struct cg *)bp->b_data; 1224 if (!cg_chkmagic(cgp)) { 1225 brelse(bp); 1226 return (0); 1227 } 1228 bp->b_xflags |= BX_BKGRDWRITE; 1229 cgp->cg_old_time = cgp->cg_time = time_second; 1230 bno = dtogd(fs, bprev); 1231 blksfree = cg_blksfree(cgp); 1232 for (i = numfrags(fs, osize); i < frags; i++) 1233 if (isclr(blksfree, bno + i)) { 1234 brelse(bp); 1235 return (0); 1236 } 1237 /* 1238 * the current fragment can be extended 1239 * deduct the count on fragment being extended into 1240 * increase the count on the remaining fragment (if any) 1241 * allocate the extended piece 1242 */ 1243 for (i = frags; i < fs->fs_frag - bbase; i++) 1244 if (isclr(blksfree, bno + i)) 1245 break; 1246 cgp->cg_frsum[i - numfrags(fs, osize)]--; 1247 if (i != frags) 1248 cgp->cg_frsum[i - frags]++; 1249 for (i = numfrags(fs, osize); i < frags; i++) { 1250 clrbit(blksfree, bno + i); 1251 cgp->cg_cs.cs_nffree--; 1252 fs->fs_cstotal.cs_nffree--; 1253 fs->fs_cs(fs, cg).cs_nffree--; 1254 } 1255 fs->fs_fmod = 1; 1256 if (DOINGSOFTDEP(ITOV(ip))) 1257 softdep_setup_blkmapdep(bp, fs, bprev); 1258 if (fs->fs_active != 0) 1259 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1260 bdwrite(bp); 1261 return (bprev); 1262 } 1263 1264 /* 1265 * Determine whether a block can be allocated. 1266 * 1267 * Check to see if a block of the appropriate size is available, 1268 * and if it is, allocate it. 1269 */ 1270 static ufs2_daddr_t 1271 ffs_alloccg(ip, cg, bpref, size) 1272 struct inode *ip; 1273 int cg; 1274 ufs2_daddr_t bpref; 1275 int size; 1276 { 1277 struct fs *fs; 1278 struct cg *cgp; 1279 struct buf *bp; 1280 ufs1_daddr_t bno; 1281 ufs2_daddr_t blkno; 1282 int i, allocsiz, error, frags; 1283 u_int8_t *blksfree; 1284 1285 fs = ip->i_fs; 1286 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 1287 return (0); 1288 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1289 (int)fs->fs_cgsize, NOCRED, &bp); 1290 if (error) { 1291 brelse(bp); 1292 return (0); 1293 } 1294 cgp = (struct cg *)bp->b_data; 1295 if (!cg_chkmagic(cgp) || 1296 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 1297 brelse(bp); 1298 return (0); 1299 } 1300 bp->b_xflags |= BX_BKGRDWRITE; 1301 cgp->cg_old_time = cgp->cg_time = time_second; 1302 if (size == fs->fs_bsize) { 1303 blkno = ffs_alloccgblk(ip, bp, bpref); 1304 if (fs->fs_active != 0) 1305 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1306 bdwrite(bp); 1307 return (blkno); 1308 } 1309 /* 1310 * check to see if any fragments are already available 1311 * allocsiz is the size which will be allocated, hacking 1312 * it down to a smaller size if necessary 1313 */ 1314 blksfree = cg_blksfree(cgp); 1315 frags = numfrags(fs, size); 1316 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 1317 if (cgp->cg_frsum[allocsiz] != 0) 1318 break; 1319 if (allocsiz == fs->fs_frag) { 1320 /* 1321 * no fragments were available, so a block will be 1322 * allocated, and hacked up 1323 */ 1324 if (cgp->cg_cs.cs_nbfree == 0) { 1325 brelse(bp); 1326 return (0); 1327 } 1328 blkno = ffs_alloccgblk(ip, bp, bpref); 1329 bno = dtogd(fs, blkno); 1330 for (i = frags; i < fs->fs_frag; i++) 1331 setbit(blksfree, bno + i); 1332 i = fs->fs_frag - frags; 1333 cgp->cg_cs.cs_nffree += i; 1334 fs->fs_cstotal.cs_nffree += i; 1335 fs->fs_cs(fs, cg).cs_nffree += i; 1336 fs->fs_fmod = 1; 1337 cgp->cg_frsum[i]++; 1338 if (fs->fs_active != 0) 1339 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1340 bdwrite(bp); 1341 return (blkno); 1342 } 1343 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 1344 if (bno < 0) { 1345 brelse(bp); 1346 return (0); 1347 } 1348 for (i = 0; i < frags; i++) 1349 clrbit(blksfree, bno + i); 1350 cgp->cg_cs.cs_nffree -= frags; 1351 fs->fs_cstotal.cs_nffree -= frags; 1352 fs->fs_cs(fs, cg).cs_nffree -= frags; 1353 fs->fs_fmod = 1; 1354 cgp->cg_frsum[allocsiz]--; 1355 if (frags != allocsiz) 1356 cgp->cg_frsum[allocsiz - frags]++; 1357 blkno = cg * fs->fs_fpg + bno; 1358 if (DOINGSOFTDEP(ITOV(ip))) 1359 softdep_setup_blkmapdep(bp, fs, blkno); 1360 if (fs->fs_active != 0) 1361 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1362 bdwrite(bp); 1363 return (blkno); 1364 } 1365 1366 /* 1367 * Allocate a block in a cylinder group. 1368 * 1369 * This algorithm implements the following policy: 1370 * 1) allocate the requested block. 1371 * 2) allocate a rotationally optimal block in the same cylinder. 1372 * 3) allocate the next available block on the block rotor for the 1373 * specified cylinder group. 1374 * Note that this routine only allocates fs_bsize blocks; these 1375 * blocks may be fragmented by the routine that allocates them. 1376 */ 1377 static ufs2_daddr_t 1378 ffs_alloccgblk(ip, bp, bpref) 1379 struct inode *ip; 1380 struct buf *bp; 1381 ufs2_daddr_t bpref; 1382 { 1383 struct fs *fs; 1384 struct cg *cgp; 1385 ufs1_daddr_t bno; 1386 ufs2_daddr_t blkno; 1387 u_int8_t *blksfree; 1388 1389 fs = ip->i_fs; 1390 cgp = (struct cg *)bp->b_data; 1391 blksfree = cg_blksfree(cgp); 1392 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) { 1393 bpref = cgp->cg_rotor; 1394 } else { 1395 bpref = blknum(fs, bpref); 1396 bno = dtogd(fs, bpref); 1397 /* 1398 * if the requested block is available, use it 1399 */ 1400 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno))) 1401 goto gotit; 1402 } 1403 /* 1404 * Take the next available block in this cylinder group. 1405 */ 1406 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 1407 if (bno < 0) 1408 return (0); 1409 cgp->cg_rotor = bno; 1410 gotit: 1411 blkno = fragstoblks(fs, bno); 1412 ffs_clrblock(fs, blksfree, (long)blkno); 1413 ffs_clusteracct(fs, cgp, blkno, -1); 1414 cgp->cg_cs.cs_nbfree--; 1415 fs->fs_cstotal.cs_nbfree--; 1416 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 1417 fs->fs_fmod = 1; 1418 blkno = cgp->cg_cgx * fs->fs_fpg + bno; 1419 if (DOINGSOFTDEP(ITOV(ip))) 1420 softdep_setup_blkmapdep(bp, fs, blkno); 1421 return (blkno); 1422 } 1423 1424 /* 1425 * Determine whether a cluster can be allocated. 1426 * 1427 * We do not currently check for optimal rotational layout if there 1428 * are multiple choices in the same cylinder group. Instead we just 1429 * take the first one that we find following bpref. 1430 */ 1431 static ufs2_daddr_t 1432 ffs_clusteralloc(ip, cg, bpref, len) 1433 struct inode *ip; 1434 int cg; 1435 ufs2_daddr_t bpref; 1436 int len; 1437 { 1438 struct fs *fs; 1439 struct cg *cgp; 1440 struct buf *bp; 1441 int i, run, bit, map, got; 1442 ufs2_daddr_t bno; 1443 u_char *mapp; 1444 int32_t *lp; 1445 u_int8_t *blksfree; 1446 1447 fs = ip->i_fs; 1448 if (fs->fs_maxcluster[cg] < len) 1449 return (0); 1450 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, 1451 NOCRED, &bp)) 1452 goto fail; 1453 cgp = (struct cg *)bp->b_data; 1454 if (!cg_chkmagic(cgp)) 1455 goto fail; 1456 bp->b_xflags |= BX_BKGRDWRITE; 1457 /* 1458 * Check to see if a cluster of the needed size (or bigger) is 1459 * available in this cylinder group. 1460 */ 1461 lp = &cg_clustersum(cgp)[len]; 1462 for (i = len; i <= fs->fs_contigsumsize; i++) 1463 if (*lp++ > 0) 1464 break; 1465 if (i > fs->fs_contigsumsize) { 1466 /* 1467 * This is the first time looking for a cluster in this 1468 * cylinder group. Update the cluster summary information 1469 * to reflect the true maximum sized cluster so that 1470 * future cluster allocation requests can avoid reading 1471 * the cylinder group map only to find no clusters. 1472 */ 1473 lp = &cg_clustersum(cgp)[len - 1]; 1474 for (i = len - 1; i > 0; i--) 1475 if (*lp-- > 0) 1476 break; 1477 fs->fs_maxcluster[cg] = i; 1478 goto fail; 1479 } 1480 /* 1481 * Search the cluster map to find a big enough cluster. 1482 * We take the first one that we find, even if it is larger 1483 * than we need as we prefer to get one close to the previous 1484 * block allocation. We do not search before the current 1485 * preference point as we do not want to allocate a block 1486 * that is allocated before the previous one (as we will 1487 * then have to wait for another pass of the elevator 1488 * algorithm before it will be read). We prefer to fail and 1489 * be recalled to try an allocation in the next cylinder group. 1490 */ 1491 if (dtog(fs, bpref) != cg) 1492 bpref = 0; 1493 else 1494 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref))); 1495 mapp = &cg_clustersfree(cgp)[bpref / NBBY]; 1496 map = *mapp++; 1497 bit = 1 << (bpref % NBBY); 1498 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) { 1499 if ((map & bit) == 0) { 1500 run = 0; 1501 } else { 1502 run++; 1503 if (run == len) 1504 break; 1505 } 1506 if ((got & (NBBY - 1)) != (NBBY - 1)) { 1507 bit <<= 1; 1508 } else { 1509 map = *mapp++; 1510 bit = 1; 1511 } 1512 } 1513 if (got >= cgp->cg_nclusterblks) 1514 goto fail; 1515 /* 1516 * Allocate the cluster that we have found. 1517 */ 1518 blksfree = cg_blksfree(cgp); 1519 for (i = 1; i <= len; i++) 1520 if (!ffs_isblock(fs, blksfree, got - run + i)) 1521 panic("ffs_clusteralloc: map mismatch"); 1522 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1); 1523 if (dtog(fs, bno) != cg) 1524 panic("ffs_clusteralloc: allocated out of group"); 1525 len = blkstofrags(fs, len); 1526 for (i = 0; i < len; i += fs->fs_frag) 1527 if (ffs_alloccgblk(ip, bp, bno + i) != bno + i) 1528 panic("ffs_clusteralloc: lost block"); 1529 if (fs->fs_active != 0) 1530 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1531 bdwrite(bp); 1532 return (bno); 1533 1534 fail: 1535 brelse(bp); 1536 return (0); 1537 } 1538 1539 /* 1540 * Determine whether an inode can be allocated. 1541 * 1542 * Check to see if an inode is available, and if it is, 1543 * allocate it using the following policy: 1544 * 1) allocate the requested inode. 1545 * 2) allocate the next available inode after the requested 1546 * inode in the specified cylinder group. 1547 */ 1548 static ufs2_daddr_t 1549 ffs_nodealloccg(ip, cg, ipref, mode) 1550 struct inode *ip; 1551 int cg; 1552 ufs2_daddr_t ipref; 1553 int mode; 1554 { 1555 struct fs *fs; 1556 struct cg *cgp; 1557 struct buf *bp, *ibp; 1558 u_int8_t *inosused; 1559 struct ufs2_dinode *dp2; 1560 int error, start, len, loc, map, i; 1561 1562 fs = ip->i_fs; 1563 if (fs->fs_cs(fs, cg).cs_nifree == 0) 1564 return (0); 1565 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1566 (int)fs->fs_cgsize, NOCRED, &bp); 1567 if (error) { 1568 brelse(bp); 1569 return (0); 1570 } 1571 cgp = (struct cg *)bp->b_data; 1572 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 1573 brelse(bp); 1574 return (0); 1575 } 1576 bp->b_xflags |= BX_BKGRDWRITE; 1577 cgp->cg_old_time = cgp->cg_time = time_second; 1578 inosused = cg_inosused(cgp); 1579 if (ipref) { 1580 ipref %= fs->fs_ipg; 1581 if (isclr(inosused, ipref)) 1582 goto gotit; 1583 } 1584 start = cgp->cg_irotor / NBBY; 1585 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 1586 loc = skpc(0xff, len, &inosused[start]); 1587 if (loc == 0) { 1588 len = start + 1; 1589 start = 0; 1590 loc = skpc(0xff, len, &inosused[0]); 1591 if (loc == 0) { 1592 printf("cg = %d, irotor = %ld, fs = %s\n", 1593 cg, (long)cgp->cg_irotor, fs->fs_fsmnt); 1594 panic("ffs_nodealloccg: map corrupted"); 1595 /* NOTREACHED */ 1596 } 1597 } 1598 i = start + len - loc; 1599 map = inosused[i]; 1600 ipref = i * NBBY; 1601 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 1602 if ((map & i) == 0) { 1603 cgp->cg_irotor = ipref; 1604 goto gotit; 1605 } 1606 } 1607 printf("fs = %s\n", fs->fs_fsmnt); 1608 panic("ffs_nodealloccg: block not in map"); 1609 /* NOTREACHED */ 1610 gotit: 1611 if (DOINGSOFTDEP(ITOV(ip))) 1612 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref); 1613 setbit(inosused, ipref); 1614 cgp->cg_cs.cs_nifree--; 1615 fs->fs_cstotal.cs_nifree--; 1616 fs->fs_cs(fs, cg).cs_nifree--; 1617 fs->fs_fmod = 1; 1618 if ((mode & IFMT) == IFDIR) { 1619 cgp->cg_cs.cs_ndir++; 1620 fs->fs_cstotal.cs_ndir++; 1621 fs->fs_cs(fs, cg).cs_ndir++; 1622 } 1623 /* 1624 * Check to see if we need to initialize more inodes. 1625 */ 1626 if (fs->fs_magic == FS_UFS2_MAGIC && 1627 ipref + INOPB(fs) > cgp->cg_initediblk && 1628 cgp->cg_initediblk < cgp->cg_niblk) { 1629 ibp = getblk(ip->i_devvp, fsbtodb(fs, 1630 ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)), 1631 (int)fs->fs_bsize, 0, 0); 1632 bzero(ibp->b_data, (int)fs->fs_bsize); 1633 dp2 = (struct ufs2_dinode *)(ibp->b_data); 1634 for (i = 0; i < INOPB(fs); i++) { 1635 dp2->di_gen = random() / 2 + 1; 1636 dp2++; 1637 } 1638 bawrite(ibp); 1639 cgp->cg_initediblk += INOPB(fs); 1640 } 1641 if (fs->fs_active != 0) 1642 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1643 bdwrite(bp); 1644 return (cg * fs->fs_ipg + ipref); 1645 } 1646 1647 /* 1648 * check if a block is free 1649 */ 1650 static int 1651 ffs_isfreeblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h) 1652 { 1653 1654 switch ((int)fs->fs_frag) { 1655 case 8: 1656 return (cp[h] == 0); 1657 case 4: 1658 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0); 1659 case 2: 1660 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0); 1661 case 1: 1662 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0); 1663 default: 1664 panic("ffs_isfreeblock"); 1665 } 1666 return (0); 1667 } 1668 1669 /* 1670 * Free a block or fragment. 1671 * 1672 * The specified block or fragment is placed back in the 1673 * free map. If a fragment is deallocated, a possible 1674 * block reassembly is checked. 1675 */ 1676 void 1677 ffs_blkfree(fs, devvp, bno, size, inum) 1678 struct fs *fs; 1679 struct vnode *devvp; 1680 ufs2_daddr_t bno; 1681 long size; 1682 ino_t inum; 1683 { 1684 struct cg *cgp; 1685 struct buf *bp; 1686 ufs1_daddr_t fragno, cgbno; 1687 ufs2_daddr_t cgblkno; 1688 int i, error, cg, blk, frags, bbase; 1689 u_int8_t *blksfree; 1690 dev_t dev; 1691 1692 cg = dtog(fs, bno); 1693 if (devvp->v_type != VCHR) { 1694 /* devvp is a snapshot */ 1695 dev = VTOI(devvp)->i_devvp->v_rdev; 1696 cgblkno = fragstoblks(fs, cgtod(fs, cg)); 1697 } else { 1698 /* devvp is a normal disk device */ 1699 dev = devvp->v_rdev; 1700 cgblkno = fsbtodb(fs, cgtod(fs, cg)); 1701 ASSERT_VOP_LOCKED(devvp, "ffs_blkfree"); 1702 if ((devvp->v_vflag & VV_COPYONWRITE) && 1703 ffs_snapblkfree(fs, devvp, bno, size, inum)) 1704 return; 1705 VOP_FREEBLKS(devvp, fsbtodb(fs, bno), size); 1706 } 1707 #ifdef DIAGNOSTIC 1708 if (dev->si_mountpoint && 1709 (dev->si_mountpoint->mnt_kern_flag & MNTK_SUSPENDED)) 1710 panic("ffs_blkfree: deallocation on suspended filesystem"); 1711 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 || 1712 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) { 1713 printf("dev=%s, bno = %lld, bsize = %ld, size = %ld, fs = %s\n", 1714 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize, 1715 size, fs->fs_fsmnt); 1716 panic("ffs_blkfree: bad size"); 1717 } 1718 #endif 1719 if ((u_int)bno >= fs->fs_size) { 1720 printf("bad block %jd, ino %lu\n", (intmax_t)bno, 1721 (u_long)inum); 1722 ffs_fserr(fs, inum, "bad block"); 1723 return; 1724 } 1725 if ((error = bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp))) { 1726 brelse(bp); 1727 return; 1728 } 1729 cgp = (struct cg *)bp->b_data; 1730 if (!cg_chkmagic(cgp)) { 1731 brelse(bp); 1732 return; 1733 } 1734 bp->b_xflags |= BX_BKGRDWRITE; 1735 cgp->cg_old_time = cgp->cg_time = time_second; 1736 cgbno = dtogd(fs, bno); 1737 blksfree = cg_blksfree(cgp); 1738 if (size == fs->fs_bsize) { 1739 fragno = fragstoblks(fs, cgbno); 1740 if (!ffs_isfreeblock(fs, blksfree, fragno)) { 1741 if (devvp->v_type != VCHR) { 1742 /* devvp is a snapshot */ 1743 brelse(bp); 1744 return; 1745 } 1746 printf("dev = %s, block = %jd, fs = %s\n", 1747 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt); 1748 panic("ffs_blkfree: freeing free block"); 1749 } 1750 ffs_setblock(fs, blksfree, fragno); 1751 ffs_clusteracct(fs, cgp, fragno, 1); 1752 cgp->cg_cs.cs_nbfree++; 1753 fs->fs_cstotal.cs_nbfree++; 1754 fs->fs_cs(fs, cg).cs_nbfree++; 1755 } else { 1756 bbase = cgbno - fragnum(fs, cgbno); 1757 /* 1758 * decrement the counts associated with the old frags 1759 */ 1760 blk = blkmap(fs, blksfree, bbase); 1761 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 1762 /* 1763 * deallocate the fragment 1764 */ 1765 frags = numfrags(fs, size); 1766 for (i = 0; i < frags; i++) { 1767 if (isset(blksfree, cgbno + i)) { 1768 printf("dev = %s, block = %jd, fs = %s\n", 1769 devtoname(dev), (intmax_t)(bno + i), 1770 fs->fs_fsmnt); 1771 panic("ffs_blkfree: freeing free frag"); 1772 } 1773 setbit(blksfree, cgbno + i); 1774 } 1775 cgp->cg_cs.cs_nffree += i; 1776 fs->fs_cstotal.cs_nffree += i; 1777 fs->fs_cs(fs, cg).cs_nffree += i; 1778 /* 1779 * add back in counts associated with the new frags 1780 */ 1781 blk = blkmap(fs, blksfree, bbase); 1782 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 1783 /* 1784 * if a complete block has been reassembled, account for it 1785 */ 1786 fragno = fragstoblks(fs, bbase); 1787 if (ffs_isblock(fs, blksfree, fragno)) { 1788 cgp->cg_cs.cs_nffree -= fs->fs_frag; 1789 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 1790 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 1791 ffs_clusteracct(fs, cgp, fragno, 1); 1792 cgp->cg_cs.cs_nbfree++; 1793 fs->fs_cstotal.cs_nbfree++; 1794 fs->fs_cs(fs, cg).cs_nbfree++; 1795 } 1796 } 1797 fs->fs_fmod = 1; 1798 if (fs->fs_active != 0) 1799 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1800 bdwrite(bp); 1801 } 1802 1803 #ifdef DIAGNOSTIC 1804 /* 1805 * Verify allocation of a block or fragment. Returns true if block or 1806 * fragment is allocated, false if it is free. 1807 */ 1808 static int 1809 ffs_checkblk(ip, bno, size) 1810 struct inode *ip; 1811 ufs2_daddr_t bno; 1812 long size; 1813 { 1814 struct fs *fs; 1815 struct cg *cgp; 1816 struct buf *bp; 1817 ufs1_daddr_t cgbno; 1818 int i, error, frags, free; 1819 u_int8_t *blksfree; 1820 1821 fs = ip->i_fs; 1822 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 1823 printf("bsize = %ld, size = %ld, fs = %s\n", 1824 (long)fs->fs_bsize, size, fs->fs_fsmnt); 1825 panic("ffs_checkblk: bad size"); 1826 } 1827 if ((u_int)bno >= fs->fs_size) 1828 panic("ffs_checkblk: bad block %lld", (intmax_t)bno); 1829 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))), 1830 (int)fs->fs_cgsize, NOCRED, &bp); 1831 if (error) 1832 panic("ffs_checkblk: cg bread failed"); 1833 cgp = (struct cg *)bp->b_data; 1834 if (!cg_chkmagic(cgp)) 1835 panic("ffs_checkblk: cg magic mismatch"); 1836 bp->b_xflags |= BX_BKGRDWRITE; 1837 blksfree = cg_blksfree(cgp); 1838 cgbno = dtogd(fs, bno); 1839 if (size == fs->fs_bsize) { 1840 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno)); 1841 } else { 1842 frags = numfrags(fs, size); 1843 for (free = 0, i = 0; i < frags; i++) 1844 if (isset(blksfree, cgbno + i)) 1845 free++; 1846 if (free != 0 && free != frags) 1847 panic("ffs_checkblk: partially free fragment"); 1848 } 1849 brelse(bp); 1850 return (!free); 1851 } 1852 #endif /* DIAGNOSTIC */ 1853 1854 /* 1855 * Free an inode. 1856 */ 1857 int 1858 ffs_vfree(pvp, ino, mode) 1859 struct vnode *pvp; 1860 ino_t ino; 1861 int mode; 1862 { 1863 if (DOINGSOFTDEP(pvp)) { 1864 softdep_freefile(pvp, ino, mode); 1865 return (0); 1866 } 1867 return (ffs_freefile(VTOI(pvp)->i_fs, VTOI(pvp)->i_devvp, ino, mode)); 1868 } 1869 1870 /* 1871 * Do the actual free operation. 1872 * The specified inode is placed back in the free map. 1873 */ 1874 int 1875 ffs_freefile(fs, devvp, ino, mode) 1876 struct fs *fs; 1877 struct vnode *devvp; 1878 ino_t ino; 1879 int mode; 1880 { 1881 struct cg *cgp; 1882 struct buf *bp; 1883 ufs2_daddr_t cgbno; 1884 int error, cg; 1885 u_int8_t *inosused; 1886 dev_t dev; 1887 1888 cg = ino_to_cg(fs, ino); 1889 if (devvp->v_type != VCHR) { 1890 /* devvp is a snapshot */ 1891 dev = VTOI(devvp)->i_devvp->v_rdev; 1892 cgbno = fragstoblks(fs, cgtod(fs, cg)); 1893 } else { 1894 /* devvp is a normal disk device */ 1895 dev = devvp->v_rdev; 1896 cgbno = fsbtodb(fs, cgtod(fs, cg)); 1897 } 1898 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 1899 panic("ffs_vfree: range: dev = %s, ino = %d, fs = %s", 1900 devtoname(dev), ino, fs->fs_fsmnt); 1901 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) { 1902 brelse(bp); 1903 return (error); 1904 } 1905 cgp = (struct cg *)bp->b_data; 1906 if (!cg_chkmagic(cgp)) { 1907 brelse(bp); 1908 return (0); 1909 } 1910 bp->b_xflags |= BX_BKGRDWRITE; 1911 cgp->cg_old_time = cgp->cg_time = time_second; 1912 inosused = cg_inosused(cgp); 1913 ino %= fs->fs_ipg; 1914 if (isclr(inosused, ino)) { 1915 printf("dev = %s, ino = %lu, fs = %s\n", devtoname(dev), 1916 (u_long)ino + cg * fs->fs_ipg, fs->fs_fsmnt); 1917 if (fs->fs_ronly == 0) 1918 panic("ffs_vfree: freeing free inode"); 1919 } 1920 clrbit(inosused, ino); 1921 if (ino < cgp->cg_irotor) 1922 cgp->cg_irotor = ino; 1923 cgp->cg_cs.cs_nifree++; 1924 fs->fs_cstotal.cs_nifree++; 1925 fs->fs_cs(fs, cg).cs_nifree++; 1926 if ((mode & IFMT) == IFDIR) { 1927 cgp->cg_cs.cs_ndir--; 1928 fs->fs_cstotal.cs_ndir--; 1929 fs->fs_cs(fs, cg).cs_ndir--; 1930 } 1931 fs->fs_fmod = 1; 1932 if (fs->fs_active != 0) 1933 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1934 bdwrite(bp); 1935 return (0); 1936 } 1937 1938 /* 1939 * Find a block of the specified size in the specified cylinder group. 1940 * 1941 * It is a panic if a request is made to find a block if none are 1942 * available. 1943 */ 1944 static ufs1_daddr_t 1945 ffs_mapsearch(fs, cgp, bpref, allocsiz) 1946 struct fs *fs; 1947 struct cg *cgp; 1948 ufs2_daddr_t bpref; 1949 int allocsiz; 1950 { 1951 ufs1_daddr_t bno; 1952 int start, len, loc, i; 1953 int blk, field, subfield, pos; 1954 u_int8_t *blksfree; 1955 1956 /* 1957 * find the fragment by searching through the free block 1958 * map for an appropriate bit pattern 1959 */ 1960 if (bpref) 1961 start = dtogd(fs, bpref) / NBBY; 1962 else 1963 start = cgp->cg_frotor / NBBY; 1964 blksfree = cg_blksfree(cgp); 1965 len = howmany(fs->fs_fpg, NBBY) - start; 1966 loc = scanc((u_int)len, (u_char *)&blksfree[start], 1967 (u_char *)fragtbl[fs->fs_frag], 1968 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1969 if (loc == 0) { 1970 len = start + 1; 1971 start = 0; 1972 loc = scanc((u_int)len, (u_char *)&blksfree[0], 1973 (u_char *)fragtbl[fs->fs_frag], 1974 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1975 if (loc == 0) { 1976 printf("start = %d, len = %d, fs = %s\n", 1977 start, len, fs->fs_fsmnt); 1978 panic("ffs_alloccg: map corrupted"); 1979 /* NOTREACHED */ 1980 } 1981 } 1982 bno = (start + len - loc) * NBBY; 1983 cgp->cg_frotor = bno; 1984 /* 1985 * found the byte in the map 1986 * sift through the bits to find the selected frag 1987 */ 1988 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 1989 blk = blkmap(fs, blksfree, bno); 1990 blk <<= 1; 1991 field = around[allocsiz]; 1992 subfield = inside[allocsiz]; 1993 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 1994 if ((blk & field) == subfield) 1995 return (bno + pos); 1996 field <<= 1; 1997 subfield <<= 1; 1998 } 1999 } 2000 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt); 2001 panic("ffs_alloccg: block not in map"); 2002 return (-1); 2003 } 2004 2005 /* 2006 * Update the cluster map because of an allocation or free. 2007 * 2008 * Cnt == 1 means free; cnt == -1 means allocating. 2009 */ 2010 void 2011 ffs_clusteracct(fs, cgp, blkno, cnt) 2012 struct fs *fs; 2013 struct cg *cgp; 2014 ufs1_daddr_t blkno; 2015 int cnt; 2016 { 2017 int32_t *sump; 2018 int32_t *lp; 2019 u_char *freemapp, *mapp; 2020 int i, start, end, forw, back, map, bit; 2021 2022 if (fs->fs_contigsumsize <= 0) 2023 return; 2024 freemapp = cg_clustersfree(cgp); 2025 sump = cg_clustersum(cgp); 2026 /* 2027 * Allocate or clear the actual block. 2028 */ 2029 if (cnt > 0) 2030 setbit(freemapp, blkno); 2031 else 2032 clrbit(freemapp, blkno); 2033 /* 2034 * Find the size of the cluster going forward. 2035 */ 2036 start = blkno + 1; 2037 end = start + fs->fs_contigsumsize; 2038 if (end >= cgp->cg_nclusterblks) 2039 end = cgp->cg_nclusterblks; 2040 mapp = &freemapp[start / NBBY]; 2041 map = *mapp++; 2042 bit = 1 << (start % NBBY); 2043 for (i = start; i < end; i++) { 2044 if ((map & bit) == 0) 2045 break; 2046 if ((i & (NBBY - 1)) != (NBBY - 1)) { 2047 bit <<= 1; 2048 } else { 2049 map = *mapp++; 2050 bit = 1; 2051 } 2052 } 2053 forw = i - start; 2054 /* 2055 * Find the size of the cluster going backward. 2056 */ 2057 start = blkno - 1; 2058 end = start - fs->fs_contigsumsize; 2059 if (end < 0) 2060 end = -1; 2061 mapp = &freemapp[start / NBBY]; 2062 map = *mapp--; 2063 bit = 1 << (start % NBBY); 2064 for (i = start; i > end; i--) { 2065 if ((map & bit) == 0) 2066 break; 2067 if ((i & (NBBY - 1)) != 0) { 2068 bit >>= 1; 2069 } else { 2070 map = *mapp--; 2071 bit = 1 << (NBBY - 1); 2072 } 2073 } 2074 back = start - i; 2075 /* 2076 * Account for old cluster and the possibly new forward and 2077 * back clusters. 2078 */ 2079 i = back + forw + 1; 2080 if (i > fs->fs_contigsumsize) 2081 i = fs->fs_contigsumsize; 2082 sump[i] += cnt; 2083 if (back > 0) 2084 sump[back] -= cnt; 2085 if (forw > 0) 2086 sump[forw] -= cnt; 2087 /* 2088 * Update cluster summary information. 2089 */ 2090 lp = &sump[fs->fs_contigsumsize]; 2091 for (i = fs->fs_contigsumsize; i > 0; i--) 2092 if (*lp-- > 0) 2093 break; 2094 fs->fs_maxcluster[cgp->cg_cgx] = i; 2095 } 2096 2097 /* 2098 * Fserr prints the name of a filesystem with an error diagnostic. 2099 * 2100 * The form of the error message is: 2101 * fs: error message 2102 */ 2103 static void 2104 ffs_fserr(fs, inum, cp) 2105 struct fs *fs; 2106 ino_t inum; 2107 char *cp; 2108 { 2109 struct proc *p = curproc; /* XXX */ 2110 2111 log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n", 2112 p ? p->p_pid : -1, p ? p->p_comm : "-", 2113 p ? p->p_ucred->cr_uid : 0, inum, fs->fs_fsmnt, cp); 2114 } 2115 2116 /* 2117 * This function provides the capability for the fsck program to 2118 * update an active filesystem. Six operations are provided: 2119 * 2120 * adjrefcnt(inode, amt) - adjusts the reference count on the 2121 * specified inode by the specified amount. Under normal 2122 * operation the count should always go down. Decrementing 2123 * the count to zero will cause the inode to be freed. 2124 * adjblkcnt(inode, amt) - adjust the number of blocks used to 2125 * by the specifed amount. 2126 * freedirs(inode, count) - directory inodes [inode..inode + count - 1] 2127 * are marked as free. Inodes should never have to be marked 2128 * as in use. 2129 * freefiles(inode, count) - file inodes [inode..inode + count - 1] 2130 * are marked as free. Inodes should never have to be marked 2131 * as in use. 2132 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1] 2133 * are marked as free. Blocks should never have to be marked 2134 * as in use. 2135 * setflags(flags, set/clear) - the fs_flags field has the specified 2136 * flags set (second parameter +1) or cleared (second parameter -1). 2137 */ 2138 2139 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS); 2140 2141 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT, 2142 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count"); 2143 2144 SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR, 2145 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count"); 2146 2147 SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR, 2148 sysctl_ffs_fsck, "Free Range of Directory Inodes"); 2149 2150 SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR, 2151 sysctl_ffs_fsck, "Free Range of File Inodes"); 2152 2153 SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR, 2154 sysctl_ffs_fsck, "Free Range of Blocks"); 2155 2156 SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR, 2157 sysctl_ffs_fsck, "Change Filesystem Flags"); 2158 2159 #ifdef DEBUG 2160 static int fsckcmds = 0; 2161 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, ""); 2162 #endif /* DEBUG */ 2163 2164 static int 2165 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS) 2166 { 2167 struct fsck_cmd cmd; 2168 struct ufsmount *ump; 2169 struct vnode *vp; 2170 struct inode *ip; 2171 struct mount *mp; 2172 struct fs *fs; 2173 ufs2_daddr_t blkno; 2174 long blkcnt, blksize; 2175 struct file *fp; 2176 int filetype, error; 2177 2178 if (req->newlen > sizeof cmd) 2179 return (EBADRPC); 2180 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0) 2181 return (error); 2182 if (cmd.version != FFS_CMD_VERSION) 2183 return (ERPCMISMATCH); 2184 if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0) 2185 return (error); 2186 vn_start_write((struct vnode *)fp->f_data, &mp, V_WAIT); 2187 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) { 2188 vn_finished_write(mp); 2189 fdrop(fp, curthread); 2190 return (EINVAL); 2191 } 2192 if (mp->mnt_flag & MNT_RDONLY) { 2193 vn_finished_write(mp); 2194 fdrop(fp, curthread); 2195 return (EROFS); 2196 } 2197 ump = VFSTOUFS(mp); 2198 fs = ump->um_fs; 2199 filetype = IFREG; 2200 2201 switch (oidp->oid_number) { 2202 2203 case FFS_SET_FLAGS: 2204 #ifdef DEBUG 2205 if (fsckcmds) 2206 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname, 2207 cmd.size > 0 ? "set" : "clear"); 2208 #endif /* DEBUG */ 2209 if (cmd.size > 0) 2210 fs->fs_flags |= (long)cmd.value; 2211 else 2212 fs->fs_flags &= ~(long)cmd.value; 2213 break; 2214 2215 case FFS_ADJ_REFCNT: 2216 #ifdef DEBUG 2217 if (fsckcmds) { 2218 printf("%s: adjust inode %jd count by %jd\n", 2219 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value, 2220 (intmax_t)cmd.size); 2221 } 2222 #endif /* DEBUG */ 2223 if ((error = VFS_VGET(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp))) 2224 break; 2225 ip = VTOI(vp); 2226 ip->i_nlink += cmd.size; 2227 DIP(ip, i_nlink) = ip->i_nlink; 2228 ip->i_effnlink += cmd.size; 2229 ip->i_flag |= IN_CHANGE; 2230 if (DOINGSOFTDEP(vp)) 2231 softdep_change_linkcnt(ip); 2232 vput(vp); 2233 break; 2234 2235 case FFS_ADJ_BLKCNT: 2236 #ifdef DEBUG 2237 if (fsckcmds) { 2238 printf("%s: adjust inode %jd block count by %jd\n", 2239 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value, 2240 (intmax_t)cmd.size); 2241 } 2242 #endif /* DEBUG */ 2243 if ((error = VFS_VGET(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp))) 2244 break; 2245 ip = VTOI(vp); 2246 DIP(ip, i_blocks) += cmd.size; 2247 ip->i_flag |= IN_CHANGE; 2248 vput(vp); 2249 break; 2250 2251 case FFS_DIR_FREE: 2252 filetype = IFDIR; 2253 /* fall through */ 2254 2255 case FFS_FILE_FREE: 2256 #ifdef DEBUG 2257 if (fsckcmds) { 2258 if (cmd.size == 1) 2259 printf("%s: free %s inode %d\n", 2260 mp->mnt_stat.f_mntonname, 2261 filetype == IFDIR ? "directory" : "file", 2262 (ino_t)cmd.value); 2263 else 2264 printf("%s: free %s inodes %d-%d\n", 2265 mp->mnt_stat.f_mntonname, 2266 filetype == IFDIR ? "directory" : "file", 2267 (ino_t)cmd.value, 2268 (ino_t)(cmd.value + cmd.size - 1)); 2269 } 2270 #endif /* DEBUG */ 2271 while (cmd.size > 0) { 2272 if ((error = ffs_freefile(fs, ump->um_devvp, cmd.value, 2273 filetype))) 2274 break; 2275 cmd.size -= 1; 2276 cmd.value += 1; 2277 } 2278 break; 2279 2280 case FFS_BLK_FREE: 2281 #ifdef DEBUG 2282 if (fsckcmds) { 2283 if (cmd.size == 1) 2284 printf("%s: free block %lld\n", 2285 mp->mnt_stat.f_mntonname, 2286 (intmax_t)cmd.value); 2287 else 2288 printf("%s: free blocks %lld-%lld\n", 2289 mp->mnt_stat.f_mntonname, 2290 (intmax_t)cmd.value, 2291 (intmax_t)cmd.value + cmd.size - 1); 2292 } 2293 #endif /* DEBUG */ 2294 blkno = cmd.value; 2295 blkcnt = cmd.size; 2296 blksize = fs->fs_frag - (blkno % fs->fs_frag); 2297 while (blkcnt > 0) { 2298 if (blksize > blkcnt) 2299 blksize = blkcnt; 2300 ffs_blkfree(fs, ump->um_devvp, blkno, 2301 blksize * fs->fs_fsize, ROOTINO); 2302 blkno += blksize; 2303 blkcnt -= blksize; 2304 blksize = fs->fs_frag; 2305 } 2306 break; 2307 2308 default: 2309 #ifdef DEBUG 2310 if (fsckcmds) { 2311 printf("Invalid request %d from fsck\n", 2312 oidp->oid_number); 2313 } 2314 #endif /* DEBUG */ 2315 error = EINVAL; 2316 break; 2317 2318 } 2319 fdrop(fp, curthread); 2320 vn_finished_write(mp); 2321 return (error); 2322 } 2323