xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_alloc.c (revision 503611ba29d4c920cb1878a9ece7ebd1e0ac2e16)
1 /*	$NetBSD: ext2fs_alloc.c,v 1.30 2006/06/07 22:34:18 kardel Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ffs_alloc.c	8.11 (Berkeley) 10/27/94
32  *  Modified for ext2fs by Manuel Bouyer.
33  */
34 
35 /*
36  * Copyright (c) 1997 Manuel Bouyer.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by Manuel Bouyer.
49  * 4. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *	@(#)ffs_alloc.c	8.11 (Berkeley) 10/27/94
64  *  Modified for ext2fs by Manuel Bouyer.
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_alloc.c,v 1.30 2006/06/07 22:34:18 kardel Exp $");
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/buf.h>
73 #include <sys/proc.h>
74 #include <sys/vnode.h>
75 #include <sys/mount.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78 #include <sys/kauth.h>
79 
80 #include <ufs/ufs/inode.h>
81 #include <ufs/ufs/ufs_extern.h>
82 #include <ufs/ufs/ufsmount.h>
83 
84 #include <ufs/ext2fs/ext2fs.h>
85 #include <ufs/ext2fs/ext2fs_extern.h>
86 
87 u_long ext2gennumber;
88 
89 static daddr_t	ext2fs_alloccg(struct inode *, int, daddr_t, int);
90 static u_long	ext2fs_dirpref(struct m_ext2fs *);
91 static void	ext2fs_fserr(struct m_ext2fs *, u_int, const char *);
92 static u_long	ext2fs_hashalloc(struct inode *, int, long, int,
93 				   daddr_t (*)(struct inode *, int, daddr_t,
94 						   int));
95 static daddr_t	ext2fs_nodealloccg(struct inode *, int, daddr_t, int);
96 static daddr_t	ext2fs_mapsearch(struct m_ext2fs *, char *, daddr_t);
97 
98 /*
99  * Allocate a block in the file system.
100  *
101  * A preference may be optionally specified. If a preference is given
102  * the following hierarchy is used to allocate a block:
103  *   1) allocate the requested block.
104  *   2) allocate a rotationally optimal block in the same cylinder.
105  *   3) allocate a block in the same cylinder group.
106  *   4) quadradically rehash into other cylinder groups, until an
107  *	  available block is located.
108  * If no block preference is given the following hierarchy is used
109  * to allocate a block:
110  *   1) allocate a block in the cylinder group that contains the
111  *	  inode for the file.
112  *   2) quadradically rehash into other cylinder groups, until an
113  *	  available block is located.
114  */
115 int
116 ext2fs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, kauth_cred_t cred,
117 		daddr_t *bnp)
118 {
119 	struct m_ext2fs *fs;
120 	daddr_t bno;
121 	int cg;
122 
123 	*bnp = 0;
124 	fs = ip->i_e2fs;
125 #ifdef DIAGNOSTIC
126 	if (cred == NOCRED)
127 		panic("ext2fs_alloc: missing credential");
128 #endif /* DIAGNOSTIC */
129 	if (fs->e2fs.e2fs_fbcount == 0)
130 		goto nospace;
131 	if (kauth_cred_geteuid(cred) != 0 && freespace(fs) <= 0)
132 		goto nospace;
133 	if (bpref >= fs->e2fs.e2fs_bcount)
134 		bpref = 0;
135 	if (bpref == 0)
136 		cg = ino_to_cg(fs, ip->i_number);
137 	else
138 		cg = dtog(fs, bpref);
139 	bno = (daddr_t)ext2fs_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
140 						 ext2fs_alloccg);
141 	if (bno > 0) {
142 		ip->i_e2fs_nblock += btodb(fs->e2fs_bsize);
143 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
144 		*bnp = bno;
145 		return (0);
146 	}
147 nospace:
148 	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
149 	uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
150 	return (ENOSPC);
151 }
152 
153 /*
154  * Allocate an inode in the file system.
155  *
156  * If allocating a directory, use ext2fs_dirpref to select the inode.
157  * If allocating in a directory, the following hierarchy is followed:
158  *   1) allocate the preferred inode.
159  *   2) allocate an inode in the same cylinder group.
160  *   3) quadradically rehash into other cylinder groups, until an
161  *	  available inode is located.
162  * If no inode preference is given the following hierarchy is used
163  * to allocate an inode:
164  *   1) allocate an inode in cylinder group 0.
165  *   2) quadradically rehash into other cylinder groups, until an
166  *	  available inode is located.
167  */
168 int
169 ext2fs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
170     struct vnode **vpp)
171 {
172 	struct inode *pip;
173 	struct m_ext2fs *fs;
174 	struct inode *ip;
175 	ino_t ino, ipref;
176 	int cg, error;
177 
178 	*vpp = NULL;
179 	pip = VTOI(pvp);
180 	fs = pip->i_e2fs;
181 	if (fs->e2fs.e2fs_ficount == 0)
182 		goto noinodes;
183 
184 	if ((mode & IFMT) == IFDIR)
185 		cg = ext2fs_dirpref(fs);
186 	else
187 		cg = ino_to_cg(fs, pip->i_number);
188 	ipref = cg * fs->e2fs.e2fs_ipg + 1;
189 	ino = (ino_t)ext2fs_hashalloc(pip, cg, (long)ipref, mode, ext2fs_nodealloccg);
190 	if (ino == 0)
191 		goto noinodes;
192 	error = VFS_VGET(pvp->v_mount, ino, vpp);
193 	if (error) {
194 		ext2fs_vfree(pvp, ino, mode);
195 		return (error);
196 	}
197 	ip = VTOI(*vpp);
198 	if (ip->i_e2fs_mode && ip->i_e2fs_nlink != 0) {
199 		printf("mode = 0%o, nlinks %d, inum = %llu, fs = %s\n",
200 		    ip->i_e2fs_mode, ip->i_e2fs_nlink,
201 		    (unsigned long long)ip->i_number, fs->e2fs_fsmnt);
202 		panic("ext2fs_valloc: dup alloc");
203 	}
204 
205 	memset(ip->i_din.e2fs_din, 0, sizeof(struct ext2fs_dinode));
206 
207 	/*
208 	 * Set up a new generation number for this inode.
209 	 */
210 	if (++ext2gennumber < time_second)
211 		ext2gennumber = time_second;
212 	ip->i_e2fs_gen = ext2gennumber;
213 	return (0);
214 noinodes:
215 	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes");
216 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
217 	return (ENOSPC);
218 }
219 
220 /*
221  * Find a cylinder to place a directory.
222  *
223  * The policy implemented by this algorithm is to select from
224  * among those cylinder groups with above the average number of
225  * free inodes, the one with the smallest number of directories.
226  */
227 static u_long
228 ext2fs_dirpref(struct m_ext2fs *fs)
229 {
230 	int cg, maxspace, mincg, avgifree;
231 
232 	avgifree = fs->e2fs.e2fs_ficount / fs->e2fs_ncg;
233 	maxspace = 0;
234 	mincg = -1;
235 	for (cg = 0; cg < fs->e2fs_ncg; cg++)
236 		if ( fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree) {
237 			if (mincg == -1 || fs->e2fs_gd[cg].ext2bgd_nbfree > maxspace) {
238 				mincg = cg;
239 				maxspace = fs->e2fs_gd[cg].ext2bgd_nbfree;
240 			}
241 		}
242 	return mincg;
243 }
244 
245 /*
246  * Select the desired position for the next block in a file.  The file is
247  * logically divided into sections. The first section is composed of the
248  * direct blocks. Each additional section contains fs_maxbpg blocks.
249  *
250  * If no blocks have been allocated in the first section, the policy is to
251  * request a block in the same cylinder group as the inode that describes
252  * the file. Otherwise, the policy is to try to allocate the blocks
253  * contigously. The two fields of the ext2 inode extension (see
254  * ufs/ufs/inode.h) help this.
255  */
256 daddr_t
257 ext2fs_blkpref(struct inode *ip, daddr_t lbn, int indx,
258 		int32_t *bap /* XXX ondisk32 */)
259 {
260 	struct m_ext2fs *fs;
261 	int cg, i;
262 
263 	fs = ip->i_e2fs;
264 	/*
265 	 * if we are doing contigous lbn allocation, try to alloc blocks
266 	 * contigously on disk
267 	 */
268 
269 	if ( ip->i_e2fs_last_blk && lbn == ip->i_e2fs_last_lblk + 1) {
270 		return ip->i_e2fs_last_blk + 1;
271 	}
272 
273 	/*
274 	 * bap, if provided, gives us a list of blocks to which we want to
275 	 * stay close
276 	 */
277 
278 	if (bap) {
279 		for (i = indx; i >= 0 ; i--) {
280 			if (bap[i]) {
281 				return fs2h32(bap[i]) + 1;
282 			}
283 		}
284 	}
285 
286 	/* fall back to the first block of the cylinder containing the inode */
287 
288 	cg = ino_to_cg(fs, ip->i_number);
289 	return fs->e2fs.e2fs_bpg * cg + fs->e2fs.e2fs_first_dblock + 1;
290 }
291 
292 /*
293  * Implement the cylinder overflow algorithm.
294  *
295  * The policy implemented by this algorithm is:
296  *   1) allocate the block in its requested cylinder group.
297  *   2) quadradically rehash on the cylinder group number.
298  *   3) brute force search for a free block.
299  */
300 static u_long
301 ext2fs_hashalloc(struct inode *ip, int cg, long pref, int size,
302 		daddr_t (*allocator)(struct inode *, int, daddr_t, int))
303 {
304 	struct m_ext2fs *fs;
305 	long result;
306 	int i, icg = cg;
307 
308 	fs = ip->i_e2fs;
309 	/*
310 	 * 1: preferred cylinder group
311 	 */
312 	result = (*allocator)(ip, cg, pref, size);
313 	if (result)
314 		return (result);
315 	/*
316 	 * 2: quadratic rehash
317 	 */
318 	for (i = 1; i < fs->e2fs_ncg; i *= 2) {
319 		cg += i;
320 		if (cg >= fs->e2fs_ncg)
321 			cg -= fs->e2fs_ncg;
322 		result = (*allocator)(ip, cg, 0, size);
323 		if (result)
324 			return (result);
325 	}
326 	/*
327 	 * 3: brute force search
328 	 * Note that we start at i == 2, since 0 was checked initially,
329 	 * and 1 is always checked in the quadratic rehash.
330 	 */
331 	cg = (icg + 2) % fs->e2fs_ncg;
332 	for (i = 2; i < fs->e2fs_ncg; i++) {
333 		result = (*allocator)(ip, cg, 0, size);
334 		if (result)
335 			return (result);
336 		cg++;
337 		if (cg == fs->e2fs_ncg)
338 			cg = 0;
339 	}
340 	return (0);
341 }
342 
343 /*
344  * Determine whether a block can be allocated.
345  *
346  * Check to see if a block of the appropriate size is available,
347  * and if it is, allocate it.
348  */
349 
350 static daddr_t
351 ext2fs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
352 {
353 	struct m_ext2fs *fs;
354 	char *bbp;
355 	struct buf *bp;
356 	/* XXX ondisk32 */
357 	int error, bno, start, end, loc;
358 
359 	fs = ip->i_e2fs;
360 	if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0)
361 		return (0);
362 	error = bread(ip->i_devvp, fsbtodb(fs,
363 		fs->e2fs_gd[cg].ext2bgd_b_bitmap),
364 		(int)fs->e2fs_bsize, NOCRED, &bp);
365 	if (error) {
366 		brelse(bp);
367 		return (0);
368 	}
369 	bbp = (char *)bp->b_data;
370 
371 	if (dtog(fs, bpref) != cg)
372 		bpref = 0;
373 	if (bpref != 0) {
374 		bpref = dtogd(fs, bpref);
375 		/*
376 		 * if the requested block is available, use it
377 		 */
378 		if (isclr(bbp, bpref)) {
379 			bno = bpref;
380 			goto gotit;
381 		}
382 	}
383 	/*
384 	 * no blocks in the requested cylinder, so take next
385 	 * available one in this cylinder group.
386 	 * first try to get 8 contigous blocks, then fall back to a single
387 	 * block.
388 	 */
389 	if (bpref)
390 		start = dtogd(fs, bpref) / NBBY;
391 	else
392 		start = 0;
393 	end = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
394 	for (loc = start; loc < end; loc++) {
395 		if (bbp[loc] == 0) {
396 			bno = loc * NBBY;
397 			goto gotit;
398 		}
399 	}
400 	for (loc = 0; loc < start; loc++) {
401 		if (bbp[loc] == 0) {
402 			bno = loc * NBBY;
403 			goto gotit;
404 		}
405 	}
406 
407 	bno = ext2fs_mapsearch(fs, bbp, bpref);
408 	if (bno < 0)
409 		return (0);
410 gotit:
411 #ifdef DIAGNOSTIC
412 	if (isset(bbp, (daddr_t)bno)) {
413 		printf("ext2fs_alloccgblk: cg=%d bno=%d fs=%s\n",
414 			cg, bno, fs->e2fs_fsmnt);
415 		panic("ext2fs_alloccg: dup alloc");
416 	}
417 #endif
418 	setbit(bbp, (daddr_t)bno);
419 	fs->e2fs.e2fs_fbcount--;
420 	fs->e2fs_gd[cg].ext2bgd_nbfree--;
421 	fs->e2fs_fmod = 1;
422 	bdwrite(bp);
423 	return (cg * fs->e2fs.e2fs_fpg + fs->e2fs.e2fs_first_dblock + bno);
424 }
425 
426 /*
427  * Determine whether an inode can be allocated.
428  *
429  * Check to see if an inode is available, and if it is,
430  * allocate it using the following policy:
431  *   1) allocate the requested inode.
432  *   2) allocate the next available inode after the requested
433  *	  inode in the specified cylinder group.
434  */
435 static daddr_t
436 ext2fs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
437 {
438 	struct m_ext2fs *fs;
439 	char *ibp;
440 	struct buf *bp;
441 	int error, start, len, loc, map, i;
442 
443 	ipref--; /* to avoid a lot of (ipref -1) */
444 	fs = ip->i_e2fs;
445 	if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
446 		return (0);
447 	error = bread(ip->i_devvp, fsbtodb(fs,
448 		fs->e2fs_gd[cg].ext2bgd_i_bitmap),
449 		(int)fs->e2fs_bsize, NOCRED, &bp);
450 	if (error) {
451 		brelse(bp);
452 		return (0);
453 	}
454 	ibp = (char *)bp->b_data;
455 	if (ipref) {
456 		ipref %= fs->e2fs.e2fs_ipg;
457 		if (isclr(ibp, ipref))
458 			goto gotit;
459 	}
460 	start = ipref / NBBY;
461 	len = howmany(fs->e2fs.e2fs_ipg - ipref, NBBY);
462 	loc = skpc(0xff, len, &ibp[start]);
463 	if (loc == 0) {
464 		len = start + 1;
465 		start = 0;
466 		loc = skpc(0xff, len, &ibp[0]);
467 		if (loc == 0) {
468 			printf("cg = %d, ipref = %lld, fs = %s\n",
469 				cg, (long long)ipref, fs->e2fs_fsmnt);
470 			panic("ext2fs_nodealloccg: map corrupted");
471 			/* NOTREACHED */
472 		}
473 	}
474 	i = start + len - loc;
475 	map = ibp[i];
476 	ipref = i * NBBY;
477 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
478 		if ((map & i) == 0) {
479 			goto gotit;
480 		}
481 	}
482 	printf("fs = %s\n", fs->e2fs_fsmnt);
483 	panic("ext2fs_nodealloccg: block not in map");
484 	/* NOTREACHED */
485 gotit:
486 	setbit(ibp, ipref);
487 	fs->e2fs.e2fs_ficount--;
488 	fs->e2fs_gd[cg].ext2bgd_nifree--;
489 	fs->e2fs_fmod = 1;
490 	if ((mode & IFMT) == IFDIR) {
491 		fs->e2fs_gd[cg].ext2bgd_ndirs++;
492 	}
493 	bdwrite(bp);
494 	return (cg * fs->e2fs.e2fs_ipg + ipref +1);
495 }
496 
497 /*
498  * Free a block.
499  *
500  * The specified block is placed back in the
501  * free map.
502  */
503 void
504 ext2fs_blkfree(struct inode *ip, daddr_t bno)
505 {
506 	struct m_ext2fs *fs;
507 	char *bbp;
508 	struct buf *bp;
509 	int error, cg;
510 
511 	fs = ip->i_e2fs;
512 	cg = dtog(fs, bno);
513 	if ((u_int)bno >= fs->e2fs.e2fs_bcount) {
514 		printf("bad block %lld, ino %llu\n", (long long)bno,
515 		    (unsigned long long)ip->i_number);
516 		ext2fs_fserr(fs, ip->i_e2fs_uid, "bad block");
517 		return;
518 	}
519 	error = bread(ip->i_devvp,
520 		fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
521 		(int)fs->e2fs_bsize, NOCRED, &bp);
522 	if (error) {
523 		brelse(bp);
524 		return;
525 	}
526 	bbp = (char *)bp->b_data;
527 	bno = dtogd(fs, bno);
528 	if (isclr(bbp, bno)) {
529 		printf("dev = 0x%x, block = %lld, fs = %s\n",
530 			ip->i_dev, (long long)bno, fs->e2fs_fsmnt);
531 		panic("blkfree: freeing free block");
532 	}
533 	clrbit(bbp, bno);
534 	fs->e2fs.e2fs_fbcount++;
535 	fs->e2fs_gd[cg].ext2bgd_nbfree++;
536 
537 	fs->e2fs_fmod = 1;
538 	bdwrite(bp);
539 }
540 
541 /*
542  * Free an inode.
543  *
544  * The specified inode is placed back in the free map.
545  */
546 int
547 ext2fs_vfree(struct vnode *pvp, ino_t ino, int mode)
548 {
549 	struct m_ext2fs *fs;
550 	char *ibp;
551 	struct inode *pip;
552 	struct buf *bp;
553 	int error, cg;
554 
555 	pip = VTOI(pvp);
556 	fs = pip->i_e2fs;
557 	if ((u_int)ino >= fs->e2fs.e2fs_icount || (u_int)ino < EXT2_FIRSTINO)
558 		panic("ifree: range: dev = 0x%x, ino = %llu, fs = %s",
559 			pip->i_dev, (unsigned long long)ino, fs->e2fs_fsmnt);
560 	cg = ino_to_cg(fs, ino);
561 	error = bread(pip->i_devvp,
562 		fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_i_bitmap),
563 		(int)fs->e2fs_bsize, NOCRED, &bp);
564 	if (error) {
565 		brelse(bp);
566 		return (0);
567 	}
568 	ibp = (char *)bp->b_data;
569 	ino = (ino - 1) % fs->e2fs.e2fs_ipg;
570 	if (isclr(ibp, ino)) {
571 		printf("dev = 0x%x, ino = %llu, fs = %s\n",
572 		    pip->i_dev, (unsigned long long)ino, fs->e2fs_fsmnt);
573 		if (fs->e2fs_ronly == 0)
574 			panic("ifree: freeing free inode");
575 	}
576 	clrbit(ibp, ino);
577 	fs->e2fs.e2fs_ficount++;
578 	fs->e2fs_gd[cg].ext2bgd_nifree++;
579 	if ((mode & IFMT) == IFDIR) {
580 		fs->e2fs_gd[cg].ext2bgd_ndirs--;
581 	}
582 	fs->e2fs_fmod = 1;
583 	bdwrite(bp);
584 	return (0);
585 }
586 
587 /*
588  * Find a block in the specified cylinder group.
589  *
590  * It is a panic if a request is made to find a block if none are
591  * available.
592  */
593 
594 static daddr_t
595 ext2fs_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
596 {
597 	daddr_t bno;
598 	int start, len, loc, i, map;
599 
600 	/*
601 	 * find the fragment by searching through the free block
602 	 * map for an appropriate bit pattern
603 	 */
604 	if (bpref)
605 		start = dtogd(fs, bpref) / NBBY;
606 	else
607 		start = 0;
608 	len = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
609 	loc = skpc(0xff, len, &bbp[start]);
610 	if (loc == 0) {
611 		len = start + 1;
612 		start = 0;
613 		loc = skpc(0xff, len, &bbp[start]);
614 		if (loc == 0) {
615 			printf("start = %d, len = %d, fs = %s\n",
616 				start, len, fs->e2fs_fsmnt);
617 			panic("ext2fs_alloccg: map corrupted");
618 			/* NOTREACHED */
619 		}
620 	}
621 	i = start + len - loc;
622 	map = bbp[i];
623 	bno = i * NBBY;
624 	for (i = 1; i < (1 << NBBY); i <<= 1, bno++) {
625 		if ((map & i) == 0)
626 			return (bno);
627 	}
628 	printf("fs = %s\n", fs->e2fs_fsmnt);
629 	panic("ext2fs_mapsearch: block not in map");
630 	/* NOTREACHED */
631 }
632 
633 /*
634  * Fserr prints the name of a file system with an error diagnostic.
635  *
636  * The form of the error message is:
637  *	fs: error message
638  */
639 static void
640 ext2fs_fserr(struct m_ext2fs *fs, u_int uid, const char *cp)
641 {
642 
643 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
644 }
645