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
37 #include "opt_quota.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
50 #include <ufs/ufs/quota.h>
51 #include <ufs/ufs/inode.h>
52 #include <ufs/ufs/ufs_extern.h>
53 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ffs/ffs_extern.h>
58 typedef ufs_daddr_t allocfcn_t __P((struct inode *ip, int cg, ufs_daddr_t bpref,
61 static ufs_daddr_t ffs_alloccg __P((struct inode *, int, ufs_daddr_t, int));
63 ffs_alloccgblk __P((struct inode *, struct buf *, ufs_daddr_t));
65 static int ffs_checkblk __P((struct inode *, ufs_daddr_t, long));
67 static void ffs_clusteracct __P((struct fs *, struct cg *, ufs_daddr_t,
69 static ufs_daddr_t ffs_clusteralloc __P((struct inode *, int, ufs_daddr_t,
71 static ino_t ffs_dirpref __P((struct fs *));
72 static ufs_daddr_t ffs_fragextend __P((struct inode *, int, long, int, int));
73 static void ffs_fserr __P((struct fs *, u_int, char *));
74 static u_long ffs_hashalloc
75 __P((struct inode *, int, long, int, allocfcn_t *));
76 static ino_t ffs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int));
77 static ufs_daddr_t ffs_mapsearch __P((struct fs *, struct cg *, ufs_daddr_t,
81 * Allocate a block in the file system.
83 * The size of the requested block is given, which must be some
84 * multiple of fs_fsize and <= fs_bsize.
85 * A preference may be optionally specified. If a preference is given
86 * the following hierarchy is used to allocate a block:
87 * 1) allocate the requested block.
88 * 2) allocate a rotationally optimal block in the same cylinder.
89 * 3) allocate a block in the same cylinder group.
90 * 4) quadradically rehash into other cylinder groups, until an
91 * available block is located.
92 * If no block preference is given the following heirarchy is used
93 * to allocate a block:
94 * 1) allocate a block in the cylinder group that contains the
96 * 2) quadradically rehash into other cylinder groups, until an
97 * available block is located.
100 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
101 register struct inode *ip;
102 ufs_daddr_t lbn, bpref;
107 register struct fs *fs;
117 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
118 printf("dev = %s, bsize = %ld, size = %d, fs = %s\n",
119 devtoname(ip->i_dev), (long)fs->fs_bsize, size,
121 panic("ffs_alloc: bad size");
124 panic("ffs_alloc: missing credential");
125 #endif /* DIAGNOSTIC */
126 if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
128 if (cred->cr_uid != 0 &&
129 freespace(fs, fs->fs_minfree) - numfrags(fs, size) < 0)
132 error = chkdq(ip, (long)btodb(size), cred, 0);
136 if (bpref >= fs->fs_size)
139 cg = ino_to_cg(fs, ip->i_number);
141 cg = dtog(fs, bpref);
142 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
145 ip->i_blocks += btodb(size);
146 ip->i_flag |= IN_CHANGE | IN_UPDATE;
152 * Restore user's disk quota because allocation failed.
154 (void) chkdq(ip, (long)-btodb(size), cred, FORCE);
157 ffs_fserr(fs, cred->cr_uid, "file system full");
158 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
163 * Reallocate a fragment to a bigger size
165 * The number and size of the old block is given, and a preference
166 * and new size is also specified. The allocator attempts to extend
167 * the original block. Failing that, the regular block allocator is
168 * invoked to get an appropriate block.
171 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
172 register struct inode *ip;
179 register struct fs *fs;
181 int cg, request, error;
182 ufs_daddr_t bprev, bno;
187 if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
188 (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
190 "dev = %s, bsize = %ld, osize = %d, nsize = %d, fs = %s\n",
191 devtoname(ip->i_dev), (long)fs->fs_bsize, osize,
192 nsize, fs->fs_fsmnt);
193 panic("ffs_realloccg: bad size");
196 panic("ffs_realloccg: missing credential");
197 #endif /* DIAGNOSTIC */
198 if (cred->cr_uid != 0 &&
199 freespace(fs, fs->fs_minfree) - numfrags(fs, nsize - osize) < 0)
201 if ((bprev = ip->i_db[lbprev]) == 0) {
202 printf("dev = %s, bsize = %ld, bprev = %ld, fs = %s\n",
203 devtoname(ip->i_dev), (long)fs->fs_bsize, (long)bprev,
205 panic("ffs_realloccg: bad bprev");
208 * Allocate the extra space in the buffer.
210 error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp);
216 if( bp->b_blkno == bp->b_lblkno) {
217 if( lbprev >= NDADDR)
218 panic("ffs_realloccg: lbprev out of range");
219 bp->b_blkno = fsbtodb(fs, bprev);
223 error = chkdq(ip, (long)btodb(nsize - osize), cred, 0);
230 * Check for extension in the existing location.
232 cg = dtog(fs, bprev);
233 bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize);
235 if (bp->b_blkno != fsbtodb(fs, bno))
236 panic("ffs_realloccg: bad blockno");
237 ip->i_blocks += btodb(nsize - osize);
238 ip->i_flag |= IN_CHANGE | IN_UPDATE;
240 bp->b_flags |= B_DONE;
241 bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
246 * Allocate a new disk location.
248 if (bpref >= fs->fs_size)
250 switch ((int)fs->fs_optim) {
253 * Allocate an exact sized fragment. Although this makes
254 * best use of space, we will waste time relocating it if
255 * the file continues to grow. If the fragmentation is
256 * less than half of the minimum free reserve, we choose
257 * to begin optimizing for time.
260 if (fs->fs_minfree <= 5 ||
261 fs->fs_cstotal.cs_nffree >
262 fs->fs_dsize * fs->fs_minfree / (2 * 100))
264 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
266 fs->fs_optim = FS_OPTTIME;
270 * At this point we have discovered a file that is trying to
271 * grow a small fragment to a larger fragment. To save time,
272 * we allocate a full sized block, then free the unused portion.
273 * If the file continues to grow, the `ffs_fragextend' call
274 * above will be able to grow it in place without further
275 * copying. If aberrant programs cause disk fragmentation to
276 * grow within 2% of the free reserve, we choose to begin
277 * optimizing for space.
279 request = fs->fs_bsize;
280 if (fs->fs_cstotal.cs_nffree <
281 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
283 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
285 fs->fs_optim = FS_OPTSPACE;
288 printf("dev = %s, optim = %ld, fs = %s\n",
289 devtoname(ip->i_dev), (long)fs->fs_optim, fs->fs_fsmnt);
290 panic("ffs_realloccg: bad optim");
293 bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
296 bp->b_blkno = fsbtodb(fs, bno);
297 if (!DOINGSOFTDEP(ITOV(ip)))
298 ffs_blkfree(ip, bprev, (long)osize);
300 ffs_blkfree(ip, bno + numfrags(fs, nsize),
301 (long)(request - nsize));
302 ip->i_blocks += btodb(nsize - osize);
303 ip->i_flag |= IN_CHANGE | IN_UPDATE;
305 bp->b_flags |= B_DONE;
306 bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
312 * Restore user's disk quota because allocation failed.
314 (void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
321 ffs_fserr(fs, cred->cr_uid, "file system full");
322 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
326 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW, 0, "FFS filesystem");
329 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
331 * The vnode and an array of buffer pointers for a range of sequential
332 * logical blocks to be made contiguous is given. The allocator attempts
333 * to find a range of sequential blocks starting as close as possible to
334 * an fs_rotdelay offset from the end of the allocation for the logical
335 * block immediately preceeding the current range. If successful, the
336 * physical block numbers in the buffer pointers and in the inode are
337 * changed to reflect the new allocation. If unsuccessful, the allocation
338 * is left unchanged. The success in doing the reallocation is returned.
339 * Note that the error return is not reflected back to the user. Rather
340 * the previous block allocation will be used.
342 static int doasyncfree = 1;
343 SYSCTL_INT(_vfs_ffs, FFS_ASYNCFREE, doasyncfree, CTLFLAG_RW, &doasyncfree, 0, "");
345 static int doreallocblks = 1;
346 SYSCTL_INT(_vfs_ffs, FFS_REALLOCBLKS, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
349 static volatile int prtrealloc = 0;
354 struct vop_reallocblks_args /* {
356 struct cluster_save *a_buflist;
362 struct buf *sbp, *ebp;
363 ufs_daddr_t *bap, *sbap, *ebap = 0;
364 struct cluster_save *buflist;
365 ufs_daddr_t start_lbn, end_lbn, soff, newblk, blkno;
366 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
367 int i, len, start_lvl, end_lvl, pref, ssize;
369 if (doreallocblks == 0)
374 if (fs->fs_contigsumsize <= 0)
376 buflist = ap->a_buflist;
377 len = buflist->bs_nchildren;
378 start_lbn = buflist->bs_children[0]->b_lblkno;
379 end_lbn = start_lbn + len - 1;
381 for (i = 0; i < len; i++)
382 if (!ffs_checkblk(ip,
383 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
384 panic("ffs_reallocblks: unallocated block 1");
385 for (i = 1; i < len; i++)
386 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
387 panic("ffs_reallocblks: non-logical cluster");
388 blkno = buflist->bs_children[0]->b_blkno;
389 ssize = fsbtodb(fs, fs->fs_frag);
390 for (i = 1; i < len - 1; i++)
391 if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
392 panic("ffs_reallocblks: non-physical cluster %d", i);
395 * If the latest allocation is in a new cylinder group, assume that
396 * the filesystem has decided to move and do not force it back to
397 * the previous cylinder group.
399 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
400 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
402 if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
403 ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
406 * Get the starting offset and block map for the first block.
408 if (start_lvl == 0) {
412 idp = &start_ap[start_lvl - 1];
413 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
417 sbap = (ufs_daddr_t *)sbp->b_data;
421 * Find the preferred location for the cluster.
423 pref = ffs_blkpref(ip, start_lbn, soff, sbap);
425 * If the block range spans two block maps, get the second map.
427 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
431 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
432 panic("ffs_reallocblk: start == end");
434 ssize = len - (idp->in_off + 1);
435 if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
437 ebap = (ufs_daddr_t *)ebp->b_data;
440 * Search the block map looking for an allocation of the desired size.
442 if ((newblk = (ufs_daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
443 len, ffs_clusteralloc)) == 0)
446 * We have found a new contiguous block.
448 * First we have to replace the old block pointers with the new
449 * block pointers in the inode and indirect blocks associated
454 printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
458 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
464 if (!ffs_checkblk(ip,
465 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
466 panic("ffs_reallocblks: unallocated block 2");
467 if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
468 panic("ffs_reallocblks: alloc mismatch");
472 printf(" %d,", *bap);
474 if (DOINGSOFTDEP(vp)) {
475 if (sbap == &ip->i_db[0] && i < ssize)
476 softdep_setup_allocdirect(ip, start_lbn + i,
477 blkno, *bap, fs->fs_bsize, fs->fs_bsize,
478 buflist->bs_children[i]);
480 softdep_setup_allocindir_page(ip, start_lbn + i,
481 i < ssize ? sbp : ebp, soff + i, blkno,
482 *bap, buflist->bs_children[i]);
487 * Next we must write out the modified inode and indirect blocks.
488 * For strict correctness, the writes should be synchronous since
489 * the old block values may have been written to disk. In practise
490 * they are almost never written, but if we are concerned about
491 * strict correctness, the `doasyncfree' flag should be set to zero.
493 * The test on `doasyncfree' should be changed to test a flag
494 * that shows whether the associated buffers and inodes have
495 * been written. The flag should be set when the cluster is
496 * started and cleared whenever the buffer or inode is flushed.
497 * We can then check below to see if it is set, and do the
498 * synchronous write only when it has been cleared.
500 if (sbap != &ip->i_db[0]) {
506 ip->i_flag |= IN_CHANGE | IN_UPDATE;
517 * Last, free the old blocks and assign the new blocks to the buffers.
523 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
524 if (!DOINGSOFTDEP(vp))
526 dbtofsb(fs, buflist->bs_children[i]->b_blkno),
528 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
530 if (!ffs_checkblk(ip,
531 dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
532 panic("ffs_reallocblks: unallocated block 3");
536 printf(" %d,", blkno);
550 if (sbap != &ip->i_db[0])
556 * Allocate an inode in the file system.
558 * If allocating a directory, use ffs_dirpref to select the inode.
559 * If allocating in a directory, the following hierarchy is followed:
560 * 1) allocate the preferred inode.
561 * 2) allocate an inode in the same cylinder group.
562 * 3) quadradically rehash into other cylinder groups, until an
563 * available inode is located.
564 * If no inode preference is given the following heirarchy is used
565 * to allocate an inode:
566 * 1) allocate an inode in cylinder group 0.
567 * 2) quadradically rehash into other cylinder groups, until an
568 * available inode is located.
571 ffs_valloc(pvp, mode, cred, vpp)
577 register struct inode *pip;
578 register struct fs *fs;
579 register struct inode *ip;
586 if (fs->fs_cstotal.cs_nifree == 0)
589 if ((mode & IFMT) == IFDIR)
590 ipref = ffs_dirpref(fs);
592 ipref = pip->i_number;
593 if (ipref >= fs->fs_ncg * fs->fs_ipg)
595 cg = ino_to_cg(fs, ipref);
596 ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode,
597 (allocfcn_t *)ffs_nodealloccg);
600 error = VFS_VGET(pvp->v_mount, ino, vpp);
602 UFS_VFREE(pvp, ino, mode);
607 printf("mode = 0%o, inum = %lu, fs = %s\n",
608 ip->i_mode, (u_long)ip->i_number, fs->fs_fsmnt);
609 panic("ffs_valloc: dup alloc");
611 if (ip->i_blocks) { /* XXX */
612 printf("free inode %s/%lu had %ld blocks\n",
613 fs->fs_fsmnt, (u_long)ino, (long)ip->i_blocks);
618 * Set up a new generation number for this inode.
620 if (ip->i_gen == 0 || ++ip->i_gen == 0)
621 ip->i_gen = random() / 2 + 1;
624 ffs_fserr(fs, cred->cr_uid, "out of inodes");
625 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
630 * Find a cylinder to place a directory.
632 * The policy implemented by this algorithm is to select from
633 * among those cylinder groups with above the average number of
634 * free inodes, the one with the smallest number of directories.
638 register struct fs *fs;
640 int cg, minndir, mincg, avgifree;
642 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
643 minndir = fs->fs_ipg;
645 for (cg = 0; cg < fs->fs_ncg; cg++)
646 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
647 fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
649 minndir = fs->fs_cs(fs, cg).cs_ndir;
651 return ((ino_t)(fs->fs_ipg * mincg));
655 * Select the desired position for the next block in a file. The file is
656 * logically divided into sections. The first section is composed of the
657 * direct blocks. Each additional section contains fs_maxbpg blocks.
659 * If no blocks have been allocated in the first section, the policy is to
660 * request a block in the same cylinder group as the inode that describes
661 * the file. If no blocks have been allocated in any other section, the
662 * policy is to place the section in a cylinder group with a greater than
663 * average number of free blocks. An appropriate cylinder group is found
664 * by using a rotor that sweeps the cylinder groups. When a new group of
665 * blocks is needed, the sweep begins in the cylinder group following the
666 * cylinder group from which the previous allocation was made. The sweep
667 * continues until a cylinder group with greater than the average number
668 * of free blocks is found. If the allocation is for the first block in an
669 * indirect block, the information on the previous allocation is unavailable;
670 * here a best guess is made based upon the logical block number being
673 * If a section is already partially allocated, the policy is to
674 * contiguously allocate fs_maxcontig blocks. The end of one of these
675 * contiguous blocks and the beginning of the next is physically separated
676 * so that the disk head will be in transit between them for at least
677 * fs_rotdelay milliseconds. This is to allow time for the processor to
678 * schedule another I/O transfer.
681 ffs_blkpref(ip, lbn, indx, bap)
687 register struct fs *fs;
689 int avgbfree, startcg;
693 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
694 if (lbn < NDADDR + NINDIR(fs)) {
695 cg = ino_to_cg(fs, ip->i_number);
696 return (fs->fs_fpg * cg + fs->fs_frag);
699 * Find a cylinder with greater than average number of
700 * unused data blocks.
702 if (indx == 0 || bap[indx - 1] == 0)
704 ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
706 startcg = dtog(fs, bap[indx - 1]) + 1;
707 startcg %= fs->fs_ncg;
708 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
709 for (cg = startcg; cg < fs->fs_ncg; cg++)
710 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
712 return (fs->fs_fpg * cg + fs->fs_frag);
714 for (cg = 0; cg <= startcg; cg++)
715 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
717 return (fs->fs_fpg * cg + fs->fs_frag);
722 * One or more previous blocks have been laid out. If less
723 * than fs_maxcontig previous blocks are contiguous, the
724 * next block is requested contiguously, otherwise it is
725 * requested rotationally delayed by fs_rotdelay milliseconds.
727 nextblk = bap[indx - 1] + fs->fs_frag;
728 if (fs->fs_rotdelay == 0 || indx < fs->fs_maxcontig ||
729 bap[indx - fs->fs_maxcontig] +
730 blkstofrags(fs, fs->fs_maxcontig) != nextblk)
733 * Here we convert ms of delay to frags as:
734 * (frags) = (ms) * (rev/sec) * (sect/rev) /
735 * ((sect/frag) * (ms/sec))
736 * then round up to the next block.
738 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
739 (NSPF(fs) * 1000), fs->fs_frag);
744 * Implement the cylinder overflow algorithm.
746 * The policy implemented by this algorithm is:
747 * 1) allocate the block in its requested cylinder group.
748 * 2) quadradically rehash on the cylinder group number.
749 * 3) brute force search for a free block.
753 ffs_hashalloc(ip, cg, pref, size, allocator)
757 int size; /* size for data blocks, mode for inodes */
758 allocfcn_t *allocator;
760 register struct fs *fs;
761 long result; /* XXX why not same type as we return? */
766 * 1: preferred cylinder group
768 result = (*allocator)(ip, cg, pref, size);
772 * 2: quadratic rehash
774 for (i = 1; i < fs->fs_ncg; i *= 2) {
776 if (cg >= fs->fs_ncg)
778 result = (*allocator)(ip, cg, 0, size);
783 * 3: brute force search
784 * Note that we start at i == 2, since 0 was checked initially,
785 * and 1 is always checked in the quadratic rehash.
787 cg = (icg + 2) % fs->fs_ncg;
788 for (i = 2; i < fs->fs_ncg; i++) {
789 result = (*allocator)(ip, cg, 0, size);
793 if (cg == fs->fs_ncg)
800 * Determine whether a fragment can be extended.
802 * Check to see if the necessary fragments are available, and
803 * if they are, allocate them.
806 ffs_fragextend(ip, cg, bprev, osize, nsize)
812 register struct fs *fs;
813 register struct cg *cgp;
820 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
822 frags = numfrags(fs, nsize);
823 bbase = fragnum(fs, bprev);
824 if (bbase > fragnum(fs, (bprev + frags - 1))) {
825 /* cannot extend across a block boundary */
828 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
829 (int)fs->fs_cgsize, NOCRED, &bp);
834 cgp = (struct cg *)bp->b_data;
835 if (!cg_chkmagic(cgp)) {
839 bp->b_xflags |= BX_BKGRDWRITE;
840 cgp->cg_time = time_second;
841 bno = dtogd(fs, bprev);
842 for (i = numfrags(fs, osize); i < frags; i++)
843 if (isclr(cg_blksfree(cgp), bno + i)) {
848 * the current fragment can be extended
849 * deduct the count on fragment being extended into
850 * increase the count on the remaining fragment (if any)
851 * allocate the extended piece
853 for (i = frags; i < fs->fs_frag - bbase; i++)
854 if (isclr(cg_blksfree(cgp), bno + i))
856 cgp->cg_frsum[i - numfrags(fs, osize)]--;
858 cgp->cg_frsum[i - frags]++;
859 for (i = numfrags(fs, osize); i < frags; i++) {
860 clrbit(cg_blksfree(cgp), bno + i);
861 cgp->cg_cs.cs_nffree--;
862 fs->fs_cstotal.cs_nffree--;
863 fs->fs_cs(fs, cg).cs_nffree--;
866 if (DOINGSOFTDEP(ITOV(ip)))
867 softdep_setup_blkmapdep(bp, fs, bprev);
873 * Determine whether a block can be allocated.
875 * Check to see if a block of the appropriate size is available,
876 * and if it is, allocate it.
879 ffs_alloccg(ip, cg, bpref, size)
885 register struct fs *fs;
886 register struct cg *cgp;
889 ufs_daddr_t bno, blkno;
890 int allocsiz, error, frags;
893 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
895 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
896 (int)fs->fs_cgsize, NOCRED, &bp);
901 cgp = (struct cg *)bp->b_data;
902 if (!cg_chkmagic(cgp) ||
903 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
907 bp->b_xflags |= BX_BKGRDWRITE;
908 cgp->cg_time = time_second;
909 if (size == fs->fs_bsize) {
910 bno = ffs_alloccgblk(ip, bp, bpref);
915 * check to see if any fragments are already available
916 * allocsiz is the size which will be allocated, hacking
917 * it down to a smaller size if necessary
919 frags = numfrags(fs, size);
920 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
921 if (cgp->cg_frsum[allocsiz] != 0)
923 if (allocsiz == fs->fs_frag) {
925 * no fragments were available, so a block will be
926 * allocated, and hacked up
928 if (cgp->cg_cs.cs_nbfree == 0) {
932 bno = ffs_alloccgblk(ip, bp, bpref);
933 bpref = dtogd(fs, bno);
934 for (i = frags; i < fs->fs_frag; i++)
935 setbit(cg_blksfree(cgp), bpref + i);
936 i = fs->fs_frag - frags;
937 cgp->cg_cs.cs_nffree += i;
938 fs->fs_cstotal.cs_nffree += i;
939 fs->fs_cs(fs, cg).cs_nffree += i;
945 bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
950 for (i = 0; i < frags; i++)
951 clrbit(cg_blksfree(cgp), bno + i);
952 cgp->cg_cs.cs_nffree -= frags;
953 fs->fs_cstotal.cs_nffree -= frags;
954 fs->fs_cs(fs, cg).cs_nffree -= frags;
956 cgp->cg_frsum[allocsiz]--;
957 if (frags != allocsiz)
958 cgp->cg_frsum[allocsiz - frags]++;
959 blkno = cg * fs->fs_fpg + bno;
960 if (DOINGSOFTDEP(ITOV(ip)))
961 softdep_setup_blkmapdep(bp, fs, blkno);
963 return ((u_long)blkno);
967 * Allocate a block in a cylinder group.
969 * This algorithm implements the following policy:
970 * 1) allocate the requested block.
971 * 2) allocate a rotationally optimal block in the same cylinder.
972 * 3) allocate the next available block on the block rotor for the
973 * specified cylinder group.
974 * Note that this routine only allocates fs_bsize blocks; these
975 * blocks may be fragmented by the routine that allocates them.
978 ffs_alloccgblk(ip, bp, bpref)
985 ufs_daddr_t bno, blkno;
986 int cylno, pos, delta;
991 cgp = (struct cg *)bp->b_data;
992 if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
993 bpref = cgp->cg_rotor;
996 bpref = blknum(fs, bpref);
997 bpref = dtogd(fs, bpref);
999 * if the requested block is available, use it
1001 if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
1005 if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
1007 * Block layout information is not available.
1008 * Leaving bpref unchanged means we take the
1009 * next available free block following the one
1010 * we just allocated. Hopefully this will at
1011 * least hit a track cache on drives of unknown
1012 * geometry (e.g. SCSI).
1017 * check for a block available on the same cylinder
1019 cylno = cbtocylno(fs, bpref);
1020 if (cg_blktot(cgp)[cylno] == 0)
1023 * check the summary information to see if a block is
1024 * available in the requested cylinder starting at the
1025 * requested rotational position and proceeding around.
1027 cylbp = cg_blks(fs, cgp, cylno);
1028 pos = cbtorpos(fs, bpref);
1029 for (i = pos; i < fs->fs_nrpos; i++)
1032 if (i == fs->fs_nrpos)
1033 for (i = 0; i < pos; i++)
1038 * found a rotational position, now find the actual
1039 * block. A panic if none is actually there.
1041 pos = cylno % fs->fs_cpc;
1042 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
1043 if (fs_postbl(fs, pos)[i] == -1) {
1044 printf("pos = %d, i = %d, fs = %s\n",
1045 pos, i, fs->fs_fsmnt);
1046 panic("ffs_alloccgblk: cyl groups corrupted");
1048 for (i = fs_postbl(fs, pos)[i];; ) {
1049 if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
1050 bno = blkstofrags(fs, (bno + i));
1053 delta = fs_rotbl(fs)[i];
1055 delta + i > fragstoblks(fs, fs->fs_fpg))
1059 printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
1060 panic("ffs_alloccgblk: can't find blk in cyl");
1064 * no blocks in the requested cylinder, so take next
1065 * available one in this cylinder group.
1067 bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1070 cgp->cg_rotor = bno;
1072 blkno = fragstoblks(fs, bno);
1073 ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno);
1074 ffs_clusteracct(fs, cgp, blkno, -1);
1075 cgp->cg_cs.cs_nbfree--;
1076 fs->fs_cstotal.cs_nbfree--;
1077 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
1078 cylno = cbtocylno(fs, bno);
1079 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
1080 cg_blktot(cgp)[cylno]--;
1082 blkno = cgp->cg_cgx * fs->fs_fpg + bno;
1083 if (DOINGSOFTDEP(ITOV(ip)))
1084 softdep_setup_blkmapdep(bp, fs, blkno);
1089 * Determine whether a cluster can be allocated.
1091 * We do not currently check for optimal rotational layout if there
1092 * are multiple choices in the same cylinder group. Instead we just
1093 * take the first one that we find following bpref.
1096 ffs_clusteralloc(ip, cg, bpref, len)
1102 register struct fs *fs;
1103 register struct cg *cgp;
1105 int i, got, run, bno, bit, map;
1110 if (fs->fs_maxcluster[cg] < len)
1112 if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
1115 cgp = (struct cg *)bp->b_data;
1116 if (!cg_chkmagic(cgp))
1118 bp->b_xflags |= BX_BKGRDWRITE;
1120 * Check to see if a cluster of the needed size (or bigger) is
1121 * available in this cylinder group.
1123 lp = &cg_clustersum(cgp)[len];
1124 for (i = len; i <= fs->fs_contigsumsize; i++)
1127 if (i > fs->fs_contigsumsize) {
1129 * This is the first time looking for a cluster in this
1130 * cylinder group. Update the cluster summary information
1131 * to reflect the true maximum sized cluster so that
1132 * future cluster allocation requests can avoid reading
1133 * the cylinder group map only to find no clusters.
1135 lp = &cg_clustersum(cgp)[len - 1];
1136 for (i = len - 1; i > 0; i--)
1139 fs->fs_maxcluster[cg] = i;
1143 * Search the cluster map to find a big enough cluster.
1144 * We take the first one that we find, even if it is larger
1145 * than we need as we prefer to get one close to the previous
1146 * block allocation. We do not search before the current
1147 * preference point as we do not want to allocate a block
1148 * that is allocated before the previous one (as we will
1149 * then have to wait for another pass of the elevator
1150 * algorithm before it will be read). We prefer to fail and
1151 * be recalled to try an allocation in the next cylinder group.
1153 if (dtog(fs, bpref) != cg)
1156 bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1157 mapp = &cg_clustersfree(cgp)[bpref / NBBY];
1159 bit = 1 << (bpref % NBBY);
1160 for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
1161 if ((map & bit) == 0) {
1168 if ((got & (NBBY - 1)) != (NBBY - 1)) {
1175 if (got >= cgp->cg_nclusterblks)
1178 * Allocate the cluster that we have found.
1180 for (i = 1; i <= len; i++)
1181 if (!ffs_isblock(fs, cg_blksfree(cgp), got - run + i))
1182 panic("ffs_clusteralloc: map mismatch");
1183 bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
1184 if (dtog(fs, bno) != cg)
1185 panic("ffs_clusteralloc: allocated out of group");
1186 len = blkstofrags(fs, len);
1187 for (i = 0; i < len; i += fs->fs_frag)
1188 if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1189 panic("ffs_clusteralloc: lost block");
1199 * Determine whether an inode can be allocated.
1201 * Check to see if an inode is available, and if it is,
1202 * allocate it using the following policy:
1203 * 1) allocate the requested inode.
1204 * 2) allocate the next available inode after the requested
1205 * inode in the specified cylinder group.
1208 ffs_nodealloccg(ip, cg, ipref, mode)
1214 register struct fs *fs;
1215 register struct cg *cgp;
1217 int error, start, len, loc, map, i;
1220 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1222 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1223 (int)fs->fs_cgsize, NOCRED, &bp);
1228 cgp = (struct cg *)bp->b_data;
1229 if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
1233 bp->b_xflags |= BX_BKGRDWRITE;
1234 cgp->cg_time = time_second;
1236 ipref %= fs->fs_ipg;
1237 if (isclr(cg_inosused(cgp), ipref))
1240 start = cgp->cg_irotor / NBBY;
1241 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
1242 loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
1246 loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
1248 printf("cg = %d, irotor = %ld, fs = %s\n",
1249 cg, (long)cgp->cg_irotor, fs->fs_fsmnt);
1250 panic("ffs_nodealloccg: map corrupted");
1254 i = start + len - loc;
1255 map = cg_inosused(cgp)[i];
1257 for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
1258 if ((map & i) == 0) {
1259 cgp->cg_irotor = ipref;
1263 printf("fs = %s\n", fs->fs_fsmnt);
1264 panic("ffs_nodealloccg: block not in map");
1267 if (DOINGSOFTDEP(ITOV(ip)))
1268 softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1269 setbit(cg_inosused(cgp), ipref);
1270 cgp->cg_cs.cs_nifree--;
1271 fs->fs_cstotal.cs_nifree--;
1272 fs->fs_cs(fs, cg).cs_nifree--;
1274 if ((mode & IFMT) == IFDIR) {
1275 cgp->cg_cs.cs_ndir++;
1276 fs->fs_cstotal.cs_ndir++;
1277 fs->fs_cs(fs, cg).cs_ndir++;
1280 return (cg * fs->fs_ipg + ipref);
1284 * Free a block or fragment.
1286 * The specified block or fragment is placed back in the
1287 * free map. If a fragment is deallocated, a possible
1288 * block reassembly is checked.
1291 ffs_blkfree(ip, bno, size)
1292 register struct inode *ip;
1296 register struct fs *fs;
1297 register struct cg *cgp;
1300 int i, error, cg, blk, frags, bbase;
1303 VOP_FREEBLKS(ip->i_devvp, fsbtodb(fs, bno), size);
1304 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
1305 fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1306 printf("dev=%s, bno = %ld, bsize = %ld, size = %ld, fs = %s\n",
1307 devtoname(ip->i_dev), (long)bno, (long)fs->fs_bsize, size,
1309 panic("ffs_blkfree: bad size");
1312 if ((u_int)bno >= fs->fs_size) {
1313 printf("bad block %ld, ino %lu\n",
1314 (long)bno, (u_long)ip->i_number);
1315 ffs_fserr(fs, ip->i_uid, "bad block");
1318 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1319 (int)fs->fs_cgsize, NOCRED, &bp);
1324 cgp = (struct cg *)bp->b_data;
1325 if (!cg_chkmagic(cgp)) {
1329 bp->b_xflags |= BX_BKGRDWRITE;
1330 cgp->cg_time = time_second;
1331 bno = dtogd(fs, bno);
1332 if (size == fs->fs_bsize) {
1333 blkno = fragstoblks(fs, bno);
1334 if (!ffs_isfreeblock(fs, cg_blksfree(cgp), blkno)) {
1335 printf("dev = %s, block = %ld, fs = %s\n",
1336 devtoname(ip->i_dev), (long)bno, fs->fs_fsmnt);
1337 panic("ffs_blkfree: freeing free block");
1339 ffs_setblock(fs, cg_blksfree(cgp), blkno);
1340 ffs_clusteracct(fs, cgp, blkno, 1);
1341 cgp->cg_cs.cs_nbfree++;
1342 fs->fs_cstotal.cs_nbfree++;
1343 fs->fs_cs(fs, cg).cs_nbfree++;
1344 i = cbtocylno(fs, bno);
1345 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
1346 cg_blktot(cgp)[i]++;
1348 bbase = bno - fragnum(fs, bno);
1350 * decrement the counts associated with the old frags
1352 blk = blkmap(fs, cg_blksfree(cgp), bbase);
1353 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
1355 * deallocate the fragment
1357 frags = numfrags(fs, size);
1358 for (i = 0; i < frags; i++) {
1359 if (isset(cg_blksfree(cgp), bno + i)) {
1360 printf("dev = %s, block = %ld, fs = %s\n",
1361 devtoname(ip->i_dev), (long)(bno + i),
1363 panic("ffs_blkfree: freeing free frag");
1365 setbit(cg_blksfree(cgp), bno + i);
1367 cgp->cg_cs.cs_nffree += i;
1368 fs->fs_cstotal.cs_nffree += i;
1369 fs->fs_cs(fs, cg).cs_nffree += i;
1371 * add back in counts associated with the new frags
1373 blk = blkmap(fs, cg_blksfree(cgp), bbase);
1374 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1376 * if a complete block has been reassembled, account for it
1378 blkno = fragstoblks(fs, bbase);
1379 if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
1380 cgp->cg_cs.cs_nffree -= fs->fs_frag;
1381 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
1382 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1383 ffs_clusteracct(fs, cgp, blkno, 1);
1384 cgp->cg_cs.cs_nbfree++;
1385 fs->fs_cstotal.cs_nbfree++;
1386 fs->fs_cs(fs, cg).cs_nbfree++;
1387 i = cbtocylno(fs, bbase);
1388 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
1389 cg_blktot(cgp)[i]++;
1398 * Verify allocation of a block or fragment. Returns true if block or
1399 * fragment is allocated, false if it is free.
1402 ffs_checkblk(ip, bno, size)
1410 int i, error, frags, free;
1413 if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1414 printf("bsize = %ld, size = %ld, fs = %s\n",
1415 (long)fs->fs_bsize, size, fs->fs_fsmnt);
1416 panic("ffs_checkblk: bad size");
1418 if ((u_int)bno >= fs->fs_size)
1419 panic("ffs_checkblk: bad block %d", bno);
1420 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
1421 (int)fs->fs_cgsize, NOCRED, &bp);
1423 panic("ffs_checkblk: cg bread failed");
1424 cgp = (struct cg *)bp->b_data;
1425 if (!cg_chkmagic(cgp))
1426 panic("ffs_checkblk: cg magic mismatch");
1427 bp->b_xflags |= BX_BKGRDWRITE;
1428 bno = dtogd(fs, bno);
1429 if (size == fs->fs_bsize) {
1430 free = ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
1432 frags = numfrags(fs, size);
1433 for (free = 0, i = 0; i < frags; i++)
1434 if (isset(cg_blksfree(cgp), bno + i))
1436 if (free != 0 && free != frags)
1437 panic("ffs_checkblk: partially free fragment");
1442 #endif /* DIAGNOSTIC */
1448 ffs_vfree( pvp, ino, mode)
1453 if (DOINGSOFTDEP(pvp)) {
1454 softdep_freefile(pvp, ino, mode);
1457 return (ffs_freefile(pvp, ino, mode));
1461 * Do the actual free operation.
1462 * The specified inode is placed back in the free map.
1465 ffs_freefile( pvp, ino, mode)
1470 register struct fs *fs;
1471 register struct cg *cgp;
1472 register struct inode *pip;
1478 if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1479 panic("ffs_vfree: range: dev = (%d,%d), ino = %d, fs = %s",
1480 major(pip->i_dev), minor(pip->i_dev), ino, fs->fs_fsmnt);
1481 cg = ino_to_cg(fs, ino);
1482 error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
1483 (int)fs->fs_cgsize, NOCRED, &bp);
1488 cgp = (struct cg *)bp->b_data;
1489 if (!cg_chkmagic(cgp)) {
1493 bp->b_xflags |= BX_BKGRDWRITE;
1494 cgp->cg_time = time_second;
1496 if (isclr(cg_inosused(cgp), ino)) {
1497 printf("dev = %s, ino = %lu, fs = %s\n",
1498 devtoname(pip->i_dev), (u_long)ino, fs->fs_fsmnt);
1499 if (fs->fs_ronly == 0)
1500 panic("ffs_vfree: freeing free inode");
1502 clrbit(cg_inosused(cgp), ino);
1503 if (ino < cgp->cg_irotor)
1504 cgp->cg_irotor = ino;
1505 cgp->cg_cs.cs_nifree++;
1506 fs->fs_cstotal.cs_nifree++;
1507 fs->fs_cs(fs, cg).cs_nifree++;
1508 if ((mode & IFMT) == IFDIR) {
1509 cgp->cg_cs.cs_ndir--;
1510 fs->fs_cstotal.cs_ndir--;
1511 fs->fs_cs(fs, cg).cs_ndir--;
1519 * Find a block of the specified size in the specified cylinder group.
1521 * It is a panic if a request is made to find a block if none are
1525 ffs_mapsearch(fs, cgp, bpref, allocsiz)
1526 register struct fs *fs;
1527 register struct cg *cgp;
1532 int start, len, loc, i;
1533 int blk, field, subfield, pos;
1536 * find the fragment by searching through the free block
1537 * map for an appropriate bit pattern
1540 start = dtogd(fs, bpref) / NBBY;
1542 start = cgp->cg_frotor / NBBY;
1543 len = howmany(fs->fs_fpg, NBBY) - start;
1544 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start],
1545 (u_char *)fragtbl[fs->fs_frag],
1546 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1550 loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0],
1551 (u_char *)fragtbl[fs->fs_frag],
1552 (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
1554 printf("start = %d, len = %d, fs = %s\n",
1555 start, len, fs->fs_fsmnt);
1556 panic("ffs_alloccg: map corrupted");
1560 bno = (start + len - loc) * NBBY;
1561 cgp->cg_frotor = bno;
1563 * found the byte in the map
1564 * sift through the bits to find the selected frag
1566 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1567 blk = blkmap(fs, cg_blksfree(cgp), bno);
1569 field = around[allocsiz];
1570 subfield = inside[allocsiz];
1571 for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
1572 if ((blk & field) == subfield)
1578 printf("bno = %lu, fs = %s\n", (u_long)bno, fs->fs_fsmnt);
1579 panic("ffs_alloccg: block not in map");
1584 * Update the cluster map because of an allocation or free.
1586 * Cnt == 1 means free; cnt == -1 means allocating.
1589 ffs_clusteracct(fs, cgp, blkno, cnt)
1597 u_char *freemapp, *mapp;
1598 int i, start, end, forw, back, map, bit;
1600 if (fs->fs_contigsumsize <= 0)
1602 freemapp = cg_clustersfree(cgp);
1603 sump = cg_clustersum(cgp);
1605 * Allocate or clear the actual block.
1608 setbit(freemapp, blkno);
1610 clrbit(freemapp, blkno);
1612 * Find the size of the cluster going forward.
1615 end = start + fs->fs_contigsumsize;
1616 if (end >= cgp->cg_nclusterblks)
1617 end = cgp->cg_nclusterblks;
1618 mapp = &freemapp[start / NBBY];
1620 bit = 1 << (start % NBBY);
1621 for (i = start; i < end; i++) {
1622 if ((map & bit) == 0)
1624 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1633 * Find the size of the cluster going backward.
1636 end = start - fs->fs_contigsumsize;
1639 mapp = &freemapp[start / NBBY];
1641 bit = 1 << (start % NBBY);
1642 for (i = start; i > end; i--) {
1643 if ((map & bit) == 0)
1645 if ((i & (NBBY - 1)) != 0) {
1649 bit = 1 << (NBBY - 1);
1654 * Account for old cluster and the possibly new forward and
1657 i = back + forw + 1;
1658 if (i > fs->fs_contigsumsize)
1659 i = fs->fs_contigsumsize;
1666 * Update cluster summary information.
1668 lp = &sump[fs->fs_contigsumsize];
1669 for (i = fs->fs_contigsumsize; i > 0; i--)
1672 fs->fs_maxcluster[cgp->cg_cgx] = i;
1676 * Fserr prints the name of a file system with an error diagnostic.
1678 * The form of the error message is:
1682 ffs_fserr(fs, uid, cp)
1687 struct proc *p = curproc; /* XXX */
1689 log(LOG_ERR, "pid %d (%s), uid %d on %s: %s\n", p ? p->p_pid : -1,
1690 p ? p->p_comm : "-", uid, fs->fs_fsmnt, cp);