xref: /freebsd/sys/fs/ext2fs/ext2_alloc.c (revision 675be9115aae86ad6b3d877155d4fd7822892105)
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/inode.h>
50 #include <fs/ext2fs/ext2_mount.h>
51 #include <fs/ext2fs/ext2fs.h>
52 #include <fs/ext2fs/fs.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 file system.
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(ip, lbn, bpref, size, cred, bnp)
84 	struct inode *ip;
85 	int32_t lbn, bpref;
86 	int size;
87 	struct ucred *cred;
88 	int32_t *bnp;
89 {
90 	struct m_ext2fs *fs;
91 	struct ext2mount *ump;
92 	int32_t bno;
93 	int cg;
94 	*bnp = 0;
95 	fs = ip->i_e2fs;
96 	ump = ip->i_ump;
97 	mtx_assert(EXT2_MTX(ump), MA_OWNED);
98 #ifdef DIAGNOSTIC
99 	if ((u_int)size > fs->e2fs_bsize || blkoff(fs, size) != 0) {
100 		vn_printf(ip->i_devvp, "bsize = %lu, size = %d, fs = %s\n",
101 		    (long unsigned int)fs->e2fs_bsize, size, fs->e2fs_fsmnt);
102 		panic("ext2_alloc: bad size");
103 	}
104 	if (cred == NOCRED)
105 		panic("ext2_alloc: missing credential");
106 #endif /* DIAGNOSTIC */
107 	if (size == fs->e2fs_bsize && fs->e2fs->e2fs_fbcount == 0)
108 		goto nospace;
109 	if (cred->cr_uid != 0 &&
110 		fs->e2fs->e2fs_fbcount < fs->e2fs->e2fs_rbcount)
111 		goto nospace;
112 	if (bpref >= fs->e2fs->e2fs_bcount)
113 		bpref = 0;
114 	if (bpref == 0)
115 		cg = ino_to_cg(fs, ip->i_number);
116 	else
117 		cg = dtog(fs, bpref);
118 	bno = (daddr_t)ext2_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
119 				      ext2_alloccg);
120 	if (bno > 0) {
121 		/* set next_alloc fields as done in block_getblk */
122 		ip->i_next_alloc_block = lbn;
123 		ip->i_next_alloc_goal = bno;
124 
125 		ip->i_blocks += btodb(fs->e2fs_bsize);
126 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
127 		*bnp = bno;
128 		return (0);
129         }
130 nospace:
131 	EXT2_UNLOCK(ump);
132 	ext2_fserr(fs, cred->cr_uid, "file system full");
133 	uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
134 	return (ENOSPC);
135 }
136 
137 /*
138  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
139  *
140  * The vnode and an array of buffer pointers for a range of sequential
141  * logical blocks to be made contiguous is given. The allocator attempts
142  * to find a range of sequential blocks starting as close as possible to
143  * an fs_rotdelay offset from the end of the allocation for the logical
144  * block immediately preceding the current range. If successful, the
145  * physical block numbers in the buffer pointers and in the inode are
146  * changed to reflect the new allocation. If unsuccessful, the allocation
147  * is left unchanged. The success in doing the reallocation is returned.
148  * Note that the error return is not reflected back to the user. Rather
149  * the previous block allocation will be used.
150  */
151 
152 static SYSCTL_NODE(_vfs, OID_AUTO, ext2fs, CTLFLAG_RW, 0, "EXT2FS filesystem");
153 
154 static int doasyncfree = 1;
155 SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0,
156     "Use asychronous writes to update block pointers when freeing blocks");
157 
158 static int doreallocblks = 1;
159 SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
160 
161 int
162 ext2_reallocblks(ap)
163 	struct vop_reallocblks_args /* {
164 		struct vnode *a_vp;
165 		struct cluster_save *a_buflist;
166 	} */ *ap;
167 {
168 	struct m_ext2fs *fs;
169 	struct inode *ip;
170 	struct vnode *vp;
171 	struct buf *sbp, *ebp;
172 	int32_t *bap, *sbap, *ebap = 0;
173 	struct ext2mount *ump;
174 	struct cluster_save *buflist;
175 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
176 	int32_t start_lbn, end_lbn, soff, newblk, blkno;
177 	int i, len, start_lvl, end_lvl, pref, ssize;
178 
179 	if (doreallocblks == 0)
180 		  return (ENOSPC);
181 
182 	vp = ap->a_vp;
183 	ip = VTOI(vp);
184 	fs = ip->i_e2fs;
185 	ump = ip->i_ump;
186 
187 	if (fs->e2fs_contigsumsize <= 0)
188 		return (ENOSPC);
189 
190 	buflist = ap->a_buflist;
191 	len = buflist->bs_nchildren;
192 	start_lbn = buflist->bs_children[0]->b_lblkno;
193 	end_lbn = start_lbn + len - 1;
194 #ifdef DIAGNOSTIC
195 	for (i = 1; i < len; i++)
196 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
197 			panic("ext2_reallocblks: non-cluster");
198 #endif
199 	/*
200 	 * If the latest allocation is in a new cylinder group, assume that
201 	 * the filesystem has decided to move and do not force it back to
202 	 * the previous cylinder group.
203 	 */
204 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
205 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
206 		return (ENOSPC);
207 	if (ext2_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
208 	    ext2_getlbns(vp, end_lbn, end_ap, &end_lvl))
209 		return (ENOSPC);
210 	/*
211 	 * Get the starting offset and block map for the first block.
212 	 */
213 	if (start_lvl == 0) {
214 		sbap = &ip->i_db[0];
215 		soff = start_lbn;
216 	} else {
217 		idp = &start_ap[start_lvl - 1];
218 		if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &sbp)) {
219 			brelse(sbp);
220 			return (ENOSPC);
221 		}
222 		sbap = (int32_t *)sbp->b_data;
223 		soff = idp->in_off;
224 	}
225 	/*
226 	 * If the block range spans two block maps, get the second map.
227 	 */
228 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
229 		ssize = len;
230 	} else {
231 #ifdef DIAGNOSTIC
232 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
233 			panic("ext2_reallocblk: start == end");
234 #endif
235 		ssize = len - (idp->in_off + 1);
236 		if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &ebp))
237 			goto fail;
238 		ebap = (int32_t *)ebp->b_data;
239 	}
240 	/*
241 	 * Find the preferred location for the cluster.
242 	 */
243 	EXT2_LOCK(ump);
244 	pref = ext2_blkpref(ip, start_lbn, soff, sbap, 0);
245 	/*
246 	 * Search the block map looking for an allocation of the desired size.
247 	 */
248 	if ((newblk = (int32_t)ext2_hashalloc(ip, dtog(fs, pref), pref,
249 	    len, ext2_clusteralloc)) == 0){
250 		EXT2_UNLOCK(ump);
251 		goto fail;
252 	}
253 	/*
254 	 * We have found a new contiguous block.
255 	 *
256 	 * First we have to replace the old block pointers with the new
257 	 * block pointers in the inode and indirect blocks associated
258 	 * with the file.
259 	 */
260 #ifdef DEBUG
261 	printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
262 	    (intmax_t)start_lbn, (intmax_t)end_lbn);
263 #endif /* DEBUG */
264 	blkno = newblk;
265 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
266 		if (i == ssize) {
267 			bap = ebap;
268 			soff = -i;
269 		}
270 #ifdef DIAGNOSTIC
271 		if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap))
272 			panic("ext2_reallocblks: alloc mismatch");
273 #endif
274 #ifdef DEBUG
275 	printf(" %d,", *bap);
276 #endif /* DEBUG */
277 		*bap++ = blkno;
278 	}
279 	/*
280 	 * Next we must write out the modified inode and indirect blocks.
281 	 * For strict correctness, the writes should be synchronous since
282 	 * the old block values may have been written to disk. In practise
283 	 * they are almost never written, but if we are concerned about
284 	 * strict correctness, the `doasyncfree' flag should be set to zero.
285 	 *
286 	 * The test on `doasyncfree' should be changed to test a flag
287 	 * that shows whether the associated buffers and inodes have
288 	 * been written. The flag should be set when the cluster is
289 	 * started and cleared whenever the buffer or inode is flushed.
290 	 * We can then check below to see if it is set, and do the
291 	 * synchronous write only when it has been cleared.
292 	 */
293 	if (sbap != &ip->i_db[0]) {
294 		if (doasyncfree)
295 			bdwrite(sbp);
296 		else
297 			bwrite(sbp);
298 	} else {
299 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
300 		if (!doasyncfree)
301 			ext2_update(vp, 1);
302 	}
303 	if (ssize < len) {
304 		if (doasyncfree)
305 			bdwrite(ebp);
306 		else
307 			bwrite(ebp);
308 	}
309 	/*
310 	 * Last, free the old blocks and assign the new blocks to the buffers.
311 	 */
312 #ifdef DEBUG
313 	printf("\n\tnew:");
314 #endif /* DEBUG */
315 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
316 		ext2_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
317 		    fs->e2fs_bsize);
318 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
319 #ifdef DEBUG
320 		printf(" %d,", blkno);
321 #endif /* DEBUG */
322 	}
323 #ifdef DEBUG
324 	printf("\n");
325 #endif /* DEBUG */
326 	return (0);
327 
328 fail:
329 	if (ssize < len)
330 		brelse(ebp);
331 	if (sbap != &ip->i_db[0])
332 		brelse(sbp);
333 	return (ENOSPC);
334 }
335 
336 /*
337  * Allocate an inode in the file system.
338  *
339  */
340 int
341 ext2_valloc(pvp, mode, cred, vpp)
342 	struct vnode *pvp;
343 	int mode;
344 	struct ucred *cred;
345 	struct vnode **vpp;
346 {
347 	struct inode *pip;
348 	struct m_ext2fs *fs;
349 	struct inode *ip;
350 	struct ext2mount *ump;
351 	ino_t ino, ipref;
352 	int i, error, cg;
353 
354 	*vpp = NULL;
355 	pip = VTOI(pvp);
356 	fs = pip->i_e2fs;
357 	ump = pip->i_ump;
358 
359 	EXT2_LOCK(ump);
360 	if (fs->e2fs->e2fs_ficount == 0)
361 		goto noinodes;
362 	/*
363 	 * If it is a directory then obtain a cylinder group based on
364 	 * ext2_dirpref else obtain it using ino_to_cg. The preferred inode is
365 	 * always the next inode.
366 	 */
367 	if((mode & IFMT) == IFDIR) {
368 		cg = ext2_dirpref(pip);
369 		if (fs->e2fs_contigdirs[cg] < 255)
370 			fs->e2fs_contigdirs[cg]++;
371 	} else {
372 		cg = ino_to_cg(fs, pip->i_number);
373 		if (fs->e2fs_contigdirs[cg] > 0)
374 			fs->e2fs_contigdirs[cg]--;
375 	}
376 	ipref = cg * fs->e2fs->e2fs_ipg + 1;
377 	ino = (ino_t)ext2_hashalloc(pip, cg, (long)ipref, mode, ext2_nodealloccg);
378 
379 	if (ino == 0)
380 		goto noinodes;
381 	error = VFS_VGET(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
382 	if (error) {
383 		ext2_vfree(pvp, ino, mode);
384 		return (error);
385 	}
386 	ip = VTOI(*vpp);
387 
388 	/*
389 	  the question is whether using VGET was such good idea at all -
390 	  Linux doesn't read the old inode in when it's allocating a
391 	  new one. I will set at least i_size & i_blocks the zero.
392 	*/
393 	ip->i_mode = 0;
394 	ip->i_size = 0;
395 	ip->i_blocks = 0;
396 	ip->i_flags = 0;
397         /* now we want to make sure that the block pointers are zeroed out */
398         for (i = 0; i < NDADDR; i++)
399                 ip->i_db[i] = 0;
400         for (i = 0; i < NIADDR; i++)
401                 ip->i_ib[i] = 0;
402 
403 	/*
404 	 * Set up a new generation number for this inode.
405 	 * XXX check if this makes sense in ext2
406 	 */
407 	if (ip->i_gen == 0 || ++ip->i_gen == 0)
408 		ip->i_gen = random() / 2 + 1;
409 /*
410 printf("ext2_valloc: allocated inode %d\n", ino);
411 */
412 	return (0);
413 noinodes:
414 	EXT2_UNLOCK(ump);
415 	ext2_fserr(fs, cred->cr_uid, "out of inodes");
416 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
417 	return (ENOSPC);
418 }
419 
420 /*
421  * Find a cylinder to place a directory.
422  *
423  * The policy implemented by this algorithm is to allocate a
424  * directory inode in the same cylinder group as its parent
425  * directory, but also to reserve space for its files inodes
426  * and data. Restrict the number of directories which may be
427  * allocated one after another in the same cylinder group
428  * without intervening allocation of files.
429  *
430  * If we allocate a first level directory then force allocation
431  * in another cylinder group.
432  *
433  */
434 static u_long
435 ext2_dirpref(struct inode *pip)
436 {
437 	struct m_ext2fs *fs;
438         int cg, prefcg, dirsize, cgsize;
439 	int avgifree, avgbfree, avgndir, curdirsize;
440 	int minifree, minbfree, maxndir;
441 	int mincg, minndir;
442 	int maxcontigdirs;
443 
444 	mtx_assert(EXT2_MTX(pip->i_ump), MA_OWNED);
445 	fs = pip->i_e2fs;
446 
447  	avgifree = fs->e2fs->e2fs_ficount / fs->e2fs_gcount;
448 	avgbfree = fs->e2fs->e2fs_fbcount / fs->e2fs_gcount;
449 	avgndir  = fs->e2fs_total_dir / fs->e2fs_gcount;
450 
451 	/*
452 	 * Force allocation in another cg if creating a first level dir.
453 	 */
454 	ASSERT_VOP_LOCKED(ITOV(pip), "ext2fs_dirpref");
455 	if (ITOV(pip)->v_vflag & VV_ROOT) {
456 		prefcg = arc4random() % fs->e2fs_gcount;
457 		mincg = prefcg;
458 		minndir = fs->e2fs_ipg;
459 		for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
460 			if (fs->e2fs_gd[cg].ext2bgd_ndirs < minndir &&
461 			    fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree &&
462 			    fs->e2fs_gd[cg].ext2bgd_nbfree >= avgbfree) {
463 				mincg = cg;
464 				minndir = fs->e2fs_gd[cg].ext2bgd_ndirs;
465 			}
466 		for (cg = 0; cg < prefcg; 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 
474 		return (mincg);
475 	}
476 
477 	/*
478 	 * Count various limits which used for
479 	 * optimal allocation of a directory inode.
480 	 */
481 	maxndir = min(avgndir + fs->e2fs_ipg / 16, fs->e2fs_ipg);
482 	minifree = avgifree - avgifree / 4;
483 	if (minifree < 1)
484 		minifree = 1;
485 	minbfree = avgbfree - avgbfree / 4;
486 	if (minbfree < 1)
487 		minbfree = 1;
488 	cgsize = fs->e2fs_fsize * fs->e2fs_fpg;
489 	dirsize = AVGDIRSIZE;
490 	curdirsize = avgndir ? (cgsize - avgbfree * fs->e2fs_bsize) / avgndir : 0;
491 	if (dirsize < curdirsize)
492 		dirsize = curdirsize;
493 	if (dirsize <= 0)
494 		maxcontigdirs = 0;		/* dirsize overflowed */
495 	else
496 		maxcontigdirs = min((avgbfree * fs->e2fs_bsize) / dirsize, 255);
497 	maxcontigdirs = min(maxcontigdirs, fs->e2fs_ipg / AFPDIR);
498 	if (maxcontigdirs == 0)
499 		maxcontigdirs = 1;
500 
501 	/*
502 	 * Limit number of dirs in one cg and reserve space for
503 	 * regular files, but only if we have no deficit in
504 	 * inodes or space.
505 	 */
506 	prefcg = ino_to_cg(fs, pip->i_number);
507 	for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
508 		if (fs->e2fs_gd[cg].ext2bgd_ndirs < maxndir &&
509 		    fs->e2fs_gd[cg].ext2bgd_nifree >= minifree &&
510 	    	    fs->e2fs_gd[cg].ext2bgd_nbfree >= minbfree) {
511 			if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
512 				return (cg);
513 		}
514 	for (cg = 0; cg < prefcg; cg++)
515 		if (fs->e2fs_gd[cg].ext2bgd_ndirs < maxndir &&
516 		    fs->e2fs_gd[cg].ext2bgd_nifree >= minifree &&
517 	    	    fs->e2fs_gd[cg].ext2bgd_nbfree >= minbfree) {
518 			if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
519 				return (cg);
520 		}
521 	/*
522 	 * This is a backstop when we have deficit in space.
523 	 */
524 	for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
525 		if (fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree)
526 			return (cg);
527 	for (cg = 0; cg < prefcg; cg++)
528 		if (fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree)
529 			break;
530 	return (cg);
531 }
532 
533 /*
534  * Select the desired position for the next block in a file.
535  *
536  * we try to mimic what Remy does in inode_getblk/block_getblk
537  *
538  * we note: blocknr == 0 means that we're about to allocate either
539  * a direct block or a pointer block at the first level of indirection
540  * (In other words, stuff that will go in i_db[] or i_ib[])
541  *
542  * blocknr != 0 means that we're allocating a block that is none
543  * of the above. Then, blocknr tells us the number of the block
544  * that will hold the pointer
545  */
546 int32_t
547 ext2_blkpref(ip, lbn, indx, bap, blocknr)
548 	struct inode *ip;
549 	int32_t lbn;
550 	int indx;
551 	int32_t *bap;
552 	int32_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 			(int32_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 DIAGNOSTIC
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 	bp->b_xflags |= BX_BKGRDWRITE;
796 
797 	EXT2_LOCK(ump);
798 	/*
799 	 * Check to see if a cluster of the needed size (or bigger) is
800 	 * available in this cylinder group.
801 	 */
802 	lp = &fs->e2fs_clustersum[cg].cs_sum[len];
803 	for (i = len; i <= fs->e2fs_contigsumsize; i++)
804 		if (*lp++ > 0)
805 			break;
806 	if (i > fs->e2fs_contigsumsize) {
807 		/*
808 		 * Update the cluster summary information to reflect
809 		 * the true maximum-sized cluster so that future cluster
810 		 * allocation requests can avoid reading the bitmap only
811 		 * to find no cluster.
812 		 */
813 		lp = &fs->e2fs_clustersum[cg].cs_sum[len - 1];
814 			for (i = len - 1; i > 0; i--)
815 				if (*lp-- > 0)
816 					break;
817 		fs->e2fs_maxcluster[cg] = i;
818 		goto fail;
819 	}
820 	EXT2_UNLOCK(ump);
821 
822 	/* Search the bitmap to find a big enough cluster like in FFS. */
823 	if (dtog(fs, bpref) != cg)
824 		bpref = 0;
825 	if (bpref != 0)
826 		bpref = dtogd(fs, bpref);
827 	loc = bpref / NBBY;
828 	bit = 1 << (bpref % NBBY);
829 	for (run = 0, got = bpref; got < fs->e2fs->e2fs_fpg; got++) {
830 		if ((bbp[loc] & bit) != 0)
831 			run = 0;
832 		else {
833 			run++;
834 			if (run == len)
835 				break;
836 		}
837 		if ((got & (NBBY - 1)) != (NBBY - 1))
838 			bit <<= 1;
839 		else {
840 			loc++;
841 			bit = 1;
842 		}
843 	}
844 
845 	if (got >= fs->e2fs->e2fs_fpg)
846 		goto fail_lock;
847 
848 	/* Allocate the cluster that we found. */
849 	for (i = 1; i < len; i++)
850 		if (!isclr(bbp, got - run + i))
851 			panic("ext2_clusteralloc: map mismatch");
852 
853 	bno = got - run + 1;
854 	if (bno >= fs->e2fs->e2fs_fpg)
855 		panic("ext2_clusteralloc: allocated out of group");
856 
857 	EXT2_LOCK(ump);
858 	for (i = 0; i < len; i += fs->e2fs_fpb) {
859 		setbit(bbp, bno + i);
860 		ext2_clusteracct(fs, bbp, cg, bno + i, -1);
861 		fs->e2fs->e2fs_fbcount--;
862 		fs->e2fs_gd[cg].ext2bgd_nbfree--;
863 	}
864 	fs->e2fs_fmod = 1;
865 	EXT2_UNLOCK(ump);
866 
867 	bdwrite(bp);
868 	return (cg * fs->e2fs->e2fs_fpg + fs->e2fs->e2fs_first_dblock + bno);
869 
870 fail_lock:
871 	EXT2_LOCK(ump);
872 fail:
873 	brelse(bp);
874 	return (0);
875 }
876 
877 /*
878  * Determine whether an inode can be allocated.
879  *
880  * Check to see if an inode is available, and if it is,
881  * allocate it using tode in the specified cylinder group.
882  */
883 static daddr_t
884 ext2_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
885 {
886 	struct m_ext2fs *fs;
887 	struct buf *bp;
888 	struct ext2mount *ump;
889 	int error, start, len, loc, map, i;
890 	char *ibp;
891 	ipref--; /* to avoid a lot of (ipref -1) */
892 	if (ipref == -1)
893 		ipref = 0;
894 	fs = ip->i_e2fs;
895 	ump = ip->i_ump;
896 	if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
897 		return (0);
898 	EXT2_UNLOCK(ump);
899 	error = bread(ip->i_devvp, fsbtodb(fs,
900 		fs->e2fs_gd[cg].ext2bgd_i_bitmap),
901 		(int)fs->e2fs_bsize, NOCRED, &bp);
902 	if (error) {
903 		brelse(bp);
904 		EXT2_LOCK(ump);
905 		return (0);
906 	}
907 	if (fs->e2fs_gd[cg].ext2bgd_nifree == 0) {
908 		/*
909 		 * Another thread allocated the last i-node in this
910 		 * group while we were waiting for the buffer.
911 		 */
912 		brelse(bp);
913 		EXT2_LOCK(ump);
914 		return (0);
915 	}
916 	ibp = (char *)bp->b_data;
917 	if (ipref) {
918 		ipref %= fs->e2fs->e2fs_ipg;
919 		if (isclr(ibp, ipref))
920 			goto gotit;
921 	}
922 	start = ipref / NBBY;
923 	len = howmany(fs->e2fs->e2fs_ipg - ipref, NBBY);
924 	loc = skpc(0xff, len, &ibp[start]);
925 	if (loc == 0) {
926 		len = start + 1;
927 		start = 0;
928 		loc = skpc(0xff, len, &ibp[0]);
929 		if (loc == 0) {
930 			printf("cg = %d, ipref = %lld, fs = %s\n",
931 				cg, (long long)ipref, fs->e2fs_fsmnt);
932 			panic("ext2fs_nodealloccg: map corrupted");
933 			/* NOTREACHED */
934 		}
935 	}
936 	i = start + len - loc;
937 	map = ibp[i] ^ 0xff;
938 	if (map == 0) {
939 		printf("fs = %s\n", fs->e2fs_fsmnt);
940 		panic("ext2fs_nodealloccg: block not in map");
941 	}
942 	ipref = i * NBBY + ffs(map) - 1;
943 gotit:
944 	setbit(ibp, ipref);
945 	EXT2_LOCK(ump);
946 	fs->e2fs_gd[cg].ext2bgd_nifree--;
947 	fs->e2fs->e2fs_ficount--;
948 	fs->e2fs_fmod = 1;
949 	if ((mode & IFMT) == IFDIR) {
950 		fs->e2fs_gd[cg].ext2bgd_ndirs++;
951 		fs->e2fs_total_dir++;
952 	}
953 	EXT2_UNLOCK(ump);
954 	bdwrite(bp);
955 	return (cg * fs->e2fs->e2fs_ipg + ipref +1);
956 }
957 
958 /*
959  * Free a block or fragment.
960  *
961  */
962 void
963 ext2_blkfree(ip, bno, size)
964 	struct inode *ip;
965 	int32_t bno;
966 	long size;
967 {
968 	struct m_ext2fs *fs;
969 	struct buf *bp;
970 	struct ext2mount *ump;
971 	int cg, error;
972 	char *bbp;
973 
974 	fs = ip->i_e2fs;
975 	ump = ip->i_ump;
976 	cg = dtog(fs, bno);
977 	if ((u_int)bno >= fs->e2fs->e2fs_bcount) {
978                 printf("bad block %lld, ino %llu\n", (long long)bno,
979                     (unsigned long long)ip->i_number);
980                 ext2_fserr(fs, ip->i_uid, "bad block");
981                 return;
982         }
983         error = bread(ip->i_devvp,
984                 fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
985                 (int)fs->e2fs_bsize, NOCRED, &bp);
986         if (error) {
987                 brelse(bp);
988                 return;
989         }
990         bbp = (char *)bp->b_data;
991         bno = dtogd(fs, bno);
992         if (isclr(bbp, bno)) {
993                 printf("block = %lld, fs = %s\n",
994                      (long long)bno, fs->e2fs_fsmnt);
995                 panic("blkfree: freeing free block");
996         }
997         clrbit(bbp, bno);
998 	EXT2_LOCK(ump);
999 	ext2_clusteracct(fs, bbp, cg, bno, 1);
1000         fs->e2fs->e2fs_fbcount++;
1001         fs->e2fs_gd[cg].ext2bgd_nbfree++;
1002         fs->e2fs_fmod = 1;
1003 	EXT2_UNLOCK(ump);
1004         bdwrite(bp);
1005 }
1006 
1007 /*
1008  * Free an inode.
1009  *
1010  */
1011 int
1012 ext2_vfree(pvp, ino, mode)
1013 	struct vnode *pvp;
1014 	ino_t ino;
1015 	int mode;
1016 {
1017 	struct m_ext2fs *fs;
1018 	struct inode *pip;
1019 	struct buf *bp;
1020 	struct ext2mount *ump;
1021 	int error, cg;
1022 	char * ibp;
1023 /*	mode_t save_i_mode; */
1024 
1025 	pip = VTOI(pvp);
1026 	fs = pip->i_e2fs;
1027 	ump = pip->i_ump;
1028 	if ((u_int)ino > fs->e2fs_ipg * fs->e2fs_gcount)
1029 		panic("ext2_vfree: range: devvp = %p, ino = %d, fs = %s",
1030 		    pip->i_devvp, ino, fs->e2fs_fsmnt);
1031 
1032 	cg = ino_to_cg(fs, ino);
1033 	error = bread(pip->i_devvp,
1034 		fsbtodb(fs, fs->e2fs_gd[cg].ext2bgd_i_bitmap),
1035 		(int)fs->e2fs_bsize, NOCRED, &bp);
1036 	if (error) {
1037 		brelse(bp);
1038 		return (0);
1039 	}
1040 	ibp = (char *)bp->b_data;
1041 	ino = (ino - 1) % fs->e2fs->e2fs_ipg;
1042 	if (isclr(ibp, ino)) {
1043 		printf("ino = %llu, fs = %s\n",
1044 			 (unsigned long long)ino, fs->e2fs_fsmnt);
1045 		if (fs->e2fs_ronly == 0)
1046 			panic("ifree: freeing free inode");
1047 	}
1048 	clrbit(ibp, ino);
1049 	EXT2_LOCK(ump);
1050 	fs->e2fs->e2fs_ficount++;
1051 	fs->e2fs_gd[cg].ext2bgd_nifree++;
1052 	if ((mode & IFMT) == IFDIR) {
1053 		fs->e2fs_gd[cg].ext2bgd_ndirs--;
1054 		fs->e2fs_total_dir--;
1055 	}
1056 	fs->e2fs_fmod = 1;
1057 	EXT2_UNLOCK(ump);
1058 	bdwrite(bp);
1059 	return (0);
1060 }
1061 
1062 /*
1063  * Find a block in the specified cylinder group.
1064  *
1065  * It is a panic if a request is made to find a block if none are
1066  * available.
1067  */
1068 static daddr_t
1069 ext2_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
1070 {
1071 	int start, len, loc, i, map;
1072 
1073 	/*
1074 	 * find the fragment by searching through the free block
1075 	 * map for an appropriate bit pattern
1076 	 */
1077 	if (bpref)
1078 		start = dtogd(fs, bpref) / NBBY;
1079 	else
1080 		start = 0;
1081 	len = howmany(fs->e2fs->e2fs_fpg, NBBY) - start;
1082 	loc = skpc(0xff, len, &bbp[start]);
1083 	if (loc == 0) {
1084 		len = start + 1;
1085 		start = 0;
1086 		loc = skpc(0xff, len, &bbp[start]);
1087 		if (loc == 0) {
1088 			printf("start = %d, len = %d, fs = %s\n",
1089 				start, len, fs->e2fs_fsmnt);
1090 			panic("ext2fs_alloccg: map corrupted");
1091 			/* NOTREACHED */
1092 		}
1093 	}
1094 	i = start + len - loc;
1095 	map = bbp[i] ^ 0xff;
1096 	if (map == 0) {
1097 		printf("fs = %s\n", fs->e2fs_fsmnt);
1098 		panic("ext2fs_mapsearch: block not in map");
1099 	}
1100 	return (i * NBBY + ffs(map) - 1);
1101 }
1102 
1103 /*
1104  * Fserr prints the name of a file system with an error diagnostic.
1105  *
1106  * The form of the error message is:
1107  *	fs: error message
1108  */
1109 static void
1110 ext2_fserr(fs, uid, cp)
1111 	struct m_ext2fs *fs;
1112 	uid_t uid;
1113 	char *cp;
1114 {
1115 
1116 	log(LOG_ERR, "uid %u on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
1117 }
1118 
1119 int
1120 cg_has_sb(int i)
1121 {
1122         int a3, a5, a7;
1123 
1124         if (i == 0 || i == 1)
1125                 return 1;
1126         for (a3 = 3, a5 = 5, a7 = 7;
1127             a3 <= i || a5 <= i || a7 <= i;
1128             a3 *= 3, a5 *= 5, a7 *= 7)
1129                 if (i == a3 || i == a5 || i == a7)
1130                         return 1;
1131         return 0;
1132 }
1133