xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 6716)
1*6716Smckusick /*	lfs_alloc.c	2.5	82/05/07	*/
24359Smckusick 
34359Smckusick #include "../h/param.h"
44359Smckusick #include "../h/systm.h"
54359Smckusick #include "../h/mount.h"
64359Smckusick #include "../h/fs.h"
74359Smckusick #include "../h/conf.h"
84359Smckusick #include "../h/buf.h"
94359Smckusick #include "../h/inode.h"
106567Smckusic #include "../h/dir.h"
114359Smckusick #include "../h/user.h"
124359Smckusick 
135212Smckusic extern u_long		hashalloc();
145212Smckusic extern ino_t		ialloccg();
154651Smckusic extern daddr_t		alloccg();
164651Smckusic extern daddr_t		alloccgblk();
174651Smckusic extern daddr_t		fragextend();
184651Smckusic extern daddr_t		blkpref();
194651Smckusic extern daddr_t		mapsearch();
204607Smckusic extern int		inside[], around[];
215322Smckusic extern unsigned char	*fragtbl[];
224359Smckusick 
235375Smckusic /*
245375Smckusic  * Allocate a block in the file system.
255375Smckusic  *
265375Smckusic  * The size of the requested block is given, which must be some
275375Smckusic  * multiple of fs_fsize and <= fs_bsize.
285375Smckusic  * A preference may be optionally specified. If a preference is given
295375Smckusic  * the following hierarchy is used to allocate a block:
305375Smckusic  *   1) allocate the requested block.
315375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
325375Smckusic  *   3) allocate a block in the same cylinder group.
335375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
345375Smckusic  *      available block is located.
355375Smckusic  * If no block preference is given the following heirarchy is used
365375Smckusic  * to allocate a block:
375375Smckusic  *   1) allocate a block in the cylinder group that contains the
385375Smckusic  *      inode for the file.
395375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
405375Smckusic  *      available block is located.
415375Smckusic  */
424359Smckusick struct buf *
435965Smckusic alloc(ip, bpref, size)
444463Smckusic 	register struct inode *ip;
454359Smckusick 	daddr_t bpref;
464359Smckusick 	int size;
474359Smckusick {
484359Smckusick 	daddr_t bno;
494359Smckusick 	register struct fs *fs;
504463Smckusic 	register struct buf *bp;
514359Smckusick 	int cg;
524359Smckusick 
535965Smckusic 	fs = ip->i_fs;
54*6716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
55*6716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
56*6716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
574463Smckusic 		panic("alloc: bad size");
58*6716Smckusick 	}
595322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
604359Smckusick 		goto nospace;
614792Smckusic 	if (u.u_uid != 0 &&
625322Smckusic 	    fs->fs_cstotal.cs_nbfree * fs->fs_frag + fs->fs_cstotal.cs_nffree <
635322Smckusic 	      fs->fs_dsize * fs->fs_minfree / 100)
644792Smckusic 		goto nospace;
654948Smckusic 	if (bpref >= fs->fs_size)
664948Smckusic 		bpref = 0;
674359Smckusick 	if (bpref == 0)
685377Smckusic 		cg = itog(fs, ip->i_number);
694359Smckusick 	else
705377Smckusic 		cg = dtog(fs, bpref);
715965Smckusic 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, alloccg);
726567Smckusic 	if (bno <= 0)
734359Smckusick 		goto nospace;
745965Smckusic 	bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
754359Smckusick 	clrbuf(bp);
764359Smckusick 	return (bp);
774359Smckusick nospace:
784359Smckusick 	fserr(fs, "file system full");
794359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
804359Smckusick 	u.u_error = ENOSPC;
814359Smckusick 	return (NULL);
824359Smckusick }
834359Smckusick 
845375Smckusic /*
855375Smckusic  * Reallocate a fragment to a bigger size
865375Smckusic  *
875375Smckusic  * The number and size of the old block is given, and a preference
885375Smckusic  * and new size is also specified. The allocator attempts to extend
895375Smckusic  * the original block. Failing that, the regular block allocator is
905375Smckusic  * invoked to get an appropriate block.
915375Smckusic  */
924426Smckusic struct buf *
935965Smckusic realloccg(ip, bprev, bpref, osize, nsize)
945965Smckusic 	register struct inode *ip;
954651Smckusic 	daddr_t bprev, bpref;
964426Smckusic 	int osize, nsize;
974426Smckusic {
984426Smckusic 	daddr_t bno;
994426Smckusic 	register struct fs *fs;
1004463Smckusic 	register struct buf *bp, *obp;
1014426Smckusic 	int cg;
1024426Smckusic 
1035965Smckusic 	fs = ip->i_fs;
1045960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
105*6716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
106*6716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
107*6716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1084463Smckusic 		panic("realloccg: bad size");
109*6716Smckusick 	}
1104792Smckusic 	if (u.u_uid != 0 &&
1115322Smckusic 	    fs->fs_cstotal.cs_nbfree * fs->fs_frag + fs->fs_cstotal.cs_nffree <
1125322Smckusic 	      fs->fs_dsize * fs->fs_minfree / 100)
1134792Smckusic 		goto nospace;
114*6716Smckusick 	if (bprev == 0) {
115*6716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
116*6716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1174463Smckusic 		panic("realloccg: bad bprev");
118*6716Smckusick 	}
1196294Smckusick 	cg = dtog(fs, bprev);
1205965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1214463Smckusic 	if (bno != 0) {
1225965Smckusic 		bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
1235960Smckusic 		if (bp->b_flags & B_ERROR) {
1245960Smckusic 			brelse(bp);
1256294Smckusick 			return (NULL);
1265960Smckusic 		}
1276567Smckusic 		brealloc(bp, nsize);
1284463Smckusic 		blkclr(bp->b_un.b_addr + osize, nsize - osize);
1294463Smckusic 		return (bp);
1304463Smckusic 	}
1314948Smckusic 	if (bpref >= fs->fs_size)
1324948Smckusic 		bpref = 0;
1335965Smckusic 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize, alloccg);
1346567Smckusic 	if (bno > 0) {
1355965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
1365960Smckusic 		if (obp->b_flags & B_ERROR) {
1375960Smckusic 			brelse(obp);
1386294Smckusick 			return (NULL);
1395960Smckusic 		}
1405965Smckusic 		bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize);
1416567Smckusic 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, osize);
1426567Smckusic 		blkclr(bp->b_un.b_addr + osize, nsize - osize);
1434463Smckusic 		brelse(obp);
1445965Smckusic 		fre(ip, bprev, (off_t)osize);
1456294Smckusick 		return (bp);
1464463Smckusic 	}
1474792Smckusic nospace:
1484463Smckusic 	/*
1494463Smckusic 	 * no space available
1504463Smckusic 	 */
1514426Smckusic 	fserr(fs, "file system full");
1524426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
1534426Smckusic 	u.u_error = ENOSPC;
1544426Smckusic 	return (NULL);
1554426Smckusic }
1564426Smckusic 
1575375Smckusic /*
1585375Smckusic  * Allocate an inode in the file system.
1595375Smckusic  *
1605375Smckusic  * A preference may be optionally specified. If a preference is given
1615375Smckusic  * the following hierarchy is used to allocate an inode:
1625375Smckusic  *   1) allocate the requested inode.
1635375Smckusic  *   2) allocate an inode in the same cylinder group.
1645375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
1655375Smckusic  *      available inode is located.
1665375Smckusic  * If no inode preference is given the following heirarchy is used
1675375Smckusic  * to allocate an inode:
1685375Smckusic  *   1) allocate an inode in cylinder group 0.
1695375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
1705375Smckusic  *      available inode is located.
1715375Smckusic  */
1724359Smckusick struct inode *
1735965Smckusic ialloc(pip, ipref, mode)
1745965Smckusic 	register struct inode *pip;
1754359Smckusick 	ino_t ipref;
1764359Smckusick 	int mode;
1774359Smckusick {
1785212Smckusic 	ino_t ino;
1794359Smckusick 	register struct fs *fs;
1804359Smckusick 	register struct inode *ip;
1814359Smckusick 	int cg;
1824359Smckusick 
1835965Smckusic 	fs = pip->i_fs;
1844792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
1854359Smckusick 		goto noinodes;
1864948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
1874948Smckusic 		ipref = 0;
1885377Smckusic 	cg = itog(fs, ipref);
1895965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
1904359Smckusick 	if (ino == 0)
1914359Smckusick 		goto noinodes;
1925965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
1934359Smckusick 	if (ip == NULL) {
1945965Smckusic 		ifree(ip, ino, 0);
1954359Smckusick 		return (NULL);
1964359Smckusick 	}
197*6716Smckusick 	if (ip->i_mode) {
198*6716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
199*6716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2004359Smckusick 		panic("ialloc: dup alloc");
201*6716Smckusick 	}
2024359Smckusick 	return (ip);
2034359Smckusick noinodes:
2044359Smckusick 	fserr(fs, "out of inodes");
2056294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
2064359Smckusick 	u.u_error = ENOSPC;
2074359Smckusick 	return (NULL);
2084359Smckusick }
2094359Smckusick 
2104651Smckusic /*
2115375Smckusic  * Find a cylinder to place a directory.
2125375Smckusic  *
2135375Smckusic  * The policy implemented by this algorithm is to select from
2145375Smckusic  * among those cylinder groups with above the average number of
2155375Smckusic  * free inodes, the one with the smallest number of directories.
2164651Smckusic  */
2175965Smckusic dirpref(fs)
2185965Smckusic 	register struct fs *fs;
2194359Smckusick {
2204651Smckusic 	int cg, minndir, mincg, avgifree;
2214359Smckusick 
2224792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
2234651Smckusic 	minndir = fs->fs_ipg;
2244359Smckusick 	mincg = 0;
2254651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
2265322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
2275322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
2284359Smckusick 			mincg = cg;
2295322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
2304359Smckusick 		}
2314359Smckusick 	return (fs->fs_ipg * mincg);
2324359Smckusick }
2334359Smckusick 
2344651Smckusic /*
2355375Smckusic  * Select a cylinder to place a large block of data.
2365375Smckusic  *
2375375Smckusic  * The policy implemented by this algorithm is to maintain a
2385375Smckusic  * rotor that sweeps the cylinder groups. When a block is
2395375Smckusic  * needed, the rotor is advanced until a cylinder group with
2405375Smckusic  * greater than the average number of free blocks is found.
2414651Smckusic  */
2425212Smckusic daddr_t
2435965Smckusic blkpref(fs)
2445965Smckusic 	register struct fs *fs;
2454651Smckusic {
2464651Smckusic 	int cg, avgbfree;
2474651Smckusic 
2484792Smckusic 	avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
2494651Smckusic 	for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++)
2505322Smckusic 		if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2514651Smckusic 			fs->fs_cgrotor = cg;
2525322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
2534651Smckusic 		}
2544651Smckusic 	for (cg = 0; cg <= fs->fs_cgrotor; cg++)
2555322Smckusic 		if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2564651Smckusic 			fs->fs_cgrotor = cg;
2575322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
2584651Smckusic 		}
2596294Smckusick 	return (NULL);
2604651Smckusic }
2614651Smckusic 
2625375Smckusic /*
2635375Smckusic  * Implement the cylinder overflow algorithm.
2645375Smckusic  *
2655375Smckusic  * The policy implemented by this algorithm is:
2665375Smckusic  *   1) allocate the block in its requested cylinder group.
2675375Smckusic  *   2) quadradically rehash on the cylinder group number.
2685375Smckusic  *   3) brute force search for a free block.
2695375Smckusic  */
2705212Smckusic /*VARARGS5*/
2715212Smckusic u_long
2725965Smckusic hashalloc(ip, cg, pref, size, allocator)
2735965Smckusic 	struct inode *ip;
2744359Smckusick 	int cg;
2754359Smckusick 	long pref;
2764359Smckusick 	int size;	/* size for data blocks, mode for inodes */
2775212Smckusic 	u_long (*allocator)();
2784359Smckusick {
2795965Smckusic 	register struct fs *fs;
2804359Smckusick 	long result;
2814359Smckusick 	int i, icg = cg;
2824359Smckusick 
2835965Smckusic 	fs = ip->i_fs;
2844359Smckusick 	/*
2854359Smckusick 	 * 1: preferred cylinder group
2864359Smckusick 	 */
2875965Smckusic 	result = (*allocator)(ip, cg, pref, size);
2884359Smckusick 	if (result)
2894359Smckusick 		return (result);
2904359Smckusick 	/*
2914359Smckusick 	 * 2: quadratic rehash
2924359Smckusick 	 */
2934359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
2944359Smckusick 		cg += i;
2954359Smckusick 		if (cg >= fs->fs_ncg)
2964359Smckusick 			cg -= fs->fs_ncg;
2975965Smckusic 		result = (*allocator)(ip, cg, 0, size);
2984359Smckusick 		if (result)
2994359Smckusick 			return (result);
3004359Smckusick 	}
3014359Smckusick 	/*
3024359Smckusick 	 * 3: brute force search
3034359Smckusick 	 */
3044359Smckusick 	cg = icg;
3054359Smckusick 	for (i = 0; i < fs->fs_ncg; i++) {
3065965Smckusic 		result = (*allocator)(ip, cg, 0, size);
3074359Smckusick 		if (result)
3084359Smckusick 			return (result);
3094359Smckusick 		cg++;
3104359Smckusick 		if (cg == fs->fs_ncg)
3114359Smckusick 			cg = 0;
3124359Smckusick 	}
3136294Smckusick 	return (NULL);
3144359Smckusick }
3154359Smckusick 
3165375Smckusic /*
3175375Smckusic  * Determine whether a fragment can be extended.
3185375Smckusic  *
3195375Smckusic  * Check to see if the necessary fragments are available, and
3205375Smckusic  * if they are, allocate them.
3215375Smckusic  */
3224359Smckusick daddr_t
3235965Smckusic fragextend(ip, cg, bprev, osize, nsize)
3245965Smckusic 	struct inode *ip;
3254426Smckusic 	int cg;
3264463Smckusic 	long bprev;
3274426Smckusic 	int osize, nsize;
3284426Smckusic {
3295965Smckusic 	register struct fs *fs;
3304463Smckusic 	register struct buf *bp;
3314463Smckusic 	register struct cg *cgp;
3324463Smckusic 	long bno;
3334463Smckusic 	int frags, bbase;
3344426Smckusic 	int i;
3354426Smckusic 
3365965Smckusic 	fs = ip->i_fs;
3376531Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < nsize - osize)
3386531Smckusick 		return (NULL);
3395960Smckusic 	frags = numfrags(fs, nsize);
3405960Smckusic 	bbase = fragoff(fs, bprev);
3415322Smckusic 	if (bbase > (bprev + frags - 1) % fs->fs_frag) {
3424463Smckusic 		/* cannot extend across a block boundry */
3436294Smckusick 		return (NULL);
3444463Smckusic 	}
3455965Smckusic 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize);
3466531Smckusick 	cgp = bp->b_un.b_cg;
3476531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
3485960Smckusic 		brelse(bp);
3496294Smckusick 		return (NULL);
3505960Smckusic 	}
3515377Smckusic 	bno = dtogd(fs, bprev);
3525960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
3535361Smckusic 		if (isclr(cgp->cg_free, bno + i)) {
3545361Smckusic 			brelse(bp);
3556294Smckusick 			return (NULL);
3565361Smckusic 		}
3575361Smckusic 	/*
3585361Smckusic 	 * the current fragment can be extended
3595361Smckusic 	 * deduct the count on fragment being extended into
3605361Smckusic 	 * increase the count on the remaining fragment (if any)
3615361Smckusic 	 * allocate the extended piece
3625361Smckusic 	 */
3635361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
3644463Smckusic 		if (isclr(cgp->cg_free, bno + i))
3654463Smckusic 			break;
3665960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
3675361Smckusic 	if (i != frags)
3685361Smckusic 		cgp->cg_frsum[i - frags]++;
3695960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
3705361Smckusic 		clrbit(cgp->cg_free, bno + i);
3715361Smckusic 		cgp->cg_cs.cs_nffree--;
3725361Smckusic 		fs->fs_cstotal.cs_nffree--;
3735361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
3744463Smckusic 	}
3755361Smckusic 	fs->fs_fmod++;
3765361Smckusic 	bdwrite(bp);
3775361Smckusic 	return (bprev);
3784426Smckusic }
3794426Smckusic 
3805375Smckusic /*
3815375Smckusic  * Determine whether a block can be allocated.
3825375Smckusic  *
3835375Smckusic  * Check to see if a block of the apprpriate size is available,
3845375Smckusic  * and if it is, allocate it.
3855375Smckusic  */
3864426Smckusic daddr_t
3875965Smckusic alloccg(ip, cg, bpref, size)
3885965Smckusic 	struct inode *ip;
3894359Smckusick 	int cg;
3904359Smckusick 	daddr_t bpref;
3914359Smckusick 	int size;
3924359Smckusick {
3935965Smckusic 	register struct fs *fs;
3944463Smckusic 	register struct buf *bp;
3954463Smckusic 	register struct cg *cgp;
3964463Smckusic 	int bno, frags;
3974463Smckusic 	int allocsiz;
3984463Smckusic 	register int i;
3994359Smckusick 
4005965Smckusic 	fs = ip->i_fs;
4015322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
4026294Smckusick 		return (NULL);
4035965Smckusic 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize);
4046531Smckusick 	cgp = bp->b_un.b_cg;
4056531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
4065960Smckusic 		brelse(bp);
4076294Smckusick 		return (NULL);
4085960Smckusic 	}
4095322Smckusic 	if (size == fs->fs_bsize) {
4105212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
4114463Smckusic 		bdwrite(bp);
4124463Smckusic 		return (bno);
4134463Smckusic 	}
4144463Smckusic 	/*
4154463Smckusic 	 * check to see if any fragments are already available
4164463Smckusic 	 * allocsiz is the size which will be allocated, hacking
4174463Smckusic 	 * it down to a smaller size if necessary
4184463Smckusic 	 */
4195960Smckusic 	frags = numfrags(fs, size);
4205322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
4214463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
4224463Smckusic 			break;
4235322Smckusic 	if (allocsiz == fs->fs_frag) {
4244463Smckusic 		/*
4254463Smckusic 		 * no fragments were available, so a block will be
4264463Smckusic 		 * allocated, and hacked up
4274463Smckusic 		 */
4284792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
4294463Smckusic 			brelse(bp);
4306294Smckusick 			return (NULL);
4314463Smckusic 		}
4325212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
4335377Smckusic 		bpref = dtogd(fs, bno);
4345322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
4354463Smckusic 			setbit(cgp->cg_free, bpref + i);
4365322Smckusic 		i = fs->fs_frag - frags;
4374792Smckusic 		cgp->cg_cs.cs_nffree += i;
4384792Smckusic 		fs->fs_cstotal.cs_nffree += i;
4395322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
4404463Smckusic 		cgp->cg_frsum[i]++;
4414463Smckusic 		bdwrite(bp);
4424463Smckusic 		return (bno);
4434463Smckusic 	}
4444651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
4456567Smckusic 	if (bno < 0)
4466294Smckusick 		return (NULL);
4474463Smckusic 	for (i = 0; i < frags; i++)
4484463Smckusic 		clrbit(cgp->cg_free, bno + i);
4494792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
4504792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
4515322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
4524463Smckusic 	cgp->cg_frsum[allocsiz]--;
4534463Smckusic 	if (frags != allocsiz)
4544463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
4554463Smckusic 	bdwrite(bp);
4564463Smckusic 	return (cg * fs->fs_fpg + bno);
4574463Smckusic }
4584463Smckusic 
4595375Smckusic /*
4605375Smckusic  * Allocate a block in a cylinder group.
4615375Smckusic  *
4625375Smckusic  * This algorithm implements the following policy:
4635375Smckusic  *   1) allocate the requested block.
4645375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
4655375Smckusic  *   3) allocate the next available block on the block rotor for the
4665375Smckusic  *      specified cylinder group.
4675375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
4685375Smckusic  * blocks may be fragmented by the routine that allocates them.
4695375Smckusic  */
4704463Smckusic daddr_t
4715212Smckusic alloccgblk(fs, cgp, bpref)
4725965Smckusic 	register struct fs *fs;
4734463Smckusic 	register struct cg *cgp;
4744463Smckusic 	daddr_t bpref;
4754463Smckusic {
4764651Smckusic 	daddr_t bno;
4776294Smckusick 	int cylno, pos, delta;
4784651Smckusic 	short *cylbp;
4795361Smckusic 	register int i;
4804463Smckusic 
4814651Smckusic 	if (bpref == 0) {
4824651Smckusic 		bpref = cgp->cg_rotor;
4835361Smckusic 		goto norot;
4845361Smckusic 	}
4855361Smckusic 	bpref &= ~(fs->fs_frag - 1);
4865377Smckusic 	bpref = dtogd(fs, bpref);
4875361Smckusic 	/*
4885361Smckusic 	 * if the requested block is available, use it
4895361Smckusic 	 */
4905361Smckusic 	if (isblock(fs, cgp->cg_free, bpref/fs->fs_frag)) {
4915361Smckusic 		bno = bpref;
4925361Smckusic 		goto gotit;
4935361Smckusic 	}
4945361Smckusic 	/*
4955361Smckusic 	 * check for a block available on the same cylinder
4965361Smckusic 	 */
4975361Smckusic 	cylno = cbtocylno(fs, bpref);
4985375Smckusic 	if (cgp->cg_btot[cylno] == 0)
4995375Smckusic 		goto norot;
5005375Smckusic 	if (fs->fs_cpc == 0) {
5015375Smckusic 		/*
5025375Smckusic 		 * block layout info is not available, so just have
5035375Smckusic 		 * to take any block in this cylinder.
5045375Smckusic 		 */
5055375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
5065375Smckusic 		goto norot;
5075375Smckusic 	}
5085375Smckusic 	/*
5095375Smckusic 	 * find a block that is rotationally optimal
5105375Smckusic 	 */
5115361Smckusic 	cylbp = cgp->cg_b[cylno];
5125361Smckusic 	if (fs->fs_rotdelay == 0) {
5135361Smckusic 		pos = cbtorpos(fs, bpref);
5144651Smckusic 	} else {
5154651Smckusic 		/*
5165361Smckusic 		 * here we convert ms of delay to frags as:
5175361Smckusic 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
5185361Smckusic 		 *	((sect/frag) * (ms/sec))
5195361Smckusic 		 * then round up to the next rotational position
5204651Smckusic 		 */
5215361Smckusic 		bpref += fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
5225361Smckusic 		    (NSPF(fs) * 1000);
5235361Smckusic 		pos = cbtorpos(fs, bpref);
5245361Smckusic 		pos = (pos + 1) % NRPOS;
5255361Smckusic 	}
5265361Smckusic 	/*
5275361Smckusic 	 * check the summary information to see if a block is
5285361Smckusic 	 * available in the requested cylinder starting at the
5295361Smckusic 	 * optimal rotational position and proceeding around.
5305361Smckusic 	 */
5315361Smckusic 	for (i = pos; i < NRPOS; i++)
5325361Smckusic 		if (cylbp[i] > 0)
5335361Smckusic 			break;
5345361Smckusic 	if (i == NRPOS)
5355361Smckusic 		for (i = 0; i < pos; i++)
5365361Smckusic 			if (cylbp[i] > 0)
5375361Smckusic 				break;
5385361Smckusic 	if (cylbp[i] > 0) {
5394651Smckusic 		/*
5405361Smckusic 		 * found a rotational position, now find the actual
5415361Smckusic 		 * block. A panic if none is actually there.
5424651Smckusic 		 */
5435361Smckusic 		pos = cylno % fs->fs_cpc;
5445361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
545*6716Smckusick 		if (fs->fs_postbl[pos][i] == -1) {
546*6716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
547*6716Smckusick 			    pos, i, fs->fs_fsmnt);
5485361Smckusic 			panic("alloccgblk: cyl groups corrupted");
549*6716Smckusick 		}
5506294Smckusick 		for (i = fs->fs_postbl[pos][i];; ) {
5515361Smckusic 			if (isblock(fs, cgp->cg_free, bno + i)) {
5525361Smckusic 				bno = (bno + i) * fs->fs_frag;
5535361Smckusic 				goto gotit;
5545361Smckusic 			}
5556294Smckusick 			delta = fs->fs_rotbl[i];
5566294Smckusick 			if (delta <= 0 || delta > MAXBPC - i)
5574651Smckusic 				break;
5586294Smckusick 			i += delta;
5594651Smckusic 		}
560*6716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
5615361Smckusic 		panic("alloccgblk: can't find blk in cyl");
5624359Smckusick 	}
5635361Smckusic norot:
5645361Smckusic 	/*
5655361Smckusic 	 * no blocks in the requested cylinder, so take next
5665361Smckusic 	 * available one in this cylinder group.
5675361Smckusic 	 */
5685322Smckusic 	bno = mapsearch(fs, cgp, bpref, fs->fs_frag);
5696567Smckusic 	if (bno < 0)
5706294Smckusick 		return (NULL);
5714651Smckusic 	cgp->cg_rotor = bno;
5724359Smckusick gotit:
5735322Smckusic 	clrblock(fs, cgp->cg_free, bno/fs->fs_frag);
5744792Smckusic 	cgp->cg_cs.cs_nbfree--;
5754792Smckusic 	fs->fs_cstotal.cs_nbfree--;
5765322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
5775375Smckusic 	cylno = cbtocylno(fs, bno);
5785375Smckusic 	cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
5795375Smckusic 	cgp->cg_btot[cylno]--;
5804359Smckusick 	fs->fs_fmod++;
5814651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
5824359Smckusick }
5834359Smckusick 
5845375Smckusic /*
5855375Smckusic  * Determine whether an inode can be allocated.
5865375Smckusic  *
5875375Smckusic  * Check to see if an inode is available, and if it is,
5885375Smckusic  * allocate it using the following policy:
5895375Smckusic  *   1) allocate the requested inode.
5905375Smckusic  *   2) allocate the next available inode after the requested
5915375Smckusic  *      inode in the specified cylinder group.
5925375Smckusic  */
5935212Smckusic ino_t
5945965Smckusic ialloccg(ip, cg, ipref, mode)
5955965Smckusic 	struct inode *ip;
5964359Smckusick 	int cg;
5974359Smckusick 	daddr_t ipref;
5984359Smckusick 	int mode;
5994359Smckusick {
6005965Smckusic 	register struct fs *fs;
6014463Smckusic 	register struct buf *bp;
6024463Smckusic 	register struct cg *cgp;
6034359Smckusick 	int i;
6044359Smckusick 
6055965Smckusic 	fs = ip->i_fs;
6065322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
6076294Smckusick 		return (NULL);
6085965Smckusic 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize);
6096531Smckusick 	cgp = bp->b_un.b_cg;
6106531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
6115960Smckusic 		brelse(bp);
6126294Smckusick 		return (NULL);
6135960Smckusic 	}
6144359Smckusick 	if (ipref) {
6154359Smckusick 		ipref %= fs->fs_ipg;
6164359Smckusick 		if (isclr(cgp->cg_iused, ipref))
6174359Smckusick 			goto gotit;
6184359Smckusick 	} else
6194359Smckusick 		ipref = cgp->cg_irotor;
6204359Smckusick 	for (i = 0; i < fs->fs_ipg; i++) {
6214359Smckusick 		ipref++;
6224359Smckusick 		if (ipref >= fs->fs_ipg)
6234359Smckusick 			ipref = 0;
6244359Smckusick 		if (isclr(cgp->cg_iused, ipref)) {
6254359Smckusick 			cgp->cg_irotor = ipref;
6264359Smckusick 			goto gotit;
6274359Smckusick 		}
6284359Smckusick 	}
6294359Smckusick 	brelse(bp);
6306294Smckusick 	return (NULL);
6314359Smckusick gotit:
6324359Smckusick 	setbit(cgp->cg_iused, ipref);
6334792Smckusic 	cgp->cg_cs.cs_nifree--;
6344792Smckusic 	fs->fs_cstotal.cs_nifree--;
6355322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
6364359Smckusick 	fs->fs_fmod++;
6374359Smckusick 	if ((mode & IFMT) == IFDIR) {
6384792Smckusic 		cgp->cg_cs.cs_ndir++;
6394792Smckusic 		fs->fs_cstotal.cs_ndir++;
6405322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
6414359Smckusick 	}
6424359Smckusick 	bdwrite(bp);
6434359Smckusick 	return (cg * fs->fs_ipg + ipref);
6444359Smckusick }
6454359Smckusick 
6465375Smckusic /*
6475375Smckusic  * Free a block or fragment.
6485375Smckusic  *
6495375Smckusic  * The specified block or fragment is placed back in the
6505375Smckusic  * free map. If a fragment is deallocated, a possible
6515375Smckusic  * block reassembly is checked.
6525375Smckusic  */
6535965Smckusic fre(ip, bno, size)
6545965Smckusic 	register struct inode *ip;
6554359Smckusick 	daddr_t bno;
6565212Smckusic 	off_t size;
6574359Smckusick {
6584359Smckusick 	register struct fs *fs;
6594359Smckusick 	register struct cg *cgp;
6604359Smckusick 	register struct buf *bp;
6614463Smckusic 	int cg, blk, frags, bbase;
6624463Smckusic 	register int i;
6634359Smckusick 
6645965Smckusic 	fs = ip->i_fs;
665*6716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
666*6716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
667*6716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
6684426Smckusic 		panic("free: bad size");
669*6716Smckusick 	}
6705377Smckusic 	cg = dtog(fs, bno);
6716567Smckusic 	if (badblock(fs, bno)) {
6726567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
6734359Smckusick 		return;
6746567Smckusic 	}
6755965Smckusic 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize);
6766531Smckusick 	cgp = bp->b_un.b_cg;
6776531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
6785960Smckusic 		brelse(bp);
6794359Smckusick 		return;
6805960Smckusic 	}
6815377Smckusic 	bno = dtogd(fs, bno);
6825322Smckusic 	if (size == fs->fs_bsize) {
6836567Smckusic 		if (isblock(fs, cgp->cg_free, bno/fs->fs_frag)) {
684*6716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
685*6716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
6864426Smckusic 			panic("free: freeing free block");
6876567Smckusic 		}
6885322Smckusic 		setblock(fs, cgp->cg_free, bno/fs->fs_frag);
6894792Smckusic 		cgp->cg_cs.cs_nbfree++;
6904792Smckusic 		fs->fs_cstotal.cs_nbfree++;
6915322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
6925375Smckusic 		i = cbtocylno(fs, bno);
6935375Smckusic 		cgp->cg_b[i][cbtorpos(fs, bno)]++;
6945375Smckusic 		cgp->cg_btot[i]++;
6954426Smckusic 	} else {
6965322Smckusic 		bbase = bno - (bno % fs->fs_frag);
6974463Smckusic 		/*
6984463Smckusic 		 * decrement the counts associated with the old frags
6994463Smckusic 		 */
7006294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
7015322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
7024463Smckusic 		/*
7034463Smckusic 		 * deallocate the fragment
7044463Smckusic 		 */
7055960Smckusic 		frags = numfrags(fs, size);
7064463Smckusic 		for (i = 0; i < frags; i++) {
707*6716Smckusick 			if (isset(cgp->cg_free, bno + i)) {
708*6716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
709*6716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
7104426Smckusic 				panic("free: freeing free frag");
711*6716Smckusick 			}
7124426Smckusic 			setbit(cgp->cg_free, bno + i);
7134426Smckusic 		}
7146294Smckusick 		cgp->cg_cs.cs_nffree += i;
7156294Smckusick 		fs->fs_cstotal.cs_nffree += i;
7166294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
7174463Smckusic 		/*
7184463Smckusic 		 * add back in counts associated with the new frags
7194463Smckusic 		 */
7206294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
7215322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
7224463Smckusic 		/*
7234463Smckusic 		 * if a complete block has been reassembled, account for it
7244463Smckusic 		 */
7255322Smckusic 		if (isblock(fs, cgp->cg_free, bbase / fs->fs_frag)) {
7265322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
7275322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
7285322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
7294792Smckusic 			cgp->cg_cs.cs_nbfree++;
7304792Smckusic 			fs->fs_cstotal.cs_nbfree++;
7315322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
7325375Smckusic 			i = cbtocylno(fs, bbase);
7335375Smckusic 			cgp->cg_b[i][cbtorpos(fs, bbase)]++;
7345375Smckusic 			cgp->cg_btot[i]++;
7354426Smckusic 		}
7364426Smckusic 	}
7374359Smckusick 	fs->fs_fmod++;
7384359Smckusick 	bdwrite(bp);
7394359Smckusick }
7404359Smckusick 
7415375Smckusic /*
7425375Smckusic  * Free an inode.
7435375Smckusic  *
7445375Smckusic  * The specified inode is placed back in the free map.
7455375Smckusic  */
7465965Smckusic ifree(ip, ino, mode)
7475965Smckusic 	struct inode *ip;
7484359Smckusick 	ino_t ino;
7494359Smckusick 	int mode;
7504359Smckusick {
7514359Smckusick 	register struct fs *fs;
7524359Smckusick 	register struct cg *cgp;
7534359Smckusick 	register struct buf *bp;
7544359Smckusick 	int cg;
7554359Smckusick 
7565965Smckusic 	fs = ip->i_fs;
757*6716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
758*6716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
759*6716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
7604359Smckusick 		panic("ifree: range");
761*6716Smckusick 	}
7625377Smckusic 	cg = itog(fs, ino);
7635965Smckusic 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), fs->fs_bsize);
7646531Smckusick 	cgp = bp->b_un.b_cg;
7656531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
7665960Smckusic 		brelse(bp);
7674359Smckusick 		return;
7685960Smckusic 	}
7694359Smckusick 	ino %= fs->fs_ipg;
770*6716Smckusick 	if (isclr(cgp->cg_iused, ino)) {
771*6716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
772*6716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
7734359Smckusick 		panic("ifree: freeing free inode");
774*6716Smckusick 	}
7754359Smckusick 	clrbit(cgp->cg_iused, ino);
7764792Smckusic 	cgp->cg_cs.cs_nifree++;
7774792Smckusic 	fs->fs_cstotal.cs_nifree++;
7785322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
7794359Smckusick 	if ((mode & IFMT) == IFDIR) {
7804792Smckusic 		cgp->cg_cs.cs_ndir--;
7814792Smckusic 		fs->fs_cstotal.cs_ndir--;
7825322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
7834359Smckusick 	}
7844359Smckusick 	fs->fs_fmod++;
7854359Smckusick 	bdwrite(bp);
7864359Smckusick }
7874359Smckusick 
7884463Smckusic /*
7895375Smckusic  * Find a block of the specified size in the specified cylinder group.
7905375Smckusic  *
7914651Smckusic  * It is a panic if a request is made to find a block if none are
7924651Smckusic  * available.
7934651Smckusic  */
7944651Smckusic daddr_t
7954651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
7964651Smckusic 	register struct fs *fs;
7974651Smckusic 	register struct cg *cgp;
7984651Smckusic 	daddr_t bpref;
7994651Smckusic 	int allocsiz;
8004651Smckusic {
8014651Smckusic 	daddr_t bno;
8024651Smckusic 	int start, len, loc, i;
8034651Smckusic 	int blk, field, subfield, pos;
8044651Smckusic 
8054651Smckusic 	/*
8064651Smckusic 	 * find the fragment by searching through the free block
8074651Smckusic 	 * map for an appropriate bit pattern
8084651Smckusic 	 */
8094651Smckusic 	if (bpref)
8105377Smckusic 		start = dtogd(fs, bpref) / NBBY;
8114651Smckusic 	else
8124651Smckusic 		start = cgp->cg_frotor / NBBY;
8135398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
8145322Smckusic 	loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag],
8156292Smckusick 		1 << (allocsiz - 1 + (fs->fs_frag % NBBY)));
8164651Smckusic 	if (loc == 0) {
8176531Smckusick 		len = start + 1;
8186531Smckusick 		start = 0;
8195322Smckusic 		loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag],
8206292Smckusick 			1 << (allocsiz - 1 + (fs->fs_frag % NBBY)));
8214651Smckusic 		if (loc == 0) {
822*6716Smckusick 			printf("start = %d, len = %d, fs = %s\n",
823*6716Smckusick 			    start, len, fs->fs_fsmnt);
8244651Smckusic 			panic("alloccg: map corrupted");
8256531Smckusick 			return (-1);
8264651Smckusic 		}
8274651Smckusic 	}
8284651Smckusic 	bno = (start + len - loc) * NBBY;
8294651Smckusic 	cgp->cg_frotor = bno;
8304651Smckusic 	/*
8314651Smckusic 	 * found the byte in the map
8324651Smckusic 	 * sift through the bits to find the selected frag
8334651Smckusic 	 */
8346294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
8356294Smckusick 		blk = blkmap(fs, cgp->cg_free, bno);
8364651Smckusic 		blk <<= 1;
8374651Smckusic 		field = around[allocsiz];
8384651Smckusic 		subfield = inside[allocsiz];
8395322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
8406294Smckusick 			if ((blk & field) == subfield)
8416294Smckusick 				return (bno + pos);
8424651Smckusic 			field <<= 1;
8434651Smckusic 			subfield <<= 1;
8444651Smckusic 		}
8454651Smckusic 	}
846*6716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
8474651Smckusic 	panic("alloccg: block not in map");
8486531Smckusick 	return (-1);
8494651Smckusic }
8504651Smckusic 
8514651Smckusic /*
8525375Smckusic  * Update the frsum fields to reflect addition or deletion
8535375Smckusic  * of some frags.
8544463Smckusic  */
8555322Smckusic fragacct(fs, fragmap, fraglist, cnt)
8565322Smckusic 	struct fs *fs;
8574472Smckusic 	int fragmap;
8584792Smckusic 	long fraglist[];
8594463Smckusic 	int cnt;
8604463Smckusic {
8614463Smckusic 	int inblk;
8624463Smckusic 	register int field, subfield;
8634463Smckusic 	register int siz, pos;
8644463Smckusic 
8655322Smckusic 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
8664463Smckusic 	fragmap <<= 1;
8675322Smckusic 	for (siz = 1; siz < fs->fs_frag; siz++) {
8686292Smckusick 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
8694463Smckusic 			continue;
8704463Smckusic 		field = around[siz];
8714463Smckusic 		subfield = inside[siz];
8725322Smckusic 		for (pos = siz; pos <= fs->fs_frag; pos++) {
8734463Smckusic 			if ((fragmap & field) == subfield) {
8744463Smckusic 				fraglist[siz] += cnt;
8754463Smckusic 				pos += siz;
8764463Smckusic 				field <<= siz;
8774463Smckusic 				subfield <<= siz;
8784463Smckusic 			}
8794463Smckusic 			field <<= 1;
8804463Smckusic 			subfield <<= 1;
8814463Smckusic 		}
8824463Smckusic 	}
8834463Smckusic }
8844463Smckusic 
8855375Smckusic /*
8865375Smckusic  * Check that a specified block number is in range.
8875375Smckusic  */
8884359Smckusick badblock(fs, bn)
8894359Smckusick 	register struct fs *fs;
8904359Smckusick 	daddr_t bn;
8914359Smckusick {
8924359Smckusick 
8936531Smckusick 	if ((unsigned)bn >= fs->fs_size) {
8946567Smckusic 		printf("bad block %d, ", bn);
8954359Smckusick 		fserr(fs, "bad block");
8964359Smckusick 		return (1);
8974359Smckusick 	}
8984359Smckusick 	return (0);
8994359Smckusick }
9004359Smckusick 
9014359Smckusick /*
9025375Smckusic  * Getfs maps a device number into a pointer to the incore super block.
9034359Smckusick  *
9045375Smckusic  * The algorithm is a linear search through the mount table. A
9055375Smckusic  * consistency check of the super block magic number is performed.
9065375Smckusic  *
9074359Smckusick  * panic: no fs -- the device is not mounted.
9084359Smckusick  *	this "cannot happen"
9094359Smckusick  */
9104359Smckusick struct fs *
9114359Smckusick getfs(dev)
9124359Smckusick 	dev_t dev;
9134359Smckusick {
9144359Smckusick 	register struct mount *mp;
9154359Smckusick 	register struct fs *fs;
9164359Smckusick 
9176294Smckusick 	for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++) {
9186294Smckusick 		if (mp->m_bufp == NULL || mp->m_dev != dev)
9196294Smckusick 			continue;
9206294Smckusick 		fs = mp->m_bufp->b_un.b_fs;
921*6716Smckusick 		if (fs->fs_magic != FS_MAGIC) {
922*6716Smckusick 			printf("dev = 0x%x, fs = %s\n", dev, fs->fs_fsmnt);
9236294Smckusick 			panic("getfs: bad magic");
924*6716Smckusick 		}
9256294Smckusick 		return (fs);
9266294Smckusick 	}
927*6716Smckusick 	printf("dev = 0x%x\n", dev);
9284359Smckusick 	panic("getfs: no fs");
9294359Smckusick 	return (NULL);
9304359Smckusick }
9314359Smckusick 
9324359Smckusick /*
9335375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
9345375Smckusic  *
9355375Smckusic  * The form of the error message is:
9364359Smckusick  *	fs: error message
9374359Smckusick  */
9384359Smckusick fserr(fs, cp)
9394359Smckusick 	struct fs *fs;
9404359Smckusick 	char *cp;
9414359Smckusick {
9424359Smckusick 
9434359Smckusick 	printf("%s: %s\n", fs->fs_fsmnt, cp);
9444359Smckusick }
9454359Smckusick 
9464359Smckusick /*
9474359Smckusick  * Getfsx returns the index in the file system
9484359Smckusick  * table of the specified device.  The swap device
9494359Smckusick  * is also assigned a pseudo-index.  The index may
9504359Smckusick  * be used as a compressed indication of the location
9514359Smckusick  * of a block, recording
9524359Smckusick  *	<getfsx(dev),blkno>
9534359Smckusick  * rather than
9544359Smckusick  *	<dev, blkno>
9554359Smckusick  * provided the information need remain valid only
9564359Smckusick  * as long as the file system is mounted.
9574359Smckusick  */
9584359Smckusick getfsx(dev)
9594359Smckusick 	dev_t dev;
9604359Smckusick {
9614359Smckusick 	register struct mount *mp;
9624359Smckusick 
9634359Smckusick 	if (dev == swapdev)
9644359Smckusick 		return (MSWAPX);
9654359Smckusick 	for(mp = &mount[0]; mp < &mount[NMOUNT]; mp++)
9664359Smckusick 		if (mp->m_dev == dev)
9674359Smckusick 			return (mp - &mount[0]);
9684359Smckusick 	return (-1);
9694359Smckusick }
9704359Smckusick 
9714359Smckusick /*
9724359Smckusick  * Update is the internal name of 'sync'.  It goes through the disk
9734359Smckusick  * queues to initiate sandbagged IO; goes through the inodes to write
9745375Smckusic  * modified nodes; and it goes through the mount table to initiate
9755375Smckusic  * the writing of the modified super blocks.
9764359Smckusick  */
9776294Smckusick update(flag)
9786294Smckusick 	int flag;
9794359Smckusick {
9804359Smckusick 	register struct inode *ip;
9814359Smckusick 	register struct mount *mp;
9824359Smckusick 	register struct buf *bp;
9834359Smckusick 	struct fs *fs;
9844651Smckusic 	int i, blks;
9854359Smckusick 
9864359Smckusick 	if (updlock)
9874359Smckusick 		return;
9884359Smckusick 	updlock++;
9894359Smckusick 	/*
9904359Smckusick 	 * Write back modified superblocks.
9914359Smckusick 	 * Consistency check that the superblock
9924359Smckusick 	 * of each file system is still in the buffer cache.
9934359Smckusick 	 */
9946294Smckusick 	for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++) {
9956294Smckusick 		if (mp->m_bufp == NULL)
9966294Smckusick 			continue;
9976294Smckusick 		fs = mp->m_bufp->b_un.b_fs;
9986294Smckusick 		if (fs->fs_fmod == 0)
9996294Smckusick 			continue;
1000*6716Smckusick 		if (fs->fs_ronly != 0) {
1001*6716Smckusick 			printf("fs = %s\n", fs->fs_fsmnt);
10026294Smckusick 			panic("update: rofs mod");
1003*6716Smckusick 		}
10046294Smckusick 		bp = getblk(mp->m_dev, SBLOCK, SBSIZE);
1005*6716Smckusick 		if (bp->b_un.b_fs != fs || fs->fs_magic != FS_MAGIC) {
1006*6716Smckusick 			printf("fs = %s\n", fs->fs_fsmnt);
10076531Smckusick 			panic("update: bad b_fs");
1008*6716Smckusick 		}
10096294Smckusick 		fs->fs_fmod = 0;
10106310Smckusick 		fs->fs_time = time;
10116294Smckusick 		bwrite(bp);
10126531Smckusick 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
10136531Smckusick 		for (i = 0; i < blks; i += fs->fs_frag) {
10146294Smckusick 			bp = getblk(mp->m_dev,
10156531Smckusick 			    fsbtodb(fs, fs->fs_csaddr + i),
10166531Smckusick 			    blks - i < fs->fs_frag ?
10176531Smckusick 				(blks - i) * fs->fs_fsize :
10186531Smckusick 				fs->fs_bsize);
10194359Smckusick 			bwrite(bp);
10204359Smckusick 		}
10216294Smckusick 	}
10224359Smckusick 	/*
10234359Smckusick 	 * Write back each (modified) inode.
10244359Smckusick 	 */
10256294Smckusick 	for (ip = inode; ip < inodeNINODE; ip++) {
10266294Smckusick 		if ((ip->i_flag & ILOCK) != 0 || ip->i_count == 0)
10276294Smckusick 			continue;
10286294Smckusick 		ip->i_flag |= ILOCK;
10296294Smckusick 		ip->i_count++;
10306310Smckusick 		iupdat(ip, &time, &time, 0);
10316294Smckusick 		iput(ip);
10326294Smckusick 	}
10334359Smckusick 	updlock = 0;
10344359Smckusick 	/*
10354359Smckusick 	 * Force stale buffer cache information to be flushed,
10364359Smckusick 	 * for all devices.
10374359Smckusick 	 */
10384359Smckusick 	bflush(NODEV);
10394359Smckusick }
10405322Smckusic 
10415322Smckusic /*
10425375Smckusic  * block operations
10435375Smckusic  *
10445375Smckusic  * check if a block is available
10455322Smckusic  */
10465322Smckusic isblock(fs, cp, h)
10475322Smckusic 	struct fs *fs;
10485322Smckusic 	unsigned char *cp;
10495322Smckusic 	int h;
10505322Smckusic {
10515322Smckusic 	unsigned char mask;
10525322Smckusic 
10535322Smckusic 	switch (fs->fs_frag) {
10545322Smckusic 	case 8:
10555322Smckusic 		return (cp[h] == 0xff);
10565322Smckusic 	case 4:
10575322Smckusic 		mask = 0x0f << ((h & 0x1) << 2);
10585322Smckusic 		return ((cp[h >> 1] & mask) == mask);
10595322Smckusic 	case 2:
10605322Smckusic 		mask = 0x03 << ((h & 0x3) << 1);
10615322Smckusic 		return ((cp[h >> 2] & mask) == mask);
10625322Smckusic 	case 1:
10635322Smckusic 		mask = 0x01 << (h & 0x7);
10645322Smckusic 		return ((cp[h >> 3] & mask) == mask);
10655322Smckusic 	default:
10666294Smckusick 		panic("isblock");
10676294Smckusick 		return (NULL);
10685322Smckusic 	}
10695322Smckusic }
10705375Smckusic 
10715375Smckusic /*
10725375Smckusic  * take a block out of the map
10735375Smckusic  */
10745322Smckusic clrblock(fs, cp, h)
10755322Smckusic 	struct fs *fs;
10765322Smckusic 	unsigned char *cp;
10775322Smckusic 	int h;
10785322Smckusic {
10795322Smckusic 	switch ((fs)->fs_frag) {
10805322Smckusic 	case 8:
10815322Smckusic 		cp[h] = 0;
10825322Smckusic 		return;
10835322Smckusic 	case 4:
10845322Smckusic 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
10855322Smckusic 		return;
10865322Smckusic 	case 2:
10875322Smckusic 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
10885322Smckusic 		return;
10895322Smckusic 	case 1:
10905322Smckusic 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
10915322Smckusic 		return;
10925322Smckusic 	default:
10936294Smckusick 		panic("clrblock");
10945322Smckusic 		return;
10955322Smckusic 	}
10965322Smckusic }
10975375Smckusic 
10985375Smckusic /*
10995375Smckusic  * put a block into the map
11005375Smckusic  */
11015322Smckusic setblock(fs, cp, h)
11025322Smckusic 	struct fs *fs;
11035322Smckusic 	unsigned char *cp;
11045322Smckusic 	int h;
11055322Smckusic {
11065322Smckusic 	switch (fs->fs_frag) {
11075322Smckusic 	case 8:
11085322Smckusic 		cp[h] = 0xff;
11095322Smckusic 		return;
11105322Smckusic 	case 4:
11115322Smckusic 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
11125322Smckusic 		return;
11135322Smckusic 	case 2:
11145322Smckusic 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
11155322Smckusic 		return;
11165322Smckusic 	case 1:
11175322Smckusic 		cp[h >> 3] |= (0x01 << (h & 0x7));
11185322Smckusic 		return;
11195322Smckusic 	default:
11206294Smckusick 		panic("setblock");
11215322Smckusic 		return;
11225322Smckusic 	}
11235322Smckusic }
1124