123394Smckusick /*
263371Sbostic * Copyright (c) 1982, 1986, 1989, 1993
363371Sbostic * The Regents of the University of California. All rights reserved.
423394Smckusick *
544536Sbostic * %sccs.include.redist.c%
634432Sbostic *
7*69705Smckusick * @(#)ffs_alloc.c 8.18 (Berkeley) 05/26/95
823394Smckusick */
94359Smckusick
1051469Sbostic #include <sys/param.h>
1151469Sbostic #include <sys/systm.h>
1251469Sbostic #include <sys/buf.h>
1351469Sbostic #include <sys/proc.h>
1451469Sbostic #include <sys/vnode.h>
1554653Smckusick #include <sys/mount.h>
1651469Sbostic #include <sys/kernel.h>
1751469Sbostic #include <sys/syslog.h>
184359Smckusick
1953317Smckusick #include <vm/vm.h>
2053317Smckusick
2151469Sbostic #include <ufs/ufs/quota.h>
2251469Sbostic #include <ufs/ufs/inode.h>
2347571Skarels
2451469Sbostic #include <ufs/ffs/fs.h>
2551469Sbostic #include <ufs/ffs/ffs_extern.h>
264359Smckusick
2751469Sbostic extern u_long nextgennumber;
2851469Sbostic
2968554Smckusick static ufs_daddr_t ffs_alloccg __P((struct inode *, int, ufs_daddr_t, int));
3068554Smckusick static ufs_daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, ufs_daddr_t));
3168554Smckusick static ufs_daddr_t ffs_clusteralloc __P((struct inode *, int, ufs_daddr_t,
3268554Smckusick int));
3351469Sbostic static ino_t ffs_dirpref __P((struct fs *));
3468554Smckusick static ufs_daddr_t ffs_fragextend __P((struct inode *, int, long, int, int));
3551469Sbostic static void ffs_fserr __P((struct fs *, u_int, char *));
3651469Sbostic static u_long ffs_hashalloc
3768122Smckusick __P((struct inode *, int, long, int, u_int32_t (*)()));
3868554Smckusick static ino_t ffs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int));
3968554Smckusick static ufs_daddr_t ffs_mapsearch __P((struct fs *, struct cg *, ufs_daddr_t,
4068554Smckusick int));
4151469Sbostic
425375Smckusic /*
435375Smckusic * Allocate a block in the file system.
445375Smckusic *
455375Smckusic * The size of the requested block is given, which must be some
465375Smckusic * multiple of fs_fsize and <= fs_bsize.
475375Smckusic * A preference may be optionally specified. If a preference is given
485375Smckusic * the following hierarchy is used to allocate a block:
495375Smckusic * 1) allocate the requested block.
505375Smckusic * 2) allocate a rotationally optimal block in the same cylinder.
515375Smckusic * 3) allocate a block in the same cylinder group.
525375Smckusic * 4) quadradically rehash into other cylinder groups, until an
535375Smckusic * available block is located.
545375Smckusic * If no block preference is given the following heirarchy is used
555375Smckusic * to allocate a block:
565375Smckusic * 1) allocate a block in the cylinder group that contains the
575375Smckusic * inode for the file.
585375Smckusic * 2) quadradically rehash into other cylinder groups, until an
595375Smckusic * available block is located.
605375Smckusic */
ffs_alloc(ip,lbn,bpref,size,cred,bnp)6153244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp)
624463Smckusic register struct inode *ip;
6368554Smckusick ufs_daddr_t lbn, bpref;
644359Smckusick int size;
6553244Smckusick struct ucred *cred;
6668554Smckusick ufs_daddr_t *bnp;
674359Smckusick {
6865468Sbostic register struct fs *fs;
6968554Smckusick ufs_daddr_t bno;
7037735Smckusick int cg, error;
714359Smckusick
7239678Smckusick *bnp = 0;
735965Smckusic fs = ip->i_fs;
7453244Smckusick #ifdef DIAGNOSTIC
7564508Sbostic if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
766716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
776716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7851469Sbostic panic("ffs_alloc: bad size");
796716Smckusick }
8053244Smckusick if (cred == NOCRED)
8153244Smckusick panic("ffs_alloc: missing credential\n");
8253244Smckusick #endif /* DIAGNOSTIC */
835322Smckusic if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
844359Smckusick goto nospace;
8541309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
864792Smckusic goto nospace;
877650Ssam #ifdef QUOTA
8841309Smckusick if (error = chkdq(ip, (long)btodb(size), cred, 0))
8937735Smckusick return (error);
907483Skre #endif
914948Smckusic if (bpref >= fs->fs_size)
924948Smckusic bpref = 0;
934359Smckusick if (bpref == 0)
9464603Sbostic cg = ino_to_cg(fs, ip->i_number);
954359Smckusick else
965377Smckusic cg = dtog(fs, bpref);
9768554Smckusick bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
9868122Smckusick (u_int32_t (*)())ffs_alloccg);
9939678Smckusick if (bno > 0) {
10039678Smckusick ip->i_blocks += btodb(size);
10164603Sbostic ip->i_flag |= IN_CHANGE | IN_UPDATE;
10239678Smckusick *bnp = bno;
10339678Smckusick return (0);
10439678Smckusick }
10545173Smckusick #ifdef QUOTA
10645173Smckusick /*
10745173Smckusick * Restore user's disk quota because allocation failed.
10845173Smckusick */
10945173Smckusick (void) chkdq(ip, (long)-btodb(size), cred, FORCE);
11045173Smckusick #endif
1114359Smckusick nospace:
11251469Sbostic ffs_fserr(fs, cred->cr_uid, "file system full");
1134359Smckusick uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
11437735Smckusick return (ENOSPC);
1154359Smckusick }
1164359Smckusick
1175375Smckusic /*
1185375Smckusic * Reallocate a fragment to a bigger size
1195375Smckusic *
1205375Smckusic * The number and size of the old block is given, and a preference
1215375Smckusic * and new size is also specified. The allocator attempts to extend
1225375Smckusic * the original block. Failing that, the regular block allocator is
1235375Smckusic * invoked to get an appropriate block.
1245375Smckusic */
ffs_realloccg(ip,lbprev,bpref,osize,nsize,cred,bpp)12553244Smckusick ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
1265965Smckusic register struct inode *ip;
12768554Smckusick ufs_daddr_t lbprev;
12868554Smckusick ufs_daddr_t bpref;
1294426Smckusic int osize, nsize;
13053244Smckusick struct ucred *cred;
13137735Smckusick struct buf **bpp;
1324426Smckusic {
1334426Smckusic register struct fs *fs;
13465468Sbostic struct buf *bp;
13545719Smckusick int cg, request, error;
13668554Smckusick ufs_daddr_t bprev, bno;
1374426Smckusic
13837735Smckusick *bpp = 0;
1395965Smckusic fs = ip->i_fs;
14053244Smckusick #ifdef DIAGNOSTIC
14164508Sbostic if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
14264508Sbostic (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
14351469Sbostic printf(
14451469Sbostic "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1456716Smckusick ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
14651469Sbostic panic("ffs_realloccg: bad size");
1476716Smckusick }
14853244Smckusick if (cred == NOCRED)
14953244Smckusick panic("ffs_realloccg: missing credential\n");
15053244Smckusick #endif /* DIAGNOSTIC */
15141309Smckusick if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1524792Smckusic goto nospace;
15339678Smckusick if ((bprev = ip->i_db[lbprev]) == 0) {
1546716Smckusick printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1556716Smckusick ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
15651469Sbostic panic("ffs_realloccg: bad bprev");
1576716Smckusick }
15839678Smckusick /*
15939678Smckusick * Allocate the extra space in the buffer.
16039678Smckusick */
16139678Smckusick if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
16239678Smckusick brelse(bp);
16339678Smckusick return (error);
16439678Smckusick }
16545173Smckusick #ifdef QUOTA
16645173Smckusick if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
16745173Smckusick brelse(bp);
16845173Smckusick return (error);
16945173Smckusick }
17045173Smckusick #endif
17139678Smckusick /*
17239678Smckusick * Check for extension in the existing location.
17339678Smckusick */
1746294Smckusick cg = dtog(fs, bprev);
17551469Sbostic if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
17639887Smckusick if (bp->b_blkno != fsbtodb(fs, bno))
17739678Smckusick panic("bad blockno");
17839762Smckusick ip->i_blocks += btodb(nsize - osize);
17964603Sbostic ip->i_flag |= IN_CHANGE | IN_UPDATE;
18048948Smckusick allocbuf(bp, nsize);
18148948Smckusick bp->b_flags |= B_DONE;
18264508Sbostic bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
18337735Smckusick *bpp = bp;
18437735Smckusick return (0);
1854463Smckusic }
18639678Smckusick /*
18739678Smckusick * Allocate a new disk location.
18839678Smckusick */
1894948Smckusic if (bpref >= fs->fs_size)
1904948Smckusic bpref = 0;
19126253Skarels switch ((int)fs->fs_optim) {
19225256Smckusick case FS_OPTSPACE:
19325256Smckusick /*
19425256Smckusick * Allocate an exact sized fragment. Although this makes
19525256Smckusick * best use of space, we will waste time relocating it if
19625256Smckusick * the file continues to grow. If the fragmentation is
19725256Smckusick * less than half of the minimum free reserve, we choose
19825256Smckusick * to begin optimizing for time.
19925256Smckusick */
20024698Smckusick request = nsize;
20125256Smckusick if (fs->fs_minfree < 5 ||
20225256Smckusick fs->fs_cstotal.cs_nffree >
20325256Smckusick fs->fs_dsize * fs->fs_minfree / (2 * 100))
20425256Smckusick break;
20525256Smckusick log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
20625256Smckusick fs->fs_fsmnt);
20725256Smckusick fs->fs_optim = FS_OPTTIME;
20825256Smckusick break;
20925256Smckusick case FS_OPTTIME:
21025256Smckusick /*
21151469Sbostic * At this point we have discovered a file that is trying to
21251469Sbostic * grow a small fragment to a larger fragment. To save time,
21351469Sbostic * we allocate a full sized block, then free the unused portion.
21451469Sbostic * If the file continues to grow, the `ffs_fragextend' call
21551469Sbostic * above will be able to grow it in place without further
21651469Sbostic * copying. If aberrant programs cause disk fragmentation to
21751469Sbostic * grow within 2% of the free reserve, we choose to begin
21851469Sbostic * optimizing for space.
21925256Smckusick */
22024698Smckusick request = fs->fs_bsize;
22125256Smckusick if (fs->fs_cstotal.cs_nffree <
22225256Smckusick fs->fs_dsize * (fs->fs_minfree - 2) / 100)
22325256Smckusick break;
22425256Smckusick log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
22525256Smckusick fs->fs_fsmnt);
22625256Smckusick fs->fs_optim = FS_OPTSPACE;
22725256Smckusick break;
22825256Smckusick default:
22925256Smckusick printf("dev = 0x%x, optim = %d, fs = %s\n",
23025256Smckusick ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
23151469Sbostic panic("ffs_realloccg: bad optim");
23225256Smckusick /* NOTREACHED */
23325256Smckusick }
23468554Smckusick bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
23568122Smckusick (u_int32_t (*)())ffs_alloccg);
2366567Smckusic if (bno > 0) {
23745719Smckusick bp->b_blkno = fsbtodb(fs, bno);
23845719Smckusick (void) vnode_pager_uncache(ITOV(ip));
23953059Smckusick ffs_blkfree(ip, bprev, (long)osize);
24024698Smckusick if (nsize < request)
24151469Sbostic ffs_blkfree(ip, bno + numfrags(fs, nsize),
24253059Smckusick (long)(request - nsize));
24312643Ssam ip->i_blocks += btodb(nsize - osize);
24464603Sbostic ip->i_flag |= IN_CHANGE | IN_UPDATE;
24548948Smckusick allocbuf(bp, nsize);
24648948Smckusick bp->b_flags |= B_DONE;
24764508Sbostic bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
24837735Smckusick *bpp = bp;
24937735Smckusick return (0);
2504463Smckusic }
25145173Smckusick #ifdef QUOTA
25245173Smckusick /*
25345173Smckusick * Restore user's disk quota because allocation failed.
25445173Smckusick */
25545173Smckusick (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
25645173Smckusick #endif
25739678Smckusick brelse(bp);
2584792Smckusic nospace:
2594463Smckusic /*
2604463Smckusic * no space available
2614463Smckusic */
26251469Sbostic ffs_fserr(fs, cred->cr_uid, "file system full");
2634426Smckusic uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
26437735Smckusick return (ENOSPC);
2654426Smckusic }
2664426Smckusic
2675375Smckusic /*
26865999Smckusick * Reallocate a sequence of blocks into a contiguous sequence of blocks.
26965999Smckusick *
27065999Smckusick * The vnode and an array of buffer pointers for a range of sequential
27165999Smckusick * logical blocks to be made contiguous is given. The allocator attempts
27265999Smckusick * to find a range of sequential blocks starting as close as possible to
27365999Smckusick * an fs_rotdelay offset from the end of the allocation for the logical
27465999Smckusick * block immediately preceeding the current range. If successful, the
27565999Smckusick * physical block numbers in the buffer pointers and in the inode are
27665999Smckusick * changed to reflect the new allocation. If unsuccessful, the allocation
27765999Smckusick * is left unchanged. The success in doing the reallocation is returned.
27865999Smckusick * Note that the error return is not reflected back to the user. Rather
27965999Smckusick * the previous block allocation will be used.
28065999Smckusick */
28166176Smckusick int doasyncfree = 1;
28268664Smckusick int doreallocblks = 1;
28367871Smckusick int prtrealloc = 0;
28467392Smkm
28565999Smckusick int
ffs_reallocblks(ap)28665999Smckusick ffs_reallocblks(ap)
28765999Smckusick struct vop_reallocblks_args /* {
28865999Smckusick struct vnode *a_vp;
28965999Smckusick struct cluster_save *a_buflist;
29065999Smckusick } */ *ap;
29165999Smckusick {
29265999Smckusick struct fs *fs;
29365999Smckusick struct inode *ip;
29465999Smckusick struct vnode *vp;
29565999Smckusick struct buf *sbp, *ebp;
29668554Smckusick ufs_daddr_t *bap, *sbap, *ebap;
29765999Smckusick struct cluster_save *buflist;
29868554Smckusick ufs_daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno;
29965999Smckusick struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
30065999Smckusick int i, len, start_lvl, end_lvl, pref, ssize;
30165999Smckusick
30268664Smckusick if (doreallocblks == 0)
30368664Smckusick return (ENOSPC);
30465999Smckusick vp = ap->a_vp;
30565999Smckusick ip = VTOI(vp);
30665999Smckusick fs = ip->i_fs;
30765999Smckusick if (fs->fs_contigsumsize <= 0)
30865999Smckusick return (ENOSPC);
30965999Smckusick buflist = ap->a_buflist;
31065999Smckusick len = buflist->bs_nchildren;
31165999Smckusick start_lbn = buflist->bs_children[0]->b_lblkno;
31265999Smckusick end_lbn = start_lbn + len - 1;
31365999Smckusick #ifdef DIAGNOSTIC
314*69705Smckusick for (i = 0; i < len; i++)
315*69705Smckusick if (!ffs_checkblk(ip,
316*69705Smckusick dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
317*69705Smckusick panic("ffs_reallocblks: unallocated block 1");
31865999Smckusick for (i = 1; i < len; i++)
31965999Smckusick if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
320*69705Smckusick panic("ffs_reallocblks: non-logical cluster");
321*69705Smckusick blkno = buflist->bs_children[0]->b_blkno;
322*69705Smckusick ssize = fsbtodb(fs, fs->fs_frag);
323*69705Smckusick for (i = 1; i < len - 1; i++)
324*69705Smckusick if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
325*69705Smckusick panic("ffs_reallocblks: non-physical cluster %d", i);
32665999Smckusick #endif
32765999Smckusick /*
32865999Smckusick * If the latest allocation is in a new cylinder group, assume that
32965999Smckusick * the filesystem has decided to move and do not force it back to
33065999Smckusick * the previous cylinder group.
33165999Smckusick */
33265999Smckusick if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
33365999Smckusick dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
33465999Smckusick return (ENOSPC);
33565999Smckusick if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
33665999Smckusick ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
33765999Smckusick return (ENOSPC);
33865999Smckusick /*
33965999Smckusick * Get the starting offset and block map for the first block.
34065999Smckusick */
34165999Smckusick if (start_lvl == 0) {
34265999Smckusick sbap = &ip->i_db[0];
34365999Smckusick soff = start_lbn;
34465999Smckusick } else {
34565999Smckusick idp = &start_ap[start_lvl - 1];
34665999Smckusick if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
34765999Smckusick brelse(sbp);
34865999Smckusick return (ENOSPC);
34965999Smckusick }
35068554Smckusick sbap = (ufs_daddr_t *)sbp->b_data;
35165999Smckusick soff = idp->in_off;
35265999Smckusick }
35365999Smckusick /*
35465999Smckusick * Find the preferred location for the cluster.
35565999Smckusick */
35665999Smckusick pref = ffs_blkpref(ip, start_lbn, soff, sbap);
35765999Smckusick /*
35865999Smckusick * If the block range spans two block maps, get the second map.
35965999Smckusick */
36066086Smckusick if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
36165999Smckusick ssize = len;
36265999Smckusick } else {
36366086Smckusick #ifdef DIAGNOSTIC
36466086Smckusick if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
36566086Smckusick panic("ffs_reallocblk: start == end");
36666086Smckusick #endif
36765999Smckusick ssize = len - (idp->in_off + 1);
36865999Smckusick if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
36965999Smckusick goto fail;
37068554Smckusick ebap = (ufs_daddr_t *)ebp->b_data;
37165999Smckusick }
37265999Smckusick /*
37365999Smckusick * Search the block map looking for an allocation of the desired size.
37465999Smckusick */
37568554Smckusick if ((newblk = (ufs_daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
37668122Smckusick len, (u_int32_t (*)())ffs_clusteralloc)) == 0)
37765999Smckusick goto fail;
37865999Smckusick /*
37965999Smckusick * We have found a new contiguous block.
38065999Smckusick *
38165999Smckusick * First we have to replace the old block pointers with the new
38265999Smckusick * block pointers in the inode and indirect blocks associated
38365999Smckusick * with the file.
38465999Smckusick */
38567871Smckusick #ifdef DEBUG
38667871Smckusick if (prtrealloc)
38767871Smckusick printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
38868112Smckusick start_lbn, end_lbn);
38968112Smckusick #endif
39065999Smckusick blkno = newblk;
39165999Smckusick for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
39265999Smckusick if (i == ssize)
39365999Smckusick bap = ebap;
39465999Smckusick #ifdef DIAGNOSTIC
395*69705Smckusick if (!ffs_checkblk(ip,
396*69705Smckusick dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
397*69705Smckusick panic("ffs_reallocblks: unallocated block 2");
39867869Smckusick if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
39965999Smckusick panic("ffs_reallocblks: alloc mismatch");
40065999Smckusick #endif
40167871Smckusick #ifdef DEBUG
40267871Smckusick if (prtrealloc)
40367871Smckusick printf(" %d,", *bap);
40467871Smckusick #endif
40565999Smckusick *bap++ = blkno;
40665999Smckusick }
40765999Smckusick /*
40865999Smckusick * Next we must write out the modified inode and indirect blocks.
40966176Smckusick * For strict correctness, the writes should be synchronous since
41066176Smckusick * the old block values may have been written to disk. In practise
41166176Smckusick * they are almost never written, but if we are concerned about
41266176Smckusick * strict correctness, the `doasyncfree' flag should be set to zero.
41365999Smckusick *
41466176Smckusick * The test on `doasyncfree' should be changed to test a flag
41566176Smckusick * that shows whether the associated buffers and inodes have
41666176Smckusick * been written. The flag should be set when the cluster is
41766176Smckusick * started and cleared whenever the buffer or inode is flushed.
41866176Smckusick * We can then check below to see if it is set, and do the
41966176Smckusick * synchronous write only when it has been cleared.
42065999Smckusick */
42165999Smckusick if (sbap != &ip->i_db[0]) {
42266176Smckusick if (doasyncfree)
42366176Smckusick bdwrite(sbp);
42466176Smckusick else
42566176Smckusick bwrite(sbp);
42665999Smckusick } else {
42765999Smckusick ip->i_flag |= IN_CHANGE | IN_UPDATE;
42866176Smckusick if (!doasyncfree)
42966176Smckusick VOP_UPDATE(vp, &time, &time, MNT_WAIT);
43065999Smckusick }
43165999Smckusick if (ssize < len)
43266176Smckusick if (doasyncfree)
43366176Smckusick bdwrite(ebp);
43466176Smckusick else
43566176Smckusick bwrite(ebp);
43665999Smckusick /*
43765999Smckusick * Last, free the old blocks and assign the new blocks to the buffers.
43865999Smckusick */
43967871Smckusick #ifdef DEBUG
44067871Smckusick if (prtrealloc)
44167871Smckusick printf("\n\tnew:");
44267871Smckusick #endif
44365999Smckusick for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
44465999Smckusick ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
44565999Smckusick fs->fs_bsize);
44665999Smckusick buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
44767871Smckusick #ifdef DEBUG
448*69705Smckusick if (!ffs_checkblk(ip,
449*69705Smckusick dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
450*69705Smckusick panic("ffs_reallocblks: unallocated block 3");
45167871Smckusick if (prtrealloc)
45267871Smckusick printf(" %d,", blkno);
45367871Smckusick #endif
45465999Smckusick }
45567871Smckusick #ifdef DEBUG
45667871Smckusick if (prtrealloc) {
45767871Smckusick prtrealloc--;
45867871Smckusick printf("\n");
45967871Smckusick }
46067871Smckusick #endif
46165999Smckusick return (0);
46265999Smckusick
46365999Smckusick fail:
46465999Smckusick if (ssize < len)
46565999Smckusick brelse(ebp);
46665999Smckusick if (sbap != &ip->i_db[0])
46765999Smckusick brelse(sbp);
46865999Smckusick return (ENOSPC);
46965999Smckusick }
47065999Smckusick
47165999Smckusick /*
4725375Smckusic * Allocate an inode in the file system.
4735375Smckusic *
47451469Sbostic * If allocating a directory, use ffs_dirpref to select the inode.
47551469Sbostic * If allocating in a directory, the following hierarchy is followed:
47651469Sbostic * 1) allocate the preferred inode.
4775375Smckusic * 2) allocate an inode in the same cylinder group.
4785375Smckusic * 3) quadradically rehash into other cylinder groups, until an
4795375Smckusic * available inode is located.
4805375Smckusic * If no inode preference is given the following heirarchy is used
4815375Smckusic * to allocate an inode:
4825375Smckusic * 1) allocate an inode in cylinder group 0.
4835375Smckusic * 2) quadradically rehash into other cylinder groups, until an
4845375Smckusic * available inode is located.
4855375Smckusic */
48654653Smckusick ffs_valloc(ap)
48754653Smckusick struct vop_valloc_args /* {
48854653Smckusick struct vnode *a_pvp;
48954653Smckusick int a_mode;
49054653Smckusick struct ucred *a_cred;
49154653Smckusick struct vnode **a_vpp;
49254653Smckusick } */ *ap;
4934359Smckusick {
49453865Sheideman register struct vnode *pvp = ap->a_pvp;
49551541Smckusick register struct inode *pip;
4964359Smckusick register struct fs *fs;
4974359Smckusick register struct inode *ip;
49854653Smckusick mode_t mode = ap->a_mode;
49951469Sbostic ino_t ino, ipref;
50037735Smckusick int cg, error;
5014359Smckusick
50253583Sheideman *ap->a_vpp = NULL;
50353865Sheideman pip = VTOI(pvp);
5045965Smckusic fs = pip->i_fs;
5054792Smckusic if (fs->fs_cstotal.cs_nifree == 0)
5064359Smckusick goto noinodes;
50751469Sbostic
50854653Smckusick if ((mode & IFMT) == IFDIR)
50951541Smckusick ipref = ffs_dirpref(fs);
51051469Sbostic else
51151469Sbostic ipref = pip->i_number;
5124948Smckusic if (ipref >= fs->fs_ncg * fs->fs_ipg)
5134948Smckusic ipref = 0;
51464603Sbostic cg = ino_to_cg(fs, ipref);
51564603Sbostic ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
5164359Smckusick if (ino == 0)
5174359Smckusick goto noinodes;
51854653Smckusick error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
51937735Smckusick if (error) {
52054653Smckusick VOP_VFREE(pvp, ino, mode);
52137735Smckusick return (error);
5224359Smckusick }
52353583Sheideman ip = VTOI(*ap->a_vpp);
5246716Smckusick if (ip->i_mode) {
52554653Smckusick printf("mode = 0%o, inum = %d, fs = %s\n",
5266716Smckusick ip->i_mode, ip->i_number, fs->fs_fsmnt);
52751541Smckusick panic("ffs_valloc: dup alloc");
5286716Smckusick }
52912643Ssam if (ip->i_blocks) { /* XXX */
53012643Ssam printf("free inode %s/%d had %d blocks\n",
53112643Ssam fs->fs_fsmnt, ino, ip->i_blocks);
53212643Ssam ip->i_blocks = 0;
53312643Ssam }
53439516Smckusick ip->i_flags = 0;
53538255Smckusick /*
53638255Smckusick * Set up a new generation number for this inode.
53738255Smckusick */
53838255Smckusick if (++nextgennumber < (u_long)time.tv_sec)
53938255Smckusick nextgennumber = time.tv_sec;
54038255Smckusick ip->i_gen = nextgennumber;
54137735Smckusick return (0);
5424359Smckusick noinodes:
54353583Sheideman ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
5446294Smckusick uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
54537735Smckusick return (ENOSPC);
5464359Smckusick }
5474359Smckusick
5484651Smckusic /*
5495375Smckusic * Find a cylinder to place a directory.
5505375Smckusic *
5515375Smckusic * The policy implemented by this algorithm is to select from
5525375Smckusic * among those cylinder groups with above the average number of
5535375Smckusic * free inodes, the one with the smallest number of directories.
5544651Smckusic */
55551469Sbostic static ino_t
ffs_dirpref(fs)55651469Sbostic ffs_dirpref(fs)
5575965Smckusic register struct fs *fs;
5584359Smckusick {
5594651Smckusic int cg, minndir, mincg, avgifree;
5604359Smckusick
5614792Smckusic avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
5624651Smckusic minndir = fs->fs_ipg;
5634359Smckusick mincg = 0;
5644651Smckusic for (cg = 0; cg < fs->fs_ncg; cg++)
5655322Smckusic if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
5665322Smckusic fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
5674359Smckusick mincg = cg;
5685322Smckusic minndir = fs->fs_cs(fs, cg).cs_ndir;
5694359Smckusick }
5709163Ssam return ((ino_t)(fs->fs_ipg * mincg));
5714359Smckusick }
5724359Smckusick
5734651Smckusic /*
5749163Ssam * Select the desired position for the next block in a file. The file is
5759163Ssam * logically divided into sections. The first section is composed of the
5769163Ssam * direct blocks. Each additional section contains fs_maxbpg blocks.
5779163Ssam *
5789163Ssam * If no blocks have been allocated in the first section, the policy is to
5799163Ssam * request a block in the same cylinder group as the inode that describes
5809163Ssam * the file. If no blocks have been allocated in any other section, the
5819163Ssam * policy is to place the section in a cylinder group with a greater than
5829163Ssam * average number of free blocks. An appropriate cylinder group is found
58317696Smckusick * by using a rotor that sweeps the cylinder groups. When a new group of
58417696Smckusick * blocks is needed, the sweep begins in the cylinder group following the
58517696Smckusick * cylinder group from which the previous allocation was made. The sweep
58617696Smckusick * continues until a cylinder group with greater than the average number
58717696Smckusick * of free blocks is found. If the allocation is for the first block in an
58817696Smckusick * indirect block, the information on the previous allocation is unavailable;
58917696Smckusick * here a best guess is made based upon the logical block number being
59017696Smckusick * allocated.
5919163Ssam *
5929163Ssam * If a section is already partially allocated, the policy is to
5939163Ssam * contiguously allocate fs_maxcontig blocks. The end of one of these
5949163Ssam * contiguous blocks and the beginning of the next is physically separated
5959163Ssam * so that the disk head will be in transit between them for at least
5969163Ssam * fs_rotdelay milliseconds. This is to allow time for the processor to
5979163Ssam * schedule another I/O transfer.
5984651Smckusic */
59968554Smckusick ufs_daddr_t
ffs_blkpref(ip,lbn,indx,bap)60051469Sbostic ffs_blkpref(ip, lbn, indx, bap)
6019163Ssam struct inode *ip;
60268554Smckusick ufs_daddr_t lbn;
6039163Ssam int indx;
60468554Smckusick ufs_daddr_t *bap;
6059163Ssam {
6065965Smckusic register struct fs *fs;
60717696Smckusick register int cg;
60817696Smckusick int avgbfree, startcg;
60968554Smckusick ufs_daddr_t nextblk;
6104651Smckusic
6119163Ssam fs = ip->i_fs;
6129163Ssam if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
6139163Ssam if (lbn < NDADDR) {
61464603Sbostic cg = ino_to_cg(fs, ip->i_number);
6155322Smckusic return (fs->fs_fpg * cg + fs->fs_frag);
6164651Smckusic }
6179163Ssam /*
6189163Ssam * Find a cylinder with greater than average number of
6199163Ssam * unused data blocks.
6209163Ssam */
62117696Smckusick if (indx == 0 || bap[indx - 1] == 0)
62264603Sbostic startcg =
62364603Sbostic ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
62417696Smckusick else
62517696Smckusick startcg = dtog(fs, bap[indx - 1]) + 1;
62617696Smckusick startcg %= fs->fs_ncg;
6279163Ssam avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
62817696Smckusick for (cg = startcg; cg < fs->fs_ncg; cg++)
6299163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
6309163Ssam fs->fs_cgrotor = cg;
6319163Ssam return (fs->fs_fpg * cg + fs->fs_frag);
6329163Ssam }
63317696Smckusick for (cg = 0; cg <= startcg; cg++)
6349163Ssam if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
6359163Ssam fs->fs_cgrotor = cg;
6369163Ssam return (fs->fs_fpg * cg + fs->fs_frag);
6379163Ssam }
6389163Ssam return (NULL);
6399163Ssam }
6409163Ssam /*
6419163Ssam * One or more previous blocks have been laid out. If less
6429163Ssam * than fs_maxcontig previous blocks are contiguous, the
6439163Ssam * next block is requested contiguously, otherwise it is
6449163Ssam * requested rotationally delayed by fs_rotdelay milliseconds.
6459163Ssam */
6469163Ssam nextblk = bap[indx - 1] + fs->fs_frag;
64756665Smargo if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] +
64856665Smargo blkstofrags(fs, fs->fs_maxcontig) != nextblk)
6499163Ssam return (nextblk);
6509163Ssam if (fs->fs_rotdelay != 0)
6519163Ssam /*
6529163Ssam * Here we convert ms of delay to frags as:
6539163Ssam * (frags) = (ms) * (rev/sec) * (sect/rev) /
6549163Ssam * ((sect/frag) * (ms/sec))
6559163Ssam * then round up to the next block.
6569163Ssam */
6579163Ssam nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
6589163Ssam (NSPF(fs) * 1000), fs->fs_frag);
6599163Ssam return (nextblk);
6604651Smckusic }
6614651Smckusic
6625375Smckusic /*
6635375Smckusic * Implement the cylinder overflow algorithm.
6645375Smckusic *
6655375Smckusic * The policy implemented by this algorithm is:
6665375Smckusic * 1) allocate the block in its requested cylinder group.
6675375Smckusic * 2) quadradically rehash on the cylinder group number.
6685375Smckusic * 3) brute force search for a free block.
6695375Smckusic */
6705212Smckusic /*VARARGS5*/
67151469Sbostic static u_long
ffs_hashalloc(ip,cg,pref,size,allocator)67251469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator)
6735965Smckusic struct inode *ip;
6744359Smckusick int cg;
6754359Smckusick long pref;
6764359Smckusick int size; /* size for data blocks, mode for inodes */
67768122Smckusick u_int32_t (*allocator)();
6784359Smckusick {
6795965Smckusic register struct fs *fs;
6804359Smckusick long result;
6814359Smckusick int i, icg = cg;
6824359Smckusick
6835965Smckusic fs = ip->i_fs;
6844359Smckusick /*
6854359Smckusick * 1: preferred cylinder group
6864359Smckusick */
6875965Smckusic result = (*allocator)(ip, cg, pref, size);
6884359Smckusick if (result)
6894359Smckusick return (result);
6904359Smckusick /*
6914359Smckusick * 2: quadratic rehash
6924359Smckusick */
6934359Smckusick for (i = 1; i < fs->fs_ncg; i *= 2) {
6944359Smckusick cg += i;
6954359Smckusick if (cg >= fs->fs_ncg)
6964359Smckusick cg -= fs->fs_ncg;
6975965Smckusic result = (*allocator)(ip, cg, 0, size);
6984359Smckusick if (result)
6994359Smckusick return (result);
7004359Smckusick }
7014359Smckusick /*
7024359Smckusick * 3: brute force search
70310847Ssam * Note that we start at i == 2, since 0 was checked initially,
70410847Ssam * and 1 is always checked in the quadratic rehash.
7054359Smckusick */
70610848Smckusick cg = (icg + 2) % fs->fs_ncg;
70710847Ssam for (i = 2; i < fs->fs_ncg; i++) {
7085965Smckusic result = (*allocator)(ip, cg, 0, size);
7094359Smckusick if (result)
7104359Smckusick return (result);
7114359Smckusick cg++;
7124359Smckusick if (cg == fs->fs_ncg)
7134359Smckusick cg = 0;
7144359Smckusick }
7156294Smckusick return (NULL);
7164359Smckusick }
7174359Smckusick
7185375Smckusic /*
7195375Smckusic * Determine whether a fragment can be extended.
7205375Smckusic *
7215375Smckusic * Check to see if the necessary fragments are available, and
7225375Smckusic * if they are, allocate them.
7235375Smckusic */
72468554Smckusick static ufs_daddr_t
ffs_fragextend(ip,cg,bprev,osize,nsize)72551469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize)
7265965Smckusic struct inode *ip;
7274426Smckusic int cg;
7284463Smckusic long bprev;
7294426Smckusic int osize, nsize;
7304426Smckusic {
7315965Smckusic register struct fs *fs;
7324463Smckusic register struct cg *cgp;
73337735Smckusick struct buf *bp;
7344463Smckusic long bno;
7354463Smckusic int frags, bbase;
73637735Smckusick int i, error;
7374426Smckusic
7385965Smckusic fs = ip->i_fs;
73917224Smckusick if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
7406531Smckusick return (NULL);
7415960Smckusic frags = numfrags(fs, nsize);
74217224Smckusick bbase = fragnum(fs, bprev);
74317224Smckusick if (bbase > fragnum(fs, (bprev + frags - 1))) {
74430749Skarels /* cannot extend across a block boundary */
7456294Smckusick return (NULL);
7464463Smckusic }
74737735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
74838776Smckusick (int)fs->fs_cgsize, NOCRED, &bp);
74937735Smckusick if (error) {
75037735Smckusick brelse(bp);
75137735Smckusick return (NULL);
75237735Smckusick }
75364508Sbostic cgp = (struct cg *)bp->b_data;
75437735Smckusick if (!cg_chkmagic(cgp)) {
7555960Smckusic brelse(bp);
7566294Smckusick return (NULL);
7575960Smckusic }
7588105Sroot cgp->cg_time = time.tv_sec;
7595377Smckusic bno = dtogd(fs, bprev);
7605960Smckusic for (i = numfrags(fs, osize); i < frags; i++)
76134143Smckusick if (isclr(cg_blksfree(cgp), bno + i)) {
7625361Smckusic brelse(bp);
7636294Smckusick return (NULL);
7645361Smckusic }
7655361Smckusic /*
7665361Smckusic * the current fragment can be extended
7675361Smckusic * deduct the count on fragment being extended into
7685361Smckusic * increase the count on the remaining fragment (if any)
7695361Smckusic * allocate the extended piece
7705361Smckusic */
7715361Smckusic for (i = frags; i < fs->fs_frag - bbase; i++)
77234143Smckusick if (isclr(cg_blksfree(cgp), bno + i))
7734463Smckusic break;
7745960Smckusic cgp->cg_frsum[i - numfrags(fs, osize)]--;
7755361Smckusic if (i != frags)
7765361Smckusic cgp->cg_frsum[i - frags]++;
7775960Smckusic for (i = numfrags(fs, osize); i < frags; i++) {
77834143Smckusick clrbit(cg_blksfree(cgp), bno + i);
7795361Smckusic cgp->cg_cs.cs_nffree--;
7805361Smckusic fs->fs_cstotal.cs_nffree--;
7815361Smckusic fs->fs_cs(fs, cg).cs_nffree--;
7824463Smckusic }
78350893Smckusick fs->fs_fmod = 1;
7845361Smckusic bdwrite(bp);
7855361Smckusic return (bprev);
7864426Smckusic }
7874426Smckusic
7885375Smckusic /*
7895375Smckusic * Determine whether a block can be allocated.
7905375Smckusic *
79164603Sbostic * Check to see if a block of the appropriate size is available,
7925375Smckusic * and if it is, allocate it.
7935375Smckusic */
79468554Smckusick static ufs_daddr_t
ffs_alloccg(ip,cg,bpref,size)79551469Sbostic ffs_alloccg(ip, cg, bpref, size)
7965965Smckusic struct inode *ip;
7974359Smckusick int cg;
79868554Smckusick ufs_daddr_t bpref;
7994359Smckusick int size;
8004359Smckusick {
8015965Smckusic register struct fs *fs;
8024463Smckusic register struct cg *cgp;
80337735Smckusick struct buf *bp;
8044463Smckusic register int i;
80537735Smckusick int error, bno, frags, allocsiz;
8064359Smckusick
8075965Smckusic fs = ip->i_fs;
8085322Smckusic if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
8096294Smckusick return (NULL);
81037735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
81138776Smckusick (int)fs->fs_cgsize, NOCRED, &bp);
81237735Smckusick if (error) {
81337735Smckusick brelse(bp);
81437735Smckusick return (NULL);
81537735Smckusick }
81664508Sbostic cgp = (struct cg *)bp->b_data;
81737735Smckusick if (!cg_chkmagic(cgp) ||
81815950Smckusick (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
8195960Smckusic brelse(bp);
8206294Smckusick return (NULL);
8215960Smckusic }
8228105Sroot cgp->cg_time = time.tv_sec;
8235322Smckusic if (size == fs->fs_bsize) {
82451469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref);
8254463Smckusic bdwrite(bp);
8264463Smckusic return (bno);
8274463Smckusic }
8284463Smckusic /*
8294463Smckusic * check to see if any fragments are already available
8304463Smckusic * allocsiz is the size which will be allocated, hacking
8314463Smckusic * it down to a smaller size if necessary
8324463Smckusic */
8335960Smckusic frags = numfrags(fs, size);
8345322Smckusic for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
8354463Smckusic if (cgp->cg_frsum[allocsiz] != 0)
8364463Smckusic break;
8375322Smckusic if (allocsiz == fs->fs_frag) {
8384463Smckusic /*
8394463Smckusic * no fragments were available, so a block will be
8404463Smckusic * allocated, and hacked up
8414463Smckusic */
8424792Smckusic if (cgp->cg_cs.cs_nbfree == 0) {
8434463Smckusic brelse(bp);
8446294Smckusick return (NULL);
8454463Smckusic }
84651469Sbostic bno = ffs_alloccgblk(fs, cgp, bpref);
8475377Smckusic bpref = dtogd(fs, bno);
8485322Smckusic for (i = frags; i < fs->fs_frag; i++)
84934143Smckusick setbit(cg_blksfree(cgp), bpref + i);
8505322Smckusic i = fs->fs_frag - frags;
8514792Smckusic cgp->cg_cs.cs_nffree += i;
8524792Smckusic fs->fs_cstotal.cs_nffree += i;
8535322Smckusic fs->fs_cs(fs, cg).cs_nffree += i;
85450893Smckusick fs->fs_fmod = 1;
8554463Smckusic cgp->cg_frsum[i]++;
8564463Smckusic bdwrite(bp);
8574463Smckusic return (bno);
8584463Smckusic }
85951469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
86015950Smckusick if (bno < 0) {
86115950Smckusick brelse(bp);
8626294Smckusick return (NULL);
86315950Smckusick }
8644463Smckusic for (i = 0; i < frags; i++)
86534143Smckusick clrbit(cg_blksfree(cgp), bno + i);
8664792Smckusic cgp->cg_cs.cs_nffree -= frags;
8674792Smckusic fs->fs_cstotal.cs_nffree -= frags;
8685322Smckusic fs->fs_cs(fs, cg).cs_nffree -= frags;
86950893Smckusick fs->fs_fmod = 1;
8704463Smckusic cgp->cg_frsum[allocsiz]--;
8714463Smckusic if (frags != allocsiz)
8724463Smckusic cgp->cg_frsum[allocsiz - frags]++;
8734463Smckusic bdwrite(bp);
8744463Smckusic return (cg * fs->fs_fpg + bno);
8754463Smckusic }
8764463Smckusic
8775375Smckusic /*
8785375Smckusic * Allocate a block in a cylinder group.
8795375Smckusic *
8805375Smckusic * This algorithm implements the following policy:
8815375Smckusic * 1) allocate the requested block.
8825375Smckusic * 2) allocate a rotationally optimal block in the same cylinder.
8835375Smckusic * 3) allocate the next available block on the block rotor for the
8845375Smckusic * specified cylinder group.
8855375Smckusic * Note that this routine only allocates fs_bsize blocks; these
8865375Smckusic * blocks may be fragmented by the routine that allocates them.
8875375Smckusic */
88868554Smckusick static ufs_daddr_t
ffs_alloccgblk(fs,cgp,bpref)88951469Sbostic ffs_alloccgblk(fs, cgp, bpref)
8905965Smckusic register struct fs *fs;
8914463Smckusic register struct cg *cgp;
89268554Smckusick ufs_daddr_t bpref;
8934463Smckusic {
89468554Smckusick ufs_daddr_t bno, blkno;
8956294Smckusick int cylno, pos, delta;
8964651Smckusic short *cylbp;
8975361Smckusic register int i;
8984463Smckusic
89965999Smckusick if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
9004651Smckusic bpref = cgp->cg_rotor;
9015361Smckusic goto norot;
9025361Smckusic }
90317224Smckusick bpref = blknum(fs, bpref);
9045377Smckusic bpref = dtogd(fs, bpref);
9055361Smckusic /*
9065361Smckusic * if the requested block is available, use it
9075361Smckusic */
90851469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
9095361Smckusic bno = bpref;
9105361Smckusic goto gotit;
9115361Smckusic }
91269190Smckusick if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
9135375Smckusic /*
91457000Smckusick * Block layout information is not available.
91557000Smckusick * Leaving bpref unchanged means we take the
91657000Smckusick * next available free block following the one
91757000Smckusick * we just allocated. Hopefully this will at
91857000Smckusick * least hit a track cache on drives of unknown
91957000Smckusick * geometry (e.g. SCSI).
9205375Smckusic */
9215375Smckusic goto norot;
9225375Smckusic }
9235375Smckusic /*
92469190Smckusick * check for a block available on the same cylinder
92569190Smckusick */
92669190Smckusick cylno = cbtocylno(fs, bpref);
92769190Smckusick if (cg_blktot(cgp)[cylno] == 0)
92869190Smckusick goto norot;
92969190Smckusick /*
9305361Smckusic * check the summary information to see if a block is
9315361Smckusic * available in the requested cylinder starting at the
9329163Ssam * requested rotational position and proceeding around.
9335361Smckusic */
93434143Smckusick cylbp = cg_blks(fs, cgp, cylno);
9359163Ssam pos = cbtorpos(fs, bpref);
93634143Smckusick for (i = pos; i < fs->fs_nrpos; i++)
9375361Smckusic if (cylbp[i] > 0)
9385361Smckusic break;
93934143Smckusick if (i == fs->fs_nrpos)
9405361Smckusic for (i = 0; i < pos; i++)
9415361Smckusic if (cylbp[i] > 0)
9425361Smckusic break;
9435361Smckusic if (cylbp[i] > 0) {
9444651Smckusic /*
9455361Smckusic * found a rotational position, now find the actual
9465361Smckusic * block. A panic if none is actually there.
9474651Smckusic */
9485361Smckusic pos = cylno % fs->fs_cpc;
9495361Smckusic bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
95034143Smckusick if (fs_postbl(fs, pos)[i] == -1) {
9516716Smckusick printf("pos = %d, i = %d, fs = %s\n",
9526716Smckusick pos, i, fs->fs_fsmnt);
95351469Sbostic panic("ffs_alloccgblk: cyl groups corrupted");
9546716Smckusick }
95534143Smckusick for (i = fs_postbl(fs, pos)[i];; ) {
95651469Sbostic if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
95711638Ssam bno = blkstofrags(fs, (bno + i));
9585361Smckusic goto gotit;
9595361Smckusic }
96034143Smckusick delta = fs_rotbl(fs)[i];
96134143Smckusick if (delta <= 0 ||
96234143Smckusick delta + i > fragstoblks(fs, fs->fs_fpg))
9634651Smckusic break;
9646294Smckusick i += delta;
9654651Smckusic }
9666716Smckusick printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
96751469Sbostic panic("ffs_alloccgblk: can't find blk in cyl");
9684359Smckusick }
9695361Smckusic norot:
9705361Smckusic /*
9715361Smckusic * no blocks in the requested cylinder, so take next
9725361Smckusic * available one in this cylinder group.
9735361Smckusic */
97451469Sbostic bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
9756567Smckusic if (bno < 0)
9766294Smckusick return (NULL);
9774651Smckusic cgp->cg_rotor = bno;
9784359Smckusick gotit:
97965999Smckusick blkno = fragstoblks(fs, bno);
98065999Smckusick ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno);
98165999Smckusick ffs_clusteracct(fs, cgp, blkno, -1);
9824792Smckusic cgp->cg_cs.cs_nbfree--;
9834792Smckusic fs->fs_cstotal.cs_nbfree--;
9845322Smckusic fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
9855375Smckusic cylno = cbtocylno(fs, bno);
98634143Smckusick cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
98734143Smckusick cg_blktot(cgp)[cylno]--;
98850893Smckusick fs->fs_fmod = 1;
9894651Smckusic return (cgp->cg_cgx * fs->fs_fpg + bno);
9904359Smckusick }
99134143Smckusick
9925375Smckusic /*
99365999Smckusick * Determine whether a cluster can be allocated.
99465999Smckusick *
99565999Smckusick * We do not currently check for optimal rotational layout if there
99665999Smckusick * are multiple choices in the same cylinder group. Instead we just
99765999Smckusick * take the first one that we find following bpref.
99865999Smckusick */
99968554Smckusick static ufs_daddr_t
ffs_clusteralloc(ip,cg,bpref,len)100065999Smckusick ffs_clusteralloc(ip, cg, bpref, len)
100165999Smckusick struct inode *ip;
100265999Smckusick int cg;
100368554Smckusick ufs_daddr_t bpref;
100465999Smckusick int len;
100565999Smckusick {
100665999Smckusick register struct fs *fs;
100765999Smckusick register struct cg *cgp;
100865999Smckusick struct buf *bp;
100968336Smckusick int i, got, run, bno, bit, map;
101065999Smckusick u_char *mapp;
101167869Smckusick int32_t *lp;
101265999Smckusick
101365999Smckusick fs = ip->i_fs;
101467869Smckusick if (fs->fs_maxcluster[cg] < len)
101565999Smckusick return (NULL);
101665999Smckusick if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
101765999Smckusick NOCRED, &bp))
101865999Smckusick goto fail;
101965999Smckusick cgp = (struct cg *)bp->b_data;
102065999Smckusick if (!cg_chkmagic(cgp))
102165999Smckusick goto fail;
102265999Smckusick /*
102365999Smckusick * Check to see if a cluster of the needed size (or bigger) is
102465999Smckusick * available in this cylinder group.
102565999Smckusick */
102667869Smckusick lp = &cg_clustersum(cgp)[len];
102765999Smckusick for (i = len; i <= fs->fs_contigsumsize; i++)
102867869Smckusick if (*lp++ > 0)
102965999Smckusick break;
103067869Smckusick if (i > fs->fs_contigsumsize) {
103167869Smckusick /*
103267869Smckusick * This is the first time looking for a cluster in this
103367869Smckusick * cylinder group. Update the cluster summary information
103467869Smckusick * to reflect the true maximum sized cluster so that
103567869Smckusick * future cluster allocation requests can avoid reading
103667869Smckusick * the cylinder group map only to find no clusters.
103767869Smckusick */
103867869Smckusick lp = &cg_clustersum(cgp)[len - 1];
103967869Smckusick for (i = len - 1; i > 0; i--)
104067869Smckusick if (*lp-- > 0)
104167869Smckusick break;
104267869Smckusick fs->fs_maxcluster[cg] = i;
104365999Smckusick goto fail;
104467869Smckusick }
104565999Smckusick /*
104665999Smckusick * Search the cluster map to find a big enough cluster.
104765999Smckusick * We take the first one that we find, even if it is larger
104865999Smckusick * than we need as we prefer to get one close to the previous
104965999Smckusick * block allocation. We do not search before the current
105065999Smckusick * preference point as we do not want to allocate a block
105165999Smckusick * that is allocated before the previous one (as we will
105265999Smckusick * then have to wait for another pass of the elevator
105365999Smckusick * algorithm before it will be read). We prefer to fail and
105465999Smckusick * be recalled to try an allocation in the next cylinder group.
105565999Smckusick */
105665999Smckusick if (dtog(fs, bpref) != cg)
105765999Smckusick bpref = 0;
105865999Smckusick else
105965999Smckusick bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
106065999Smckusick mapp = &cg_clustersfree(cgp)[bpref / NBBY];
106165999Smckusick map = *mapp++;
106265999Smckusick bit = 1 << (bpref % NBBY);
106368336Smckusick for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
106465999Smckusick if ((map & bit) == 0) {
106565999Smckusick run = 0;
106665999Smckusick } else {
106765999Smckusick run++;
106865999Smckusick if (run == len)
106965999Smckusick break;
107065999Smckusick }
107168336Smckusick if ((got & (NBBY - 1)) != (NBBY - 1)) {
107265999Smckusick bit <<= 1;
107365999Smckusick } else {
107465999Smckusick map = *mapp++;
107565999Smckusick bit = 1;
107665999Smckusick }
107765999Smckusick }
107868336Smckusick if (got == cgp->cg_nclusterblks)
107965999Smckusick goto fail;
108065999Smckusick /*
108165999Smckusick * Allocate the cluster that we have found.
108265999Smckusick */
108368336Smckusick for (i = 1; i <= len; i++)
108468336Smckusick if (!ffs_isblock(fs, cg_blksfree(cgp), got - run + i))
108568336Smckusick panic("ffs_clusteralloc: map mismatch");
108668336Smckusick bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1087*69705Smckusick if (dtog(fs, bno) != cg)
1088*69705Smckusick panic("ffs_clusteralloc: allocated out of group");
108965999Smckusick len = blkstofrags(fs, len);
109065999Smckusick for (i = 0; i < len; i += fs->fs_frag)
109168336Smckusick if ((got = ffs_alloccgblk(fs, cgp, bno + i)) != bno + i)
109265999Smckusick panic("ffs_clusteralloc: lost block");
109365999Smckusick brelse(bp);
109465999Smckusick return (bno);
109565999Smckusick
109665999Smckusick fail:
109765999Smckusick brelse(bp);
109865999Smckusick return (0);
109965999Smckusick }
110065999Smckusick
110165999Smckusick /*
11025375Smckusic * Determine whether an inode can be allocated.
11035375Smckusic *
11045375Smckusic * Check to see if an inode is available, and if it is,
11055375Smckusic * allocate it using the following policy:
11065375Smckusic * 1) allocate the requested inode.
11075375Smckusic * 2) allocate the next available inode after the requested
11085375Smckusic * inode in the specified cylinder group.
11095375Smckusic */
111068122Smckusick static ino_t
ffs_nodealloccg(ip,cg,ipref,mode)111164603Sbostic ffs_nodealloccg(ip, cg, ipref, mode)
11125965Smckusic struct inode *ip;
11134359Smckusick int cg;
111468554Smckusick ufs_daddr_t ipref;
11154359Smckusick int mode;
11164359Smckusick {
11175965Smckusic register struct fs *fs;
11184463Smckusic register struct cg *cgp;
111916784Smckusick struct buf *bp;
112037735Smckusick int error, start, len, loc, map, i;
11214359Smckusick
11225965Smckusic fs = ip->i_fs;
11235322Smckusic if (fs->fs_cs(fs, cg).cs_nifree == 0)
11246294Smckusick return (NULL);
112537735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
112638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp);
112737735Smckusick if (error) {
112837735Smckusick brelse(bp);
112937735Smckusick return (NULL);
113037735Smckusick }
113164508Sbostic cgp = (struct cg *)bp->b_data;
113237735Smckusick if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
11335960Smckusic brelse(bp);
11346294Smckusick return (NULL);
11355960Smckusic }
11368105Sroot cgp->cg_time = time.tv_sec;
11374359Smckusick if (ipref) {
11384359Smckusick ipref %= fs->fs_ipg;
113934143Smckusick if (isclr(cg_inosused(cgp), ipref))
11404359Smckusick goto gotit;
114116784Smckusick }
114216784Smckusick start = cgp->cg_irotor / NBBY;
114316784Smckusick len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
114434143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
114516784Smckusick if (loc == 0) {
114617697Smckusick len = start + 1;
114717697Smckusick start = 0;
114834143Smckusick loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
114917697Smckusick if (loc == 0) {
115065718Sbostic printf("cg = %d, irotor = %d, fs = %s\n",
115117697Smckusick cg, cgp->cg_irotor, fs->fs_fsmnt);
115264603Sbostic panic("ffs_nodealloccg: map corrupted");
115317697Smckusick /* NOTREACHED */
115417697Smckusick }
115516784Smckusick }
115616784Smckusick i = start + len - loc;
115734143Smckusick map = cg_inosused(cgp)[i];
115816784Smckusick ipref = i * NBBY;
115916784Smckusick for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
116016784Smckusick if ((map & i) == 0) {
11614359Smckusick cgp->cg_irotor = ipref;
11624359Smckusick goto gotit;
11634359Smckusick }
11644359Smckusick }
116516784Smckusick printf("fs = %s\n", fs->fs_fsmnt);
116664603Sbostic panic("ffs_nodealloccg: block not in map");
116716784Smckusick /* NOTREACHED */
11684359Smckusick gotit:
116934143Smckusick setbit(cg_inosused(cgp), ipref);
11704792Smckusic cgp->cg_cs.cs_nifree--;
11714792Smckusic fs->fs_cstotal.cs_nifree--;
11725322Smckusic fs->fs_cs(fs, cg).cs_nifree--;
117350893Smckusick fs->fs_fmod = 1;
11744359Smckusick if ((mode & IFMT) == IFDIR) {
11754792Smckusic cgp->cg_cs.cs_ndir++;
11764792Smckusic fs->fs_cstotal.cs_ndir++;
11775322Smckusic fs->fs_cs(fs, cg).cs_ndir++;
11784359Smckusick }
11794359Smckusick bdwrite(bp);
11804359Smckusick return (cg * fs->fs_ipg + ipref);
11814359Smckusick }
11824359Smckusick
11835375Smckusic /*
11845375Smckusic * Free a block or fragment.
11855375Smckusic *
11865375Smckusic * The specified block or fragment is placed back in the
11875375Smckusic * free map. If a fragment is deallocated, a possible
11885375Smckusic * block reassembly is checked.
11895375Smckusic */
ffs_blkfree(ip,bno,size)119051469Sbostic ffs_blkfree(ip, bno, size)
11915965Smckusic register struct inode *ip;
119268554Smckusick ufs_daddr_t bno;
119353059Smckusick long size;
11944359Smckusick {
11954359Smckusick register struct fs *fs;
11964359Smckusick register struct cg *cgp;
119737735Smckusick struct buf *bp;
119868554Smckusick ufs_daddr_t blkno;
119965999Smckusick int i, error, cg, blk, frags, bbase;
12004359Smckusick
12015965Smckusic fs = ip->i_fs;
120264508Sbostic if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
12036716Smckusick printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
12046716Smckusick ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
120531402Smckusick panic("blkfree: bad size");
12066716Smckusick }
12075377Smckusic cg = dtog(fs, bno);
120864508Sbostic if ((u_int)bno >= fs->fs_size) {
12096567Smckusic printf("bad block %d, ino %d\n", bno, ip->i_number);
121053244Smckusick ffs_fserr(fs, ip->i_uid, "bad block");
12114359Smckusick return;
12126567Smckusic }
121337735Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
121438776Smckusick (int)fs->fs_cgsize, NOCRED, &bp);
121537735Smckusick if (error) {
121637735Smckusick brelse(bp);
121737735Smckusick return;
121837735Smckusick }
121964508Sbostic cgp = (struct cg *)bp->b_data;
122037735Smckusick if (!cg_chkmagic(cgp)) {
12215960Smckusic brelse(bp);
12224359Smckusick return;
12235960Smckusic }
12248105Sroot cgp->cg_time = time.tv_sec;
12255377Smckusic bno = dtogd(fs, bno);
12265322Smckusic if (size == fs->fs_bsize) {
122765999Smckusick blkno = fragstoblks(fs, bno);
122865999Smckusick if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
12296716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n",
12306716Smckusick ip->i_dev, bno, fs->fs_fsmnt);
123131402Smckusick panic("blkfree: freeing free block");
12326567Smckusic }
123365999Smckusick ffs_setblock(fs, cg_blksfree(cgp), blkno);
123465999Smckusick ffs_clusteracct(fs, cgp, blkno, 1);
12354792Smckusic cgp->cg_cs.cs_nbfree++;
12364792Smckusic fs->fs_cstotal.cs_nbfree++;
12375322Smckusic fs->fs_cs(fs, cg).cs_nbfree++;
12385375Smckusic i = cbtocylno(fs, bno);
123934143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
124034143Smckusick cg_blktot(cgp)[i]++;
12414426Smckusic } else {
124217224Smckusick bbase = bno - fragnum(fs, bno);
12434463Smckusic /*
12444463Smckusic * decrement the counts associated with the old frags
12454463Smckusic */
124634143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase);
124751469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
12484463Smckusic /*
12494463Smckusic * deallocate the fragment
12504463Smckusic */
12515960Smckusic frags = numfrags(fs, size);
12524463Smckusic for (i = 0; i < frags; i++) {
125334143Smckusick if (isset(cg_blksfree(cgp), bno + i)) {
12546716Smckusick printf("dev = 0x%x, block = %d, fs = %s\n",
12556716Smckusick ip->i_dev, bno + i, fs->fs_fsmnt);
125631402Smckusick panic("blkfree: freeing free frag");
12576716Smckusick }
125834143Smckusick setbit(cg_blksfree(cgp), bno + i);
12594426Smckusic }
12606294Smckusick cgp->cg_cs.cs_nffree += i;
12616294Smckusick fs->fs_cstotal.cs_nffree += i;
12626294Smckusick fs->fs_cs(fs, cg).cs_nffree += i;
12634463Smckusic /*
12644463Smckusic * add back in counts associated with the new frags
12654463Smckusic */
126634143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bbase);
126751469Sbostic ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
12684463Smckusic /*
12694463Smckusic * if a complete block has been reassembled, account for it
12704463Smckusic */
127165999Smckusick blkno = fragstoblks(fs, bbase);
127265999Smckusick if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
12735322Smckusic cgp->cg_cs.cs_nffree -= fs->fs_frag;
12745322Smckusic fs->fs_cstotal.cs_nffree -= fs->fs_frag;
12755322Smckusic fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
127665999Smckusick ffs_clusteracct(fs, cgp, blkno, 1);
12774792Smckusic cgp->cg_cs.cs_nbfree++;
12784792Smckusic fs->fs_cstotal.cs_nbfree++;
12795322Smckusic fs->fs_cs(fs, cg).cs_nbfree++;
12805375Smckusic i = cbtocylno(fs, bbase);
128134143Smckusick cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
128234143Smckusick cg_blktot(cgp)[i]++;
12834426Smckusic }
12844426Smckusic }
128550893Smckusick fs->fs_fmod = 1;
12864359Smckusick bdwrite(bp);
12874359Smckusick }
12884359Smckusick
1289*69705Smckusick #ifdef DIAGNOSTIC
12905375Smckusic /*
1291*69705Smckusick * Verify allocation of a block or fragment. Returns true if block or
1292*69705Smckusick * fragment is allocated, false if it is free.
1293*69705Smckusick */
1294*69705Smckusick ffs_checkblk(ip, bno, size)
1295*69705Smckusick struct inode *ip;
1296*69705Smckusick ufs_daddr_t bno;
1297*69705Smckusick long size;
1298*69705Smckusick {
1299*69705Smckusick struct fs *fs;
1300*69705Smckusick struct cg *cgp;
1301*69705Smckusick struct buf *bp;
1302*69705Smckusick int i, error, frags, free;
1303*69705Smckusick
1304*69705Smckusick fs = ip->i_fs;
1305*69705Smckusick if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1306*69705Smckusick printf("bsize = %d, size = %d, fs = %s\n",
1307*69705Smckusick fs->fs_bsize, size, fs->fs_fsmnt);
1308*69705Smckusick panic("checkblk: bad size");
1309*69705Smckusick }
1310*69705Smckusick if ((u_int)bno >= fs->fs_size)
1311*69705Smckusick panic("checkblk: bad block %d", bno);
1312*69705Smckusick error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1313*69705Smckusick (int)fs->fs_cgsize, NOCRED, &bp);
1314*69705Smckusick if (error) {
1315*69705Smckusick brelse(bp);
1316*69705Smckusick return;
1317*69705Smckusick }
1318*69705Smckusick cgp = (struct cg *)bp->b_data;
1319*69705Smckusick if (!cg_chkmagic(cgp)) {
1320*69705Smckusick brelse(bp);
1321*69705Smckusick return;
1322*69705Smckusick }
1323*69705Smckusick bno = dtogd(fs, bno);
1324*69705Smckusick if (size == fs->fs_bsize) {
1325*69705Smckusick free = ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
1326*69705Smckusick } else {
1327*69705Smckusick frags = numfrags(fs, size);
1328*69705Smckusick for (free = 0, i = 0; i < frags; i++)
1329*69705Smckusick if (isset(cg_blksfree(cgp), bno + i))
1330*69705Smckusick free++;
1331*69705Smckusick if (free != 0 && free != frags)
1332*69705Smckusick panic("checkblk: partially free fragment");
1333*69705Smckusick }
1334*69705Smckusick brelse(bp);
1335*69705Smckusick return (!free);
1336*69705Smckusick }
1337*69705Smckusick #endif /* DIAGNOSTIC */
1338*69705Smckusick
1339*69705Smckusick /*
13405375Smckusic * Free an inode.
13415375Smckusic *
13425375Smckusic * The specified inode is placed back in the free map.
13435375Smckusic */
134453577Sheideman int
ffs_vfree(ap)134554653Smckusick ffs_vfree(ap)
134654653Smckusick struct vop_vfree_args /* {
134754653Smckusick struct vnode *a_pvp;
134854653Smckusick ino_t a_ino;
134954653Smckusick int a_mode;
135054653Smckusick } */ *ap;
13514359Smckusick {
13524359Smckusick register struct fs *fs;
13534359Smckusick register struct cg *cgp;
135451541Smckusick register struct inode *pip;
135554653Smckusick ino_t ino = ap->a_ino;
135637735Smckusick struct buf *bp;
135737735Smckusick int error, cg;
13584359Smckusick
135953583Sheideman pip = VTOI(ap->a_pvp);
136051469Sbostic fs = pip->i_fs;
136154653Smckusick if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
136254653Smckusick panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
136354653Smckusick pip->i_dev, ino, fs->fs_fsmnt);
136464603Sbostic cg = ino_to_cg(fs, ino);
136551469Sbostic error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
136638776Smckusick (int)fs->fs_cgsize, NOCRED, &bp);
136737735Smckusick if (error) {
136837735Smckusick brelse(bp);
136953577Sheideman return (0);
137037735Smckusick }
137164508Sbostic cgp = (struct cg *)bp->b_data;
137237735Smckusick if (!cg_chkmagic(cgp)) {
13735960Smckusic brelse(bp);
137453577Sheideman return (0);
13755960Smckusic }
13768105Sroot cgp->cg_time = time.tv_sec;
137754653Smckusick ino %= fs->fs_ipg;
137854653Smckusick if (isclr(cg_inosused(cgp), ino)) {
137954653Smckusick printf("dev = 0x%x, ino = %d, fs = %s\n",
138054653Smckusick pip->i_dev, ino, fs->fs_fsmnt);
138148057Smckusick if (fs->fs_ronly == 0)
138248057Smckusick panic("ifree: freeing free inode");
13836716Smckusick }
138454653Smckusick clrbit(cg_inosused(cgp), ino);
138554653Smckusick if (ino < cgp->cg_irotor)
138654653Smckusick cgp->cg_irotor = ino;
13874792Smckusic cgp->cg_cs.cs_nifree++;
13884792Smckusic fs->fs_cstotal.cs_nifree++;
13895322Smckusic fs->fs_cs(fs, cg).cs_nifree++;
139053583Sheideman if ((ap->a_mode & IFMT) == IFDIR) {
13914792Smckusic cgp->cg_cs.cs_ndir--;
13924792Smckusic fs->fs_cstotal.cs_ndir--;
13935322Smckusic fs->fs_cs(fs, cg).cs_ndir--;
13944359Smckusick }
139550893Smckusick fs->fs_fmod = 1;
13964359Smckusick bdwrite(bp);
139753577Sheideman return (0);
13984359Smckusick }
13994359Smckusick
14004463Smckusic /*
14015375Smckusic * Find a block of the specified size in the specified cylinder group.
14025375Smckusic *
14034651Smckusic * It is a panic if a request is made to find a block if none are
14044651Smckusic * available.
14054651Smckusic */
140668554Smckusick static ufs_daddr_t
ffs_mapsearch(fs,cgp,bpref,allocsiz)140751469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz)
14084651Smckusic register struct fs *fs;
14094651Smckusic register struct cg *cgp;
141068554Smckusick ufs_daddr_t bpref;
14114651Smckusic int allocsiz;
14124651Smckusic {
141368554Smckusick ufs_daddr_t bno;
14144651Smckusic int start, len, loc, i;
14154651Smckusic int blk, field, subfield, pos;
14164651Smckusic
14174651Smckusic /*
14184651Smckusic * find the fragment by searching through the free block
14194651Smckusic * map for an appropriate bit pattern
14204651Smckusic */
14214651Smckusic if (bpref)
14225377Smckusic start = dtogd(fs, bpref) / NBBY;
14234651Smckusic else
14244651Smckusic start = cgp->cg_frotor / NBBY;
14255398Smckusic len = howmany(fs->fs_fpg, NBBY) - start;
142664508Sbostic loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start],
142734476Smckusick (u_char *)fragtbl[fs->fs_frag],
142834476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
14294651Smckusic if (loc == 0) {
14306531Smckusick len = start + 1;
14316531Smckusick start = 0;
143264508Sbostic loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0],
143334476Smckusick (u_char *)fragtbl[fs->fs_frag],
143434476Smckusick (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
143516784Smckusick if (loc == 0) {
143616784Smckusick printf("start = %d, len = %d, fs = %s\n",
143716784Smckusick start, len, fs->fs_fsmnt);
143851469Sbostic panic("ffs_alloccg: map corrupted");
143917697Smckusick /* NOTREACHED */
144016784Smckusick }
14414651Smckusic }
14424651Smckusic bno = (start + len - loc) * NBBY;
14434651Smckusic cgp->cg_frotor = bno;
14444651Smckusic /*
14454651Smckusic * found the byte in the map
14464651Smckusic * sift through the bits to find the selected frag
14474651Smckusic */
14486294Smckusick for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
144934143Smckusick blk = blkmap(fs, cg_blksfree(cgp), bno);
14504651Smckusic blk <<= 1;
14514651Smckusic field = around[allocsiz];
14524651Smckusic subfield = inside[allocsiz];
14535322Smckusic for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
14546294Smckusick if ((blk & field) == subfield)
14556294Smckusick return (bno + pos);
14564651Smckusic field <<= 1;
14574651Smckusic subfield <<= 1;
14584651Smckusic }
14594651Smckusic }
14606716Smckusick printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
146151469Sbostic panic("ffs_alloccg: block not in map");
14626531Smckusick return (-1);
14634651Smckusic }
14644651Smckusic
14654651Smckusic /*
146665999Smckusick * Update the cluster map because of an allocation or free.
146765999Smckusick *
146865999Smckusick * Cnt == 1 means free; cnt == -1 means allocating.
146965999Smckusick */
147065999Smckusick ffs_clusteracct(fs, cgp, blkno, cnt)
147165999Smckusick struct fs *fs;
147265999Smckusick struct cg *cgp;
147368554Smckusick ufs_daddr_t blkno;
147465999Smckusick int cnt;
147565999Smckusick {
147668122Smckusick int32_t *sump;
147767869Smckusick int32_t *lp;
147865999Smckusick u_char *freemapp, *mapp;
147965999Smckusick int i, start, end, forw, back, map, bit;
148065999Smckusick
148165999Smckusick if (fs->fs_contigsumsize <= 0)
148265999Smckusick return;
148365999Smckusick freemapp = cg_clustersfree(cgp);
148465999Smckusick sump = cg_clustersum(cgp);
148565999Smckusick /*
148665999Smckusick * Allocate or clear the actual block.
148765999Smckusick */
148865999Smckusick if (cnt > 0)
148965999Smckusick setbit(freemapp, blkno);
149065999Smckusick else
149165999Smckusick clrbit(freemapp, blkno);
149265999Smckusick /*
149365999Smckusick * Find the size of the cluster going forward.
149465999Smckusick */
149565999Smckusick start = blkno + 1;
149665999Smckusick end = start + fs->fs_contigsumsize;
149765999Smckusick if (end >= cgp->cg_nclusterblks)
149865999Smckusick end = cgp->cg_nclusterblks;
149965999Smckusick mapp = &freemapp[start / NBBY];
150065999Smckusick map = *mapp++;
150165999Smckusick bit = 1 << (start % NBBY);
150265999Smckusick for (i = start; i < end; i++) {
150365999Smckusick if ((map & bit) == 0)
150465999Smckusick break;
150565999Smckusick if ((i & (NBBY - 1)) != (NBBY - 1)) {
150665999Smckusick bit <<= 1;
150765999Smckusick } else {
150865999Smckusick map = *mapp++;
150965999Smckusick bit = 1;
151065999Smckusick }
151165999Smckusick }
151265999Smckusick forw = i - start;
151365999Smckusick /*
151465999Smckusick * Find the size of the cluster going backward.
151565999Smckusick */
151665999Smckusick start = blkno - 1;
151765999Smckusick end = start - fs->fs_contigsumsize;
151865999Smckusick if (end < 0)
151965999Smckusick end = -1;
152065999Smckusick mapp = &freemapp[start / NBBY];
152165999Smckusick map = *mapp--;
152265999Smckusick bit = 1 << (start % NBBY);
152365999Smckusick for (i = start; i > end; i--) {
152465999Smckusick if ((map & bit) == 0)
152565999Smckusick break;
152665999Smckusick if ((i & (NBBY - 1)) != 0) {
152765999Smckusick bit >>= 1;
152865999Smckusick } else {
152965999Smckusick map = *mapp--;
153065999Smckusick bit = 1 << (NBBY - 1);
153165999Smckusick }
153265999Smckusick }
153365999Smckusick back = start - i;
153465999Smckusick /*
153565999Smckusick * Account for old cluster and the possibly new forward and
153665999Smckusick * back clusters.
153765999Smckusick */
153865999Smckusick i = back + forw + 1;
153965999Smckusick if (i > fs->fs_contigsumsize)
154065999Smckusick i = fs->fs_contigsumsize;
154165999Smckusick sump[i] += cnt;
154265999Smckusick if (back > 0)
154365999Smckusick sump[back] -= cnt;
154465999Smckusick if (forw > 0)
154565999Smckusick sump[forw] -= cnt;
154667869Smckusick /*
154767869Smckusick * Update cluster summary information.
154867869Smckusick */
154967869Smckusick lp = &sump[fs->fs_contigsumsize];
155067869Smckusick for (i = fs->fs_contigsumsize; i > 0; i--)
155167869Smckusick if (*lp-- > 0)
155267869Smckusick break;
155367869Smckusick fs->fs_maxcluster[cgp->cg_cgx] = i;
155465999Smckusick }
155565999Smckusick
155665999Smckusick /*
15575375Smckusic * Fserr prints the name of a file system with an error diagnostic.
15585375Smckusic *
15595375Smckusic * The form of the error message is:
15604359Smckusick * fs: error message
15614359Smckusick */
156251469Sbostic static void
ffs_fserr(fs,uid,cp)156351469Sbostic ffs_fserr(fs, uid, cp)
15644359Smckusick struct fs *fs;
156551469Sbostic u_int uid;
15664359Smckusick char *cp;
15674359Smckusick {
15684359Smckusick
156942318Smckusick log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
15704359Smckusick }
1571