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.12 2005/01/20 18:08:54 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.
1285 * 3) the inode must not already be in the inode hash table. We
1286 * can encounter such a case because the vnode reclamation sequence
1288 * 3) the inode must not already be in the inode hash, otherwise it
1289 * may be in the process of being deallocated. This can occur
1290 * because the bitmap is updated before the inode is removed from
1291 * hash. If we were to reallocate the inode the caller could wind
1292 * up returning a vnode/inode combination which is in an indeterminate
1296 ffs_nodealloccg(struct inode *ip, int cg, ufs_daddr_t ipref, int mode)
1302 int error, len, map, i;
1307 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1309 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1310 (int)fs->fs_cgsize, &bp);
1315 cgp = (struct cg *)bp->b_data;
1316 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1320 inosused = cg_inosused(cgp);
1324 * Quick check, reuse the most recently free inode or continue
1325 * a scan from where we left off the last time.
1327 ibase = cg * fs->fs_ipg;
1329 ipref %= fs->fs_ipg;
1330 if (isclr(inosused, ipref)) {
1331 if (ufs_ihashcheck(ip->i_dev, ibase + ipref) == 0)
1337 * Scan the inode bitmap starting at irotor, be sure to handle
1338 * the edge case by going back to the beginning of the array.
1339 * Note that 'len' can go negative. Start the scan at bit 0 to
1340 * simplify the code.
1342 ipref = (cgp->cg_irotor % fs->fs_ipg) & ~7;
1343 map = inosused[ipref >> 3];
1347 if (map != (1 << NBBY) - 1) {
1348 for (i = 0; i < NBBY; ++i) {
1350 * If we find a free bit we have to make sure
1351 * that the inode is not in the middle of
1352 * being destroyed. The inode should not exist
1353 * in the inode hash.
1355 * Adjust the rotor to try to hit the
1356 * quick-check up above.
1358 if ((map & (1 << i)) == 0) {
1359 KKASSERT(ipref + i < fs->fs_ipg);
1360 if (ufs_ihashcheck(ip->i_dev, ibase + ipref + i) == 0) {
1362 cgp->cg_irotor = (ipref + 1) % fs->fs_ipg;
1371 * Setup for the next byte. If we hit the edge make sure to
1372 * adjust the remaining length only by the number of bits in
1376 if (ipref >= fs->fs_ipg) {
1378 len -= fs->fs_ipg & 7;
1382 map = inosused[ipref >> 3];
1384 if (icheckmiss == cgp->cg_cs.cs_nifree) {
1388 printf("fs = %s\n", fs->fs_fsmnt);
1389 panic("ffs_nodealloccg: block not in map, icheckmiss/nfree %d/%d",
1390 icheckmiss, cgp->cg_cs.cs_nifree);
1394 printf("Warning: inode free race avoided %d times\n",
1397 bp->b_xflags |= BX_BKGRDWRITE;
1398 cgp->cg_time = time_second;
1399 if (DOINGSOFTDEP(ITOV(ip)))
1400 softdep_setup_inomapdep(bp, ip, ibase + ipref);
1401 setbit(inosused, ipref);
1402 cgp->cg_cs.cs_nifree--;
1403 fs->fs_cstotal.cs_nifree--;
1404 fs->fs_cs(fs, cg).cs_nifree--;
1406 if ((mode & IFMT) == IFDIR) {
1407 cgp->cg_cs.cs_ndir++;
1408 fs->fs_cstotal.cs_ndir++;
1409 fs->fs_cs(fs, cg).cs_ndir++;
1412 return (ibase + ipref);
1416 * Free a block or fragment.
1418 * The specified block or fragment is placed back in the
1419 * free map. If a fragment is deallocated, a possible
1420 * block reassembly is checked.
1423 ffs_blkfree(struct inode *ip, ufs_daddr_t bno, long size)
1429 int i, error, cg, blk, frags, bbase;
1433 VOP_FREEBLKS(ip->i_devvp, fsbtodb(fs, bno), size);
1434 if ((uint)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1435 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1436 printf("dev=%s, bno = %ld, bsize = %ld, size = %ld, fs = %s\n",
1437 devtoname(ip->i_dev), (long)bno, (long)fs->fs_bsize, size,
1439 panic("ffs_blkfree: bad size");
1442 if ((uint)bno >= fs->fs_size) {
1443 printf("bad block %ld, ino %lu\n",
1444 (long)bno, (u_long)ip->i_number);
1445 ffs_fserr(fs, ip->i_uid, "bad block");
1448 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1449 (int)fs->fs_cgsize, &bp);
1454 cgp = (struct cg *)bp->b_data;
1455 if (!cg_chkmagic(cgp)) {
1459 bp->b_xflags |= BX_BKGRDWRITE;
1460 cgp->cg_time = time_second;
1461 bno = dtogd(fs, bno);
1462 blksfree = cg_blksfree(cgp);
1463 if (size == fs->fs_bsize) {
1464 blkno = fragstoblks(fs, bno);
1465 if (!ffs_isfreeblock(fs, blksfree, blkno)) {
1466 printf("dev = %s, block = %ld, fs = %s\n",
1467 devtoname(ip->i_dev), (long)bno, fs->fs_fsmnt);
1468 panic("ffs_blkfree: freeing free block");
1470 ffs_setblock(fs, blksfree, blkno);
1471 ffs_clusteracct(fs, cgp, blkno, 1);
1472 cgp->cg_cs.cs_nbfree++;
1473 fs->fs_cstotal.cs_nbfree++;
1474 fs->fs_cs(fs, cg).cs_nbfree++;
1475 i = cbtocylno(fs, bno);
1476 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
1477 cg_blktot(cgp)[i]++;
1479 bbase = bno - fragnum(fs, bno);
1481 * decrement the counts associated with the old frags
1483 blk = blkmap(fs, blksfree, bbase);
1484 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1486 * deallocate the fragment
1488 frags = numfrags(fs, size);
1489 for (i = 0; i < frags; i++) {
1490 if (isset(blksfree, bno + i)) {
1491 printf("dev = %s, block = %ld, fs = %s\n",
1492 devtoname(ip->i_dev), (long)(bno + i),
1494 panic("ffs_blkfree: freeing free frag");
1496 setbit(blksfree, bno + i);
1498 cgp->cg_cs.cs_nffree += i;
1499 fs->fs_cstotal.cs_nffree += i;
1500 fs->fs_cs(fs, cg).cs_nffree += i;
1502 * add back in counts associated with the new frags
1504 blk = blkmap(fs, blksfree, bbase);
1505 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1507 * if a complete block has been reassembled, account for it
1509 blkno = fragstoblks(fs, bbase);
1510 if (ffs_isblock(fs, blksfree, blkno)) {
1511 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1512 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1513 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1514 ffs_clusteracct(fs, cgp, blkno, 1);
1515 cgp->cg_cs.cs_nbfree++;
1516 fs->fs_cstotal.cs_nbfree++;
1517 fs->fs_cs(fs, cg).cs_nbfree++;
1518 i = cbtocylno(fs, bbase);
1519 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
1520 cg_blktot(cgp)[i]++;
1529 * Verify allocation of a block or fragment. Returns true if block or
1530 * fragment is allocated, false if it is free.
1533 ffs_checkblk(struct inode *ip, ufs_daddr_t bno, long size)
1538 int i, error, frags, free;
1542 if ((uint)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1543 printf("bsize = %ld, size = %ld, fs = %s\n",
1544 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1545 panic("ffs_checkblk: bad size");
1547 if ((uint)bno >= fs->fs_size)
1548 panic("ffs_checkblk: bad block %d", bno);
1549 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1550 (int)fs->fs_cgsize, &bp);
1552 panic("ffs_checkblk: cg bread failed");
1553 cgp = (struct cg *)bp->b_data;
1554 if (!cg_chkmagic(cgp))
1555 panic("ffs_checkblk: cg magic mismatch");
1556 bp->b_xflags |= BX_BKGRDWRITE;
1557 blksfree = cg_blksfree(cgp);
1558 bno = dtogd(fs, bno);
1559 if (size == fs->fs_bsize) {
1560 free = ffs_isblock(fs, blksfree, fragstoblks(fs, bno));
1562 frags = numfrags(fs, size);
1563 for (free = 0, i = 0; i < frags; i++)
1564 if (isset(blksfree, bno + i))
1566 if (free != 0 && free != frags)
1567 panic("ffs_checkblk: partially free fragment");
1572 #endif /* DIAGNOSTIC */
1578 ffs_vfree(struct vnode *pvp, ino_t ino, int mode)
1580 if (DOINGSOFTDEP(pvp)) {
1581 softdep_freefile(pvp, ino, mode);
1584 return (ffs_freefile(pvp, ino, mode));
1588 * Do the actual free operation.
1589 * The specified inode is placed back in the free map.
1592 ffs_freefile(struct vnode *pvp, ino_t ino, int mode)
1603 if ((uint)ino >= fs->fs_ipg * fs->fs_ncg)
1604 panic("ffs_vfree: range: dev = (%d,%d), ino = %d, fs = %s",
1605 major(pip->i_dev), minor(pip->i_dev), ino, fs->fs_fsmnt);
1606 cg = ino_to_cg(fs, ino);
1607 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1608 (int)fs->fs_cgsize, &bp);
1613 cgp = (struct cg *)bp->b_data;
1614 if (!cg_chkmagic(cgp)) {
1618 bp->b_xflags |= BX_BKGRDWRITE;
1619 cgp->cg_time = time_second;
1620 inosused = cg_inosused(cgp);
1622 if (isclr(inosused, ino)) {
1623 printf("dev = %s, ino = %lu, fs = %s\n",
1624 devtoname(pip->i_dev), (u_long)ino, fs->fs_fsmnt);
1625 if (fs->fs_ronly == 0)
1626 panic("ffs_vfree: freeing free inode");
1628 clrbit(inosused, ino);
1629 if (ino < cgp->cg_irotor)
1630 cgp->cg_irotor = ino;
1631 cgp->cg_cs.cs_nifree++;
1632 fs->fs_cstotal.cs_nifree++;
1633 fs->fs_cs(fs, cg).cs_nifree++;
1634 if ((mode & IFMT) == IFDIR) {
1635 cgp->cg_cs.cs_ndir--;
1636 fs->fs_cstotal.cs_ndir--;
1637 fs->fs_cs(fs, cg).cs_ndir--;
1645 * Find a block of the specified size in the specified cylinder group.
1647 * It is a panic if a request is made to find a block if none are
1651 ffs_mapsearch(struct fs *fs, struct cg *cgp, ufs_daddr_t bpref, int allocsiz)
1654 int start, len, loc, i;
1655 int blk, field, subfield, pos;
1659 * find the fragment by searching through the free block
1660 * map for an appropriate bit pattern
1663 start = dtogd(fs, bpref) / NBBY;
1665 start = cgp->cg_frotor / NBBY;
1666 blksfree = cg_blksfree(cgp);
1667 len = howmany(fs->fs_fpg, NBBY) - start;
1668 loc = scanc((uint)len, (u_char *)&blksfree[start],
1669 (u_char *)fragtbl[fs->fs_frag],
1670 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1674 loc = scanc((uint)len, (u_char *)&blksfree[0],
1675 (u_char *)fragtbl[fs->fs_frag],
1676 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1678 printf("start = %d, len = %d, fs = %s\n",
1679 start, len, fs->fs_fsmnt);
1680 panic("ffs_alloccg: map corrupted");
1684 bno = (start + len - loc) * NBBY;
1685 cgp->cg_frotor = bno;
1687 * found the byte in the map
1688 * sift through the bits to find the selected frag
1690 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1691 blk = blkmap(fs, blksfree, bno);
1693 field = around[allocsiz];
1694 subfield = inside[allocsiz];
1695 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1696 if ((blk & field) == subfield)
1702 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
1703 panic("ffs_alloccg: block not in map");
1708 * Update the cluster map because of an allocation or free.
1710 * Cnt == 1 means free; cnt == -1 means allocating.
1713 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs_daddr_t blkno, int cnt)
1717 u_char *freemapp, *mapp;
1718 int i, start, end, forw, back, map, bit;
1720 if (fs->fs_contigsumsize <= 0)
1722 freemapp = cg_clustersfree(cgp);
1723 sump = cg_clustersum(cgp);
1725 * Allocate or clear the actual block.
1728 setbit(freemapp, blkno);
1730 clrbit(freemapp, blkno);
1732 * Find the size of the cluster going forward.
1735 end = start + fs->fs_contigsumsize;
1736 if (end >= cgp->cg_nclusterblks)
1737 end = cgp->cg_nclusterblks;
1738 mapp = &freemapp[start / NBBY];
1740 bit = 1 << (start % NBBY);
1741 for (i = start; i < end; i++) {
1742 if ((map & bit) == 0)
1744 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1753 * Find the size of the cluster going backward.
1756 end = start - fs->fs_contigsumsize;
1759 mapp = &freemapp[start / NBBY];
1761 bit = 1 << (start % NBBY);
1762 for (i = start; i > end; i--) {
1763 if ((map & bit) == 0)
1765 if ((i & (NBBY - 1)) != 0) {
1769 bit = 1 << (NBBY - 1);
1774 * Account for old cluster and the possibly new forward and
1777 i = back + forw + 1;
1778 if (i > fs->fs_contigsumsize)
1779 i = fs->fs_contigsumsize;
1786 * Update cluster summary information.
1788 lp = &sump[fs->fs_contigsumsize];
1789 for (i = fs->fs_contigsumsize; i > 0; i--)
1792 fs->fs_maxcluster[cgp->cg_cgx] = i;
1796 * Fserr prints the name of a filesystem with an error diagnostic.
1798 * The form of the error message is:
1802 ffs_fserr(struct fs *fs, uint uid, char *cp)
1804 struct thread *td = curthread;
1807 if ((p = td->td_proc) != NULL) {
1808 log(LOG_ERR, "pid %d (%s), uid %d on %s: %s\n", p ? p->p_pid : -1,
1809 p ? p->p_comm : "-", uid, fs->fs_fsmnt, cp);
1811 log(LOG_ERR, "system thread %p, uid %d on %s: %s\n",
1812 td, uid, fs->fs_fsmnt, cp);