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/inode.h>
50 #include <fs/ext2fs/ext2_mount.h>
51 #include <fs/ext2fs/ext2fs.h>
52 #include <fs/ext2fs/fs.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 file system.
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(ip, lbn, bpref, size, cred, bnp)
91 struct ext2mount *ump;
97 mtx_assert(EXT2_MTX(ump), MA_OWNED);
99 if ((u_int)size > fs->e2fs_bsize || blkoff(fs, size) != 0) {
100 vn_printf(ip->i_devvp, "bsize = %lu, size = %d, fs = %s\n",
101 (long unsigned int)fs->e2fs_bsize, size, fs->e2fs_fsmnt);
102 panic("ext2_alloc: bad size");
105 panic("ext2_alloc: missing credential");
106 #endif /* DIAGNOSTIC */
107 if (size == fs->e2fs_bsize && fs->e2fs->e2fs_fbcount == 0)
109 if (cred->cr_uid != 0 &&
110 fs->e2fs->e2fs_fbcount < fs->e2fs->e2fs_rbcount)
112 if (bpref >= fs->e2fs->e2fs_bcount)
115 cg = ino_to_cg(fs, ip->i_number);
117 cg = dtog(fs, bpref);
118 bno = (daddr_t)ext2_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
121 /* set next_alloc fields as done in block_getblk */
122 ip->i_next_alloc_block = lbn;
123 ip->i_next_alloc_goal = bno;
125 ip->i_blocks += btodb(fs->e2fs_bsize);
126 ip->i_flag |= IN_CHANGE | IN_UPDATE;
132 ext2_fserr(fs, cred->cr_uid, "file system full");
133 uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
138 * Reallocate a sequence of blocks into a contiguous sequence of blocks.
140 * The vnode and an array of buffer pointers for a range of sequential
141 * logical blocks to be made contiguous is given. The allocator attempts
142 * to find a range of sequential blocks starting as close as possible to
143 * an fs_rotdelay offset from the end of the allocation for the logical
144 * block immediately preceding the current range. If successful, the
145 * physical block numbers in the buffer pointers and in the inode are
146 * changed to reflect the new allocation. If unsuccessful, the allocation
147 * is left unchanged. The success in doing the reallocation is returned.
148 * Note that the error return is not reflected back to the user. Rather
149 * the previous block allocation will be used.
152 SYSCTL_NODE(_vfs, OID_AUTO, ext2fs, CTLFLAG_RW, 0, "EXT2FS filesystem");
154 static int doasyncfree = 1;
155 SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0,
156 "Use asychronous writes to update block pointers when freeing blocks");
158 static int doreallocblks = 1;
159 SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
163 struct vop_reallocblks_args /* {
165 struct cluster_save *a_buflist;
171 struct buf *sbp, *ebp;
172 int32_t *bap, *sbap, *ebap = 0;
173 struct ext2mount *ump;
174 struct cluster_save *buflist;
175 struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
176 int32_t start_lbn, end_lbn, soff, newblk, blkno;
177 int i, len, start_lvl, end_lvl, pref, ssize;
179 if (doreallocblks == 0)
187 if (fs->e2fs_contigsumsize <= 0)
190 buflist = ap->a_buflist;
191 len = buflist->bs_nchildren;
192 start_lbn = buflist->bs_children[0]->b_lblkno;
193 end_lbn = start_lbn + len - 1;
195 for (i = 1; i < len; i++)
196 if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
197 panic("ext2_reallocblks: non-cluster");
200 * If the latest allocation is in a new cylinder group, assume that
201 * the filesystem has decided to move and do not force it back to
202 * the previous cylinder group.
204 if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
205 dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
207 if (ext2_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
208 ext2_getlbns(vp, end_lbn, end_ap, &end_lvl))
211 * Get the starting offset and block map for the first block.
213 if (start_lvl == 0) {
217 idp = &start_ap[start_lvl - 1];
218 if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &sbp)) {
222 sbap = (int32_t *)sbp->b_data;
226 * If the block range spans two block maps, get the second map.
228 if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
232 if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
233 panic("ext2_reallocblk: start == end");
235 ssize = len - (idp->in_off + 1);
236 if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &ebp))
238 ebap = (int32_t *)ebp->b_data;
241 * Find the preferred location for the cluster.
244 pref = ext2_blkpref(ip, start_lbn, soff, sbap, 0);
246 * Search the block map looking for an allocation of the desired size.
248 if ((newblk = (int32_t)ext2_hashalloc(ip, dtog(fs, pref), pref,
249 len, ext2_clusteralloc)) == 0){
254 * We have found a new contiguous block.
256 * First we have to replace the old block pointers with the new
257 * block pointers in the inode and indirect blocks associated
261 printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
262 (intmax_t)start_lbn, (intmax_t)end_lbn);
265 for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
271 if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap))
272 panic("ext2_reallocblks: alloc mismatch");
275 printf(" %d,", *bap);
280 * Next we must write out the modified inode and indirect blocks.
281 * For strict correctness, the writes should be synchronous since
282 * the old block values may have been written to disk. In practise
283 * they are almost never written, but if we are concerned about
284 * strict correctness, the `doasyncfree' flag should be set to zero.
286 * The test on `doasyncfree' should be changed to test a flag
287 * that shows whether the associated buffers and inodes have
288 * been written. The flag should be set when the cluster is
289 * started and cleared whenever the buffer or inode is flushed.
290 * We can then check below to see if it is set, and do the
291 * synchronous write only when it has been cleared.
293 if (sbap != &ip->i_db[0]) {
299 ip->i_flag |= IN_CHANGE | IN_UPDATE;
310 * Last, free the old blocks and assign the new blocks to the buffers.
315 for (blkno = newblk, i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
316 ext2_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
318 buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
320 printf(" %d,", blkno);
331 if (sbap != &ip->i_db[0])
337 * Allocate an inode in the file system.
341 ext2_valloc(pvp, mode, cred, vpp)
351 struct ext2mount *ump;
361 if (fs->e2fs->e2fs_ficount == 0)
364 * If it is a directory then obtain a cylinder group based on
365 * ext2_dirpref else obtain it using ino_to_cg. The preferred inode is
366 * always the next inode.
368 if ((mode & IFMT) == IFDIR) {
369 cg = ext2_dirpref(pip);
370 if (fs->e2fs_contigdirs[cg] < 255)
371 fs->e2fs_contigdirs[cg]++;
373 cg = ino_to_cg(fs, pip->i_number);
374 if (fs->e2fs_contigdirs[cg] > 0)
375 fs->e2fs_contigdirs[cg]--;
377 ipref = cg * fs->e2fs->e2fs_ipg + 1;
378 ino = (ino_t)ext2_hashalloc(pip, cg, (long)ipref, mode, ext2_nodealloccg);
382 error = VFS_VGET(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
384 ext2_vfree(pvp, ino, mode);
390 * The question is whether using VGET was such good idea at all:
391 * Linux doesn't read the old inode in when it is allocating a
392 * new one. I will set at least i_size and i_blocks to zero.
398 /* now we want to make sure that the block pointers are zeroed out */
399 for (i = 0; i < NDADDR; i++)
401 for (i = 0; i < NIADDR; i++)
405 * Set up a new generation number for this inode.
406 * XXX check if this makes sense in ext2
408 if (ip->i_gen == 0 || ++ip->i_gen == 0)
409 ip->i_gen = random() / 2 + 1;
412 ip->i_birthtime = ts.tv_sec;
413 ip->i_birthnsec = ts.tv_nsec;
416 printf("ext2_valloc: allocated inode %d\n", ino);
421 ext2_fserr(fs, cred->cr_uid, "out of inodes");
422 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
427 * Find a cylinder to place a directory.
429 * The policy implemented by this algorithm is to allocate a
430 * directory inode in the same cylinder group as its parent
431 * directory, but also to reserve space for its files inodes
432 * and data. Restrict the number of directories which may be
433 * allocated one after another in the same cylinder group
434 * without intervening allocation of files.
436 * If we allocate a first level directory then force allocation
437 * in another cylinder group.
441 ext2_dirpref(struct inode *pip)
444 int cg, prefcg, dirsize, cgsize;
445 int avgifree, avgbfree, avgndir, curdirsize;
446 int minifree, minbfree, maxndir;
450 mtx_assert(EXT2_MTX(pip->i_ump), MA_OWNED);
453 avgifree = fs->e2fs->e2fs_ficount / fs->e2fs_gcount;
454 avgbfree = fs->e2fs->e2fs_fbcount / fs->e2fs_gcount;
455 avgndir = fs->e2fs_total_dir / fs->e2fs_gcount;
458 * Force allocation in another cg if creating a first level dir.
460 ASSERT_VOP_LOCKED(ITOV(pip), "ext2fs_dirpref");
461 if (ITOV(pip)->v_vflag & VV_ROOT) {
462 prefcg = arc4random() % fs->e2fs_gcount;
464 minndir = fs->e2fs_ipg;
465 for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
466 if (fs->e2fs_gd[cg].ext2bgd_ndirs < minndir &&
467 fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree &&
468 fs->e2fs_gd[cg].ext2bgd_nbfree >= avgbfree) {
470 minndir = fs->e2fs_gd[cg].ext2bgd_ndirs;
472 for (cg = 0; cg < prefcg; cg++)
473 if (fs->e2fs_gd[cg].ext2bgd_ndirs < minndir &&
474 fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree &&
475 fs->e2fs_gd[cg].ext2bgd_nbfree >= avgbfree) {
477 minndir = fs->e2fs_gd[cg].ext2bgd_ndirs;
484 * Count various limits which used for
485 * optimal allocation of a directory inode.
487 maxndir = min(avgndir + fs->e2fs_ipg / 16, fs->e2fs_ipg);
488 minifree = avgifree - avgifree / 4;
491 minbfree = avgbfree - avgbfree / 4;
494 cgsize = fs->e2fs_fsize * fs->e2fs_fpg;
495 dirsize = AVGDIRSIZE;
496 curdirsize = avgndir ? (cgsize - avgbfree * fs->e2fs_bsize) / avgndir : 0;
497 if (dirsize < curdirsize)
498 dirsize = curdirsize;
500 maxcontigdirs = 0; /* dirsize overflowed */
502 maxcontigdirs = min((avgbfree * fs->e2fs_bsize) / dirsize, 255);
503 maxcontigdirs = min(maxcontigdirs, fs->e2fs_ipg / AFPDIR);
504 if (maxcontigdirs == 0)
508 * Limit number of dirs in one cg and reserve space for
509 * regular files, but only if we have no deficit in
512 prefcg = ino_to_cg(fs, pip->i_number);
513 for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
514 if (fs->e2fs_gd[cg].ext2bgd_ndirs < maxndir &&
515 fs->e2fs_gd[cg].ext2bgd_nifree >= minifree &&
516 fs->e2fs_gd[cg].ext2bgd_nbfree >= minbfree) {
517 if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
520 for (cg = 0; cg < prefcg; cg++)
521 if (fs->e2fs_gd[cg].ext2bgd_ndirs < maxndir &&
522 fs->e2fs_gd[cg].ext2bgd_nifree >= minifree &&
523 fs->e2fs_gd[cg].ext2bgd_nbfree >= minbfree) {
524 if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
528 * This is a backstop when we have deficit in space.
530 for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
531 if (fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree)
533 for (cg = 0; cg < prefcg; cg++)
534 if (fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree)
540 * Select the desired position for the next block in a file.
542 * we try to mimic what Remy does in inode_getblk/block_getblk
544 * we note: blocknr == 0 means that we're about to allocate either
545 * a direct block or a pointer block at the first level of indirection
546 * (In other words, stuff that will go in i_db[] or i_ib[])
548 * blocknr != 0 means that we're allocating a block that is none
549 * of the above. Then, blocknr tells us the number of the block
550 * that will hold the pointer
553 ext2_blkpref(ip, lbn, indx, bap, blocknr)
561 mtx_assert(EXT2_MTX(ip->i_ump), MA_OWNED);
563 /* if the next block is actually what we thought it is,
564 then set the goal to what we thought it should be
566 if (ip->i_next_alloc_block == lbn && ip->i_next_alloc_goal != 0)
567 return ip->i_next_alloc_goal;
569 /* now check whether we were provided with an array that basically
570 tells us previous blocks to which we want to stay closeby
573 for (tmp = indx - 1; tmp >= 0; tmp--)
577 /* else let's fall back to the blocknr, or, if there is none,
578 follow the rule that a block should be allocated near its inode
580 return blocknr ? blocknr :
581 (int32_t)(ip->i_block_group *
582 EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
583 ip->i_e2fs->e2fs->e2fs_first_dblock;
587 * Implement the cylinder overflow algorithm.
589 * The policy implemented by this algorithm is:
590 * 1) allocate the block in its requested cylinder group.
591 * 2) quadradically rehash on the cylinder group number.
592 * 3) brute force search for a free block.
595 ext2_hashalloc(struct inode *ip, int cg, long pref, int size,
596 daddr_t (*allocator)(struct inode *, int, daddr_t, int))
602 mtx_assert(EXT2_MTX(ip->i_ump), MA_OWNED);
605 * 1: preferred cylinder group
607 result = (*allocator)(ip, cg, pref, size);
611 * 2: quadratic rehash
613 for (i = 1; i < fs->e2fs_gcount; i *= 2) {
615 if (cg >= fs->e2fs_gcount)
616 cg -= fs->e2fs_gcount;
617 result = (*allocator)(ip, cg, 0, size);
622 * 3: brute force search
623 * Note that we start at i == 2, since 0 was checked initially,
624 * and 1 is always checked in the quadratic rehash.
626 cg = (icg + 2) % fs->e2fs_gcount;
627 for (i = 2; i < fs->e2fs_gcount; i++) {
628 result = (*allocator)(ip, cg, 0, size);
632 if (cg == fs->e2fs_gcount)
639 * Determine whether a block can be allocated.
641 * Check to see if a block of the appropriate size is available,
642 * and if it is, allocate it.
645 ext2_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
649 struct ext2mount *ump;
650 daddr_t bno, runstart, runlen;
651 int bit, loc, end, error, start;
656 if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0)
659 error = bread(ip->i_devvp, fsbtodb(fs,
660 fs->e2fs_gd[cg].ext2bgd_b_bitmap),
661 (int)fs->e2fs_bsize, NOCRED, &bp);
667 if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0) {
669 * Another thread allocated the last block in this
670 * group while we were waiting for the buffer.
676 bbp = (char *)bp->b_data;
678 if (dtog(fs, bpref) != cg)
681 bpref = dtogd(fs, bpref);
683 * if the requested block is available, use it
685 if (isclr(bbp, bpref)) {
691 * no blocks in the requested cylinder, so take next
692 * available one in this cylinder group.
693 * first try to get 8 contigous blocks, then fall back to a single
697 start = dtogd(fs, bpref) / NBBY;
700 end = howmany(fs->e2fs->e2fs_fpg, NBBY) - start;
704 for (loc = start; loc < end; loc++) {
705 if (bbp[loc] == (char)0xff) {
710 /* Start of a run, find the number of high clear bits. */
714 runstart = loc * NBBY + bit;
715 } else if (bbp[loc] == 0) {
716 /* Continue a run. */
720 * Finish the current run. If it isn't long
721 * enough, start a new one.
723 bit = ffs(bbp[loc]) - 1;
730 /* Run was too short, start a new one. */
733 runstart = loc * NBBY + bit;
736 /* If the current run is long enough, use it. */
748 bno = ext2_mapsearch(fs, bbp, bpref);
756 if (isset(bbp, bno)) {
757 printf("ext2fs_alloccgblk: cg=%d bno=%jd fs=%s\n",
758 cg, (intmax_t)bno, fs->e2fs_fsmnt);
759 panic("ext2fs_alloccg: dup alloc");
764 ext2_clusteracct(fs, bbp, cg, bno, -1);
765 fs->e2fs->e2fs_fbcount--;
766 fs->e2fs_gd[cg].ext2bgd_nbfree--;
770 return (cg * fs->e2fs->e2fs_fpg + fs->e2fs->e2fs_first_dblock + bno);
774 * Determine whether a cluster can be allocated.
777 ext2_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
780 struct ext2mount *ump;
783 int bit, error, got, i, loc, run;
790 if (fs->e2fs_maxcluster[cg] < len)
794 error = bread(ip->i_devvp,
795 fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
796 (int)fs->e2fs_bsize, NOCRED, &bp);
800 bbp = (char *)bp->b_data;
801 bp->b_xflags |= BX_BKGRDWRITE;
805 * Check to see if a cluster of the needed size (or bigger) is
806 * available in this cylinder group.
808 lp = &fs->e2fs_clustersum[cg].cs_sum[len];
809 for (i = len; i <= fs->e2fs_contigsumsize; i++)
812 if (i > fs->e2fs_contigsumsize) {
814 * Update the cluster summary information to reflect
815 * the true maximum-sized cluster so that future cluster
816 * allocation requests can avoid reading the bitmap only
817 * to find no cluster.
819 lp = &fs->e2fs_clustersum[cg].cs_sum[len - 1];
820 for (i = len - 1; i > 0; i--)
823 fs->e2fs_maxcluster[cg] = i;
828 /* Search the bitmap to find a big enough cluster like in FFS. */
829 if (dtog(fs, bpref) != cg)
832 bpref = dtogd(fs, bpref);
834 bit = 1 << (bpref % NBBY);
835 for (run = 0, got = bpref; got < fs->e2fs->e2fs_fpg; got++) {
836 if ((bbp[loc] & bit) != 0)
843 if ((got & (NBBY - 1)) != (NBBY - 1))
851 if (got >= fs->e2fs->e2fs_fpg)
854 /* Allocate the cluster that we found. */
855 for (i = 1; i < len; i++)
856 if (!isclr(bbp, got - run + i))
857 panic("ext2_clusteralloc: map mismatch");
860 if (bno >= fs->e2fs->e2fs_fpg)
861 panic("ext2_clusteralloc: allocated out of group");
864 for (i = 0; i < len; i += fs->e2fs_fpb) {
865 setbit(bbp, bno + i);
866 ext2_clusteracct(fs, bbp, cg, bno + i, -1);
867 fs->e2fs->e2fs_fbcount--;
868 fs->e2fs_gd[cg].ext2bgd_nbfree--;
874 return (cg * fs->e2fs->e2fs_fpg + fs->e2fs->e2fs_first_dblock + bno);
884 * Determine whether an inode can be allocated.
886 * Check to see if an inode is available, and if it is,
887 * allocate it using tode in the specified cylinder group.
890 ext2_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
894 struct ext2mount *ump;
895 int error, start, len, loc, map, i;
897 ipref--; /* to avoid a lot of (ipref -1) */
902 if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
905 error = bread(ip->i_devvp, fsbtodb(fs,
906 fs->e2fs_gd[cg].ext2bgd_i_bitmap),
907 (int)fs->e2fs_bsize, NOCRED, &bp);
913 if (fs->e2fs_gd[cg].ext2bgd_nifree == 0) {
915 * Another thread allocated the last i-node in this
916 * group while we were waiting for the buffer.
922 ibp = (char *)bp->b_data;
924 ipref %= fs->e2fs->e2fs_ipg;
925 if (isclr(ibp, ipref))
928 start = ipref / NBBY;
929 len = howmany(fs->e2fs->e2fs_ipg - ipref, NBBY);
930 loc = skpc(0xff, len, &ibp[start]);
934 loc = skpc(0xff, len, &ibp[0]);
936 printf("cg = %d, ipref = %lld, fs = %s\n",
937 cg, (long long)ipref, fs->e2fs_fsmnt);
938 panic("ext2fs_nodealloccg: map corrupted");
942 i = start + len - loc;
945 printf("fs = %s\n", fs->e2fs_fsmnt);
946 panic("ext2fs_nodealloccg: block not in map");
948 ipref = i * NBBY + ffs(map) - 1;
952 fs->e2fs_gd[cg].ext2bgd_nifree--;
953 fs->e2fs->e2fs_ficount--;
955 if ((mode & IFMT) == IFDIR) {
956 fs->e2fs_gd[cg].ext2bgd_ndirs++;
957 fs->e2fs_total_dir++;
961 return (cg * fs->e2fs->e2fs_ipg + ipref +1);
965 * Free a block or fragment.
969 ext2_blkfree(ip, bno, size)
976 struct ext2mount *ump;
983 if ((u_int)bno >= fs->e2fs->e2fs_bcount) {
984 printf("bad block %lld, ino %llu\n", (long long)bno,
985 (unsigned long long)ip->i_number);
986 ext2_fserr(fs, ip->i_uid, "bad block");
989 error = bread(ip->i_devvp,
990 fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
991 (int)fs->e2fs_bsize, NOCRED, &bp);
996 bbp = (char *)bp->b_data;
997 bno = dtogd(fs, bno);
998 if (isclr(bbp, bno)) {
999 printf("block = %lld, fs = %s\n",
1000 (long long)bno, fs->e2fs_fsmnt);
1001 panic("blkfree: freeing free block");
1005 ext2_clusteracct(fs, bbp, cg, bno, 1);
1006 fs->e2fs->e2fs_fbcount++;
1007 fs->e2fs_gd[cg].ext2bgd_nbfree++;
1018 ext2_vfree(pvp, ino, mode)
1023 struct m_ext2fs *fs;
1026 struct ext2mount *ump;
1029 /* mode_t save_i_mode; */
1034 if ((u_int)ino > fs->e2fs_ipg * fs->e2fs_gcount)
1035 panic("ext2_vfree: range: devvp = %p, ino = %d, fs = %s",
1036 pip->i_devvp, ino, fs->e2fs_fsmnt);
1038 cg = ino_to_cg(fs, ino);
1039 error = bread(pip->i_devvp,
1040 fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_i_bitmap),
1041 (int)fs->e2fs_bsize, NOCRED, &bp);
1046 ibp = (char *)bp->b_data;
1047 ino = (ino - 1) % fs->e2fs->e2fs_ipg;
1048 if (isclr(ibp, ino)) {
1049 printf("ino = %llu, fs = %s\n",
1050 (unsigned long long)ino, fs->e2fs_fsmnt);
1051 if (fs->e2fs_ronly == 0)
1052 panic("ifree: freeing free inode");
1056 fs->e2fs->e2fs_ficount++;
1057 fs->e2fs_gd[cg].ext2bgd_nifree++;
1058 if ((mode & IFMT) == IFDIR) {
1059 fs->e2fs_gd[cg].ext2bgd_ndirs--;
1060 fs->e2fs_total_dir--;
1069 * Find a block in the specified cylinder group.
1071 * It is a panic if a request is made to find a block if none are
1075 ext2_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
1077 int start, len, loc, i, map;
1080 * find the fragment by searching through the free block
1081 * map for an appropriate bit pattern
1084 start = dtogd(fs, bpref) / NBBY;
1087 len = howmany(fs->e2fs->e2fs_fpg, NBBY) - start;
1088 loc = skpc(0xff, len, &bbp[start]);
1092 loc = skpc(0xff, len, &bbp[start]);
1094 printf("start = %d, len = %d, fs = %s\n",
1095 start, len, fs->e2fs_fsmnt);
1096 panic("ext2fs_alloccg: map corrupted");
1100 i = start + len - loc;
1101 map = bbp[i] ^ 0xff;
1103 printf("fs = %s\n", fs->e2fs_fsmnt);
1104 panic("ext2fs_mapsearch: block not in map");
1106 return (i * NBBY + ffs(map) - 1);
1110 * Fserr prints the name of a file system with an error diagnostic.
1112 * The form of the error message is:
1116 ext2_fserr(fs, uid, cp)
1117 struct m_ext2fs *fs;
1122 log(LOG_ERR, "uid %u on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
1130 if (i == 0 || i == 1)
1132 for (a3 = 3, a5 = 5, a7 = 7;
1133 a3 <= i || a5 <= i || a7 <= i;
1134 a3 *= 3, a5 *= 5, a7 *= 7)
1135 if (i == a3 || i == a5 || i == a7)