1 /* 2 * Copyright (c) 2003 Juli Mallett. All rights reserved. 3 * 4 * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the 5 * FreeBSD project. Redistribution and use in source and binary forms, with 6 * or without modification, are permitted provided that the following 7 * conditions are met: 8 * 9 * 1. Redistribution of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 2. Redistribution in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/mount.h> 33 #include <sys/disklabel.h> 34 #include <sys/stat.h> 35 36 #include <ufs/ufs/ufsmount.h> 37 #include <ufs/ufs/dinode.h> 38 #include <ufs/ffs/fs.h> 39 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 #include <libufs.h> 48 49 ufs2_daddr_t 50 cgballoc(struct uufsd *disk) 51 { 52 u_int8_t *blksfree; 53 struct cg *cgp; 54 struct fs *fs; 55 long bno; 56 57 fs = &disk->d_fs; 58 cgp = &disk->d_cg; 59 blksfree = cg_blksfree(cgp); 60 for (bno = 0; bno < fs->fs_fpg / fs->fs_frag; bno++) 61 if (ffs_isblock(fs, blksfree, bno)) 62 goto gotit; 63 return (0); 64 gotit: 65 fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; 66 ffs_clrblock(fs, blksfree, (long)bno); 67 ffs_clusteracct(fs, cgp, bno, -1); 68 cgp->cg_cs.cs_nbfree--; 69 fs->fs_cstotal.cs_nbfree--; 70 fs->fs_fmod = 1; 71 return (cgbase(fs, cgp->cg_cgx) + blkstofrags(fs, bno)); 72 } 73 74 int 75 cgbfree(struct uufsd *disk, ufs2_daddr_t bno, long size) 76 { 77 u_int8_t *blksfree; 78 struct fs *fs; 79 struct cg *cgp; 80 ufs1_daddr_t fragno, cgbno; 81 int i, cg, blk, frags, bbase; 82 83 fs = &disk->d_fs; 84 cg = dtog(fs, bno); 85 if (cgread1(disk, cg) != 1) 86 return (-1); 87 cgp = &disk->d_cg; 88 cgbno = dtogd(fs, bno); 89 blksfree = cg_blksfree(cgp); 90 if (size == fs->fs_bsize) { 91 fragno = fragstoblks(fs, cgbno); 92 ffs_setblock(fs, blksfree, fragno); 93 ffs_clusteracct(fs, cgp, fragno, 1); 94 cgp->cg_cs.cs_nbfree++; 95 fs->fs_cstotal.cs_nbfree++; 96 fs->fs_cs(fs, cg).cs_nbfree++; 97 } else { 98 bbase = cgbno - fragnum(fs, cgbno); 99 /* 100 * decrement the counts associated with the old frags 101 */ 102 blk = blkmap(fs, blksfree, bbase); 103 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 104 /* 105 * deallocate the fragment 106 */ 107 frags = numfrags(fs, size); 108 for (i = 0; i < frags; i++) 109 setbit(blksfree, cgbno + i); 110 cgp->cg_cs.cs_nffree += i; 111 fs->fs_cstotal.cs_nffree += i; 112 fs->fs_cs(fs, cg).cs_nffree += i; 113 /* 114 * add back in counts associated with the new frags 115 */ 116 blk = blkmap(fs, blksfree, bbase); 117 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 118 /* 119 * if a complete block has been reassembled, account for it 120 */ 121 fragno = fragstoblks(fs, bbase); 122 if (ffs_isblock(fs, blksfree, fragno)) { 123 cgp->cg_cs.cs_nffree -= fs->fs_frag; 124 fs->fs_cstotal.cs_nffree -= fs->fs_frag; 125 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; 126 ffs_clusteracct(fs, cgp, fragno, 1); 127 cgp->cg_cs.cs_nbfree++; 128 fs->fs_cstotal.cs_nbfree++; 129 fs->fs_cs(fs, cg).cs_nbfree++; 130 } 131 } 132 return cgwrite(disk); 133 } 134 135 ino_t 136 cgialloc(struct uufsd *disk) 137 { 138 struct ufs2_dinode *dp2; 139 u_int8_t *inosused; 140 struct cg *cgp; 141 struct fs *fs; 142 ino_t ino; 143 int i; 144 145 fs = &disk->d_fs; 146 cgp = &disk->d_cg; 147 inosused = cg_inosused(cgp); 148 for (ino = 0; ino < fs->fs_ipg; ino++) 149 if (isclr(inosused, ino)) 150 goto gotit; 151 return (0); 152 gotit: 153 if (fs->fs_magic == FS_UFS2_MAGIC && 154 ino + INOPB(fs) > cgp->cg_initediblk && 155 cgp->cg_initediblk < cgp->cg_niblk) { 156 char block[MAXBSIZE]; 157 bzero(block, (int)fs->fs_bsize); 158 dp2 = (struct ufs2_dinode *)█ 159 for (i = 0; i < INOPB(fs); i++) { 160 dp2->di_gen = arc4random(); 161 dp2++; 162 } 163 if (bwrite(disk, ino_to_fsba(fs, 164 cgp->cg_cgx * fs->fs_ipg + cgp->cg_initediblk), 165 block, fs->fs_bsize)) 166 return (0); 167 cgp->cg_initediblk += INOPB(fs); 168 } 169 170 setbit(inosused, ino); 171 cgp->cg_irotor = ino; 172 cgp->cg_cs.cs_nifree--; 173 fs->fs_cstotal.cs_nifree--; 174 fs->fs_cs(fs, cgp->cg_cgx).cs_nifree--; 175 fs->fs_fmod = 1; 176 177 return (ino + (cgp->cg_cgx * fs->fs_ipg)); 178 } 179 180 int 181 cgread(struct uufsd *disk) 182 { 183 return (cgread1(disk, disk->d_ccg++)); 184 } 185 186 int 187 cgread1(struct uufsd *disk, int c) 188 { 189 struct fs *fs; 190 191 fs = &disk->d_fs; 192 193 if ((unsigned)c >= fs->fs_ncg) { 194 return (0); 195 } 196 if (bread(disk, fsbtodb(fs, cgtod(fs, c)), disk->d_cgunion.d_buf, 197 fs->fs_bsize) == -1) { 198 ERROR(disk, "unable to read cylinder group"); 199 return (-1); 200 } 201 disk->d_lcg = c; 202 return (1); 203 } 204 205 int 206 cgwrite(struct uufsd *disk) 207 { 208 return (cgwrite1(disk, disk->d_lcg)); 209 } 210 211 int 212 cgwrite1(struct uufsd *disk, int c) 213 { 214 struct fs *fs; 215 216 fs = &disk->d_fs; 217 if (bwrite(disk, fsbtodb(fs, cgtod(fs, c)), 218 disk->d_cgunion.d_buf, fs->fs_bsize) == -1) { 219 ERROR(disk, "unable to write cylinder group"); 220 return (-1); 221 } 222 return (0); 223 } 224