2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ffs_alloc.c 8.18 (Berkeley) 5/26/95
34 * $FreeBSD: src/sys/ufs/ffs/ffs_alloc.c,v 1.64.2.2 2001/09/21 19:15:21 dillon Exp $
35 * $DragonFly: src/sys/vfs/ufs/ffs_alloc.c,v 1.11 2004/08/09 19:41:04 dillon Exp $
38 #include "opt_quota.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
53 #include "ufs_extern.h"
57 #include "ffs_extern.h"
59 typedef ufs_daddr_t allocfcn_t (struct inode *ip, int cg, ufs_daddr_t bpref,
62 static ufs_daddr_t ffs_alloccg (struct inode *, int, ufs_daddr_t, int);
64 ffs_alloccgblk (struct inode *, struct buf *, ufs_daddr_t);
66 static int ffs_checkblk (struct inode *, ufs_daddr_t, long);
68 static void ffs_clusteracct (struct fs *, struct cg *, ufs_daddr_t,
70 static ufs_daddr_t ffs_clusteralloc (struct inode *, int, ufs_daddr_t,
72 static ino_t ffs_dirpref (struct inode *);
73 static ufs_daddr_t ffs_fragextend (struct inode *, int, long, int, int);
74 static void ffs_fserr (struct fs *, uint, char *);
75 static u_long ffs_hashalloc
76 (struct inode *, int, long, int, allocfcn_t *);
77 static ino_t ffs_nodealloccg (struct inode *, int, ufs_daddr_t, int);
78 static ufs_daddr_t ffs_mapsearch (struct fs *, struct cg *, ufs_daddr_t,
82 * Allocate a block in the filesystem.
84 * The size of the requested block is given, which must be some
85 * multiple of fs_fsize and <= fs_bsize.
86 * A preference may be optionally specified. If a preference is given
87 * the following hierarchy is used to allocate a block:
88 * 1) allocate the requested block.
89 * 2) allocate a rotationally optimal block in the same cylinder.
90 * 3) allocate a block in the same cylinder group.
91 * 4) quadradically rehash into other cylinder groups, until an
92 * available block is located.
93 * If no block preference is given the following heirarchy is used
94 * to allocate a block:
95 * 1) allocate a block in the cylinder group that contains the
97 * 2) quadradically rehash into other cylinder groups, until an
98 * available block is located.
101 ffs_alloc(struct inode *ip, ufs_daddr_t lbn, ufs_daddr_t bpref, int size,
102 struct ucred *cred, ufs_daddr_t *bnp)
114 if ((uint)size > fs->fs_bsize || fragoff(fs, size) != 0) {
115 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
116 devtoname(ip->i_dev), (long)fs->fs_bsize, size,
118 panic("ffs_alloc: bad size");
121 panic("ffs_alloc: missing credential");
122 #endif /* DIAGNOSTIC */
123 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
125 if (cred->cr_uid != 0 &&
126 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
129 error = chkdq(ip, (long)btodb(size), cred, 0);
133 if (bpref >= fs->fs_size)
136 cg = ino_to_cg(fs, ip->i_number);
138 cg = dtog(fs, bpref);
139 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
142 ip->i_blocks += btodb(size);
143 ip->i_flag |= IN_CHANGE | IN_UPDATE;
149 * Restore user's disk quota because allocation failed.
151 (void) chkdq(ip, (long)-btodb(size), cred, FORCE);
154 ffs_fserr(fs, cred->cr_uid, "filesystem full");
155 uprintf("\n%s: write failed, filesystem is full\n", fs->fs_fsmnt);
160 * Reallocate a fragment to a bigger size
162 * The number and size of the old block is given, and a preference
163 * and new size is also specified. The allocator attempts to extend
164 * the original block. Failing that, the regular block allocator is
165 * invoked to get an appropriate block.
168 ffs_realloccg(struct inode *ip, ufs_daddr_t lbprev, ufs_daddr_t bpref,
169 int osize, int nsize, struct ucred *cred, struct buf **bpp)
173 int cg, request, error;
174 ufs_daddr_t bprev, bno;
179 if ((uint)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
180 (uint)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
182 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
183 devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
184 nsize, fs->fs_fsmnt);
185 panic("ffs_realloccg: bad size");
188 panic("ffs_realloccg: missing credential");
189 #endif /* DIAGNOSTIC */
190 if (cred->cr_uid != 0 &&
191 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0)
193 if ((bprev = ip->i_db[lbprev]) == 0) {
194 printf("dev = %s, bsize = %ld, bprev = %ld, fs = %s\n",
195 devtoname(ip->i_dev), (long)fs->fs_bsize, (long)bprev,
197 panic("ffs_realloccg: bad bprev");
200 * Allocate the extra space in the buffer.
202 error = bread(ITOV(ip), lbprev, osize, &bp);
208 if( bp->b_blkno == bp->b_lblkno) {
209 if( lbprev >= NDADDR)
210 panic("ffs_realloccg: lbprev out of range");
211 bp->b_blkno = fsbtodb(fs, bprev);
215 error = chkdq(ip, (long)btodb(nsize - osize), cred, 0);
222 * Check for extension in the existing location.
224 cg = dtog(fs, bprev);
225 bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize);
227 if (bp->b_blkno != fsbtodb(fs, bno))
228 panic("ffs_realloccg: bad blockno");
229 ip->i_blocks += btodb(nsize - osize);
230 ip->i_flag |= IN_CHANGE | IN_UPDATE;
232 bp->b_flags |= B_DONE;
233 bzero((char *)bp->b_data + osize, (uint)nsize - osize);
238 * Allocate a new disk location.
240 if (bpref >= fs->fs_size)
242 switch ((int)fs->fs_optim) {
245 * Allocate an exact sized fragment. Although this makes
246 * best use of space, we will waste time relocating it if
247 * the file continues to grow. If the fragmentation is
248 * less than half of the minimum free reserve, we choose
249 * to begin optimizing for time.
252 if (fs->fs_minfree <= 5 ||
253 fs->fs_cstotal.cs_nffree >
254 (off_t)fs->fs_dsize * fs->fs_minfree / (2 * 100))
256 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
258 fs->fs_optim = FS_OPTTIME;
262 * At this point we have discovered a file that is trying to
263 * grow a small fragment to a larger fragment. To save time,
264 * we allocate a full sized block, then free the unused portion.
265 * If the file continues to grow, the `ffs_fragextend' call
266 * above will be able to grow it in place without further
267 * copying. If aberrant programs cause disk fragmentation to
268 * grow within 2% of the free reserve, we choose to begin
269 * optimizing for space.
271 request = fs->fs_bsize;
272 if (fs->fs_cstotal.cs_nffree <
273 (off_t)fs->fs_dsize * (fs->fs_minfree - 2) / 100)
275 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
277 fs->fs_optim = FS_OPTSPACE;
280 printf("dev = %s, optim = %ld, fs = %s\n",
281 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
282 panic("ffs_realloccg: bad optim");
285 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
288 bp->b_blkno = fsbtodb(fs, bno);
289 if (!DOINGSOFTDEP(ITOV(ip)))
290 ffs_blkfree(ip, bprev, (long)osize);
292 ffs_blkfree(ip, bno + numfrags(fs, nsize),
293 (long)(request - nsize));
294 ip->i_blocks += btodb(nsize - osize);
295 ip->i_flag |= IN_CHANGE | IN_UPDATE;
297 bp->b_flags |= B_DONE;
298 bzero((char *)bp->b_data + osize, (uint)nsize - osize);
304 * Restore user's disk quota because allocation failed.
306 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
313 ffs_fserr(fs, cred->cr_uid, "filesystem full");
314 uprintf("\n%s: write failed, filesystem is full\n", fs->fs_fsmnt);
318 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
321 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
323 * The vnode and an array of buffer pointers for a range of sequential
324 * logical blocks to be made contiguous is given. The allocator attempts
325 * to find a range of sequential blocks starting as close as possible to
326 * an fs_rotdelay offset from the end of the allocation for the logical
327 * block immediately preceeding the current range. If successful, the
328 * physical block numbers in the buffer pointers and in the inode are
329 * changed to reflect the new allocation. If unsuccessful, the allocation
330 * is left unchanged. The success in doing the reallocation is returned.
331 * Note that the error return is not reflected back to the user. Rather
332 * the previous block allocation will be used.
334 static int doasyncfree = 1;
335 SYSCTL_INT(_vfs_ffs, FFS_ASYNCFREE, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
337 static int doreallocblks = 1;
338 SYSCTL_INT(_vfs_ffs, FFS_REALLOCBLKS, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
341 static volatile int prtrealloc = 0;
345 * ffs_reallocblks(struct vnode *a_vp, struct cluster_save *a_buflist)
348 ffs_reallocblks(struct vop_reallocblks_args *ap)
353 struct buf *sbp, *ebp;
354 ufs_daddr_t *bap, *sbap, *ebap = 0;
355 struct cluster_save *buflist;
356 ufs_daddr_t start_lbn, end_lbn, soff, newblk, blkno;
357 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
358 int i, len, start_lvl, end_lvl, pref, ssize;
360 if (doreallocblks == 0)
365 if (fs->fs_contigsumsize <= 0)
367 buflist = ap->a_buflist;
368 len = buflist->bs_nchildren;
369 start_lbn = buflist->bs_children[0]->b_lblkno;
370 end_lbn = start_lbn + len - 1;
372 for (i = 0; i < len; i++)
373 if (!ffs_checkblk(ip,
374 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
375 panic("ffs_reallocblks: unallocated block 1");
376 for (i = 1; i < len; i++)
377 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
378 panic("ffs_reallocblks: non-logical cluster");
379 blkno = buflist->bs_children[0]->b_blkno;
380 ssize = fsbtodb(fs, fs->fs_frag);
381 for (i = 1; i < len - 1; i++)
382 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
383 panic("ffs_reallocblks: non-physical cluster %d", i);
386 * If the latest allocation is in a new cylinder group, assume that
387 * the filesystem has decided to move and do not force it back to
388 * the previous cylinder group.
390 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
391 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
393 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
394 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
397 * Get the starting offset and block map for the first block.
399 if (start_lvl == 0) {
403 idp = &start_ap[start_lvl - 1];
404 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, &sbp)) {
408 sbap = (ufs_daddr_t *)sbp->b_data;
412 * Find the preferred location for the cluster.
414 pref = ffs_blkpref(ip, start_lbn, soff, sbap);
416 * If the block range spans two block maps, get the second map.
418 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
422 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
423 panic("ffs_reallocblk: start == end");
425 ssize = len - (idp->in_off + 1);
426 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, &ebp))
428 ebap = (ufs_daddr_t *)ebp->b_data;
431 * Search the block map looking for an allocation of the desired size.
433 if ((newblk = (ufs_daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
434 len, ffs_clusteralloc)) == 0)
437 * We have found a new contiguous block.
439 * First we have to replace the old block pointers with the new
440 * block pointers in the inode and indirect blocks associated
445 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
449 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
455 if (!ffs_checkblk(ip,
456 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
457 panic("ffs_reallocblks: unallocated block 2");
458 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
459 panic("ffs_reallocblks: alloc mismatch");
463 printf(" %d,", *bap);
465 if (DOINGSOFTDEP(vp)) {
466 if (sbap == &ip->i_db[0] && i < ssize)
467 softdep_setup_allocdirect(ip, start_lbn + i,
468 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
469 buflist->bs_children[i]);
471 softdep_setup_allocindir_page(ip, start_lbn + i,
472 i < ssize ? sbp : ebp, soff + i, blkno,
473 *bap, buflist->bs_children[i]);
478 * Next we must write out the modified inode and indirect blocks.
479 * For strict correctness, the writes should be synchronous since
480 * the old block values may have been written to disk. In practise
481 * they are almost never written, but if we are concerned about
482 * strict correctness, the `doasyncfree' flag should be set to zero.
484 * The test on `doasyncfree' should be changed to test a flag
485 * that shows whether the associated buffers and inodes have
486 * been written. The flag should be set when the cluster is
487 * started and cleared whenever the buffer or inode is flushed.
488 * We can then check below to see if it is set, and do the
489 * synchronous write only when it has been cleared.
491 if (sbap != &ip->i_db[0]) {
497 ip->i_flag |= IN_CHANGE | IN_UPDATE;
508 * Last, free the old blocks and assign the new blocks to the buffers.
514 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
515 if (!DOINGSOFTDEP(vp))
517 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
519 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
521 if (!ffs_checkblk(ip,
522 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
523 panic("ffs_reallocblks: unallocated block 3");
527 printf(" %d,", blkno);
541 if (sbap != &ip->i_db[0])
547 * Allocate an inode in the filesystem.
549 * If allocating a directory, use ffs_dirpref to select the inode.
550 * If allocating in a directory, the following hierarchy is followed:
551 * 1) allocate the preferred inode.
552 * 2) allocate an inode in the same cylinder group.
553 * 3) quadradically rehash into other cylinder groups, until an
554 * available inode is located.
555 * If no inode preference is given the following heirarchy is used
556 * to allocate an inode:
557 * 1) allocate an inode in cylinder group 0.
558 * 2) quadradically rehash into other cylinder groups, until an
559 * available inode is located.
562 ffs_valloc(struct vnode *pvp, int mode, struct ucred *cred, struct vnode **vpp)
573 if (fs->fs_cstotal.cs_nifree == 0)
576 if ((mode & IFMT) == IFDIR)
577 ipref = ffs_dirpref(pip);
579 ipref = pip->i_number;
580 if (ipref >= fs->fs_ncg * fs->fs_ipg)
582 cg = ino_to_cg(fs, ipref);
584 * Track number of dirs created one after another
585 * in a same cg without intervening by files.
587 if ((mode & IFMT) == IFDIR) {
588 if (fs->fs_contigdirs[cg] < 255)
589 fs->fs_contigdirs[cg]++;
591 if (fs->fs_contigdirs[cg] > 0)
592 fs->fs_contigdirs[cg]--;
594 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode,
595 (allocfcn_t *)ffs_nodealloccg);
598 error = VFS_VGET(pvp->v_mount, ino, vpp);
600 UFS_VFREE(pvp, ino, mode);
605 printf("mode = 0%o, inum = %lu, fs = %s\n",
606 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
607 panic("ffs_valloc: dup alloc");
609 if (ip->i_blocks) { /* XXX */
610 printf("free inode %s/%lu had %ld blocks\n",
611 fs->fs_fsmnt, (u_long)ino, (long)ip->i_blocks);
616 * Set up a new generation number for this inode.
618 if (ip->i_gen == 0 || ++ip->i_gen == 0)
619 ip->i_gen = random() / 2 + 1;
622 ffs_fserr(fs, cred->cr_uid, "out of inodes");
623 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
628 * Find a cylinder group to place a directory.
630 * The policy implemented by this algorithm is to allocate a
631 * directory inode in the same cylinder group as its parent
632 * directory, but also to reserve space for its files inodes
633 * and data. Restrict the number of directories which may be
634 * allocated one after another in the same cylinder group
635 * without intervening allocation of files.
637 * If we allocate a first level directory then force allocation
638 * in another cylinder group.
641 ffs_dirpref(struct inode *pip)
644 int cg, prefcg, dirsize, cgsize;
646 int avgifree, avgbfree, avgndir, curdirsize;
647 int minifree, minbfree, maxndir;
653 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
654 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
655 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
658 * Force allocation in another cg if creating a first level dir.
660 if (ITOV(pip)->v_flag & VROOT) {
661 prefcg = arc4random() % fs->fs_ncg;
663 minndir = fs->fs_ipg;
664 for (cg = prefcg; cg < fs->fs_ncg; cg++)
665 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
666 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
667 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
669 minndir = fs->fs_cs(fs, cg).cs_ndir;
671 for (cg = 0; cg < prefcg; cg++)
672 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
673 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
674 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
676 minndir = fs->fs_cs(fs, cg).cs_ndir;
678 return ((ino_t)(fs->fs_ipg * mincg));
682 * Count various limits which used for
683 * optimal allocation of a directory inode.
685 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
686 minifree = avgifree - avgifree / 4;
689 minbfree = avgbfree - avgbfree / 4;
692 cgsize = fs->fs_fsize * fs->fs_fpg;
695 * fs_avgfilesize and fs_avgfpdir are user-settable entities and
696 * multiplying them may overflow a 32 bit integer.
698 dirsize64 = fs->fs_avgfilesize * (int64_t)fs->fs_avgfpdir;
699 if (dirsize64 > 0x7fffffff) {
702 dirsize = (int)dirsize64;
703 curdirsize = avgndir ?
704 (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
705 if (dirsize < curdirsize)
706 dirsize = curdirsize;
707 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
708 if (fs->fs_avgfpdir > 0)
709 maxcontigdirs = min(maxcontigdirs,
710 fs->fs_ipg / fs->fs_avgfpdir);
711 if (maxcontigdirs == 0)
716 * Limit number of dirs in one cg and reserve space for
717 * regular files, but only if we have no deficit in
720 prefcg = ino_to_cg(fs, pip->i_number);
721 for (cg = prefcg; cg < fs->fs_ncg; cg++)
722 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
723 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
724 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
725 if (fs->fs_contigdirs[cg] < maxcontigdirs)
726 return ((ino_t)(fs->fs_ipg * cg));
728 for (cg = 0; cg < prefcg; cg++)
729 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
730 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
731 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
732 if (fs->fs_contigdirs[cg] < maxcontigdirs)
733 return ((ino_t)(fs->fs_ipg * cg));
736 * This is a backstop when we have deficit in space.
738 for (cg = prefcg; cg < fs->fs_ncg; cg++)
739 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
740 return ((ino_t)(fs->fs_ipg * cg));
741 for (cg = 0; cg < prefcg; cg++)
742 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
744 return ((ino_t)(fs->fs_ipg * cg));
748 * Select the desired position for the next block in a file. The file is
749 * logically divided into sections. The first section is composed of the
750 * direct blocks. Each additional section contains fs_maxbpg blocks.
752 * If no blocks have been allocated in the first section, the policy is to
753 * request a block in the same cylinder group as the inode that describes
754 * the file. If no blocks have been allocated in any other section, the
755 * policy is to place the section in a cylinder group with a greater than
756 * average number of free blocks. An appropriate cylinder group is found
757 * by using a rotor that sweeps the cylinder groups. When a new group of
758 * blocks is needed, the sweep begins in the cylinder group following the
759 * cylinder group from which the previous allocation was made. The sweep
760 * continues until a cylinder group with greater than the average number
761 * of free blocks is found. If the allocation is for the first block in an
762 * indirect block, the information on the previous allocation is unavailable;
763 * here a best guess is made based upon the logical block number being
766 * If a section is already partially allocated, the policy is to
767 * contiguously allocate fs_maxcontig blocks. The end of one of these
768 * contiguous blocks and the beginning of the next is physically separated
769 * so that the disk head will be in transit between them for at least
770 * fs_rotdelay milliseconds. This is to allow time for the processor to
771 * schedule another I/O transfer.
774 ffs_blkpref(struct inode *ip, ufs_daddr_t lbn, int indx, ufs_daddr_t *bap)
778 int avgbfree, startcg;
782 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
783 if (lbn < NDADDR + NINDIR(fs)) {
784 cg = ino_to_cg(fs, ip->i_number);
785 return (fs->fs_fpg * cg + fs->fs_frag);
788 * Find a cylinder with greater than average number of
789 * unused data blocks.
791 if (indx == 0 || bap[indx - 1] == 0)
793 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
795 startcg = dtog(fs, bap[indx - 1]) + 1;
796 startcg %= fs->fs_ncg;
797 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
798 for (cg = startcg; cg < fs->fs_ncg; cg++)
799 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
801 return (fs->fs_fpg * cg + fs->fs_frag);
803 for (cg = 0; cg <= startcg; cg++)
804 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
806 return (fs->fs_fpg * cg + fs->fs_frag);
811 * One or more previous blocks have been laid out. If less
812 * than fs_maxcontig previous blocks are contiguous, the
813 * next block is requested contiguously, otherwise it is
814 * requested rotationally delayed by fs_rotdelay milliseconds.
816 nextblk = bap[indx - 1] + fs->fs_frag;
817 if (fs->fs_rotdelay == 0 || indx < fs->fs_maxcontig ||
818 bap[indx - fs->fs_maxcontig] +
819 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
822 * Here we convert ms of delay to frags as:
823 * (frags) = (ms) * (rev/sec) * (sect/rev) /
824 * ((sect/frag) * (ms/sec))
825 * then round up to the next block.
827 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
828 (NSPF(fs) * 1000), fs->fs_frag);
833 * Implement the cylinder overflow algorithm.
835 * The policy implemented by this algorithm is:
836 * 1) allocate the block in its requested cylinder group.
837 * 2) quadradically rehash on the cylinder group number.
838 * 3) brute force search for a free block.
842 ffs_hashalloc(struct inode *ip, int cg, long pref,
843 int size, /* size for data blocks, mode for inodes */
844 allocfcn_t *allocator)
847 long result; /* XXX why not same type as we return? */
852 * 1: preferred cylinder group
854 result = (*allocator)(ip, cg, pref, size);
858 * 2: quadratic rehash
860 for (i = 1; i < fs->fs_ncg; i *= 2) {
862 if (cg >= fs->fs_ncg)
864 result = (*allocator)(ip, cg, 0, size);
869 * 3: brute force search
870 * Note that we start at i == 2, since 0 was checked initially,
871 * and 1 is always checked in the quadratic rehash.
873 cg = (icg + 2) % fs->fs_ncg;
874 for (i = 2; i < fs->fs_ncg; i++) {
875 result = (*allocator)(ip, cg, 0, size);
879 if (cg == fs->fs_ncg)
886 * Determine whether a fragment can be extended.
888 * Check to see if the necessary fragments are available, and
889 * if they are, allocate them.
892 ffs_fragextend(struct inode *ip, int cg, long bprev, int osize, int nsize)
903 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
905 frags = numfrags(fs, nsize);
906 bbase = fragnum(fs, bprev);
907 if (bbase > fragnum(fs, (bprev + frags - 1))) {
908 /* cannot extend across a block boundary */
911 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
912 (int)fs->fs_cgsize, &bp);
917 cgp = (struct cg *)bp->b_data;
918 if (!cg_chkmagic(cgp)) {
922 bp->b_xflags |= BX_BKGRDWRITE;
923 cgp->cg_time = time_second;
924 bno = dtogd(fs, bprev);
925 blksfree = cg_blksfree(cgp);
926 for (i = numfrags(fs, osize); i < frags; i++)
927 if (isclr(blksfree, bno + i)) {
932 * the current fragment can be extended
933 * deduct the count on fragment being extended into
934 * increase the count on the remaining fragment (if any)
935 * allocate the extended piece
937 for (i = frags; i < fs->fs_frag - bbase; i++)
938 if (isclr(blksfree, bno + i))
940 cgp->cg_frsum[i - numfrags(fs, osize)]--;
942 cgp->cg_frsum[i - frags]++;
943 for (i = numfrags(fs, osize); i < frags; i++) {
944 clrbit(blksfree, bno + i);
945 cgp->cg_cs.cs_nffree--;
946 fs->fs_cstotal.cs_nffree--;
947 fs->fs_cs(fs, cg).cs_nffree--;
950 if (DOINGSOFTDEP(ITOV(ip)))
951 softdep_setup_blkmapdep(bp, fs, bprev);
957 * Determine whether a block can be allocated.
959 * Check to see if a block of the appropriate size is available,
960 * and if it is, allocate it.
963 ffs_alloccg(struct inode *ip, int cg, ufs_daddr_t bpref, int size)
969 ufs_daddr_t bno, blkno;
970 int allocsiz, error, frags;
974 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
976 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
977 (int)fs->fs_cgsize, &bp);
982 cgp = (struct cg *)bp->b_data;
983 if (!cg_chkmagic(cgp) ||
984 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
988 bp->b_xflags |= BX_BKGRDWRITE;
989 cgp->cg_time = time_second;
990 if (size == fs->fs_bsize) {
991 bno = ffs_alloccgblk(ip, bp, bpref);
996 * check to see if any fragments are already available
997 * allocsiz is the size which will be allocated, hacking
998 * it down to a smaller size if necessary
1000 blksfree = cg_blksfree(cgp);
1001 frags = numfrags(fs, size);
1002 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
1003 if (cgp->cg_frsum[allocsiz] != 0)
1005 if (allocsiz == fs->fs_frag) {
1007 * no fragments were available, so a block will be
1008 * allocated, and hacked up
1010 if (cgp->cg_cs.cs_nbfree == 0) {
1014 bno = ffs_alloccgblk(ip, bp, bpref);
1015 bpref = dtogd(fs, bno);
1016 for (i = frags; i < fs->fs_frag; i++)
1017 setbit(blksfree, bpref + i);
1018 i = fs->fs_frag - frags;
1019 cgp->cg_cs.cs_nffree += i;
1020 fs->fs_cstotal.cs_nffree += i;
1021 fs->fs_cs(fs, cg).cs_nffree += i;
1027 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1032 for (i = 0; i < frags; i++)
1033 clrbit(blksfree, bno + i);
1034 cgp->cg_cs.cs_nffree -= frags;
1035 fs->fs_cstotal.cs_nffree -= frags;
1036 fs->fs_cs(fs, cg).cs_nffree -= frags;
1038 cgp->cg_frsum[allocsiz]--;
1039 if (frags != allocsiz)
1040 cgp->cg_frsum[allocsiz - frags]++;
1041 blkno = cg * fs->fs_fpg + bno;
1042 if (DOINGSOFTDEP(ITOV(ip)))
1043 softdep_setup_blkmapdep(bp, fs, blkno);
1045 return ((u_long)blkno);
1049 * Allocate a block in a cylinder group.
1051 * This algorithm implements the following policy:
1052 * 1) allocate the requested block.
1053 * 2) allocate a rotationally optimal block in the same cylinder.
1054 * 3) allocate the next available block on the block rotor for the
1055 * specified cylinder group.
1056 * Note that this routine only allocates fs_bsize blocks; these
1057 * blocks may be fragmented by the routine that allocates them.
1060 ffs_alloccgblk(struct inode *ip, struct buf *bp, ufs_daddr_t bpref)
1064 ufs_daddr_t bno, blkno;
1065 int cylno, pos, delta;
1071 cgp = (struct cg *)bp->b_data;
1072 blksfree = cg_blksfree(cgp);
1073 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1074 bpref = cgp->cg_rotor;
1077 bpref = blknum(fs, bpref);
1078 bpref = dtogd(fs, bpref);
1080 * if the requested block is available, use it
1082 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bpref))) {
1086 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1088 * Block layout information is not available.
1089 * Leaving bpref unchanged means we take the
1090 * next available free block following the one
1091 * we just allocated. Hopefully this will at
1092 * least hit a track cache on drives of unknown
1093 * geometry (e.g. SCSI).
1098 * check for a block available on the same cylinder
1100 cylno = cbtocylno(fs, bpref);
1101 if (cg_blktot(cgp)[cylno] == 0)
1104 * check the summary information to see if a block is
1105 * available in the requested cylinder starting at the
1106 * requested rotational position and proceeding around.
1108 cylbp = cg_blks(fs, cgp, cylno);
1109 pos = cbtorpos(fs, bpref);
1110 for (i = pos; i < fs->fs_nrpos; i++)
1113 if (i == fs->fs_nrpos)
1114 for (i = 0; i < pos; i++)
1119 * found a rotational position, now find the actual
1120 * block. A panic if none is actually there.
1122 pos = cylno % fs->fs_cpc;
1123 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1124 if (fs_postbl(fs, pos)[i] == -1) {
1125 printf("pos = %d, i = %d, fs = %s\n",
1126 pos, i, fs->fs_fsmnt);
1127 panic("ffs_alloccgblk: cyl groups corrupted");
1129 for (i = fs_postbl(fs, pos)[i];; ) {
1130 if (ffs_isblock(fs, blksfree, bno + i)) {
1131 bno = blkstofrags(fs, (bno + i));
1134 delta = fs_rotbl(fs)[i];
1136 delta + i > fragstoblks(fs, fs->fs_fpg))
1140 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1141 panic("ffs_alloccgblk: can't find blk in cyl");
1145 * no blocks in the requested cylinder, so take next
1146 * available one in this cylinder group.
1148 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1151 cgp->cg_rotor = bno;
1153 blkno = fragstoblks(fs, bno);
1154 ffs_clrblock(fs, blksfree, (long)blkno);
1155 ffs_clusteracct(fs, cgp, blkno, -1);
1156 cgp->cg_cs.cs_nbfree--;
1157 fs->fs_cstotal.cs_nbfree--;
1158 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1159 cylno = cbtocylno(fs, bno);
1160 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
1161 cg_blktot(cgp)[cylno]--;
1163 blkno = cgp->cg_cgx * fs->fs_fpg + bno;
1164 if (DOINGSOFTDEP(ITOV(ip)))
1165 softdep_setup_blkmapdep(bp, fs, blkno);
1170 * Determine whether a cluster can be allocated.
1172 * We do not currently check for optimal rotational layout if there
1173 * are multiple choices in the same cylinder group. Instead we just
1174 * take the first one that we find following bpref.
1177 ffs_clusteralloc(struct inode *ip, int cg, ufs_daddr_t bpref, int len)
1182 int i, got, run, bno, bit, map;
1188 if (fs->fs_maxcluster[cg] < len)
1190 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1193 cgp = (struct cg *)bp->b_data;
1194 if (!cg_chkmagic(cgp))
1196 bp->b_xflags |= BX_BKGRDWRITE;
1198 * Check to see if a cluster of the needed size (or bigger) is
1199 * available in this cylinder group.
1201 lp = &cg_clustersum(cgp)[len];
1202 for (i = len; i <= fs->fs_contigsumsize; i++)
1205 if (i > fs->fs_contigsumsize) {
1207 * This is the first time looking for a cluster in this
1208 * cylinder group. Update the cluster summary information
1209 * to reflect the true maximum sized cluster so that
1210 * future cluster allocation requests can avoid reading
1211 * the cylinder group map only to find no clusters.
1213 lp = &cg_clustersum(cgp)[len - 1];
1214 for (i = len - 1; i > 0; i--)
1217 fs->fs_maxcluster[cg] = i;
1221 * Search the cluster map to find a big enough cluster.
1222 * We take the first one that we find, even if it is larger
1223 * than we need as we prefer to get one close to the previous
1224 * block allocation. We do not search before the current
1225 * preference point as we do not want to allocate a block
1226 * that is allocated before the previous one (as we will
1227 * then have to wait for another pass of the elevator
1228 * algorithm before it will be read). We prefer to fail and
1229 * be recalled to try an allocation in the next cylinder group.
1231 if (dtog(fs, bpref) != cg)
1234 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1235 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1237 bit = 1 << (bpref % NBBY);
1238 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1239 if ((map & bit) == 0) {
1246 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1253 if (got >= cgp->cg_nclusterblks)
1256 * Allocate the cluster that we have found.
1258 blksfree = cg_blksfree(cgp);
1259 for (i = 1; i <= len; i++)
1260 if (!ffs_isblock(fs, blksfree, got - run + i))
1261 panic("ffs_clusteralloc: map mismatch");
1262 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1263 if (dtog(fs, bno) != cg)
1264 panic("ffs_clusteralloc: allocated out of group");
1265 len = blkstofrags(fs, len);
1266 for (i = 0; i < len; i += fs->fs_frag)
1267 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1268 panic("ffs_clusteralloc: lost block");
1278 * Determine whether an inode can be allocated.
1280 * Check to see if an inode is available, and if it is,
1281 * allocate it using the following policy:
1282 * 1) allocate the requested inode.
1283 * 2) allocate the next available inode after the requested
1284 * inode in the specified cylinder group.
1287 ffs_nodealloccg(struct inode *ip, int cg, ufs_daddr_t ipref, int mode)
1293 int error, start, len, loc, map, i;
1296 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1298 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1299 (int)fs->fs_cgsize, &bp);
1304 cgp = (struct cg *)bp->b_data;
1305 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1309 bp->b_xflags |= BX_BKGRDWRITE;
1310 cgp->cg_time = time_second;
1311 inosused = cg_inosused(cgp);
1313 ipref %= fs->fs_ipg;
1314 if (isclr(inosused, ipref))
1317 start = cgp->cg_irotor / NBBY;
1318 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1319 loc = skpc(0xff, len, &inosused[start]);
1323 loc = skpc(0xff, len, &inosused[0]);
1325 printf("cg = %d, irotor = %ld, fs = %s\n",
1326 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1327 panic("ffs_nodealloccg: map corrupted");
1331 i = start + len - loc;
1334 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1335 if ((map & i) == 0) {
1336 cgp->cg_irotor = ipref;
1340 printf("fs = %s\n", fs->fs_fsmnt);
1341 panic("ffs_nodealloccg: block not in map");
1344 if (DOINGSOFTDEP(ITOV(ip)))
1345 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1346 setbit(inosused, ipref);
1347 cgp->cg_cs.cs_nifree--;
1348 fs->fs_cstotal.cs_nifree--;
1349 fs->fs_cs(fs, cg).cs_nifree--;
1351 if ((mode & IFMT) == IFDIR) {
1352 cgp->cg_cs.cs_ndir++;
1353 fs->fs_cstotal.cs_ndir++;
1354 fs->fs_cs(fs, cg).cs_ndir++;
1357 return (cg * fs->fs_ipg + ipref);
1361 * Free a block or fragment.
1363 * The specified block or fragment is placed back in the
1364 * free map. If a fragment is deallocated, a possible
1365 * block reassembly is checked.
1368 ffs_blkfree(struct inode *ip, ufs_daddr_t bno, long size)
1374 int i, error, cg, blk, frags, bbase;
1378 VOP_FREEBLKS(ip->i_devvp, fsbtodb(fs, bno), size);
1379 if ((uint)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1380 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1381 printf("dev=%s, bno = %ld, bsize = %ld, size = %ld, fs = %s\n",
1382 devtoname(ip->i_dev), (long)bno, (long)fs->fs_bsize, size,
1384 panic("ffs_blkfree: bad size");
1387 if ((uint)bno >= fs->fs_size) {
1388 printf("bad block %ld, ino %lu\n",
1389 (long)bno, (u_long)ip->i_number);
1390 ffs_fserr(fs, ip->i_uid, "bad block");
1393 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1394 (int)fs->fs_cgsize, &bp);
1399 cgp = (struct cg *)bp->b_data;
1400 if (!cg_chkmagic(cgp)) {
1404 bp->b_xflags |= BX_BKGRDWRITE;
1405 cgp->cg_time = time_second;
1406 bno = dtogd(fs, bno);
1407 blksfree = cg_blksfree(cgp);
1408 if (size == fs->fs_bsize) {
1409 blkno = fragstoblks(fs, bno);
1410 if (!ffs_isfreeblock(fs, blksfree, blkno)) {
1411 printf("dev = %s, block = %ld, fs = %s\n",
1412 devtoname(ip->i_dev), (long)bno, fs->fs_fsmnt);
1413 panic("ffs_blkfree: freeing free block");
1415 ffs_setblock(fs, blksfree, blkno);
1416 ffs_clusteracct(fs, cgp, blkno, 1);
1417 cgp->cg_cs.cs_nbfree++;
1418 fs->fs_cstotal.cs_nbfree++;
1419 fs->fs_cs(fs, cg).cs_nbfree++;
1420 i = cbtocylno(fs, bno);
1421 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
1422 cg_blktot(cgp)[i]++;
1424 bbase = bno - fragnum(fs, bno);
1426 * decrement the counts associated with the old frags
1428 blk = blkmap(fs, blksfree, bbase);
1429 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1431 * deallocate the fragment
1433 frags = numfrags(fs, size);
1434 for (i = 0; i < frags; i++) {
1435 if (isset(blksfree, bno + i)) {
1436 printf("dev = %s, block = %ld, fs = %s\n",
1437 devtoname(ip->i_dev), (long)(bno + i),
1439 panic("ffs_blkfree: freeing free frag");
1441 setbit(blksfree, bno + i);
1443 cgp->cg_cs.cs_nffree += i;
1444 fs->fs_cstotal.cs_nffree += i;
1445 fs->fs_cs(fs, cg).cs_nffree += i;
1447 * add back in counts associated with the new frags
1449 blk = blkmap(fs, blksfree, bbase);
1450 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1452 * if a complete block has been reassembled, account for it
1454 blkno = fragstoblks(fs, bbase);
1455 if (ffs_isblock(fs, blksfree, blkno)) {
1456 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1457 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1458 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1459 ffs_clusteracct(fs, cgp, blkno, 1);
1460 cgp->cg_cs.cs_nbfree++;
1461 fs->fs_cstotal.cs_nbfree++;
1462 fs->fs_cs(fs, cg).cs_nbfree++;
1463 i = cbtocylno(fs, bbase);
1464 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
1465 cg_blktot(cgp)[i]++;
1474 * Verify allocation of a block or fragment. Returns true if block or
1475 * fragment is allocated, false if it is free.
1478 ffs_checkblk(struct inode *ip, ufs_daddr_t bno, long size)
1483 int i, error, frags, free;
1487 if ((uint)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1488 printf("bsize = %ld, size = %ld, fs = %s\n",
1489 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1490 panic("ffs_checkblk: bad size");
1492 if ((uint)bno >= fs->fs_size)
1493 panic("ffs_checkblk: bad block %d", bno);
1494 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1495 (int)fs->fs_cgsize, &bp);
1497 panic("ffs_checkblk: cg bread failed");
1498 cgp = (struct cg *)bp->b_data;
1499 if (!cg_chkmagic(cgp))
1500 panic("ffs_checkblk: cg magic mismatch");
1501 bp->b_xflags |= BX_BKGRDWRITE;
1502 blksfree = cg_blksfree(cgp);
1503 bno = dtogd(fs, bno);
1504 if (size == fs->fs_bsize) {
1505 free = ffs_isblock(fs, blksfree, fragstoblks(fs, bno));
1507 frags = numfrags(fs, size);
1508 for (free = 0, i = 0; i < frags; i++)
1509 if (isset(blksfree, bno + i))
1511 if (free != 0 && free != frags)
1512 panic("ffs_checkblk: partially free fragment");
1517 #endif /* DIAGNOSTIC */
1523 ffs_vfree(struct vnode *pvp, ino_t ino, int mode)
1525 if (DOINGSOFTDEP(pvp)) {
1526 softdep_freefile(pvp, ino, mode);
1529 return (ffs_freefile(pvp, ino, mode));
1533 * Do the actual free operation.
1534 * The specified inode is placed back in the free map.
1537 ffs_freefile(struct vnode *pvp, ino_t ino, int mode)
1548 if ((uint)ino >= fs->fs_ipg * fs->fs_ncg)
1549 panic("ffs_vfree: range: dev = (%d,%d), ino = %d, fs = %s",
1550 major(pip->i_dev), minor(pip->i_dev), ino, fs->fs_fsmnt);
1551 cg = ino_to_cg(fs, ino);
1552 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1553 (int)fs->fs_cgsize, &bp);
1558 cgp = (struct cg *)bp->b_data;
1559 if (!cg_chkmagic(cgp)) {
1563 bp->b_xflags |= BX_BKGRDWRITE;
1564 cgp->cg_time = time_second;
1565 inosused = cg_inosused(cgp);
1567 if (isclr(inosused, ino)) {
1568 printf("dev = %s, ino = %lu, fs = %s\n",
1569 devtoname(pip->i_dev), (u_long)ino, fs->fs_fsmnt);
1570 if (fs->fs_ronly == 0)
1571 panic("ffs_vfree: freeing free inode");
1573 clrbit(inosused, ino);
1574 if (ino < cgp->cg_irotor)
1575 cgp->cg_irotor = ino;
1576 cgp->cg_cs.cs_nifree++;
1577 fs->fs_cstotal.cs_nifree++;
1578 fs->fs_cs(fs, cg).cs_nifree++;
1579 if ((mode & IFMT) == IFDIR) {
1580 cgp->cg_cs.cs_ndir--;
1581 fs->fs_cstotal.cs_ndir--;
1582 fs->fs_cs(fs, cg).cs_ndir--;
1590 * Find a block of the specified size in the specified cylinder group.
1592 * It is a panic if a request is made to find a block if none are
1596 ffs_mapsearch(struct fs *fs, struct cg *cgp, ufs_daddr_t bpref, int allocsiz)
1599 int start, len, loc, i;
1600 int blk, field, subfield, pos;
1604 * find the fragment by searching through the free block
1605 * map for an appropriate bit pattern
1608 start = dtogd(fs, bpref) / NBBY;
1610 start = cgp->cg_frotor / NBBY;
1611 blksfree = cg_blksfree(cgp);
1612 len = howmany(fs->fs_fpg, NBBY) - start;
1613 loc = scanc((uint)len, (u_char *)&blksfree[start],
1614 (u_char *)fragtbl[fs->fs_frag],
1615 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1619 loc = scanc((uint)len, (u_char *)&blksfree[0],
1620 (u_char *)fragtbl[fs->fs_frag],
1621 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1623 printf("start = %d, len = %d, fs = %s\n",
1624 start, len, fs->fs_fsmnt);
1625 panic("ffs_alloccg: map corrupted");
1629 bno = (start + len - loc) * NBBY;
1630 cgp->cg_frotor = bno;
1632 * found the byte in the map
1633 * sift through the bits to find the selected frag
1635 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1636 blk = blkmap(fs, blksfree, bno);
1638 field = around[allocsiz];
1639 subfield = inside[allocsiz];
1640 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1641 if ((blk & field) == subfield)
1647 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
1648 panic("ffs_alloccg: block not in map");
1653 * Update the cluster map because of an allocation or free.
1655 * Cnt == 1 means free; cnt == -1 means allocating.
1658 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs_daddr_t blkno, int cnt)
1662 u_char *freemapp, *mapp;
1663 int i, start, end, forw, back, map, bit;
1665 if (fs->fs_contigsumsize <= 0)
1667 freemapp = cg_clustersfree(cgp);
1668 sump = cg_clustersum(cgp);
1670 * Allocate or clear the actual block.
1673 setbit(freemapp, blkno);
1675 clrbit(freemapp, blkno);
1677 * Find the size of the cluster going forward.
1680 end = start + fs->fs_contigsumsize;
1681 if (end >= cgp->cg_nclusterblks)
1682 end = cgp->cg_nclusterblks;
1683 mapp = &freemapp[start / NBBY];
1685 bit = 1 << (start % NBBY);
1686 for (i = start; i < end; i++) {
1687 if ((map & bit) == 0)
1689 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1698 * Find the size of the cluster going backward.
1701 end = start - fs->fs_contigsumsize;
1704 mapp = &freemapp[start / NBBY];
1706 bit = 1 << (start % NBBY);
1707 for (i = start; i > end; i--) {
1708 if ((map & bit) == 0)
1710 if ((i & (NBBY - 1)) != 0) {
1714 bit = 1 << (NBBY - 1);
1719 * Account for old cluster and the possibly new forward and
1722 i = back + forw + 1;
1723 if (i > fs->fs_contigsumsize)
1724 i = fs->fs_contigsumsize;
1731 * Update cluster summary information.
1733 lp = &sump[fs->fs_contigsumsize];
1734 for (i = fs->fs_contigsumsize; i > 0; i--)
1737 fs->fs_maxcluster[cgp->cg_cgx] = i;
1741 * Fserr prints the name of a filesystem with an error diagnostic.
1743 * The form of the error message is:
1747 ffs_fserr(struct fs *fs, uint uid, char *cp)
1749 struct thread *td = curthread;
1752 if ((p = td->td_proc) != NULL) {
1753 log(LOG_ERR, "pid %d (%s), uid %d on %s: %s\n", p ? p->p_pid : -1,
1754 p ? p->p_comm : "-", uid, fs->fs_fsmnt, cp);
1756 log(LOG_ERR, "system thread %p, uid %d on %s: %s\n",
1757 td, uid, fs->fs_fsmnt, cp);