xref: /freebsd/sys/fs/ext2fs/ext2_alloc.c (revision ab00ac327a66a53edaac95b536b209db3ae2cd9f)
1 /*-
2  *  modified for Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*-
8  * Copyright (c) 1982, 1986, 1989, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)ffs_alloc.c	8.8 (Berkeley) 2/21/94
36  * $FreeBSD$
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/vnode.h>
43 #include <sys/stat.h>
44 #include <sys/mount.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/buf.h>
48 
49 #include <fs/ext2fs/fs.h>
50 #include <fs/ext2fs/inode.h>
51 #include <fs/ext2fs/ext2_mount.h>
52 #include <fs/ext2fs/ext2fs.h>
53 #include <fs/ext2fs/ext2_extern.h>
54 
55 static daddr_t	ext2_alloccg(struct inode *, int, daddr_t, int);
56 static daddr_t	ext2_clusteralloc(struct inode *, int, daddr_t, int);
57 static u_long	ext2_dirpref(struct inode *);
58 static void	ext2_fserr(struct m_ext2fs *, uid_t, char *);
59 static u_long	ext2_hashalloc(struct inode *, int, long, int,
60 				daddr_t (*)(struct inode *, int, daddr_t,
61 						int));
62 static daddr_t	ext2_nodealloccg(struct inode *, int, daddr_t, int);
63 static daddr_t  ext2_mapsearch(struct m_ext2fs *, char *, daddr_t);
64 
65 /*
66  * Allocate a block in the filesystem.
67  *
68  * A preference may be optionally specified. If a preference is given
69  * the following hierarchy is used to allocate a block:
70  *   1) allocate the requested block.
71  *   2) allocate a rotationally optimal block in the same cylinder.
72  *   3) allocate a block in the same cylinder group.
73  *   4) quadradically rehash into other cylinder groups, until an
74  *        available block is located.
75  * If no block preference is given the following hierarchy is used
76  * to allocate a block:
77  *   1) allocate a block in the cylinder group that contains the
78  *        inode for the file.
79  *   2) quadradically rehash into other cylinder groups, until an
80  *        available block is located.
81  */
82 int
83 ext2_alloc(struct inode *ip, daddr_t lbn, e4fs_daddr_t bpref, int size,
84     struct ucred *cred, e4fs_daddr_t *bnp)
85 {
86 	struct m_ext2fs *fs;
87 	struct ext2mount *ump;
88 	int32_t bno;
89 	int cg;
90 
91 	*bnp = 0;
92 	fs = ip->i_e2fs;
93 	ump = ip->i_ump;
94 	mtx_assert(EXT2_MTX(ump), MA_OWNED);
95 #ifdef INVARIANTS
96 	if ((u_int)size > fs->e2fs_bsize || blkoff(fs, size) != 0) {
97 		vn_printf(ip->i_devvp, "bsize = %lu, size = %d, fs = %s\n",
98 		    (long unsigned int)fs->e2fs_bsize, size, fs->e2fs_fsmnt);
99 		panic("ext2_alloc: bad size");
100 	}
101 	if (cred == NOCRED)
102 		panic("ext2_alloc: missing credential");
103 #endif		/* INVARIANTS */
104 	if (size == fs->e2fs_bsize && fs->e2fs->e2fs_fbcount == 0)
105 		goto nospace;
106 	if (cred->cr_uid != 0 &&
107 	    fs->e2fs->e2fs_fbcount < fs->e2fs->e2fs_rbcount)
108 		goto nospace;
109 	if (bpref >= fs->e2fs->e2fs_bcount)
110 		bpref = 0;
111 	if (bpref == 0)
112 		cg = ino_to_cg(fs, ip->i_number);
113 	else
114 		cg = dtog(fs, bpref);
115 	bno = (daddr_t)ext2_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
116 	    ext2_alloccg);
117 	if (bno > 0) {
118 		/* set next_alloc fields as done in block_getblk */
119 		ip->i_next_alloc_block = lbn;
120 		ip->i_next_alloc_goal = bno;
121 
122 		ip->i_blocks += btodb(fs->e2fs_bsize);
123 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
124 		*bnp = bno;
125 		return (0);
126 	}
127 nospace:
128 	EXT2_UNLOCK(ump);
129 	ext2_fserr(fs, cred->cr_uid, "filesystem full");
130 	uprintf("\n%s: write failed, filesystem is full\n", fs->e2fs_fsmnt);
131 	return (ENOSPC);
132 }
133 
134 /*
135  * Allocate EA's block for inode.
136  */
137 daddr_t
138 ext2_allocfacl(struct inode *ip)
139 {
140 	struct m_ext2fs *fs;
141 	daddr_t facl;
142 
143 	fs = ip->i_e2fs;
144 
145 	EXT2_LOCK(ip->i_ump);
146 	facl = ext2_alloccg(ip, ino_to_cg(fs, ip->i_number), 0, fs->e2fs_bsize);
147 	if (0 == facl)
148 		EXT2_UNLOCK(ip->i_ump);
149 
150 	return (facl);
151 }
152 
153 /*
154  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
155  *
156  * The vnode and an array of buffer pointers for a range of sequential
157  * logical blocks to be made contiguous is given. The allocator attempts
158  * to find a range of sequential blocks starting as close as possible to
159  * an fs_rotdelay offset from the end of the allocation for the logical
160  * block immediately preceding the current range. If successful, the
161  * physical block numbers in the buffer pointers and in the inode are
162  * changed to reflect the new allocation. If unsuccessful, the allocation
163  * is left unchanged. The success in doing the reallocation is returned.
164  * Note that the error return is not reflected back to the user. Rather
165  * the previous block allocation will be used.
166  */
167 
168 static SYSCTL_NODE(_vfs, OID_AUTO, ext2fs, CTLFLAG_RW, 0, "EXT2FS filesystem");
169 
170 static int doasyncfree = 1;
171 
172 SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0,
173     "Use asychronous writes to update block pointers when freeing blocks");
174 
175 static int doreallocblks = 1;
176 
177 SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
178 
179 int
180 ext2_reallocblks(struct vop_reallocblks_args *ap)
181 {
182 	struct m_ext2fs *fs;
183 	struct inode *ip;
184 	struct vnode *vp;
185 	struct buf *sbp, *ebp;
186 	uint32_t *bap, *sbap, *ebap;
187 	struct ext2mount *ump;
188 	struct cluster_save *buflist;
189 	struct indir start_ap[EXT2_NIADDR + 1], end_ap[EXT2_NIADDR + 1], *idp;
190 	e2fs_lbn_t start_lbn, end_lbn;
191 	int soff;
192 	e2fs_daddr_t newblk, blkno;
193 	int i, len, start_lvl, end_lvl, pref, ssize;
194 
195 	if (doreallocblks == 0)
196 		return (ENOSPC);
197 
198 	vp = ap->a_vp;
199 	ip = VTOI(vp);
200 	fs = ip->i_e2fs;
201 	ump = ip->i_ump;
202 
203 	if (fs->e2fs_contigsumsize <= 0)
204 		return (ENOSPC);
205 
206 	buflist = ap->a_buflist;
207 	len = buflist->bs_nchildren;
208 	start_lbn = buflist->bs_children[0]->b_lblkno;
209 	end_lbn = start_lbn + len - 1;
210 #ifdef INVARIANTS
211 	for (i = 1; i < len; i++)
212 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
213 			panic("ext2_reallocblks: non-cluster");
214 #endif
215 	/*
216 	 * If the cluster crosses the boundary for the first indirect
217 	 * block, leave space for the indirect block. Indirect blocks
218 	 * are initially laid out in a position after the last direct
219 	 * block. Block reallocation would usually destroy locality by
220 	 * moving the indirect block out of the way to make room for
221 	 * data blocks if we didn't compensate here. We should also do
222 	 * this for other indirect block boundaries, but it is only
223 	 * important for the first one.
224 	 */
225 	if (start_lbn < EXT2_NDADDR && end_lbn >= EXT2_NDADDR)
226 		return (ENOSPC);
227 	/*
228 	 * If the latest allocation is in a new cylinder group, assume that
229 	 * the filesystem has decided to move and do not force it back to
230 	 * the previous cylinder group.
231 	 */
232 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
233 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
234 		return (ENOSPC);
235 	if (ext2_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
236 	    ext2_getlbns(vp, end_lbn, end_ap, &end_lvl))
237 		return (ENOSPC);
238 	/*
239 	 * Get the starting offset and block map for the first block.
240 	 */
241 	if (start_lvl == 0) {
242 		sbap = &ip->i_db[0];
243 		soff = start_lbn;
244 	} else {
245 		idp = &start_ap[start_lvl - 1];
246 		if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &sbp)) {
247 			brelse(sbp);
248 			return (ENOSPC);
249 		}
250 		sbap = (u_int *)sbp->b_data;
251 		soff = idp->in_off;
252 	}
253 	/*
254 	 * If the block range spans two block maps, get the second map.
255 	 */
256 	ebap = NULL;
257 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
258 		ssize = len;
259 	} else {
260 #ifdef INVARIANTS
261 		if (start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
262 			panic("ext2_reallocblks: start == end");
263 #endif
264 		ssize = len - (idp->in_off + 1);
265 		if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &ebp))
266 			goto fail;
267 		ebap = (u_int *)ebp->b_data;
268 	}
269 	/*
270 	 * Find the preferred location for the cluster.
271 	 */
272 	EXT2_LOCK(ump);
273 	pref = ext2_blkpref(ip, start_lbn, soff, sbap, 0);
274 	/*
275 	 * Search the block map looking for an allocation of the desired size.
276 	 */
277 	if ((newblk = (e2fs_daddr_t)ext2_hashalloc(ip, dtog(fs, pref), pref,
278 	    len, ext2_clusteralloc)) == 0) {
279 		EXT2_UNLOCK(ump);
280 		goto fail;
281 	}
282 	/*
283 	 * We have found a new contiguous block.
284 	 *
285 	 * First we have to replace the old block pointers with the new
286 	 * block pointers in the inode and indirect blocks associated
287 	 * with the file.
288 	 */
289 #ifdef DEBUG
290 	printf("realloc: ino %ju, lbns %jd-%jd\n\told:",
291 	    (uintmax_t)ip->i_number, (intmax_t)start_lbn, (intmax_t)end_lbn);
292 #endif	/* DEBUG */
293 	blkno = newblk;
294 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
295 		if (i == ssize) {
296 			bap = ebap;
297 			soff = -i;
298 		}
299 #ifdef INVARIANTS
300 		if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap))
301 			panic("ext2_reallocblks: alloc mismatch");
302 #endif
303 #ifdef DEBUG
304 		printf(" %d,", *bap);
305 #endif	/* DEBUG */
306 		*bap++ = blkno;
307 	}
308 	/*
309 	 * Next we must write out the modified inode and indirect blocks.
310 	 * For strict correctness, the writes should be synchronous since
311 	 * the old block values may have been written to disk. In practise
312 	 * they are almost never written, but if we are concerned about
313 	 * strict correctness, the `doasyncfree' flag should be set to zero.
314 	 *
315 	 * The test on `doasyncfree' should be changed to test a flag
316 	 * that shows whether the associated buffers and inodes have
317 	 * been written. The flag should be set when the cluster is
318 	 * started and cleared whenever the buffer or inode is flushed.
319 	 * We can then check below to see if it is set, and do the
320 	 * synchronous write only when it has been cleared.
321 	 */
322 	if (sbap != &ip->i_db[0]) {
323 		if (doasyncfree)
324 			bdwrite(sbp);
325 		else
326 			bwrite(sbp);
327 	} else {
328 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
329 		if (!doasyncfree)
330 			ext2_update(vp, 1);
331 	}
332 	if (ssize < len) {
333 		if (doasyncfree)
334 			bdwrite(ebp);
335 		else
336 			bwrite(ebp);
337 	}
338 	/*
339 	 * Last, free the old blocks and assign the new blocks to the buffers.
340 	 */
341 #ifdef DEBUG
342 	printf("\n\tnew:");
343 #endif	/* DEBUG */
344 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
345 		ext2_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
346 		    fs->e2fs_bsize);
347 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
348 #ifdef DEBUG
349 		printf(" %d,", blkno);
350 #endif	/* DEBUG */
351 	}
352 #ifdef DEBUG
353 	printf("\n");
354 #endif	/* DEBUG */
355 	return (0);
356 
357 fail:
358 	if (ssize < len)
359 		brelse(ebp);
360 	if (sbap != &ip->i_db[0])
361 		brelse(sbp);
362 	return (ENOSPC);
363 }
364 
365 /*
366  * Allocate an inode in the filesystem.
367  *
368  */
369 int
370 ext2_valloc(struct vnode *pvp, int mode, struct ucred *cred, struct vnode **vpp)
371 {
372 	struct timespec ts;
373 	struct inode *pip;
374 	struct m_ext2fs *fs;
375 	struct inode *ip;
376 	struct ext2mount *ump;
377 	ino_t ino, ipref;
378 	int i, error, cg;
379 
380 	*vpp = NULL;
381 	pip = VTOI(pvp);
382 	fs = pip->i_e2fs;
383 	ump = pip->i_ump;
384 
385 	EXT2_LOCK(ump);
386 	if (fs->e2fs->e2fs_ficount == 0)
387 		goto noinodes;
388 	/*
389 	 * If it is a directory then obtain a cylinder group based on
390 	 * ext2_dirpref else obtain it using ino_to_cg. The preferred inode is
391 	 * always the next inode.
392 	 */
393 	if ((mode & IFMT) == IFDIR) {
394 		cg = ext2_dirpref(pip);
395 		if (fs->e2fs_contigdirs[cg] < 255)
396 			fs->e2fs_contigdirs[cg]++;
397 	} else {
398 		cg = ino_to_cg(fs, pip->i_number);
399 		if (fs->e2fs_contigdirs[cg] > 0)
400 			fs->e2fs_contigdirs[cg]--;
401 	}
402 	ipref = cg * fs->e2fs->e2fs_ipg + 1;
403 	ino = (ino_t)ext2_hashalloc(pip, cg, (long)ipref, mode, ext2_nodealloccg);
404 
405 	if (ino == 0)
406 		goto noinodes;
407 	error = VFS_VGET(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
408 	if (error) {
409 		ext2_vfree(pvp, ino, mode);
410 		return (error);
411 	}
412 	ip = VTOI(*vpp);
413 
414 	/*
415 	 * The question is whether using VGET was such good idea at all:
416 	 * Linux doesn't read the old inode in when it is allocating a
417 	 * new one. I will set at least i_size and i_blocks to zero.
418 	 */
419 	ip->i_flag = 0;
420 	ip->i_size = 0;
421 	ip->i_blocks = 0;
422 	ip->i_mode = 0;
423 	ip->i_flags = 0;
424 	/* now we want to make sure that the block pointers are zeroed out */
425 	for (i = 0; i < EXT2_NDADDR; i++)
426 		ip->i_db[i] = 0;
427 	for (i = 0; i < EXT2_NIADDR; i++)
428 		ip->i_ib[i] = 0;
429 
430 	/*
431 	 * Set up a new generation number for this inode.
432 	 * Avoid zero values.
433 	 */
434 	do {
435 		ip->i_gen = arc4random();
436 	} while (ip->i_gen == 0);
437 
438 	vfs_timestamp(&ts);
439 	ip->i_birthtime = ts.tv_sec;
440 	ip->i_birthnsec = ts.tv_nsec;
441 
442 /*
443 printf("ext2_valloc: allocated inode %d\n", ino);
444 */
445 	return (0);
446 noinodes:
447 	EXT2_UNLOCK(ump);
448 	ext2_fserr(fs, cred->cr_uid, "out of inodes");
449 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
450 	return (ENOSPC);
451 }
452 
453 /*
454  * Find a cylinder to place a directory.
455  *
456  * The policy implemented by this algorithm is to allocate a
457  * directory inode in the same cylinder group as its parent
458  * directory, but also to reserve space for its files inodes
459  * and data. Restrict the number of directories which may be
460  * allocated one after another in the same cylinder group
461  * without intervening allocation of files.
462  *
463  * If we allocate a first level directory then force allocation
464  * in another cylinder group.
465  *
466  */
467 static u_long
468 ext2_dirpref(struct inode *pip)
469 {
470 	struct m_ext2fs *fs;
471 	int cg, prefcg, cgsize;
472 	u_int avgifree, avgbfree, avgndir, curdirsize;
473 	u_int minifree, minbfree, maxndir;
474 	u_int mincg, minndir;
475 	u_int dirsize, maxcontigdirs;
476 
477 	mtx_assert(EXT2_MTX(pip->i_ump), MA_OWNED);
478 	fs = pip->i_e2fs;
479 
480 	avgifree = fs->e2fs->e2fs_ficount / fs->e2fs_gcount;
481 	avgbfree = fs->e2fs->e2fs_fbcount / fs->e2fs_gcount;
482 	avgndir = fs->e2fs_total_dir / fs->e2fs_gcount;
483 
484 	/*
485 	 * Force allocation in another cg if creating a first level dir.
486 	 */
487 	ASSERT_VOP_LOCKED(ITOV(pip), "ext2fs_dirpref");
488 	if (ITOV(pip)->v_vflag & VV_ROOT) {
489 		prefcg = arc4random() % fs->e2fs_gcount;
490 		mincg = prefcg;
491 		minndir = fs->e2fs_ipg;
492 		for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
493 			if (fs->e2fs_gd[cg].ext2bgd_ndirs < minndir &&
494 			    fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree &&
495 			    fs->e2fs_gd[cg].ext2bgd_nbfree >= avgbfree) {
496 				mincg = cg;
497 				minndir = fs->e2fs_gd[cg].ext2bgd_ndirs;
498 			}
499 		for (cg = 0; cg < prefcg; cg++)
500 			if (fs->e2fs_gd[cg].ext2bgd_ndirs < minndir &&
501 			    fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree &&
502 			    fs->e2fs_gd[cg].ext2bgd_nbfree >= avgbfree) {
503 				mincg = cg;
504 				minndir = fs->e2fs_gd[cg].ext2bgd_ndirs;
505 			}
506 		return (mincg);
507 	}
508 	/*
509 	 * Count various limits which used for
510 	 * optimal allocation of a directory inode.
511 	 */
512 	maxndir = min(avgndir + fs->e2fs_ipg / 16, fs->e2fs_ipg);
513 	minifree = avgifree - avgifree / 4;
514 	if (minifree < 1)
515 		minifree = 1;
516 	minbfree = avgbfree - avgbfree / 4;
517 	if (minbfree < 1)
518 		minbfree = 1;
519 	cgsize = fs->e2fs_fsize * fs->e2fs_fpg;
520 	dirsize = AVGDIRSIZE;
521 	curdirsize = avgndir ? (cgsize - avgbfree * fs->e2fs_bsize) / avgndir : 0;
522 	if (dirsize < curdirsize)
523 		dirsize = curdirsize;
524 	maxcontigdirs = min((avgbfree * fs->e2fs_bsize) / dirsize, 255);
525 	maxcontigdirs = min(maxcontigdirs, fs->e2fs_ipg / AFPDIR);
526 	if (maxcontigdirs == 0)
527 		maxcontigdirs = 1;
528 
529 	/*
530 	 * Limit number of dirs in one cg and reserve space for
531 	 * regular files, but only if we have no deficit in
532 	 * inodes or space.
533 	 */
534 	prefcg = ino_to_cg(fs, pip->i_number);
535 	for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
536 		if (fs->e2fs_gd[cg].ext2bgd_ndirs < maxndir &&
537 		    fs->e2fs_gd[cg].ext2bgd_nifree >= minifree &&
538 		    fs->e2fs_gd[cg].ext2bgd_nbfree >= minbfree) {
539 			if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
540 				return (cg);
541 		}
542 	for (cg = 0; cg < prefcg; cg++)
543 		if (fs->e2fs_gd[cg].ext2bgd_ndirs < maxndir &&
544 		    fs->e2fs_gd[cg].ext2bgd_nifree >= minifree &&
545 		    fs->e2fs_gd[cg].ext2bgd_nbfree >= minbfree) {
546 			if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
547 				return (cg);
548 		}
549 	/*
550 	 * This is a backstop when we have deficit in space.
551 	 */
552 	for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
553 		if (fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree)
554 			return (cg);
555 	for (cg = 0; cg < prefcg; cg++)
556 		if (fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree)
557 			break;
558 	return (cg);
559 }
560 
561 /*
562  * Select the desired position for the next block in a file.
563  *
564  * we try to mimic what Remy does in inode_getblk/block_getblk
565  *
566  * we note: blocknr == 0 means that we're about to allocate either
567  * a direct block or a pointer block at the first level of indirection
568  * (In other words, stuff that will go in i_db[] or i_ib[])
569  *
570  * blocknr != 0 means that we're allocating a block that is none
571  * of the above. Then, blocknr tells us the number of the block
572  * that will hold the pointer
573  */
574 e4fs_daddr_t
575 ext2_blkpref(struct inode *ip, e2fs_lbn_t lbn, int indx, e2fs_daddr_t *bap,
576     e2fs_daddr_t blocknr)
577 {
578 	int tmp;
579 
580 	mtx_assert(EXT2_MTX(ip->i_ump), MA_OWNED);
581 
582 	/*
583 	 * If the next block is actually what we thought it is, then set the
584 	 * goal to what we thought it should be.
585 	 */
586 	if (ip->i_next_alloc_block == lbn && ip->i_next_alloc_goal != 0)
587 		return ip->i_next_alloc_goal;
588 
589 	/*
590 	 * Now check whether we were provided with an array that basically
591 	 * tells us previous blocks to which we want to stay close.
592 	 */
593 	if (bap)
594 		for (tmp = indx - 1; tmp >= 0; tmp--)
595 			if (bap[tmp])
596 				return bap[tmp];
597 
598 	/*
599 	 * Else lets fall back to the blocknr or, if there is none, follow
600 	 * the rule that a block should be allocated near its inode.
601 	 */
602 	return blocknr ? blocknr :
603 	    (e2fs_daddr_t)(ip->i_block_group *
604 	    EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
605 	    ip->i_e2fs->e2fs->e2fs_first_dblock;
606 }
607 
608 /*
609  * Implement the cylinder overflow algorithm.
610  *
611  * The policy implemented by this algorithm is:
612  *   1) allocate the block in its requested cylinder group.
613  *   2) quadradically rehash on the cylinder group number.
614  *   3) brute force search for a free block.
615  */
616 static u_long
617 ext2_hashalloc(struct inode *ip, int cg, long pref, int size,
618     daddr_t (*allocator) (struct inode *, int, daddr_t, int))
619 {
620 	struct m_ext2fs *fs;
621 	ino_t result;
622 	int i, icg = cg;
623 
624 	mtx_assert(EXT2_MTX(ip->i_ump), MA_OWNED);
625 	fs = ip->i_e2fs;
626 	/*
627 	 * 1: preferred cylinder group
628 	 */
629 	result = (*allocator)(ip, cg, pref, size);
630 	if (result)
631 		return (result);
632 	/*
633 	 * 2: quadratic rehash
634 	 */
635 	for (i = 1; i < fs->e2fs_gcount; i *= 2) {
636 		cg += i;
637 		if (cg >= fs->e2fs_gcount)
638 			cg -= fs->e2fs_gcount;
639 		result = (*allocator)(ip, cg, 0, size);
640 		if (result)
641 			return (result);
642 	}
643 	/*
644 	 * 3: brute force search
645 	 * Note that we start at i == 2, since 0 was checked initially,
646 	 * and 1 is always checked in the quadratic rehash.
647 	 */
648 	cg = (icg + 2) % fs->e2fs_gcount;
649 	for (i = 2; i < fs->e2fs_gcount; i++) {
650 		result = (*allocator)(ip, cg, 0, size);
651 		if (result)
652 			return (result);
653 		cg++;
654 		if (cg == fs->e2fs_gcount)
655 			cg = 0;
656 	}
657 	return (0);
658 }
659 
660 /*
661  * Determine whether a block can be allocated.
662  *
663  * Check to see if a block of the appropriate size is available,
664  * and if it is, allocate it.
665  */
666 static daddr_t
667 ext2_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
668 {
669 	struct m_ext2fs *fs;
670 	struct buf *bp;
671 	struct ext2mount *ump;
672 	daddr_t bno, runstart, runlen;
673 	int bit, loc, end, error, start;
674 	char *bbp;
675 	/* XXX ondisk32 */
676 	fs = ip->i_e2fs;
677 	ump = ip->i_ump;
678 	if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0)
679 		return (0);
680 	EXT2_UNLOCK(ump);
681 	error = bread(ip->i_devvp, fsbtodb(fs,
682 	    fs->e2fs_gd[cg].ext2bgd_b_bitmap),
683 	    (int)fs->e2fs_bsize, NOCRED, &bp);
684 	if (error) {
685 		brelse(bp);
686 		EXT2_LOCK(ump);
687 		return (0);
688 	}
689 	if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0) {
690 		/*
691 		 * Another thread allocated the last block in this
692 		 * group while we were waiting for the buffer.
693 		 */
694 		brelse(bp);
695 		EXT2_LOCK(ump);
696 		return (0);
697 	}
698 	bbp = (char *)bp->b_data;
699 
700 	if (dtog(fs, bpref) != cg)
701 		bpref = 0;
702 	if (bpref != 0) {
703 		bpref = dtogd(fs, bpref);
704 		/*
705 		 * if the requested block is available, use it
706 		 */
707 		if (isclr(bbp, bpref)) {
708 			bno = bpref;
709 			goto gotit;
710 		}
711 	}
712 	/*
713 	 * no blocks in the requested cylinder, so take next
714 	 * available one in this cylinder group.
715 	 * first try to get 8 contigous blocks, then fall back to a single
716 	 * block.
717 	 */
718 	if (bpref)
719 		start = dtogd(fs, bpref) / NBBY;
720 	else
721 		start = 0;
722 	end = howmany(fs->e2fs->e2fs_fpg, NBBY) - start;
723 retry:
724 	runlen = 0;
725 	runstart = 0;
726 	for (loc = start; loc < end; loc++) {
727 		if (bbp[loc] == (char)0xff) {
728 			runlen = 0;
729 			continue;
730 		}
731 
732 		/* Start of a run, find the number of high clear bits. */
733 		if (runlen == 0) {
734 			bit = fls(bbp[loc]);
735 			runlen = NBBY - bit;
736 			runstart = loc * NBBY + bit;
737 		} else if (bbp[loc] == 0) {
738 			/* Continue a run. */
739 			runlen += NBBY;
740 		} else {
741 			/*
742 			 * Finish the current run.  If it isn't long
743 			 * enough, start a new one.
744 			 */
745 			bit = ffs(bbp[loc]) - 1;
746 			runlen += bit;
747 			if (runlen >= 8) {
748 				bno = runstart;
749 				goto gotit;
750 			}
751 
752 			/* Run was too short, start a new one. */
753 			bit = fls(bbp[loc]);
754 			runlen = NBBY - bit;
755 			runstart = loc * NBBY + bit;
756 		}
757 
758 		/* If the current run is long enough, use it. */
759 		if (runlen >= 8) {
760 			bno = runstart;
761 			goto gotit;
762 		}
763 	}
764 	if (start != 0) {
765 		end = start;
766 		start = 0;
767 		goto retry;
768 	}
769 	bno = ext2_mapsearch(fs, bbp, bpref);
770 	if (bno < 0) {
771 		brelse(bp);
772 		EXT2_LOCK(ump);
773 		return (0);
774 	}
775 gotit:
776 #ifdef INVARIANTS
777 	if (isset(bbp, bno)) {
778 		printf("ext2fs_alloccgblk: cg=%d bno=%jd fs=%s\n",
779 		    cg, (intmax_t)bno, fs->e2fs_fsmnt);
780 		panic("ext2fs_alloccg: dup alloc");
781 	}
782 #endif
783 	setbit(bbp, bno);
784 	EXT2_LOCK(ump);
785 	ext2_clusteracct(fs, bbp, cg, bno, -1);
786 	fs->e2fs->e2fs_fbcount--;
787 	fs->e2fs_gd[cg].ext2bgd_nbfree--;
788 	fs->e2fs_fmod = 1;
789 	EXT2_UNLOCK(ump);
790 	bdwrite(bp);
791 	return (cg * fs->e2fs->e2fs_fpg + fs->e2fs->e2fs_first_dblock + bno);
792 }
793 
794 /*
795  * Determine whether a cluster can be allocated.
796  */
797 static daddr_t
798 ext2_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
799 {
800 	struct m_ext2fs *fs;
801 	struct ext2mount *ump;
802 	struct buf *bp;
803 	char *bbp;
804 	int bit, error, got, i, loc, run;
805 	int32_t *lp;
806 	daddr_t bno;
807 
808 	fs = ip->i_e2fs;
809 	ump = ip->i_ump;
810 
811 	if (fs->e2fs_maxcluster[cg] < len)
812 		return (0);
813 
814 	EXT2_UNLOCK(ump);
815 	error = bread(ip->i_devvp,
816 	    fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
817 	    (int)fs->e2fs_bsize, NOCRED, &bp);
818 	if (error)
819 		goto fail_lock;
820 
821 	bbp = (char *)bp->b_data;
822 	EXT2_LOCK(ump);
823 	/*
824 	 * Check to see if a cluster of the needed size (or bigger) is
825 	 * available in this cylinder group.
826 	 */
827 	lp = &fs->e2fs_clustersum[cg].cs_sum[len];
828 	for (i = len; i <= fs->e2fs_contigsumsize; i++)
829 		if (*lp++ > 0)
830 			break;
831 	if (i > fs->e2fs_contigsumsize) {
832 		/*
833 		 * Update the cluster summary information to reflect
834 		 * the true maximum-sized cluster so that future cluster
835 		 * allocation requests can avoid reading the bitmap only
836 		 * to find no cluster.
837 		 */
838 		lp = &fs->e2fs_clustersum[cg].cs_sum[len - 1];
839 		for (i = len - 1; i > 0; i--)
840 			if (*lp-- > 0)
841 				break;
842 		fs->e2fs_maxcluster[cg] = i;
843 		goto fail;
844 	}
845 	EXT2_UNLOCK(ump);
846 
847 	/* Search the bitmap to find a big enough cluster like in FFS. */
848 	if (dtog(fs, bpref) != cg)
849 		bpref = 0;
850 	if (bpref != 0)
851 		bpref = dtogd(fs, bpref);
852 	loc = bpref / NBBY;
853 	bit = 1 << (bpref % NBBY);
854 	for (run = 0, got = bpref; got < fs->e2fs->e2fs_fpg; got++) {
855 		if ((bbp[loc] & bit) != 0)
856 			run = 0;
857 		else {
858 			run++;
859 			if (run == len)
860 				break;
861 		}
862 		if ((got & (NBBY - 1)) != (NBBY - 1))
863 			bit <<= 1;
864 		else {
865 			loc++;
866 			bit = 1;
867 		}
868 	}
869 
870 	if (got >= fs->e2fs->e2fs_fpg)
871 		goto fail_lock;
872 
873 	/* Allocate the cluster that we found. */
874 	for (i = 1; i < len; i++)
875 		if (!isclr(bbp, got - run + i))
876 			panic("ext2_clusteralloc: map mismatch");
877 
878 	bno = got - run + 1;
879 	if (bno >= fs->e2fs->e2fs_fpg)
880 		panic("ext2_clusteralloc: allocated out of group");
881 
882 	EXT2_LOCK(ump);
883 	for (i = 0; i < len; i += fs->e2fs_fpb) {
884 		setbit(bbp, bno + i);
885 		ext2_clusteracct(fs, bbp, cg, bno + i, -1);
886 		fs->e2fs->e2fs_fbcount--;
887 		fs->e2fs_gd[cg].ext2bgd_nbfree--;
888 	}
889 	fs->e2fs_fmod = 1;
890 	EXT2_UNLOCK(ump);
891 
892 	bdwrite(bp);
893 	return (cg * fs->e2fs->e2fs_fpg + fs->e2fs->e2fs_first_dblock + bno);
894 
895 fail_lock:
896 	EXT2_LOCK(ump);
897 fail:
898 	brelse(bp);
899 	return (0);
900 }
901 
902 /*
903  * Determine whether an inode can be allocated.
904  *
905  * Check to see if an inode is available, and if it is,
906  * allocate it using tode in the specified cylinder group.
907  */
908 static daddr_t
909 ext2_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
910 {
911 	struct m_ext2fs *fs;
912 	struct buf *bp;
913 	struct ext2mount *ump;
914 	int error, start, len;
915 	char *ibp, *loc;
916 
917 	ipref--;	/* to avoid a lot of (ipref -1) */
918 	if (ipref == -1)
919 		ipref = 0;
920 	fs = ip->i_e2fs;
921 	ump = ip->i_ump;
922 	if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
923 		return (0);
924 	EXT2_UNLOCK(ump);
925 	error = bread(ip->i_devvp, fsbtodb(fs,
926 	    fs->e2fs_gd[cg].ext2bgd_i_bitmap),
927 	    (int)fs->e2fs_bsize, NOCRED, &bp);
928 	if (error) {
929 		brelse(bp);
930 		EXT2_LOCK(ump);
931 		return (0);
932 	}
933 	if (fs->e2fs_gd[cg].ext2bgd_nifree == 0) {
934 		/*
935 		 * Another thread allocated the last i-node in this
936 		 * group while we were waiting for the buffer.
937 		 */
938 		brelse(bp);
939 		EXT2_LOCK(ump);
940 		return (0);
941 	}
942 	ibp = (char *)bp->b_data;
943 	if (ipref) {
944 		ipref %= fs->e2fs->e2fs_ipg;
945 		if (isclr(ibp, ipref))
946 			goto gotit;
947 	}
948 	start = ipref / NBBY;
949 	len = howmany(fs->e2fs->e2fs_ipg - ipref, NBBY);
950 	loc = memcchr(&ibp[start], 0xff, len);
951 	if (loc == NULL) {
952 		len = start + 1;
953 		start = 0;
954 		loc = memcchr(&ibp[start], 0xff, len);
955 		if (loc == NULL) {
956 			printf("cg = %d, ipref = %lld, fs = %s\n",
957 			    cg, (long long)ipref, fs->e2fs_fsmnt);
958 			panic("ext2fs_nodealloccg: map corrupted");
959 			/* NOTREACHED */
960 		}
961 	}
962 	ipref = (loc - ibp) * NBBY + ffs(~*loc) - 1;
963 gotit:
964 	setbit(ibp, ipref);
965 	EXT2_LOCK(ump);
966 	fs->e2fs_gd[cg].ext2bgd_nifree--;
967 	fs->e2fs->e2fs_ficount--;
968 	fs->e2fs_fmod = 1;
969 	if ((mode & IFMT) == IFDIR) {
970 		fs->e2fs_gd[cg].ext2bgd_ndirs++;
971 		fs->e2fs_total_dir++;
972 	}
973 	EXT2_UNLOCK(ump);
974 	bdwrite(bp);
975 	return (cg * fs->e2fs->e2fs_ipg + ipref + 1);
976 }
977 
978 /*
979  * Free a block or fragment.
980  *
981  */
982 void
983 ext2_blkfree(struct inode *ip, e4fs_daddr_t bno, long size)
984 {
985 	struct m_ext2fs *fs;
986 	struct buf *bp;
987 	struct ext2mount *ump;
988 	int cg, error;
989 	char *bbp;
990 
991 	fs = ip->i_e2fs;
992 	ump = ip->i_ump;
993 	cg = dtog(fs, bno);
994 	if ((u_int)bno >= fs->e2fs->e2fs_bcount) {
995 		printf("bad block %lld, ino %ju\n", (long long)bno,
996 		    (uintmax_t)ip->i_number);
997 		ext2_fserr(fs, ip->i_uid, "bad block");
998 		return;
999 	}
1000 	error = bread(ip->i_devvp,
1001 	    fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
1002 	    (int)fs->e2fs_bsize, NOCRED, &bp);
1003 	if (error) {
1004 		brelse(bp);
1005 		return;
1006 	}
1007 	bbp = (char *)bp->b_data;
1008 	bno = dtogd(fs, bno);
1009 	if (isclr(bbp, bno)) {
1010 		printf("block = %lld, fs = %s\n",
1011 		    (long long)bno, fs->e2fs_fsmnt);
1012 		panic("ext2_blkfree: freeing free block");
1013 	}
1014 	clrbit(bbp, bno);
1015 	EXT2_LOCK(ump);
1016 	ext2_clusteracct(fs, bbp, cg, bno, 1);
1017 	fs->e2fs->e2fs_fbcount++;
1018 	fs->e2fs_gd[cg].ext2bgd_nbfree++;
1019 	fs->e2fs_fmod = 1;
1020 	EXT2_UNLOCK(ump);
1021 	bdwrite(bp);
1022 }
1023 
1024 /*
1025  * Free an inode.
1026  *
1027  */
1028 int
1029 ext2_vfree(struct vnode *pvp, ino_t ino, int mode)
1030 {
1031 	struct m_ext2fs *fs;
1032 	struct inode *pip;
1033 	struct buf *bp;
1034 	struct ext2mount *ump;
1035 	int error, cg;
1036 	char *ibp;
1037 
1038 	pip = VTOI(pvp);
1039 	fs = pip->i_e2fs;
1040 	ump = pip->i_ump;
1041 	if ((u_int)ino > fs->e2fs_ipg * fs->e2fs_gcount)
1042 		panic("ext2_vfree: range: devvp = %p, ino = %ju, fs = %s",
1043 		    pip->i_devvp, (uintmax_t)ino, fs->e2fs_fsmnt);
1044 
1045 	cg = ino_to_cg(fs, ino);
1046 	error = bread(pip->i_devvp,
1047 	    fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_i_bitmap),
1048 	    (int)fs->e2fs_bsize, NOCRED, &bp);
1049 	if (error) {
1050 		brelse(bp);
1051 		return (0);
1052 	}
1053 	ibp = (char *)bp->b_data;
1054 	ino = (ino - 1) % fs->e2fs->e2fs_ipg;
1055 	if (isclr(ibp, ino)) {
1056 		printf("ino = %llu, fs = %s\n",
1057 		    (unsigned long long)ino, fs->e2fs_fsmnt);
1058 		if (fs->e2fs_ronly == 0)
1059 			panic("ext2_vfree: freeing free inode");
1060 	}
1061 	clrbit(ibp, ino);
1062 	EXT2_LOCK(ump);
1063 	fs->e2fs->e2fs_ficount++;
1064 	fs->e2fs_gd[cg].ext2bgd_nifree++;
1065 	if ((mode & IFMT) == IFDIR) {
1066 		fs->e2fs_gd[cg].ext2bgd_ndirs--;
1067 		fs->e2fs_total_dir--;
1068 	}
1069 	fs->e2fs_fmod = 1;
1070 	EXT2_UNLOCK(ump);
1071 	bdwrite(bp);
1072 	return (0);
1073 }
1074 
1075 /*
1076  * Find a block in the specified cylinder group.
1077  *
1078  * It is a panic if a request is made to find a block if none are
1079  * available.
1080  */
1081 static daddr_t
1082 ext2_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
1083 {
1084 	char *loc;
1085 	int start, len;
1086 
1087 	/*
1088 	 * find the fragment by searching through the free block
1089 	 * map for an appropriate bit pattern
1090 	 */
1091 	if (bpref)
1092 		start = dtogd(fs, bpref) / NBBY;
1093 	else
1094 		start = 0;
1095 	len = howmany(fs->e2fs->e2fs_fpg, NBBY) - start;
1096 	loc = memcchr(&bbp[start], 0xff, len);
1097 	if (loc == NULL) {
1098 		len = start + 1;
1099 		start = 0;
1100 		loc = memcchr(&bbp[start], 0xff, len);
1101 		if (loc == NULL) {
1102 			printf("start = %d, len = %d, fs = %s\n",
1103 			    start, len, fs->e2fs_fsmnt);
1104 			panic("ext2_mapsearch: map corrupted");
1105 			/* NOTREACHED */
1106 		}
1107 	}
1108 	return ((loc - bbp) * NBBY + ffs(~*loc) - 1);
1109 }
1110 
1111 /*
1112  * Fserr prints the name of a filesystem with an error diagnostic.
1113  *
1114  * The form of the error message is:
1115  *	fs: error message
1116  */
1117 static void
1118 ext2_fserr(struct m_ext2fs *fs, uid_t uid, char *cp)
1119 {
1120 
1121 	log(LOG_ERR, "uid %u on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
1122 }
1123 
1124 int
1125 cg_has_sb(int i)
1126 {
1127 	int a3, a5, a7;
1128 
1129 	if (i == 0 || i == 1)
1130 		return 1;
1131 	for (a3 = 3, a5 = 5, a7 = 7;
1132 	    a3 <= i || a5 <= i || a7 <= i;
1133 	    a3 *= 3, a5 *= 5, a7 *= 7)
1134 		if (i == a3 || i == a5 || i == a7)
1135 			return 1;
1136 	return 0;
1137 }
1138