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