2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Juli Mallett. All rights reserved.
6 * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
7 * FreeBSD project. Redistribution and use in source and binary forms, with
8 * or without modification, are permitted provided that the following
11 * 1. Redistribution of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistribution in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/mount.h>
35 #include <sys/disklabel.h>
38 #include <ufs/ufs/ufsmount.h>
39 #include <ufs/ufs/dinode.h>
40 #include <ufs/ffs/fs.h>
52 cgballoc(struct uufsd *disk)
61 blksfree = cg_blksfree(cgp);
62 for (bno = 0; bno < fs->fs_fpg / fs->fs_frag; bno++)
63 if (ffs_isblock(fs, blksfree, bno))
67 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
68 ffs_clrblock(fs, blksfree, (long)bno);
69 ffs_clusteracct(fs, cgp, bno, -1);
70 cgp->cg_cs.cs_nbfree--;
71 fs->fs_cstotal.cs_nbfree--;
73 return (cgbase(fs, cgp->cg_cgx) + blkstofrags(fs, bno));
77 cgbfree(struct uufsd *disk, ufs2_daddr_t bno, long size)
82 ufs1_daddr_t fragno, cgbno;
83 int i, cg, blk, frags, bbase;
87 if (cgread1(disk, cg) != 1)
90 cgbno = dtogd(fs, bno);
91 blksfree = cg_blksfree(cgp);
92 if (size == fs->fs_bsize) {
93 fragno = fragstoblks(fs, cgbno);
94 ffs_setblock(fs, blksfree, fragno);
95 ffs_clusteracct(fs, cgp, fragno, 1);
96 cgp->cg_cs.cs_nbfree++;
97 fs->fs_cstotal.cs_nbfree++;
98 fs->fs_cs(fs, cg).cs_nbfree++;
100 bbase = cgbno - fragnum(fs, cgbno);
102 * decrement the counts associated with the old frags
104 blk = blkmap(fs, blksfree, bbase);
105 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
107 * deallocate the fragment
109 frags = numfrags(fs, size);
110 for (i = 0; i < frags; i++)
111 setbit(blksfree, cgbno + i);
112 cgp->cg_cs.cs_nffree += i;
113 fs->fs_cstotal.cs_nffree += i;
114 fs->fs_cs(fs, cg).cs_nffree += i;
116 * add back in counts associated with the new frags
118 blk = blkmap(fs, blksfree, bbase);
119 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
121 * if a complete block has been reassembled, account for it
123 fragno = fragstoblks(fs, bbase);
124 if (ffs_isblock(fs, blksfree, fragno)) {
125 cgp->cg_cs.cs_nffree -= fs->fs_frag;
126 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
127 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
128 ffs_clusteracct(fs, cgp, fragno, 1);
129 cgp->cg_cs.cs_nbfree++;
130 fs->fs_cstotal.cs_nbfree++;
131 fs->fs_cs(fs, cg).cs_nbfree++;
134 return cgwrite(disk);
138 cgialloc(struct uufsd *disk)
140 struct ufs2_dinode *dp2;
149 inosused = cg_inosused(cgp);
150 for (ino = 0; ino < fs->fs_ipg; ino++)
151 if (isclr(inosused, ino))
155 if (fs->fs_magic == FS_UFS2_MAGIC &&
156 ino + INOPB(fs) > cgp->cg_initediblk &&
157 cgp->cg_initediblk < cgp->cg_niblk) {
158 char block[MAXBSIZE];
159 bzero(block, (int)fs->fs_bsize);
160 dp2 = (struct ufs2_dinode *)█
161 for (i = 0; i < INOPB(fs); i++) {
162 dp2->di_gen = arc4random();
165 if (bwrite(disk, ino_to_fsba(fs,
166 cgp->cg_cgx * fs->fs_ipg + cgp->cg_initediblk),
167 block, fs->fs_bsize))
169 cgp->cg_initediblk += INOPB(fs);
172 setbit(inosused, ino);
173 cgp->cg_irotor = ino;
174 cgp->cg_cs.cs_nifree--;
175 fs->fs_cstotal.cs_nifree--;
176 fs->fs_cs(fs, cgp->cg_cgx).cs_nifree--;
179 return (ino + (cgp->cg_cgx * fs->fs_ipg));
183 cgread(struct uufsd *disk)
186 if (disk->d_ccg >= disk->d_fs.fs_ncg)
188 return (cgread1(disk, disk->d_ccg++));
192 cgread1(struct uufsd *disk, int c)
195 if ((cgget(disk, c, &disk->d_cg)) == 0)
201 cgget(struct uufsd *disk, int cg, struct cg *cgp)
204 uint32_t cghash, calchash;
207 if (bread(disk, fsbtodb(fs, cgtod(fs, cg)), (void *)cgp,
208 fs->fs_cgsize) == -1) {
209 ERROR(disk, "unable to read cylinder group");
212 calchash = cgp->cg_ckhash;
213 if ((fs->fs_metackhash & CK_CYLGRP) != 0) {
214 cghash = cgp->cg_ckhash;
216 calchash = calculate_crc32c(~0L, (void *)cgp, fs->fs_cgsize);
217 cgp->cg_ckhash = cghash;
219 if (cgp->cg_ckhash != calchash || !cg_chkmagic(cgp) ||
221 ERROR(disk, "cylinder group checks failed");
230 cgwrite(struct uufsd *disk)
233 return (cgput(disk, &disk->d_cg));
237 cgwrite1(struct uufsd *disk, int cg)
239 static char errmsg[BUFSIZ];
241 if (cg == disk->d_cg.cg_cgx)
242 return (cgput(disk, &disk->d_cg));
243 snprintf(errmsg, BUFSIZ, "Cylinder group %d in buffer does not match "
244 "the cylinder group %d that cgwrite1 requested",
245 disk->d_cg.cg_cgx, cg);
252 cgput(struct uufsd *disk, struct cg *cgp)
257 if ((fs->fs_metackhash & CK_CYLGRP) != 0) {
260 calculate_crc32c(~0L, (void *)cgp, fs->fs_cgsize);
262 if (bwrite(disk, fsbtodb(fs, cgtod(fs, cgp->cg_cgx)), cgp,
263 fs->fs_cgsize) == -1) {
264 ERROR(disk, "unable to write cylinder group");