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