2 * modified for Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)ffs_alloc.c 8.8 (Berkeley) 2/21/94
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
49 #include <fs/ext2fs/fs.h>
50 #include <fs/ext2fs/inode.h>
51 #include <fs/ext2fs/ext2_mount.h>
52 #include <fs/ext2fs/ext2fs.h>
53 #include <fs/ext2fs/ext2_extern.h>
55 static daddr_t ext2_alloccg(struct inode *, int, daddr_t, int);
56 static daddr_t ext2_clusteralloc(struct inode *, int, daddr_t, int);
57 static u_long ext2_dirpref(struct inode *);
58 static void ext2_fserr(struct m_ext2fs *, uid_t, char *);
59 static u_long ext2_hashalloc(struct inode *, int, long, int,
60 daddr_t (*)(struct inode *, int, daddr_t,
62 static daddr_t ext2_nodealloccg(struct inode *, int, daddr_t, int);
63 static daddr_t ext2_mapsearch(struct m_ext2fs *, char *, daddr_t);
66 * Allocate a block in the filesystem.
68 * A preference may be optionally specified. If a preference is given
69 * the following hierarchy is used to allocate a block:
70 * 1) allocate the requested block.
71 * 2) allocate a rotationally optimal block in the same cylinder.
72 * 3) allocate a block in the same cylinder group.
73 * 4) quadradically rehash into other cylinder groups, until an
74 * available block is located.
75 * If no block preference is given the following hierarchy is used
76 * to allocate a block:
77 * 1) allocate a block in the cylinder group that contains the
79 * 2) quadradically rehash into other cylinder groups, until an
80 * available block is located.
83 ext2_alloc(struct inode *ip, daddr_t lbn, e4fs_daddr_t bpref, int size,
84 struct ucred *cred, e4fs_daddr_t *bnp)
87 struct ext2mount *ump;
93 mtx_assert(EXT2_MTX(ump), MA_OWNED);
95 if ((u_int)size > fs->e2fs_bsize || blkoff(fs, size) != 0) {
96 vn_printf(ip->i_devvp, "bsize = %lu, size = %d, fs = %s\n",
97 (long unsigned int)fs->e2fs_bsize, size, fs->e2fs_fsmnt);
98 panic("ext2_alloc: bad size");
101 panic("ext2_alloc: missing credential");
102 #endif /* INVARIANTS */
103 if (size == fs->e2fs_bsize && fs->e2fs->e2fs_fbcount == 0)
105 if (cred->cr_uid != 0 &&
106 fs->e2fs->e2fs_fbcount < fs->e2fs->e2fs_rbcount)
108 if (bpref >= fs->e2fs->e2fs_bcount)
111 cg = ino_to_cg(fs, ip->i_number);
113 cg = dtog(fs, bpref);
114 bno = (daddr_t)ext2_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
117 /* set next_alloc fields as done in block_getblk */
118 ip->i_next_alloc_block = lbn;
119 ip->i_next_alloc_goal = bno;
121 ip->i_blocks += btodb(fs->e2fs_bsize);
122 ip->i_flag |= IN_CHANGE | IN_UPDATE;
128 ext2_fserr(fs, cred->cr_uid, "filesystem full");
129 uprintf("\n%s: write failed, filesystem is full\n", fs->e2fs_fsmnt);
134 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
136 * The vnode and an array of buffer pointers for a range of sequential
137 * logical blocks to be made contiguous is given. The allocator attempts
138 * to find a range of sequential blocks starting as close as possible to
139 * an fs_rotdelay offset from the end of the allocation for the logical
140 * block immediately preceding the current range. If successful, the
141 * physical block numbers in the buffer pointers and in the inode are
142 * changed to reflect the new allocation. If unsuccessful, the allocation
143 * is left unchanged. The success in doing the reallocation is returned.
144 * Note that the error return is not reflected back to the user. Rather
145 * the previous block allocation will be used.
148 static SYSCTL_NODE(_vfs, OID_AUTO, ext2fs, CTLFLAG_RW, 0, "EXT2FS filesystem");
150 static int doasyncfree = 1;
151 SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0,
152 "Use asychronous writes to update block pointers when freeing blocks");
154 static int doreallocblks = 1;
155 SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
158 ext2_reallocblks(struct vop_reallocblks_args *ap)
163 struct buf *sbp, *ebp;
164 uint32_t *bap, *sbap, *ebap = 0;
165 struct ext2mount *ump;
166 struct cluster_save *buflist;
167 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
168 e2fs_lbn_t start_lbn, end_lbn;
170 e2fs_daddr_t newblk, blkno;
171 int i, len, start_lvl, end_lvl, pref, ssize;
173 if (doreallocblks == 0)
181 if (fs->e2fs_contigsumsize <= 0)
184 buflist = ap->a_buflist;
185 len = buflist->bs_nchildren;
186 start_lbn = buflist->bs_children[0]->b_lblkno;
187 end_lbn = start_lbn + len - 1;
189 for (i = 1; i < len; i++)
190 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
191 panic("ext2_reallocblks: non-cluster");
194 * If the cluster crosses the boundary for the first indirect
195 * block, leave space for the indirect block. Indirect blocks
196 * are initially laid out in a position after the last direct
197 * block. Block reallocation would usually destroy locality by
198 * moving the indirect block out of the way to make room for
199 * data blocks if we didn't compensate here. We should also do
200 * this for other indirect block boundaries, but it is only
201 * important for the first one.
203 if (start_lbn < NDADDR && end_lbn >= NDADDR)
206 * If the latest allocation is in a new cylinder group, assume that
207 * the filesystem has decided to move and do not force it back to
208 * the previous cylinder group.
210 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
211 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
213 if (ext2_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
214 ext2_getlbns(vp, end_lbn, end_ap, &end_lvl))
217 * Get the starting offset and block map for the first block.
219 if (start_lvl == 0) {
223 idp = &start_ap[start_lvl - 1];
224 if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &sbp)) {
228 sbap = (u_int *)sbp->b_data;
232 * If the block range spans two block maps, get the second map.
234 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
238 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
239 panic("ext2_reallocblks: start == end");
241 ssize = len - (idp->in_off + 1);
242 if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &ebp))
244 ebap = (u_int *)ebp->b_data;
247 * Find the preferred location for the cluster.
250 pref = ext2_blkpref(ip, start_lbn, soff, sbap, 0);
252 * Search the block map looking for an allocation of the desired size.
254 if ((newblk = (e2fs_daddr_t)ext2_hashalloc(ip, dtog(fs, pref), pref,
255 len, ext2_clusteralloc)) == 0){
260 * We have found a new contiguous block.
262 * First we have to replace the old block pointers with the new
263 * block pointers in the inode and indirect blocks associated
267 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
268 (intmax_t)start_lbn, (intmax_t)end_lbn);
271 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
277 if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap))
278 panic("ext2_reallocblks: alloc mismatch");
281 printf(" %d,", *bap);
286 * Next we must write out the modified inode and indirect blocks.
287 * For strict correctness, the writes should be synchronous since
288 * the old block values may have been written to disk. In practise
289 * they are almost never written, but if we are concerned about
290 * strict correctness, the `doasyncfree' flag should be set to zero.
292 * The test on `doasyncfree' should be changed to test a flag
293 * that shows whether the associated buffers and inodes have
294 * been written. The flag should be set when the cluster is
295 * started and cleared whenever the buffer or inode is flushed.
296 * We can then check below to see if it is set, and do the
297 * synchronous write only when it has been cleared.
299 if (sbap != &ip->i_db[0]) {
305 ip->i_flag |= IN_CHANGE | IN_UPDATE;
316 * Last, free the old blocks and assign the new blocks to the buffers.
321 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
322 ext2_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
324 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
326 printf(" %d,", blkno);
337 if (sbap != &ip->i_db[0])
343 * Allocate an inode in the filesystem.
347 ext2_valloc(struct vnode *pvp, int mode, struct ucred *cred, struct vnode **vpp)
353 struct ext2mount *ump;
363 if (fs->e2fs->e2fs_ficount == 0)
366 * If it is a directory then obtain a cylinder group based on
367 * ext2_dirpref else obtain it using ino_to_cg. The preferred inode is
368 * always the next inode.
370 if ((mode & IFMT) == IFDIR) {
371 cg = ext2_dirpref(pip);
372 if (fs->e2fs_contigdirs[cg] < 255)
373 fs->e2fs_contigdirs[cg]++;
375 cg = ino_to_cg(fs, pip->i_number);
376 if (fs->e2fs_contigdirs[cg] > 0)
377 fs->e2fs_contigdirs[cg]--;
379 ipref = cg * fs->e2fs->e2fs_ipg + 1;
380 ino = (ino_t)ext2_hashalloc(pip, cg, (long)ipref, mode, ext2_nodealloccg);
384 error = VFS_VGET(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
386 ext2_vfree(pvp, ino, mode);
392 * The question is whether using VGET was such good idea at all:
393 * Linux doesn't read the old inode in when it is allocating a
394 * new one. I will set at least i_size and i_blocks to zero.
400 /* now we want to make sure that the block pointers are zeroed out */
401 for (i = 0; i < NDADDR; i++)
403 for (i = 0; i < NIADDR; i++)
407 * Set up a new generation number for this inode.
408 * XXX check if this makes sense in ext2
410 if (ip->i_gen == 0 || ++ip->i_gen == 0)
411 ip->i_gen = random() / 2 + 1;
414 ip->i_birthtime = ts.tv_sec;
415 ip->i_birthnsec = ts.tv_nsec;
418 printf("ext2_valloc: allocated inode %d\n", ino);
423 ext2_fserr(fs, cred->cr_uid, "out of inodes");
424 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
429 * Find a cylinder to place a directory.
431 * The policy implemented by this algorithm is to allocate a
432 * directory inode in the same cylinder group as its parent
433 * directory, but also to reserve space for its files inodes
434 * and data. Restrict the number of directories which may be
435 * allocated one after another in the same cylinder group
436 * without intervening allocation of files.
438 * If we allocate a first level directory then force allocation
439 * in another cylinder group.
443 ext2_dirpref(struct inode *pip)
446 int cg, prefcg, cgsize;
447 u_int avgifree, avgbfree, avgndir, curdirsize;
448 u_int minifree, minbfree, maxndir;
449 u_int mincg, minndir;
450 u_int dirsize, maxcontigdirs;
452 mtx_assert(EXT2_MTX(pip->i_ump), MA_OWNED);
455 avgifree = fs->e2fs->e2fs_ficount / fs->e2fs_gcount;
456 avgbfree = fs->e2fs->e2fs_fbcount / fs->e2fs_gcount;
457 avgndir = fs->e2fs_total_dir / fs->e2fs_gcount;
460 * Force allocation in another cg if creating a first level dir.
462 ASSERT_VOP_LOCKED(ITOV(pip), "ext2fs_dirpref");
463 if (ITOV(pip)->v_vflag & VV_ROOT) {
464 prefcg = arc4random() % fs->e2fs_gcount;
466 minndir = fs->e2fs_ipg;
467 for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
468 if (fs->e2fs_gd[cg].ext2bgd_ndirs < minndir &&
469 fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree &&
470 fs->e2fs_gd[cg].ext2bgd_nbfree >= avgbfree) {
472 minndir = fs->e2fs_gd[cg].ext2bgd_ndirs;
474 for (cg = 0; cg < prefcg; cg++)
475 if (fs->e2fs_gd[cg].ext2bgd_ndirs < minndir &&
476 fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree &&
477 fs->e2fs_gd[cg].ext2bgd_nbfree >= avgbfree) {
479 minndir = fs->e2fs_gd[cg].ext2bgd_ndirs;
486 * Count various limits which used for
487 * optimal allocation of a directory inode.
489 maxndir = min(avgndir + fs->e2fs_ipg / 16, fs->e2fs_ipg);
490 minifree = avgifree - avgifree / 4;
493 minbfree = avgbfree - avgbfree / 4;
496 cgsize = fs->e2fs_fsize * fs->e2fs_fpg;
497 dirsize = AVGDIRSIZE;
498 curdirsize = avgndir ? (cgsize - avgbfree * fs->e2fs_bsize) / avgndir : 0;
499 if (dirsize < curdirsize)
500 dirsize = curdirsize;
501 maxcontigdirs = min((avgbfree * fs->e2fs_bsize) / dirsize, 255);
502 maxcontigdirs = min(maxcontigdirs, fs->e2fs_ipg / AFPDIR);
503 if (maxcontigdirs == 0)
507 * Limit number of dirs in one cg and reserve space for
508 * regular files, but only if we have no deficit in
511 prefcg = ino_to_cg(fs, pip->i_number);
512 for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
513 if (fs->e2fs_gd[cg].ext2bgd_ndirs < maxndir &&
514 fs->e2fs_gd[cg].ext2bgd_nifree >= minifree &&
515 fs->e2fs_gd[cg].ext2bgd_nbfree >= minbfree) {
516 if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
519 for (cg = 0; cg < prefcg; cg++)
520 if (fs->e2fs_gd[cg].ext2bgd_ndirs < maxndir &&
521 fs->e2fs_gd[cg].ext2bgd_nifree >= minifree &&
522 fs->e2fs_gd[cg].ext2bgd_nbfree >= minbfree) {
523 if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
527 * This is a backstop when we have deficit in space.
529 for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
530 if (fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree)
532 for (cg = 0; cg < prefcg; cg++)
533 if (fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree)
539 * Select the desired position for the next block in a file.
541 * we try to mimic what Remy does in inode_getblk/block_getblk
543 * we note: blocknr == 0 means that we're about to allocate either
544 * a direct block or a pointer block at the first level of indirection
545 * (In other words, stuff that will go in i_db[] or i_ib[])
547 * blocknr != 0 means that we're allocating a block that is none
548 * of the above. Then, blocknr tells us the number of the block
549 * that will hold the pointer
552 ext2_blkpref(struct inode *ip, e2fs_lbn_t lbn, int indx, e2fs_daddr_t *bap,
553 e2fs_daddr_t blocknr)
556 mtx_assert(EXT2_MTX(ip->i_ump), MA_OWNED);
558 /* if the next block is actually what we thought it is,
559 then set the goal to what we thought it should be
561 if (ip->i_next_alloc_block == lbn && ip->i_next_alloc_goal != 0)
562 return ip->i_next_alloc_goal;
564 /* now check whether we were provided with an array that basically
565 tells us previous blocks to which we want to stay closeby
568 for (tmp = indx - 1; tmp >= 0; tmp--)
572 /* else let's fall back to the blocknr, or, if there is none,
573 follow the rule that a block should be allocated near its inode
575 return blocknr ? blocknr :
576 (e2fs_daddr_t)(ip->i_block_group *
577 EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
578 ip->i_e2fs->e2fs->e2fs_first_dblock;
582 * Implement the cylinder overflow algorithm.
584 * The policy implemented by this algorithm is:
585 * 1) allocate the block in its requested cylinder group.
586 * 2) quadradically rehash on the cylinder group number.
587 * 3) brute force search for a free block.
590 ext2_hashalloc(struct inode *ip, int cg, long pref, int size,
591 daddr_t (*allocator)(struct inode *, int, daddr_t, int))
597 mtx_assert(EXT2_MTX(ip->i_ump), MA_OWNED);
600 * 1: preferred cylinder group
602 result = (*allocator)(ip, cg, pref, size);
606 * 2: quadratic rehash
608 for (i = 1; i < fs->e2fs_gcount; i *= 2) {
610 if (cg >= fs->e2fs_gcount)
611 cg -= fs->e2fs_gcount;
612 result = (*allocator)(ip, cg, 0, size);
617 * 3: brute force search
618 * Note that we start at i == 2, since 0 was checked initially,
619 * and 1 is always checked in the quadratic rehash.
621 cg = (icg + 2) % fs->e2fs_gcount;
622 for (i = 2; i < fs->e2fs_gcount; i++) {
623 result = (*allocator)(ip, cg, 0, size);
627 if (cg == fs->e2fs_gcount)
634 * Determine whether a block can be allocated.
636 * Check to see if a block of the appropriate size is available,
637 * and if it is, allocate it.
640 ext2_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
644 struct ext2mount *ump;
645 daddr_t bno, runstart, runlen;
646 int bit, loc, end, error, start;
651 if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0)
654 error = bread(ip->i_devvp, fsbtodb(fs,
655 fs->e2fs_gd[cg].ext2bgd_b_bitmap),
656 (int)fs->e2fs_bsize, NOCRED, &bp);
662 if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0) {
664 * Another thread allocated the last block in this
665 * group while we were waiting for the buffer.
671 bbp = (char *)bp->b_data;
673 if (dtog(fs, bpref) != cg)
676 bpref = dtogd(fs, bpref);
678 * if the requested block is available, use it
680 if (isclr(bbp, bpref)) {
686 * no blocks in the requested cylinder, so take next
687 * available one in this cylinder group.
688 * first try to get 8 contigous blocks, then fall back to a single
692 start = dtogd(fs, bpref) / NBBY;
695 end = howmany(fs->e2fs->e2fs_fpg, NBBY) - start;
699 for (loc = start; loc < end; loc++) {
700 if (bbp[loc] == (char)0xff) {
705 /* Start of a run, find the number of high clear bits. */
709 runstart = loc * NBBY + bit;
710 } else if (bbp[loc] == 0) {
711 /* Continue a run. */
715 * Finish the current run. If it isn't long
716 * enough, start a new one.
718 bit = ffs(bbp[loc]) - 1;
725 /* Run was too short, start a new one. */
728 runstart = loc * NBBY + bit;
731 /* If the current run is long enough, use it. */
743 bno = ext2_mapsearch(fs, bbp, bpref);
751 if (isset(bbp, bno)) {
752 printf("ext2fs_alloccgblk: cg=%d bno=%jd fs=%s\n",
753 cg, (intmax_t)bno, fs->e2fs_fsmnt);
754 panic("ext2fs_alloccg: dup alloc");
759 ext2_clusteracct(fs, bbp, cg, bno, -1);
760 fs->e2fs->e2fs_fbcount--;
761 fs->e2fs_gd[cg].ext2bgd_nbfree--;
765 return (cg * fs->e2fs->e2fs_fpg + fs->e2fs->e2fs_first_dblock + bno);
769 * Determine whether a cluster can be allocated.
772 ext2_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
775 struct ext2mount *ump;
778 int bit, error, got, i, loc, run;
785 if (fs->e2fs_maxcluster[cg] < len)
789 error = bread(ip->i_devvp,
790 fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
791 (int)fs->e2fs_bsize, NOCRED, &bp);
795 bbp = (char *)bp->b_data;
798 * Check to see if a cluster of the needed size (or bigger) is
799 * available in this cylinder group.
801 lp = &fs->e2fs_clustersum[cg].cs_sum[len];
802 for (i = len; i <= fs->e2fs_contigsumsize; i++)
805 if (i > fs->e2fs_contigsumsize) {
807 * Update the cluster summary information to reflect
808 * the true maximum-sized cluster so that future cluster
809 * allocation requests can avoid reading the bitmap only
810 * to find no cluster.
812 lp = &fs->e2fs_clustersum[cg].cs_sum[len - 1];
813 for (i = len - 1; i > 0; i--)
816 fs->e2fs_maxcluster[cg] = i;
821 /* Search the bitmap to find a big enough cluster like in FFS. */
822 if (dtog(fs, bpref) != cg)
825 bpref = dtogd(fs, bpref);
827 bit = 1 << (bpref % NBBY);
828 for (run = 0, got = bpref; got < fs->e2fs->e2fs_fpg; got++) {
829 if ((bbp[loc] & bit) != 0)
836 if ((got & (NBBY - 1)) != (NBBY - 1))
844 if (got >= fs->e2fs->e2fs_fpg)
847 /* Allocate the cluster that we found. */
848 for (i = 1; i < len; i++)
849 if (!isclr(bbp, got - run + i))
850 panic("ext2_clusteralloc: map mismatch");
853 if (bno >= fs->e2fs->e2fs_fpg)
854 panic("ext2_clusteralloc: allocated out of group");
857 for (i = 0; i < len; i += fs->e2fs_fpb) {
858 setbit(bbp, bno + i);
859 ext2_clusteracct(fs, bbp, cg, bno + i, -1);
860 fs->e2fs->e2fs_fbcount--;
861 fs->e2fs_gd[cg].ext2bgd_nbfree--;
867 return (cg * fs->e2fs->e2fs_fpg + fs->e2fs->e2fs_first_dblock + bno);
877 * Determine whether an inode can be allocated.
879 * Check to see if an inode is available, and if it is,
880 * allocate it using tode in the specified cylinder group.
883 ext2_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
887 struct ext2mount *ump;
888 int error, start, len;
890 ipref--; /* to avoid a lot of (ipref -1) */
895 if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
898 error = bread(ip->i_devvp, fsbtodb(fs,
899 fs->e2fs_gd[cg].ext2bgd_i_bitmap),
900 (int)fs->e2fs_bsize, NOCRED, &bp);
906 if (fs->e2fs_gd[cg].ext2bgd_nifree == 0) {
908 * Another thread allocated the last i-node in this
909 * group while we were waiting for the buffer.
915 ibp = (char *)bp->b_data;
917 ipref %= fs->e2fs->e2fs_ipg;
918 if (isclr(ibp, ipref))
921 start = ipref / NBBY;
922 len = howmany(fs->e2fs->e2fs_ipg - ipref, NBBY);
923 loc = memcchr(&ibp[start], 0xff, len);
927 loc = memcchr(&ibp[start], 0xff, len);
929 printf("cg = %d, ipref = %lld, fs = %s\n",
930 cg, (long long)ipref, fs->e2fs_fsmnt);
931 panic("ext2fs_nodealloccg: map corrupted");
935 ipref = (loc - ibp) * NBBY + ffs(~*loc) - 1;
939 fs->e2fs_gd[cg].ext2bgd_nifree--;
940 fs->e2fs->e2fs_ficount--;
942 if ((mode & IFMT) == IFDIR) {
943 fs->e2fs_gd[cg].ext2bgd_ndirs++;
944 fs->e2fs_total_dir++;
948 return (cg * fs->e2fs->e2fs_ipg + ipref +1);
952 * Free a block or fragment.
956 ext2_blkfree(struct inode *ip, e4fs_daddr_t bno, long size)
960 struct ext2mount *ump;
967 if ((u_int)bno >= fs->e2fs->e2fs_bcount) {
968 printf("bad block %lld, ino %llu\n", (long long)bno,
969 (unsigned long long)ip->i_number);
970 ext2_fserr(fs, ip->i_uid, "bad block");
973 error = bread(ip->i_devvp,
974 fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
975 (int)fs->e2fs_bsize, NOCRED, &bp);
980 bbp = (char *)bp->b_data;
981 bno = dtogd(fs, bno);
982 if (isclr(bbp, bno)) {
983 printf("block = %lld, fs = %s\n",
984 (long long)bno, fs->e2fs_fsmnt);
985 panic("ext2_blkfree: freeing free block");
989 ext2_clusteracct(fs, bbp, cg, bno, 1);
990 fs->e2fs->e2fs_fbcount++;
991 fs->e2fs_gd[cg].ext2bgd_nbfree++;
1002 ext2_vfree(struct vnode *pvp, ino_t ino, int mode)
1004 struct m_ext2fs *fs;
1007 struct ext2mount *ump;
1014 if ((u_int)ino > fs->e2fs_ipg * fs->e2fs_gcount)
1015 panic("ext2_vfree: range: devvp = %p, ino = %ju, fs = %s",
1016 pip->i_devvp, (uintmax_t)ino, fs->e2fs_fsmnt);
1018 cg = ino_to_cg(fs, ino);
1019 error = bread(pip->i_devvp,
1020 fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_i_bitmap),
1021 (int)fs->e2fs_bsize, NOCRED, &bp);
1026 ibp = (char *)bp->b_data;
1027 ino = (ino - 1) % fs->e2fs->e2fs_ipg;
1028 if (isclr(ibp, ino)) {
1029 printf("ino = %llu, fs = %s\n",
1030 (unsigned long long)ino, fs->e2fs_fsmnt);
1031 if (fs->e2fs_ronly == 0)
1032 panic("ext2_vfree: freeing free inode");
1036 fs->e2fs->e2fs_ficount++;
1037 fs->e2fs_gd[cg].ext2bgd_nifree++;
1038 if ((mode & IFMT) == IFDIR) {
1039 fs->e2fs_gd[cg].ext2bgd_ndirs--;
1040 fs->e2fs_total_dir--;
1049 * Find a block in the specified cylinder group.
1051 * It is a panic if a request is made to find a block if none are
1055 ext2_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
1061 * find the fragment by searching through the free block
1062 * map for an appropriate bit pattern
1065 start = dtogd(fs, bpref) / NBBY;
1068 len = howmany(fs->e2fs->e2fs_fpg, NBBY) - start;
1069 loc = memcchr(&bbp[start], 0xff, len);
1073 loc = memcchr(&bbp[start], 0xff, len);
1075 printf("start = %d, len = %d, fs = %s\n",
1076 start, len, fs->e2fs_fsmnt);
1077 panic("ext2_mapsearch: map corrupted");
1081 return ((loc - bbp) * NBBY + ffs(~*loc) - 1);
1085 * Fserr prints the name of a filesystem with an error diagnostic.
1087 * The form of the error message is:
1091 ext2_fserr(struct m_ext2fs *fs, uid_t uid, char *cp)
1094 log(LOG_ERR, "uid %u on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
1102 if (i == 0 || i == 1)
1104 for (a3 = 3, a5 = 5, a7 = 7;
1105 a3 <= i || a5 <= i || a7 <= i;
1106 a3 *= 3, a5 *= 5, a7 *= 7)
1107 if (i == a3 || i == a5 || i == a7)