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