Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
7188ac27 | 2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. |
202a4bd9 | 3 | * All rights reserved. |
da7c5cc6 | 4 | * |
b702c21d | 5 | * %sccs.include.redist.c% |
202a4bd9 | 6 | * |
9cf42d55 | 7 | * @(#)lfs_alloc.c 7.21 (Berkeley) %G% |
da7c5cc6 | 8 | */ |
e3fe2d69 | 9 | |
94368568 JB |
10 | #include "param.h" |
11 | #include "systm.h" | |
94368568 | 12 | #include "buf.h" |
94368568 | 13 | #include "user.h" |
7188ac27 | 14 | #include "vnode.h" |
94368568 | 15 | #include "kernel.h" |
7580177b | 16 | #include "syslog.h" |
aec7dd3b | 17 | #include "cmap.h" |
7188ac27 KM |
18 | #include "../ufs/quota.h" |
19 | #include "../ufs/inode.h" | |
20 | #include "../ufs/fs.h" | |
e3fe2d69 | 21 | |
daaf7bee | 22 | extern u_long hashalloc(); |
4f083fd7 SL |
23 | extern ino_t ialloccg(); |
24 | extern daddr_t alloccg(); | |
743f1ef7 KM |
25 | extern daddr_t alloccgblk(); |
26 | extern daddr_t fragextend(); | |
27 | extern daddr_t blkpref(); | |
28 | extern daddr_t mapsearch(); | |
1d7a08c5 | 29 | extern int inside[], around[]; |
b6407c9d | 30 | extern unsigned char *fragtbl[]; |
e3fe2d69 | 31 | |
502770a3 KM |
32 | /* |
33 | * Allocate a block in the file system. | |
34 | * | |
35 | * The size of the requested block is given, which must be some | |
36 | * multiple of fs_fsize and <= fs_bsize. | |
37 | * A preference may be optionally specified. If a preference is given | |
38 | * the following hierarchy is used to allocate a block: | |
39 | * 1) allocate the requested block. | |
40 | * 2) allocate a rotationally optimal block in the same cylinder. | |
41 | * 3) allocate a block in the same cylinder group. | |
42 | * 4) quadradically rehash into other cylinder groups, until an | |
43 | * available block is located. | |
44 | * If no block preference is given the following heirarchy is used | |
45 | * to allocate a block: | |
46 | * 1) allocate a block in the cylinder group that contains the | |
47 | * inode for the file. | |
48 | * 2) quadradically rehash into other cylinder groups, until an | |
49 | * available block is located. | |
50 | */ | |
0c36425b | 51 | alloc(ip, lbn, bpref, size, bnp) |
f3c028b7 | 52 | register struct inode *ip; |
0c36425b | 53 | daddr_t lbn, bpref; |
e3fe2d69 | 54 | int size; |
0c36425b | 55 | daddr_t *bnp; |
e3fe2d69 KM |
56 | { |
57 | daddr_t bno; | |
58 | register struct fs *fs; | |
f3c028b7 | 59 | register struct buf *bp; |
7188ac27 | 60 | int cg, error; |
00bff950 | 61 | struct ucred *cred = u.u_cred; /* XXX */ |
e3fe2d69 | 62 | |
0c36425b | 63 | *bnp = 0; |
f7287e4b | 64 | fs = ip->i_fs; |
ffd90e52 KM |
65 | if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { |
66 | printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", | |
67 | ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); | |
b6407c9d | 68 | panic("alloc: bad size"); |
ffd90e52 | 69 | } |
b6407c9d | 70 | if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) |
0947395d | 71 | goto nospace; |
00bff950 | 72 | if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) |
e3fe2d69 | 73 | goto nospace; |
b4567e9c | 74 | #ifdef QUOTA |
00bff950 | 75 | if (error = chkdq(ip, (long)btodb(size), cred, 0)) |
7188ac27 | 76 | return (error); |
ca90a6bf | 77 | #endif |
260e5e3c KM |
78 | if (bpref >= fs->fs_size) |
79 | bpref = 0; | |
e3fe2d69 | 80 | if (bpref == 0) |
6994bf5d | 81 | cg = itog(fs, ip->i_number); |
e3fe2d69 | 82 | else |
6994bf5d | 83 | cg = dtog(fs, bpref); |
4f083fd7 SL |
84 | bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, |
85 | (u_long (*)())alloccg); | |
0c36425b KM |
86 | if (bno > 0) { |
87 | ip->i_blocks += btodb(size); | |
88 | ip->i_flag |= IUPD|ICHG; | |
89 | *bnp = bno; | |
90 | return (0); | |
91 | } | |
e3fe2d69 | 92 | nospace: |
e2546752 | 93 | fserr(fs, cred->cr_uid, "file system full"); |
e3fe2d69 | 94 | uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); |
7188ac27 | 95 | return (ENOSPC); |
e3fe2d69 KM |
96 | } |
97 | ||
502770a3 KM |
98 | /* |
99 | * Reallocate a fragment to a bigger size | |
100 | * | |
101 | * The number and size of the old block is given, and a preference | |
102 | * and new size is also specified. The allocator attempts to extend | |
103 | * the original block. Failing that, the regular block allocator is | |
104 | * invoked to get an appropriate block. | |
105 | */ | |
0c36425b | 106 | realloccg(ip, lbprev, bpref, osize, nsize, bpp) |
f7287e4b | 107 | register struct inode *ip; |
0c36425b KM |
108 | off_t lbprev; |
109 | daddr_t bpref; | |
07670f7d | 110 | int osize, nsize; |
7188ac27 | 111 | struct buf **bpp; |
07670f7d | 112 | { |
07670f7d | 113 | register struct fs *fs; |
7188ac27 | 114 | struct buf *bp, *obp; |
f7fa0c54 | 115 | int cg, request; |
0c36425b | 116 | daddr_t bprev, bno, bn; |
7188ac27 | 117 | int i, error, count; |
00bff950 | 118 | struct ucred *cred = u.u_cred; /* XXX */ |
07670f7d | 119 | |
7188ac27 | 120 | *bpp = 0; |
f7287e4b | 121 | fs = ip->i_fs; |
d995d89d | 122 | if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || |
ffd90e52 KM |
123 | (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { |
124 | printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", | |
125 | ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); | |
b6407c9d | 126 | panic("realloccg: bad size"); |
ffd90e52 | 127 | } |
00bff950 | 128 | if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) |
0947395d | 129 | goto nospace; |
0c36425b | 130 | if ((bprev = ip->i_db[lbprev]) == 0) { |
ffd90e52 KM |
131 | printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", |
132 | ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); | |
502770a3 | 133 | panic("realloccg: bad bprev"); |
ffd90e52 | 134 | } |
b4567e9c | 135 | #ifdef QUOTA |
00bff950 | 136 | if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) |
7188ac27 | 137 | return (error); |
ca90a6bf | 138 | #endif |
0c36425b KM |
139 | /* |
140 | * Allocate the extra space in the buffer. | |
141 | */ | |
142 | if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) { | |
143 | brelse(bp); | |
144 | return (error); | |
145 | } | |
9cf42d55 | 146 | allocbuf(bp, nsize); |
0c36425b KM |
147 | bp->b_flags |= B_DONE; |
148 | bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); | |
0c36425b KM |
149 | /* |
150 | * Check for extension in the existing location. | |
151 | */ | |
ae851115 | 152 | cg = dtog(fs, bprev); |
0c36425b | 153 | if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) { |
72ef728e | 154 | if (bp->b_blkno != fsbtodb(fs, bno)) |
0c36425b | 155 | panic("bad blockno"); |
2393702a KM |
156 | ip->i_blocks += btodb(nsize - osize); |
157 | ip->i_flag |= IUPD|ICHG; | |
7188ac27 KM |
158 | *bpp = bp; |
159 | return (0); | |
f3c028b7 | 160 | } |
0c36425b KM |
161 | /* |
162 | * Allocate a new disk location. | |
163 | */ | |
260e5e3c KM |
164 | if (bpref >= fs->fs_size) |
165 | bpref = 0; | |
aec7dd3b | 166 | switch ((int)fs->fs_optim) { |
f8484b5f KM |
167 | case FS_OPTSPACE: |
168 | /* | |
169 | * Allocate an exact sized fragment. Although this makes | |
170 | * best use of space, we will waste time relocating it if | |
171 | * the file continues to grow. If the fragmentation is | |
172 | * less than half of the minimum free reserve, we choose | |
173 | * to begin optimizing for time. | |
174 | */ | |
f7fa0c54 | 175 | request = nsize; |
f8484b5f KM |
176 | if (fs->fs_minfree < 5 || |
177 | fs->fs_cstotal.cs_nffree > | |
178 | fs->fs_dsize * fs->fs_minfree / (2 * 100)) | |
179 | break; | |
180 | log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", | |
181 | fs->fs_fsmnt); | |
182 | fs->fs_optim = FS_OPTTIME; | |
183 | break; | |
184 | case FS_OPTTIME: | |
185 | /* | |
186 | * At this point we have discovered a file that is trying | |
187 | * to grow a small fragment to a larger fragment. To save | |
188 | * time, we allocate a full sized block, then free the | |
189 | * unused portion. If the file continues to grow, the | |
190 | * `fragextend' call above will be able to grow it in place | |
191 | * without further copying. If aberrant programs cause | |
192 | * disk fragmentation to grow within 2% of the free reserve, | |
193 | * we choose to begin optimizing for space. | |
194 | */ | |
f7fa0c54 | 195 | request = fs->fs_bsize; |
f8484b5f KM |
196 | if (fs->fs_cstotal.cs_nffree < |
197 | fs->fs_dsize * (fs->fs_minfree - 2) / 100) | |
198 | break; | |
199 | log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", | |
200 | fs->fs_fsmnt); | |
201 | fs->fs_optim = FS_OPTSPACE; | |
202 | break; | |
203 | default: | |
204 | printf("dev = 0x%x, optim = %d, fs = %s\n", | |
205 | ip->i_dev, fs->fs_optim, fs->fs_fsmnt); | |
206 | panic("realloccg: bad optim"); | |
207 | /* NOTREACHED */ | |
208 | } | |
f7fa0c54 | 209 | bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, |
4f083fd7 | 210 | (u_long (*)())alloccg); |
6459ebe0 | 211 | if (bno > 0) { |
ec67a3ce MK |
212 | #ifdef SECSIZE |
213 | obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize, | |
214 | fs->fs_dbsize); | |
215 | #else SECSIZE | |
0c36425b | 216 | bp->b_blkno = bn = fsbtodb(fs, bno); |
ec67a3ce MK |
217 | count = howmany(osize, CLBYTES); |
218 | for (i = 0; i < count; i++) | |
219 | #ifdef SECSIZE | |
220 | munhash(ip->i_dev, bn + i * CLBYTES / fs->fs_dbsize); | |
221 | #else SECSIZE | |
222 | munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE); | |
223 | #endif SECSIZE | |
ced3a252 | 224 | blkfree(ip, bprev, (off_t)osize); |
f7fa0c54 | 225 | if (nsize < request) |
ced3a252 | 226 | blkfree(ip, bno + numfrags(fs, nsize), |
f7fa0c54 | 227 | (off_t)(request - nsize)); |
2523b389 SL |
228 | ip->i_blocks += btodb(nsize - osize); |
229 | ip->i_flag |= IUPD|ICHG; | |
7188ac27 KM |
230 | *bpp = bp; |
231 | return (0); | |
f3c028b7 | 232 | } |
0c36425b | 233 | brelse(bp); |
0947395d | 234 | nospace: |
f3c028b7 KM |
235 | /* |
236 | * no space available | |
237 | */ | |
e2546752 | 238 | fserr(fs, cred->cr_uid, "file system full"); |
07670f7d | 239 | uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); |
7188ac27 | 240 | return (ENOSPC); |
07670f7d KM |
241 | } |
242 | ||
502770a3 KM |
243 | /* |
244 | * Allocate an inode in the file system. | |
245 | * | |
246 | * A preference may be optionally specified. If a preference is given | |
247 | * the following hierarchy is used to allocate an inode: | |
248 | * 1) allocate the requested inode. | |
249 | * 2) allocate an inode in the same cylinder group. | |
250 | * 3) quadradically rehash into other cylinder groups, until an | |
251 | * available inode is located. | |
252 | * If no inode preference is given the following heirarchy is used | |
253 | * to allocate an inode: | |
254 | * 1) allocate an inode in cylinder group 0. | |
255 | * 2) quadradically rehash into other cylinder groups, until an | |
256 | * available inode is located. | |
257 | */ | |
00bff950 | 258 | ialloc(pip, ipref, mode, cred, ipp) |
f7287e4b | 259 | register struct inode *pip; |
e3fe2d69 KM |
260 | ino_t ipref; |
261 | int mode; | |
00bff950 | 262 | struct ucred *cred; |
7188ac27 | 263 | struct inode **ipp; |
e3fe2d69 | 264 | { |
daaf7bee | 265 | ino_t ino; |
e3fe2d69 KM |
266 | register struct fs *fs; |
267 | register struct inode *ip; | |
7188ac27 | 268 | int cg, error; |
e3fe2d69 | 269 | |
7188ac27 | 270 | *ipp = 0; |
f7287e4b | 271 | fs = pip->i_fs; |
0947395d | 272 | if (fs->fs_cstotal.cs_nifree == 0) |
e3fe2d69 | 273 | goto noinodes; |
260e5e3c KM |
274 | if (ipref >= fs->fs_ncg * fs->fs_ipg) |
275 | ipref = 0; | |
6994bf5d | 276 | cg = itog(fs, ipref); |
f7287e4b | 277 | ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); |
e3fe2d69 KM |
278 | if (ino == 0) |
279 | goto noinodes; | |
7188ac27 | 280 | error = iget(pip, ino, ipp); |
7188ac27 | 281 | if (error) { |
00bff950 | 282 | ifree(pip, ino, mode); |
7188ac27 | 283 | return (error); |
e3fe2d69 | 284 | } |
fb92d0ab | 285 | ip = *ipp; |
ffd90e52 KM |
286 | if (ip->i_mode) { |
287 | printf("mode = 0%o, inum = %d, fs = %s\n", | |
288 | ip->i_mode, ip->i_number, fs->fs_fsmnt); | |
e3fe2d69 | 289 | panic("ialloc: dup alloc"); |
ffd90e52 | 290 | } |
2523b389 SL |
291 | if (ip->i_blocks) { /* XXX */ |
292 | printf("free inode %s/%d had %d blocks\n", | |
293 | fs->fs_fsmnt, ino, ip->i_blocks); | |
294 | ip->i_blocks = 0; | |
295 | } | |
7e0dee76 | 296 | ip->i_flags = 0; |
fb92d0ab KM |
297 | /* |
298 | * Set up a new generation number for this inode. | |
299 | */ | |
300 | if (++nextgennumber < (u_long)time.tv_sec) | |
301 | nextgennumber = time.tv_sec; | |
302 | ip->i_gen = nextgennumber; | |
7188ac27 | 303 | return (0); |
e3fe2d69 | 304 | noinodes: |
e2546752 | 305 | fserr(fs, cred->cr_uid, "out of inodes"); |
ae851115 | 306 | uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); |
7188ac27 | 307 | return (ENOSPC); |
e3fe2d69 KM |
308 | } |
309 | ||
743f1ef7 | 310 | /* |
502770a3 KM |
311 | * Find a cylinder to place a directory. |
312 | * | |
313 | * The policy implemented by this algorithm is to select from | |
314 | * among those cylinder groups with above the average number of | |
315 | * free inodes, the one with the smallest number of directories. | |
743f1ef7 | 316 | */ |
4f083fd7 | 317 | ino_t |
f7287e4b | 318 | dirpref(fs) |
e3fe2d69 | 319 | register struct fs *fs; |
f7287e4b | 320 | { |
743f1ef7 | 321 | int cg, minndir, mincg, avgifree; |
e3fe2d69 | 322 | |
0947395d | 323 | avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; |
743f1ef7 | 324 | minndir = fs->fs_ipg; |
e3fe2d69 | 325 | mincg = 0; |
743f1ef7 | 326 | for (cg = 0; cg < fs->fs_ncg; cg++) |
b6407c9d KM |
327 | if (fs->fs_cs(fs, cg).cs_ndir < minndir && |
328 | fs->fs_cs(fs, cg).cs_nifree >= avgifree) { | |
e3fe2d69 | 329 | mincg = cg; |
b6407c9d | 330 | minndir = fs->fs_cs(fs, cg).cs_ndir; |
e3fe2d69 | 331 | } |
4f083fd7 | 332 | return ((ino_t)(fs->fs_ipg * mincg)); |
e3fe2d69 KM |
333 | } |
334 | ||
743f1ef7 | 335 | /* |
4f083fd7 SL |
336 | * Select the desired position for the next block in a file. The file is |
337 | * logically divided into sections. The first section is composed of the | |
338 | * direct blocks. Each additional section contains fs_maxbpg blocks. | |
339 | * | |
340 | * If no blocks have been allocated in the first section, the policy is to | |
341 | * request a block in the same cylinder group as the inode that describes | |
342 | * the file. If no blocks have been allocated in any other section, the | |
343 | * policy is to place the section in a cylinder group with a greater than | |
344 | * average number of free blocks. An appropriate cylinder group is found | |
16e7863f KM |
345 | * by using a rotor that sweeps the cylinder groups. When a new group of |
346 | * blocks is needed, the sweep begins in the cylinder group following the | |
347 | * cylinder group from which the previous allocation was made. The sweep | |
348 | * continues until a cylinder group with greater than the average number | |
349 | * of free blocks is found. If the allocation is for the first block in an | |
350 | * indirect block, the information on the previous allocation is unavailable; | |
351 | * here a best guess is made based upon the logical block number being | |
352 | * allocated. | |
4f083fd7 SL |
353 | * |
354 | * If a section is already partially allocated, the policy is to | |
355 | * contiguously allocate fs_maxcontig blocks. The end of one of these | |
356 | * contiguous blocks and the beginning of the next is physically separated | |
357 | * so that the disk head will be in transit between them for at least | |
358 | * fs_rotdelay milliseconds. This is to allow time for the processor to | |
359 | * schedule another I/O transfer. | |
743f1ef7 | 360 | */ |
daaf7bee | 361 | daddr_t |
4f083fd7 SL |
362 | blkpref(ip, lbn, indx, bap) |
363 | struct inode *ip; | |
364 | daddr_t lbn; | |
365 | int indx; | |
366 | daddr_t *bap; | |
f7287e4b | 367 | { |
4f083fd7 | 368 | register struct fs *fs; |
16e7863f KM |
369 | register int cg; |
370 | int avgbfree, startcg; | |
4f083fd7 | 371 | daddr_t nextblk; |
743f1ef7 | 372 | |
4f083fd7 SL |
373 | fs = ip->i_fs; |
374 | if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { | |
375 | if (lbn < NDADDR) { | |
376 | cg = itog(fs, ip->i_number); | |
b6407c9d | 377 | return (fs->fs_fpg * cg + fs->fs_frag); |
743f1ef7 | 378 | } |
4f083fd7 SL |
379 | /* |
380 | * Find a cylinder with greater than average number of | |
381 | * unused data blocks. | |
382 | */ | |
16e7863f KM |
383 | if (indx == 0 || bap[indx - 1] == 0) |
384 | startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; | |
385 | else | |
386 | startcg = dtog(fs, bap[indx - 1]) + 1; | |
387 | startcg %= fs->fs_ncg; | |
4f083fd7 | 388 | avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; |
16e7863f | 389 | for (cg = startcg; cg < fs->fs_ncg; cg++) |
4f083fd7 SL |
390 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
391 | fs->fs_cgrotor = cg; | |
392 | return (fs->fs_fpg * cg + fs->fs_frag); | |
393 | } | |
16e7863f | 394 | for (cg = 0; cg <= startcg; cg++) |
4f083fd7 SL |
395 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
396 | fs->fs_cgrotor = cg; | |
397 | return (fs->fs_fpg * cg + fs->fs_frag); | |
398 | } | |
399 | return (NULL); | |
400 | } | |
401 | /* | |
402 | * One or more previous blocks have been laid out. If less | |
403 | * than fs_maxcontig previous blocks are contiguous, the | |
404 | * next block is requested contiguously, otherwise it is | |
405 | * requested rotationally delayed by fs_rotdelay milliseconds. | |
406 | */ | |
407 | nextblk = bap[indx - 1] + fs->fs_frag; | |
408 | if (indx > fs->fs_maxcontig && | |
240a4664 | 409 | bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) |
4f083fd7 SL |
410 | != nextblk) |
411 | return (nextblk); | |
412 | if (fs->fs_rotdelay != 0) | |
413 | /* | |
414 | * Here we convert ms of delay to frags as: | |
415 | * (frags) = (ms) * (rev/sec) * (sect/rev) / | |
416 | * ((sect/frag) * (ms/sec)) | |
417 | * then round up to the next block. | |
418 | */ | |
419 | nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / | |
420 | (NSPF(fs) * 1000), fs->fs_frag); | |
421 | return (nextblk); | |
743f1ef7 KM |
422 | } |
423 | ||
502770a3 KM |
424 | /* |
425 | * Implement the cylinder overflow algorithm. | |
426 | * | |
427 | * The policy implemented by this algorithm is: | |
428 | * 1) allocate the block in its requested cylinder group. | |
429 | * 2) quadradically rehash on the cylinder group number. | |
430 | * 3) brute force search for a free block. | |
431 | */ | |
daaf7bee KM |
432 | /*VARARGS5*/ |
433 | u_long | |
f7287e4b KM |
434 | hashalloc(ip, cg, pref, size, allocator) |
435 | struct inode *ip; | |
e3fe2d69 KM |
436 | int cg; |
437 | long pref; | |
438 | int size; /* size for data blocks, mode for inodes */ | |
daaf7bee | 439 | u_long (*allocator)(); |
e3fe2d69 | 440 | { |
f7287e4b | 441 | register struct fs *fs; |
e3fe2d69 KM |
442 | long result; |
443 | int i, icg = cg; | |
444 | ||
f7287e4b | 445 | fs = ip->i_fs; |
e3fe2d69 KM |
446 | /* |
447 | * 1: preferred cylinder group | |
448 | */ | |
f7287e4b | 449 | result = (*allocator)(ip, cg, pref, size); |
e3fe2d69 KM |
450 | if (result) |
451 | return (result); | |
452 | /* | |
453 | * 2: quadratic rehash | |
454 | */ | |
455 | for (i = 1; i < fs->fs_ncg; i *= 2) { | |
456 | cg += i; | |
457 | if (cg >= fs->fs_ncg) | |
458 | cg -= fs->fs_ncg; | |
f7287e4b | 459 | result = (*allocator)(ip, cg, 0, size); |
e3fe2d69 KM |
460 | if (result) |
461 | return (result); | |
462 | } | |
463 | /* | |
464 | * 3: brute force search | |
620b3290 SL |
465 | * Note that we start at i == 2, since 0 was checked initially, |
466 | * and 1 is always checked in the quadratic rehash. | |
e3fe2d69 | 467 | */ |
2136305e | 468 | cg = (icg + 2) % fs->fs_ncg; |
620b3290 | 469 | for (i = 2; i < fs->fs_ncg; i++) { |
f7287e4b | 470 | result = (*allocator)(ip, cg, 0, size); |
e3fe2d69 KM |
471 | if (result) |
472 | return (result); | |
473 | cg++; | |
474 | if (cg == fs->fs_ncg) | |
475 | cg = 0; | |
476 | } | |
ae851115 | 477 | return (NULL); |
e3fe2d69 KM |
478 | } |
479 | ||
502770a3 KM |
480 | /* |
481 | * Determine whether a fragment can be extended. | |
482 | * | |
483 | * Check to see if the necessary fragments are available, and | |
484 | * if they are, allocate them. | |
485 | */ | |
07670f7d | 486 | daddr_t |
f7287e4b KM |
487 | fragextend(ip, cg, bprev, osize, nsize) |
488 | struct inode *ip; | |
07670f7d | 489 | int cg; |
f3c028b7 | 490 | long bprev; |
07670f7d KM |
491 | int osize, nsize; |
492 | { | |
f7287e4b | 493 | register struct fs *fs; |
f3c028b7 | 494 | register struct cg *cgp; |
7188ac27 | 495 | struct buf *bp; |
f3c028b7 KM |
496 | long bno; |
497 | int frags, bbase; | |
7188ac27 | 498 | int i, error; |
07670f7d | 499 | |
f7287e4b | 500 | fs = ip->i_fs; |
a8580723 | 501 | if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) |
e5476900 | 502 | return (NULL); |
d995d89d | 503 | frags = numfrags(fs, nsize); |
a8580723 KM |
504 | bbase = fragnum(fs, bprev); |
505 | if (bbase > fragnum(fs, (bprev + frags - 1))) { | |
ec67a3ce | 506 | /* cannot extend across a block boundary */ |
ae851115 | 507 | return (NULL); |
f3c028b7 | 508 | } |
ec67a3ce MK |
509 | #ifdef SECSIZE |
510 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, | |
511 | fs->fs_dbsize); | |
512 | #else SECSIZE | |
7188ac27 | 513 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), |
a937f856 | 514 | (int)fs->fs_cgsize, NOCRED, &bp); |
7188ac27 KM |
515 | if (error) { |
516 | brelse(bp); | |
517 | return (NULL); | |
518 | } | |
ec67a3ce | 519 | #endif SECSIZE |
e5476900 | 520 | cgp = bp->b_un.b_cg; |
7188ac27 | 521 | if (!cg_chkmagic(cgp)) { |
d995d89d | 522 | brelse(bp); |
ae851115 | 523 | return (NULL); |
d995d89d | 524 | } |
ad9250ee | 525 | cgp->cg_time = time.tv_sec; |
6994bf5d | 526 | bno = dtogd(fs, bprev); |
d995d89d | 527 | for (i = numfrags(fs, osize); i < frags; i++) |
10adeb11 | 528 | if (isclr(cg_blksfree(cgp), bno + i)) { |
aca50d72 | 529 | brelse(bp); |
ae851115 | 530 | return (NULL); |
aca50d72 KM |
531 | } |
532 | /* | |
533 | * the current fragment can be extended | |
534 | * deduct the count on fragment being extended into | |
535 | * increase the count on the remaining fragment (if any) | |
536 | * allocate the extended piece | |
537 | */ | |
538 | for (i = frags; i < fs->fs_frag - bbase; i++) | |
10adeb11 | 539 | if (isclr(cg_blksfree(cgp), bno + i)) |
f3c028b7 | 540 | break; |
d995d89d | 541 | cgp->cg_frsum[i - numfrags(fs, osize)]--; |
aca50d72 KM |
542 | if (i != frags) |
543 | cgp->cg_frsum[i - frags]++; | |
d995d89d | 544 | for (i = numfrags(fs, osize); i < frags; i++) { |
10adeb11 | 545 | clrbit(cg_blksfree(cgp), bno + i); |
aca50d72 KM |
546 | cgp->cg_cs.cs_nffree--; |
547 | fs->fs_cstotal.cs_nffree--; | |
548 | fs->fs_cs(fs, cg).cs_nffree--; | |
f3c028b7 | 549 | } |
aca50d72 KM |
550 | fs->fs_fmod++; |
551 | bdwrite(bp); | |
552 | return (bprev); | |
07670f7d KM |
553 | } |
554 | ||
502770a3 KM |
555 | /* |
556 | * Determine whether a block can be allocated. | |
557 | * | |
558 | * Check to see if a block of the apprpriate size is available, | |
559 | * and if it is, allocate it. | |
560 | */ | |
4f083fd7 | 561 | daddr_t |
f7287e4b KM |
562 | alloccg(ip, cg, bpref, size) |
563 | struct inode *ip; | |
e3fe2d69 KM |
564 | int cg; |
565 | daddr_t bpref; | |
566 | int size; | |
567 | { | |
f7287e4b | 568 | register struct fs *fs; |
f3c028b7 | 569 | register struct cg *cgp; |
7188ac27 | 570 | struct buf *bp; |
f3c028b7 | 571 | register int i; |
7188ac27 | 572 | int error, bno, frags, allocsiz; |
e3fe2d69 | 573 | |
f7287e4b | 574 | fs = ip->i_fs; |
b6407c9d | 575 | if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) |
ae851115 | 576 | return (NULL); |
ec67a3ce MK |
577 | #ifdef SECSIZE |
578 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, | |
579 | fs->fs_dbsize); | |
580 | #else SECSIZE | |
7188ac27 | 581 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), |
a937f856 | 582 | (int)fs->fs_cgsize, NOCRED, &bp); |
7188ac27 KM |
583 | if (error) { |
584 | brelse(bp); | |
585 | return (NULL); | |
586 | } | |
ec67a3ce | 587 | #endif SECSIZE |
e5476900 | 588 | cgp = bp->b_un.b_cg; |
7188ac27 | 589 | if (!cg_chkmagic(cgp) || |
0f538882 | 590 | (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { |
d995d89d | 591 | brelse(bp); |
ae851115 | 592 | return (NULL); |
d995d89d | 593 | } |
ad9250ee | 594 | cgp->cg_time = time.tv_sec; |
b6407c9d | 595 | if (size == fs->fs_bsize) { |
daaf7bee | 596 | bno = alloccgblk(fs, cgp, bpref); |
f3c028b7 KM |
597 | bdwrite(bp); |
598 | return (bno); | |
599 | } | |
600 | /* | |
601 | * check to see if any fragments are already available | |
602 | * allocsiz is the size which will be allocated, hacking | |
603 | * it down to a smaller size if necessary | |
604 | */ | |
d995d89d | 605 | frags = numfrags(fs, size); |
b6407c9d | 606 | for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) |
f3c028b7 KM |
607 | if (cgp->cg_frsum[allocsiz] != 0) |
608 | break; | |
b6407c9d | 609 | if (allocsiz == fs->fs_frag) { |
f3c028b7 KM |
610 | /* |
611 | * no fragments were available, so a block will be | |
612 | * allocated, and hacked up | |
613 | */ | |
0947395d | 614 | if (cgp->cg_cs.cs_nbfree == 0) { |
f3c028b7 | 615 | brelse(bp); |
ae851115 | 616 | return (NULL); |
f3c028b7 | 617 | } |
daaf7bee | 618 | bno = alloccgblk(fs, cgp, bpref); |
6994bf5d | 619 | bpref = dtogd(fs, bno); |
b6407c9d | 620 | for (i = frags; i < fs->fs_frag; i++) |
10adeb11 | 621 | setbit(cg_blksfree(cgp), bpref + i); |
b6407c9d | 622 | i = fs->fs_frag - frags; |
0947395d KM |
623 | cgp->cg_cs.cs_nffree += i; |
624 | fs->fs_cstotal.cs_nffree += i; | |
b6407c9d | 625 | fs->fs_cs(fs, cg).cs_nffree += i; |
961945a8 | 626 | fs->fs_fmod++; |
f3c028b7 KM |
627 | cgp->cg_frsum[i]++; |
628 | bdwrite(bp); | |
629 | return (bno); | |
630 | } | |
743f1ef7 | 631 | bno = mapsearch(fs, cgp, bpref, allocsiz); |
0f538882 KM |
632 | if (bno < 0) { |
633 | brelse(bp); | |
ae851115 | 634 | return (NULL); |
0f538882 | 635 | } |
f3c028b7 | 636 | for (i = 0; i < frags; i++) |
10adeb11 | 637 | clrbit(cg_blksfree(cgp), bno + i); |
0947395d KM |
638 | cgp->cg_cs.cs_nffree -= frags; |
639 | fs->fs_cstotal.cs_nffree -= frags; | |
b6407c9d | 640 | fs->fs_cs(fs, cg).cs_nffree -= frags; |
961945a8 | 641 | fs->fs_fmod++; |
f3c028b7 KM |
642 | cgp->cg_frsum[allocsiz]--; |
643 | if (frags != allocsiz) | |
644 | cgp->cg_frsum[allocsiz - frags]++; | |
645 | bdwrite(bp); | |
646 | return (cg * fs->fs_fpg + bno); | |
647 | } | |
648 | ||
502770a3 KM |
649 | /* |
650 | * Allocate a block in a cylinder group. | |
651 | * | |
652 | * This algorithm implements the following policy: | |
653 | * 1) allocate the requested block. | |
654 | * 2) allocate a rotationally optimal block in the same cylinder. | |
655 | * 3) allocate the next available block on the block rotor for the | |
656 | * specified cylinder group. | |
657 | * Note that this routine only allocates fs_bsize blocks; these | |
658 | * blocks may be fragmented by the routine that allocates them. | |
659 | */ | |
f3c028b7 | 660 | daddr_t |
daaf7bee | 661 | alloccgblk(fs, cgp, bpref) |
f7287e4b | 662 | register struct fs *fs; |
f3c028b7 KM |
663 | register struct cg *cgp; |
664 | daddr_t bpref; | |
665 | { | |
743f1ef7 | 666 | daddr_t bno; |
ae851115 | 667 | int cylno, pos, delta; |
743f1ef7 | 668 | short *cylbp; |
aca50d72 | 669 | register int i; |
f3c028b7 | 670 | |
743f1ef7 KM |
671 | if (bpref == 0) { |
672 | bpref = cgp->cg_rotor; | |
aca50d72 KM |
673 | goto norot; |
674 | } | |
a8580723 | 675 | bpref = blknum(fs, bpref); |
6994bf5d | 676 | bpref = dtogd(fs, bpref); |
aca50d72 KM |
677 | /* |
678 | * if the requested block is available, use it | |
679 | */ | |
10adeb11 | 680 | if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) { |
aca50d72 KM |
681 | bno = bpref; |
682 | goto gotit; | |
683 | } | |
aca50d72 KM |
684 | /* |
685 | * check for a block available on the same cylinder | |
aca50d72 KM |
686 | */ |
687 | cylno = cbtocylno(fs, bpref); | |
10adeb11 | 688 | if (cg_blktot(cgp)[cylno] == 0) |
502770a3 KM |
689 | goto norot; |
690 | if (fs->fs_cpc == 0) { | |
691 | /* | |
692 | * block layout info is not available, so just have | |
693 | * to take any block in this cylinder. | |
694 | */ | |
695 | bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); | |
696 | goto norot; | |
697 | } | |
aca50d72 KM |
698 | /* |
699 | * check the summary information to see if a block is | |
700 | * available in the requested cylinder starting at the | |
4f083fd7 | 701 | * requested rotational position and proceeding around. |
aca50d72 | 702 | */ |
10adeb11 | 703 | cylbp = cg_blks(fs, cgp, cylno); |
4f083fd7 | 704 | pos = cbtorpos(fs, bpref); |
10adeb11 | 705 | for (i = pos; i < fs->fs_nrpos; i++) |
aca50d72 KM |
706 | if (cylbp[i] > 0) |
707 | break; | |
10adeb11 | 708 | if (i == fs->fs_nrpos) |
aca50d72 | 709 | for (i = 0; i < pos; i++) |
743f1ef7 KM |
710 | if (cylbp[i] > 0) |
711 | break; | |
aca50d72 KM |
712 | if (cylbp[i] > 0) { |
713 | /* | |
714 | * found a rotational position, now find the actual | |
715 | * block. A panic if none is actually there. | |
716 | */ | |
717 | pos = cylno % fs->fs_cpc; | |
718 | bno = (cylno - pos) * fs->fs_spc / NSPB(fs); | |
10adeb11 | 719 | if (fs_postbl(fs, pos)[i] == -1) { |
ffd90e52 KM |
720 | printf("pos = %d, i = %d, fs = %s\n", |
721 | pos, i, fs->fs_fsmnt); | |
aca50d72 | 722 | panic("alloccgblk: cyl groups corrupted"); |
ffd90e52 | 723 | } |
10adeb11 KM |
724 | for (i = fs_postbl(fs, pos)[i];; ) { |
725 | if (isblock(fs, cg_blksfree(cgp), bno + i)) { | |
240a4664 | 726 | bno = blkstofrags(fs, (bno + i)); |
aca50d72 | 727 | goto gotit; |
743f1ef7 | 728 | } |
10adeb11 KM |
729 | delta = fs_rotbl(fs)[i]; |
730 | if (delta <= 0 || | |
731 | delta + i > fragstoblks(fs, fs->fs_fpg)) | |
aca50d72 | 732 | break; |
ae851115 | 733 | i += delta; |
743f1ef7 | 734 | } |
ffd90e52 | 735 | printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); |
aca50d72 | 736 | panic("alloccgblk: can't find blk in cyl"); |
e3fe2d69 | 737 | } |
aca50d72 KM |
738 | norot: |
739 | /* | |
740 | * no blocks in the requested cylinder, so take next | |
741 | * available one in this cylinder group. | |
742 | */ | |
b32450f4 | 743 | bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); |
6459ebe0 | 744 | if (bno < 0) |
ae851115 | 745 | return (NULL); |
743f1ef7 | 746 | cgp->cg_rotor = bno; |
e3fe2d69 | 747 | gotit: |
10adeb11 | 748 | clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno)); |
0947395d KM |
749 | cgp->cg_cs.cs_nbfree--; |
750 | fs->fs_cstotal.cs_nbfree--; | |
b6407c9d | 751 | fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; |
502770a3 | 752 | cylno = cbtocylno(fs, bno); |
10adeb11 KM |
753 | cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--; |
754 | cg_blktot(cgp)[cylno]--; | |
e3fe2d69 | 755 | fs->fs_fmod++; |
743f1ef7 | 756 | return (cgp->cg_cgx * fs->fs_fpg + bno); |
e3fe2d69 | 757 | } |
10adeb11 | 758 | |
502770a3 KM |
759 | /* |
760 | * Determine whether an inode can be allocated. | |
761 | * | |
762 | * Check to see if an inode is available, and if it is, | |
763 | * allocate it using the following policy: | |
764 | * 1) allocate the requested inode. | |
765 | * 2) allocate the next available inode after the requested | |
766 | * inode in the specified cylinder group. | |
767 | */ | |
4f083fd7 | 768 | ino_t |
f7287e4b KM |
769 | ialloccg(ip, cg, ipref, mode) |
770 | struct inode *ip; | |
e3fe2d69 KM |
771 | int cg; |
772 | daddr_t ipref; | |
773 | int mode; | |
774 | { | |
f7287e4b | 775 | register struct fs *fs; |
f3c028b7 | 776 | register struct cg *cgp; |
4e0c7b8a | 777 | struct buf *bp; |
7188ac27 | 778 | int error, start, len, loc, map, i; |
e3fe2d69 | 779 | |
f7287e4b | 780 | fs = ip->i_fs; |
b6407c9d | 781 | if (fs->fs_cs(fs, cg).cs_nifree == 0) |
ae851115 | 782 | return (NULL); |
ec67a3ce MK |
783 | #ifdef SECSIZE |
784 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, | |
785 | fs->fs_dbsize); | |
786 | #else SECSIZE | |
7188ac27 | 787 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), |
a937f856 | 788 | (int)fs->fs_cgsize, NOCRED, &bp); |
7188ac27 KM |
789 | if (error) { |
790 | brelse(bp); | |
791 | return (NULL); | |
792 | } | |
ec67a3ce | 793 | #endif SECSIZE |
e5476900 | 794 | cgp = bp->b_un.b_cg; |
7188ac27 | 795 | if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) { |
d995d89d | 796 | brelse(bp); |
ae851115 | 797 | return (NULL); |
d995d89d | 798 | } |
ad9250ee | 799 | cgp->cg_time = time.tv_sec; |
e3fe2d69 KM |
800 | if (ipref) { |
801 | ipref %= fs->fs_ipg; | |
10adeb11 | 802 | if (isclr(cg_inosused(cgp), ipref)) |
e3fe2d69 | 803 | goto gotit; |
4e0c7b8a KM |
804 | } |
805 | start = cgp->cg_irotor / NBBY; | |
806 | len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); | |
10adeb11 | 807 | loc = skpc(0xff, len, &cg_inosused(cgp)[start]); |
4e0c7b8a | 808 | if (loc == 0) { |
e5889092 KM |
809 | len = start + 1; |
810 | start = 0; | |
10adeb11 | 811 | loc = skpc(0xff, len, &cg_inosused(cgp)[0]); |
e5889092 KM |
812 | if (loc == 0) { |
813 | printf("cg = %s, irotor = %d, fs = %s\n", | |
814 | cg, cgp->cg_irotor, fs->fs_fsmnt); | |
815 | panic("ialloccg: map corrupted"); | |
816 | /* NOTREACHED */ | |
817 | } | |
4e0c7b8a KM |
818 | } |
819 | i = start + len - loc; | |
10adeb11 | 820 | map = cg_inosused(cgp)[i]; |
4e0c7b8a KM |
821 | ipref = i * NBBY; |
822 | for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { | |
823 | if ((map & i) == 0) { | |
e3fe2d69 KM |
824 | cgp->cg_irotor = ipref; |
825 | goto gotit; | |
826 | } | |
827 | } | |
4e0c7b8a KM |
828 | printf("fs = %s\n", fs->fs_fsmnt); |
829 | panic("ialloccg: block not in map"); | |
830 | /* NOTREACHED */ | |
e3fe2d69 | 831 | gotit: |
10adeb11 | 832 | setbit(cg_inosused(cgp), ipref); |
0947395d KM |
833 | cgp->cg_cs.cs_nifree--; |
834 | fs->fs_cstotal.cs_nifree--; | |
b6407c9d | 835 | fs->fs_cs(fs, cg).cs_nifree--; |
e3fe2d69 KM |
836 | fs->fs_fmod++; |
837 | if ((mode & IFMT) == IFDIR) { | |
0947395d KM |
838 | cgp->cg_cs.cs_ndir++; |
839 | fs->fs_cstotal.cs_ndir++; | |
b6407c9d | 840 | fs->fs_cs(fs, cg).cs_ndir++; |
e3fe2d69 KM |
841 | } |
842 | bdwrite(bp); | |
843 | return (cg * fs->fs_ipg + ipref); | |
844 | } | |
845 | ||
502770a3 KM |
846 | /* |
847 | * Free a block or fragment. | |
848 | * | |
849 | * The specified block or fragment is placed back in the | |
850 | * free map. If a fragment is deallocated, a possible | |
851 | * block reassembly is checked. | |
852 | */ | |
ced3a252 | 853 | blkfree(ip, bno, size) |
f7287e4b | 854 | register struct inode *ip; |
e3fe2d69 | 855 | daddr_t bno; |
daaf7bee | 856 | off_t size; |
e3fe2d69 KM |
857 | { |
858 | register struct fs *fs; | |
859 | register struct cg *cgp; | |
7188ac27 KM |
860 | struct buf *bp; |
861 | int error, cg, blk, frags, bbase; | |
f3c028b7 | 862 | register int i; |
e2546752 | 863 | struct ucred *cred = u.u_cred; /* XXX */ |
e3fe2d69 | 864 | |
f7287e4b | 865 | fs = ip->i_fs; |
ffd90e52 KM |
866 | if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { |
867 | printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", | |
868 | ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); | |
ced3a252 | 869 | panic("blkfree: bad size"); |
ffd90e52 | 870 | } |
6994bf5d | 871 | cg = dtog(fs, bno); |
e2546752 | 872 | if ((unsigned)bno >= fs->fs_size) { |
6459ebe0 | 873 | printf("bad block %d, ino %d\n", bno, ip->i_number); |
e2546752 | 874 | fserr(fs, cred->cr_uid, "bad block"); |
e3fe2d69 | 875 | return; |
6459ebe0 | 876 | } |
ec67a3ce MK |
877 | #ifdef SECSIZE |
878 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, | |
879 | fs->fs_dbsize); | |
880 | #else SECSIZE | |
7188ac27 | 881 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), |
a937f856 | 882 | (int)fs->fs_cgsize, NOCRED, &bp); |
7188ac27 KM |
883 | if (error) { |
884 | brelse(bp); | |
885 | return; | |
886 | } | |
ec67a3ce | 887 | #endif SECSIZE |
e5476900 | 888 | cgp = bp->b_un.b_cg; |
7188ac27 | 889 | if (!cg_chkmagic(cgp)) { |
d995d89d | 890 | brelse(bp); |
e3fe2d69 | 891 | return; |
d995d89d | 892 | } |
ad9250ee | 893 | cgp->cg_time = time.tv_sec; |
6994bf5d | 894 | bno = dtogd(fs, bno); |
b6407c9d | 895 | if (size == fs->fs_bsize) { |
10adeb11 | 896 | if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) { |
ffd90e52 KM |
897 | printf("dev = 0x%x, block = %d, fs = %s\n", |
898 | ip->i_dev, bno, fs->fs_fsmnt); | |
ced3a252 | 899 | panic("blkfree: freeing free block"); |
6459ebe0 | 900 | } |
10adeb11 | 901 | setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno)); |
0947395d KM |
902 | cgp->cg_cs.cs_nbfree++; |
903 | fs->fs_cstotal.cs_nbfree++; | |
b6407c9d | 904 | fs->fs_cs(fs, cg).cs_nbfree++; |
502770a3 | 905 | i = cbtocylno(fs, bno); |
10adeb11 KM |
906 | cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++; |
907 | cg_blktot(cgp)[i]++; | |
07670f7d | 908 | } else { |
a8580723 | 909 | bbase = bno - fragnum(fs, bno); |
f3c028b7 KM |
910 | /* |
911 | * decrement the counts associated with the old frags | |
912 | */ | |
10adeb11 | 913 | blk = blkmap(fs, cg_blksfree(cgp), bbase); |
b6407c9d | 914 | fragacct(fs, blk, cgp->cg_frsum, -1); |
f3c028b7 KM |
915 | /* |
916 | * deallocate the fragment | |
917 | */ | |
d995d89d | 918 | frags = numfrags(fs, size); |
f3c028b7 | 919 | for (i = 0; i < frags; i++) { |
10adeb11 | 920 | if (isset(cg_blksfree(cgp), bno + i)) { |
ffd90e52 KM |
921 | printf("dev = 0x%x, block = %d, fs = %s\n", |
922 | ip->i_dev, bno + i, fs->fs_fsmnt); | |
ced3a252 | 923 | panic("blkfree: freeing free frag"); |
ffd90e52 | 924 | } |
10adeb11 | 925 | setbit(cg_blksfree(cgp), bno + i); |
07670f7d | 926 | } |
ae851115 KM |
927 | cgp->cg_cs.cs_nffree += i; |
928 | fs->fs_cstotal.cs_nffree += i; | |
929 | fs->fs_cs(fs, cg).cs_nffree += i; | |
f3c028b7 KM |
930 | /* |
931 | * add back in counts associated with the new frags | |
932 | */ | |
10adeb11 | 933 | blk = blkmap(fs, cg_blksfree(cgp), bbase); |
b6407c9d | 934 | fragacct(fs, blk, cgp->cg_frsum, 1); |
f3c028b7 KM |
935 | /* |
936 | * if a complete block has been reassembled, account for it | |
937 | */ | |
9523729e KM |
938 | if (isblock(fs, cg_blksfree(cgp), |
939 | (daddr_t)fragstoblks(fs, bbase))) { | |
b6407c9d KM |
940 | cgp->cg_cs.cs_nffree -= fs->fs_frag; |
941 | fs->fs_cstotal.cs_nffree -= fs->fs_frag; | |
942 | fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; | |
0947395d KM |
943 | cgp->cg_cs.cs_nbfree++; |
944 | fs->fs_cstotal.cs_nbfree++; | |
b6407c9d | 945 | fs->fs_cs(fs, cg).cs_nbfree++; |
502770a3 | 946 | i = cbtocylno(fs, bbase); |
10adeb11 KM |
947 | cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++; |
948 | cg_blktot(cgp)[i]++; | |
07670f7d KM |
949 | } |
950 | } | |
e3fe2d69 | 951 | fs->fs_fmod++; |
e3fe2d69 KM |
952 | bdwrite(bp); |
953 | } | |
954 | ||
502770a3 KM |
955 | /* |
956 | * Free an inode. | |
957 | * | |
958 | * The specified inode is placed back in the free map. | |
959 | */ | |
f7287e4b KM |
960 | ifree(ip, ino, mode) |
961 | struct inode *ip; | |
e3fe2d69 KM |
962 | ino_t ino; |
963 | int mode; | |
964 | { | |
965 | register struct fs *fs; | |
966 | register struct cg *cgp; | |
7188ac27 KM |
967 | struct buf *bp; |
968 | int error, cg; | |
e3fe2d69 | 969 | |
f7287e4b | 970 | fs = ip->i_fs; |
ffd90e52 KM |
971 | if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { |
972 | printf("dev = 0x%x, ino = %d, fs = %s\n", | |
973 | ip->i_dev, ino, fs->fs_fsmnt); | |
e3fe2d69 | 974 | panic("ifree: range"); |
ffd90e52 | 975 | } |
6994bf5d | 976 | cg = itog(fs, ino); |
ec67a3ce MK |
977 | #ifdef SECSIZE |
978 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, | |
979 | fs->fs_dbsize); | |
980 | #else SECSIZE | |
7188ac27 | 981 | error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), |
a937f856 | 982 | (int)fs->fs_cgsize, NOCRED, &bp); |
7188ac27 KM |
983 | if (error) { |
984 | brelse(bp); | |
985 | return; | |
986 | } | |
ec67a3ce | 987 | #endif SECSIZE |
e5476900 | 988 | cgp = bp->b_un.b_cg; |
7188ac27 | 989 | if (!cg_chkmagic(cgp)) { |
d995d89d | 990 | brelse(bp); |
e3fe2d69 | 991 | return; |
d995d89d | 992 | } |
ad9250ee | 993 | cgp->cg_time = time.tv_sec; |
e3fe2d69 | 994 | ino %= fs->fs_ipg; |
10adeb11 | 995 | if (isclr(cg_inosused(cgp), ino)) { |
ffd90e52 KM |
996 | printf("dev = 0x%x, ino = %d, fs = %s\n", |
997 | ip->i_dev, ino, fs->fs_fsmnt); | |
e3fe2d69 | 998 | panic("ifree: freeing free inode"); |
ffd90e52 | 999 | } |
10adeb11 | 1000 | clrbit(cg_inosused(cgp), ino); |
4e0c7b8a KM |
1001 | if (ino < cgp->cg_irotor) |
1002 | cgp->cg_irotor = ino; | |
0947395d KM |
1003 | cgp->cg_cs.cs_nifree++; |
1004 | fs->fs_cstotal.cs_nifree++; | |
b6407c9d | 1005 | fs->fs_cs(fs, cg).cs_nifree++; |
e3fe2d69 | 1006 | if ((mode & IFMT) == IFDIR) { |
0947395d KM |
1007 | cgp->cg_cs.cs_ndir--; |
1008 | fs->fs_cstotal.cs_ndir--; | |
b6407c9d | 1009 | fs->fs_cs(fs, cg).cs_ndir--; |
e3fe2d69 KM |
1010 | } |
1011 | fs->fs_fmod++; | |
1012 | bdwrite(bp); | |
1013 | } | |
1014 | ||
743f1ef7 | 1015 | /* |
502770a3 KM |
1016 | * Find a block of the specified size in the specified cylinder group. |
1017 | * | |
743f1ef7 KM |
1018 | * It is a panic if a request is made to find a block if none are |
1019 | * available. | |
1020 | */ | |
1021 | daddr_t | |
1022 | mapsearch(fs, cgp, bpref, allocsiz) | |
1023 | register struct fs *fs; | |
1024 | register struct cg *cgp; | |
1025 | daddr_t bpref; | |
1026 | int allocsiz; | |
1027 | { | |
1028 | daddr_t bno; | |
1029 | int start, len, loc, i; | |
1030 | int blk, field, subfield, pos; | |
1031 | ||
1032 | /* | |
1033 | * find the fragment by searching through the free block | |
1034 | * map for an appropriate bit pattern | |
1035 | */ | |
1036 | if (bpref) | |
6994bf5d | 1037 | start = dtogd(fs, bpref) / NBBY; |
743f1ef7 KM |
1038 | else |
1039 | start = cgp->cg_frotor / NBBY; | |
942bd18b | 1040 | len = howmany(fs->fs_fpg, NBBY) - start; |
9523729e KM |
1041 | loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start], |
1042 | (u_char *)fragtbl[fs->fs_frag], | |
1043 | (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); | |
743f1ef7 | 1044 | if (loc == 0) { |
e5476900 KM |
1045 | len = start + 1; |
1046 | start = 0; | |
9523729e KM |
1047 | loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0], |
1048 | (u_char *)fragtbl[fs->fs_frag], | |
1049 | (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); | |
4e0c7b8a KM |
1050 | if (loc == 0) { |
1051 | printf("start = %d, len = %d, fs = %s\n", | |
1052 | start, len, fs->fs_fsmnt); | |
1053 | panic("alloccg: map corrupted"); | |
e5889092 | 1054 | /* NOTREACHED */ |
4e0c7b8a | 1055 | } |
743f1ef7 KM |
1056 | } |
1057 | bno = (start + len - loc) * NBBY; | |
1058 | cgp->cg_frotor = bno; | |
1059 | /* | |
1060 | * found the byte in the map | |
1061 | * sift through the bits to find the selected frag | |
1062 | */ | |
ae851115 | 1063 | for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { |
10adeb11 | 1064 | blk = blkmap(fs, cg_blksfree(cgp), bno); |
743f1ef7 KM |
1065 | blk <<= 1; |
1066 | field = around[allocsiz]; | |
1067 | subfield = inside[allocsiz]; | |
b6407c9d | 1068 | for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { |
ae851115 KM |
1069 | if ((blk & field) == subfield) |
1070 | return (bno + pos); | |
743f1ef7 KM |
1071 | field <<= 1; |
1072 | subfield <<= 1; | |
1073 | } | |
1074 | } | |
ffd90e52 | 1075 | printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); |
743f1ef7 | 1076 | panic("alloccg: block not in map"); |
e5476900 | 1077 | return (-1); |
743f1ef7 KM |
1078 | } |
1079 | ||
e3fe2d69 | 1080 | /* |
502770a3 KM |
1081 | * Fserr prints the name of a file system with an error diagnostic. |
1082 | * | |
1083 | * The form of the error message is: | |
e3fe2d69 KM |
1084 | * fs: error message |
1085 | */ | |
e2546752 | 1086 | fserr(fs, uid, cp) |
e3fe2d69 | 1087 | struct fs *fs; |
e2546752 | 1088 | uid_t uid; |
e3fe2d69 KM |
1089 | char *cp; |
1090 | { | |
1091 | ||
e2546752 | 1092 | log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp); |
e3fe2d69 | 1093 | } |