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 if (ITOV(pip)->v_flag & VROOT) { 932 prefcg = arc4random() % fs->fs_ncg; 933 mincg = prefcg; 934 minndir = fs->fs_ipg; 935 for (cg = prefcg; cg < fs->fs_ncg; cg++) 936 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 937 fs->fs_cs(fs, cg).cs_nifree >= avgifree && 938 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 939 mincg = cg; 940 minndir = fs->fs_cs(fs, cg).cs_ndir; 941 } 942 for (cg = 0; cg < prefcg; cg++) 943 if (fs->fs_cs(fs, cg).cs_ndir < minndir && 944 fs->fs_cs(fs, cg).cs_nifree >= avgifree && 945 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 946 mincg = cg; 947 minndir = fs->fs_cs(fs, cg).cs_ndir; 948 } 949 return ((ino_t)(fs->fs_ipg * mincg)); 950 } 951 952 /* 953 * Count various limits which used for 954 * optimal allocation of a directory inode. 955 */ 956 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg); 957 minifree = avgifree - fs->fs_ipg / 4; 958 if (minifree < 0) 959 minifree = 0; 960 minbfree = avgbfree - fs->fs_fpg / fs->fs_frag / 4; 961 if (minbfree < 0) 962 minbfree = 0; 963 cgsize = fs->fs_fsize * fs->fs_fpg; 964 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir; 965 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0; 966 if (dirsize < curdirsize) 967 dirsize = curdirsize; 968 maxcontigdirs = min(cgsize / dirsize, 255); 969 if (fs->fs_avgfpdir > 0) 970 maxcontigdirs = min(maxcontigdirs, 971 fs->fs_ipg / fs->fs_avgfpdir); 972 if (maxcontigdirs == 0) 973 maxcontigdirs = 1; 974 975 /* 976 * Limit number of dirs in one cg and reserve space for 977 * regular files, but only if we have no deficit in 978 * inodes or space. 979 */ 980 prefcg = ino_to_cg(fs, pip->i_number); 981 for (cg = prefcg; cg < fs->fs_ncg; cg++) 982 if (fs->fs_cs(fs, cg).cs_ndir < maxndir && 983 fs->fs_cs(fs, cg).cs_nifree >= minifree && 984 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { 985 if (fs->fs_contigdirs[cg] < maxcontigdirs) 986 return ((ino_t)(fs->fs_ipg * cg)); 987 } 988 for (cg = 0; cg < prefcg; cg++) 989 if (fs->fs_cs(fs, cg).cs_ndir < maxndir && 990 fs->fs_cs(fs, cg).cs_nifree >= minifree && 991 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) { 992 if (fs->fs_contigdirs[cg] < maxcontigdirs) 993 return ((ino_t)(fs->fs_ipg * cg)); 994 } 995 /* 996 * This is a backstop when we have deficit in space. 997 */ 998 for (cg = prefcg; cg < fs->fs_ncg; cg++) 999 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) 1000 return ((ino_t)(fs->fs_ipg * cg)); 1001 for (cg = 0; cg < prefcg; cg++) 1002 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree) 1003 break; 1004 return ((ino_t)(fs->fs_ipg * cg)); 1005 } 1006 1007 /* 1008 * Select the desired position for the next block in a file. The file is 1009 * logically divided into sections. The first section is composed of the 1010 * direct blocks. Each additional section contains fs_maxbpg blocks. 1011 * 1012 * If no blocks have been allocated in the first section, the policy is to 1013 * request a block in the same cylinder group as the inode that describes 1014 * the file. If no blocks have been allocated in any other section, the 1015 * policy is to place the section in a cylinder group with a greater than 1016 * average number of free blocks. An appropriate cylinder group is found 1017 * by using a rotor that sweeps the cylinder groups. When a new group of 1018 * blocks is needed, the sweep begins in the cylinder group following the 1019 * cylinder group from which the previous allocation was made. The sweep 1020 * continues until a cylinder group with greater than the average number 1021 * of free blocks is found. If the allocation is for the first block in an 1022 * indirect block, the information on the previous allocation is unavailable; 1023 * here a best guess is made based upon the logical block number being 1024 * allocated. 1025 * 1026 * If a section is already partially allocated, the policy is to 1027 * contiguously allocate fs_maxcontig blocks. The end of one of these 1028 * contiguous blocks and the beginning of the next is laid out 1029 * contiguously if possible. 1030 */ 1031 ufs2_daddr_t 1032 ffs_blkpref_ufs1(ip, lbn, indx, bap) 1033 struct inode *ip; 1034 ufs_lbn_t lbn; 1035 int indx; 1036 ufs1_daddr_t *bap; 1037 { 1038 struct fs *fs; 1039 int cg; 1040 int avgbfree, startcg; 1041 1042 fs = ip->i_fs; 1043 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 1044 if (lbn < NDADDR + NINDIR(fs)) { 1045 cg = ino_to_cg(fs, ip->i_number); 1046 return (fs->fs_fpg * cg + fs->fs_frag); 1047 } 1048 /* 1049 * Find a cylinder with greater than average number of 1050 * unused data blocks. 1051 */ 1052 if (indx == 0 || bap[indx - 1] == 0) 1053 startcg = 1054 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 1055 else 1056 startcg = dtog(fs, bap[indx - 1]) + 1; 1057 startcg %= fs->fs_ncg; 1058 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 1059 for (cg = startcg; cg < fs->fs_ncg; cg++) 1060 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1061 fs->fs_cgrotor = cg; 1062 return (fs->fs_fpg * cg + fs->fs_frag); 1063 } 1064 for (cg = 0; cg <= startcg; cg++) 1065 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1066 fs->fs_cgrotor = cg; 1067 return (fs->fs_fpg * cg + fs->fs_frag); 1068 } 1069 return (0); 1070 } 1071 /* 1072 * We just always try to lay things out contiguously. 1073 */ 1074 return (bap[indx - 1] + fs->fs_frag); 1075 } 1076 1077 /* 1078 * Same as above, but for UFS2 1079 */ 1080 ufs2_daddr_t 1081 ffs_blkpref_ufs2(ip, lbn, indx, bap) 1082 struct inode *ip; 1083 ufs_lbn_t lbn; 1084 int indx; 1085 ufs2_daddr_t *bap; 1086 { 1087 struct fs *fs; 1088 int cg; 1089 int avgbfree, startcg; 1090 1091 fs = ip->i_fs; 1092 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { 1093 if (lbn < NDADDR + NINDIR(fs)) { 1094 cg = ino_to_cg(fs, ip->i_number); 1095 return (fs->fs_fpg * cg + fs->fs_frag); 1096 } 1097 /* 1098 * Find a cylinder with greater than average number of 1099 * unused data blocks. 1100 */ 1101 if (indx == 0 || bap[indx - 1] == 0) 1102 startcg = 1103 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg; 1104 else 1105 startcg = dtog(fs, bap[indx - 1]) + 1; 1106 startcg %= fs->fs_ncg; 1107 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; 1108 for (cg = startcg; cg < fs->fs_ncg; cg++) 1109 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1110 fs->fs_cgrotor = cg; 1111 return (fs->fs_fpg * cg + fs->fs_frag); 1112 } 1113 for (cg = 0; cg <= startcg; cg++) 1114 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { 1115 fs->fs_cgrotor = cg; 1116 return (fs->fs_fpg * cg + fs->fs_frag); 1117 } 1118 return (0); 1119 } 1120 /* 1121 * We just always try to lay things out contiguously. 1122 */ 1123 return (bap[indx - 1] + fs->fs_frag); 1124 } 1125 1126 /* 1127 * Implement the cylinder overflow algorithm. 1128 * 1129 * The policy implemented by this algorithm is: 1130 * 1) allocate the block in its requested cylinder group. 1131 * 2) quadradically rehash on the cylinder group number. 1132 * 3) brute force search for a free block. 1133 */ 1134 /*VARARGS5*/ 1135 static ufs2_daddr_t 1136 ffs_hashalloc(ip, cg, pref, size, allocator) 1137 struct inode *ip; 1138 int cg; 1139 ufs2_daddr_t pref; 1140 int size; /* size for data blocks, mode for inodes */ 1141 allocfcn_t *allocator; 1142 { 1143 struct fs *fs; 1144 ufs2_daddr_t result; 1145 int i, icg = cg; 1146 1147 #ifdef DIAGNOSTIC 1148 if (ITOV(ip)->v_mount->mnt_kern_flag & MNTK_SUSPENDED) 1149 panic("ffs_hashalloc: allocation on suspended filesystem"); 1150 #endif 1151 fs = ip->i_fs; 1152 /* 1153 * 1: preferred cylinder group 1154 */ 1155 result = (*allocator)(ip, cg, pref, size); 1156 if (result) 1157 return (result); 1158 /* 1159 * 2: quadratic rehash 1160 */ 1161 for (i = 1; i < fs->fs_ncg; i *= 2) { 1162 cg += i; 1163 if (cg >= fs->fs_ncg) 1164 cg -= fs->fs_ncg; 1165 result = (*allocator)(ip, cg, 0, size); 1166 if (result) 1167 return (result); 1168 } 1169 /* 1170 * 3: brute force search 1171 * Note that we start at i == 2, since 0 was checked initially, 1172 * and 1 is always checked in the quadratic rehash. 1173 */ 1174 cg = (icg + 2) % fs->fs_ncg; 1175 for (i = 2; i < fs->fs_ncg; i++) { 1176 result = (*allocator)(ip, cg, 0, size); 1177 if (result) 1178 return (result); 1179 cg++; 1180 if (cg == fs->fs_ncg) 1181 cg = 0; 1182 } 1183 return (0); 1184 } 1185 1186 /* 1187 * Determine whether a fragment can be extended. 1188 * 1189 * Check to see if the necessary fragments are available, and 1190 * if they are, allocate them. 1191 */ 1192 static ufs2_daddr_t 1193 ffs_fragextend(ip, cg, bprev, osize, nsize) 1194 struct inode *ip; 1195 int cg; 1196 ufs2_daddr_t bprev; 1197 int osize, nsize; 1198 { 1199 struct fs *fs; 1200 struct cg *cgp; 1201 struct buf *bp; 1202 long bno; 1203 int frags, bbase; 1204 int i, error; 1205 u_int8_t *blksfree; 1206 1207 fs = ip->i_fs; 1208 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) 1209 return (0); 1210 frags = numfrags(fs, nsize); 1211 bbase = fragnum(fs, bprev); 1212 if (bbase > fragnum(fs, (bprev + frags - 1))) { 1213 /* cannot extend across a block boundary */ 1214 return (0); 1215 } 1216 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1217 (int)fs->fs_cgsize, NOCRED, &bp); 1218 if (error) { 1219 brelse(bp); 1220 return (0); 1221 } 1222 cgp = (struct cg *)bp->b_data; 1223 if (!cg_chkmagic(cgp)) { 1224 brelse(bp); 1225 return (0); 1226 } 1227 bp->b_xflags |= BX_BKGRDWRITE; 1228 cgp->cg_old_time = cgp->cg_time = time_second; 1229 bno = dtogd(fs, bprev); 1230 blksfree = cg_blksfree(cgp); 1231 for (i = numfrags(fs, osize); i < frags; i++) 1232 if (isclr(blksfree, bno + i)) { 1233 brelse(bp); 1234 return (0); 1235 } 1236 /* 1237 * the current fragment can be extended 1238 * deduct the count on fragment being extended into 1239 * increase the count on the remaining fragment (if any) 1240 * allocate the extended piece 1241 */ 1242 for (i = frags; i < fs->fs_frag - bbase; i++) 1243 if (isclr(blksfree, bno + i)) 1244 break; 1245 cgp->cg_frsum[i - numfrags(fs, osize)]--; 1246 if (i != frags) 1247 cgp->cg_frsum[i - frags]++; 1248 for (i = numfrags(fs, osize); i < frags; i++) { 1249 clrbit(blksfree, bno + i); 1250 cgp->cg_cs.cs_nffree--; 1251 fs->fs_cstotal.cs_nffree--; 1252 fs->fs_cs(fs, cg).cs_nffree--; 1253 } 1254 fs->fs_fmod = 1; 1255 if (DOINGSOFTDEP(ITOV(ip))) 1256 softdep_setup_blkmapdep(bp, fs, bprev); 1257 if (fs->fs_active != 0) 1258 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1259 bdwrite(bp); 1260 return (bprev); 1261 } 1262 1263 /* 1264 * Determine whether a block can be allocated. 1265 * 1266 * Check to see if a block of the appropriate size is available, 1267 * and if it is, allocate it. 1268 */ 1269 static ufs2_daddr_t 1270 ffs_alloccg(ip, cg, bpref, size) 1271 struct inode *ip; 1272 int cg; 1273 ufs2_daddr_t bpref; 1274 int size; 1275 { 1276 struct fs *fs; 1277 struct cg *cgp; 1278 struct buf *bp; 1279 ufs1_daddr_t bno; 1280 ufs2_daddr_t blkno; 1281 int i, allocsiz, error, frags; 1282 u_int8_t *blksfree; 1283 1284 fs = ip->i_fs; 1285 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) 1286 return (0); 1287 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1288 (int)fs->fs_cgsize, NOCRED, &bp); 1289 if (error) { 1290 brelse(bp); 1291 return (0); 1292 } 1293 cgp = (struct cg *)bp->b_data; 1294 if (!cg_chkmagic(cgp) || 1295 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { 1296 brelse(bp); 1297 return (0); 1298 } 1299 bp->b_xflags |= BX_BKGRDWRITE; 1300 cgp->cg_old_time = cgp->cg_time = time_second; 1301 if (size == fs->fs_bsize) { 1302 blkno = ffs_alloccgblk(ip, bp, bpref); 1303 if (fs->fs_active != 0) 1304 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1305 bdwrite(bp); 1306 return (blkno); 1307 } 1308 /* 1309 * check to see if any fragments are already available 1310 * allocsiz is the size which will be allocated, hacking 1311 * it down to a smaller size if necessary 1312 */ 1313 blksfree = cg_blksfree(cgp); 1314 frags = numfrags(fs, size); 1315 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) 1316 if (cgp->cg_frsum[allocsiz] != 0) 1317 break; 1318 if (allocsiz == fs->fs_frag) { 1319 /* 1320 * no fragments were available, so a block will be 1321 * allocated, and hacked up 1322 */ 1323 if (cgp->cg_cs.cs_nbfree == 0) { 1324 brelse(bp); 1325 return (0); 1326 } 1327 blkno = ffs_alloccgblk(ip, bp, bpref); 1328 bno = dtogd(fs, blkno); 1329 for (i = frags; i < fs->fs_frag; i++) 1330 setbit(blksfree, bno + i); 1331 i = fs->fs_frag - frags; 1332 cgp->cg_cs.cs_nffree += i; 1333 fs->fs_cstotal.cs_nffree += i; 1334 fs->fs_cs(fs, cg).cs_nffree += i; 1335 fs->fs_fmod = 1; 1336 cgp->cg_frsum[i]++; 1337 if (fs->fs_active != 0) 1338 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1339 bdwrite(bp); 1340 return (blkno); 1341 } 1342 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz); 1343 if (bno < 0) { 1344 brelse(bp); 1345 return (0); 1346 } 1347 for (i = 0; i < frags; i++) 1348 clrbit(blksfree, bno + i); 1349 cgp->cg_cs.cs_nffree -= frags; 1350 fs->fs_cstotal.cs_nffree -= frags; 1351 fs->fs_cs(fs, cg).cs_nffree -= frags; 1352 fs->fs_fmod = 1; 1353 cgp->cg_frsum[allocsiz]--; 1354 if (frags != allocsiz) 1355 cgp->cg_frsum[allocsiz - frags]++; 1356 blkno = cg * fs->fs_fpg + bno; 1357 if (DOINGSOFTDEP(ITOV(ip))) 1358 softdep_setup_blkmapdep(bp, fs, blkno); 1359 if (fs->fs_active != 0) 1360 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1361 bdwrite(bp); 1362 return (blkno); 1363 } 1364 1365 /* 1366 * Allocate a block in a cylinder group. 1367 * 1368 * This algorithm implements the following policy: 1369 * 1) allocate the requested block. 1370 * 2) allocate a rotationally optimal block in the same cylinder. 1371 * 3) allocate the next available block on the block rotor for the 1372 * specified cylinder group. 1373 * Note that this routine only allocates fs_bsize blocks; these 1374 * blocks may be fragmented by the routine that allocates them. 1375 */ 1376 static ufs2_daddr_t 1377 ffs_alloccgblk(ip, bp, bpref) 1378 struct inode *ip; 1379 struct buf *bp; 1380 ufs2_daddr_t bpref; 1381 { 1382 struct fs *fs; 1383 struct cg *cgp; 1384 ufs1_daddr_t bno; 1385 ufs2_daddr_t blkno; 1386 u_int8_t *blksfree; 1387 1388 fs = ip->i_fs; 1389 cgp = (struct cg *)bp->b_data; 1390 blksfree = cg_blksfree(cgp); 1391 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) { 1392 bpref = cgp->cg_rotor; 1393 } else { 1394 bpref = blknum(fs, bpref); 1395 bno = dtogd(fs, bpref); 1396 /* 1397 * if the requested block is available, use it 1398 */ 1399 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno))) 1400 goto gotit; 1401 } 1402 /* 1403 * Take the next available block in this cylinder group. 1404 */ 1405 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag); 1406 if (bno < 0) 1407 return (0); 1408 cgp->cg_rotor = bno; 1409 gotit: 1410 blkno = fragstoblks(fs, bno); 1411 ffs_clrblock(fs, blksfree, (long)blkno); 1412 ffs_clusteracct(fs, cgp, blkno, -1); 1413 cgp->cg_cs.cs_nbfree--; 1414 fs->fs_cstotal.cs_nbfree--; 1415 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 1416 fs->fs_fmod = 1; 1417 blkno = cgp->cg_cgx * fs->fs_fpg + bno; 1418 if (DOINGSOFTDEP(ITOV(ip))) 1419 softdep_setup_blkmapdep(bp, fs, blkno); 1420 return (blkno); 1421 } 1422 1423 /* 1424 * Determine whether a cluster can be allocated. 1425 * 1426 * We do not currently check for optimal rotational layout if there 1427 * are multiple choices in the same cylinder group. Instead we just 1428 * take the first one that we find following bpref. 1429 */ 1430 static ufs2_daddr_t 1431 ffs_clusteralloc(ip, cg, bpref, len) 1432 struct inode *ip; 1433 int cg; 1434 ufs2_daddr_t bpref; 1435 int len; 1436 { 1437 struct fs *fs; 1438 struct cg *cgp; 1439 struct buf *bp; 1440 int i, run, bit, map, got; 1441 ufs2_daddr_t bno; 1442 u_char *mapp; 1443 int32_t *lp; 1444 u_int8_t *blksfree; 1445 1446 fs = ip->i_fs; 1447 if (fs->fs_maxcluster[cg] < len) 1448 return (0); 1449 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, 1450 NOCRED, &bp)) 1451 goto fail; 1452 cgp = (struct cg *)bp->b_data; 1453 if (!cg_chkmagic(cgp)) 1454 goto fail; 1455 bp->b_xflags |= BX_BKGRDWRITE; 1456 /* 1457 * Check to see if a cluster of the needed size (or bigger) is 1458 * available in this cylinder group. 1459 */ 1460 lp = &cg_clustersum(cgp)[len]; 1461 for (i = len; i <= fs->fs_contigsumsize; i++) 1462 if (*lp++ > 0) 1463 break; 1464 if (i > fs->fs_contigsumsize) { 1465 /* 1466 * This is the first time looking for a cluster in this 1467 * cylinder group. Update the cluster summary information 1468 * to reflect the true maximum sized cluster so that 1469 * future cluster allocation requests can avoid reading 1470 * the cylinder group map only to find no clusters. 1471 */ 1472 lp = &cg_clustersum(cgp)[len - 1]; 1473 for (i = len - 1; i > 0; i--) 1474 if (*lp-- > 0) 1475 break; 1476 fs->fs_maxcluster[cg] = i; 1477 goto fail; 1478 } 1479 /* 1480 * Search the cluster map to find a big enough cluster. 1481 * We take the first one that we find, even if it is larger 1482 * than we need as we prefer to get one close to the previous 1483 * block allocation. We do not search before the current 1484 * preference point as we do not want to allocate a block 1485 * that is allocated before the previous one (as we will 1486 * then have to wait for another pass of the elevator 1487 * algorithm before it will be read). We prefer to fail and 1488 * be recalled to try an allocation in the next cylinder group. 1489 */ 1490 if (dtog(fs, bpref) != cg) 1491 bpref = 0; 1492 else 1493 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref))); 1494 mapp = &cg_clustersfree(cgp)[bpref / NBBY]; 1495 map = *mapp++; 1496 bit = 1 << (bpref % NBBY); 1497 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) { 1498 if ((map & bit) == 0) { 1499 run = 0; 1500 } else { 1501 run++; 1502 if (run == len) 1503 break; 1504 } 1505 if ((got & (NBBY - 1)) != (NBBY - 1)) { 1506 bit <<= 1; 1507 } else { 1508 map = *mapp++; 1509 bit = 1; 1510 } 1511 } 1512 if (got >= cgp->cg_nclusterblks) 1513 goto fail; 1514 /* 1515 * Allocate the cluster that we have found. 1516 */ 1517 blksfree = cg_blksfree(cgp); 1518 for (i = 1; i <= len; i++) 1519 if (!ffs_isblock(fs, blksfree, got - run + i)) 1520 panic("ffs_clusteralloc: map mismatch"); 1521 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1); 1522 if (dtog(fs, bno) != cg) 1523 panic("ffs_clusteralloc: allocated out of group"); 1524 len = blkstofrags(fs, len); 1525 for (i = 0; i < len; i += fs->fs_frag) 1526 if (ffs_alloccgblk(ip, bp, bno + i) != bno + i) 1527 panic("ffs_clusteralloc: lost block"); 1528 if (fs->fs_active != 0) 1529 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1530 bdwrite(bp); 1531 return (bno); 1532 1533 fail: 1534 brelse(bp); 1535 return (0); 1536 } 1537 1538 /* 1539 * Determine whether an inode can be allocated. 1540 * 1541 * Check to see if an inode is available, and if it is, 1542 * allocate it using the following policy: 1543 * 1) allocate the requested inode. 1544 * 2) allocate the next available inode after the requested 1545 * inode in the specified cylinder group. 1546 */ 1547 static ufs2_daddr_t 1548 ffs_nodealloccg(ip, cg, ipref, mode) 1549 struct inode *ip; 1550 int cg; 1551 ufs2_daddr_t ipref; 1552 int mode; 1553 { 1554 struct fs *fs; 1555 struct cg *cgp; 1556 struct buf *bp, *ibp; 1557 u_int8_t *inosused; 1558 struct ufs2_dinode *dp2; 1559 int error, start, len, loc, map, i; 1560 1561 fs = ip->i_fs; 1562 if (fs->fs_cs(fs, cg).cs_nifree == 0) 1563 return (0); 1564 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), 1565 (int)fs->fs_cgsize, NOCRED, &bp); 1566 if (error) { 1567 brelse(bp); 1568 return (0); 1569 } 1570 cgp = (struct cg *)bp->b_data; 1571 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { 1572 brelse(bp); 1573 return (0); 1574 } 1575 bp->b_xflags |= BX_BKGRDWRITE; 1576 cgp->cg_old_time = cgp->cg_time = time_second; 1577 inosused = cg_inosused(cgp); 1578 if (ipref) { 1579 ipref %= fs->fs_ipg; 1580 if (isclr(inosused, ipref)) 1581 goto gotit; 1582 } 1583 start = cgp->cg_irotor / NBBY; 1584 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); 1585 loc = skpc(0xff, len, &inosused[start]); 1586 if (loc == 0) { 1587 len = start + 1; 1588 start = 0; 1589 loc = skpc(0xff, len, &inosused[0]); 1590 if (loc == 0) { 1591 printf("cg = %d, irotor = %ld, fs = %s\n", 1592 cg, (long)cgp->cg_irotor, fs->fs_fsmnt); 1593 panic("ffs_nodealloccg: map corrupted"); 1594 /* NOTREACHED */ 1595 } 1596 } 1597 i = start + len - loc; 1598 map = inosused[i]; 1599 ipref = i * NBBY; 1600 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { 1601 if ((map & i) == 0) { 1602 cgp->cg_irotor = ipref; 1603 goto gotit; 1604 } 1605 } 1606 printf("fs = %s\n", fs->fs_fsmnt); 1607 panic("ffs_nodealloccg: block not in map"); 1608 /* NOTREACHED */ 1609 gotit: 1610 if (DOINGSOFTDEP(ITOV(ip))) 1611 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref); 1612 setbit(inosused, ipref); 1613 cgp->cg_cs.cs_nifree--; 1614 fs->fs_cstotal.cs_nifree--; 1615 fs->fs_cs(fs, cg).cs_nifree--; 1616 fs->fs_fmod = 1; 1617 if ((mode & IFMT) == IFDIR) { 1618 cgp->cg_cs.cs_ndir++; 1619 fs->fs_cstotal.cs_ndir++; 1620 fs->fs_cs(fs, cg).cs_ndir++; 1621 } 1622 /* 1623 * Check to see if we need to initialize more inodes. 1624 */ 1625 if (fs->fs_magic == FS_UFS2_MAGIC && 1626 ipref + INOPB(fs) > cgp->cg_initediblk && 1627 cgp->cg_initediblk < cgp->cg_niblk) { 1628 ibp = getblk(ip->i_devvp, fsbtodb(fs, 1629 ino_to_fsba(fs, cg * fs->fs_ipg + cgp->cg_initediblk)), 1630 (int)fs->fs_bsize, 0, 0); 1631 bzero(ibp->b_data, (int)fs->fs_bsize); 1632 dp2 = (struct ufs2_dinode *)(ibp->b_data); 1633 for (i = 0; i < INOPB(fs); i++) { 1634 dp2->di_gen = random() / 2 + 1; 1635 dp2++; 1636 } 1637 bawrite(ibp); 1638 cgp->cg_initediblk += INOPB(fs); 1639 } 1640 if (fs->fs_active != 0) 1641 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1642 bdwrite(bp); 1643 return (cg * fs->fs_ipg + ipref); 1644 } 1645 1646 /* 1647 * Free a block or fragment. 1648 * 1649 * The specified block or fragment is placed back in the 1650 * free map. If a fragment is deallocated, a possible 1651 * block reassembly is checked. 1652 */ 1653 void 1654 ffs_blkfree(fs, devvp, bno, size, inum) 1655 struct fs *fs; 1656 struct vnode *devvp; 1657 ufs2_daddr_t bno; 1658 long size; 1659 ino_t inum; 1660 { 1661 struct cg *cgp; 1662 struct buf *bp; 1663 ufs1_daddr_t fragno, cgbno; 1664 ufs2_daddr_t cgblkno; 1665 int i, error, cg, blk, frags, bbase; 1666 u_int8_t *blksfree; 1667 dev_t dev; 1668 1669 cg = dtog(fs, bno); 1670 if (devvp->v_type != VCHR) { 1671 /* devvp is a snapshot */ 1672 dev = VTOI(devvp)->i_devvp->v_rdev; 1673 cgblkno = fragstoblks(fs, cgtod(fs, cg)); 1674 } else { 1675 /* devvp is a normal disk device */ 1676 dev = devvp->v_rdev; 1677 cgblkno = fsbtodb(fs, cgtod(fs, cg)); 1678 if ((devvp->v_flag & VCOPYONWRITE) && 1679 ffs_snapblkfree(fs, devvp, bno, size, inum)) 1680 return; 1681 VOP_FREEBLKS(devvp, fsbtodb(fs, bno), size); 1682 } 1683 #ifdef DIAGNOSTIC 1684 if (dev->si_mountpoint && 1685 (dev->si_mountpoint->mnt_kern_flag & MNTK_SUSPENDED)) 1686 panic("ffs_blkfree: deallocation on suspended filesystem"); 1687 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 || 1688 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) { 1689 printf("dev=%s, bno = %lld, bsize = %ld, size = %ld, fs = %s\n", 1690 devtoname(dev), (intmax_t)bno, (long)fs->fs_bsize, 1691 size, fs->fs_fsmnt); 1692 panic("ffs_blkfree: bad size"); 1693 } 1694 #endif 1695 if ((u_int)bno >= fs->fs_size) { 1696 printf("bad block %jd, ino %lu\n", (intmax_t)bno, 1697 (u_long)inum); 1698 ffs_fserr(fs, inum, "bad block"); 1699 return; 1700 } 1701 if ((error = bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp))) { 1702 brelse(bp); 1703 return; 1704 } 1705 cgp = (struct cg *)bp->b_data; 1706 if (!cg_chkmagic(cgp)) { 1707 brelse(bp); 1708 return; 1709 } 1710 bp->b_xflags |= BX_BKGRDWRITE; 1711 cgp->cg_old_time = cgp->cg_time = time_second; 1712 cgbno = dtogd(fs, bno); 1713 blksfree = cg_blksfree(cgp); 1714 if (size == fs->fs_bsize) { 1715 fragno = fragstoblks(fs, cgbno); 1716 if (!ffs_isfreeblock(fs, blksfree, fragno)) { 1717 if (devvp->v_type != VCHR) { 1718 /* devvp is a snapshot */ 1719 brelse(bp); 1720 return; 1721 } 1722 printf("dev = %s, block = %jd, fs = %s\n", 1723 devtoname(dev), (intmax_t)bno, fs->fs_fsmnt); 1724 panic("ffs_blkfree: freeing free block"); 1725 } 1726 ffs_setblock(fs, blksfree, fragno); 1727 ffs_clusteracct(fs, cgp, fragno, 1); 1728 cgp->cg_cs.cs_nbfree++; 1729 fs->fs_cstotal.cs_nbfree++; 1730 fs->fs_cs(fs, cg).cs_nbfree++; 1731 } else { 1732 bbase = cgbno - fragnum(fs, cgbno); 1733 /* 1734 * decrement the counts associated with the old frags 1735 */ 1736 blk = blkmap(fs, blksfree, bbase); 1737 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 1738 /* 1739 * deallocate the fragment 1740 */ 1741 frags = numfrags(fs, size); 1742 for (i = 0; i < frags; i++) { 1743 if (isset(blksfree, cgbno + i)) { 1744 printf("dev = %s, block = %jd, fs = %s\n", 1745 devtoname(dev), (intmax_t)(bno + i), 1746 fs->fs_fsmnt); 1747 panic("ffs_blkfree: freeing free frag"); 1748 } 1749 setbit(blksfree, cgbno + i); 1750 } 1751 cgp->cg_cs.cs_nffree += i; 1752 fs->fs_cstotal.cs_nffree += i; 1753 fs->fs_cs(fs, cg).cs_nffree += i; 1754 /* 1755 * add back in counts associated with the new frags 1756 */ 1757 blk = blkmap(fs, blksfree, bbase); 1758 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 1759 /* 1760 * if a complete block has been reassembled, account for it 1761 */ 1762 fragno = fragstoblks(fs, bbase); 1763 if (ffs_isblock(fs, blksfree, fragno)) { 1764 cgp->cg_cs.cs_nffree -= fs->fs_frag; 1765 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 1766 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 1767 ffs_clusteracct(fs, cgp, fragno, 1); 1768 cgp->cg_cs.cs_nbfree++; 1769 fs->fs_cstotal.cs_nbfree++; 1770 fs->fs_cs(fs, cg).cs_nbfree++; 1771 } 1772 } 1773 fs->fs_fmod = 1; 1774 if (fs->fs_active != 0) 1775 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1776 bdwrite(bp); 1777 } 1778 1779 #ifdef DIAGNOSTIC 1780 /* 1781 * Verify allocation of a block or fragment. Returns true if block or 1782 * fragment is allocated, false if it is free. 1783 */ 1784 static int 1785 ffs_checkblk(ip, bno, size) 1786 struct inode *ip; 1787 ufs2_daddr_t bno; 1788 long size; 1789 { 1790 struct fs *fs; 1791 struct cg *cgp; 1792 struct buf *bp; 1793 ufs1_daddr_t cgbno; 1794 int i, error, frags, free; 1795 u_int8_t *blksfree; 1796 1797 fs = ip->i_fs; 1798 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) { 1799 printf("bsize = %ld, size = %ld, fs = %s\n", 1800 (long)fs->fs_bsize, size, fs->fs_fsmnt); 1801 panic("ffs_checkblk: bad size"); 1802 } 1803 if ((u_int)bno >= fs->fs_size) 1804 panic("ffs_checkblk: bad block %lld", (intmax_t)bno); 1805 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))), 1806 (int)fs->fs_cgsize, NOCRED, &bp); 1807 if (error) 1808 panic("ffs_checkblk: cg bread failed"); 1809 cgp = (struct cg *)bp->b_data; 1810 if (!cg_chkmagic(cgp)) 1811 panic("ffs_checkblk: cg magic mismatch"); 1812 bp->b_xflags |= BX_BKGRDWRITE; 1813 blksfree = cg_blksfree(cgp); 1814 cgbno = dtogd(fs, bno); 1815 if (size == fs->fs_bsize) { 1816 free = ffs_isblock(fs, blksfree, fragstoblks(fs, cgbno)); 1817 } else { 1818 frags = numfrags(fs, size); 1819 for (free = 0, i = 0; i < frags; i++) 1820 if (isset(blksfree, cgbno + i)) 1821 free++; 1822 if (free != 0 && free != frags) 1823 panic("ffs_checkblk: partially free fragment"); 1824 } 1825 brelse(bp); 1826 return (!free); 1827 } 1828 #endif /* DIAGNOSTIC */ 1829 1830 /* 1831 * Free an inode. 1832 */ 1833 int 1834 ffs_vfree(pvp, ino, mode) 1835 struct vnode *pvp; 1836 ino_t ino; 1837 int mode; 1838 { 1839 if (DOINGSOFTDEP(pvp)) { 1840 softdep_freefile(pvp, ino, mode); 1841 return (0); 1842 } 1843 return (ffs_freefile(VTOI(pvp)->i_fs, VTOI(pvp)->i_devvp, ino, mode)); 1844 } 1845 1846 /* 1847 * Do the actual free operation. 1848 * The specified inode is placed back in the free map. 1849 */ 1850 int 1851 ffs_freefile(fs, devvp, ino, mode) 1852 struct fs *fs; 1853 struct vnode *devvp; 1854 ino_t ino; 1855 int mode; 1856 { 1857 struct cg *cgp; 1858 struct buf *bp; 1859 ufs2_daddr_t cgbno; 1860 int error, cg; 1861 u_int8_t *inosused; 1862 dev_t dev; 1863 1864 cg = ino_to_cg(fs, ino); 1865 if (devvp->v_type != VCHR) { 1866 /* devvp is a snapshot */ 1867 dev = VTOI(devvp)->i_devvp->v_rdev; 1868 cgbno = fragstoblks(fs, cgtod(fs, cg)); 1869 } else { 1870 /* devvp is a normal disk device */ 1871 dev = devvp->v_rdev; 1872 cgbno = fsbtodb(fs, cgtod(fs, cg)); 1873 } 1874 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg) 1875 panic("ffs_vfree: range: dev = %s, ino = %d, fs = %s", 1876 devtoname(dev), ino, fs->fs_fsmnt); 1877 if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) { 1878 brelse(bp); 1879 return (error); 1880 } 1881 cgp = (struct cg *)bp->b_data; 1882 if (!cg_chkmagic(cgp)) { 1883 brelse(bp); 1884 return (0); 1885 } 1886 bp->b_xflags |= BX_BKGRDWRITE; 1887 cgp->cg_old_time = cgp->cg_time = time_second; 1888 inosused = cg_inosused(cgp); 1889 ino %= fs->fs_ipg; 1890 if (isclr(inosused, ino)) { 1891 printf("dev = %s, ino = %lu, fs = %s\n", devtoname(dev), 1892 (u_long)ino + cg * fs->fs_ipg, fs->fs_fsmnt); 1893 if (fs->fs_ronly == 0) 1894 panic("ffs_vfree: freeing free inode"); 1895 } 1896 clrbit(inosused, ino); 1897 if (ino < cgp->cg_irotor) 1898 cgp->cg_irotor = ino; 1899 cgp->cg_cs.cs_nifree++; 1900 fs->fs_cstotal.cs_nifree++; 1901 fs->fs_cs(fs, cg).cs_nifree++; 1902 if ((mode & IFMT) == IFDIR) { 1903 cgp->cg_cs.cs_ndir--; 1904 fs->fs_cstotal.cs_ndir--; 1905 fs->fs_cs(fs, cg).cs_ndir--; 1906 } 1907 fs->fs_fmod = 1; 1908 if (fs->fs_active != 0) 1909 atomic_clear_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg)); 1910 bdwrite(bp); 1911 return (0); 1912 } 1913 1914 /* 1915 * Find a block of the specified size in the specified cylinder group. 1916 * 1917 * It is a panic if a request is made to find a block if none are 1918 * available. 1919 */ 1920 static ufs1_daddr_t 1921 ffs_mapsearch(fs, cgp, bpref, allocsiz) 1922 struct fs *fs; 1923 struct cg *cgp; 1924 ufs2_daddr_t bpref; 1925 int allocsiz; 1926 { 1927 ufs1_daddr_t bno; 1928 int start, len, loc, i; 1929 int blk, field, subfield, pos; 1930 u_int8_t *blksfree; 1931 1932 /* 1933 * find the fragment by searching through the free block 1934 * map for an appropriate bit pattern 1935 */ 1936 if (bpref) 1937 start = dtogd(fs, bpref) / NBBY; 1938 else 1939 start = cgp->cg_frotor / NBBY; 1940 blksfree = cg_blksfree(cgp); 1941 len = howmany(fs->fs_fpg, NBBY) - start; 1942 loc = scanc((u_int)len, (u_char *)&blksfree[start], 1943 (u_char *)fragtbl[fs->fs_frag], 1944 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1945 if (loc == 0) { 1946 len = start + 1; 1947 start = 0; 1948 loc = scanc((u_int)len, (u_char *)&blksfree[0], 1949 (u_char *)fragtbl[fs->fs_frag], 1950 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); 1951 if (loc == 0) { 1952 printf("start = %d, len = %d, fs = %s\n", 1953 start, len, fs->fs_fsmnt); 1954 panic("ffs_alloccg: map corrupted"); 1955 /* NOTREACHED */ 1956 } 1957 } 1958 bno = (start + len - loc) * NBBY; 1959 cgp->cg_frotor = bno; 1960 /* 1961 * found the byte in the map 1962 * sift through the bits to find the selected frag 1963 */ 1964 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { 1965 blk = blkmap(fs, blksfree, bno); 1966 blk <<= 1; 1967 field = around[allocsiz]; 1968 subfield = inside[allocsiz]; 1969 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { 1970 if ((blk & field) == subfield) 1971 return (bno + pos); 1972 field <<= 1; 1973 subfield <<= 1; 1974 } 1975 } 1976 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt); 1977 panic("ffs_alloccg: block not in map"); 1978 return (-1); 1979 } 1980 1981 /* 1982 * Update the cluster map because of an allocation or free. 1983 * 1984 * Cnt == 1 means free; cnt == -1 means allocating. 1985 */ 1986 void 1987 ffs_clusteracct(fs, cgp, blkno, cnt) 1988 struct fs *fs; 1989 struct cg *cgp; 1990 ufs1_daddr_t blkno; 1991 int cnt; 1992 { 1993 int32_t *sump; 1994 int32_t *lp; 1995 u_char *freemapp, *mapp; 1996 int i, start, end, forw, back, map, bit; 1997 1998 if (fs->fs_contigsumsize <= 0) 1999 return; 2000 freemapp = cg_clustersfree(cgp); 2001 sump = cg_clustersum(cgp); 2002 /* 2003 * Allocate or clear the actual block. 2004 */ 2005 if (cnt > 0) 2006 setbit(freemapp, blkno); 2007 else 2008 clrbit(freemapp, blkno); 2009 /* 2010 * Find the size of the cluster going forward. 2011 */ 2012 start = blkno + 1; 2013 end = start + fs->fs_contigsumsize; 2014 if (end >= cgp->cg_nclusterblks) 2015 end = cgp->cg_nclusterblks; 2016 mapp = &freemapp[start / NBBY]; 2017 map = *mapp++; 2018 bit = 1 << (start % NBBY); 2019 for (i = start; i < end; i++) { 2020 if ((map & bit) == 0) 2021 break; 2022 if ((i & (NBBY - 1)) != (NBBY - 1)) { 2023 bit <<= 1; 2024 } else { 2025 map = *mapp++; 2026 bit = 1; 2027 } 2028 } 2029 forw = i - start; 2030 /* 2031 * Find the size of the cluster going backward. 2032 */ 2033 start = blkno - 1; 2034 end = start - fs->fs_contigsumsize; 2035 if (end < 0) 2036 end = -1; 2037 mapp = &freemapp[start / NBBY]; 2038 map = *mapp--; 2039 bit = 1 << (start % NBBY); 2040 for (i = start; i > end; i--) { 2041 if ((map & bit) == 0) 2042 break; 2043 if ((i & (NBBY - 1)) != 0) { 2044 bit >>= 1; 2045 } else { 2046 map = *mapp--; 2047 bit = 1 << (NBBY - 1); 2048 } 2049 } 2050 back = start - i; 2051 /* 2052 * Account for old cluster and the possibly new forward and 2053 * back clusters. 2054 */ 2055 i = back + forw + 1; 2056 if (i > fs->fs_contigsumsize) 2057 i = fs->fs_contigsumsize; 2058 sump[i] += cnt; 2059 if (back > 0) 2060 sump[back] -= cnt; 2061 if (forw > 0) 2062 sump[forw] -= cnt; 2063 /* 2064 * Update cluster summary information. 2065 */ 2066 lp = &sump[fs->fs_contigsumsize]; 2067 for (i = fs->fs_contigsumsize; i > 0; i--) 2068 if (*lp-- > 0) 2069 break; 2070 fs->fs_maxcluster[cgp->cg_cgx] = i; 2071 } 2072 2073 /* 2074 * Fserr prints the name of a filesystem with an error diagnostic. 2075 * 2076 * The form of the error message is: 2077 * fs: error message 2078 */ 2079 static void 2080 ffs_fserr(fs, inum, cp) 2081 struct fs *fs; 2082 ino_t inum; 2083 char *cp; 2084 { 2085 struct proc *p = curproc; /* XXX */ 2086 2087 log(LOG_ERR, "pid %d (%s), uid %d inumber %d on %s: %s\n", 2088 p ? p->p_pid : -1, p ? p->p_comm : "-", 2089 p ? p->p_ucred->cr_uid : 0, inum, fs->fs_fsmnt, cp); 2090 } 2091 2092 /* 2093 * This function provides the capability for the fsck program to 2094 * update an active filesystem. Six operations are provided: 2095 * 2096 * adjrefcnt(inode, amt) - adjusts the reference count on the 2097 * specified inode by the specified amount. Under normal 2098 * operation the count should always go down. Decrementing 2099 * the count to zero will cause the inode to be freed. 2100 * adjblkcnt(inode, amt) - adjust the number of blocks used to 2101 * by the specifed amount. 2102 * freedirs(inode, count) - directory inodes [inode..inode + count - 1] 2103 * are marked as free. Inodes should never have to be marked 2104 * as in use. 2105 * freefiles(inode, count) - file inodes [inode..inode + count - 1] 2106 * are marked as free. Inodes should never have to be marked 2107 * as in use. 2108 * freeblks(blockno, size) - blocks [blockno..blockno + size - 1] 2109 * are marked as free. Blocks should never have to be marked 2110 * as in use. 2111 * setflags(flags, set/clear) - the fs_flags field has the specified 2112 * flags set (second parameter +1) or cleared (second parameter -1). 2113 */ 2114 2115 static int sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS); 2116 2117 SYSCTL_PROC(_vfs_ffs, FFS_ADJ_REFCNT, adjrefcnt, CTLFLAG_WR|CTLTYPE_STRUCT, 2118 0, 0, sysctl_ffs_fsck, "S,fsck", "Adjust Inode Reference Count"); 2119 2120 SYSCTL_NODE(_vfs_ffs, FFS_ADJ_BLKCNT, adjblkcnt, CTLFLAG_WR, 2121 sysctl_ffs_fsck, "Adjust Inode Used Blocks Count"); 2122 2123 SYSCTL_NODE(_vfs_ffs, FFS_DIR_FREE, freedirs, CTLFLAG_WR, 2124 sysctl_ffs_fsck, "Free Range of Directory Inodes"); 2125 2126 SYSCTL_NODE(_vfs_ffs, FFS_FILE_FREE, freefiles, CTLFLAG_WR, 2127 sysctl_ffs_fsck, "Free Range of File Inodes"); 2128 2129 SYSCTL_NODE(_vfs_ffs, FFS_BLK_FREE, freeblks, CTLFLAG_WR, 2130 sysctl_ffs_fsck, "Free Range of Blocks"); 2131 2132 SYSCTL_NODE(_vfs_ffs, FFS_SET_FLAGS, setflags, CTLFLAG_WR, 2133 sysctl_ffs_fsck, "Change Filesystem Flags"); 2134 2135 #ifdef DEBUG 2136 static int fsckcmds = 0; 2137 SYSCTL_INT(_debug, OID_AUTO, fsckcmds, CTLFLAG_RW, &fsckcmds, 0, ""); 2138 #endif /* DEBUG */ 2139 2140 static int 2141 sysctl_ffs_fsck(SYSCTL_HANDLER_ARGS) 2142 { 2143 struct fsck_cmd cmd; 2144 struct ufsmount *ump; 2145 struct vnode *vp; 2146 struct inode *ip; 2147 struct mount *mp; 2148 struct fs *fs; 2149 ufs2_daddr_t blkno; 2150 long blkcnt, blksize; 2151 struct file *fp; 2152 int filetype, error; 2153 2154 if (req->newlen > sizeof cmd) 2155 return (EBADRPC); 2156 if ((error = SYSCTL_IN(req, &cmd, sizeof cmd)) != 0) 2157 return (error); 2158 if (cmd.version != FFS_CMD_VERSION) 2159 return (ERPCMISMATCH); 2160 if ((error = getvnode(curproc->p_fd, cmd.handle, &fp)) != 0) 2161 return (error); 2162 vn_start_write((struct vnode *)fp->f_data, &mp, V_WAIT); 2163 if (mp == 0 || strncmp(mp->mnt_stat.f_fstypename, "ufs", MFSNAMELEN)) { 2164 vn_finished_write(mp); 2165 fdrop(fp, curthread); 2166 return (EINVAL); 2167 } 2168 if (mp->mnt_flag & MNT_RDONLY) { 2169 vn_finished_write(mp); 2170 fdrop(fp, curthread); 2171 return (EROFS); 2172 } 2173 ump = VFSTOUFS(mp); 2174 fs = ump->um_fs; 2175 filetype = IFREG; 2176 2177 switch (oidp->oid_number) { 2178 2179 case FFS_SET_FLAGS: 2180 #ifdef DEBUG 2181 if (fsckcmds) 2182 printf("%s: %s flags\n", mp->mnt_stat.f_mntonname, 2183 cmd.size > 0 ? "set" : "clear"); 2184 #endif /* DEBUG */ 2185 if (cmd.size > 0) 2186 fs->fs_flags |= (long)cmd.value; 2187 else 2188 fs->fs_flags &= ~(long)cmd.value; 2189 break; 2190 2191 case FFS_ADJ_REFCNT: 2192 #ifdef DEBUG 2193 if (fsckcmds) { 2194 printf("%s: adjust inode %jd count by %jd\n", 2195 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value, 2196 (intmax_t)cmd.size); 2197 } 2198 #endif /* DEBUG */ 2199 if ((error = VFS_VGET(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp))) 2200 break; 2201 ip = VTOI(vp); 2202 ip->i_nlink += cmd.size; 2203 DIP(ip, i_nlink) = ip->i_nlink; 2204 ip->i_effnlink += cmd.size; 2205 ip->i_flag |= IN_CHANGE; 2206 if (DOINGSOFTDEP(vp)) 2207 softdep_change_linkcnt(ip); 2208 vput(vp); 2209 break; 2210 2211 case FFS_ADJ_BLKCNT: 2212 #ifdef DEBUG 2213 if (fsckcmds) { 2214 printf("%s: adjust inode %jd block count by %jd\n", 2215 mp->mnt_stat.f_mntonname, (intmax_t)cmd.value, 2216 (intmax_t)cmd.size); 2217 } 2218 #endif /* DEBUG */ 2219 if ((error = VFS_VGET(mp, (ino_t)cmd.value, LK_EXCLUSIVE, &vp))) 2220 break; 2221 ip = VTOI(vp); 2222 DIP(ip, i_blocks) += cmd.size; 2223 ip->i_flag |= IN_CHANGE; 2224 vput(vp); 2225 break; 2226 2227 case FFS_DIR_FREE: 2228 filetype = IFDIR; 2229 /* fall through */ 2230 2231 case FFS_FILE_FREE: 2232 #ifdef DEBUG 2233 if (fsckcmds) { 2234 if (cmd.size == 1) 2235 printf("%s: free %s inode %d\n", 2236 mp->mnt_stat.f_mntonname, 2237 filetype == IFDIR ? "directory" : "file", 2238 (ino_t)cmd.value); 2239 else 2240 printf("%s: free %s inodes %d-%d\n", 2241 mp->mnt_stat.f_mntonname, 2242 filetype == IFDIR ? "directory" : "file", 2243 (ino_t)cmd.value, 2244 (ino_t)(cmd.value + cmd.size - 1)); 2245 } 2246 #endif /* DEBUG */ 2247 while (cmd.size > 0) { 2248 if ((error = ffs_freefile(fs, ump->um_devvp, cmd.value, 2249 filetype))) 2250 break; 2251 cmd.size -= 1; 2252 cmd.value += 1; 2253 } 2254 break; 2255 2256 case FFS_BLK_FREE: 2257 #ifdef DEBUG 2258 if (fsckcmds) { 2259 if (cmd.size == 1) 2260 printf("%s: free block %lld\n", 2261 mp->mnt_stat.f_mntonname, 2262 (intmax_t)cmd.value); 2263 else 2264 printf("%s: free blocks %lld-%lld\n", 2265 mp->mnt_stat.f_mntonname, 2266 (intmax_t)cmd.value, 2267 (intmax_t)cmd.value + cmd.size - 1); 2268 } 2269 #endif /* DEBUG */ 2270 blkno = cmd.value; 2271 blkcnt = cmd.size; 2272 blksize = fs->fs_frag - (blkno % fs->fs_frag); 2273 while (blkcnt > 0) { 2274 if (blksize > blkcnt) 2275 blksize = blkcnt; 2276 ffs_blkfree(fs, ump->um_devvp, blkno, 2277 blksize * fs->fs_fsize, ROOTINO); 2278 blkno += blksize; 2279 blkcnt -= blksize; 2280 blksize = fs->fs_frag; 2281 } 2282 break; 2283 2284 default: 2285 #ifdef DEBUG 2286 if (fsckcmds) { 2287 printf("Invalid request %d from fsck\n", 2288 oidp->oid_number); 2289 } 2290 #endif /* DEBUG */ 2291 error = EINVAL; 2292 break; 2293 2294 } 2295 fdrop(fp, curthread); 2296 vn_finished_write(mp); 2297 return (error); 2298 } 2299