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