Deleted Added
full compact
cgroup.c (203768) cgroup.c (207141)
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 *

--- 12 unchanged lines hidden (view full) ---

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>
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 *

--- 12 unchanged lines hidden (view full) ---

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: head/lib/libufs/cgroup.c 203768 2010-02-11 06:22:38Z mckusick $");
29__FBSDID("$FreeBSD: head/lib/libufs/cgroup.c 207141 2010-04-24 07:05:35Z jeff $");
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>
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>
43#include <string.h>
44#include <unistd.h>
45
46#include <libufs.h>
47
44#include <string.h>
45#include <unistd.h>
46
47#include <libufs.h>
48
49ufs2_daddr_t
50cgballoc(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);
64gotit:
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
48int
74int
75cgbfree(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
135ino_t
136cgialloc(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 / NBBY; ino++)
149 if (isclr(inosused, ino))
150 goto gotit;
151 return (0);
152gotit:
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 *)&block;
159 for (i = 0; i < INOPB(fs); i++) {
160 dp2->di_gen = arc4random() / 2 + 1;
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
180int
49cgread(struct uufsd *disk)
50{
51 return (cgread1(disk, disk->d_ccg++));
52}
53
54int
55cgread1(struct uufsd *disk, int c)
56{
57 struct fs *fs;
181cgread(struct uufsd *disk)
182{
183 return (cgread1(disk, disk->d_ccg++));
184}
185
186int
187cgread1(struct uufsd *disk, int c)
188{
189 struct fs *fs;
58 off_t ccg;
59
60 fs = &disk->d_fs;
61
62 if ((unsigned)c >= fs->fs_ncg) {
63 return (0);
64 }
190
191 fs = &disk->d_fs;
192
193 if ((unsigned)c >= fs->fs_ncg) {
194 return (0);
195 }
65 ccg = fsbtodb(fs, cgtod(fs, c)) * disk->d_bsize;
66 if (bread(disk, fsbtodb(fs, cgtod(fs, c)), disk->d_cgunion.d_buf,
67 fs->fs_bsize) == -1) {
68 ERROR(disk, "unable to read cylinder group");
69 return (-1);
70 }
71 disk->d_lcg = c;
72 return (1);
73}
74
75int
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
205int
206cgwrite(struct uufsd *disk)
207{
208 return (cgwrite1(disk, disk->d_lcg));
209}
210
211int
76cgwrite1(struct uufsd *disk, int c)
77{
78 struct fs *fs;
79
80 fs = &disk->d_fs;
81 if (bwrite(disk, fsbtodb(fs, cgtod(fs, c)),
82 disk->d_cgunion.d_buf, fs->fs_bsize) == -1) {
83 ERROR(disk, "unable to write cylinder group");
84 return (-1);
85 }
86 return (0);
87}
212cgwrite1(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}