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