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