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