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.10 2004/07/18 19:43:48 drhodus 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;
645 int avgifree, avgbfree, avgndir, curdirsize;
646 int minifree, minbfree, maxndir;
652 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
653 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
654 avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
657 * Force allocation in another cg if creating a first level dir.
659 if (ITOV(pip)->v_flag & VROOT) {
660 prefcg = arc4random() % fs->fs_ncg;
662 minndir = fs->fs_ipg;
663 for (cg = prefcg; cg < fs->fs_ncg; cg++)
664 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
665 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
666 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
668 minndir = fs->fs_cs(fs, cg).cs_ndir;
670 for (cg = 0; cg < prefcg; cg++)
671 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
672 fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
673 fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
675 minndir = fs->fs_cs(fs, cg).cs_ndir;
677 return ((ino_t)(fs->fs_ipg * mincg));
681 * Count various limits which used for
682 * optimal allocation of a directory inode.
684 maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
685 minifree = avgifree - avgifree / 4;
688 minbfree = avgbfree - avgbfree / 4;
691 cgsize = fs->fs_fsize * fs->fs_fpg;
692 dirsize = fs->fs_avgfilesize * fs->fs_avgfpdir;
693 curdirsize = avgndir ? (cgsize - avgbfree * fs->fs_bsize) / avgndir : 0;
694 if (dirsize < curdirsize)
695 dirsize = curdirsize;
696 maxcontigdirs = min((avgbfree * fs->fs_bsize) / dirsize, 255);
697 if (fs->fs_avgfpdir > 0)
698 maxcontigdirs = min(maxcontigdirs,
699 fs->fs_ipg / fs->fs_avgfpdir);
700 if (maxcontigdirs == 0)
704 * Limit number of dirs in one cg and reserve space for
705 * regular files, but only if we have no deficit in
708 prefcg = ino_to_cg(fs, pip->i_number);
709 for (cg = prefcg; cg < fs->fs_ncg; cg++)
710 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
711 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
712 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
713 if (fs->fs_contigdirs[cg] < maxcontigdirs)
714 return ((ino_t)(fs->fs_ipg * cg));
716 for (cg = 0; cg < prefcg; cg++)
717 if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
718 fs->fs_cs(fs, cg).cs_nifree >= minifree &&
719 fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
720 if (fs->fs_contigdirs[cg] < maxcontigdirs)
721 return ((ino_t)(fs->fs_ipg * cg));
724 * This is a backstop when we have deficit in space.
726 for (cg = prefcg; cg < fs->fs_ncg; cg++)
727 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
728 return ((ino_t)(fs->fs_ipg * cg));
729 for (cg = 0; cg < prefcg; cg++)
730 if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
732 return ((ino_t)(fs->fs_ipg * cg));
736 * Select the desired position for the next block in a file. The file is
737 * logically divided into sections. The first section is composed of the
738 * direct blocks. Each additional section contains fs_maxbpg blocks.
740 * If no blocks have been allocated in the first section, the policy is to
741 * request a block in the same cylinder group as the inode that describes
742 * the file. If no blocks have been allocated in any other section, the
743 * policy is to place the section in a cylinder group with a greater than
744 * average number of free blocks. An appropriate cylinder group is found
745 * by using a rotor that sweeps the cylinder groups. When a new group of
746 * blocks is needed, the sweep begins in the cylinder group following the
747 * cylinder group from which the previous allocation was made. The sweep
748 * continues until a cylinder group with greater than the average number
749 * of free blocks is found. If the allocation is for the first block in an
750 * indirect block, the information on the previous allocation is unavailable;
751 * here a best guess is made based upon the logical block number being
754 * If a section is already partially allocated, the policy is to
755 * contiguously allocate fs_maxcontig blocks. The end of one of these
756 * contiguous blocks and the beginning of the next is physically separated
757 * so that the disk head will be in transit between them for at least
758 * fs_rotdelay milliseconds. This is to allow time for the processor to
759 * schedule another I/O transfer.
762 ffs_blkpref(struct inode *ip, ufs_daddr_t lbn, int indx, ufs_daddr_t *bap)
766 int avgbfree, startcg;
770 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
771 if (lbn < NDADDR + NINDIR(fs)) {
772 cg = ino_to_cg(fs, ip->i_number);
773 return (fs->fs_fpg * cg + fs->fs_frag);
776 * Find a cylinder with greater than average number of
777 * unused data blocks.
779 if (indx == 0 || bap[indx - 1] == 0)
781 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
783 startcg = dtog(fs, bap[indx - 1]) + 1;
784 startcg %= fs->fs_ncg;
785 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
786 for (cg = startcg; cg < fs->fs_ncg; cg++)
787 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
789 return (fs->fs_fpg * cg + fs->fs_frag);
791 for (cg = 0; cg <= startcg; cg++)
792 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
794 return (fs->fs_fpg * cg + fs->fs_frag);
799 * One or more previous blocks have been laid out. If less
800 * than fs_maxcontig previous blocks are contiguous, the
801 * next block is requested contiguously, otherwise it is
802 * requested rotationally delayed by fs_rotdelay milliseconds.
804 nextblk = bap[indx - 1] + fs->fs_frag;
805 if (fs->fs_rotdelay == 0 || indx < fs->fs_maxcontig ||
806 bap[indx - fs->fs_maxcontig] +
807 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
810 * Here we convert ms of delay to frags as:
811 * (frags) = (ms) * (rev/sec) * (sect/rev) /
812 * ((sect/frag) * (ms/sec))
813 * then round up to the next block.
815 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
816 (NSPF(fs) * 1000), fs->fs_frag);
821 * Implement the cylinder overflow algorithm.
823 * The policy implemented by this algorithm is:
824 * 1) allocate the block in its requested cylinder group.
825 * 2) quadradically rehash on the cylinder group number.
826 * 3) brute force search for a free block.
830 ffs_hashalloc(struct inode *ip, int cg, long pref,
831 int size, /* size for data blocks, mode for inodes */
832 allocfcn_t *allocator)
835 long result; /* XXX why not same type as we return? */
840 * 1: preferred cylinder group
842 result = (*allocator)(ip, cg, pref, size);
846 * 2: quadratic rehash
848 for (i = 1; i < fs->fs_ncg; i *= 2) {
850 if (cg >= fs->fs_ncg)
852 result = (*allocator)(ip, cg, 0, size);
857 * 3: brute force search
858 * Note that we start at i == 2, since 0 was checked initially,
859 * and 1 is always checked in the quadratic rehash.
861 cg = (icg + 2) % fs->fs_ncg;
862 for (i = 2; i < fs->fs_ncg; i++) {
863 result = (*allocator)(ip, cg, 0, size);
867 if (cg == fs->fs_ncg)
874 * Determine whether a fragment can be extended.
876 * Check to see if the necessary fragments are available, and
877 * if they are, allocate them.
880 ffs_fragextend(struct inode *ip, int cg, long bprev, int osize, int nsize)
891 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
893 frags = numfrags(fs, nsize);
894 bbase = fragnum(fs, bprev);
895 if (bbase > fragnum(fs, (bprev + frags - 1))) {
896 /* cannot extend across a block boundary */
899 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
900 (int)fs->fs_cgsize, &bp);
905 cgp = (struct cg *)bp->b_data;
906 if (!cg_chkmagic(cgp)) {
910 bp->b_xflags |= BX_BKGRDWRITE;
911 cgp->cg_time = time_second;
912 bno = dtogd(fs, bprev);
913 blksfree = cg_blksfree(cgp);
914 for (i = numfrags(fs, osize); i < frags; i++)
915 if (isclr(blksfree, bno + i)) {
920 * the current fragment can be extended
921 * deduct the count on fragment being extended into
922 * increase the count on the remaining fragment (if any)
923 * allocate the extended piece
925 for (i = frags; i < fs->fs_frag - bbase; i++)
926 if (isclr(blksfree, bno + i))
928 cgp->cg_frsum[i - numfrags(fs, osize)]--;
930 cgp->cg_frsum[i - frags]++;
931 for (i = numfrags(fs, osize); i < frags; i++) {
932 clrbit(blksfree, bno + i);
933 cgp->cg_cs.cs_nffree--;
934 fs->fs_cstotal.cs_nffree--;
935 fs->fs_cs(fs, cg).cs_nffree--;
938 if (DOINGSOFTDEP(ITOV(ip)))
939 softdep_setup_blkmapdep(bp, fs, bprev);
945 * Determine whether a block can be allocated.
947 * Check to see if a block of the appropriate size is available,
948 * and if it is, allocate it.
951 ffs_alloccg(struct inode *ip, int cg, ufs_daddr_t bpref, int size)
957 ufs_daddr_t bno, blkno;
958 int allocsiz, error, frags;
962 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
964 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
965 (int)fs->fs_cgsize, &bp);
970 cgp = (struct cg *)bp->b_data;
971 if (!cg_chkmagic(cgp) ||
972 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
976 bp->b_xflags |= BX_BKGRDWRITE;
977 cgp->cg_time = time_second;
978 if (size == fs->fs_bsize) {
979 bno = ffs_alloccgblk(ip, bp, bpref);
984 * check to see if any fragments are already available
985 * allocsiz is the size which will be allocated, hacking
986 * it down to a smaller size if necessary
988 blksfree = cg_blksfree(cgp);
989 frags = numfrags(fs, size);
990 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
991 if (cgp->cg_frsum[allocsiz] != 0)
993 if (allocsiz == fs->fs_frag) {
995 * no fragments were available, so a block will be
996 * allocated, and hacked up
998 if (cgp->cg_cs.cs_nbfree == 0) {
1002 bno = ffs_alloccgblk(ip, bp, bpref);
1003 bpref = dtogd(fs, bno);
1004 for (i = frags; i < fs->fs_frag; i++)
1005 setbit(blksfree, bpref + i);
1006 i = fs->fs_frag - frags;
1007 cgp->cg_cs.cs_nffree += i;
1008 fs->fs_cstotal.cs_nffree += i;
1009 fs->fs_cs(fs, cg).cs_nffree += i;
1015 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
1020 for (i = 0; i < frags; i++)
1021 clrbit(blksfree, bno + i);
1022 cgp->cg_cs.cs_nffree -= frags;
1023 fs->fs_cstotal.cs_nffree -= frags;
1024 fs->fs_cs(fs, cg).cs_nffree -= frags;
1026 cgp->cg_frsum[allocsiz]--;
1027 if (frags != allocsiz)
1028 cgp->cg_frsum[allocsiz - frags]++;
1029 blkno = cg * fs->fs_fpg + bno;
1030 if (DOINGSOFTDEP(ITOV(ip)))
1031 softdep_setup_blkmapdep(bp, fs, blkno);
1033 return ((u_long)blkno);
1037 * Allocate a block in a cylinder group.
1039 * This algorithm implements the following policy:
1040 * 1) allocate the requested block.
1041 * 2) allocate a rotationally optimal block in the same cylinder.
1042 * 3) allocate the next available block on the block rotor for the
1043 * specified cylinder group.
1044 * Note that this routine only allocates fs_bsize blocks; these
1045 * blocks may be fragmented by the routine that allocates them.
1048 ffs_alloccgblk(struct inode *ip, struct buf *bp, ufs_daddr_t bpref)
1052 ufs_daddr_t bno, blkno;
1053 int cylno, pos, delta;
1059 cgp = (struct cg *)bp->b_data;
1060 blksfree = cg_blksfree(cgp);
1061 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
1062 bpref = cgp->cg_rotor;
1065 bpref = blknum(fs, bpref);
1066 bpref = dtogd(fs, bpref);
1068 * if the requested block is available, use it
1070 if (ffs_isblock(fs, blksfree, fragstoblks(fs, bpref))) {
1074 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1076 * Block layout information is not available.
1077 * Leaving bpref unchanged means we take the
1078 * next available free block following the one
1079 * we just allocated. Hopefully this will at
1080 * least hit a track cache on drives of unknown
1081 * geometry (e.g. SCSI).
1086 * check for a block available on the same cylinder
1088 cylno = cbtocylno(fs, bpref);
1089 if (cg_blktot(cgp)[cylno] == 0)
1092 * check the summary information to see if a block is
1093 * available in the requested cylinder starting at the
1094 * requested rotational position and proceeding around.
1096 cylbp = cg_blks(fs, cgp, cylno);
1097 pos = cbtorpos(fs, bpref);
1098 for (i = pos; i < fs->fs_nrpos; i++)
1101 if (i == fs->fs_nrpos)
1102 for (i = 0; i < pos; i++)
1107 * found a rotational position, now find the actual
1108 * block. A panic if none is actually there.
1110 pos = cylno % fs->fs_cpc;
1111 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1112 if (fs_postbl(fs, pos)[i] == -1) {
1113 printf("pos = %d, i = %d, fs = %s\n",
1114 pos, i, fs->fs_fsmnt);
1115 panic("ffs_alloccgblk: cyl groups corrupted");
1117 for (i = fs_postbl(fs, pos)[i];; ) {
1118 if (ffs_isblock(fs, blksfree, bno + i)) {
1119 bno = blkstofrags(fs, (bno + i));
1122 delta = fs_rotbl(fs)[i];
1124 delta + i > fragstoblks(fs, fs->fs_fpg))
1128 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1129 panic("ffs_alloccgblk: can't find blk in cyl");
1133 * no blocks in the requested cylinder, so take next
1134 * available one in this cylinder group.
1136 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1139 cgp->cg_rotor = bno;
1141 blkno = fragstoblks(fs, bno);
1142 ffs_clrblock(fs, blksfree, (long)blkno);
1143 ffs_clusteracct(fs, cgp, blkno, -1);
1144 cgp->cg_cs.cs_nbfree--;
1145 fs->fs_cstotal.cs_nbfree--;
1146 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1147 cylno = cbtocylno(fs, bno);
1148 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
1149 cg_blktot(cgp)[cylno]--;
1151 blkno = cgp->cg_cgx * fs->fs_fpg + bno;
1152 if (DOINGSOFTDEP(ITOV(ip)))
1153 softdep_setup_blkmapdep(bp, fs, blkno);
1158 * Determine whether a cluster can be allocated.
1160 * We do not currently check for optimal rotational layout if there
1161 * are multiple choices in the same cylinder group. Instead we just
1162 * take the first one that we find following bpref.
1165 ffs_clusteralloc(struct inode *ip, int cg, ufs_daddr_t bpref, int len)
1170 int i, got, run, bno, bit, map;
1176 if (fs->fs_maxcluster[cg] < len)
1178 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1181 cgp = (struct cg *)bp->b_data;
1182 if (!cg_chkmagic(cgp))
1184 bp->b_xflags |= BX_BKGRDWRITE;
1186 * Check to see if a cluster of the needed size (or bigger) is
1187 * available in this cylinder group.
1189 lp = &cg_clustersum(cgp)[len];
1190 for (i = len; i <= fs->fs_contigsumsize; i++)
1193 if (i > fs->fs_contigsumsize) {
1195 * This is the first time looking for a cluster in this
1196 * cylinder group. Update the cluster summary information
1197 * to reflect the true maximum sized cluster so that
1198 * future cluster allocation requests can avoid reading
1199 * the cylinder group map only to find no clusters.
1201 lp = &cg_clustersum(cgp)[len - 1];
1202 for (i = len - 1; i > 0; i--)
1205 fs->fs_maxcluster[cg] = i;
1209 * Search the cluster map to find a big enough cluster.
1210 * We take the first one that we find, even if it is larger
1211 * than we need as we prefer to get one close to the previous
1212 * block allocation. We do not search before the current
1213 * preference point as we do not want to allocate a block
1214 * that is allocated before the previous one (as we will
1215 * then have to wait for another pass of the elevator
1216 * algorithm before it will be read). We prefer to fail and
1217 * be recalled to try an allocation in the next cylinder group.
1219 if (dtog(fs, bpref) != cg)
1222 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1223 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1225 bit = 1 << (bpref % NBBY);
1226 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1227 if ((map & bit) == 0) {
1234 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1241 if (got >= cgp->cg_nclusterblks)
1244 * Allocate the cluster that we have found.
1246 blksfree = cg_blksfree(cgp);
1247 for (i = 1; i <= len; i++)
1248 if (!ffs_isblock(fs, blksfree, got - run + i))
1249 panic("ffs_clusteralloc: map mismatch");
1250 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1251 if (dtog(fs, bno) != cg)
1252 panic("ffs_clusteralloc: allocated out of group");
1253 len = blkstofrags(fs, len);
1254 for (i = 0; i < len; i += fs->fs_frag)
1255 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1256 panic("ffs_clusteralloc: lost block");
1266 * Determine whether an inode can be allocated.
1268 * Check to see if an inode is available, and if it is,
1269 * allocate it using the following policy:
1270 * 1) allocate the requested inode.
1271 * 2) allocate the next available inode after the requested
1272 * inode in the specified cylinder group.
1275 ffs_nodealloccg(struct inode *ip, int cg, ufs_daddr_t ipref, int mode)
1281 int error, start, len, loc, map, i;
1284 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1286 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1287 (int)fs->fs_cgsize, &bp);
1292 cgp = (struct cg *)bp->b_data;
1293 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1297 bp->b_xflags |= BX_BKGRDWRITE;
1298 cgp->cg_time = time_second;
1299 inosused = cg_inosused(cgp);
1301 ipref %= fs->fs_ipg;
1302 if (isclr(inosused, ipref))
1305 start = cgp->cg_irotor / NBBY;
1306 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1307 loc = skpc(0xff, len, &inosused[start]);
1311 loc = skpc(0xff, len, &inosused[0]);
1313 printf("cg = %d, irotor = %ld, fs = %s\n",
1314 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1315 panic("ffs_nodealloccg: map corrupted");
1319 i = start + len - loc;
1322 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1323 if ((map & i) == 0) {
1324 cgp->cg_irotor = ipref;
1328 printf("fs = %s\n", fs->fs_fsmnt);
1329 panic("ffs_nodealloccg: block not in map");
1332 if (DOINGSOFTDEP(ITOV(ip)))
1333 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1334 setbit(inosused, ipref);
1335 cgp->cg_cs.cs_nifree--;
1336 fs->fs_cstotal.cs_nifree--;
1337 fs->fs_cs(fs, cg).cs_nifree--;
1339 if ((mode & IFMT) == IFDIR) {
1340 cgp->cg_cs.cs_ndir++;
1341 fs->fs_cstotal.cs_ndir++;
1342 fs->fs_cs(fs, cg).cs_ndir++;
1345 return (cg * fs->fs_ipg + ipref);
1349 * Free a block or fragment.
1351 * The specified block or fragment is placed back in the
1352 * free map. If a fragment is deallocated, a possible
1353 * block reassembly is checked.
1356 ffs_blkfree(struct inode *ip, ufs_daddr_t bno, long size)
1362 int i, error, cg, blk, frags, bbase;
1366 VOP_FREEBLKS(ip->i_devvp, fsbtodb(fs, bno), size);
1367 if ((uint)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1368 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1369 printf("dev=%s, bno = %ld, bsize = %ld, size = %ld, fs = %s\n",
1370 devtoname(ip->i_dev), (long)bno, (long)fs->fs_bsize, size,
1372 panic("ffs_blkfree: bad size");
1375 if ((uint)bno >= fs->fs_size) {
1376 printf("bad block %ld, ino %lu\n",
1377 (long)bno, (u_long)ip->i_number);
1378 ffs_fserr(fs, ip->i_uid, "bad block");
1381 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1382 (int)fs->fs_cgsize, &bp);
1387 cgp = (struct cg *)bp->b_data;
1388 if (!cg_chkmagic(cgp)) {
1392 bp->b_xflags |= BX_BKGRDWRITE;
1393 cgp->cg_time = time_second;
1394 bno = dtogd(fs, bno);
1395 blksfree = cg_blksfree(cgp);
1396 if (size == fs->fs_bsize) {
1397 blkno = fragstoblks(fs, bno);
1398 if (!ffs_isfreeblock(fs, blksfree, blkno)) {
1399 printf("dev = %s, block = %ld, fs = %s\n",
1400 devtoname(ip->i_dev), (long)bno, fs->fs_fsmnt);
1401 panic("ffs_blkfree: freeing free block");
1403 ffs_setblock(fs, blksfree, blkno);
1404 ffs_clusteracct(fs, cgp, blkno, 1);
1405 cgp->cg_cs.cs_nbfree++;
1406 fs->fs_cstotal.cs_nbfree++;
1407 fs->fs_cs(fs, cg).cs_nbfree++;
1408 i = cbtocylno(fs, bno);
1409 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
1410 cg_blktot(cgp)[i]++;
1412 bbase = bno - fragnum(fs, bno);
1414 * decrement the counts associated with the old frags
1416 blk = blkmap(fs, blksfree, bbase);
1417 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1419 * deallocate the fragment
1421 frags = numfrags(fs, size);
1422 for (i = 0; i < frags; i++) {
1423 if (isset(blksfree, bno + i)) {
1424 printf("dev = %s, block = %ld, fs = %s\n",
1425 devtoname(ip->i_dev), (long)(bno + i),
1427 panic("ffs_blkfree: freeing free frag");
1429 setbit(blksfree, bno + i);
1431 cgp->cg_cs.cs_nffree += i;
1432 fs->fs_cstotal.cs_nffree += i;
1433 fs->fs_cs(fs, cg).cs_nffree += i;
1435 * add back in counts associated with the new frags
1437 blk = blkmap(fs, blksfree, bbase);
1438 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1440 * if a complete block has been reassembled, account for it
1442 blkno = fragstoblks(fs, bbase);
1443 if (ffs_isblock(fs, blksfree, blkno)) {
1444 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1445 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1446 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1447 ffs_clusteracct(fs, cgp, blkno, 1);
1448 cgp->cg_cs.cs_nbfree++;
1449 fs->fs_cstotal.cs_nbfree++;
1450 fs->fs_cs(fs, cg).cs_nbfree++;
1451 i = cbtocylno(fs, bbase);
1452 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
1453 cg_blktot(cgp)[i]++;
1462 * Verify allocation of a block or fragment. Returns true if block or
1463 * fragment is allocated, false if it is free.
1466 ffs_checkblk(struct inode *ip, ufs_daddr_t bno, long size)
1471 int i, error, frags, free;
1475 if ((uint)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1476 printf("bsize = %ld, size = %ld, fs = %s\n",
1477 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1478 panic("ffs_checkblk: bad size");
1480 if ((uint)bno >= fs->fs_size)
1481 panic("ffs_checkblk: bad block %d", bno);
1482 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1483 (int)fs->fs_cgsize, &bp);
1485 panic("ffs_checkblk: cg bread failed");
1486 cgp = (struct cg *)bp->b_data;
1487 if (!cg_chkmagic(cgp))
1488 panic("ffs_checkblk: cg magic mismatch");
1489 bp->b_xflags |= BX_BKGRDWRITE;
1490 blksfree = cg_blksfree(cgp);
1491 bno = dtogd(fs, bno);
1492 if (size == fs->fs_bsize) {
1493 free = ffs_isblock(fs, blksfree, fragstoblks(fs, bno));
1495 frags = numfrags(fs, size);
1496 for (free = 0, i = 0; i < frags; i++)
1497 if (isset(blksfree, bno + i))
1499 if (free != 0 && free != frags)
1500 panic("ffs_checkblk: partially free fragment");
1505 #endif /* DIAGNOSTIC */
1511 ffs_vfree(struct vnode *pvp, ino_t ino, int mode)
1513 if (DOINGSOFTDEP(pvp)) {
1514 softdep_freefile(pvp, ino, mode);
1517 return (ffs_freefile(pvp, ino, mode));
1521 * Do the actual free operation.
1522 * The specified inode is placed back in the free map.
1525 ffs_freefile(struct vnode *pvp, ino_t ino, int mode)
1536 if ((uint)ino >= fs->fs_ipg * fs->fs_ncg)
1537 panic("ffs_vfree: range: dev = (%d,%d), ino = %d, fs = %s",
1538 major(pip->i_dev), minor(pip->i_dev), ino, fs->fs_fsmnt);
1539 cg = ino_to_cg(fs, ino);
1540 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1541 (int)fs->fs_cgsize, &bp);
1546 cgp = (struct cg *)bp->b_data;
1547 if (!cg_chkmagic(cgp)) {
1551 bp->b_xflags |= BX_BKGRDWRITE;
1552 cgp->cg_time = time_second;
1553 inosused = cg_inosused(cgp);
1555 if (isclr(inosused, ino)) {
1556 printf("dev = %s, ino = %lu, fs = %s\n",
1557 devtoname(pip->i_dev), (u_long)ino, fs->fs_fsmnt);
1558 if (fs->fs_ronly == 0)
1559 panic("ffs_vfree: freeing free inode");
1561 clrbit(inosused, ino);
1562 if (ino < cgp->cg_irotor)
1563 cgp->cg_irotor = ino;
1564 cgp->cg_cs.cs_nifree++;
1565 fs->fs_cstotal.cs_nifree++;
1566 fs->fs_cs(fs, cg).cs_nifree++;
1567 if ((mode & IFMT) == IFDIR) {
1568 cgp->cg_cs.cs_ndir--;
1569 fs->fs_cstotal.cs_ndir--;
1570 fs->fs_cs(fs, cg).cs_ndir--;
1578 * Find a block of the specified size in the specified cylinder group.
1580 * It is a panic if a request is made to find a block if none are
1584 ffs_mapsearch(struct fs *fs, struct cg *cgp, ufs_daddr_t bpref, int allocsiz)
1587 int start, len, loc, i;
1588 int blk, field, subfield, pos;
1592 * find the fragment by searching through the free block
1593 * map for an appropriate bit pattern
1596 start = dtogd(fs, bpref) / NBBY;
1598 start = cgp->cg_frotor / NBBY;
1599 blksfree = cg_blksfree(cgp);
1600 len = howmany(fs->fs_fpg, NBBY) - start;
1601 loc = scanc((uint)len, (u_char *)&blksfree[start],
1602 (u_char *)fragtbl[fs->fs_frag],
1603 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1607 loc = scanc((uint)len, (u_char *)&blksfree[0],
1608 (u_char *)fragtbl[fs->fs_frag],
1609 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1611 printf("start = %d, len = %d, fs = %s\n",
1612 start, len, fs->fs_fsmnt);
1613 panic("ffs_alloccg: map corrupted");
1617 bno = (start + len - loc) * NBBY;
1618 cgp->cg_frotor = bno;
1620 * found the byte in the map
1621 * sift through the bits to find the selected frag
1623 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1624 blk = blkmap(fs, blksfree, bno);
1626 field = around[allocsiz];
1627 subfield = inside[allocsiz];
1628 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1629 if ((blk & field) == subfield)
1635 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
1636 panic("ffs_alloccg: block not in map");
1641 * Update the cluster map because of an allocation or free.
1643 * Cnt == 1 means free; cnt == -1 means allocating.
1646 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs_daddr_t blkno, int cnt)
1650 u_char *freemapp, *mapp;
1651 int i, start, end, forw, back, map, bit;
1653 if (fs->fs_contigsumsize <= 0)
1655 freemapp = cg_clustersfree(cgp);
1656 sump = cg_clustersum(cgp);
1658 * Allocate or clear the actual block.
1661 setbit(freemapp, blkno);
1663 clrbit(freemapp, blkno);
1665 * Find the size of the cluster going forward.
1668 end = start + fs->fs_contigsumsize;
1669 if (end >= cgp->cg_nclusterblks)
1670 end = cgp->cg_nclusterblks;
1671 mapp = &freemapp[start / NBBY];
1673 bit = 1 << (start % NBBY);
1674 for (i = start; i < end; i++) {
1675 if ((map & bit) == 0)
1677 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1686 * Find the size of the cluster going backward.
1689 end = start - fs->fs_contigsumsize;
1692 mapp = &freemapp[start / NBBY];
1694 bit = 1 << (start % NBBY);
1695 for (i = start; i > end; i--) {
1696 if ((map & bit) == 0)
1698 if ((i & (NBBY - 1)) != 0) {
1702 bit = 1 << (NBBY - 1);
1707 * Account for old cluster and the possibly new forward and
1710 i = back + forw + 1;
1711 if (i > fs->fs_contigsumsize)
1712 i = fs->fs_contigsumsize;
1719 * Update cluster summary information.
1721 lp = &sump[fs->fs_contigsumsize];
1722 for (i = fs->fs_contigsumsize; i > 0; i--)
1725 fs->fs_maxcluster[cgp->cg_cgx] = i;
1729 * Fserr prints the name of a filesystem with an error diagnostic.
1731 * The form of the error message is:
1735 ffs_fserr(struct fs *fs, uint uid, char *cp)
1737 struct thread *td = curthread;
1740 if ((p = td->td_proc) != NULL) {
1741 log(LOG_ERR, "pid %d (%s), uid %d on %s: %s\n", p ? p->p_pid : -1,
1742 p ? p->p_comm : "-", uid, fs->fs_fsmnt, cp);
1744 log(LOG_ERR, "system thread %p, uid %d on %s: %s\n",
1745 td, uid, fs->fs_fsmnt, cp);