macroize cylinder group array references to allow dynamic tables
[unix-history] / usr / src / sys / ufs / lfs / lfs_alloc.c
CommitLineData
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 *
10adeb11 6 * @(#)lfs_alloc.c 7.5 (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 22extern u_long hashalloc();
4f083fd7
SL
23extern ino_t ialloccg();
24extern daddr_t alloccg();
743f1ef7
KM
25extern daddr_t alloccgblk();
26extern daddr_t fragextend();
27extern daddr_t blkpref();
28extern daddr_t mapsearch();
1d7a08c5 29extern int inside[], around[];
b6407c9d 30extern 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 51struct buf *
f7287e4b 52alloc(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);
96nospace:
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 111struct buf *
f7287e4b
KM
112realloccg(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 120 daddr_t bno, bn;
8dbbac04 121 int i, count;
07670f7d 122
f7287e4b 123 fs = ip->i_fs;
d995d89d 124 if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
ffd90e52
KM
125 (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
126 printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
127 ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
b6407c9d 128 panic("realloccg: bad size");
ffd90e52 129 }
240a4664 130 if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
0947395d 131 goto nospace;
ffd90e52
KM
132 if (bprev == 0) {
133 printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
134 ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
502770a3 135 panic("realloccg: bad bprev");
ffd90e52 136 }
b4567e9c 137#ifdef QUOTA
2523b389
SL
138 u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
139 if (u.u_error)
140 return (NULL);
ca90a6bf 141#endif
ae851115 142 cg = dtog(fs, bprev);
f7287e4b 143 bno = fragextend(ip, cg, (long)bprev, osize, nsize);
f3c028b7 144 if (bno != 0) {
9d6d37ce 145 do {
ec67a3ce
MK
146#ifdef SECSIZE
147 bp = bread(ip->i_dev, fsbtodb(fs, bno), osize,
148 fs->fs_dbsize);
149#else SECSIZE
9d6d37ce 150 bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
ec67a3ce 151#endif SECSIZE
9d6d37ce
BJ
152 if (bp->b_flags & B_ERROR) {
153 brelse(bp);
154 return (NULL);
155 }
156 } while (brealloc(bp, nsize) == 0);
157 bp->b_flags |= B_DONE;
4f083fd7 158 bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
2523b389
SL
159 ip->i_blocks += btodb(nsize - osize);
160 ip->i_flag |= IUPD|ICHG;
f3c028b7
KM
161 return (bp);
162 }
260e5e3c
KM
163 if (bpref >= fs->fs_size)
164 bpref = 0;
aec7dd3b 165 switch ((int)fs->fs_optim) {
f8484b5f
KM
166 case FS_OPTSPACE:
167 /*
168 * Allocate an exact sized fragment. Although this makes
169 * best use of space, we will waste time relocating it if
170 * the file continues to grow. If the fragmentation is
171 * less than half of the minimum free reserve, we choose
172 * to begin optimizing for time.
173 */
f7fa0c54 174 request = nsize;
f8484b5f
KM
175 if (fs->fs_minfree < 5 ||
176 fs->fs_cstotal.cs_nffree >
177 fs->fs_dsize * fs->fs_minfree / (2 * 100))
178 break;
179 log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
180 fs->fs_fsmnt);
181 fs->fs_optim = FS_OPTTIME;
182 break;
183 case FS_OPTTIME:
184 /*
185 * At this point we have discovered a file that is trying
186 * to grow a small fragment to a larger fragment. To save
187 * time, we allocate a full sized block, then free the
188 * unused portion. If the file continues to grow, the
189 * `fragextend' call above will be able to grow it in place
190 * without further copying. If aberrant programs cause
191 * disk fragmentation to grow within 2% of the free reserve,
192 * we choose to begin optimizing for space.
193 */
f7fa0c54 194 request = fs->fs_bsize;
f8484b5f
KM
195 if (fs->fs_cstotal.cs_nffree <
196 fs->fs_dsize * (fs->fs_minfree - 2) / 100)
197 break;
198 log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
199 fs->fs_fsmnt);
200 fs->fs_optim = FS_OPTSPACE;
201 break;
202 default:
203 printf("dev = 0x%x, optim = %d, fs = %s\n",
204 ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
205 panic("realloccg: bad optim");
206 /* NOTREACHED */
207 }
f7fa0c54 208 bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
4f083fd7 209 (u_long (*)())alloccg);
6459ebe0 210 if (bno > 0) {
ec67a3ce
MK
211#ifdef SECSIZE
212 obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize,
213 fs->fs_dbsize);
214#else SECSIZE
f7287e4b 215 obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
ec67a3ce 216#endif SECSIZE
d995d89d
KM
217 if (obp->b_flags & B_ERROR) {
218 brelse(obp);
ae851115 219 return (NULL);
d995d89d 220 }
5d934daa 221 bn = fsbtodb(fs, bno);
ec67a3ce
MK
222#ifdef SECSIZE
223 bp = getblk(ip->i_dev, bn, nsize, fs->fs_dbsize);
224#else SECSIZE
5d934daa 225 bp = getblk(ip->i_dev, bn, nsize);
ec67a3ce 226#endif SECSIZE
260ede8e 227 bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
ec67a3ce
MK
228 count = howmany(osize, CLBYTES);
229 for (i = 0; i < count; i++)
230#ifdef SECSIZE
231 munhash(ip->i_dev, bn + i * CLBYTES / fs->fs_dbsize);
232#else SECSIZE
233 munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE);
234#endif SECSIZE
260ede8e
KM
235 bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
236 if (obp->b_flags & B_DELWRI) {
237 obp->b_flags &= ~B_DELWRI;
238 u.u_ru.ru_oublock--; /* delete charge */
239 }
f3c028b7 240 brelse(obp);
ced3a252 241 blkfree(ip, bprev, (off_t)osize);
f7fa0c54 242 if (nsize < request)
ced3a252 243 blkfree(ip, bno + numfrags(fs, nsize),
f7fa0c54 244 (off_t)(request - nsize));
2523b389
SL
245 ip->i_blocks += btodb(nsize - osize);
246 ip->i_flag |= IUPD|ICHG;
ae851115 247 return (bp);
f3c028b7 248 }
0947395d 249nospace:
f3c028b7
KM
250 /*
251 * no space available
252 */
07670f7d
KM
253 fserr(fs, "file system full");
254 uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
255 u.u_error = ENOSPC;
256 return (NULL);
257}
258
502770a3
KM
259/*
260 * Allocate an inode in the file system.
261 *
262 * A preference may be optionally specified. If a preference is given
263 * the following hierarchy is used to allocate an inode:
264 * 1) allocate the requested inode.
265 * 2) allocate an inode in the same cylinder group.
266 * 3) quadradically rehash into other cylinder groups, until an
267 * available inode is located.
268 * If no inode preference is given the following heirarchy is used
269 * to allocate an inode:
270 * 1) allocate an inode in cylinder group 0.
271 * 2) quadradically rehash into other cylinder groups, until an
272 * available inode is located.
273 */
e3fe2d69 274struct inode *
f7287e4b
KM
275ialloc(pip, ipref, mode)
276 register struct inode *pip;
e3fe2d69
KM
277 ino_t ipref;
278 int mode;
279{
daaf7bee 280 ino_t ino;
e3fe2d69
KM
281 register struct fs *fs;
282 register struct inode *ip;
283 int cg;
284
f7287e4b 285 fs = pip->i_fs;
0947395d 286 if (fs->fs_cstotal.cs_nifree == 0)
e3fe2d69 287 goto noinodes;
b4567e9c 288#ifdef QUOTA
2523b389
SL
289 u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
290 if (u.u_error)
291 return (NULL);
ca90a6bf 292#endif
260e5e3c
KM
293 if (ipref >= fs->fs_ncg * fs->fs_ipg)
294 ipref = 0;
6994bf5d 295 cg = itog(fs, ipref);
f7287e4b 296 ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
e3fe2d69
KM
297 if (ino == 0)
298 goto noinodes;
f7287e4b 299 ip = iget(pip->i_dev, pip->i_fs, ino);
e3fe2d69 300 if (ip == NULL) {
bcd8515e 301 ifree(pip, ino, 0);
e3fe2d69
KM
302 return (NULL);
303 }
ffd90e52
KM
304 if (ip->i_mode) {
305 printf("mode = 0%o, inum = %d, fs = %s\n",
306 ip->i_mode, ip->i_number, fs->fs_fsmnt);
e3fe2d69 307 panic("ialloc: dup alloc");
ffd90e52 308 }
2523b389
SL
309 if (ip->i_blocks) { /* XXX */
310 printf("free inode %s/%d had %d blocks\n",
311 fs->fs_fsmnt, ino, ip->i_blocks);
312 ip->i_blocks = 0;
313 }
e3fe2d69
KM
314 return (ip);
315noinodes:
316 fserr(fs, "out of inodes");
ae851115 317 uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
e3fe2d69
KM
318 u.u_error = ENOSPC;
319 return (NULL);
320}
321
743f1ef7 322/*
502770a3
KM
323 * Find a cylinder to place a directory.
324 *
325 * The policy implemented by this algorithm is to select from
326 * among those cylinder groups with above the average number of
327 * free inodes, the one with the smallest number of directories.
743f1ef7 328 */
4f083fd7 329ino_t
f7287e4b 330dirpref(fs)
e3fe2d69 331 register struct fs *fs;
f7287e4b 332{
743f1ef7 333 int cg, minndir, mincg, avgifree;
e3fe2d69 334
0947395d 335 avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
743f1ef7 336 minndir = fs->fs_ipg;
e3fe2d69 337 mincg = 0;
743f1ef7 338 for (cg = 0; cg < fs->fs_ncg; cg++)
b6407c9d
KM
339 if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
340 fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
e3fe2d69 341 mincg = cg;
b6407c9d 342 minndir = fs->fs_cs(fs, cg).cs_ndir;
e3fe2d69 343 }
4f083fd7 344 return ((ino_t)(fs->fs_ipg * mincg));
e3fe2d69
KM
345}
346
743f1ef7 347/*
4f083fd7
SL
348 * Select the desired position for the next block in a file. The file is
349 * logically divided into sections. The first section is composed of the
350 * direct blocks. Each additional section contains fs_maxbpg blocks.
351 *
352 * If no blocks have been allocated in the first section, the policy is to
353 * request a block in the same cylinder group as the inode that describes
354 * the file. If no blocks have been allocated in any other section, the
355 * policy is to place the section in a cylinder group with a greater than
356 * average number of free blocks. An appropriate cylinder group is found
16e7863f
KM
357 * by using a rotor that sweeps the cylinder groups. When a new group of
358 * blocks is needed, the sweep begins in the cylinder group following the
359 * cylinder group from which the previous allocation was made. The sweep
360 * continues until a cylinder group with greater than the average number
361 * of free blocks is found. If the allocation is for the first block in an
362 * indirect block, the information on the previous allocation is unavailable;
363 * here a best guess is made based upon the logical block number being
364 * allocated.
4f083fd7
SL
365 *
366 * If a section is already partially allocated, the policy is to
367 * contiguously allocate fs_maxcontig blocks. The end of one of these
368 * contiguous blocks and the beginning of the next is physically separated
369 * so that the disk head will be in transit between them for at least
370 * fs_rotdelay milliseconds. This is to allow time for the processor to
371 * schedule another I/O transfer.
743f1ef7 372 */
daaf7bee 373daddr_t
4f083fd7
SL
374blkpref(ip, lbn, indx, bap)
375 struct inode *ip;
376 daddr_t lbn;
377 int indx;
378 daddr_t *bap;
f7287e4b 379{
4f083fd7 380 register struct fs *fs;
16e7863f
KM
381 register int cg;
382 int avgbfree, startcg;
4f083fd7 383 daddr_t nextblk;
743f1ef7 384
4f083fd7
SL
385 fs = ip->i_fs;
386 if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
387 if (lbn < NDADDR) {
388 cg = itog(fs, ip->i_number);
b6407c9d 389 return (fs->fs_fpg * cg + fs->fs_frag);
743f1ef7 390 }
4f083fd7
SL
391 /*
392 * Find a cylinder with greater than average number of
393 * unused data blocks.
394 */
16e7863f
KM
395 if (indx == 0 || bap[indx - 1] == 0)
396 startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
397 else
398 startcg = dtog(fs, bap[indx - 1]) + 1;
399 startcg %= fs->fs_ncg;
4f083fd7 400 avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
16e7863f 401 for (cg = startcg; cg < fs->fs_ncg; cg++)
4f083fd7
SL
402 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
403 fs->fs_cgrotor = cg;
404 return (fs->fs_fpg * cg + fs->fs_frag);
405 }
16e7863f 406 for (cg = 0; cg <= startcg; cg++)
4f083fd7
SL
407 if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
408 fs->fs_cgrotor = cg;
409 return (fs->fs_fpg * cg + fs->fs_frag);
410 }
411 return (NULL);
412 }
413 /*
414 * One or more previous blocks have been laid out. If less
415 * than fs_maxcontig previous blocks are contiguous, the
416 * next block is requested contiguously, otherwise it is
417 * requested rotationally delayed by fs_rotdelay milliseconds.
418 */
419 nextblk = bap[indx - 1] + fs->fs_frag;
420 if (indx > fs->fs_maxcontig &&
240a4664 421 bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4f083fd7
SL
422 != nextblk)
423 return (nextblk);
424 if (fs->fs_rotdelay != 0)
425 /*
426 * Here we convert ms of delay to frags as:
427 * (frags) = (ms) * (rev/sec) * (sect/rev) /
428 * ((sect/frag) * (ms/sec))
429 * then round up to the next block.
430 */
431 nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
432 (NSPF(fs) * 1000), fs->fs_frag);
433 return (nextblk);
743f1ef7
KM
434}
435
502770a3
KM
436/*
437 * Implement the cylinder overflow algorithm.
438 *
439 * The policy implemented by this algorithm is:
440 * 1) allocate the block in its requested cylinder group.
441 * 2) quadradically rehash on the cylinder group number.
442 * 3) brute force search for a free block.
443 */
daaf7bee
KM
444/*VARARGS5*/
445u_long
f7287e4b
KM
446hashalloc(ip, cg, pref, size, allocator)
447 struct inode *ip;
e3fe2d69
KM
448 int cg;
449 long pref;
450 int size; /* size for data blocks, mode for inodes */
daaf7bee 451 u_long (*allocator)();
e3fe2d69 452{
f7287e4b 453 register struct fs *fs;
e3fe2d69
KM
454 long result;
455 int i, icg = cg;
456
f7287e4b 457 fs = ip->i_fs;
e3fe2d69
KM
458 /*
459 * 1: preferred cylinder group
460 */
f7287e4b 461 result = (*allocator)(ip, cg, pref, size);
e3fe2d69
KM
462 if (result)
463 return (result);
464 /*
465 * 2: quadratic rehash
466 */
467 for (i = 1; i < fs->fs_ncg; i *= 2) {
468 cg += i;
469 if (cg >= fs->fs_ncg)
470 cg -= fs->fs_ncg;
f7287e4b 471 result = (*allocator)(ip, cg, 0, size);
e3fe2d69
KM
472 if (result)
473 return (result);
474 }
475 /*
476 * 3: brute force search
620b3290
SL
477 * Note that we start at i == 2, since 0 was checked initially,
478 * and 1 is always checked in the quadratic rehash.
e3fe2d69 479 */
2136305e 480 cg = (icg + 2) % fs->fs_ncg;
620b3290 481 for (i = 2; i < fs->fs_ncg; i++) {
f7287e4b 482 result = (*allocator)(ip, cg, 0, size);
e3fe2d69
KM
483 if (result)
484 return (result);
485 cg++;
486 if (cg == fs->fs_ncg)
487 cg = 0;
488 }
ae851115 489 return (NULL);
e3fe2d69
KM
490}
491
502770a3
KM
492/*
493 * Determine whether a fragment can be extended.
494 *
495 * Check to see if the necessary fragments are available, and
496 * if they are, allocate them.
497 */
07670f7d 498daddr_t
f7287e4b
KM
499fragextend(ip, cg, bprev, osize, nsize)
500 struct inode *ip;
07670f7d 501 int cg;
f3c028b7 502 long bprev;
07670f7d
KM
503 int osize, nsize;
504{
f7287e4b 505 register struct fs *fs;
f3c028b7
KM
506 register struct buf *bp;
507 register struct cg *cgp;
508 long bno;
509 int frags, bbase;
07670f7d
KM
510 int i;
511
f7287e4b 512 fs = ip->i_fs;
a8580723 513 if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
e5476900 514 return (NULL);
d995d89d 515 frags = numfrags(fs, nsize);
a8580723
KM
516 bbase = fragnum(fs, bprev);
517 if (bbase > fragnum(fs, (bprev + frags - 1))) {
ec67a3ce 518 /* cannot extend across a block boundary */
ae851115 519 return (NULL);
f3c028b7 520 }
ec67a3ce
MK
521#ifdef SECSIZE
522 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
523 fs->fs_dbsize);
524#else SECSIZE
d65bd829 525 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
ec67a3ce 526#endif SECSIZE
e5476900 527 cgp = bp->b_un.b_cg;
10adeb11 528 if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
d995d89d 529 brelse(bp);
ae851115 530 return (NULL);
d995d89d 531 }
ad9250ee 532 cgp->cg_time = time.tv_sec;
6994bf5d 533 bno = dtogd(fs, bprev);
d995d89d 534 for (i = numfrags(fs, osize); i < frags; i++)
10adeb11 535 if (isclr(cg_blksfree(cgp), bno + i)) {
aca50d72 536 brelse(bp);
ae851115 537 return (NULL);
aca50d72
KM
538 }
539 /*
540 * the current fragment can be extended
541 * deduct the count on fragment being extended into
542 * increase the count on the remaining fragment (if any)
543 * allocate the extended piece
544 */
545 for (i = frags; i < fs->fs_frag - bbase; i++)
10adeb11 546 if (isclr(cg_blksfree(cgp), bno + i))
f3c028b7 547 break;
d995d89d 548 cgp->cg_frsum[i - numfrags(fs, osize)]--;
aca50d72
KM
549 if (i != frags)
550 cgp->cg_frsum[i - frags]++;
d995d89d 551 for (i = numfrags(fs, osize); i < frags; i++) {
10adeb11 552 clrbit(cg_blksfree(cgp), bno + i);
aca50d72
KM
553 cgp->cg_cs.cs_nffree--;
554 fs->fs_cstotal.cs_nffree--;
555 fs->fs_cs(fs, cg).cs_nffree--;
f3c028b7 556 }
aca50d72
KM
557 fs->fs_fmod++;
558 bdwrite(bp);
559 return (bprev);
07670f7d
KM
560}
561
502770a3
KM
562/*
563 * Determine whether a block can be allocated.
564 *
565 * Check to see if a block of the apprpriate size is available,
566 * and if it is, allocate it.
567 */
4f083fd7 568daddr_t
f7287e4b
KM
569alloccg(ip, cg, bpref, size)
570 struct inode *ip;
e3fe2d69
KM
571 int cg;
572 daddr_t bpref;
573 int size;
574{
f7287e4b 575 register struct fs *fs;
f3c028b7
KM
576 register struct buf *bp;
577 register struct cg *cgp;
578 int bno, frags;
579 int allocsiz;
f3c028b7 580 register int i;
e3fe2d69 581
f7287e4b 582 fs = ip->i_fs;
b6407c9d 583 if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
ae851115 584 return (NULL);
ec67a3ce
MK
585#ifdef SECSIZE
586 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
587 fs->fs_dbsize);
588#else SECSIZE
d65bd829 589 bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
ec67a3ce 590#endif SECSIZE
e5476900 591 cgp = bp->b_un.b_cg;
10adeb11 592 if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
0f538882 593 (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
d995d89d 594 brelse(bp);
ae851115 595 return (NULL);
d995d89d 596 }
ad9250ee 597 cgp->cg_time = time.tv_sec;
b6407c9d 598 if (size == fs->fs_bsize) {
daaf7bee 599 bno = alloccgblk(fs, cgp, bpref);
f3c028b7
KM
600 bdwrite(bp);
601 return (bno);
602 }
603 /*
604 * check to see if any fragments are already available
605 * allocsiz is the size which will be allocated, hacking
606 * it down to a smaller size if necessary
607 */
d995d89d 608 frags = numfrags(fs, size);
b6407c9d 609 for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
f3c028b7
KM
610 if (cgp->cg_frsum[allocsiz] != 0)
611 break;
b6407c9d 612 if (allocsiz == fs->fs_frag) {
f3c028b7
KM
613 /*
614 * no fragments were available, so a block will be
615 * allocated, and hacked up
616 */
0947395d 617 if (cgp->cg_cs.cs_nbfree == 0) {
f3c028b7 618 brelse(bp);
ae851115 619 return (NULL);
f3c028b7 620 }
daaf7bee 621 bno = alloccgblk(fs, cgp, bpref);
6994bf5d 622 bpref = dtogd(fs, bno);
b6407c9d 623 for (i = frags; i < fs->fs_frag; i++)
10adeb11 624 setbit(cg_blksfree(cgp), bpref + i);
b6407c9d 625 i = fs->fs_frag - frags;
0947395d
KM
626 cgp->cg_cs.cs_nffree += i;
627 fs->fs_cstotal.cs_nffree += i;
b6407c9d 628 fs->fs_cs(fs, cg).cs_nffree += i;
961945a8 629 fs->fs_fmod++;
f3c028b7
KM
630 cgp->cg_frsum[i]++;
631 bdwrite(bp);
632 return (bno);
633 }
743f1ef7 634 bno = mapsearch(fs, cgp, bpref, allocsiz);
0f538882
KM
635 if (bno < 0) {
636 brelse(bp);
ae851115 637 return (NULL);
0f538882 638 }
f3c028b7 639 for (i = 0; i < frags; i++)
10adeb11 640 clrbit(cg_blksfree(cgp), bno + i);
0947395d
KM
641 cgp->cg_cs.cs_nffree -= frags;
642 fs->fs_cstotal.cs_nffree -= frags;
b6407c9d 643 fs->fs_cs(fs, cg).cs_nffree -= frags;
961945a8 644 fs->fs_fmod++;
f3c028b7
KM
645 cgp->cg_frsum[allocsiz]--;
646 if (frags != allocsiz)
647 cgp->cg_frsum[allocsiz - frags]++;
648 bdwrite(bp);
649 return (cg * fs->fs_fpg + bno);
650}
651
502770a3
KM
652/*
653 * Allocate a block in a cylinder group.
654 *
655 * This algorithm implements the following policy:
656 * 1) allocate the requested block.
657 * 2) allocate a rotationally optimal block in the same cylinder.
658 * 3) allocate the next available block on the block rotor for the
659 * specified cylinder group.
660 * Note that this routine only allocates fs_bsize blocks; these
661 * blocks may be fragmented by the routine that allocates them.
662 */
f3c028b7 663daddr_t
daaf7bee 664alloccgblk(fs, cgp, bpref)
f7287e4b 665 register struct fs *fs;
f3c028b7
KM
666 register struct cg *cgp;
667 daddr_t bpref;
668{
743f1ef7 669 daddr_t bno;
ae851115 670 int cylno, pos, delta;
743f1ef7 671 short *cylbp;
aca50d72 672 register int i;
f3c028b7 673
743f1ef7
KM
674 if (bpref == 0) {
675 bpref = cgp->cg_rotor;
aca50d72
KM
676 goto norot;
677 }
a8580723 678 bpref = blknum(fs, bpref);
6994bf5d 679 bpref = dtogd(fs, bpref);
aca50d72
KM
680 /*
681 * if the requested block is available, use it
682 */
10adeb11 683 if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
aca50d72
KM
684 bno = bpref;
685 goto gotit;
686 }
aca50d72
KM
687 /*
688 * check for a block available on the same cylinder
aca50d72
KM
689 */
690 cylno = cbtocylno(fs, bpref);
10adeb11 691 if (cg_blktot(cgp)[cylno] == 0)
502770a3
KM
692 goto norot;
693 if (fs->fs_cpc == 0) {
694 /*
695 * block layout info is not available, so just have
696 * to take any block in this cylinder.
697 */
698 bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
699 goto norot;
700 }
aca50d72
KM
701 /*
702 * check the summary information to see if a block is
703 * available in the requested cylinder starting at the
4f083fd7 704 * requested rotational position and proceeding around.
aca50d72 705 */
10adeb11 706 cylbp = cg_blks(fs, cgp, cylno);
4f083fd7 707 pos = cbtorpos(fs, bpref);
10adeb11 708 for (i = pos; i < fs->fs_nrpos; i++)
aca50d72
KM
709 if (cylbp[i] > 0)
710 break;
10adeb11 711 if (i == fs->fs_nrpos)
aca50d72 712 for (i = 0; i < pos; i++)
743f1ef7
KM
713 if (cylbp[i] > 0)
714 break;
aca50d72
KM
715 if (cylbp[i] > 0) {
716 /*
717 * found a rotational position, now find the actual
718 * block. A panic if none is actually there.
719 */
720 pos = cylno % fs->fs_cpc;
721 bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
10adeb11 722 if (fs_postbl(fs, pos)[i] == -1) {
ffd90e52
KM
723 printf("pos = %d, i = %d, fs = %s\n",
724 pos, i, fs->fs_fsmnt);
aca50d72 725 panic("alloccgblk: cyl groups corrupted");
ffd90e52 726 }
10adeb11
KM
727 for (i = fs_postbl(fs, pos)[i];; ) {
728 if (isblock(fs, cg_blksfree(cgp), bno + i)) {
240a4664 729 bno = blkstofrags(fs, (bno + i));
aca50d72 730 goto gotit;
743f1ef7 731 }
10adeb11
KM
732 delta = fs_rotbl(fs)[i];
733 if (delta <= 0 ||
734 delta + i > fragstoblks(fs, fs->fs_fpg))
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
741norot:
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 750gotit:
10adeb11 751 clrblock(fs, cg_blksfree(cgp), (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 755 cylno = cbtocylno(fs, bno);
10adeb11
KM
756 cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
757 cg_blktot(cgp)[cylno]--;
e3fe2d69 758 fs->fs_fmod++;
743f1ef7 759 return (cgp->cg_cgx * fs->fs_fpg + bno);
e3fe2d69 760}
10adeb11 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 771ino_t
f7287e4b
KM
772ialloccg(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;
10adeb11 793 if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
0f538882 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;
10adeb11 801 if (isclr(cg_inosused(cgp), ipref))
e3fe2d69 802 goto gotit;
4e0c7b8a
KM
803 }
804 start = cgp->cg_irotor / NBBY;
805 len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
10adeb11 806 loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
4e0c7b8a 807 if (loc == 0) {
e5889092
KM
808 len = start + 1;
809 start = 0;
10adeb11 810 loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
e5889092
KM
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;
10adeb11 819 map = cg_inosused(cgp)[i];
4e0c7b8a
KM
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 830gotit:
10adeb11 831 setbit(cg_inosused(cgp), 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 852blkfree(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 880 cgp = bp->b_un.b_cg;
10adeb11 881 if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
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) {
10adeb11 888 if (isblock(fs, cg_blksfree(cgp), 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 }
10adeb11 893 setblock(fs, cg_blksfree(cgp), 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 897 i = cbtocylno(fs, bno);
10adeb11
KM
898 cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
899 cg_blktot(cgp)[i]++;
07670f7d 900 } else {
a8580723 901 bbase = bno - fragnum(fs, bno);
f3c028b7
KM
902 /*
903 * decrement the counts associated with the old frags
904 */
10adeb11 905 blk = blkmap(fs, cg_blksfree(cgp), 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++) {
10adeb11 912 if (isset(cg_blksfree(cgp), bno + i)) {
ffd90e52
KM
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 }
10adeb11 917 setbit(cg_blksfree(cgp), 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 */
10adeb11 925 blk = blkmap(fs, cg_blksfree(cgp), 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 */
10adeb11 930 if (isblock(fs, cg_blksfree(cgp), 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 937 i = cbtocylno(fs, bbase);
10adeb11
KM
938 cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
939 cg_blktot(cgp)[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
951ifree(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 974 cgp = bp->b_un.b_cg;
10adeb11 975 if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
d995d89d 976 brelse(bp);
e3fe2d69 977 return;
d995d89d 978 }
ad9250ee 979 cgp->cg_time = time.tv_sec;
e3fe2d69 980 ino %= fs->fs_ipg;
10adeb11 981 if (isclr(cg_inosused(cgp), ino)) {
ffd90e52
KM
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 }
10adeb11 986 clrbit(cg_inosused(cgp), 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 */
1007daddr_t
1008mapsearch(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;
10adeb11 1027 loc = scanc((unsigned)len, (caddr_t)&cg_blksfree(cgp)[start],
88a7a62a
SL
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;
10adeb11 1033 loc = scanc((unsigned)len, (caddr_t)&cg_blksfree(cgp)[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 1049 for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
10adeb11 1050 blk = blkmap(fs, cg_blksfree(cgp), 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 */
1072fserr(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}