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