+ if ((bprev = ip->i_db[lbprev]) == 0) {
+ printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
+ ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
+ panic("ffs_realloccg: bad bprev");
+ }
+ /*
+ * Allocate the extra space in the buffer.
+ */
+ if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
+ brelse(bp);
+ return (error);
+ }
+#ifdef QUOTA
+ if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
+ brelse(bp);
+ return (error);
+ }
+#endif
+ /*
+ * Check for extension in the existing location.
+ */
+ cg = dtog(fs, bprev);
+ if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
+ if (bp->b_blkno != fsbtodb(fs, bno))
+ panic("bad blockno");
+ ip->i_blocks += btodb(nsize - osize);
+ ip->i_flag |= IUPD|ICHG;
+ allocbuf(bp, nsize);
+ bp->b_flags |= B_DONE;
+ bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
+ *bpp = bp;
+ return (0);
+ }
+ /*
+ * Allocate a new disk location.
+ */
+ if (bpref >= fs->fs_size)
+ bpref = 0;
+ switch ((int)fs->fs_optim) {
+ case FS_OPTSPACE:
+ /*
+ * Allocate an exact sized fragment. Although this makes
+ * best use of space, we will waste time relocating it if
+ * the file continues to grow. If the fragmentation is
+ * less than half of the minimum free reserve, we choose
+ * to begin optimizing for time.
+ */
+ request = nsize;
+ if (fs->fs_minfree < 5 ||
+ fs->fs_cstotal.cs_nffree >
+ fs->fs_dsize * fs->fs_minfree / (2 * 100))
+ break;
+ log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
+ fs->fs_fsmnt);
+ fs->fs_optim = FS_OPTTIME;
+ break;
+ case FS_OPTTIME:
+ /*
+ * At this point we have discovered a file that is trying to
+ * grow a small fragment to a larger fragment. To save time,
+ * we allocate a full sized block, then free the unused portion.
+ * If the file continues to grow, the `ffs_fragextend' call
+ * above will be able to grow it in place without further
+ * copying. If aberrant programs cause disk fragmentation to
+ * grow within 2% of the free reserve, we choose to begin
+ * optimizing for space.
+ */
+ request = fs->fs_bsize;
+ if (fs->fs_cstotal.cs_nffree <
+ fs->fs_dsize * (fs->fs_minfree - 2) / 100)
+ break;
+ log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
+ fs->fs_fsmnt);
+ fs->fs_optim = FS_OPTSPACE;
+ break;
+ default:
+ printf("dev = 0x%x, optim = %d, fs = %s\n",
+ ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
+ panic("ffs_realloccg: bad optim");
+ /* NOTREACHED */
+ }
+ bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
+ (u_long (*)())ffs_alloccg);
+ if (bno > 0) {
+#ifdef SECSIZE
+ obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize,
+ fs->fs_dbsize);
+#else SECSIZE
+ count = howmany(osize, CLBYTES);
+ for (i = 0; i < count; i++)
+#ifdef SECSIZE
+ munhash(ip->i_dev, bn + i * CLBYTES / fs->fs_dbsize);
+#else SECSIZE
+ munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE);
+#endif SECSIZE
+ ffs_blkfree(ip, bprev, (off_t)osize);
+ if (nsize < request)
+ ffs_blkfree(ip, bno + numfrags(fs, nsize),
+ (off_t)(request - nsize));
+ ip->i_blocks += btodb(nsize - osize);
+ ip->i_flag |= IUPD|ICHG;
+ allocbuf(bp, nsize);
+ bp->b_flags |= B_DONE;
+ bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
+ *bpp = bp;
+ return (0);
+ }
+#ifdef QUOTA
+ /*
+ * Restore user's disk quota because allocation failed.
+ */
+ (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
+#endif
+ brelse(bp);